OLD | NEW |
---|---|
1 // Copyright (c) 2013 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_source_player.h" | 5 #include "media/base/android/media_source_player.h" |
6 | 6 |
7 #include <limits> | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | |
9 #include "base/logging.h" | |
10 #include "base/threading/thread.h" | |
8 | 11 |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_string.h" | |
11 #include "base/barrier_closure.h" | |
12 #include "base/basictypes.h" | |
13 #include "base/bind.h" | |
14 #include "base/callback_helpers.h" | |
15 #include "base/logging.h" | |
16 #include "base/strings/string_number_conversions.h" | |
17 #include "base/trace_event/trace_event.h" | |
18 #include "media/base/android/audio_decoder_job.h" | |
19 #include "media/base/android/media_drm_bridge.h" | |
20 #include "media/base/android/media_player_manager.h" | 12 #include "media/base/android/media_player_manager.h" |
21 #include "media/base/android/video_decoder_job.h" | |
22 | 13 |
23 namespace media { | 14 namespace media { |
24 | 15 |
16 class MediaThread : public base::Thread { | |
17 public: | |
18 MediaThread() : base::Thread("MediaThread") { | |
19 Start(); | |
20 } | |
21 }; | |
22 | |
23 // Create media thread | |
24 base::LazyInstance<MediaThread>::Leaky | |
25 g_media_thread = LAZY_INSTANCE_INITIALIZER; | |
26 | |
27 | |
28 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { | |
29 return g_media_thread.Pointer()->task_runner(); | |
30 } | |
31 | |
25 MediaSourcePlayer::MediaSourcePlayer( | 32 MediaSourcePlayer::MediaSourcePlayer( |
26 int player_id, | 33 int player_id, |
27 MediaPlayerManager* manager, | 34 MediaPlayerManager* manager, |
28 const RequestMediaResourcesCB& request_media_resources_cb, | 35 const RequestMediaResourcesCB& request_media_resources_cb, |
29 scoped_ptr<DemuxerAndroid> demuxer, | 36 scoped_ptr<DemuxerAndroid> demuxer, |
30 const GURL& frame_url) | 37 const GURL& frame_url) |
31 : MediaPlayerAndroid(player_id, | 38 : MediaPlayerAndroid(player_id, |
32 manager, | 39 manager, |
33 request_media_resources_cb, | 40 request_media_resources_cb, |
34 frame_url), | 41 frame_url), |
35 demuxer_(demuxer.Pass()), | 42 demuxer_(demuxer.Pass()), |
36 pending_event_(NO_EVENT_PENDING), | 43 weak_factory_(this) |
37 playing_(false), | 44 { |
38 interpolator_(&default_tick_clock_), | 45 DVLOG(1) << "MediaSourcePlayer::Player: player_id:" << player_id; |
39 doing_browser_seek_(false), | 46 |
40 pending_seek_(false), | |
41 drm_bridge_(NULL), | |
42 cdm_registration_id_(0), | |
43 is_waiting_for_key_(false), | |
44 key_added_while_decode_pending_(false), | |
45 is_waiting_for_audio_decoder_(false), | |
46 is_waiting_for_video_decoder_(false), | |
47 prerolling_(true), | |
48 weak_factory_(this) { | |
49 audio_decoder_job_.reset(new AudioDecoderJob( | |
50 base::Bind(&DemuxerAndroid::RequestDemuxerData, | |
51 base::Unretained(demuxer_.get()), | |
52 DemuxerStream::AUDIO), | |
53 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged, | |
54 weak_factory_.GetWeakPtr()))); | |
55 video_decoder_job_.reset(new VideoDecoderJob( | |
56 base::Bind(&DemuxerAndroid::RequestDemuxerData, | |
57 base::Unretained(demuxer_.get()), | |
58 DemuxerStream::VIDEO), | |
59 base::Bind(request_media_resources_cb_, player_id), | |
60 base::Bind(&MediaSourcePlayer::OnDemuxerConfigsChanged, | |
61 weak_factory_.GetWeakPtr()))); | |
62 demuxer_->Initialize(this); | 47 demuxer_->Initialize(this); |
63 interpolator_.SetUpperBound(base::TimeDelta()); | |
64 weak_this_ = weak_factory_.GetWeakPtr(); | 48 weak_this_ = weak_factory_.GetWeakPtr(); |
qinmin
2015/04/23 18:48:38
weak_ptr is bind to the UI thread here.
| |
65 } | 49 } |
66 | 50 |
67 MediaSourcePlayer::~MediaSourcePlayer() { | 51 MediaSourcePlayer::~MediaSourcePlayer() |
68 Release(); | 52 { |
69 DCHECK_EQ(!drm_bridge_, !cdm_registration_id_); | 53 // Called on UI thread, we need to release asynchronously |
70 if (drm_bridge_) { | |
71 drm_bridge_->UnregisterPlayer(cdm_registration_id_); | |
72 cdm_registration_id_ = 0; | |
73 } | |
74 } | 54 } |
75 | 55 |
76 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { | 56 void MediaSourcePlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { |
77 DVLOG(1) << __FUNCTION__; | 57 DVLOG(1) << __FUNCTION__; |
78 if (!video_decoder_job_->SetVideoSurface(surface.Pass())) | |
79 return; | |
80 // Retry video decoder creation. | |
81 RetryDecoderCreation(false, true); | |
82 } | |
83 | 58 |
84 void MediaSourcePlayer::ScheduleSeekEventAndStopDecoding( | 59 GetMediaTaskRunner()->PostTask( |
85 base::TimeDelta seek_time) { | 60 FROM_HERE, base::Bind(&MediaSourcePlayer::SetVideoSurfaceInternal, |
86 DVLOG(1) << __FUNCTION__ << "(" << seek_time.InSecondsF() << ")"; | 61 weak_this_, base::Passed(&surface))); |
qinmin
2015/04/23 18:48:38
and the weak ptr is now being used on the media th
timav
2015/04/23 23:06:12
Yes, I learned this later, while running real impl
| |
87 DCHECK(!IsEventPending(SEEK_EVENT_PENDING)); | |
88 | |
89 pending_seek_ = false; | |
90 | |
91 interpolator_.SetBounds(seek_time, seek_time); | |
92 | |
93 if (audio_decoder_job_->is_decoding()) | |
94 audio_decoder_job_->StopDecode(); | |
95 if (video_decoder_job_->is_decoding()) | |
96 video_decoder_job_->StopDecode(); | |
97 | |
98 SetPendingEvent(SEEK_EVENT_PENDING); | |
99 ProcessPendingEvents(); | |
100 } | |
101 | |
102 void MediaSourcePlayer::BrowserSeekToCurrentTime() { | |
103 DVLOG(1) << __FUNCTION__; | |
104 | |
105 DCHECK(!IsEventPending(SEEK_EVENT_PENDING)); | |
106 doing_browser_seek_ = true; | |
107 ScheduleSeekEventAndStopDecoding(GetCurrentTime()); | |
108 } | |
109 | |
110 bool MediaSourcePlayer::Seekable() { | |
111 // If the duration TimeDelta, converted to milliseconds from microseconds, | |
112 // is >= 2^31, then the media is assumed to be unbounded and unseekable. | |
113 // 2^31 is the bound due to java player using 32-bit integer for time | |
114 // values at millisecond resolution. | |
115 return duration_ < | |
116 base::TimeDelta::FromMilliseconds(std::numeric_limits<int32>::max()); | |
117 } | 62 } |
118 | 63 |
119 void MediaSourcePlayer::Start() { | 64 void MediaSourcePlayer::Start() { |
120 DVLOG(1) << __FUNCTION__; | 65 DVLOG(1) << __FUNCTION__; |
121 | 66 |
122 playing_ = true; | 67 GetMediaTaskRunner()->PostTask( |
123 | 68 FROM_HERE, base::Bind(&MediaSourcePlayer::StartInternal, weak_this_)); |
124 StartInternal(); | |
125 } | 69 } |
126 | 70 |
127 void MediaSourcePlayer::Pause(bool is_media_related_action) { | 71 void MediaSourcePlayer::Pause(bool /* is_media_related_action */) { |
128 DVLOG(1) << __FUNCTION__; | 72 DVLOG(1) << __FUNCTION__; |
129 | 73 |
130 // Since decoder jobs have their own thread, decoding is not fully paused | 74 GetMediaTaskRunner()->PostTask( |
131 // until all the decoder jobs call MediaDecoderCallback(). It is possible | 75 FROM_HERE, base::Bind(&MediaSourcePlayer::PauseInternal, weak_this_)); |
132 // that Start() is called while the player is waiting for | |
133 // MediaDecoderCallback(). In that case, decoding will continue when | |
134 // MediaDecoderCallback() is called. | |
135 playing_ = false; | |
136 start_time_ticks_ = base::TimeTicks(); | |
137 } | |
138 | |
139 bool MediaSourcePlayer::IsPlaying() { | |
140 return playing_; | |
141 } | |
142 | |
143 int MediaSourcePlayer::GetVideoWidth() { | |
144 return video_decoder_job_->output_width(); | |
145 } | |
146 | |
147 int MediaSourcePlayer::GetVideoHeight() { | |
148 return video_decoder_job_->output_height(); | |
149 } | 76 } |
150 | 77 |
151 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) { | 78 void MediaSourcePlayer::SeekTo(base::TimeDelta timestamp) { |
152 DVLOG(1) << __FUNCTION__ << "(" << timestamp.InSecondsF() << ")"; | 79 DVLOG(1) << __FUNCTION__ << " " << timestamp; |
153 | 80 |
154 if (IsEventPending(SEEK_EVENT_PENDING)) { | 81 GetMediaTaskRunner()->PostTask( |
155 DCHECK(doing_browser_seek_) << "SeekTo while SeekTo in progress"; | 82 FROM_HERE, |
156 DCHECK(!pending_seek_) << "SeekTo while SeekTo pending browser seek"; | 83 base::Bind(&MediaSourcePlayer::SeekToInternal, weak_this_, timestamp)); |
157 | |
158 // There is a browser seek currently in progress to obtain I-frame to feed | |
159 // a newly constructed video decoder. Remember this real seek request so | |
160 // it can be initiated once OnDemuxerSeekDone() occurs for the browser seek. | |
161 pending_seek_ = true; | |
162 pending_seek_time_ = timestamp; | |
163 return; | |
164 } | |
165 | |
166 doing_browser_seek_ = false; | |
167 ScheduleSeekEventAndStopDecoding(timestamp); | |
168 } | |
169 | |
170 base::TimeDelta MediaSourcePlayer::GetCurrentTime() { | |
171 return std::min(interpolator_.GetInterpolatedTime(), duration_); | |
172 } | |
173 | |
174 base::TimeDelta MediaSourcePlayer::GetDuration() { | |
175 return duration_; | |
176 } | 84 } |
177 | 85 |
178 void MediaSourcePlayer::Release() { | 86 void MediaSourcePlayer::Release() { |
179 DVLOG(1) << __FUNCTION__; | 87 DVLOG(1) << __FUNCTION__; |
180 | 88 |
181 audio_decoder_job_->ReleaseDecoderResources(); | 89 GetMediaTaskRunner()->PostTask( |
182 video_decoder_job_->ReleaseDecoderResources(); | 90 FROM_HERE, base::Bind(&MediaSourcePlayer::ReleaseInternal, weak_this_)); |
183 | |
184 // Prevent player restart, including job re-creation attempts. | |
185 playing_ = false; | |
186 | |
187 decoder_starvation_callback_.Cancel(); | |
188 | |
189 SetAudible(false); | |
190 DetachListener(); | |
191 } | 91 } |
192 | 92 |
193 void MediaSourcePlayer::SetVolume(double volume) { | 93 void MediaSourcePlayer::SetVolume(double volume) { |
194 audio_decoder_job_->SetVolume(volume); | 94 DVLOG(1) << __FUNCTION__ << " " << volume; |
95 } | |
96 | |
97 int MediaSourcePlayer::GetVideoWidth() { | |
98 return 320; | |
99 } | |
100 | |
101 int MediaSourcePlayer::GetVideoHeight() { | |
102 return 240; | |
103 } | |
104 | |
105 base::TimeDelta MediaSourcePlayer::GetCurrentTime() { | |
106 return base::TimeDelta(); | |
107 } | |
108 | |
109 base::TimeDelta MediaSourcePlayer::GetDuration() { | |
110 return base::TimeDelta(); | |
111 } | |
112 | |
113 bool MediaSourcePlayer::IsPlaying() { | |
114 return false; | |
195 } | 115 } |
196 | 116 |
197 bool MediaSourcePlayer::CanPause() { | 117 bool MediaSourcePlayer::CanPause() { |
198 return Seekable(); | 118 return false; |
199 } | 119 } |
200 | 120 |
201 bool MediaSourcePlayer::CanSeekForward() { | 121 bool MediaSourcePlayer::CanSeekForward() { |
202 return Seekable(); | 122 return false; |
203 } | 123 } |
204 | 124 |
205 bool MediaSourcePlayer::CanSeekBackward() { | 125 bool MediaSourcePlayer::CanSeekBackward() { |
206 return Seekable(); | 126 return false; |
207 } | 127 } |
208 | 128 |
209 bool MediaSourcePlayer::IsPlayerReady() { | 129 bool MediaSourcePlayer::IsPlayerReady() { |
210 return audio_decoder_job_ || video_decoder_job_; | 130 DVLOG(1) << __FUNCTION__; |
131 return true; | |
132 } | |
133 | |
134 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) { | |
135 DVLOG(1) << __FUNCTION__; | |
136 } | |
137 | |
138 void MediaSourcePlayer::OnDemuxerConfigsAvailable( | |
139 const DemuxerConfigs& params) { | |
140 DVLOG(1) << __FUNCTION__; | |
141 | |
142 // TODO: Post on UI thread | |
143 manager()->OnMediaMetadataChanged( | |
144 player_id(), base::TimeDelta::FromMilliseconds(100000), | |
145 GetVideoWidth(), GetVideoHeight(), true); | |
146 } | |
147 | |
148 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& params) { | |
149 DVLOG(1) << __FUNCTION__; | |
150 } | |
151 | |
152 void MediaSourcePlayer::OnDemuxerSeekDone( | |
153 base::TimeDelta actual_browser_seek_time) { | |
154 DVLOG(1) << __FUNCTION__ << " actual_time:" << actual_browser_seek_time; | |
155 } | |
156 | |
157 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) { | |
158 DVLOG(1) << __FUNCTION__ << " duration:" << duration; | |
159 } | |
160 | |
161 | |
162 // Events from UI, called on Media thread | |
163 | |
164 void MediaSourcePlayer::SetVideoSurfaceInternal( | |
165 gfx::ScopedJavaSurface surface) { | |
166 DVLOG(1) << __FUNCTION__; | |
211 } | 167 } |
212 | 168 |
213 void MediaSourcePlayer::StartInternal() { | 169 void MediaSourcePlayer::StartInternal() { |
214 DVLOG(1) << __FUNCTION__; | 170 DVLOG(1) << __FUNCTION__; |
215 // If there are pending events, wait for them finish. | |
216 if (pending_event_ != NO_EVENT_PENDING) | |
217 return; | |
218 | |
219 // When we start, we could have new demuxed data coming in. This new data | |
220 // could be clear (not encrypted) or encrypted with different keys. So key | |
221 // related info should all be cleared. | |
222 is_waiting_for_key_ = false; | |
223 key_added_while_decode_pending_ = false; | |
224 AttachListener(NULL); | |
225 | |
226 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | |
227 ProcessPendingEvents(); | |
228 } | 171 } |
229 | 172 |
230 void MediaSourcePlayer::OnDemuxerConfigsAvailable( | 173 void MediaSourcePlayer::PauseInternal() { |
231 const DemuxerConfigs& configs) { | |
232 DVLOG(1) << __FUNCTION__; | 174 DVLOG(1) << __FUNCTION__; |
233 DCHECK(!HasAudio() && !HasVideo()); | |
234 | |
235 duration_ = configs.duration; | |
236 | |
237 audio_decoder_job_->SetDemuxerConfigs(configs); | |
238 video_decoder_job_->SetDemuxerConfigs(configs); | |
239 OnDemuxerConfigsChanged(); | |
240 } | 175 } |
241 | 176 |
242 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) { | 177 void MediaSourcePlayer::SeekToInternal(base::TimeDelta timestamp) { |
243 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")"; | 178 DVLOG(1) << __FUNCTION__ << " " << timestamp; |
244 DCHECK_LT(0u, data.access_units.size()); | |
245 CHECK_GE(1u, data.demuxer_configs.size()); | |
246 | |
247 if (data.type == DemuxerStream::AUDIO) | |
248 audio_decoder_job_->OnDataReceived(data); | |
249 else if (data.type == DemuxerStream::VIDEO) | |
250 video_decoder_job_->OnDataReceived(data); | |
251 } | 179 } |
252 | 180 |
253 void MediaSourcePlayer::OnDemuxerDurationChanged(base::TimeDelta duration) { | 181 void MediaSourcePlayer::ReleaseInternal() { |
254 duration_ = duration; | |
255 } | |
256 | |
257 void MediaSourcePlayer::OnMediaCryptoReady() { | |
258 DCHECK(!drm_bridge_->GetMediaCrypto().is_null()); | |
259 drm_bridge_->SetMediaCryptoReadyCB(base::Closure()); | |
260 | |
261 // Retry decoder creation if the decoders are waiting for MediaCrypto. | |
262 RetryDecoderCreation(true, true); | |
263 } | |
264 | |
265 void MediaSourcePlayer::SetCdm(BrowserCdm* cdm) { | |
266 // Currently we don't support DRM change during the middle of playback, even | |
267 // if the player is paused. | |
268 // TODO(qinmin): support DRM change after playback has started. | |
269 // http://crbug.com/253792. | |
270 if (GetCurrentTime() > base::TimeDelta()) { | |
271 VLOG(0) << "Setting DRM bridge after playback has started. " | |
272 << "This is not well supported!"; | |
273 } | |
274 | |
275 if (drm_bridge_) { | |
276 NOTREACHED() << "Currently we do not support resetting CDM."; | |
277 return; | |
278 } | |
279 | |
280 // Only MediaDrmBridge will be set on MediaSourcePlayer. | |
281 drm_bridge_ = static_cast<MediaDrmBridge*>(cdm); | |
282 | |
283 cdm_registration_id_ = drm_bridge_->RegisterPlayer( | |
284 base::Bind(&MediaSourcePlayer::OnKeyAdded, weak_this_), | |
285 base::Bind(&MediaSourcePlayer::OnCdmUnset, weak_this_)); | |
286 | |
287 audio_decoder_job_->SetDrmBridge(drm_bridge_); | |
288 video_decoder_job_->SetDrmBridge(drm_bridge_); | |
289 | |
290 if (drm_bridge_->GetMediaCrypto().is_null()) { | |
291 drm_bridge_->SetMediaCryptoReadyCB( | |
292 base::Bind(&MediaSourcePlayer::OnMediaCryptoReady, weak_this_)); | |
293 return; | |
294 } | |
295 | |
296 // If the player is previously waiting for CDM, retry decoder creation. | |
297 RetryDecoderCreation(true, true); | |
298 } | |
299 | |
300 void MediaSourcePlayer::OnDemuxerSeekDone( | |
301 base::TimeDelta actual_browser_seek_time) { | |
302 DVLOG(1) << __FUNCTION__; | 182 DVLOG(1) << __FUNCTION__; |
303 | |
304 ClearPendingEvent(SEEK_EVENT_PENDING); | |
305 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) | |
306 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | |
307 | |
308 if (pending_seek_) { | |
309 DVLOG(1) << __FUNCTION__ << "processing pending seek"; | |
310 DCHECK(doing_browser_seek_); | |
311 pending_seek_ = false; | |
312 SeekTo(pending_seek_time_); | |
313 return; | |
314 } | |
315 | |
316 // It is possible that a browser seek to I-frame had to seek to a buffered | |
317 // I-frame later than the requested one due to data removal or GC. Update | |
318 // player clock to the actual seek target. | |
319 if (doing_browser_seek_) { | |
320 DCHECK(actual_browser_seek_time != kNoTimestamp()); | |
321 base::TimeDelta seek_time = actual_browser_seek_time; | |
322 // A browser seek must not jump into the past. Ideally, it seeks to the | |
323 // requested time, but it might jump into the future. | |
324 DCHECK(seek_time >= GetCurrentTime()); | |
325 DVLOG(1) << __FUNCTION__ << " : setting clock to actual browser seek time: " | |
326 << seek_time.InSecondsF(); | |
327 interpolator_.SetBounds(seek_time, seek_time); | |
328 audio_decoder_job_->SetBaseTimestamp(seek_time); | |
329 } else { | |
330 DCHECK(actual_browser_seek_time == kNoTimestamp()); | |
331 } | |
332 | |
333 base::TimeDelta current_time = GetCurrentTime(); | |
334 // TODO(qinmin): Simplify the logic by using |start_presentation_timestamp_| | |
335 // to preroll media decoder jobs. Currently |start_presentation_timestamp_| | |
336 // is calculated from decoder output, while preroll relies on the access | |
337 // unit's timestamp. There are some differences between the two. | |
338 preroll_timestamp_ = current_time; | |
339 if (HasAudio()) | |
340 audio_decoder_job_->BeginPrerolling(preroll_timestamp_); | |
341 if (HasVideo()) | |
342 video_decoder_job_->BeginPrerolling(preroll_timestamp_); | |
343 prerolling_ = true; | |
344 | |
345 if (!doing_browser_seek_) | |
346 manager()->OnSeekComplete(player_id(), current_time); | |
347 | |
348 ProcessPendingEvents(); | |
349 } | |
350 | |
351 void MediaSourcePlayer::UpdateTimestamps( | |
352 base::TimeDelta current_presentation_timestamp, | |
353 base::TimeDelta max_presentation_timestamp) { | |
354 interpolator_.SetBounds(current_presentation_timestamp, | |
355 max_presentation_timestamp); | |
356 manager()->OnTimeUpdate(player_id(), | |
357 GetCurrentTime(), | |
358 base::TimeTicks::Now()); | |
359 } | |
360 | |
361 void MediaSourcePlayer::ProcessPendingEvents() { | |
362 DVLOG(1) << __FUNCTION__ << " : 0x" << std::hex << pending_event_; | |
363 // Wait for all the decoding jobs to finish before processing pending tasks. | |
364 if (video_decoder_job_->is_decoding()) { | |
365 DVLOG(1) << __FUNCTION__ << " : A video job is still decoding."; | |
366 return; | |
367 } | |
368 | |
369 if (audio_decoder_job_->is_decoding()) { | |
370 DVLOG(1) << __FUNCTION__ << " : An audio job is still decoding."; | |
371 return; | |
372 } | |
373 | |
374 if (IsEventPending(PREFETCH_DONE_EVENT_PENDING)) { | |
375 DVLOG(1) << __FUNCTION__ << " : PREFETCH_DONE still pending."; | |
376 return; | |
377 } | |
378 | |
379 if (IsEventPending(SEEK_EVENT_PENDING)) { | |
380 DVLOG(1) << __FUNCTION__ << " : Handling SEEK_EVENT"; | |
381 ClearDecodingData(); | |
382 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime()); | |
383 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_); | |
384 return; | |
385 } | |
386 | |
387 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) { | |
388 // Don't continue if one of the decoder is not created. | |
389 if (is_waiting_for_audio_decoder_ || is_waiting_for_video_decoder_) | |
390 return; | |
391 ClearPendingEvent(DECODER_CREATION_EVENT_PENDING); | |
392 } | |
393 | |
394 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) { | |
395 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT."; | |
396 | |
397 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1); | |
398 | |
399 // It is possible that all streams have finished decode, yet starvation | |
400 // occurred during the last stream's EOS decode. In this case, prefetch is a | |
401 // no-op. | |
402 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | |
403 if (count == 0) | |
404 return; | |
405 | |
406 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING); | |
407 base::Closure barrier = BarrierClosure( | |
408 count, base::Bind(&MediaSourcePlayer::OnPrefetchDone, weak_this_)); | |
409 | |
410 if (!AudioFinished()) | |
411 audio_decoder_job_->Prefetch(barrier); | |
412 | |
413 if (!VideoFinished()) | |
414 video_decoder_job_->Prefetch(barrier); | |
415 | |
416 return; | |
417 } | |
418 | |
419 DCHECK_EQ(pending_event_, NO_EVENT_PENDING); | |
420 | |
421 // Now that all pending events have been handled, resume decoding if we are | |
422 // still playing. | |
423 if (playing_) | |
424 StartInternal(); | |
425 } | |
426 | |
427 void MediaSourcePlayer::MediaDecoderCallback( | |
428 bool is_audio, MediaCodecStatus status, | |
429 base::TimeDelta current_presentation_timestamp, | |
430 base::TimeDelta max_presentation_timestamp) { | |
431 DVLOG(1) << __FUNCTION__ << ": " << is_audio << ", " << status; | |
432 | |
433 // TODO(xhwang): Drop IntToString() when http://crbug.com/303899 is fixed. | |
434 if (is_audio) { | |
435 TRACE_EVENT_ASYNC_END1("media", | |
436 "MediaSourcePlayer::DecodeMoreAudio", | |
437 audio_decoder_job_.get(), | |
438 "MediaCodecStatus", | |
439 base::IntToString(status)); | |
440 } else { | |
441 TRACE_EVENT_ASYNC_END1("media", | |
442 "MediaSourcePlayer::DecodeMoreVideo", | |
443 video_decoder_job_.get(), | |
444 "MediaCodecStatus", | |
445 base::IntToString(status)); | |
446 } | |
447 | |
448 // Let tests hook the completion of this decode cycle. | |
449 if (!decode_callback_for_testing_.is_null()) | |
450 base::ResetAndReturn(&decode_callback_for_testing_).Run(); | |
451 | |
452 bool is_clock_manager = is_audio || !HasAudio(); | |
453 | |
454 if (is_clock_manager) | |
455 decoder_starvation_callback_.Cancel(); | |
456 | |
457 if (status == MEDIA_CODEC_ERROR) { | |
458 DVLOG(1) << __FUNCTION__ << " : decode error"; | |
459 Release(); | |
460 manager()->OnError(player_id(), MEDIA_ERROR_DECODE); | |
461 return; | |
462 } | |
463 | |
464 DCHECK(!IsEventPending(PREFETCH_DONE_EVENT_PENDING)); | |
465 | |
466 // Let |SEEK_EVENT_PENDING| (the highest priority event outside of | |
467 // |PREFETCH_DONE_EVENT_PENDING|) preempt output EOS detection here. Process | |
468 // any other pending events only after handling EOS detection. | |
469 if (IsEventPending(SEEK_EVENT_PENDING)) { | |
470 ProcessPendingEvents(); | |
471 return; | |
472 } | |
473 | |
474 if ((status == MEDIA_CODEC_OK || status == MEDIA_CODEC_INPUT_END_OF_STREAM) && | |
475 is_clock_manager && current_presentation_timestamp != kNoTimestamp()) { | |
476 UpdateTimestamps(current_presentation_timestamp, | |
477 max_presentation_timestamp); | |
478 } | |
479 | |
480 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) { | |
481 PlaybackCompleted(is_audio); | |
482 if (is_clock_manager) | |
483 interpolator_.StopInterpolating(); | |
484 } | |
485 | |
486 if (pending_event_ != NO_EVENT_PENDING) { | |
487 ProcessPendingEvents(); | |
488 return; | |
489 } | |
490 | |
491 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) { | |
492 if (is_audio) | |
493 SetAudible(false); | |
494 return; | |
495 } | |
496 | |
497 if (!playing_) { | |
498 if (is_clock_manager) | |
499 interpolator_.StopInterpolating(); | |
500 | |
501 if (is_audio) | |
502 SetAudible(false); | |
503 return; | |
504 } | |
505 | |
506 if (status == MEDIA_CODEC_NO_KEY) { | |
507 if (key_added_while_decode_pending_) { | |
508 DVLOG(2) << __FUNCTION__ << ": Key was added during decoding."; | |
509 ResumePlaybackAfterKeyAdded(); | |
510 } else { | |
511 if (is_audio) | |
512 SetAudible(false); | |
513 | |
514 is_waiting_for_key_ = true; | |
515 manager()->OnWaitingForDecryptionKey(player_id()); | |
516 } | |
517 return; | |
518 } | |
519 | |
520 // If |key_added_while_decode_pending_| is true and both audio and video | |
521 // decoding succeeded, we should clear |key_added_while_decode_pending_| here. | |
522 // But that would add more complexity into this function. If we don't clear it | |
523 // here, the worst case would be we call ResumePlaybackAfterKeyAdded() when | |
524 // we don't really have a new key. This should rarely happen and the | |
525 // performance impact should be pretty small. | |
526 // TODO(qinmin/xhwang): This class is complicated because we handle both audio | |
527 // and video in one file. If we separate them, we should be able to remove a | |
528 // lot of duplication. | |
529 | |
530 // If the status is MEDIA_CODEC_ABORT, stop decoding new data. The player is | |
531 // in the middle of a seek or stop event and needs to wait for the IPCs to | |
532 // come. | |
533 if (status == MEDIA_CODEC_ABORT) { | |
534 if (is_audio) | |
535 SetAudible(false); | |
536 return; | |
537 } | |
538 | |
539 if (prerolling_ && IsPrerollFinished(is_audio)) { | |
540 if (IsPrerollFinished(!is_audio)) { | |
541 prerolling_ = false; | |
542 StartInternal(); | |
543 } | |
544 return; | |
545 } | |
546 | |
547 // We successfully decoded a frame and going to the next one. | |
548 // Set the audible state. | |
549 if (is_audio) { | |
550 bool is_audible = !prerolling_ && audio_decoder_job_->volume() > 0; | |
551 SetAudible(is_audible); | |
552 } | |
553 | |
554 if (is_clock_manager) { | |
555 // If we have a valid timestamp, start the starvation callback. Otherwise, | |
556 // reset the |start_time_ticks_| so that the next frame will not suffer | |
557 // from the decoding delay caused by the current frame. | |
558 if (current_presentation_timestamp != kNoTimestamp()) | |
559 StartStarvationCallback(current_presentation_timestamp, | |
560 max_presentation_timestamp); | |
561 else | |
562 start_time_ticks_ = base::TimeTicks::Now(); | |
563 } | |
564 | |
565 if (is_audio) | |
566 DecodeMoreAudio(); | |
567 else | |
568 DecodeMoreVideo(); | |
569 } | |
570 | |
571 bool MediaSourcePlayer::IsPrerollFinished(bool is_audio) const { | |
572 if (is_audio) | |
573 return !HasAudio() || !audio_decoder_job_->prerolling(); | |
574 return !HasVideo() || !video_decoder_job_->prerolling(); | |
575 } | |
576 | |
577 void MediaSourcePlayer::DecodeMoreAudio() { | |
578 DVLOG(1) << __FUNCTION__; | |
579 DCHECK(!audio_decoder_job_->is_decoding()); | |
580 DCHECK(!AudioFinished()); | |
581 | |
582 MediaDecoderJob::MediaDecoderJobStatus status = audio_decoder_job_->Decode( | |
583 start_time_ticks_, | |
584 start_presentation_timestamp_, | |
585 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, true)); | |
586 | |
587 switch (status) { | |
588 case MediaDecoderJob::STATUS_SUCCESS: | |
589 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio", | |
590 audio_decoder_job_.get()); | |
591 break; | |
592 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED: | |
593 NOTREACHED(); | |
594 break; | |
595 case MediaDecoderJob::STATUS_FAILURE: | |
596 is_waiting_for_audio_decoder_ = true; | |
597 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING)) | |
598 SetPendingEvent(DECODER_CREATION_EVENT_PENDING); | |
599 break; | |
600 } | |
601 } | |
602 | |
603 void MediaSourcePlayer::DecodeMoreVideo() { | |
604 DVLOG(1) << __FUNCTION__; | |
605 DCHECK(!video_decoder_job_->is_decoding()); | |
606 DCHECK(!VideoFinished()); | |
607 | |
608 MediaDecoderJob::MediaDecoderJobStatus status = video_decoder_job_->Decode( | |
609 start_time_ticks_, | |
610 start_presentation_timestamp_, | |
611 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, weak_this_, | |
612 false)); | |
613 | |
614 switch (status) { | |
615 case MediaDecoderJob::STATUS_SUCCESS: | |
616 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo", | |
617 video_decoder_job_.get()); | |
618 break; | |
619 case MediaDecoderJob::STATUS_KEY_FRAME_REQUIRED: | |
620 BrowserSeekToCurrentTime(); | |
621 break; | |
622 case MediaDecoderJob::STATUS_FAILURE: | |
623 is_waiting_for_video_decoder_ = true; | |
624 if (!IsEventPending(DECODER_CREATION_EVENT_PENDING)) | |
625 SetPendingEvent(DECODER_CREATION_EVENT_PENDING); | |
626 break; | |
627 } | |
628 } | |
629 | |
630 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) { | |
631 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")"; | |
632 | |
633 if (AudioFinished() && VideoFinished()) { | |
634 playing_ = false; | |
635 start_time_ticks_ = base::TimeTicks(); | |
636 manager()->OnPlaybackComplete(player_id()); | |
637 } | |
638 } | |
639 | |
640 void MediaSourcePlayer::ClearDecodingData() { | |
641 DVLOG(1) << __FUNCTION__; | |
642 audio_decoder_job_->Flush(); | |
643 video_decoder_job_->Flush(); | |
644 start_time_ticks_ = base::TimeTicks(); | |
645 } | |
646 | |
647 bool MediaSourcePlayer::HasVideo() const { | |
648 return video_decoder_job_->HasStream(); | |
649 } | |
650 | |
651 bool MediaSourcePlayer::HasAudio() const { | |
652 return audio_decoder_job_->HasStream(); | |
653 } | |
654 | |
655 bool MediaSourcePlayer::AudioFinished() { | |
656 return audio_decoder_job_->OutputEOSReached() || !HasAudio(); | |
657 } | |
658 | |
659 bool MediaSourcePlayer::VideoFinished() { | |
660 return video_decoder_job_->OutputEOSReached() || !HasVideo(); | |
661 } | |
662 | |
663 void MediaSourcePlayer::OnDecoderStarved() { | |
664 DVLOG(1) << __FUNCTION__; | |
665 | |
666 if (HasAudio()) { | |
667 // If the starvation timer fired but there are no encoded frames | |
668 // in the queue we believe the demuxer (i.e. renderer process) froze. | |
669 if (!audio_decoder_job_->HasData()) | |
670 SetAudible(false); | |
671 } | |
672 | |
673 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | |
674 ProcessPendingEvents(); | |
675 } | |
676 | |
677 void MediaSourcePlayer::StartStarvationCallback( | |
678 base::TimeDelta current_presentation_timestamp, | |
679 base::TimeDelta max_presentation_timestamp) { | |
680 // 20ms was chosen because it is the typical size of a compressed audio frame. | |
681 // Anything smaller than this would likely cause unnecessary cycling in and | |
682 // out of the prefetch state. | |
683 const base::TimeDelta kMinStarvationTimeout = | |
684 base::TimeDelta::FromMilliseconds(20); | |
685 | |
686 base::TimeDelta current_timestamp = GetCurrentTime(); | |
687 base::TimeDelta timeout; | |
688 if (HasAudio()) { | |
689 timeout = max_presentation_timestamp - current_timestamp; | |
690 } else { | |
691 DCHECK(current_timestamp <= current_presentation_timestamp); | |
692 | |
693 // For video only streams, fps can be estimated from the difference | |
694 // between the previous and current presentation timestamps. The | |
695 // previous presentation timestamp is equal to current_timestamp. | |
696 // TODO(qinmin): determine whether 2 is a good coefficient for estimating | |
697 // video frame timeout. | |
698 timeout = 2 * (current_presentation_timestamp - current_timestamp); | |
699 } | |
700 | |
701 timeout = std::max(timeout, kMinStarvationTimeout); | |
702 | |
703 decoder_starvation_callback_.Reset( | |
704 base::Bind(&MediaSourcePlayer::OnDecoderStarved, weak_this_)); | |
705 base::MessageLoop::current()->PostDelayedTask( | |
706 FROM_HERE, decoder_starvation_callback_.callback(), timeout); | |
707 } | |
708 | |
709 void MediaSourcePlayer::OnPrefetchDone() { | |
710 DVLOG(1) << __FUNCTION__; | |
711 DCHECK(!audio_decoder_job_->is_decoding()); | |
712 DCHECK(!video_decoder_job_->is_decoding()); | |
713 | |
714 // A previously posted OnPrefetchDone() could race against a Release(). If | |
715 // Release() won the race, we should no longer have decoder jobs. | |
716 // TODO(qinmin/wolenetz): Maintain channel state to not double-request data | |
717 // or drop data received across Release()+Start(). See http://crbug.com/306314 | |
718 // and http://crbug.com/304234. | |
719 if (!IsEventPending(PREFETCH_DONE_EVENT_PENDING)) { | |
720 DVLOG(1) << __FUNCTION__ << " : aborting"; | |
721 return; | |
722 } | |
723 | |
724 ClearPendingEvent(PREFETCH_DONE_EVENT_PENDING); | |
725 | |
726 if (pending_event_ != NO_EVENT_PENDING) { | |
727 ProcessPendingEvents(); | |
728 return; | |
729 } | |
730 | |
731 if (!playing_) | |
732 return; | |
733 | |
734 start_time_ticks_ = base::TimeTicks::Now(); | |
735 start_presentation_timestamp_ = GetCurrentTime(); | |
736 if (!interpolator_.interpolating()) | |
737 interpolator_.StartInterpolating(); | |
738 | |
739 if (!AudioFinished()) | |
740 DecodeMoreAudio(); | |
741 | |
742 if (!VideoFinished()) | |
743 DecodeMoreVideo(); | |
744 } | |
745 | |
746 void MediaSourcePlayer::OnDemuxerConfigsChanged() { | |
747 manager()->OnMediaMetadataChanged( | |
748 player_id(), duration_, GetVideoWidth(), GetVideoHeight(), true); | |
749 } | |
750 | |
751 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) { | |
752 // Please keep this in sync with PendingEventFlags. | |
753 static const char* kPendingEventNames[] = { | |
754 "PREFETCH_DONE", | |
755 "SEEK", | |
756 "DECODER_CREATION", | |
757 "PREFETCH_REQUEST", | |
758 }; | |
759 | |
760 int mask = 1; | |
761 for (size_t i = 0; i < arraysize(kPendingEventNames); ++i, mask <<= 1) { | |
762 if (event & mask) | |
763 return kPendingEventNames[i]; | |
764 } | |
765 | |
766 return "UNKNOWN"; | |
767 } | |
768 | |
769 bool MediaSourcePlayer::IsEventPending(PendingEventFlags event) const { | |
770 return pending_event_ & event; | |
771 } | |
772 | |
773 void MediaSourcePlayer::SetPendingEvent(PendingEventFlags event) { | |
774 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")"; | |
775 DCHECK_NE(event, NO_EVENT_PENDING); | |
776 DCHECK(!IsEventPending(event)); | |
777 | |
778 pending_event_ |= event; | |
779 } | |
780 | |
781 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) { | |
782 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")"; | |
783 DCHECK_NE(event, NO_EVENT_PENDING); | |
784 DCHECK(IsEventPending(event)) << GetEventName(event); | |
785 | |
786 pending_event_ &= ~event; | |
787 } | |
788 | |
789 void MediaSourcePlayer::RetryDecoderCreation(bool audio, bool video) { | |
790 if (audio) | |
791 is_waiting_for_audio_decoder_ = false; | |
792 if (video) | |
793 is_waiting_for_video_decoder_ = false; | |
794 if (IsEventPending(DECODER_CREATION_EVENT_PENDING)) | |
795 ProcessPendingEvents(); | |
796 } | |
797 | |
798 void MediaSourcePlayer::OnKeyAdded() { | |
799 DVLOG(1) << __FUNCTION__; | |
800 | |
801 if (is_waiting_for_key_) { | |
802 ResumePlaybackAfterKeyAdded(); | |
803 return; | |
804 } | |
805 | |
806 if ((audio_decoder_job_->is_content_encrypted() && | |
807 audio_decoder_job_->is_decoding()) || | |
808 (video_decoder_job_->is_content_encrypted() && | |
809 video_decoder_job_->is_decoding())) { | |
810 DVLOG(1) << __FUNCTION__ << ": " << "Key added during pending decode."; | |
811 key_added_while_decode_pending_ = true; | |
812 } | |
813 } | |
814 | |
815 void MediaSourcePlayer::ResumePlaybackAfterKeyAdded() { | |
816 DVLOG(1) << __FUNCTION__; | |
817 DCHECK(is_waiting_for_key_ || key_added_while_decode_pending_); | |
818 | |
819 is_waiting_for_key_ = false; | |
820 key_added_while_decode_pending_ = false; | |
821 | |
822 // StartInternal() will trigger a prefetch, where in most cases we'll just | |
823 // use previously received data. | |
824 if (playing_) | |
825 StartInternal(); | |
826 } | |
827 | |
828 void MediaSourcePlayer::OnCdmUnset() { | |
829 DVLOG(1) << __FUNCTION__; | |
830 DCHECK(drm_bridge_); | |
831 // TODO(xhwang): Currently this is only called during teardown. Support full | |
832 // detachment of CDM during playback. This will be needed when we start to | |
833 // support setMediaKeys(0) (see http://crbug.com/330324), or when we release | |
834 // MediaDrm when the video is paused, or when the device goes to sleep (see | |
835 // http://crbug.com/272421). | |
836 audio_decoder_job_->SetDrmBridge(NULL); | |
837 video_decoder_job_->SetDrmBridge(NULL); | |
838 cdm_registration_id_ = 0; | |
839 drm_bridge_ = NULL; | |
840 } | 183 } |
841 | 184 |
842 } // namespace media | 185 } // namespace media |
OLD | NEW |