Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(514)

Side by Side Diff: media/base/android/media_codec_player.h

Issue 2276343005: Delete MediaCodecPlayer, it's time! (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
7
8 #include <memory>
9
10 #include "base/android/scoped_java_ref.h"
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread.h"
14 #include "base/time/default_tick_clock.h"
15 #include "media/base/android/demuxer_android.h"
16 #include "media/base/android/media_drm_bridge.h"
17 #include "media/base/android/media_player_android.h"
18 #include "media/base/android/media_statistics.h"
19 #include "media/base/demuxer_stream.h"
20 #include "media/base/media_export.h"
21 #include "media/base/time_delta_interpolator.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gl/android/scoped_java_surface.h"
24
25 // The MediaCodecPlayer class implements the media player by using Android's
26 // MediaCodec. It differs from MediaSourcePlayer in that it removes most
27 // processing away from the UI thread: it uses a dedicated Media thread to
28 // receive the data and to handle the commands.
29
30 // The player works as a state machine. Here are relationships between states:
31 //
32 // [ Paused ] ------------------------ (Any state)
33 // | | |
34 // | v v
35 // | <------------------[ WaitingForConfig ] [ Error ]
36 // |
37 // |
38 // | <----------------------------------------------
39 // | |
40 // [ Waiting for permission ] |
41 // | |
42 // | |
43 // v |
44 // [ Prefetching ] ------------------- |
45 // | | |
46 // | v |
47 // | <-----------------[ WaitingForSurface ] |
48 // v |
49 // [ Playing ] |
50 // | |
51 // | |
52 // v |
53 // [ Stopping ] --------------------------------> [ WaitingForSeek ]
54
55
56 // Events and actions for pause/resume workflow.
57 // ---------------------------------------------
58 //
59 // Start, no config:
60 // ------------------------> [ Paused ] -----------------> [ Waiting ]
61 // | StopDone: [ for configs ]
62 // | ^ | /
63 // | | | /
64 // | Pause: | | Start w/config: /
65 // | Permission denied: | | requestPermission /
66 // | | | /
67 // | | | /
68 // | | | /
69 // | | | / DemuxerConfigs:
70 // | | | / requestPermission
71 // | | | /
72 // | | | /
73 // | | v /
74 // | /
75 // | ------------------> [ Waiting for ] <--------/
76 // | | [ permission ]
77 // | | |
78 // | | |
79 // | | | Permission granted:
80 // | | | dec.Prefetch
81 // | | |
82 // | | |
83 // | | v
84 // | |
85 // | | [ Prefetching ] [ Waiting ]
86 // | | [ ] --------------> [ for surface ]
87 // | | | PrefetchDone, /
88 // | | | no surface: /
89 // | | | /
90 // | | | /
91 // | | StopDone w/ | /
92 // | | pending start: | PrefetchDone: /
93 // | | dec.Prefetch | dec.Start /
94 // | | | / SetSurface:
95 // | | | / dec.Start
96 // | | | /
97 // | | v /
98 // | | /
99 // | | [ Playing ] <----------/
100 // | |
101 // | | |
102 // | | |
103 // | | | Pause: dec.RequestToStop
104 // | | |
105 // | | |
106 // | | v
107 // | |
108 // ------------------------- [ Stopping ]
109
110
111 // Events and actions for seek workflow.
112 // -------------------------------------
113 //
114 // Seek: -- --
115 // demuxer.RequestSeek | |
116 // [ Paused ] -----------------------> | |
117 // [ ] <----------------------- | |--
118 // SeekDone: | | |
119 // | | |
120 // | | |
121 // | | |
122 // | | | Start:
123 // | | | SetPendingStart
124 // Seek: dec.Stop | | |
125 // SetPendingStart | | |
126 // demuxer.RequestSeek | | |
127 // [ Waiting for ] -----------------------> | | |
128 // [ permission ] <---------------------- | | | Pause:
129 // SeekDone | | | RemovePendingStart
130 // | w/pending start: | | |
131 // | requestPermission | Waiting | |
132 // | | for | | Seek:
133 // | | seek | | SetPendingSeek
134 // | | | |
135 // | Seek: dec.Stop | | |
136 // v SetPendingStart | | |
137 // demuxer.RequestSeek | | |
138 // [ Prefetching ] ----------------------> | | |
139 // | | |
140 // | | | |
141 // | PrefetchDone: dec.Start | | |
142 // | | | | SeekDone
143 // v | | | w/pending seek:
144 // | | | demuxer.RequestSeek
145 // [ Playing ] | | |
146 // | | |
147 // | | |<-
148 // | Seek: SetPendingStart | |
149 // | SetPendingSeek | |
150 // | dec.RequestToStop | |
151 // | | |
152 // | | |
153 // v | |
154 // | |
155 // [ Stopping ] -----------------------> | |
156 // StopDone -- --
157 // w/pending seek:
158 // demuxer.RequestSeek
159
160 namespace media {
161
162 class AudioMediaCodecDecoder;
163 class VideoMediaCodecDecoder;
164
165 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid,
166 public DemuxerAndroidClient {
167 public:
168 // Typedefs for the notification callbacks
169 typedef base::Callback<void(base::TimeDelta, const gfx::Size&)>
170 MetadataChangedCallback;
171
172 typedef base::Callback<void(base::TimeDelta, base::TimeTicks)>
173 TimeUpdateCallback;
174
175 typedef base::Callback<void(const base::TimeDelta& current_timestamp)>
176 SeekDoneCallback;
177
178 typedef base::Callback<void(int)> ErrorCallback;
179
180 // For testing only.
181 typedef base::Callback<void(DemuxerStream::Type,
182 base::TimeDelta,
183 base::TimeDelta)> DecodersTimeCallback;
184
185 // For testing only.
186 typedef base::Callback<void(DemuxerStream::Type)> CodecCreatedCallback;
187
188 // Constructs a player with the given ID and demuxer. |manager| must outlive
189 // the lifetime of this object.
190 MediaCodecPlayer(
191 int player_id,
192 base::WeakPtr<MediaPlayerManager> manager,
193 const OnDecoderResourcesReleasedCB& on_decoder_resources_released_cb,
194 std::unique_ptr<DemuxerAndroid> demuxer,
195 const GURL& frame_url,
196 int media_session_id);
197 ~MediaCodecPlayer() override;
198
199 // A helper method that performs the media thread part of initialization.
200 void Initialize();
201
202 // MediaPlayerAndroid implementation.
203 void DeleteOnCorrectThread() override;
204 void SetVideoSurface(gl::ScopedJavaSurface surface) override;
205 void Start() override;
206 void Pause(bool is_media_related_action) override;
207 void SeekTo(base::TimeDelta timestamp) override;
208 void Release() override;
209 bool HasVideo() const override;
210 bool HasAudio() const override;
211 int GetVideoWidth() override;
212 int GetVideoHeight() override;
213 base::TimeDelta GetCurrentTime() override;
214 base::TimeDelta GetDuration() override;
215 bool IsPlaying() override;
216 bool CanPause() override;
217 bool CanSeekForward() override;
218 bool CanSeekBackward() override;
219 bool IsPlayerReady() override;
220 void SetCdm(const scoped_refptr<MediaKeys>& cdm) override;
221
222 // DemuxerAndroidClient implementation.
223 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
224 void OnDemuxerDataAvailable(const DemuxerData& params) override;
225 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
226 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
227
228 // For testing only.
229 void SetDecodersTimeCallbackForTests(DecodersTimeCallback cb);
230 void SetCodecCreatedCallbackForTests(CodecCreatedCallback cb);
231 void SetAlwaysReconfigureForTests(DemuxerStream::Type type);
232 bool IsPrerollingForTests(DemuxerStream::Type type) const;
233
234 private:
235 // The state machine states.
236 enum PlayerState {
237 kStatePaused,
238 kStateWaitingForConfig,
239 kStateWaitingForPermission,
240 kStatePrefetching,
241 kStatePlaying,
242 kStateStopping,
243 kStateWaitingForSurface,
244 kStateWaitingForKey,
245 kStateWaitingForMediaCrypto,
246 kStateWaitingForSeek,
247 kStateError,
248 };
249
250 enum StartStatus {
251 kStartOk = 0,
252 kStartBrowserSeekRequired,
253 kStartCryptoRequired,
254 kStartFailed,
255 };
256
257 // Cached values for the manager.
258 struct MediaMetadata {
259 base::TimeDelta duration;
260 gfx::Size video_size;
261 };
262
263 // Information about current seek in progress.
264 struct SeekInfo {
265 const base::TimeDelta seek_time;
266 const bool is_browser_seek;
267 SeekInfo(base::TimeDelta time, bool browser_seek)
268 : seek_time(time), is_browser_seek(browser_seek) {}
269 };
270
271 // MediaPlayerAndroid implementation.
272 void UpdateEffectiveVolumeInternal(double effective_volume) override;
273
274 // This method requests playback permission from the manager on UI
275 // thread, passing total duration and whether the media has audio
276 // track as arguments. The method posts the result to the media
277 // thread.
278 void RequestPermissionAndPostResult(base::TimeDelta duration,
279 bool has_audio) override;
280
281 // This method caches the data and calls manager's OnMediaMetadataChanged().
282 void OnMediaMetadataChanged(base::TimeDelta duration,
283 const gfx::Size& video_size) override;
284
285 // This method caches the current time and calls manager's OnTimeUpdate().
286 void OnTimeUpdate(base::TimeDelta current_timestamp,
287 base::TimeTicks current_time_ticks) override;
288
289 // Callback from manager
290 void OnPermissionDecided(bool granted);
291
292 // Callbacks from decoders
293 void RequestDemuxerData(DemuxerStream::Type stream_type);
294 void OnPrefetchDone();
295 void OnPrerollDone();
296 void OnDecoderDrained(DemuxerStream::Type type);
297 void OnStopDone(DemuxerStream::Type type);
298 void OnMissingKeyReported(DemuxerStream::Type type);
299 void OnError();
300 void OnStarvation(DemuxerStream::Type stream_type);
301 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type,
302 base::TimeDelta now_playing,
303 base::TimeDelta last_buffered,
304 bool postpone);
305
306 // Callbacks from video decoder
307 void OnVideoResolutionChanged(const gfx::Size& size);
308
309 // Callbacks from MediaDrmBridge.
310 void OnMediaCryptoReady(MediaDrmBridge::JavaObjectPtr media_crypto,
311 bool needs_protected_surface);
312 void OnKeyAdded();
313
314 // Operations called from the state machine.
315 void SetState(PlayerState new_state);
316 void SetPendingStart(bool need_to_start);
317 bool HasPendingStart() const;
318 void SetPendingSeek(base::TimeDelta timestamp);
319 base::TimeDelta GetPendingSeek() const;
320 void SetDemuxerConfigs(const DemuxerConfigs& configs);
321 void RequestPlayPermission();
322 void StartPrefetchDecoders();
323 void StartPlaybackOrBrowserSeek();
324 StartStatus StartPlaybackDecoders();
325 StartStatus ConfigureDecoders();
326 StartStatus MaybePrerollDecoders(bool* preroll_required);
327 StartStatus StartDecoders();
328 void StopDecoders();
329 void RequestToStopDecoders();
330 void RequestDemuxerSeek(base::TimeDelta seek_time,
331 bool is_browser_seek = false);
332 void ReleaseDecoderResources();
333
334 // Helper methods.
335 void CreateDecoders();
336 bool AudioFinished() const;
337 bool VideoFinished() const;
338 base::TimeDelta GetInterpolatedTime();
339
340 static const char* AsString(PlayerState state);
341
342 // Data.
343
344 // Object for posting tasks on UI thread.
345 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
346
347 // Major components: demuxer, audio and video decoders.
348 std::unique_ptr<DemuxerAndroid> demuxer_;
349 std::unique_ptr<AudioMediaCodecDecoder> audio_decoder_;
350 std::unique_ptr<VideoMediaCodecDecoder> video_decoder_;
351
352 // The state of the state machine.
353 PlayerState state_;
354
355 // Notification callbacks, they call MediaPlayerManager.
356 TimeUpdateCallback time_update_cb_;
357 base::Closure completion_cb_;
358 base::Closure waiting_for_decryption_key_cb_;
359 SeekDoneCallback seek_done_cb_;
360 ErrorCallback error_cb_;
361
362 // A callback that updates metadata cache and calls the manager.
363 MetadataChangedCallback metadata_changed_cb_;
364
365 // We call the base class' AttachListener() and DetachListener() methods on UI
366 // thread with these callbacks.
367 base::Closure attach_listener_cb_;
368 base::Closure detach_listener_cb_;
369
370 // Error callback is posted by decoders or by this class itself if we cannot
371 // configure or start decoder.
372 base::Closure internal_error_cb_;
373
374 // Total duration reported by demuxer.
375 base::TimeDelta duration_;
376
377 // base::TickClock used by |interpolator_|.
378 base::DefaultTickClock default_tick_clock_;
379
380 // Tracks the most recent media time update and provides interpolated values
381 // as playback progresses.
382 TimeDeltaInterpolator interpolator_;
383
384 // Pending data to be picked up by the upcoming state.
385 gl::ScopedJavaSurface pending_surface_;
386 bool pending_start_;
387 base::TimeDelta pending_seek_;
388
389 // Data associated with a seek in progress.
390 std::unique_ptr<SeekInfo> seek_info_;
391
392 // Configuration data for the manager, accessed on the UI thread.
393 MediaMetadata metadata_cache_;
394
395 // Cached current time, accessed on UI thread.
396 base::TimeDelta current_time_cache_;
397
398 // For testing only.
399 DecodersTimeCallback decoders_time_cb_;
400
401 // Holds a ref-count to the CDM to keep |media_crypto_| valid.
402 scoped_refptr<MediaKeys> cdm_;
403
404 MediaDrmBridge::JavaObjectPtr media_crypto_;
405
406 int cdm_registration_id_;
407
408 // The flag is set when the player receives the error from decoder that the
409 // decoder needs a new decryption key. Cleared on starting the playback.
410 bool key_is_required_;
411
412 // The flag is set after the new encryption key is added to MediaDrm. Cleared
413 // on starting the playback.
414 bool key_is_added_;
415
416 // Gathers and reports playback quality statistics to UMA.
417 // Use pointer to enable replacement of this object for tests.
418 std::unique_ptr<MediaStatistics> media_stat_;
419
420 base::WeakPtr<MediaCodecPlayer> media_weak_this_;
421 // NOTE: Weak pointers must be invalidated before all other member variables.
422 base::WeakPtrFactory<MediaCodecPlayer> media_weak_factory_;
423
424 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer);
425 };
426
427 } // namespace media
428
429 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
OLDNEW
« no previous file with comments | « media/base/android/media_codec_decoder_unittest.cc ('k') | media/base/android/media_codec_player.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698