Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: media/base/android/media_codec_decoder.cc

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

Powered by Google App Engine
This is Rietveld 408576698