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

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

Powered by Google App Engine
This is Rietveld 408576698