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 #include "media/base/android/media_codec_player.h" | 5 #include "media/base/android/media_codec_player.h" |
6 | 6 |
7 #include "base/barrier_closure.h" | |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "base/threading/thread.h" | |
13 #include "media/base/android/media_codec_audio_decoder.h" | |
14 #include "media/base/android/media_codec_video_decoder.h" | |
15 #include "media/base/android/media_player_manager.h" | |
16 #include "media/base/buffers.h" | |
11 | 17 |
12 #define RUN_ON_MEDIA_THREAD(METHOD, ...) \ | 18 #define RUN_ON_MEDIA_THREAD(METHOD, ...) \ |
13 do { \ | 19 do { \ |
14 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ | 20 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ |
15 GetMediaTaskRunner()->PostTask( \ | 21 GetMediaTaskRunner()->PostTask( \ |
16 FROM_HERE, \ | 22 FROM_HERE, base::Bind(&MediaCodecPlayer::METHOD, media_weak_this_, \ |
17 base::Bind(&MediaCodecPlayer:: METHOD, weak_this_, ##__VA_ARGS__)); \ | 23 ##__VA_ARGS__)); \ |
18 return; \ | 24 return; \ |
19 } \ | 25 } \ |
20 } while(0) | 26 } while (0) |
21 | |
22 | 27 |
23 namespace media { | 28 namespace media { |
24 | 29 |
25 class MediaThread : public base::Thread { | 30 class MediaThread : public base::Thread { |
26 public: | 31 public: |
27 MediaThread() : base::Thread("BrowserMediaThread") { | 32 MediaThread() : base::Thread("BrowserMediaThread") { |
28 Start(); | 33 Start(); |
29 } | 34 } |
30 }; | 35 }; |
31 | 36 |
32 // Create media thread | 37 // Create media thread |
33 base::LazyInstance<MediaThread>::Leaky | 38 base::LazyInstance<MediaThread>::Leaky |
34 g_media_thread = LAZY_INSTANCE_INITIALIZER; | 39 g_media_thread = LAZY_INSTANCE_INITIALIZER; |
35 | 40 |
36 | 41 |
37 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { | 42 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { |
38 return g_media_thread.Pointer()->task_runner(); | 43 return g_media_thread.Pointer()->task_runner(); |
39 } | 44 } |
40 | 45 |
41 // MediaCodecPlayer implementation. | 46 // MediaCodecPlayer implementation. |
42 | 47 |
43 MediaCodecPlayer::MediaCodecPlayer( | 48 MediaCodecPlayer::MediaCodecPlayer( |
44 int player_id, | 49 int player_id, |
45 MediaPlayerManager* manager, | 50 base::WeakPtr<MediaPlayerManager> manager, |
46 const RequestMediaResourcesCB& request_media_resources_cb, | 51 const RequestMediaResourcesCB& request_media_resources_cb, |
47 scoped_ptr<DemuxerAndroid> demuxer, | 52 scoped_ptr<DemuxerAndroid> demuxer, |
48 const GURL& frame_url) | 53 const GURL& frame_url) |
49 : MediaPlayerAndroid(player_id, | 54 : MediaPlayerAndroid(player_id, |
50 manager, | 55 manager.get(), |
51 request_media_resources_cb, | 56 request_media_resources_cb, |
52 frame_url), | 57 frame_url), |
53 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 58 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
54 demuxer_(demuxer.Pass()), | 59 demuxer_(demuxer.Pass()), |
55 weak_factory_(this) { | 60 state_(STATE_PAUSED), |
56 // UI thread | 61 interpolator_(&default_tick_clock_), |
62 pending_start_(false), | |
63 media_weak_factory_(this) { | |
57 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 64 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
58 | 65 |
59 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; | 66 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; |
60 | 67 |
61 weak_this_ = weak_factory_.GetWeakPtr(); | 68 request_resources_cb_ = base::Bind(request_media_resources_cb_, player_id); |
69 | |
70 completion_cb_ = | |
71 base::Bind(&MediaPlayerManager::OnPlaybackComplete, manager, player_id); | |
72 attach_listener_cb_ = base::Bind(&MediaPlayerAndroid::AttachListener, | |
73 WeakPtrForUIThread(), nullptr); | |
74 detach_listener_cb_ = | |
75 base::Bind(&MediaPlayerAndroid::DetachListener, WeakPtrForUIThread()); | |
76 metadata_changed_cb_ = base::Bind(&MediaPlayerAndroid::OnMediaMetadataChanged, | |
77 WeakPtrForUIThread()); | |
78 time_update_cb_ = | |
79 base::Bind(&MediaPlayerAndroid::OnTimeUpdate, WeakPtrForUIThread()); | |
80 | |
81 media_weak_this_ = media_weak_factory_.GetWeakPtr(); | |
62 | 82 |
63 // Finish initializaton on Media thread | 83 // Finish initializaton on Media thread |
64 GetMediaTaskRunner()->PostTask( | 84 GetMediaTaskRunner()->PostTask( |
65 FROM_HERE, base::Bind(&MediaCodecPlayer::Initialize, weak_this_)); | 85 FROM_HERE, base::Bind(&MediaCodecPlayer::Initialize, media_weak_this_)); |
66 } | 86 } |
67 | 87 |
68 MediaCodecPlayer::~MediaCodecPlayer() | 88 MediaCodecPlayer::~MediaCodecPlayer() |
69 { | 89 { |
70 // Media thread | |
71 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; | 90 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; |
72 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 91 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
73 } | 92 } |
74 | 93 |
75 void MediaCodecPlayer::Initialize() { | 94 void MediaCodecPlayer::Initialize() { |
76 // Media thread | |
77 DVLOG(1) << __FUNCTION__; | 95 DVLOG(1) << __FUNCTION__; |
78 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 96 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
79 | 97 |
98 interpolator_.SetUpperBound(base::TimeDelta()); | |
99 | |
100 CreateDecoders(); | |
101 | |
102 // This call might in turn call MediaCodecPlayer::OnDemuxerConfigsAvailable() | |
103 // which propagates configs into decoders. Therefore CreateDecoders() should | |
104 // be called first. | |
80 demuxer_->Initialize(this); | 105 demuxer_->Initialize(this); |
81 } | 106 } |
82 | 107 |
83 // MediaPlayerAndroid implementation. | 108 // The implementation of MediaPlayerAndroid interface. |
84 | 109 |
85 void MediaCodecPlayer::DeleteOnCorrectThread() { | 110 void MediaCodecPlayer::DeleteOnCorrectThread() { |
86 // UI thread | |
87 DVLOG(1) << __FUNCTION__; | 111 DVLOG(1) << __FUNCTION__; |
88 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 112 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
89 | 113 |
90 // The listener-related portion of the base class has to be | 114 DetachListener(); |
91 // destroyed on UI thread. | 115 |
116 // The base class part that deals with MediaPlayerListener | |
117 // has to be destroyed on UI thread. | |
92 DestroyListenerOnUIThread(); | 118 DestroyListenerOnUIThread(); |
93 | 119 |
94 // Post deletion onto Media thread | 120 // Post deletion onto Media thread |
95 GetMediaTaskRunner()->DeleteSoon(FROM_HERE, this); | 121 GetMediaTaskRunner()->DeleteSoon(FROM_HERE, this); |
96 } | 122 } |
97 | 123 |
98 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { | 124 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { |
99 RUN_ON_MEDIA_THREAD(SetVideoSurface, base::Passed(&surface)); | 125 RUN_ON_MEDIA_THREAD(SetVideoSurface, base::Passed(&surface)); |
100 | 126 |
101 // Media thread | 127 DVLOG(1) << __FUNCTION__ << (surface.IsEmpty() ? " empty" : " non-empty"); |
102 DVLOG(1) << __FUNCTION__; | |
103 | 128 |
104 NOTIMPLEMENTED(); | 129 // I assume that if video decoder already has the surface, |
130 // there will be two calls: | |
131 // (1) SetVideoSurface(0) | |
132 // (2) SetVideoSurface(new_surface) | |
133 video_decoder_->SetPendingSurface(surface.Pass()); | |
134 | |
135 if (video_decoder_->HasPendingSurface() && | |
136 state_ == STATE_WAITING_FOR_SURFACE) { | |
137 SetState(STATE_PLAYING); | |
138 StartPlaybackDecoders(); | |
139 } | |
105 } | 140 } |
106 | 141 |
107 void MediaCodecPlayer::Start() { | 142 void MediaCodecPlayer::Start() { |
108 RUN_ON_MEDIA_THREAD(Start); | 143 RUN_ON_MEDIA_THREAD(Start); |
109 | 144 |
110 // Media thread | |
111 DVLOG(1) << __FUNCTION__; | 145 DVLOG(1) << __FUNCTION__; |
112 | 146 |
113 NOTIMPLEMENTED(); | 147 switch (state_) { |
148 case STATE_PAUSED: | |
149 if (HasAudio() || HasVideo()) { | |
150 SetState(STATE_PREFETCHING); | |
151 StartPrefetchDecoders(); | |
152 } else { | |
153 SetState(STATE_WAITING_FOR_CONFIG); | |
154 } | |
155 break; | |
156 case STATE_STOPPING: | |
157 SetPendingStart(true); | |
158 break; | |
159 default: | |
160 // Ignore | |
161 break; | |
162 } | |
114 } | 163 } |
115 | 164 |
116 void MediaCodecPlayer::Pause(bool is_media_related_action) { | 165 void MediaCodecPlayer::Pause(bool is_media_related_action) { |
117 RUN_ON_MEDIA_THREAD(Pause, is_media_related_action); | 166 RUN_ON_MEDIA_THREAD(Pause, is_media_related_action); |
118 | 167 |
119 // Media thread | |
120 DVLOG(1) << __FUNCTION__; | 168 DVLOG(1) << __FUNCTION__; |
121 | 169 |
122 NOTIMPLEMENTED(); | 170 switch (state_) { |
171 case STATE_PREFETCHING: | |
172 SetState(STATE_PAUSED); | |
173 StopDecoders(); | |
174 break; | |
175 case STATE_WAITING_FOR_SURFACE: | |
176 SetState(STATE_PAUSED); | |
177 StopDecoders(); | |
178 break; | |
179 case STATE_PLAYING: | |
180 SetState(STATE_STOPPING); | |
181 RequestToStopDecoders(); | |
182 break; | |
183 default: | |
184 // Ignore | |
185 break; | |
186 } | |
123 } | 187 } |
124 | 188 |
125 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { | 189 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { |
126 RUN_ON_MEDIA_THREAD(SeekTo, timestamp); | 190 RUN_ON_MEDIA_THREAD(SeekTo, timestamp); |
127 | 191 |
128 // Media thread | |
129 DVLOG(1) << __FUNCTION__ << " " << timestamp; | 192 DVLOG(1) << __FUNCTION__ << " " << timestamp; |
130 | |
131 NOTIMPLEMENTED(); | 193 NOTIMPLEMENTED(); |
132 } | 194 } |
133 | 195 |
134 void MediaCodecPlayer::Release() { | 196 void MediaCodecPlayer::Release() { |
135 RUN_ON_MEDIA_THREAD(Release); | 197 RUN_ON_MEDIA_THREAD(Release); |
136 | 198 |
137 // Media thread | |
138 DVLOG(1) << __FUNCTION__; | 199 DVLOG(1) << __FUNCTION__; |
139 | 200 |
140 NOTIMPLEMENTED(); | 201 SetState(STATE_PAUSED); |
202 ReleaseDecoderResources(); | |
141 } | 203 } |
142 | 204 |
143 void MediaCodecPlayer::SetVolume(double volume) { | 205 void MediaCodecPlayer::SetVolume(double volume) { |
144 RUN_ON_MEDIA_THREAD(SetVolume, volume); | 206 RUN_ON_MEDIA_THREAD(SetVolume, volume); |
145 | 207 |
146 // Media thread | |
147 DVLOG(1) << __FUNCTION__ << " " << volume; | 208 DVLOG(1) << __FUNCTION__ << " " << volume; |
148 | 209 audio_decoder_->SetVolume(volume); |
149 NOTIMPLEMENTED(); | |
150 } | 210 } |
151 | 211 |
152 int MediaCodecPlayer::GetVideoWidth() { | 212 int MediaCodecPlayer::GetVideoWidth() { |
153 // UI thread | |
154 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 213 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
155 | 214 return metadata_cache_.video_size.width(); |
156 NOTIMPLEMENTED(); | |
157 return 320; | |
158 } | 215 } |
159 | 216 |
160 int MediaCodecPlayer::GetVideoHeight() { | 217 int MediaCodecPlayer::GetVideoHeight() { |
161 // UI thread | |
162 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 218 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
163 | 219 return metadata_cache_.video_size.height(); |
164 NOTIMPLEMENTED(); | |
165 return 240; | |
166 } | 220 } |
167 | 221 |
168 base::TimeDelta MediaCodecPlayer::GetCurrentTime() { | 222 base::TimeDelta MediaCodecPlayer::GetCurrentTime() { |
169 // UI thread, Media thread | 223 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
170 NOTIMPLEMENTED(); | 224 return current_time_cache_; |
171 return base::TimeDelta(); | |
172 } | 225 } |
173 | 226 |
174 base::TimeDelta MediaCodecPlayer::GetDuration() { | 227 base::TimeDelta MediaCodecPlayer::GetDuration() { |
175 // UI thread | |
176 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 228 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
177 | 229 return metadata_cache_.duration; |
178 NOTIMPLEMENTED(); | |
179 return base::TimeDelta(); | |
180 } | 230 } |
181 | 231 |
182 bool MediaCodecPlayer::IsPlaying() { | 232 bool MediaCodecPlayer::IsPlaying() { |
183 // UI thread | |
184 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 233 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
185 NOTIMPLEMENTED(); | 234 return state_ == STATE_PLAYING; |
186 return false; | |
187 } | 235 } |
188 | 236 |
189 bool MediaCodecPlayer::CanPause() { | 237 bool MediaCodecPlayer::CanPause() { |
190 // UI thread | |
191 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 238 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
192 NOTIMPLEMENTED(); | 239 NOTIMPLEMENTED(); |
193 return false; | 240 return false; |
194 } | 241 } |
195 | 242 |
196 bool MediaCodecPlayer::CanSeekForward() { | 243 bool MediaCodecPlayer::CanSeekForward() { |
197 // UI thread | |
198 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 244 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
199 NOTIMPLEMENTED(); | 245 NOTIMPLEMENTED(); |
200 return false; | 246 return false; |
201 } | 247 } |
202 | 248 |
203 bool MediaCodecPlayer::CanSeekBackward() { | 249 bool MediaCodecPlayer::CanSeekBackward() { |
204 // UI thread | |
205 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 250 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
206 NOTIMPLEMENTED(); | 251 NOTIMPLEMENTED(); |
207 return false; | 252 return false; |
208 } | 253 } |
209 | 254 |
210 bool MediaCodecPlayer::IsPlayerReady() { | 255 bool MediaCodecPlayer::IsPlayerReady() { |
211 // UI thread | |
212 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 256 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
213 NOTIMPLEMENTED(); | 257 // This method is called to check whether it's safe to release the player when |
258 // the OS needs more resources. This class can be released at any time. | |
214 return true; | 259 return true; |
215 } | 260 } |
216 | 261 |
217 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) { | 262 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) { |
218 // UI thread | |
219 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | 263 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
220 NOTIMPLEMENTED(); | 264 NOTIMPLEMENTED(); |
221 } | 265 } |
222 | 266 |
223 // Callbacks from Demuxer. | 267 // Callbacks from Demuxer. |
224 | 268 |
225 void MediaCodecPlayer::OnDemuxerConfigsAvailable( | 269 void MediaCodecPlayer::OnDemuxerConfigsAvailable( |
226 const DemuxerConfigs& configs) { | 270 const DemuxerConfigs& configs) { |
227 // Media thread | 271 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
228 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 272 |
229 | 273 DVLOG(1) << __FUNCTION__; |
230 NOTIMPLEMENTED(); | 274 |
275 duration_ = configs.duration; | |
276 | |
277 SetDemuxerConfigs(configs); | |
278 | |
279 // Update cache and notify manager on UI thread | |
280 gfx::Size video_size = HasVideo() ? configs.video_size : gfx::Size(); | |
281 ui_task_runner_->PostTask( | |
282 FROM_HERE, base::Bind(metadata_changed_cb_, duration_, video_size)); | |
231 } | 283 } |
232 | 284 |
233 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { | 285 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { |
234 // Media thread | 286 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
235 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 287 |
236 NOTIMPLEMENTED(); | 288 DCHECK_LT(0u, data.access_units.size()); |
289 CHECK_GE(1u, data.demuxer_configs.size()); | |
290 | |
291 DVLOG(2) << "Player::" << __FUNCTION__; | |
292 | |
293 if (data.type == DemuxerStream::AUDIO) | |
294 audio_decoder_->OnDemuxerDataAvailable(data); | |
295 | |
296 if (data.type == DemuxerStream::VIDEO) | |
297 video_decoder_->OnDemuxerDataAvailable(data); | |
237 } | 298 } |
238 | 299 |
239 void MediaCodecPlayer::OnDemuxerSeekDone( | 300 void MediaCodecPlayer::OnDemuxerSeekDone( |
240 base::TimeDelta actual_browser_seek_time) { | 301 base::TimeDelta actual_browser_seek_time) { |
241 // Media thread | 302 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
242 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 303 |
304 DVLOG(1) << __FUNCTION__ << " actual_time:" << actual_browser_seek_time; | |
305 | |
243 NOTIMPLEMENTED(); | 306 NOTIMPLEMENTED(); |
244 } | 307 } |
245 | 308 |
246 void MediaCodecPlayer::OnDemuxerDurationChanged( | 309 void MediaCodecPlayer::OnDemuxerDurationChanged( |
247 base::TimeDelta duration) { | 310 base::TimeDelta duration) { |
248 // Media thread | 311 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
249 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | 312 DVLOG(1) << __FUNCTION__ << " duration:" << duration; |
250 NOTIMPLEMENTED(); | 313 |
251 } | 314 duration_ = duration; |
315 } | |
316 | |
317 // Events from Player, called on UI thread | |
318 | |
319 void MediaCodecPlayer::OnMediaMetadataChanged(base::TimeDelta duration, | |
320 const gfx::Size& video_size) { | |
321 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
322 | |
323 if (duration != kNoTimestamp()) | |
324 metadata_cache_.duration = duration; | |
325 | |
326 if (!video_size.IsEmpty()) | |
327 metadata_cache_.video_size = video_size; | |
328 | |
329 manager()->OnMediaMetadataChanged(player_id(), metadata_cache_.duration, | |
330 metadata_cache_.video_size.width(), | |
331 metadata_cache_.video_size.height(), true); | |
332 } | |
333 | |
334 void MediaCodecPlayer::OnTimeUpdate(base::TimeDelta current_timestamp, | |
335 base::TimeTicks current_time_ticks) { | |
336 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
337 | |
338 current_time_cache_ = current_timestamp; | |
339 manager()->OnTimeUpdate(player_id(), current_timestamp, current_time_ticks); | |
340 } | |
341 | |
342 // Events from Decoders, called on Media thread | |
343 | |
344 void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type) { | |
345 DVLOG(2) << __FUNCTION__ << " streamType:" << stream_type; | |
346 | |
347 // Use this method instead of directly binding with | |
348 // DemuxerAndroid::RequestDemuxerData() to avoid the race condition on | |
349 // deletion: | |
350 // 1. DeleteSoon is posted from UI to Media thread. | |
351 // 2. RequestDemuxerData callback is posted from Decoder to Media thread. | |
352 // 3. DeleteSoon arrives, we delete the player and detach from | |
353 // BrowserDemuxerAndroid. | |
354 // 4. RequestDemuxerData is processed by the media thread queue. Since the | |
355 // weak_ptr was invalidated in (3), this is a no-op. If we used | |
356 // DemuxerAndroid::RequestDemuxerData() it would arrive and will try to | |
357 // call the client, but the client (i.e. this player) would not exist. | |
358 demuxer_->RequestDemuxerData(stream_type); | |
359 } | |
360 | |
361 void MediaCodecPlayer::OnPrefetchDone() { | |
362 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
363 DVLOG(1) << __FUNCTION__; | |
364 | |
365 if (state_ != STATE_PREFETCHING) | |
366 return; // Ignore | |
367 | |
368 if (!HasAudio() && !HasVideo()) { | |
369 // No configuration at all after prefetching. | |
370 // This is an error, initial configuration is expected | |
371 // before the first data chunk. | |
372 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); | |
373 return; | |
374 } | |
375 | |
376 if (HasVideo() && !HasPendingSurface()) { | |
377 SetState(STATE_WAITING_FOR_SURFACE); | |
378 return; | |
379 } | |
380 | |
381 SetState(STATE_PLAYING); | |
382 StartPlaybackDecoders(); | |
383 } | |
384 | |
385 void MediaCodecPlayer::OnStopDone() { | |
386 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
387 DVLOG(1) << __FUNCTION__; | |
388 | |
389 if (!(audio_decoder_->IsStopped() && video_decoder_->IsStopped())) | |
390 return; // Wait until other stream is stopped | |
391 | |
392 // At this point decoder threads should not be running | |
393 if (interpolator_.interpolating()) | |
394 interpolator_.StopInterpolating(); | |
395 | |
396 switch (state_) { | |
397 case STATE_STOPPING: | |
398 if (HasPendingStart()) { | |
399 SetPendingStart(false); | |
400 SetState(STATE_PREFETCHING); | |
401 StartPrefetchDecoders(); | |
402 } else { | |
403 SetState(STATE_PAUSED); | |
404 } | |
405 break; | |
406 case STATE_PLAYING: | |
407 // Unexpected stop means completion | |
408 SetState(STATE_PAUSED); | |
409 break; | |
410 default: | |
411 DVLOG(0) << __FUNCTION__ << " illegal state: " << AsString(state_); | |
412 NOTREACHED(); | |
413 break; | |
414 } | |
415 | |
416 // DetachListener to UI thread | |
417 ui_task_runner_->PostTask(FROM_HERE, detach_listener_cb_); | |
418 | |
419 if (AudioFinished() && VideoFinished()) | |
420 ui_task_runner_->PostTask(FROM_HERE, completion_cb_); | |
421 } | |
422 | |
423 void MediaCodecPlayer::OnError() { | |
424 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
425 DVLOG(1) << __FUNCTION__; | |
426 | |
427 // STATE_ERROR blocks all events | |
428 SetState(STATE_ERROR); | |
429 | |
430 ReleaseDecoderResources(); | |
431 } | |
432 | |
433 void MediaCodecPlayer::OnStarvation(DemuxerStream::Type type) { | |
434 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
435 DVLOG(1) << __FUNCTION__; | |
wolenetz
2015/06/26 00:59:25
nit: log the |type|
Tima Vaisburd
2015/06/26 01:48:02
Done.
| |
436 | |
437 if (state_ != STATE_PLAYING) | |
438 return; // Ignore | |
439 | |
440 SetState(STATE_STOPPING); | |
441 RequestToStopDecoders(); | |
442 SetPendingStart(true); | |
443 } | |
444 | |
445 void MediaCodecPlayer::OnTimeIntervalUpdate(DemuxerStream::Type type, | |
446 base::TimeDelta now_playing, | |
447 base::TimeDelta last_buffered) { | |
448 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
449 | |
450 interpolator_.SetBounds(now_playing, last_buffered); | |
451 | |
452 // Post to UI thread | |
453 ui_task_runner_->PostTask(FROM_HERE, | |
454 base::Bind(time_update_cb_, GetInterpolatedTime(), | |
455 base::TimeTicks::Now())); | |
456 } | |
457 | |
458 void MediaCodecPlayer::OnVideoCodecCreated() { | |
459 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
460 | |
461 // This callback requests resources by releasing other players. | |
462 ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_); | |
463 } | |
464 | |
465 void MediaCodecPlayer::OnVideoSizeChanged(const gfx::Size& size) { | |
466 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
467 | |
468 DVLOG(1) << __FUNCTION__ << " " << size.width() << "x" << size.height(); | |
469 | |
470 // Update cache and notify manager on UI thread | |
471 ui_task_runner_->PostTask( | |
472 FROM_HERE, base::Bind(metadata_changed_cb_, kNoTimestamp(), size)); | |
473 } | |
474 | |
475 // State machine operations, called on Media thread | |
476 | |
477 void MediaCodecPlayer::SetState(PlayerState new_state) { | |
478 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
479 | |
480 DVLOG(1) << "SetState:" << AsString(state_) << " -> " << AsString(new_state); | |
481 state_ = new_state; | |
482 } | |
483 | |
484 void MediaCodecPlayer::SetPendingSurface(gfx::ScopedJavaSurface surface) { | |
485 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
486 DVLOG(1) << __FUNCTION__; | |
487 | |
488 video_decoder_->SetPendingSurface(surface.Pass()); | |
489 } | |
490 | |
491 bool MediaCodecPlayer::HasPendingSurface() { | |
492 return video_decoder_->HasPendingSurface(); | |
493 } | |
494 | |
495 void MediaCodecPlayer::SetPendingStart(bool need_to_start) { | |
496 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
497 DVLOG(1) << __FUNCTION__ << ": " << need_to_start; | |
498 pending_start_ = need_to_start; | |
499 } | |
500 | |
501 bool MediaCodecPlayer::HasPendingStart() { | |
502 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
503 return pending_start_; | |
504 } | |
505 | |
506 bool MediaCodecPlayer::HasAudio() { | |
507 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
508 return audio_decoder_->HasStream(); | |
509 } | |
510 | |
511 bool MediaCodecPlayer::HasVideo() { | |
512 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
513 return video_decoder_->HasStream(); | |
514 } | |
515 | |
516 void MediaCodecPlayer::SetDemuxerConfigs(const DemuxerConfigs& configs) { | |
517 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
518 DVLOG(1) << __FUNCTION__ << " " << configs; | |
519 | |
520 DCHECK(audio_decoder_); | |
521 DCHECK(video_decoder_); | |
522 | |
523 // At least one valid codec must be present. | |
524 DCHECK(configs.audio_codec != kUnknownAudioCodec || | |
525 configs.video_codec != kUnknownVideoCodec); | |
526 | |
527 if (configs.audio_codec != kUnknownAudioCodec) | |
528 audio_decoder_->SetDemuxerConfigs(configs); | |
529 | |
530 if (configs.video_codec != kUnknownVideoCodec) | |
531 video_decoder_->SetDemuxerConfigs(configs); | |
532 | |
533 if (state_ == STATE_WAITING_FOR_CONFIG) { | |
534 SetState(STATE_PREFETCHING); | |
535 StartPrefetchDecoders(); | |
536 } | |
537 } | |
538 | |
539 void MediaCodecPlayer::StartPrefetchDecoders() { | |
540 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
541 DVLOG(1) << __FUNCTION__; | |
542 | |
543 bool do_audio = false; | |
544 bool do_video = false; | |
545 int count = 0; | |
546 if (!AudioFinished()) { | |
547 do_audio = true; | |
548 ++count; | |
549 } | |
550 if (!VideoFinished()) { | |
551 do_video = true; | |
552 ++count; | |
553 } | |
554 | |
555 DCHECK_LT(0, count); // at least one decoder should be active | |
556 | |
557 base::Closure prefetch_cb = base::BarrierClosure( | |
558 count, base::Bind(&MediaCodecPlayer::OnPrefetchDone, media_weak_this_)); | |
559 | |
560 if (do_audio) | |
561 audio_decoder_->Prefetch(prefetch_cb); | |
562 | |
563 if (do_video) | |
564 video_decoder_->Prefetch(prefetch_cb); | |
565 } | |
566 | |
567 void MediaCodecPlayer::StartPlaybackDecoders() { | |
568 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
569 DVLOG(1) << __FUNCTION__; | |
570 | |
571 // Configure all streams before the start since | |
572 // we may discover that browser seek is required. | |
573 | |
574 bool do_audio = !AudioFinished(); | |
575 bool do_video = !VideoFinished(); | |
576 | |
577 // If there is nothing to play, the state machine should determine | |
578 // this at the prefetch state and never call this method. | |
579 DCHECK(do_audio || do_video); | |
580 | |
581 if (do_audio) { | |
582 MediaCodecDecoder::ConfigStatus status = audio_decoder_->Configure(); | |
583 if (status != MediaCodecDecoder::CONFIG_OK) { | |
584 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); | |
585 return; | |
586 } | |
587 } | |
588 | |
589 if (do_video) { | |
590 MediaCodecDecoder::ConfigStatus status = video_decoder_->Configure(); | |
591 if (status != MediaCodecDecoder::CONFIG_OK) { | |
592 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); | |
593 return; | |
594 } | |
595 } | |
596 | |
597 // At this point decoder threads should not be running. | |
598 if (!interpolator_.interpolating()) | |
599 interpolator_.StartInterpolating(); | |
600 | |
601 base::TimeDelta current_time = GetInterpolatedTime(); | |
602 | |
603 if (do_audio) { | |
604 if (!audio_decoder_->Start(current_time)) { | |
605 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); | |
606 return; | |
607 } | |
608 | |
609 // Attach listener on UI thread | |
610 ui_task_runner_->PostTask(FROM_HERE, attach_listener_cb_); | |
611 } | |
612 | |
613 if (do_video) { | |
614 if (!video_decoder_->Start(current_time)) { | |
615 GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_); | |
616 return; | |
617 } | |
618 } | |
619 } | |
620 | |
621 void MediaCodecPlayer::StopDecoders() { | |
622 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
623 DVLOG(1) << __FUNCTION__; | |
624 | |
625 audio_decoder_->SyncStop(); | |
626 video_decoder_->SyncStop(); | |
627 } | |
628 | |
629 void MediaCodecPlayer::RequestToStopDecoders() { | |
630 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
631 DVLOG(1) << __FUNCTION__; | |
632 | |
633 bool do_audio = false; | |
634 bool do_video = false; | |
635 | |
636 if (audio_decoder_->IsPrefetchingOrPlaying()) | |
637 do_audio = true; | |
638 if (video_decoder_->IsPrefetchingOrPlaying()) | |
639 do_video = true; | |
640 | |
641 if (!do_audio && !do_video) { | |
642 GetMediaTaskRunner()->PostTask( | |
643 FROM_HERE, base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_)); | |
644 return; | |
645 } | |
646 | |
647 if (do_audio) | |
648 audio_decoder_->RequestToStop(); | |
649 if (do_video) | |
650 video_decoder_->RequestToStop(); | |
651 } | |
652 | |
653 void MediaCodecPlayer::ReleaseDecoderResources() { | |
654 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
655 DVLOG(1) << __FUNCTION__; | |
656 | |
657 if (audio_decoder_) | |
658 audio_decoder_->ReleaseDecoderResources(); | |
659 | |
660 if (video_decoder_) | |
661 video_decoder_->ReleaseDecoderResources(); | |
662 | |
663 // At this point decoder threads should not be running | |
664 if (interpolator_.interpolating()) | |
665 interpolator_.StopInterpolating(); | |
666 } | |
667 | |
668 void MediaCodecPlayer::CreateDecoders() { | |
669 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
670 DVLOG(1) << __FUNCTION__; | |
671 | |
672 error_cb_ = base::Bind(&MediaCodecPlayer::OnError, media_weak_this_); | |
673 | |
674 audio_decoder_.reset(new MediaCodecAudioDecoder( | |
675 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData, | |
676 media_weak_this_, DemuxerStream::AUDIO), | |
677 base::Bind(&MediaCodecPlayer::OnStarvation, media_weak_this_, | |
678 DemuxerStream::AUDIO), | |
679 base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_), error_cb_, | |
680 base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate, media_weak_this_, | |
681 DemuxerStream::AUDIO))); | |
682 | |
683 video_decoder_.reset(new MediaCodecVideoDecoder( | |
684 GetMediaTaskRunner(), base::Bind(&MediaCodecPlayer::RequestDemuxerData, | |
685 media_weak_this_, DemuxerStream::VIDEO), | |
686 base::Bind(&MediaCodecPlayer::OnStarvation, media_weak_this_, | |
687 DemuxerStream::VIDEO), | |
688 base::Bind(&MediaCodecPlayer::OnStopDone, media_weak_this_), error_cb_, | |
689 MediaCodecDecoder::SetTimeCallback(), // null callback | |
690 base::Bind(&MediaCodecPlayer::OnVideoSizeChanged, media_weak_this_), | |
691 base::Bind(&MediaCodecPlayer::OnVideoCodecCreated, media_weak_this_))); | |
692 } | |
693 | |
694 bool MediaCodecPlayer::AudioFinished() { | |
695 return audio_decoder_->IsCompleted() || !audio_decoder_->HasStream(); | |
696 } | |
697 | |
698 bool MediaCodecPlayer::VideoFinished() { | |
699 return video_decoder_->IsCompleted() || !video_decoder_->HasStream(); | |
700 } | |
701 | |
702 base::TimeDelta MediaCodecPlayer::GetInterpolatedTime() { | |
703 DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); | |
704 | |
705 base::TimeDelta interpolated_time = interpolator_.GetInterpolatedTime(); | |
706 return std::min(interpolated_time, duration_); | |
707 } | |
708 | |
709 #undef RETURN_STRING | |
710 #define RETURN_STRING(x) \ | |
711 case x: \ | |
712 return #x; | |
713 | |
714 const char* MediaCodecPlayer::AsString(PlayerState state) { | |
715 switch (state) { | |
716 RETURN_STRING(STATE_PAUSED); | |
717 RETURN_STRING(STATE_WAITING_FOR_CONFIG); | |
718 RETURN_STRING(STATE_PREFETCHING); | |
719 RETURN_STRING(STATE_PLAYING); | |
720 RETURN_STRING(STATE_STOPPING); | |
721 RETURN_STRING(STATE_WAITING_FOR_SURFACE); | |
722 RETURN_STRING(STATE_ERROR); | |
723 } | |
724 return nullptr; // crash early | |
725 } | |
726 | |
727 #undef RETURN_STRING | |
252 | 728 |
253 } // namespace media | 729 } // namespace media |
OLD | NEW |