OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
7 | 7 |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "base/time/default_tick_clock.h" | |
12 #include "media/base/android/demuxer_android.h" | 13 #include "media/base/android/demuxer_android.h" |
13 #include "media/base/android/media_player_android.h" | 14 #include "media/base/android/media_player_android.h" |
15 #include "media/base/demuxer_stream.h" | |
14 #include "media/base/media_export.h" | 16 #include "media/base/media_export.h" |
17 #include "media/base/time_delta_interpolator.h" | |
18 #include "ui/gfx/geometry/size.h" | |
15 #include "ui/gl/android/scoped_java_surface.h" | 19 #include "ui/gl/android/scoped_java_surface.h" |
16 | 20 |
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. | |
25 | |
26 // The player works as a state machine. Here are relationships between states: | |
27 // | |
28 // [ Paused ] ------------------------ (Any state) | |
29 // | | | | |
30 // | v v | |
31 // | <------------------[ WaitingForConfig ] [ Error ] | |
wolenetz
2015/06/26 00:59:25
How do we reach Error state if a decoder posts our
Tima Vaisburd
2015/06/26 01:48:02
That should not be possible.
| |
32 // | | |
33 // | | |
34 // | | |
35 // v | |
36 // [ Prefetching ] ------------------- | |
37 // | | | |
38 // | v | |
39 // | <-----------------[ WaitingForSurface ] | |
40 // v | |
41 // [ Playing ] | |
42 // | | |
43 // | | |
44 // v | |
45 // [ Stopping ] | |
46 | |
47 | |
48 // Events and actions for pause/resume workflow. | |
49 // --------------------------------------------- | |
50 // | |
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 ] | |
91 | |
17 namespace media { | 92 namespace media { |
18 | 93 |
19 class BrowserCdm; | 94 class BrowserCdm; |
95 class MediaCodecAudioDecoder; | |
96 class MediaCodecVideoDecoder; | |
20 | 97 |
21 // Returns the task runner for the media thread | 98 // Returns the task runner for the media thread |
22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); | 99 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
23 | 100 |
24 | |
25 // This class implements the media player using Android's MediaCodec. | |
26 // It differs from MediaSourcePlayer in that it removes most | |
27 // processing away from UI thread: it uses a dedicated Media thread | |
28 // to receive the data and to handle commands. | |
29 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, | 101 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, |
30 public DemuxerAndroidClient { | 102 public DemuxerAndroidClient { |
31 public: | 103 public: |
104 // Typedefs for the notification callbacks | |
105 typedef base::Callback<void(base::TimeDelta, const gfx::Size&)> | |
106 MetadataChangedCallback; | |
107 | |
108 typedef base::Callback<void(base::TimeDelta, base::TimeTicks)> | |
109 TimeUpdateCallback; | |
110 | |
32 // Constructs a player with the given ID and demuxer. |manager| must outlive | 111 // Constructs a player with the given ID and demuxer. |manager| must outlive |
33 // the lifetime of this object. | 112 // the lifetime of this object. |
34 MediaCodecPlayer(int player_id, | 113 MediaCodecPlayer(int player_id, |
35 MediaPlayerManager* manager, | 114 base::WeakPtr<MediaPlayerManager> manager, |
36 const RequestMediaResourcesCB& request_media_resources_cb, | 115 const RequestMediaResourcesCB& request_media_resources_cb, |
37 scoped_ptr<DemuxerAndroid> demuxer, | 116 scoped_ptr<DemuxerAndroid> demuxer, |
38 const GURL& frame_url); | 117 const GURL& frame_url); |
39 ~MediaCodecPlayer() override; | 118 ~MediaCodecPlayer() override; |
40 | 119 |
120 // A helper method that performs the media thread part of initialization. | |
121 void Initialize(); | |
122 | |
41 // MediaPlayerAndroid implementation. | 123 // MediaPlayerAndroid implementation. |
42 void DeleteOnCorrectThread() override; | 124 void DeleteOnCorrectThread() override; |
43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; | 125 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; |
44 void Start() override; | 126 void Start() override; |
45 void Pause(bool is_media_related_action) override; | 127 void Pause(bool is_media_related_action) override; |
46 void SeekTo(base::TimeDelta timestamp) override; | 128 void SeekTo(base::TimeDelta timestamp) override; |
47 void Release() override; | 129 void Release() override; |
48 void SetVolume(double volume) override; | 130 void SetVolume(double volume) override; |
49 int GetVideoWidth() override; | 131 int GetVideoWidth() override; |
50 int GetVideoHeight() override; | 132 int GetVideoHeight() override; |
51 base::TimeDelta GetCurrentTime() override; | 133 base::TimeDelta GetCurrentTime() override; |
52 base::TimeDelta GetDuration() override; | 134 base::TimeDelta GetDuration() override; |
53 bool IsPlaying() override; | 135 bool IsPlaying() override; |
54 bool CanPause() override; | 136 bool CanPause() override; |
55 bool CanSeekForward() override; | 137 bool CanSeekForward() override; |
56 bool CanSeekBackward() override; | 138 bool CanSeekBackward() override; |
57 bool IsPlayerReady() override; | 139 bool IsPlayerReady() override; |
58 void SetCdm(BrowserCdm* cdm) override; | 140 void SetCdm(BrowserCdm* cdm) override; |
59 | 141 |
60 // DemuxerAndroidClient implementation. | 142 // DemuxerAndroidClient implementation. |
61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 143 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
62 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 144 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 145 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 146 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
65 | 147 |
66 // Helper methods | 148 private: |
67 void Initialize(); | 149 // The state machine states. |
68 void DestroySelf(); | 150 enum PlayerState { |
151 STATE_PAUSED, | |
152 STATE_WAITING_FOR_CONFIG, | |
153 STATE_PREFETCHING, | |
154 STATE_PLAYING, | |
155 STATE_STOPPING, | |
156 STATE_WAITING_FOR_SURFACE, | |
157 STATE_ERROR, | |
158 }; | |
69 | 159 |
70 private: | 160 // Cached values for the manager. |
161 struct MediaMetadata { | |
162 base::TimeDelta duration; | |
163 gfx::Size video_size; | |
164 }; | |
165 | |
166 // MediaPlayerAndroid implementation. | |
167 // This method caches the data and calls manager's OnMediaMetadataChanged(). | |
168 void OnMediaMetadataChanged(base::TimeDelta duration, | |
169 const gfx::Size& video_size) override; | |
170 | |
171 // This method caches the current time and calls manager's OnTimeUpdate(). | |
172 void OnTimeUpdate(base::TimeDelta current_timestamp, | |
173 base::TimeTicks current_time_ticks) override; | |
174 | |
175 // Callbacks from decoders | |
176 void RequestDemuxerData(DemuxerStream::Type stream_type); | |
177 void OnPrefetchDone(); | |
178 void OnStopDone(); | |
179 void OnError(); | |
180 void OnStarvation(DemuxerStream::Type stream_type); | |
181 void OnTimeIntervalUpdate(DemuxerStream::Type stream_type, | |
182 base::TimeDelta now_playing, | |
183 base::TimeDelta last_buffered); | |
184 | |
185 // Callbacks from video decoder | |
186 void OnVideoCodecCreated(); | |
187 void OnVideoSizeChanged(const gfx::Size& size); | |
188 | |
189 // Operations called from the state machine. | |
190 void SetState(PlayerState new_state); | |
191 void SetPendingSurface(gfx::ScopedJavaSurface surface); | |
192 bool HasPendingSurface(); | |
193 void SetPendingStart(bool need_to_start); | |
194 bool HasPendingStart(); | |
195 bool HasVideo(); | |
196 bool HasAudio(); | |
197 void SetDemuxerConfigs(const DemuxerConfigs& configs); | |
198 void StartPrefetchDecoders(); | |
199 void StartPlaybackDecoders(); | |
200 void StopDecoders(); | |
201 void RequestToStopDecoders(); | |
202 void ReleaseDecoderResources(); | |
203 | |
204 // Helper methods. | |
205 void CreateDecoders(); | |
206 bool AudioFinished(); | |
207 bool VideoFinished(); | |
208 base::TimeDelta GetInterpolatedTime(); | |
209 | |
210 static const char* AsString(PlayerState state); | |
211 | |
212 // Data. | |
213 | |
71 // Object for posting tasks on UI thread. | 214 // Object for posting tasks on UI thread. |
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 215 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
73 | 216 |
217 // Major components: demuxer, audio and video decoders. | |
74 scoped_ptr<DemuxerAndroid> demuxer_; | 218 scoped_ptr<DemuxerAndroid> demuxer_; |
219 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_; | |
220 scoped_ptr<MediaCodecVideoDecoder> video_decoder_; | |
75 | 221 |
76 base::WeakPtr<MediaCodecPlayer> weak_this_; | 222 // The state of the state machine. |
223 PlayerState state_; | |
224 | |
225 // Notification callbacks, they call MediaPlayerManager. | |
226 base::Closure request_resources_cb_; | |
227 TimeUpdateCallback time_update_cb_; | |
228 base::Closure completion_cb_; | |
229 | |
230 // A callback that updates metadata cache and calls the manager. | |
231 MetadataChangedCallback metadata_changed_cb_; | |
232 | |
233 // We call the base class' AttachListener() and DetachListener() methods on UI | |
234 // thread with these callbacks. | |
235 base::Closure attach_listener_cb_; | |
236 base::Closure detach_listener_cb_; | |
237 | |
238 // Error callback is posted by decoders or by this class itself if we cannot | |
239 // configure or start decoder. | |
240 base::Closure error_cb_; | |
241 | |
242 // Total duration reported by demuxer. | |
243 base::TimeDelta duration_; | |
244 | |
245 // base::TickClock used by |interpolator_|. | |
246 base::DefaultTickClock default_tick_clock_; | |
247 | |
248 // Tracks the most recent media time update and provides interpolated values | |
249 // as playback progresses. | |
250 TimeDeltaInterpolator interpolator_; | |
251 | |
252 // Pending data to be picked up by the upcoming state. | |
253 gfx::ScopedJavaSurface pending_surface_; | |
254 bool pending_start_; | |
255 | |
256 // Configuration data for the manager, accessed on the UI thread. | |
257 MediaMetadata metadata_cache_; | |
258 | |
259 // Cached current time, accessed on UI thread. | |
260 base::TimeDelta current_time_cache_; | |
261 | |
262 base::WeakPtr<MediaCodecPlayer> media_weak_this_; | |
77 // NOTE: Weak pointers must be invalidated before all other member variables. | 263 // NOTE: Weak pointers must be invalidated before all other member variables. |
78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; | 264 base::WeakPtrFactory<MediaCodecPlayer> media_weak_factory_; |
79 | 265 |
80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); | 266 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); |
81 }; | 267 }; |
82 | 268 |
83 } // namespace media | 269 } // namespace media |
84 | 270 |
85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 271 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
OLD | NEW |