1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/threading/thread.h"
12 #include "base/time/default_tick_clock.h"
13 #include "media/base/android/demuxer_android.h"
14 #include "media/base/android/media_player_android.h"
15 #include "media/base/demuxer_stream.h"
16 #include "media/base/media_export.h"
17 #include "media/base/time_delta_interpolator.h"
18 #include "ui/gfx/geometry/size.h"
19 #include "ui/gl/android/scoped_java_surface.h"
21 // The MediaCodecPlayer class implements the media player by using Android's
22 // MediaCodec. It differs from MediaSourcePlayer in that it removes most
23 // processing away from the UI thread: it uses a dedicated Media thread to
24 // receive the data and to handle the commands.
26 // The player works as a state machine. Here are relationships between states:
28 // [ Paused ] ------------------------ (Any state)
31 // | <------------------[ WaitingForConfig ] [ Error ]
34 // | <----------------------------------------------
36 // [ Waiting for permission ] |
40 // [ Prefetching ] ------------------- |
43 // | <-----------------[ WaitingForSurface ] |
49 // [ Stopping ] --------------------------------> [ WaitingForSeek ]
52 // Events and actions for pause/resume workflow.
53 // ---------------------------------------------
56 // ------------------------> [ Paused ] -----------------> [ Waiting ]
57 // | StopDone: [ for configs ]
60 // | Pause: | | Start w/config: /
61 // | Permission denied: | | requestPermission /
65 // | | | / DemuxerConfigs:
66 // | | | / requestPermission
71 // | ------------------> [ Waiting for ] <--------/
75 // | | | Permission granted:
81 // | | [ Prefetching ] [ Waiting ]
82 // | | [ ] --------------> [ for surface ]
83 // | | | PrefetchDone, /
84 // | | | no surface: /
87 // | | StopDone w/ | /
88 // | | pending start: | PrefetchDone: /
89 // | | dec.Prefetch | dec.Start /
90 // | | | / SetSurface:
95 // | | [ Playing ] <----------/
99 // | | | Pause: dec.RequestToStop
104 // ------------------------- [ Stopping ]
107 // Events and actions for seek workflow.
108 // -------------------------------------
111 // demuxer.RequestSeek | |
112 // [ Paused ] -----------------------> | |
113 // [ ] <----------------------- | |--
119 // | | | SetPendingStart
120 // Seek: dec.Stop | | |
121 // SetPendingStart | | |
122 // demuxer.RequestSeek | | |
123 // [ Waiting for ] -----------------------> | | |
124 // [ permission ] <---------------------- | | | Pause:
125 // SeekDone | | | RemovePendingStart
126 // | w/pending start: | | |
127 // | requestPermission | Waiting | |
129 // | | seek | | SetPendingSeek
131 // | Seek: dec.Stop | | |
132 // v SetPendingStart | | |
133 // demuxer.RequestSeek | | |
134 // [ Prefetching ] ----------------------> | | |
137 // | PrefetchDone: dec.Start | | |
139 // v | | | w/pending seek:
140 // | | | demuxer.RequestSeek
144 // | Seek: SetPendingStart | |
145 // | SetPendingSeek | |
146 // | dec.RequestToStop | |
151 // [ Stopping ] -----------------------> | |
154 // demuxer.RequestSeek
159 class MediaCodecAudioDecoder
;
160 class MediaCodecVideoDecoder
;
162 // Returns the task runner for the media thread
163 MEDIA_EXPORT scoped_refptr
<base::SingleThreadTaskRunner
> GetMediaTaskRunner();
165 class MEDIA_EXPORT MediaCodecPlayer
: public MediaPlayerAndroid
,
166 public DemuxerAndroidClient
{
168 // Typedefs for the notification callbacks
169 typedef base::Callback
<void(base::TimeDelta
, const gfx::Size
&)>
170 MetadataChangedCallback
;
172 typedef base::Callback
<void(base::TimeDelta
, base::TimeTicks
)>
175 typedef base::Callback
<void(const base::TimeDelta
& current_timestamp
)>
178 typedef base::Callback
<void(int)> ErrorCallback
;
181 typedef base::Callback
<void(DemuxerStream::Type
,
183 base::TimeDelta
)> DecodersTimeCallback
;
186 typedef base::Callback
<void(DemuxerStream::Type
)> CodecCreatedCallback
;
188 // Constructs a player with the given ID and demuxer. |manager| must outlive
189 // the lifetime of this object.
190 MediaCodecPlayer(int player_id
,
191 base::WeakPtr
<MediaPlayerManager
> manager
,
192 const RequestMediaResourcesCB
& request_media_resources_cb
,
193 scoped_ptr
<DemuxerAndroid
> demuxer
,
194 const GURL
& frame_url
);
195 ~MediaCodecPlayer() override
;
197 // A helper method that performs the media thread part of initialization.
200 // MediaPlayerAndroid implementation.
201 void DeleteOnCorrectThread() override
;
202 void SetVideoSurface(gfx::ScopedJavaSurface surface
) override
;
203 void Start() override
;
204 void Pause(bool is_media_related_action
) override
;
205 void SeekTo(base::TimeDelta timestamp
) override
;
206 void Release() override
;
207 void SetVolume(double volume
) override
;
208 int GetVideoWidth() override
;
209 int GetVideoHeight() override
;
210 base::TimeDelta
GetCurrentTime() override
;
211 base::TimeDelta
GetDuration() override
;
212 bool IsPlaying() override
;
213 bool CanPause() override
;
214 bool CanSeekForward() override
;
215 bool CanSeekBackward() override
;
216 bool IsPlayerReady() override
;
217 void SetCdm(BrowserCdm
* cdm
) override
;
219 // DemuxerAndroidClient implementation.
220 void OnDemuxerConfigsAvailable(const DemuxerConfigs
& params
) override
;
221 void OnDemuxerDataAvailable(const DemuxerData
& params
) override
;
222 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time
) override
;
223 void OnDemuxerDurationChanged(base::TimeDelta duration
) override
;
226 void SetDecodersTimeCallbackForTests(DecodersTimeCallback cb
);
227 void SetCodecCreatedCallbackForTests(CodecCreatedCallback cb
);
228 void SetAlwaysReconfigureForTests(DemuxerStream::Type type
);
229 bool IsPrerollingForTests(DemuxerStream::Type type
) const;
232 // The state machine states.
235 kStateWaitingForConfig
,
236 kStateWaitingForPermission
,
240 kStateWaitingForSurface
,
241 kStateWaitingForSeek
,
247 kStartBrowserSeekRequired
,
251 // Cached values for the manager.
252 struct MediaMetadata
{
253 base::TimeDelta duration
;
254 gfx::Size video_size
;
257 // Information about current seek in progress.
259 const base::TimeDelta seek_time
;
260 const bool is_browser_seek
;
261 SeekInfo(base::TimeDelta time
, bool browser_seek
)
262 : seek_time(time
), is_browser_seek(browser_seek
) {}
265 // MediaPlayerAndroid implementation.
267 // This method requests playback permission from the manager on UI thread,
268 // passing total duration as an argiment. The duration must be known by the
269 // time of the call. The method posts the result to the media thread.
270 void RequestPermissionAndPostResult(base::TimeDelta duration
) override
;
272 // This method caches the data and calls manager's OnMediaMetadataChanged().
273 void OnMediaMetadataChanged(base::TimeDelta duration
,
274 const gfx::Size
& video_size
) override
;
276 // This method caches the current time and calls manager's OnTimeUpdate().
277 void OnTimeUpdate(base::TimeDelta current_timestamp
,
278 base::TimeTicks current_time_ticks
) override
;
280 // Callback from manager
281 void OnPermissionDecided(bool granted
);
283 // Callbacks from decoders
284 void RequestDemuxerData(DemuxerStream::Type stream_type
);
285 void OnPrefetchDone();
286 void OnPrerollDone();
287 void OnDecoderDrained(DemuxerStream::Type type
);
288 void OnStopDone(DemuxerStream::Type type
);
290 void OnStarvation(DemuxerStream::Type stream_type
);
291 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type
,
292 base::TimeDelta now_playing
,
293 base::TimeDelta last_buffered
,
296 // Callbacks from video decoder
297 void OnVideoCodecCreated();
298 void OnVideoResolutionChanged(const gfx::Size
& size
);
300 // Operations called from the state machine.
301 void SetState(PlayerState new_state
);
302 void SetPendingStart(bool need_to_start
);
303 bool HasPendingStart() const;
304 void SetPendingSeek(base::TimeDelta timestamp
);
305 base::TimeDelta
GetPendingSeek() const;
306 bool HasVideo() const;
307 bool HasAudio() const;
308 void SetDemuxerConfigs(const DemuxerConfigs
& configs
);
309 void RequestPlayPermission();
310 void StartPrefetchDecoders();
311 void StartPlaybackOrBrowserSeek();
312 StartStatus
StartPlaybackDecoders();
313 StartStatus
ConfigureDecoders();
314 StartStatus
MaybePrerollDecoders(bool* preroll_required
);
315 StartStatus
StartDecoders();
317 void RequestToStopDecoders();
318 void RequestDemuxerSeek(base::TimeDelta seek_time
,
319 bool is_browser_seek
= false);
320 void ReleaseDecoderResources();
323 void CreateDecoders();
324 bool AudioFinished() const;
325 bool VideoFinished() const;
326 base::TimeDelta
GetInterpolatedTime();
328 static const char* AsString(PlayerState state
);
332 // Object for posting tasks on UI thread.
333 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
335 // Major components: demuxer, audio and video decoders.
336 scoped_ptr
<DemuxerAndroid
> demuxer_
;
337 scoped_ptr
<MediaCodecAudioDecoder
> audio_decoder_
;
338 scoped_ptr
<MediaCodecVideoDecoder
> video_decoder_
;
340 // The state of the state machine.
343 // Notification callbacks, they call MediaPlayerManager.
344 base::Closure request_resources_cb_
;
345 TimeUpdateCallback time_update_cb_
;
346 base::Closure completion_cb_
;
347 SeekDoneCallback seek_done_cb_
;
348 ErrorCallback error_cb_
;
350 // A callback that updates metadata cache and calls the manager.
351 MetadataChangedCallback metadata_changed_cb_
;
353 // We call the base class' AttachListener() and DetachListener() methods on UI
354 // thread with these callbacks.
355 base::Closure attach_listener_cb_
;
356 base::Closure detach_listener_cb_
;
358 // Error callback is posted by decoders or by this class itself if we cannot
359 // configure or start decoder.
360 base::Closure internal_error_cb_
;
362 // Total duration reported by demuxer.
363 base::TimeDelta duration_
;
365 // base::TickClock used by |interpolator_|.
366 base::DefaultTickClock default_tick_clock_
;
368 // Tracks the most recent media time update and provides interpolated values
369 // as playback progresses.
370 TimeDeltaInterpolator interpolator_
;
372 // Pending data to be picked up by the upcoming state.
373 gfx::ScopedJavaSurface pending_surface_
;
375 base::TimeDelta pending_seek_
;
377 // Data associated with a seek in progress.
378 scoped_ptr
<SeekInfo
> seek_info_
;
380 // Configuration data for the manager, accessed on the UI thread.
381 MediaMetadata metadata_cache_
;
383 // Cached current time, accessed on UI thread.
384 base::TimeDelta current_time_cache_
;
387 DecodersTimeCallback decoders_time_cb_
;
389 base::WeakPtr
<MediaCodecPlayer
> media_weak_this_
;
390 // NOTE: Weak pointers must be invalidated before all other member variables.
391 base::WeakPtrFactory
<MediaCodecPlayer
> media_weak_factory_
;
393 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer
);
398 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_