| 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" |
| 13 |
| 12 #include "media/base/android/demuxer_android.h" | 14 #include "media/base/android/demuxer_android.h" |
| 13 #include "media/base/android/media_player_android.h" | 15 #include "media/base/android/media_player_android.h" |
| 16 #include "media/base/demuxer_stream.h" |
| 14 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
| 18 #include "media/base/time_delta_interpolator.h" |
| 19 |
| 15 #include "ui/gl/android/scoped_java_surface.h" | 20 #include "ui/gl/android/scoped_java_surface.h" |
| 16 | 21 |
| 17 namespace media { | 22 namespace media { |
| 18 | 23 |
| 19 class BrowserCdm; | 24 class BrowserCdm; |
| 25 class MediaCodecPlayerState; |
| 26 class MediaCodecAudioDecoder; |
| 27 class MediaCodecVideoDecoder; |
| 20 | 28 |
| 21 // Returns the task runner for the media thread | 29 // Returns the task runner for the media thread |
| 22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); | 30 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
| 23 | 31 |
| 24 | 32 |
| 25 // This class implements the media player using Android's MediaCodec. | 33 // This class implements the media player using Android's MediaCodec. |
| 26 // It differs from MediaSourcePlayer in that it removes most | 34 // It differs from MediaSourcePlayer in that it removes most |
| 27 // processing away from UI thread: it uses a dedicated Media thread | 35 // processing away from UI thread: it uses a dedicated Media thread |
| 28 // to receive the data and to handle commands. | 36 // to receive the data and to handle commands. |
| 29 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, | 37 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, |
| 30 public DemuxerAndroidClient { | 38 public DemuxerAndroidClient { |
| 31 public: | 39 public: |
| 40 |
| 41 // Typedefs for the notification callbacks |
| 42 typedef base::Callback< |
| 43 void (base::TimeDelta duration, int width, int height, bool success)> |
| 44 MetadataChangedCallback; |
| 45 |
| 46 typedef base::Callback< |
| 47 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)> |
| 48 TimeUpdateCallback; |
| 49 |
| 32 // Constructs a player with the given ID and demuxer. |manager| must outlive | 50 // Constructs a player with the given ID and demuxer. |manager| must outlive |
| 33 // the lifetime of this object. | 51 // the lifetime of this object. |
| 34 MediaCodecPlayer(int player_id, | 52 MediaCodecPlayer(int player_id, |
| 35 MediaPlayerManager* manager, | 53 base::WeakPtr<MediaPlayerManager> manager, |
| 36 const RequestMediaResourcesCB& request_media_resources_cb, | 54 const RequestMediaResourcesCB& request_media_resources_cb, |
| 37 scoped_ptr<DemuxerAndroid> demuxer, | 55 scoped_ptr<DemuxerAndroid> demuxer, |
| 38 const GURL& frame_url); | 56 const GURL& frame_url); |
| 39 ~MediaCodecPlayer() override; | 57 ~MediaCodecPlayer() override; |
| 40 | 58 |
| 41 // MediaPlayerAndroid implementation. | 59 // MediaPlayerAndroid implementation. |
| 42 void DeleteOnCorrectThread() override; | 60 void DeleteOnCorrectThread() override; |
| 43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; | 61 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; |
| 44 void Start() override; | 62 void Start() override; |
| 45 void Pause(bool is_media_related_action) override; | 63 void Pause(bool is_media_related_action) override; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 58 void SetCdm(BrowserCdm* cdm) override; | 76 void SetCdm(BrowserCdm* cdm) override; |
| 59 | 77 |
| 60 // DemuxerAndroidClient implementation. | 78 // DemuxerAndroidClient implementation. |
| 61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 79 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
| 62 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 80 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
| 63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 81 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
| 64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 82 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
| 65 | 83 |
| 66 // Helper methods | 84 // Helper methods |
| 67 void Initialize(); | 85 void Initialize(); |
| 68 void DestroySelf(); | 86 |
| 87 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; } |
| 69 | 88 |
| 70 private: | 89 private: |
| 90 // The state machine states can access the player. |
| 91 friend class MediaCodecPlayerState; |
| 92 friend class StateWaitingForConfig; |
| 93 friend class StateWaitingForSurface; |
| 94 friend class StatePaused; |
| 95 friend class StatePrefetching; |
| 96 friend class StatePlaying; |
| 97 friend class StateStopping; |
| 98 friend class StateWaitingForSeek; |
| 99 friend class StateError; |
| 100 |
| 101 // Callbacks from decoders |
| 102 void RequestDemuxerData(DemuxerStream::Type stream_type); |
| 103 void OnPrefetchDone(); |
| 104 void OnStarvation(); |
| 105 void OnStopDone(); |
| 106 void OnError(); |
| 107 void OnTimeIntervalUpdate(base::TimeDelta now_playing, |
| 108 base::TimeDelta last_buffered); |
| 109 |
| 110 // Operations called from the state machine. |
| 111 void SetState(MediaCodecPlayerState* new_state); |
| 112 void SetPendingSurface(gfx::ScopedJavaSurface surface); |
| 113 bool HasPendingSurface(); |
| 114 void SetPendingStart(bool need_to_start); |
| 115 bool HasPendingStart(); |
| 116 bool HasVideo(); |
| 117 bool HasAudio(); |
| 118 void SetDemuxerConfigs(const DemuxerConfigs& configs); |
| 119 void StartPrefetchDecoders(); |
| 120 void StartPlaybackDecoders(); |
| 121 void StopDecoders(); |
| 122 void RequestToStopDecoders(); |
| 123 void ReleaseDecoderResources(); |
| 124 |
| 125 // Helper methods. |
| 126 void CreateDecoders(); |
| 127 bool AudioFinished(); |
| 128 bool VideoFinished(); |
| 129 |
| 130 // Data. |
| 131 |
| 71 // Object for posting tasks on UI thread. | 132 // Object for posting tasks on UI thread. |
| 72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 133 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 73 | 134 |
| 135 // Major components: demuxer, audio and video decoders. |
| 74 scoped_ptr<DemuxerAndroid> demuxer_; | 136 scoped_ptr<DemuxerAndroid> demuxer_; |
| 137 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_; |
| 138 scoped_ptr<MediaCodecVideoDecoder> video_decoder_; |
| 139 |
| 140 // The state of the state machine. |
| 141 MediaCodecPlayerState* state_; |
| 142 |
| 143 // Notification callbacks, we call manager() with them. |
| 144 base::Closure request_resources_cb_; |
| 145 MetadataChangedCallback metadata_changed_cb_; |
| 146 TimeUpdateCallback time_update_cb_; |
| 147 base::Closure completion_cb_; |
| 148 |
| 149 // Total duration reported by demuxer. |
| 150 base::TimeDelta duration_; |
| 151 |
| 152 // base::TickClock used by |interpolator_|. |
| 153 base::DefaultTickClock default_tick_clock_; |
| 154 |
| 155 // Tracks the most recent media time update and provides interpolated values |
| 156 // as playback progresses. |
| 157 TimeDeltaInterpolator interpolator_; |
| 158 |
| 159 // |interpolator_| will be accessed from the UI and decoder threads |
| 160 base::Lock interpolator_lock_; |
| 161 |
| 162 // Pendng data to be picked up by the upcoming state. |
| 163 gfx::ScopedJavaSurface pending_surface_; |
| 164 bool pending_start_; |
| 75 | 165 |
| 76 base::WeakPtr<MediaCodecPlayer> weak_this_; | 166 base::WeakPtr<MediaCodecPlayer> weak_this_; |
| 77 // NOTE: Weak pointers must be invalidated before all other member variables. | 167 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; | 168 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; |
| 79 | 169 |
| 80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); | 170 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); |
| 81 }; | 171 }; |
| 82 | 172 |
| 83 } // namespace media | 173 } // namespace media |
| 84 | 174 |
| 85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 175 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
| OLD | NEW |