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

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

Powered by Google App Engine
This is Rietveld 408576698