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