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 |
17 namespace media { | 21 namespace media { |
18 | 22 |
19 class BrowserCdm; | 23 class BrowserCdm; |
24 class MediaCodecAudioDecoder; | |
25 class MediaCodecVideoDecoder; | |
20 | 26 |
21 // Returns the task runner for the media thread | 27 // Returns the task runner for the media thread |
22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); | 28 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
23 | 29 |
24 | 30 |
25 // This class implements the media player using Android's MediaCodec. | 31 // This class implements the media player using Android's MediaCodec. |
26 // It differs from MediaSourcePlayer in that it removes most | 32 // It differs from MediaSourcePlayer in that it removes most |
27 // processing away from UI thread: it uses a dedicated Media thread | 33 // processing away from UI thread: it uses a dedicated Media thread |
28 // to receive the data and to handle commands. | 34 // to receive the data and to handle commands. |
29 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, | 35 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, |
30 public DemuxerAndroidClient { | 36 public DemuxerAndroidClient { |
31 public: | 37 public: |
38 | |
39 // Typedefs for the notification callbacks | |
40 typedef base::Callback< | |
41 void (base::TimeDelta duration, const gfx::Size& video_size)> | |
42 MetadataChangedCallback; | |
43 | |
44 typedef base::Callback< | |
45 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)> | |
46 TimeUpdateCallback; | |
47 | |
32 // Constructs a player with the given ID and demuxer. |manager| must outlive | 48 // Constructs a player with the given ID and demuxer. |manager| must outlive |
33 // the lifetime of this object. | 49 // the lifetime of this object. |
34 MediaCodecPlayer(int player_id, | 50 MediaCodecPlayer(int player_id, |
35 MediaPlayerManager* manager, | 51 base::WeakPtr<MediaPlayerManager> manager, |
36 const RequestMediaResourcesCB& request_media_resources_cb, | 52 const RequestMediaResourcesCB& request_media_resources_cb, |
37 scoped_ptr<DemuxerAndroid> demuxer, | 53 scoped_ptr<DemuxerAndroid> demuxer, |
38 const GURL& frame_url); | 54 const GURL& frame_url); |
39 ~MediaCodecPlayer() override; | 55 ~MediaCodecPlayer() override; |
40 | 56 |
41 // MediaPlayerAndroid implementation. | 57 // MediaPlayerAndroid implementation. |
42 void DeleteOnCorrectThread() override; | 58 void DeleteOnCorrectThread() override; |
43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; | 59 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; |
44 void Start() override; | 60 void Start() override; |
45 void Pause(bool is_media_related_action) override; | 61 void Pause(bool is_media_related_action) override; |
(...skipping 12 matching lines...) Expand all Loading... | |
58 void SetCdm(BrowserCdm* cdm) override; | 74 void SetCdm(BrowserCdm* cdm) override; |
59 | 75 |
60 // DemuxerAndroidClient implementation. | 76 // DemuxerAndroidClient implementation. |
61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 77 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
62 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 78 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 79 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 80 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
65 | 81 |
66 // Helper methods | 82 // Helper methods |
67 void Initialize(); | 83 void Initialize(); |
68 void DestroySelf(); | 84 |
85 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; } | |
86 | |
87 protected: | |
88 // MediaPlayerAndroid implementation. | |
89 // This method caches the data and calls manager's OnMediaMetadataChanged(). | |
90 void OnMediaMetadataChanged( | |
91 base::TimeDelta duration, const gfx::Size& video_size) override; | |
69 | 92 |
70 private: | 93 private: |
94 // The state machine states. | |
95 enum PlayerState { | |
96 STATE_PAUSED, | |
97 STATE_WAITING_FOR_CONFIG, | |
wolenetz
2015/05/20 18:20:04
nit: this state isn't represented in the state dia
Tima Vaisburd
2015/05/28 19:44:31
Done.
| |
98 STATE_PREFETCHING, | |
99 STATE_PLAYING, | |
100 STATE_STOPPING, | |
101 STATE_WAITING_FOR_SURFACE, | |
102 STATE_WAITING_FOR_SEEK, | |
wolenetz
2015/05/20 18:20:04
nit ditto
Tima Vaisburd
2015/05/28 19:44:31
Done.
| |
103 STATE_ERROR, | |
104 }; | |
105 | |
106 // Cached valued for the manager, accessed on the UI thread. | |
107 struct MediaMetadata { | |
108 base::TimeDelta duration; | |
109 gfx::Size video_size; | |
110 // Have some defined aspect ratio by default. | |
111 MediaMetadata() : duration(), video_size(320, 240) {} | |
112 }; | |
113 | |
114 // Callbacks from decoders | |
115 void RequestDemuxerData(DemuxerStream::Type stream_type); | |
116 void OnPrefetchDone(); | |
117 void OnStarvation(); | |
118 void OnStopDone(); | |
119 void OnError(); | |
120 void OnTimeIntervalUpdate(base::TimeDelta now_playing, | |
121 base::TimeDelta last_buffered); | |
122 | |
123 // Operations called from the state machine. | |
124 void SetState(PlayerState new_state); | |
125 void SetPendingSurface(gfx::ScopedJavaSurface surface); | |
126 bool HasPendingSurface(); | |
127 void SetPendingStart(bool need_to_start); | |
128 bool HasPendingStart(); | |
129 bool HasVideo(); | |
130 bool HasAudio(); | |
131 void SetDemuxerConfigs(const DemuxerConfigs& configs); | |
132 void StartPrefetchDecoders(); | |
133 void StartPlaybackDecoders(); | |
134 void StopDecoders(); | |
135 void RequestToStopDecoders(); | |
136 void ReleaseDecoderResources(); | |
137 | |
138 // Helper methods. | |
139 void CreateDecoders(); | |
140 bool AudioFinished(); | |
141 bool VideoFinished(); | |
142 | |
143 static const char* AsString(PlayerState state); | |
144 | |
145 // Data. | |
146 | |
71 // Object for posting tasks on UI thread. | 147 // Object for posting tasks on UI thread. |
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 148 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
73 | 149 |
150 // Major components: demuxer, audio and video decoders. | |
74 scoped_ptr<DemuxerAndroid> demuxer_; | 151 scoped_ptr<DemuxerAndroid> demuxer_; |
152 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_; | |
153 scoped_ptr<MediaCodecVideoDecoder> video_decoder_; | |
154 | |
155 // The state of the state machine. | |
156 PlayerState state_; | |
157 | |
158 // Notification callbacks, they call MediaPlayerManager. | |
159 base::Closure request_resources_cb_; | |
160 TimeUpdateCallback time_update_cb_; | |
161 base::Closure completion_cb_; | |
162 | |
163 // A callback that updates metadata cache and calls the manager. | |
164 MetadataChangedCallback metadata_changed_cb_; | |
165 | |
166 // We call the base class' AttachListener() and DetachListener() | |
167 // methods on UI thread with these callbacks. | |
168 base::Closure attach_listener_cb_; | |
169 base::Closure detach_listener_cb_; | |
170 | |
171 // Error callback might be posted internally | |
172 base::Closure error_cb_; | |
173 | |
174 // Total duration reported by demuxer. | |
175 base::TimeDelta duration_; | |
176 | |
177 // base::TickClock used by |interpolator_|. | |
178 base::DefaultTickClock default_tick_clock_; | |
179 | |
180 // Tracks the most recent media time update and provides interpolated values | |
181 // as playback progresses. | |
182 TimeDeltaInterpolator interpolator_; | |
183 | |
184 // |interpolator_| will be accessed from the UI and decoder threads | |
185 base::Lock interpolator_lock_; | |
186 | |
187 // Pendng data to be picked up by the upcoming state. | |
188 gfx::ScopedJavaSurface pending_surface_; | |
189 bool pending_start_; | |
190 | |
191 // Configuration data for the manager, accessed on the UI thread. | |
192 MediaMetadata cache_; | |
wolenetz
2015/05/20 18:20:04
nit: this member's name lacks self-description. e.
Tima Vaisburd
2015/05/22 23:37:52
Done.
| |
75 | 193 |
76 base::WeakPtr<MediaCodecPlayer> weak_this_; | 194 base::WeakPtr<MediaCodecPlayer> weak_this_; |
77 // NOTE: Weak pointers must be invalidated before all other member variables. | 195 // NOTE: Weak pointers must be invalidated before all other member variables. |
78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; | 196 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; |
79 | 197 |
80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); | 198 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); |
81 }; | 199 }; |
82 | 200 |
83 } // namespace media | 201 } // namespace media |
84 | 202 |
85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 203 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
OLD | NEW |