Update V8 to version 4.7.51.
[chromium-blink-merge.git] / media / base / android / media_codec_player.h
blobedc17da00145944f6d1c3204423ddbebb5f6b9a2
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)
29 // | | |
30 // | v v
31 // | <------------------[ WaitingForConfig ] [ Error ]
32 // |
33 // |
34 // | <----------------------------------------------
35 // v |
36 // [ Prefetching ] ------------------- |
37 // | | |
38 // | v |
39 // | <-----------------[ WaitingForSurface ] |
40 // v |
41 // [ Playing ] |
42 // | |
43 // | |
44 // v |
45 // [ Stopping ] --------------------------------> [ WaitingForSeek ]
48 // Events and actions for pause/resume workflow.
49 // ---------------------------------------------
51 // Start, no config:
52 // ------------------------> [ Paused ] -----------------> [ Waiting ]
53 // | StopDone: [ for configs ]
54 // | ^ | /
55 // | | | /
56 // | Pause: | | Start w/config: /
57 // | | | dec.Prefetch /
58 // | | | /
59 // | | | /
60 // | | | /
61 // | | | / DemuxerConfigs:
62 // | | | / dec.Prefetch
63 // | | | /
64 // | | | /
65 // | | v /
66 // | /
67 // | ------------------> [ Prefetching ] <--------/ [ Waiting ]
68 // | | [ ] --------------> [ for surface ]
69 // | | | PrefetchDone, /
70 // | | | no surface: /
71 // | | | /
72 // | | | /
73 // | | StopDone w/ | /
74 // | | pending start: | PrefetchDone: /
75 // | | dec.Prefetch | dec.Start /
76 // | | | / SetSurface:
77 // | | | / dec.Start
78 // | | | /
79 // | | v /
80 // | | /
81 // | | [ Playing ] <----------/
82 // | |
83 // | | |
84 // | | |
85 // | | | Pause: dec.RequestToStop
86 // | | |
87 // | | |
88 // | | v
89 // | |
90 // ------------------------- [ Stopping ]
93 // Events and actions for seek workflow.
94 // -------------------------------------
96 // Seek: -- --
97 // demuxer.RequestSeek | |
98 // [ Paused ] -----------------------> | |
99 // [ ] <----------------------- | |--
100 // SeekDone: | | |
101 // | | |
102 // | | |
103 // | | |
104 // | | | Start:
105 // Seek: | | | SetPendingStart
106 // dec.Stop | | |
107 // SetPendingSeek | | |
108 // demuxer.RequestSeek | | |
109 // [ Prefetching ] -----------------------> | | |
110 // [ ] <---------------------- | | | Pause:
111 // | SeekDone | | | RemovePendingStart
112 // | w/pending start: | | |
113 // | dec.Prefetch | Waiting | |
114 // | | for | | Seek:
115 // | | seek | | SetPendingSeek
116 // | | | |
117 // | PrefetchDone: dec.Start | | |
118 // | | | | SeekDone
119 // v | | | w/pending seek:
120 // | | | demuxer.RequestSeek
121 // [ Playing ] | | |
122 // | | |
123 // | | |<-
124 // | Seek: SetPendingStart | |
125 // | SetPendingSeek | |
126 // | dec.RequestToStop | |
127 // | | |
128 // | | |
129 // v | |
130 // | |
131 // [ Stopping ] -----------------------> | |
132 // StopDone -- --
133 // w/pending seek:
134 // demuxer.RequestSeek
136 namespace media {
138 class BrowserCdm;
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 {
147 public:
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)>
153 TimeUpdateCallback;
155 typedef base::Callback<void(const base::TimeDelta& current_timestamp)>
156 SeekDoneCallback;
158 typedef base::Callback<void(int)> ErrorCallback;
160 // For testing only.
161 typedef base::Callback<void(DemuxerStream::Type,
162 base::TimeDelta,
163 base::TimeDelta)> DecodersTimeCallback;
165 // For testing only.
166 typedef base::Callback<void(DemuxerStream::Type)> CodecCreatedCallback;
168 // Constructs a player with the given ID and demuxer. |manager| must outlive
169 // the lifetime of this object.
170 MediaCodecPlayer(int player_id,
171 base::WeakPtr<MediaPlayerManager> manager,
172 const RequestMediaResourcesCB& request_media_resources_cb,
173 scoped_ptr<DemuxerAndroid> demuxer,
174 const GURL& frame_url);
175 ~MediaCodecPlayer() override;
177 // A helper method that performs the media thread part of initialization.
178 void Initialize();
180 // MediaPlayerAndroid implementation.
181 void DeleteOnCorrectThread() override;
182 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
183 void Start() override;
184 void Pause(bool is_media_related_action) override;
185 void SeekTo(base::TimeDelta timestamp) override;
186 void Release() override;
187 void SetVolume(double volume) override;
188 int GetVideoWidth() override;
189 int GetVideoHeight() override;
190 base::TimeDelta GetCurrentTime() override;
191 base::TimeDelta GetDuration() override;
192 bool IsPlaying() override;
193 bool CanPause() override;
194 bool CanSeekForward() override;
195 bool CanSeekBackward() override;
196 bool IsPlayerReady() override;
197 void SetCdm(BrowserCdm* cdm) override;
199 // DemuxerAndroidClient implementation.
200 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
201 void OnDemuxerDataAvailable(const DemuxerData& params) override;
202 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
203 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
205 // For testing only.
206 void SetDecodersTimeCallbackForTests(DecodersTimeCallback cb);
207 void SetCodecCreatedCallbackForTests(CodecCreatedCallback cb);
208 void SetAlwaysReconfigureForTests(DemuxerStream::Type type);
209 bool IsPrerollingForTests(DemuxerStream::Type type) const;
211 private:
212 // The state machine states.
213 enum PlayerState {
214 kStatePaused,
215 kStateWaitingForConfig,
216 kStatePrefetching,
217 kStatePlaying,
218 kStateStopping,
219 kStateWaitingForSurface,
220 kStateWaitingForSeek,
221 kStateError,
224 enum StartStatus {
225 kStartOk = 0,
226 kStartBrowserSeekRequired,
227 kStartFailed,
230 // Cached values for the manager.
231 struct MediaMetadata {
232 base::TimeDelta duration;
233 gfx::Size video_size;
236 // Information about current seek in progress.
237 struct SeekInfo {
238 const base::TimeDelta seek_time;
239 const bool is_browser_seek;
240 SeekInfo(base::TimeDelta time, bool browser_seek)
241 : seek_time(time), is_browser_seek(browser_seek) {}
244 // MediaPlayerAndroid implementation.
245 // This method caches the data and calls manager's OnMediaMetadataChanged().
246 void OnMediaMetadataChanged(base::TimeDelta duration,
247 const gfx::Size& video_size) override;
249 // This method caches the current time and calls manager's OnTimeUpdate().
250 void OnTimeUpdate(base::TimeDelta current_timestamp,
251 base::TimeTicks current_time_ticks) override;
253 // Callbacks from decoders
254 void RequestDemuxerData(DemuxerStream::Type stream_type);
255 void OnPrefetchDone();
256 void OnPrerollDone();
257 void OnDecoderDrained(DemuxerStream::Type type);
258 void OnStopDone(DemuxerStream::Type type);
259 void OnError();
260 void OnStarvation(DemuxerStream::Type stream_type);
261 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type,
262 base::TimeDelta now_playing,
263 base::TimeDelta last_buffered,
264 bool postpone);
266 // Callbacks from video decoder
267 void OnVideoCodecCreated();
268 void OnVideoResolutionChanged(const gfx::Size& size);
270 // Operations called from the state machine.
271 void SetState(PlayerState new_state);
272 void SetPendingStart(bool need_to_start);
273 bool HasPendingStart() const;
274 void SetPendingSeek(base::TimeDelta timestamp);
275 base::TimeDelta GetPendingSeek() const;
276 bool HasVideo() const;
277 bool HasAudio() const;
278 void SetDemuxerConfigs(const DemuxerConfigs& configs);
279 void StartPrefetchDecoders();
280 void StartPlaybackOrBrowserSeek();
281 StartStatus StartPlaybackDecoders();
282 StartStatus ConfigureDecoders();
283 StartStatus MaybePrerollDecoders(bool* preroll_required);
284 StartStatus StartDecoders();
285 void StopDecoders();
286 void RequestToStopDecoders();
287 void RequestDemuxerSeek(base::TimeDelta seek_time,
288 bool is_browser_seek = false);
289 void ReleaseDecoderResources();
291 // Helper methods.
292 void CreateDecoders();
293 bool AudioFinished() const;
294 bool VideoFinished() const;
295 base::TimeDelta GetInterpolatedTime();
297 static const char* AsString(PlayerState state);
299 // Data.
301 // Object for posting tasks on UI thread.
302 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
304 // Major components: demuxer, audio and video decoders.
305 scoped_ptr<DemuxerAndroid> demuxer_;
306 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_;
307 scoped_ptr<MediaCodecVideoDecoder> video_decoder_;
309 // The state of the state machine.
310 PlayerState state_;
312 // Notification callbacks, they call MediaPlayerManager.
313 base::Closure request_resources_cb_;
314 TimeUpdateCallback time_update_cb_;
315 base::Closure completion_cb_;
316 SeekDoneCallback seek_done_cb_;
317 ErrorCallback error_cb_;
319 // A callback that updates metadata cache and calls the manager.
320 MetadataChangedCallback metadata_changed_cb_;
322 // We call the base class' AttachListener() and DetachListener() methods on UI
323 // thread with these callbacks.
324 base::Closure attach_listener_cb_;
325 base::Closure detach_listener_cb_;
327 // Error callback is posted by decoders or by this class itself if we cannot
328 // configure or start decoder.
329 base::Closure internal_error_cb_;
331 // Total duration reported by demuxer.
332 base::TimeDelta duration_;
334 // base::TickClock used by |interpolator_|.
335 base::DefaultTickClock default_tick_clock_;
337 // Tracks the most recent media time update and provides interpolated values
338 // as playback progresses.
339 TimeDeltaInterpolator interpolator_;
341 // Pending data to be picked up by the upcoming state.
342 gfx::ScopedJavaSurface pending_surface_;
343 bool pending_start_;
344 base::TimeDelta pending_seek_;
346 // Data associated with a seek in progress.
347 scoped_ptr<SeekInfo> seek_info_;
349 // Configuration data for the manager, accessed on the UI thread.
350 MediaMetadata metadata_cache_;
352 // Cached current time, accessed on UI thread.
353 base::TimeDelta current_time_cache_;
355 // For testing only.
356 DecodersTimeCallback decoders_time_cb_;
358 base::WeakPtr<MediaCodecPlayer> media_weak_this_;
359 // NOTE: Weak pointers must be invalidated before all other member variables.
360 base::WeakPtrFactory<MediaCodecPlayer> media_weak_factory_;
362 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer);
365 } // namespace media
367 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_