blob: 5d98d985572c8909e42431db97ed08057a01d329 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
Chong Zhang7137ec72014-11-12 16:41:05 -08002 * Copyright 2014 The Android Open Source Project
Andreas Huberf9334412010-12-15 15:17:42 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerDecoder"
19#include <utils/Log.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080020#include <inttypes.h>
Andreas Huberf9334412010-12-15 15:17:42 -080021
Chong Zhang7137ec72014-11-12 16:41:05 -080022#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080023#include "NuPlayerDecoder.h"
Wei Jiac6cfd702014-11-11 16:33:20 -080024#include "NuPlayerRenderer.h"
25#include "NuPlayerSource.h"
26
Lajos Molnar1cd13982014-01-17 15:12:51 -080027#include <media/ICrypto.h>
Andreas Huberf9334412010-12-15 15:17:42 -080028#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080030#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070031#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080032#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080033#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080034#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080035
Chong Zhang7137ec72014-11-12 16:41:05 -080036#include "avc_utils.h"
37#include "ATSParser.h"
38
Andreas Huberf9334412010-12-15 15:17:42 -080039namespace android {
40
41NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080042 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080043 const sp<Source> &source,
44 const sp<Renderer> &renderer,
Chong Zhang7137ec72014-11-12 16:41:05 -080045 const sp<NativeWindowWrapper> &nativeWindow,
46 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080047 : DecoderBase(notify),
Lajos Molnar1cd13982014-01-17 15:12:51 -080048 mNativeWindow(nativeWindow),
Wei Jiac6cfd702014-11-11 16:33:20 -080049 mSource(source),
50 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080051 mCCDecoder(ccDecoder),
Wei Jiac6cfd702014-11-11 16:33:20 -080052 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080053 mNumFramesTotal(0ll),
54 mNumFramesDropped(0ll),
55 mIsAudio(true),
56 mIsVideoAVC(false),
57 mIsSecure(false),
58 mFormatChangePending(false),
Wei Jia704e7262014-06-04 16:21:56 -070059 mPaused(true),
Chong Zhangf8d71772014-11-26 15:08:34 -080060 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080061 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080062 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070063 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080064 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080065}
66
67NuPlayer::Decoder::~Decoder() {
Wei Jia4923cee2014-09-24 14:25:19 -070068 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080069}
70
Chong Zhang7137ec72014-11-12 16:41:05 -080071void NuPlayer::Decoder::getStats(
72 int64_t *numFramesTotal,
73 int64_t *numFramesDropped) const {
74 *numFramesTotal = mNumFramesTotal;
75 *numFramesDropped = mNumFramesDropped;
Lajos Molnar09524832014-07-17 14:29:51 -070076}
77
Chong Zhang7137ec72014-11-12 16:41:05 -080078void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
79 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
80
81 switch (msg->what()) {
82 case kWhatCodecNotify:
83 {
84 if (!isStaleReply(msg)) {
85 int32_t numInput, numOutput;
86
87 if (!msg->findInt32("input-buffers", &numInput)) {
88 numInput = INT32_MAX;
89 }
90
91 if (!msg->findInt32("output-buffers", &numOutput)) {
92 numOutput = INT32_MAX;
93 }
94
95 if (!mPaused) {
96 while (numInput-- > 0 && handleAnInputBuffer()) {}
97 }
98
99 while (numOutput-- > 0 && handleAnOutputBuffer()) {}
100 }
101
102 requestCodecNotification();
Lajos Molnar87603c02014-08-20 19:25:30 -0700103 break;
104 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800105
106 case kWhatRenderBuffer:
107 {
108 if (!isStaleReply(msg)) {
109 onRenderBuffer(msg);
110 }
111 break;
112 }
113
114 default:
115 DecoderBase::onMessageReceived(msg);
116 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700117 }
118}
119
Lajos Molnar1cd13982014-01-17 15:12:51 -0800120void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800121 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800122
Chong Zhang7137ec72014-11-12 16:41:05 -0800123 mFormatChangePending = false;
124
Lajos Molnar1cd13982014-01-17 15:12:51 -0800125 ++mBufferGeneration;
126
Andreas Huber84066782011-08-16 09:34:26 -0700127 AString mime;
128 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800129
Chong Zhang7137ec72014-11-12 16:41:05 -0800130 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
131 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
132
Lajos Molnar1cd13982014-01-17 15:12:51 -0800133 sp<Surface> surface = NULL;
134 if (mNativeWindow != NULL) {
135 surface = mNativeWindow->getSurfaceTextureClient();
Andreas Huber84066782011-08-16 09:34:26 -0700136 }
Andreas Huberf9334412010-12-15 15:17:42 -0800137
Lajos Molnar1cd13982014-01-17 15:12:51 -0800138 mComponentName = mime;
139 mComponentName.append(" decoder");
140 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
141
142 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700143 int32_t secure = 0;
144 if (format->findInt32("secure", &secure) && secure != 0) {
145 if (mCodec != NULL) {
146 mCodec->getName(&mComponentName);
147 mComponentName.append(".secure");
148 mCodec->release();
149 ALOGI("[%s] creating", mComponentName.c_str());
150 mCodec = MediaCodec::CreateByComponentName(
151 mCodecLooper, mComponentName.c_str());
152 }
153 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800154 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700155 ALOGE("Failed to create %s%s decoder",
156 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800157 handleError(UNKNOWN_ERROR);
158 return;
159 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800160 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800161
162 mCodec->getName(&mComponentName);
163
Lajos Molnar14986f62014-09-15 11:04:44 -0700164 status_t err;
Glenn Kasten11731182011-02-08 17:26:17 -0800165 if (mNativeWindow != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800166 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700167 err = native_window_api_disconnect(
168 surface.get(), NATIVE_WINDOW_API_MEDIA);
169 // We treat this as a warning, as this is a preparatory step.
170 // Codec will try to connect to the surface, which is where
171 // any error signaling will occur.
172 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800173 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700174 err = mCodec->configure(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800175 format, surface, NULL /* crypto */, 0 /* flags */);
176 if (err != OK) {
177 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700178 mCodec->release();
179 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800180 handleError(err);
181 return;
182 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700183 rememberCodecSpecificData(format);
184
Lajos Molnar1cd13982014-01-17 15:12:51 -0800185 // the following should work in configured state
186 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
187 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
188
189 err = mCodec->start();
190 if (err != OK) {
191 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700192 mCodec->release();
193 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800194 handleError(err);
195 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800196 }
197
Lajos Molnar1cd13982014-01-17 15:12:51 -0800198 // the following should work after start
199 CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
Lajos Molnar09524832014-07-17 14:29:51 -0700200 releaseAndResetMediaBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800201 CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
202 ALOGV("[%s] got %zu input and %zu output buffers",
203 mComponentName.c_str(),
204 mInputBuffers.size(),
205 mOutputBuffers.size());
Andreas Huber078cfcf2011-09-15 12:25:04 -0700206
Wei Jiac6cfd702014-11-11 16:33:20 -0800207 if (mRenderer != NULL) {
208 requestCodecNotification();
209 }
Wei Jia704e7262014-06-04 16:21:56 -0700210 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800211 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800212}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700213
Chong Zhang7137ec72014-11-12 16:41:05 -0800214void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
215 bool hadNoRenderer = (mRenderer == NULL);
216 mRenderer = renderer;
217 if (hadNoRenderer && mRenderer != NULL) {
218 requestCodecNotification();
219 }
220}
221
222void NuPlayer::Decoder::onGetInputBuffers(
223 Vector<sp<ABuffer> > *dstBuffers) {
224 dstBuffers->clear();
225 for (size_t i = 0; i < mInputBuffers.size(); i++) {
226 dstBuffers->push(mInputBuffers[i]);
227 }
228}
229
Chong Zhangf8d71772014-11-26 15:08:34 -0800230void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800231 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800232
233 if (notifyComplete) {
234 mResumePending = true;
235 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800236}
237
238void NuPlayer::Decoder::onFlush(bool notifyComplete) {
239 if (mCCDecoder != NULL) {
240 mCCDecoder->flush();
241 }
242
243 if (mRenderer != NULL) {
244 mRenderer->flush(mIsAudio, notifyComplete);
245 mRenderer->signalTimeDiscontinuity();
246 }
247
248 status_t err = OK;
249 if (mCodec != NULL) {
250 err = mCodec->flush();
251 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
252 ++mBufferGeneration;
253 }
254
255 if (err != OK) {
256 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
257 handleError(err);
258 // finish with posting kWhatFlushCompleted.
259 // we attempt to release the buffers even if flush fails.
260 }
261 releaseAndResetMediaBuffers();
262
263 if (notifyComplete) {
264 sp<AMessage> notify = mNotify->dup();
265 notify->setInt32("what", kWhatFlushCompleted);
266 notify->post();
267 mPaused = true;
268 }
269}
270
271void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
272 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800273
274 // if there is a pending resume request, notify complete now
275 notifyResumeCompleteIfNecessary();
276
Chong Zhang7137ec72014-11-12 16:41:05 -0800277 if (mCodec != NULL) {
278 err = mCodec->release();
279 mCodec = NULL;
280 ++mBufferGeneration;
281
282 if (mNativeWindow != NULL) {
283 // reconnect to surface as MediaCodec disconnected from it
284 status_t error =
285 native_window_api_connect(
286 mNativeWindow->getNativeWindow().get(),
287 NATIVE_WINDOW_API_MEDIA);
288 ALOGW_IF(error != NO_ERROR,
289 "[%s] failed to connect to native window, error=%d",
290 mComponentName.c_str(), error);
291 }
292 mComponentName = "decoder";
293 }
294
295 releaseAndResetMediaBuffers();
296
297 if (err != OK) {
298 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
299 handleError(err);
300 // finish with posting kWhatShutdownCompleted.
301 }
302
303 if (notifyComplete) {
304 sp<AMessage> notify = mNotify->dup();
305 notify->setInt32("what", kWhatShutdownCompleted);
306 notify->post();
307 mPaused = true;
308 }
309}
310
311void NuPlayer::Decoder::doRequestBuffers() {
312 if (mFormatChangePending) {
313 return;
314 }
315 status_t err = OK;
316 while (!mDequeuedInputBuffers.empty()) {
317 size_t bufferIx = *mDequeuedInputBuffers.begin();
318 sp<AMessage> msg = new AMessage();
319 msg->setSize("buffer-ix", bufferIx);
320 err = fetchInputData(msg);
321 if (err != OK) {
322 break;
323 }
324 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
325
326 if (!mPendingInputMessages.empty()
327 || !onInputBufferFetched(msg)) {
328 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700329 }
330 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800331
332 if (err == -EWOULDBLOCK
333 && mSource->feedMoreTSData() == OK) {
334 scheduleRequestBuffers();
Wei Jia81e50d02014-07-24 10:28:47 -0700335 }
Lajos Molnar09524832014-07-17 14:29:51 -0700336}
337
Lajos Molnar1cd13982014-01-17 15:12:51 -0800338bool NuPlayer::Decoder::handleAnInputBuffer() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800339 if (mFormatChangePending) {
340 return false;
341 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800342 size_t bufferIx = -1;
343 status_t res = mCodec->dequeueInputBuffer(&bufferIx);
344 ALOGV("[%s] dequeued input: %d",
345 mComponentName.c_str(), res == OK ? (int)bufferIx : res);
346 if (res != OK) {
347 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700348 ALOGE("Failed to dequeue input buffer for %s (err=%d)",
349 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800350 handleError(res);
351 }
352 return false;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700353 }
354
Lajos Molnar1cd13982014-01-17 15:12:51 -0800355 CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800356
Lajos Molnar09524832014-07-17 14:29:51 -0700357 if (mMediaBuffers[bufferIx] != NULL) {
358 mMediaBuffers[bufferIx]->release();
359 mMediaBuffers.editItemAt(bufferIx) = NULL;
360 }
361 mInputBufferIsDequeued.editItemAt(bufferIx) = true;
362
Lajos Molnar87603c02014-08-20 19:25:30 -0700363 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800364 sp<AMessage> msg = new AMessage();
365 msg->setSize("buffer-ix", bufferIx);
366
Lajos Molnar87603c02014-08-20 19:25:30 -0700367 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
368 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800369 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700370 mCSDsToSubmit.removeAt(0);
Chong Zhang7137ec72014-11-12 16:41:05 -0800371 CHECK(onInputBufferFetched(msg));
Wei Jia2245fc62014-10-02 15:12:25 -0700372 return true;
373 }
374
375 while (!mPendingInputMessages.empty()) {
376 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800377 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700378 break;
379 }
380 mPendingInputMessages.erase(mPendingInputMessages.begin());
381 }
382
383 if (!mInputBufferIsDequeued.editItemAt(bufferIx)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700384 return true;
385 }
386
Chong Zhang7137ec72014-11-12 16:41:05 -0800387 mDequeuedInputBuffers.push_back(bufferIx);
388
389 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800390 return true;
391}
392
Chong Zhang7137ec72014-11-12 16:41:05 -0800393bool NuPlayer::Decoder::handleAnOutputBuffer() {
394 if (mFormatChangePending) {
395 return false;
396 }
397 size_t bufferIx = -1;
398 size_t offset;
399 size_t size;
400 int64_t timeUs;
401 uint32_t flags;
402 status_t res = mCodec->dequeueOutputBuffer(
403 &bufferIx, &offset, &size, &timeUs, &flags);
404
405 if (res != OK) {
406 ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
407 } else {
408 ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
409 mComponentName.c_str(), (int)bufferIx, timeUs, flags);
410 }
411
412 if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
413 res = mCodec->getOutputBuffers(&mOutputBuffers);
414 if (res != OK) {
415 ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
416 mComponentName.c_str(), res);
417 handleError(res);
418 return false;
419 }
420 // NuPlayer ignores this
421 return true;
422 } else if (res == INFO_FORMAT_CHANGED) {
423 sp<AMessage> format = new AMessage();
424 res = mCodec->getOutputFormat(&format);
425 if (res != OK) {
426 ALOGE("Failed to get output format for %s after INFO event (err=%d)",
427 mComponentName.c_str(), res);
428 handleError(res);
429 return false;
430 }
431
432 if (!mIsAudio) {
433 sp<AMessage> notify = mNotify->dup();
434 notify->setInt32("what", kWhatVideoSizeChanged);
435 notify->setMessage("format", format);
436 notify->post();
437 } else if (mRenderer != NULL) {
438 uint32_t flags;
439 int64_t durationUs;
440 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
441 if (!hasVideo &&
442 mSource->getDuration(&durationUs) == OK &&
443 durationUs
444 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
445 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
446 } else {
447 flags = AUDIO_OUTPUT_FLAG_NONE;
448 }
449
Andy Hung202bce12014-12-03 11:47:36 -0800450 res = mRenderer->openAudioSink(
451 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaded */);
452 if (res != OK) {
453 ALOGE("Failed to open AudioSink on format change for %s (err=%d)",
454 mComponentName.c_str(), res);
455 handleError(res);
456 return false;
457 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800458 }
459 return true;
460 } else if (res == INFO_DISCONTINUITY) {
461 // nothing to do
462 return true;
463 } else if (res != OK) {
464 if (res != -EAGAIN) {
465 ALOGE("Failed to dequeue output buffer for %s (err=%d)",
466 mComponentName.c_str(), res);
467 handleError(res);
468 }
469 return false;
470 }
471
472 CHECK_LT(bufferIx, mOutputBuffers.size());
473 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
474 buffer->setRange(offset, size);
475 buffer->meta()->clear();
476 buffer->meta()->setInt64("timeUs", timeUs);
477 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
478 buffer->meta()->setInt32("eos", true);
Wei Jia474d7c72014-12-04 15:12:13 -0800479 notifyResumeCompleteIfNecessary();
Chong Zhang7137ec72014-11-12 16:41:05 -0800480 }
481 // we do not expect CODECCONFIG or SYNCFRAME for decoder
482
483 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id());
484 reply->setSize("buffer-ix", bufferIx);
485 reply->setInt32("generation", mBufferGeneration);
486
487 if (mSkipRenderingUntilMediaTimeUs >= 0) {
488 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
489 ALOGV("[%s] dropping buffer at time %lld as requested.",
490 mComponentName.c_str(), (long long)timeUs);
491
492 reply->post();
493 return true;
494 }
495
496 mSkipRenderingUntilMediaTimeUs = -1;
497 }
498
Chong Zhangf8d71772014-11-26 15:08:34 -0800499 // wait until 1st frame comes out to signal resume complete
500 notifyResumeCompleteIfNecessary();
501
Chong Zhang7137ec72014-11-12 16:41:05 -0800502 if (mRenderer != NULL) {
503 // send the buffer to renderer.
504 mRenderer->queueBuffer(mIsAudio, buffer, reply);
505 if (flags & MediaCodec::BUFFER_FLAG_EOS) {
506 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
507 }
508 }
509
510 return true;
511}
512
513void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
514 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
515 if (mMediaBuffers[i] != NULL) {
516 mMediaBuffers[i]->release();
517 mMediaBuffers.editItemAt(i) = NULL;
518 }
519 }
520 mMediaBuffers.resize(mInputBuffers.size());
521 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
522 mMediaBuffers.editItemAt(i) = NULL;
523 }
524 mInputBufferIsDequeued.clear();
525 mInputBufferIsDequeued.resize(mInputBuffers.size());
526 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
527 mInputBufferIsDequeued.editItemAt(i) = false;
528 }
529
530 mPendingInputMessages.clear();
531 mDequeuedInputBuffers.clear();
532 mSkipRenderingUntilMediaTimeUs = -1;
533}
534
535void NuPlayer::Decoder::requestCodecNotification() {
536 if (mFormatChangePending) {
537 return;
538 }
539 if (mCodec != NULL) {
540 sp<AMessage> reply = new AMessage(kWhatCodecNotify, id());
541 reply->setInt32("generation", mBufferGeneration);
542 mCodec->requestActivityNotification(reply);
543 }
544}
545
546bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
547 int32_t generation;
548 CHECK(msg->findInt32("generation", &generation));
549 return generation != mBufferGeneration;
550}
551
552status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
553 sp<ABuffer> accessUnit;
554 bool dropAccessUnit;
555 do {
556 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
557
558 if (err == -EWOULDBLOCK) {
559 return err;
560 } else if (err != OK) {
561 if (err == INFO_DISCONTINUITY) {
562 int32_t type;
563 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
564
565 bool formatChange =
566 (mIsAudio &&
567 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
568 || (!mIsAudio &&
569 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
570
571 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
572
573 ALOGI("%s discontinuity (format=%d, time=%d)",
574 mIsAudio ? "audio" : "video", formatChange, timeChange);
575
576 bool seamlessFormatChange = false;
577 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
578 if (formatChange) {
579 seamlessFormatChange =
580 supportsSeamlessFormatChange(newFormat);
581 // treat seamless format change separately
582 formatChange = !seamlessFormatChange;
583 }
584
585 if (formatChange || timeChange) {
586 sp<AMessage> msg = mNotify->dup();
587 msg->setInt32("what", kWhatInputDiscontinuity);
588 msg->setInt32("formatChange", formatChange);
589 msg->post();
590 }
591
592 if (formatChange /* not seamless */) {
593 // must change decoder
594 // return EOS and wait to be killed
595 mFormatChangePending = true;
596 return ERROR_END_OF_STREAM;
597 } else if (timeChange) {
598 // need to flush
599 // TODO: Ideally we shouldn't need a flush upon time
600 // discontinuity, flushing will cause loss of frames.
601 // We probably should queue a time change marker to the
602 // output queue, and handles it in renderer instead.
603 rememberCodecSpecificData(newFormat);
604 onFlush(false /* notifyComplete */);
605 err = OK;
606 } else if (seamlessFormatChange) {
607 // reuse existing decoder and don't flush
608 rememberCodecSpecificData(newFormat);
609 err = OK;
610 } else {
611 // This stream is unaffected by the discontinuity
612 return -EWOULDBLOCK;
613 }
614 }
615
616 reply->setInt32("err", err);
617 return OK;
618 }
619
620 if (!mIsAudio) {
621 ++mNumFramesTotal;
622 }
623
624 dropAccessUnit = false;
625 if (!mIsAudio
626 && !mIsSecure
627 && mRenderer->getVideoLateByUs() > 100000ll
628 && mIsVideoAVC
629 && !IsAVCReferenceFrame(accessUnit)) {
630 dropAccessUnit = true;
631 ++mNumFramesDropped;
632 }
633 } while (dropAccessUnit);
634
635 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
636#if 0
637 int64_t mediaTimeUs;
638 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
639 ALOGV("feeding %s input buffer at media time %.2f secs",
640 mIsAudio ? "audio" : "video",
641 mediaTimeUs / 1E6);
642#endif
643
644 if (mCCDecoder != NULL) {
645 mCCDecoder->decode(accessUnit);
646 }
647
648 reply->setBuffer("buffer", accessUnit);
649
650 return OK;
651}
652
653bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800654 size_t bufferIx;
655 CHECK(msg->findSize("buffer-ix", &bufferIx));
656 CHECK_LT(bufferIx, mInputBuffers.size());
657 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
658
659 sp<ABuffer> buffer;
660 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700661
662 // handle widevine classic source - that fills an arbitrary input buffer
663 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700664 if (hasBuffer) {
665 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
666 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700667 // likely filled another buffer than we requested: adjust buffer index
668 size_t ix;
669 for (ix = 0; ix < mInputBuffers.size(); ix++) {
670 const sp<ABuffer> &buf = mInputBuffers[ix];
671 if (buf->data() == mediaBuffer->data()) {
672 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700673 if (!mInputBufferIsDequeued[ix]) {
674 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
675 mComponentName.c_str(), ix, bufferIx);
676 mediaBuffer->release();
677 return false;
678 }
Lajos Molnar09524832014-07-17 14:29:51 -0700679
680 // TRICKY: need buffer for the metadata, so instead, set
681 // codecBuffer to the same (though incorrect) buffer to
682 // avoid a memcpy into the codecBuffer
683 codecBuffer = buffer;
684 codecBuffer->setRange(
685 mediaBuffer->range_offset(),
686 mediaBuffer->range_length());
687 bufferIx = ix;
688 break;
689 }
690 }
691 CHECK(ix < mInputBuffers.size());
692 }
693 }
694
Lajos Molnar1cd13982014-01-17 15:12:51 -0800695 if (buffer == NULL /* includes !hasBuffer */) {
696 int32_t streamErr = ERROR_END_OF_STREAM;
697 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
698
699 if (streamErr == OK) {
700 /* buffers are returned to hold on to */
Wei Jia2245fc62014-10-02 15:12:25 -0700701 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800702 }
703
704 // attempt to queue EOS
705 status_t err = mCodec->queueInputBuffer(
706 bufferIx,
707 0,
708 0,
709 0,
710 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700711 if (err == OK) {
712 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
713 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800714 streamErr = err;
715 // err will not be ERROR_END_OF_STREAM
716 }
717
718 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700719 ALOGE("Stream error for %s (err=%d), EOS %s queued",
720 mComponentName.c_str(),
721 streamErr,
722 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800723 handleError(streamErr);
724 }
725 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800726 sp<AMessage> extra;
727 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
728 int64_t resumeAtMediaTimeUs;
729 if (extra->findInt64(
730 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
731 ALOGI("[%s] suppressing rendering until %lld us",
732 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
733 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
734 }
735 }
736
Lajos Molnar1cd13982014-01-17 15:12:51 -0800737 int64_t timeUs = 0;
738 uint32_t flags = 0;
739 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
740
Lajos Molnar87603c02014-08-20 19:25:30 -0700741 int32_t eos, csd;
742 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800743 if (buffer->meta()->findInt32("eos", &eos) && eos) {
744 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700745 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
746 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800747 }
748
749 // copy into codec buffer
750 if (buffer != codecBuffer) {
751 CHECK_LE(buffer->size(), codecBuffer->capacity());
752 codecBuffer->setRange(0, buffer->size());
753 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
754 }
755
756 status_t err = mCodec->queueInputBuffer(
757 bufferIx,
758 codecBuffer->offset(),
759 codecBuffer->size(),
760 timeUs,
761 flags);
762 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700763 if (mediaBuffer != NULL) {
764 mediaBuffer->release();
765 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800766 ALOGE("Failed to queue input buffer for %s (err=%d)",
767 mComponentName.c_str(), err);
768 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700769 } else {
770 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
771 if (mediaBuffer != NULL) {
772 CHECK(mMediaBuffers[bufferIx] == NULL);
773 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
774 }
Lajos Molnar09524832014-07-17 14:29:51 -0700775 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800776 }
Wei Jia2245fc62014-10-02 15:12:25 -0700777 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800778}
779
Lajos Molnar1cd13982014-01-17 15:12:51 -0800780void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
781 status_t err;
782 int32_t render;
783 size_t bufferIx;
784 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800785
Chong Zhang7137ec72014-11-12 16:41:05 -0800786 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800787 int64_t timeUs;
788 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
789 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800790
791 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
792 mCCDecoder->display(timeUs);
793 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800794 }
795
Lajos Molnar1cd13982014-01-17 15:12:51 -0800796 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700797 int64_t timestampNs;
798 CHECK(msg->findInt64("timestampNs", &timestampNs));
799 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800800 } else {
801 err = mCodec->releaseOutputBuffer(bufferIx);
802 }
803 if (err != OK) {
804 ALOGE("failed to release output buffer for %s (err=%d)",
805 mComponentName.c_str(), err);
806 handleError(err);
807 }
808}
809
Chong Zhang7137ec72014-11-12 16:41:05 -0800810bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
811 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800812 if (targetFormat == NULL) {
813 return true;
814 }
815
816 AString mime;
817 if (!targetFormat->findString("mime", &mime)) {
818 return false;
819 }
820
821 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
822 // field-by-field comparison
823 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
824 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
825 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100826 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800827 !targetFormat->findInt32(keys[i], &newVal) ||
828 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800829 return false;
830 }
831 }
832
833 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100834 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800835 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800836 if (oldBuf->size() != newBuf->size()) {
837 return false;
838 }
839 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
840 }
841 }
842 return false;
843}
844
845bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +0100846 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800847 return false;
848 }
849
850 if (targetFormat == NULL) {
851 return true;
852 }
853
854 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100855 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800856 || !targetFormat->findString("mime", &newMime)
857 || !(oldMime == newMime)) {
858 return false;
859 }
860
861 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
862 bool seamless;
863 if (audio) {
864 seamless = supportsSeamlessAudioFormatChange(targetFormat);
865 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800866 int32_t isAdaptive;
867 seamless = (mCodec != NULL &&
868 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
869 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800870 }
871
872 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
873 return seamless;
874}
875
Chong Zhang7137ec72014-11-12 16:41:05 -0800876void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
877 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -0700878 return;
879 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800880 mCSDsForCurrentFormat.clear();
881 for (int32_t i = 0; ; ++i) {
882 AString tag = "csd-";
883 tag.append(i);
884 sp<ABuffer> buffer;
885 if (!format->findBuffer(tag.c_str(), &buffer)) {
886 break;
887 }
888 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700889 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700890}
891
Chong Zhangf8d71772014-11-26 15:08:34 -0800892void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
893 if (mResumePending) {
894 mResumePending = false;
895
896 sp<AMessage> notify = mNotify->dup();
897 notify->setInt32("what", kWhatResumeCompleted);
898 notify->post();
899 }
900}
901
Andreas Huberf9334412010-12-15 15:17:42 -0800902} // namespace android
903