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 // [ Prefetching ] ------------------- |
39 // | <-----------------[ WaitingForSurface ] |
45 // [ Stopping ] --------------------------------> [ WaitingForSeek ]
48 // Events and actions for pause/resume workflow.
49 // ---------------------------------------------
52 // ------------------------> [ Paused ] -----------------> [ Waiting ]
53 // | StopDone: [ for configs ]
56 // | Pause: | | Start w/config: /
57 // | | | dec.Prefetch /
61 // | | | / DemuxerConfigs:
62 // | | | / dec.Prefetch
67 // | ------------------> [ Prefetching ] <--------/ [ Waiting ]
68 // | | [ ] --------------> [ for surface ]
69 // | | | PrefetchDone, /
70 // | | | no surface: /
73 // | | StopDone w/ | /
74 // | | pending start: | PrefetchDone: /
75 // | | dec.Prefetch | dec.Start /
76 // | | | / SetSurface:
81 // | | [ Playing ] <----------/
85 // | | | Pause: dec.RequestToStop
90 // ------------------------- [ Stopping ]
93 // Events and actions for seek workflow.
94 // -------------------------------------
97 // demuxer.RequestSeek | |
98 // [ Paused ] -----------------------> | |
99 // [ ] <----------------------- | |--
105 // Seek: | | | SetPendingStart
107 // SetPendingSeek | | |
108 // demuxer.RequestSeek | | |
109 // [ Prefetching ] -----------------------> | | |
110 // [ ] <---------------------- | | | Pause:
111 // | SeekDone | | | RemovePendingStart
112 // | w/pending start: | | |
113 // | dec.Prefetch | Waiting | |
115 // | | seek | | SetPendingSeek
117 // | PrefetchDone: dec.Start | | |
119 // v | | | w/pending seek:
120 // | | | demuxer.RequestSeek
124 // | Seek: SetPendingStart | |
125 // | SetPendingSeek | |
126 // | dec.RequestToStop | |
131 // [ Stopping ] -----------------------> | |
134 // demuxer.RequestSeek
139 class MediaCodecAudioDecoder
;
140 class MediaCodecVideoDecoder
;
142 // Returns the task runner for the media thread
143 MEDIA_EXPORT scoped_refptr
<base::SingleThreadTaskRunner
> GetMediaTaskRunner();
145 class MEDIA_EXPORT MediaCodecPlayer
: public MediaPlayerAndroid
,
146 public DemuxerAndroidClient
{
148 // Typedefs for the notification callbacks
149 typedef base::Callback
<void(base::TimeDelta
, const gfx::Size
&)>
150 MetadataChangedCallback
;
152 typedef base::Callback
<void(base::TimeDelta
, base::TimeTicks
)>
155 typedef base::Callback
<void(const base::TimeDelta
& current_timestamp
)>
158 // Constructs a player with the given ID and demuxer. |manager| must outlive
159 // the lifetime of this object.
160 MediaCodecPlayer(int player_id
,
161 base::WeakPtr
<MediaPlayerManager
> manager
,
162 const RequestMediaResourcesCB
& request_media_resources_cb
,
163 scoped_ptr
<DemuxerAndroid
> demuxer
,
164 const GURL
& frame_url
);
165 ~MediaCodecPlayer() override
;
167 // A helper method that performs the media thread part of initialization.
170 // MediaPlayerAndroid implementation.
171 void DeleteOnCorrectThread() override
;
172 void SetVideoSurface(gfx::ScopedJavaSurface surface
) override
;
173 void Start() override
;
174 void Pause(bool is_media_related_action
) override
;
175 void SeekTo(base::TimeDelta timestamp
) override
;
176 void Release() override
;
177 void SetVolume(double volume
) override
;
178 int GetVideoWidth() override
;
179 int GetVideoHeight() override
;
180 base::TimeDelta
GetCurrentTime() override
;
181 base::TimeDelta
GetDuration() override
;
182 bool IsPlaying() override
;
183 bool CanPause() override
;
184 bool CanSeekForward() override
;
185 bool CanSeekBackward() override
;
186 bool IsPlayerReady() override
;
187 void SetCdm(BrowserCdm
* cdm
) override
;
189 // DemuxerAndroidClient implementation.
190 void OnDemuxerConfigsAvailable(const DemuxerConfigs
& params
) override
;
191 void OnDemuxerDataAvailable(const DemuxerData
& params
) override
;
192 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time
) override
;
193 void OnDemuxerDurationChanged(base::TimeDelta duration
) override
;
196 // The state machine states.
199 STATE_WAITING_FOR_CONFIG
,
203 STATE_WAITING_FOR_SURFACE
,
204 STATE_WAITING_FOR_SEEK
,
208 // Cached values for the manager.
209 struct MediaMetadata
{
210 base::TimeDelta duration
;
211 gfx::Size video_size
;
214 // Information about current seek in progress.
216 const base::TimeDelta seek_time
;
217 const bool is_browser_seek
;
218 SeekInfo(base::TimeDelta time
, bool browser_seek
)
219 : seek_time(time
), is_browser_seek(browser_seek
) {}
222 // MediaPlayerAndroid implementation.
223 // This method caches the data and calls manager's OnMediaMetadataChanged().
224 void OnMediaMetadataChanged(base::TimeDelta duration
,
225 const gfx::Size
& video_size
) override
;
227 // This method caches the current time and calls manager's OnTimeUpdate().
228 void OnTimeUpdate(base::TimeDelta current_timestamp
,
229 base::TimeTicks current_time_ticks
) override
;
231 // Callbacks from decoders
232 void RequestDemuxerData(DemuxerStream::Type stream_type
);
233 void OnPrefetchDone();
236 void OnStarvation(DemuxerStream::Type stream_type
);
237 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type
,
238 base::TimeDelta now_playing
,
239 base::TimeDelta last_buffered
);
241 // Callbacks from video decoder
242 void OnVideoCodecCreated();
243 void OnVideoResolutionChanged(const gfx::Size
& size
);
245 // Operations called from the state machine.
246 void SetState(PlayerState new_state
);
247 void SetPendingSurface(gfx::ScopedJavaSurface surface
);
248 bool HasPendingSurface() const;
249 void SetPendingStart(bool need_to_start
);
250 bool HasPendingStart() const;
251 void SetPendingSeek(base::TimeDelta timestamp
);
252 base::TimeDelta
GetPendingSeek() const;
253 bool HasVideo() const;
254 bool HasAudio() const;
255 void SetDemuxerConfigs(const DemuxerConfigs
& configs
);
256 void StartPrefetchDecoders();
257 void StartPlaybackDecoders();
259 void RequestToStopDecoders();
260 void RequestDemuxerSeek(base::TimeDelta seek_time
,
261 bool is_browser_seek
= false);
262 void ReleaseDecoderResources();
265 void CreateDecoders();
266 bool AudioFinished() const;
267 bool VideoFinished() const;
268 base::TimeDelta
GetInterpolatedTime();
270 static const char* AsString(PlayerState state
);
274 // Object for posting tasks on UI thread.
275 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
277 // Major components: demuxer, audio and video decoders.
278 scoped_ptr
<DemuxerAndroid
> demuxer_
;
279 scoped_ptr
<MediaCodecAudioDecoder
> audio_decoder_
;
280 scoped_ptr
<MediaCodecVideoDecoder
> video_decoder_
;
282 // The state of the state machine.
285 // Notification callbacks, they call MediaPlayerManager.
286 base::Closure request_resources_cb_
;
287 TimeUpdateCallback time_update_cb_
;
288 base::Closure completion_cb_
;
289 SeekDoneCallback seek_done_cb_
;
291 // A callback that updates metadata cache and calls the manager.
292 MetadataChangedCallback metadata_changed_cb_
;
294 // We call the base class' AttachListener() and DetachListener() methods on UI
295 // thread with these callbacks.
296 base::Closure attach_listener_cb_
;
297 base::Closure detach_listener_cb_
;
299 // Error callback is posted by decoders or by this class itself if we cannot
300 // configure or start decoder.
301 base::Closure error_cb_
;
303 // Total duration reported by demuxer.
304 base::TimeDelta duration_
;
306 // base::TickClock used by |interpolator_|.
307 base::DefaultTickClock default_tick_clock_
;
309 // Tracks the most recent media time update and provides interpolated values
310 // as playback progresses.
311 TimeDeltaInterpolator interpolator_
;
313 // Pending data to be picked up by the upcoming state.
314 gfx::ScopedJavaSurface pending_surface_
;
316 base::TimeDelta pending_seek_
;
318 // Data associated with a seek in progress.
319 scoped_ptr
<SeekInfo
> seek_info_
;
321 // Configuration data for the manager, accessed on the UI thread.
322 MediaMetadata metadata_cache_
;
324 // Cached current time, accessed on UI thread.
325 base::TimeDelta current_time_cache_
;
327 base::WeakPtr
<MediaCodecPlayer
> media_weak_this_
;
328 // NOTE: Weak pointers must be invalidated before all other member variables.
329 base::WeakPtrFactory
<MediaCodecPlayer
> media_weak_factory_
;
331 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer
);
336 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_