OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/android/media_codec_decoder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/callback_helpers.h" | |
10 #include "base/logging.h" | |
11 #include "media/base/android/media_codec_bridge.h" | |
12 | |
13 //#include "base/tvlog.h" | |
14 | |
15 namespace media { | |
16 | |
17 namespace { | |
18 | |
19 // Stop requesting new data in the PREFETCH state when | |
20 // the queue size rached this limit. | |
21 const int PREFETCH_LIMIT = 8; | |
22 | |
23 // Request new data in the RUNNING state if the size of the queue | |
24 // is less than this. | |
25 const int PLAYBACK_LOW_LIMIT = 4; | |
26 | |
27 // Posting delay of the next frame processing, in milliseconds | |
28 const int NEXT_FRAME_DELAY_MS = 2; | |
29 } | |
30 | |
31 MediaCodecDecoder::MediaCodecDecoder( | |
32 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | |
33 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
34 const base::Closure& request_data_cb, | |
35 const base::Closure& starvation_cb, | |
36 const base::Closure& stop_done_cb, | |
37 const base::Closure& error_cb, | |
38 const char* decoder_thread_name) | |
39 : media_task_runner_(media_task_runner), | |
40 ui_task_runner_(ui_task_runner), | |
41 decoder_thread_(decoder_thread_name), | |
42 request_data_cb_(request_data_cb), | |
43 starvation_cb_(starvation_cb), | |
44 stop_done_cb_(stop_done_cb), | |
45 error_cb_(error_cb), | |
46 state_(STOPPED), | |
47 eos_enqueued_(false), | |
48 completed_(false), | |
49 last_frame_posted_(false), | |
50 weak_factory_(this) { | |
51 // Media thread | |
52 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
53 | |
54 DVLOG(1) << "Decoder::Decoder() " << decoder_thread_name; | |
55 | |
56 weak_this_ = weak_factory_.GetWeakPtr(); | |
57 } | |
58 | |
59 MediaCodecDecoder::~MediaCodecDecoder() | |
60 { | |
qinmin
2015/05/13 23:17:31
{ belongs to the previous line
Tima Vaisburd
2015/05/14 00:14:59
Done.
| |
61 // Media thread | |
62 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
63 | |
64 DVLOG(1) << "Decoder::~Decoder()"; | |
65 | |
66 // NB: ReleaseDecoderResources() is virtual | |
67 ReleaseDecoderResources(); | |
68 } | |
69 | |
70 void MediaCodecDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) { | |
71 // Media thread | |
72 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
73 | |
74 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
75 | |
76 base::AutoLock lock(configs_lock_); | |
77 configs_ = configs; | |
78 } | |
79 | |
80 base::android::ScopedJavaLocalRef<jobject> | |
81 MediaCodecDecoder::GetMediaCrypto() { | |
82 base::android::ScopedJavaLocalRef<jobject> media_crypto; | |
83 | |
84 // drm_bridge_ is not implemented | |
85 // if (drm_bridge_) | |
86 // media_crypto = drm_bridge_->GetMediaCrypto(); | |
87 return media_crypto; | |
88 } | |
89 | |
90 void MediaCodecDecoder::Prefetch(const base::Closure& prefetch_done_cb) { | |
91 // Media thread | |
92 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
93 | |
94 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
95 | |
96 DCHECK(GetState() == STOPPED); | |
97 | |
98 prefetch_done_cb_ = prefetch_done_cb; | |
99 | |
100 SetState(PREFETCHING); | |
101 PrefetchNextChunk(); | |
102 } | |
103 | |
104 MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure() { | |
105 // Media thread | |
106 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
107 | |
108 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
109 | |
110 // Here I assume that OnDemuxerConfigsAvailable won't come | |
111 // in the middle of demuxer data. | |
112 | |
113 bool is_reconfigure_needed = !media_codec_bridge_; | |
114 // || IsCodecReconfigureNeeded(configs_, configs_); | |
115 | |
116 if (!is_reconfigure_needed) { | |
117 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
118 << ": reconfiguration is not required, ignoring"; | |
119 return CONFIG_OK; | |
120 } | |
121 | |
122 return ConfigureInternal(); | |
123 } | |
124 | |
125 bool MediaCodecDecoder::Start(base::TimeDelta current_time) { | |
126 // Media thread | |
127 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
128 | |
129 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
130 << " current_time:" << current_time; | |
131 | |
132 if (state_ == RUNNING) { | |
133 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": already started"; | |
134 return true; // already started | |
135 } | |
136 | |
137 if (state_ == STOPPING) { | |
138 DVLOG(0) << class_name() << "::" << __FUNCTION__ | |
139 << ": wrong state STOPPING, ignoring"; | |
140 return false; | |
141 } | |
142 | |
143 DCHECK(!decoder_thread_.IsRunning()); | |
144 | |
145 // We only synchronize video stream. | |
146 // When audio is present, the |current_time| is audio time. | |
147 SynchronizePTSWithTime(current_time); | |
148 | |
149 last_frame_posted_ = false; | |
150 | |
151 // Start the decoder thread | |
152 if (!decoder_thread_.Start()) { | |
153 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
154 << ": cannot start decoder thread"; | |
155 return false; | |
156 } | |
157 | |
158 SetState(RUNNING); | |
159 | |
160 decoder_thread_.task_runner()->PostTask( | |
161 FROM_HERE, | |
162 base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this))); | |
163 | |
164 return true; | |
165 } | |
166 | |
167 void MediaCodecDecoder::SyncStop() { | |
168 // Media thread | |
169 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
170 | |
171 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
172 | |
173 // After this method returns, decoder thread will not be running. | |
174 | |
175 decoder_thread_.Stop(); // synchronous | |
176 state_ = STOPPED; | |
177 | |
178 // Shall we move |delayed_buffers_| from VideoDecoder to Decoder class? | |
179 ReleaseDelayedBuffers(); | |
180 } | |
181 | |
182 void MediaCodecDecoder::RequestToStop() { | |
183 // Media thread | |
184 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
185 | |
186 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
187 | |
188 DCHECK(GetState() == RUNNING); | |
189 SetState(STOPPING); | |
190 } | |
191 | |
192 void MediaCodecDecoder::OnLastFrameRendered(bool completed) { | |
193 // Media thread | |
194 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
195 | |
196 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
197 << " completed:" << completed; | |
198 | |
199 decoder_thread_.Stop(); // synchronous | |
200 state_ = STOPPED; | |
201 completed_ = completed; | |
202 | |
203 media_task_runner_->PostTask(FROM_HERE, stop_done_cb_); | |
204 } | |
205 | |
206 void MediaCodecDecoder::Flush() { | |
207 // Media thread | |
208 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
209 | |
210 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
211 | |
212 DCHECK_EQ(GetState(), STOPPED); | |
213 | |
214 eos_enqueued_ = false; | |
215 completed_ = false; | |
216 au_queue_.Flush(); | |
217 | |
218 if (media_codec_bridge_) { | |
219 // MediaCodecBridge::Reset() performs MediaCodecBridge.flush() | |
220 MediaCodecStatus flush_status = media_codec_bridge_->Reset(); | |
221 if (flush_status != MEDIA_CODEC_OK) | |
222 media_task_runner_->PostTask(FROM_HERE, error_cb_); | |
223 } | |
224 } | |
225 | |
226 void MediaCodecDecoder::OnDemuxerDataAvailable(const DemuxerData& data) { | |
227 // Media thread | |
228 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
229 | |
230 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
231 << " #AUs:" << data.access_units.size() | |
232 << " #Configs:" << data.demuxer_configs.size(); | |
233 | |
234 if (!data.access_units.empty()) | |
235 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
236 << " AU[0]: " << data.access_units[0]; | |
237 | |
238 au_queue_.PushBack(data); | |
239 | |
240 if (state_ == PREFETCHING) | |
241 PrefetchNextChunk(); | |
242 } | |
243 | |
244 void MediaCodecDecoder::ReleaseDecoderResources() { | |
245 // Media thread | |
246 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
247 | |
248 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
249 | |
250 decoder_thread_.Stop(); // synchronous | |
251 state_ = STOPPED; | |
252 media_codec_bridge_.reset(); | |
253 } | |
254 | |
255 void MediaCodecDecoder::ReleaseMediaCodec() { | |
256 // Media thread | |
257 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
258 | |
259 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
260 | |
261 media_codec_bridge_.reset(); | |
262 } | |
263 | |
264 bool MediaCodecDecoder::IsPrefetchingOrPlaying() const { | |
265 // Media thread | |
266 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
267 | |
268 base::AutoLock lock(state_lock_); | |
269 return state_ == PREFETCHING || state_ == RUNNING; | |
270 } | |
271 | |
272 bool MediaCodecDecoder::IsStopped() const { | |
273 // Media thread | |
274 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
275 | |
276 return GetState() == STOPPED; | |
277 } | |
278 | |
279 bool MediaCodecDecoder::IsCompleted() const { | |
280 // Media thread | |
281 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
282 | |
283 return completed_; | |
284 } | |
285 | |
286 DecoderState MediaCodecDecoder::GetState() const { | |
287 base::AutoLock lock(state_lock_); | |
288 return state_; | |
289 } | |
290 | |
291 void MediaCodecDecoder::SetState(DecoderState state) { | |
292 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << state; | |
293 | |
294 base::AutoLock lock(state_lock_); | |
295 state_ = state; | |
296 } | |
297 | |
298 void MediaCodecDecoder::PrefetchNextChunk() { | |
299 // Media thread | |
300 DCHECK(media_task_runner_->BelongsToCurrentThread()); | |
301 | |
302 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
303 | |
304 if (eos_enqueued_ || | |
305 au_queue_.Length() >= PREFETCH_LIMIT || | |
306 au_queue_.HasEOS()) { | |
307 // We are done prefetching | |
308 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone"; | |
309 media_task_runner_->PostTask( | |
310 FROM_HERE, base::ResetAndReturn(&prefetch_done_cb_)); | |
311 return; | |
312 } | |
313 | |
314 request_data_cb_.Run(); | |
315 } | |
316 | |
317 void MediaCodecDecoder::ProcessNextFrame() { | |
318 // Decoder thread | |
319 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
320 | |
321 DVLOG(1) << class_name() << "::" << __FUNCTION__; | |
322 | |
323 DecoderState state = GetState(); | |
324 | |
325 if (state != RUNNING && state != STOPPING) { | |
326 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": not running"; | |
327 return; | |
328 } | |
329 | |
330 if (state == STOPPING) { | |
331 if (NumDelayedRenderTasks() == 0 && !last_frame_posted_) { | |
332 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
333 << ": STOPPING, posting OnLastFrameRendered"; | |
334 media_task_runner_->PostTask( | |
335 FROM_HERE, | |
336 base::Bind(&MediaCodecDecoder::OnLastFrameRendered, | |
337 weak_this_, false)); | |
338 last_frame_posted_ = true; | |
339 } | |
340 | |
341 // We can stop processing, the |au_queue_| and MediaCodec queues can freeze | |
342 return; | |
343 } | |
344 | |
345 DCHECK(state == RUNNING); | |
346 | |
347 // Keep the number pending video frames low, ideally maintaining | |
348 // the same audio and video duration after stop request | |
349 | |
350 if (NumDelayedRenderTasks() <= 1) { | |
351 bool success= EnqueueInputBuffer(state); | |
352 if (!success) | |
353 return; | |
354 } | |
355 | |
356 bool eos_encountered = false; | |
357 bool success = DepleteOutputBufferQueue(state, &eos_encountered); | |
358 if (!success) | |
359 return; | |
360 | |
361 if (eos_encountered) { | |
362 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
363 << " EOS dequeued, stopping frame processing"; | |
364 return; | |
365 } | |
366 | |
367 // We need a small delay if we want to stop this thread by | |
368 // decoder_thread_.Stop() reliably. | |
369 // The decoder thread message loop processes all pending | |
370 // (but not delayed) tasks before it can quit; without a delay | |
371 // the message loop might be forever processing the pendng tasks. | |
372 decoder_thread_.task_runner()->PostDelayedTask( | |
373 FROM_HERE, | |
374 base::Bind(&MediaCodecDecoder::ProcessNextFrame, | |
375 base::Unretained(this)), | |
376 base::TimeDelta::FromMilliseconds(NEXT_FRAME_DELAY_MS)); | |
377 } | |
378 | |
379 // Returns false if there was MediaCodec error. | |
380 bool MediaCodecDecoder::EnqueueInputBuffer(DecoderState state) { | |
381 // Decoder thread | |
382 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
383 | |
384 //DVLOG(2) << class_name() << "::" << __FUNCTION__; | |
385 | |
386 if (eos_enqueued_) { | |
387 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
388 << ": eos_enqueued, returning"; | |
389 return true; // Nothing to do | |
390 } | |
391 | |
392 // Get the next frame from the queue and the queue info | |
393 | |
394 AccessUnitQueue::Info au_info; | |
395 au_queue_.GetInfo(&au_info); | |
396 | |
397 // Request the data from Demuxer | |
398 if (au_info.length <= PLAYBACK_LOW_LIMIT && !au_info.has_eos) | |
399 media_task_runner_->PostTask(FROM_HERE, request_data_cb_); | |
400 | |
401 // Get the next frame from the queue | |
402 | |
403 if (!au_info.front_unit) { | |
404 // Report starvation and return, Start() will be called again later. | |
405 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
406 << ": starvation detected state:" << state; | |
407 if (state != STOPPING) | |
408 media_task_runner_->PostTask(FROM_HERE, starvation_cb_); | |
409 return true; | |
410 } | |
411 | |
412 if (au_info.configs) { | |
413 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
414 << ": received new configs, not implemented"; | |
415 // post an error for now? | |
416 media_task_runner_->PostTask(FROM_HERE, error_cb_); | |
417 return true; | |
418 } | |
419 | |
420 // Dequeue input buffer | |
421 | |
422 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(20); | |
423 int index = -1; | |
424 MediaCodecStatus status = | |
425 media_codec_bridge_->DequeueInputBuffer(timeout, &index); | |
426 | |
427 DVLOG(2) << class_name() << ":: DequeueInputBuffer index:" << index; | |
428 | |
429 switch (status) { | |
430 case MEDIA_CODEC_ERROR: | |
431 DVLOG(0) << class_name() << "::" << __FUNCTION__ | |
432 << ": MEDIA_CODEC_ERROR DequeueInputBuffer failed"; | |
433 media_task_runner_->PostTask(FROM_HERE, error_cb_); | |
434 return false; | |
435 break; | |
436 | |
437 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: | |
438 //DVLOG(2) << class_name() << "::" << __FUNCTION__ | |
439 // << " DequeueInputBuffer AGAIN"; | |
440 return true; | |
441 break; | |
442 | |
443 default: | |
444 break; | |
445 } | |
446 | |
447 // We got the buffer | |
448 DCHECK_EQ(status, MEDIA_CODEC_OK); | |
449 DCHECK_GE(index, 0); | |
450 | |
451 const AccessUnit* unit = au_info.front_unit; | |
452 | |
453 if (unit->is_end_of_stream || unit->data.empty()) { | |
454 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS"; | |
455 media_codec_bridge_->QueueEOS(index); | |
456 eos_enqueued_ = true; | |
457 return true; | |
458 } | |
459 | |
460 status = media_codec_bridge_->QueueInputBuffer( | |
461 index, &unit->data[0], unit->data.size(), unit->timestamp); | |
462 | |
463 if (status == MEDIA_CODEC_ERROR) { | |
464 DVLOG(1) << class_name() << "::" << __FUNCTION__ | |
465 << ": MEDIA_CODEC_ERROR: QueueInputBuffer failed"; | |
466 media_task_runner_->PostTask(FROM_HERE, error_cb_); | |
467 return false; | |
468 } | |
469 | |
470 // Have successfully queued input buffer | |
471 au_queue_.PopFront(); | |
472 return true; | |
473 } | |
474 | |
475 // Returns false if there was MediaCodec error. | |
476 bool MediaCodecDecoder::DepleteOutputBufferQueue(DecoderState state, | |
477 bool* eos_encountered) { | |
478 // Decoder thread | |
479 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
480 | |
481 //DVLOG(2) << class_name() << "::" << __FUNCTION__; | |
482 | |
483 int buffer_index = 0; | |
484 size_t offset = 0; | |
485 size_t size = 0; | |
486 base::TimeDelta pts; | |
487 MediaCodecStatus status; | |
488 | |
489 base::TimeDelta timeout[2]; | |
490 timeout[0] = base::TimeDelta::FromMilliseconds(20); | |
491 timeout[1] = base::TimeDelta::FromMilliseconds(0); | |
492 | |
493 int timeout_index = 0; | |
494 | |
495 do { | |
496 status = media_codec_bridge_->DequeueOutputBuffer( | |
497 timeout[timeout_index], | |
498 &buffer_index, | |
499 &offset, | |
500 &size, | |
501 &pts, | |
502 eos_encountered, | |
503 nullptr); | |
504 | |
505 timeout_index = 1; | |
506 | |
507 //DVLOG(2) << class_name() | |
508 // << ":: DequeueOutputBuffer index:" << buffer_index; | |
509 | |
510 switch (status) { | |
511 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: | |
512 // TODO: | |
513 // Audio: set new rate. If it changed, | |
514 // the MediaSourcePlayer code calls ResetTimestampHelper(). | |
515 // Video: report new frame size to manager() | |
516 DVLOG(2) << class_name() << "::" << __FUNCTION__ | |
517 << " MEDIA_CODEC_OUTPUT_FORMAT_CHANGED"; | |
518 break; | |
519 | |
520 case MEDIA_CODEC_OK: | |
521 // We got the decoded frame | |
522 Render(buffer_index, size, true, pts, *eos_encountered); | |
523 break; | |
524 | |
525 case MEDIA_CODEC_ERROR: | |
526 DVLOG(0) << class_name() << "::" << __FUNCTION__ | |
527 << ": MEDIA_CODEC_ERROR from DequeueOutputBuffer"; | |
528 media_task_runner_->PostTask(FROM_HERE, error_cb_); | |
529 break; | |
530 | |
531 default: | |
532 break; | |
533 } | |
534 | |
535 } while (status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER && | |
536 status != MEDIA_CODEC_ERROR && | |
537 !*eos_encountered); | |
538 | |
539 return status != MEDIA_CODEC_ERROR; | |
540 } | |
541 | |
542 void MediaCodecDecoder::ProcessLastFrame(bool eos_encountered, | |
543 bool has_delayed_tasks) { | |
544 // Decoder thread | |
545 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); | |
546 | |
547 bool last_frame_when_stopping = | |
548 GetState() == STOPPING && !has_delayed_tasks; | |
549 | |
550 if (last_frame_when_stopping || eos_encountered) { | |
551 media_task_runner_->PostTask( | |
552 FROM_HERE, | |
553 base::Bind(&MediaCodecDecoder::OnLastFrameRendered, | |
554 weak_this_, eos_encountered)); | |
555 last_frame_posted_ = true; | |
556 } | |
557 } | |
558 | |
559 } // namespace media | |
OLD | NEW |