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

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

Powered by Google App Engine
This is Rietveld 408576698