Chromium Code Reviews| 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 ] | |
| 32 // | | |
| 33 // | | |
| 34 // | <---------------------------------------------- | |
| 35 // v | | |
| 36 // [ Prefetching ] ------------------- | | |
| 37 // | | | | |
| 38 // | v | | |
| 39 // | <-----------------[ WaitingForSurface ] | | |
| 40 // v | | |
| 41 // [ Playing ] | | |
| 42 // | | | |
| 43 // | | | |
| 44 // v | | |
| 45 // [ Stopping ] --------------------------------> [ WaitingForSeek ] | |
| 46 // | |
| 47 // | |
| 48 | |
| 49 /* | |
| 50 Events and actions for pause/resume workflow. | |
| 51 --------------------------------------------- | |
| 52 | |
| 53 Start, no config: | |
| 54 ------------------------> [ Paused ] -----------------> [ Waiting ] | |
| 55 | StopDone: [ for configs ] | |
| 56 | ^ | / | |
| 57 | | | / | |
| 58 | Pause: | | Start w/config: / | |
| 59 | | | dec.Prefetch / | |
| 60 | | | / | |
| 61 | | | / | |
| 62 | | | / | |
| 63 | | | / DemuxerConfigs: | |
| 64 | | | / dec.Prefetch | |
| 65 | | | / | |
| 66 | | | / | |
| 67 | | v / | |
| 68 | / | |
| 69 | ------------------> [ Prefetching ] <--------/ [ Waiting ] | |
| 70 | | [ ] --------------> [ for surface ] | |
| 71 | | | PrefetchDone, / | |
| 72 | | | no surface: / | |
| 73 | | | / | |
| 74 | | | / | |
| 75 | | StopDone w/ | / | |
| 76 | | pending start: | PrefetchDone: / | |
| 77 | | dec.Prefetch | dec.Start / | |
| 78 | | | / SetSurface: | |
| 79 | | | / dec.Start | |
| 80 | | | / | |
| 81 | | v / | |
| 82 | | / | |
| 83 | | [ Playing ] <----------/ | |
| 84 | | | |
| 85 | | | | |
| 86 | | | | |
| 87 | | | Pause: dec.RequestToStop | |
| 88 | | | | |
| 89 | | | | |
| 90 | | v | |
| 91 | | | |
| 92 ------------------------- [ Stopping ] | |
| 93 | |
| 94 | |
| 95 Events and actions for seek workflow. | |
| 96 ------------------------------------- | |
| 97 | |
| 98 Seek: -- -- | |
| 99 demuxer.RequestSeek | | | |
| 100 [ Paused ] -----------------------> | | | |
| 101 [ ] <----------------------- | |-- | |
| 102 SeekDone: | | | | |
| 103 | | | | |
| 104 | | | | |
| 105 | | | | |
| 106 | | | Start: | |
| 107 Seek: | | | SetPendingStart | |
| 108 dec.Stop | | | | |
| 109 SetPendingSeek | | | | |
| 110 demuxer.RequestSeek | | | | |
| 111 [ Prefetching ] -----------------------> | | | | |
| 112 [ ] <---------------------- | | | Pause: | |
| 113 | SeekDone | | | RemovePendingStart | |
| 114 | w/pending start: | | | | |
| 115 | dec.Prefetch | Waiting | | | |
| 116 | | for | | Seek: | |
| 117 | | seek | | SetPendingSeek | |
| 118 | | | | | |
| 119 | PrefetchDone: dec.Start | | | | |
| 120 | | | | SeekDone | |
| 121 v | | | w/pending seek: | |
| 122 | | | demuxer.RequestSeek | |
| 123 [ Playing ] | | | | |
| 124 | | | | |
| 125 | | |<- | |
| 126 | Seek: SetPendingStart | | | |
| 127 | SetPendingSeek | | | |
| 128 | dec.RequestToStop | | | |
| 129 | | | | |
| 130 | | | | |
| 131 v | | | |
| 132 | | | |
| 133 [ Stopping ] -----------------------> | | | |
| 134 StopDone -- -- | |
| 135 w/pending seek: | |
| 136 demuxer.RequestSeek | |
| 137 */ | |
| 138 | |
| 17 namespace media { | 139 namespace media { |
| 18 | 140 |
| 19 class BrowserCdm; | 141 class BrowserCdm; |
| 142 class MediaCodecAudioDecoder; | |
| 143 class MediaCodecVideoDecoder; | |
| 20 | 144 |
| 21 // Returns the task runner for the media thread | 145 // Returns the task runner for the media thread |
| 22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); | 146 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
| 23 | 147 |
| 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, | 148 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, |
| 30 public DemuxerAndroidClient { | 149 public DemuxerAndroidClient { |
| 31 public: | 150 public: |
| 151 | |
| 152 // Typedefs for the notification callbacks | |
| 153 typedef base::Callback< | |
| 154 void (base::TimeDelta duration, const gfx::Size& video_size)> | |
| 155 MetadataChangedCallback; | |
| 156 | |
| 157 typedef base::Callback< | |
| 158 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)> | |
| 159 TimeUpdateCallback; | |
| 160 | |
| 32 // Constructs a player with the given ID and demuxer. |manager| must outlive | 161 // Constructs a player with the given ID and demuxer. |manager| must outlive |
| 33 // the lifetime of this object. | 162 // the lifetime of this object. |
| 34 MediaCodecPlayer(int player_id, | 163 MediaCodecPlayer(int player_id, |
| 35 MediaPlayerManager* manager, | 164 base::WeakPtr<MediaPlayerManager> manager, |
| 36 const RequestMediaResourcesCB& request_media_resources_cb, | 165 const RequestMediaResourcesCB& request_media_resources_cb, |
| 37 scoped_ptr<DemuxerAndroid> demuxer, | 166 scoped_ptr<DemuxerAndroid> demuxer, |
| 38 const GURL& frame_url); | 167 const GURL& frame_url); |
| 39 ~MediaCodecPlayer() override; | 168 ~MediaCodecPlayer() override; |
| 40 | 169 |
| 41 // MediaPlayerAndroid implementation. | 170 // MediaPlayerAndroid implementation. |
| 42 void DeleteOnCorrectThread() override; | 171 void DeleteOnCorrectThread() override; |
| 43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; | 172 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; |
| 44 void Start() override; | 173 void Start() override; |
| 45 void Pause(bool is_media_related_action) override; | 174 void Pause(bool is_media_related_action) override; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 58 void SetCdm(BrowserCdm* cdm) override; | 187 void SetCdm(BrowserCdm* cdm) override; |
| 59 | 188 |
| 60 // DemuxerAndroidClient implementation. | 189 // DemuxerAndroidClient implementation. |
| 61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 190 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
| 62 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 191 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
| 63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 192 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
| 64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 193 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
| 65 | 194 |
| 66 // Helper methods | 195 // Helper methods |
| 67 void Initialize(); | 196 void Initialize(); |
| 68 void DestroySelf(); | 197 |
| 198 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; } | |
| 199 | |
| 200 protected: | |
| 201 // MediaPlayerAndroid implementation. | |
| 202 // This method caches the data and calls manager's OnMediaMetadataChanged(). | |
| 203 void OnMediaMetadataChanged( | |
| 204 base::TimeDelta duration, const gfx::Size& video_size) override; | |
| 205 | |
| 206 // This method cached the current time and calls manager's OnTimeUpdate(). | |
| 207 void OnTimeUpdate(base::TimeDelta current_timestamp, | |
| 208 base::TimeTicks current_time_ticks) override; | |
| 69 | 209 |
| 70 private: | 210 private: |
| 211 // The state machine states. | |
| 212 enum PlayerState { | |
| 213 STATE_PAUSED, | |
| 214 STATE_WAITING_FOR_CONFIG, | |
| 215 STATE_PREFETCHING, | |
| 216 STATE_PLAYING, | |
| 217 STATE_STOPPING, | |
| 218 STATE_WAITING_FOR_SURFACE, | |
| 219 STATE_WAITING_FOR_SEEK, | |
| 220 STATE_ERROR, | |
| 221 }; | |
| 222 | |
| 223 // Cached valued for the manager, accessed on the UI thread. | |
| 224 struct MediaMetadata { | |
| 225 base::TimeDelta duration; | |
| 226 gfx::Size video_size; | |
| 227 // Have some defined aspect ratio by default. | |
| 228 MediaMetadata() : duration(), video_size(320, 240) {} | |
|
qinmin
2015/05/31 20:19:13
default size should be (0,0), MSE is not for video
Tima Vaisburd
2015/06/01 18:56:52
I did non-empty default video size to prevent a cr
Tima Vaisburd
2015/06/04 00:46:37
Never mind my comment, made the size (0,0).
| |
| 229 }; | |
| 230 | |
| 231 // Callbacks from decoders | |
| 232 void RequestDemuxerData(DemuxerStream::Type stream_type); | |
| 233 void OnPrefetchDone(); | |
| 234 void OnStarvation(); | |
| 235 void OnStopDone(); | |
| 236 void OnError(); | |
| 237 void OnTimeIntervalUpdate(base::TimeDelta now_playing, | |
| 238 base::TimeDelta last_buffered); | |
| 239 | |
| 240 // Callbacks from video decoder | |
| 241 void OnVideoCodecCreated(); | |
| 242 void OnVideoSizeChanged(const gfx::Size& size); | |
| 243 | |
| 244 // Operations called from the state machine. | |
| 245 void SetState(PlayerState new_state); | |
| 246 void SetPendingSurface(gfx::ScopedJavaSurface surface); | |
| 247 bool HasPendingSurface(); | |
| 248 void SetPendingStart(bool need_to_start); | |
| 249 bool HasPendingStart(); | |
| 250 bool HasVideo(); | |
| 251 bool HasAudio(); | |
| 252 void SetDemuxerConfigs(const DemuxerConfigs& configs); | |
| 253 void StartPrefetchDecoders(); | |
| 254 void StartPlaybackDecoders(); | |
| 255 void StopDecoders(); | |
| 256 void RequestToStopDecoders(); | |
| 257 void ReleaseDecoderResources(); | |
| 258 | |
| 259 // Helper methods. | |
| 260 void CreateDecoders(); | |
| 261 bool AudioFinished(); | |
|
qinmin
2015/05/31 20:19:13
the ordering of all the member functions doesn't m
Tima Vaisburd
2015/06/01 18:56:52
Done.
| |
| 262 bool VideoFinished(); | |
| 263 base::TimeDelta GetInterpolatedTime(); | |
| 264 | |
| 265 static const char* AsString(PlayerState state); | |
| 266 | |
| 267 // Data. | |
| 268 | |
| 71 // Object for posting tasks on UI thread. | 269 // Object for posting tasks on UI thread. |
| 72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 270 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 73 | 271 |
| 272 // Major components: demuxer, audio and video decoders. | |
| 74 scoped_ptr<DemuxerAndroid> demuxer_; | 273 scoped_ptr<DemuxerAndroid> demuxer_; |
| 274 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_; | |
| 275 scoped_ptr<MediaCodecVideoDecoder> video_decoder_; | |
| 276 | |
| 277 // The state of the state machine. | |
| 278 PlayerState state_; | |
| 279 | |
| 280 // Notification callbacks, they call MediaPlayerManager. | |
| 281 base::Closure request_resources_cb_; | |
| 282 TimeUpdateCallback time_update_cb_; | |
| 283 base::Closure completion_cb_; | |
| 284 | |
| 285 // A callback that updates metadata cache and calls the manager. | |
| 286 MetadataChangedCallback metadata_changed_cb_; | |
| 287 | |
| 288 // We call the base class' AttachListener() and DetachListener() methods on UI | |
| 289 // thread with these callbacks. | |
| 290 base::Closure attach_listener_cb_; | |
| 291 base::Closure detach_listener_cb_; | |
| 292 | |
| 293 // Error callback might be posted internally | |
| 294 base::Closure error_cb_; | |
| 295 | |
| 296 // Total duration reported by demuxer. | |
| 297 base::TimeDelta duration_; | |
| 298 | |
| 299 // base::TickClock used by |interpolator_|. | |
| 300 base::DefaultTickClock default_tick_clock_; | |
| 301 | |
| 302 // Tracks the most recent media time update and provides interpolated values | |
| 303 // as playback progresses. | |
| 304 TimeDeltaInterpolator interpolator_; | |
| 305 | |
| 306 // Pendng data to be picked up by the upcoming state. | |
| 307 gfx::ScopedJavaSurface pending_surface_; | |
| 308 bool pending_start_; | |
| 309 | |
| 310 // Configuration data for the manager, accessed on the UI thread. | |
| 311 MediaMetadata metadata_cache_; | |
| 312 | |
| 313 // Cached current time, accessed on UI thread. | |
| 314 base::TimeDelta current_time_cache_; | |
| 75 | 315 |
| 76 base::WeakPtr<MediaCodecPlayer> weak_this_; | 316 base::WeakPtr<MediaCodecPlayer> weak_this_; |
| 77 // NOTE: Weak pointers must be invalidated before all other member variables. | 317 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; | 318 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; |
| 79 | 319 |
| 80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); | 320 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); |
| 81 }; | 321 }; |
| 82 | 322 |
| 83 } // namespace media | 323 } // namespace media |
| 84 | 324 |
| 85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 325 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
| OLD | NEW |