blob: 04ac699dcb808891346a04ad39a765a2726b1392 [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),
Chong Zhang66704af2015-03-03 19:32:35 -080059 mTimeChangePending(false),
Wei Jia704e7262014-06-04 16:21:56 -070060 mPaused(true),
Chong Zhangf8d71772014-11-26 15:08:34 -080061 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080062 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080063 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070064 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080065 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080066}
67
68NuPlayer::Decoder::~Decoder() {
Wei Jia4923cee2014-09-24 14:25:19 -070069 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080070}
71
Chong Zhang7137ec72014-11-12 16:41:05 -080072void NuPlayer::Decoder::getStats(
73 int64_t *numFramesTotal,
74 int64_t *numFramesDropped) const {
75 *numFramesTotal = mNumFramesTotal;
76 *numFramesDropped = mNumFramesDropped;
Lajos Molnar09524832014-07-17 14:29:51 -070077}
78
Chong Zhang7137ec72014-11-12 16:41:05 -080079void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
80 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
81
82 switch (msg->what()) {
83 case kWhatCodecNotify:
84 {
85 if (!isStaleReply(msg)) {
86 int32_t numInput, numOutput;
87
88 if (!msg->findInt32("input-buffers", &numInput)) {
89 numInput = INT32_MAX;
90 }
91
92 if (!msg->findInt32("output-buffers", &numOutput)) {
93 numOutput = INT32_MAX;
94 }
95
96 if (!mPaused) {
97 while (numInput-- > 0 && handleAnInputBuffer()) {}
98 }
99
100 while (numOutput-- > 0 && handleAnOutputBuffer()) {}
101 }
102
103 requestCodecNotification();
Lajos Molnar87603c02014-08-20 19:25:30 -0700104 break;
105 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800106
107 case kWhatRenderBuffer:
108 {
109 if (!isStaleReply(msg)) {
110 onRenderBuffer(msg);
111 }
112 break;
113 }
114
115 default:
116 DecoderBase::onMessageReceived(msg);
117 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700118 }
119}
120
Lajos Molnar1cd13982014-01-17 15:12:51 -0800121void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800122 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800123
Chong Zhang7137ec72014-11-12 16:41:05 -0800124 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800125 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800126
Lajos Molnar1cd13982014-01-17 15:12:51 -0800127 ++mBufferGeneration;
128
Andreas Huber84066782011-08-16 09:34:26 -0700129 AString mime;
130 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800131
Chong Zhang7137ec72014-11-12 16:41:05 -0800132 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
133 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
134
Lajos Molnar1cd13982014-01-17 15:12:51 -0800135 sp<Surface> surface = NULL;
136 if (mNativeWindow != NULL) {
137 surface = mNativeWindow->getSurfaceTextureClient();
Andreas Huber84066782011-08-16 09:34:26 -0700138 }
Andreas Huberf9334412010-12-15 15:17:42 -0800139
Lajos Molnar1cd13982014-01-17 15:12:51 -0800140 mComponentName = mime;
141 mComponentName.append(" decoder");
142 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
143
144 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700145 int32_t secure = 0;
146 if (format->findInt32("secure", &secure) && secure != 0) {
147 if (mCodec != NULL) {
148 mCodec->getName(&mComponentName);
149 mComponentName.append(".secure");
150 mCodec->release();
151 ALOGI("[%s] creating", mComponentName.c_str());
152 mCodec = MediaCodec::CreateByComponentName(
153 mCodecLooper, mComponentName.c_str());
154 }
155 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800156 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700157 ALOGE("Failed to create %s%s decoder",
158 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800159 handleError(UNKNOWN_ERROR);
160 return;
161 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800162 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800163
164 mCodec->getName(&mComponentName);
165
Lajos Molnar14986f62014-09-15 11:04:44 -0700166 status_t err;
Glenn Kasten11731182011-02-08 17:26:17 -0800167 if (mNativeWindow != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800168 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700169 err = native_window_api_disconnect(
170 surface.get(), NATIVE_WINDOW_API_MEDIA);
171 // We treat this as a warning, as this is a preparatory step.
172 // Codec will try to connect to the surface, which is where
173 // any error signaling will occur.
174 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800175 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700176 err = mCodec->configure(
Lajos Molnar1cd13982014-01-17 15:12:51 -0800177 format, surface, NULL /* crypto */, 0 /* flags */);
178 if (err != OK) {
179 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700180 mCodec->release();
181 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800182 handleError(err);
183 return;
184 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700185 rememberCodecSpecificData(format);
186
Lajos Molnar1cd13982014-01-17 15:12:51 -0800187 // the following should work in configured state
188 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
189 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
190
191 err = mCodec->start();
192 if (err != OK) {
193 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700194 mCodec->release();
195 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800196 handleError(err);
197 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800198 }
199
Lajos Molnar1cd13982014-01-17 15:12:51 -0800200 // the following should work after start
201 CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
Lajos Molnar09524832014-07-17 14:29:51 -0700202 releaseAndResetMediaBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800203 CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
204 ALOGV("[%s] got %zu input and %zu output buffers",
205 mComponentName.c_str(),
206 mInputBuffers.size(),
207 mOutputBuffers.size());
Andreas Huber078cfcf2011-09-15 12:25:04 -0700208
Wei Jiac6cfd702014-11-11 16:33:20 -0800209 if (mRenderer != NULL) {
210 requestCodecNotification();
211 }
Wei Jia704e7262014-06-04 16:21:56 -0700212 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800213 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800214}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700215
Chong Zhang7137ec72014-11-12 16:41:05 -0800216void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
217 bool hadNoRenderer = (mRenderer == NULL);
218 mRenderer = renderer;
219 if (hadNoRenderer && mRenderer != NULL) {
220 requestCodecNotification();
221 }
222}
223
224void NuPlayer::Decoder::onGetInputBuffers(
225 Vector<sp<ABuffer> > *dstBuffers) {
226 dstBuffers->clear();
227 for (size_t i = 0; i < mInputBuffers.size(); i++) {
228 dstBuffers->push(mInputBuffers[i]);
229 }
230}
231
Chong Zhangf8d71772014-11-26 15:08:34 -0800232void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800233 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800234
235 if (notifyComplete) {
236 mResumePending = true;
237 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800238}
239
Chong Zhang66704af2015-03-03 19:32:35 -0800240void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800241 if (mCCDecoder != NULL) {
242 mCCDecoder->flush();
243 }
244
245 if (mRenderer != NULL) {
246 mRenderer->flush(mIsAudio, notifyComplete);
247 mRenderer->signalTimeDiscontinuity();
248 }
249
250 status_t err = OK;
251 if (mCodec != NULL) {
252 err = mCodec->flush();
253 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
254 ++mBufferGeneration;
255 }
256
257 if (err != OK) {
258 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
259 handleError(err);
260 // finish with posting kWhatFlushCompleted.
261 // we attempt to release the buffers even if flush fails.
262 }
263 releaseAndResetMediaBuffers();
Chong Zhang66704af2015-03-03 19:32:35 -0800264}
Chong Zhang7137ec72014-11-12 16:41:05 -0800265
Chong Zhang66704af2015-03-03 19:32:35 -0800266void NuPlayer::Decoder::onFlush() {
267 doFlush(true);
268
269 if (isDiscontinuityPending()) {
270 // This could happen if the client starts seeking/shutdown
271 // after we queued an EOS for discontinuities.
272 // We can consider discontinuity handled.
273 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800274 }
Chong Zhang66704af2015-03-03 19:32:35 -0800275
276 sp<AMessage> notify = mNotify->dup();
277 notify->setInt32("what", kWhatFlushCompleted);
278 notify->post();
279 mPaused = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800280}
281
282void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
283 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800284
285 // if there is a pending resume request, notify complete now
286 notifyResumeCompleteIfNecessary();
287
Chong Zhang7137ec72014-11-12 16:41:05 -0800288 if (mCodec != NULL) {
289 err = mCodec->release();
290 mCodec = NULL;
291 ++mBufferGeneration;
292
293 if (mNativeWindow != NULL) {
294 // reconnect to surface as MediaCodec disconnected from it
295 status_t error =
296 native_window_api_connect(
297 mNativeWindow->getNativeWindow().get(),
298 NATIVE_WINDOW_API_MEDIA);
299 ALOGW_IF(error != NO_ERROR,
300 "[%s] failed to connect to native window, error=%d",
301 mComponentName.c_str(), error);
302 }
303 mComponentName = "decoder";
304 }
305
306 releaseAndResetMediaBuffers();
307
308 if (err != OK) {
309 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
310 handleError(err);
311 // finish with posting kWhatShutdownCompleted.
312 }
313
314 if (notifyComplete) {
315 sp<AMessage> notify = mNotify->dup();
316 notify->setInt32("what", kWhatShutdownCompleted);
317 notify->post();
318 mPaused = true;
319 }
320}
321
322void NuPlayer::Decoder::doRequestBuffers() {
Chong Zhang66704af2015-03-03 19:32:35 -0800323 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800324 return;
325 }
326 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800327 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800328 size_t bufferIx = *mDequeuedInputBuffers.begin();
329 sp<AMessage> msg = new AMessage();
330 msg->setSize("buffer-ix", bufferIx);
331 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800332 if (err != OK && err != ERROR_END_OF_STREAM) {
333 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800334 break;
335 }
336 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
337
338 if (!mPendingInputMessages.empty()
339 || !onInputBufferFetched(msg)) {
340 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700341 }
342 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800343
344 if (err == -EWOULDBLOCK
345 && mSource->feedMoreTSData() == OK) {
346 scheduleRequestBuffers();
Wei Jia81e50d02014-07-24 10:28:47 -0700347 }
Lajos Molnar09524832014-07-17 14:29:51 -0700348}
349
Lajos Molnar1cd13982014-01-17 15:12:51 -0800350bool NuPlayer::Decoder::handleAnInputBuffer() {
Chong Zhang66704af2015-03-03 19:32:35 -0800351 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800352 return false;
353 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800354 size_t bufferIx = -1;
355 status_t res = mCodec->dequeueInputBuffer(&bufferIx);
356 ALOGV("[%s] dequeued input: %d",
357 mComponentName.c_str(), res == OK ? (int)bufferIx : res);
358 if (res != OK) {
359 if (res != -EAGAIN) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700360 ALOGE("Failed to dequeue input buffer for %s (err=%d)",
361 mComponentName.c_str(), res);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800362 handleError(res);
363 }
364 return false;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700365 }
366
Lajos Molnar1cd13982014-01-17 15:12:51 -0800367 CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800368
Lajos Molnar09524832014-07-17 14:29:51 -0700369 if (mMediaBuffers[bufferIx] != NULL) {
370 mMediaBuffers[bufferIx]->release();
371 mMediaBuffers.editItemAt(bufferIx) = NULL;
372 }
373 mInputBufferIsDequeued.editItemAt(bufferIx) = true;
374
Lajos Molnar87603c02014-08-20 19:25:30 -0700375 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800376 sp<AMessage> msg = new AMessage();
377 msg->setSize("buffer-ix", bufferIx);
378
Lajos Molnar87603c02014-08-20 19:25:30 -0700379 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
380 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800381 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700382 mCSDsToSubmit.removeAt(0);
Chong Zhang7137ec72014-11-12 16:41:05 -0800383 CHECK(onInputBufferFetched(msg));
Wei Jia2245fc62014-10-02 15:12:25 -0700384 return true;
385 }
386
387 while (!mPendingInputMessages.empty()) {
388 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800389 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700390 break;
391 }
392 mPendingInputMessages.erase(mPendingInputMessages.begin());
393 }
394
395 if (!mInputBufferIsDequeued.editItemAt(bufferIx)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700396 return true;
397 }
398
Chong Zhang7137ec72014-11-12 16:41:05 -0800399 mDequeuedInputBuffers.push_back(bufferIx);
400
401 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800402 return true;
403}
404
Chong Zhang7137ec72014-11-12 16:41:05 -0800405bool NuPlayer::Decoder::handleAnOutputBuffer() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800406 size_t bufferIx = -1;
407 size_t offset;
408 size_t size;
409 int64_t timeUs;
410 uint32_t flags;
411 status_t res = mCodec->dequeueOutputBuffer(
412 &bufferIx, &offset, &size, &timeUs, &flags);
413
414 if (res != OK) {
415 ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
416 } else {
417 ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
418 mComponentName.c_str(), (int)bufferIx, timeUs, flags);
419 }
420
421 if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
422 res = mCodec->getOutputBuffers(&mOutputBuffers);
423 if (res != OK) {
424 ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
425 mComponentName.c_str(), res);
426 handleError(res);
427 return false;
428 }
429 // NuPlayer ignores this
430 return true;
431 } else if (res == INFO_FORMAT_CHANGED) {
432 sp<AMessage> format = new AMessage();
433 res = mCodec->getOutputFormat(&format);
434 if (res != OK) {
435 ALOGE("Failed to get output format for %s after INFO event (err=%d)",
436 mComponentName.c_str(), res);
437 handleError(res);
438 return false;
439 }
440
441 if (!mIsAudio) {
442 sp<AMessage> notify = mNotify->dup();
443 notify->setInt32("what", kWhatVideoSizeChanged);
444 notify->setMessage("format", format);
445 notify->post();
446 } else if (mRenderer != NULL) {
447 uint32_t flags;
448 int64_t durationUs;
449 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
450 if (!hasVideo &&
451 mSource->getDuration(&durationUs) == OK &&
452 durationUs
453 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
454 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
455 } else {
456 flags = AUDIO_OUTPUT_FLAG_NONE;
457 }
458
Andy Hung202bce12014-12-03 11:47:36 -0800459 res = mRenderer->openAudioSink(
460 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaded */);
461 if (res != OK) {
462 ALOGE("Failed to open AudioSink on format change for %s (err=%d)",
463 mComponentName.c_str(), res);
464 handleError(res);
465 return false;
466 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800467 }
468 return true;
469 } else if (res == INFO_DISCONTINUITY) {
470 // nothing to do
471 return true;
472 } else if (res != OK) {
473 if (res != -EAGAIN) {
474 ALOGE("Failed to dequeue output buffer for %s (err=%d)",
475 mComponentName.c_str(), res);
476 handleError(res);
477 }
478 return false;
479 }
480
481 CHECK_LT(bufferIx, mOutputBuffers.size());
482 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
483 buffer->setRange(offset, size);
484 buffer->meta()->clear();
485 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800486
487 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800488 // we do not expect CODECCONFIG or SYNCFRAME for decoder
489
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800490 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800491 reply->setSize("buffer-ix", bufferIx);
492 reply->setInt32("generation", mBufferGeneration);
493
Chong Zhang66704af2015-03-03 19:32:35 -0800494 if (eos) {
495 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
496
497 buffer->meta()->setInt32("eos", true);
498 reply->setInt32("eos", true);
499 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800500 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
501 ALOGV("[%s] dropping buffer at time %lld as requested.",
502 mComponentName.c_str(), (long long)timeUs);
503
504 reply->post();
505 return true;
506 }
507
508 mSkipRenderingUntilMediaTimeUs = -1;
509 }
510
Chong Zhangf8d71772014-11-26 15:08:34 -0800511 // wait until 1st frame comes out to signal resume complete
512 notifyResumeCompleteIfNecessary();
513
Chong Zhang7137ec72014-11-12 16:41:05 -0800514 if (mRenderer != NULL) {
515 // send the buffer to renderer.
516 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800517 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800518 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
519 }
520 }
521
522 return true;
523}
524
525void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
526 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
527 if (mMediaBuffers[i] != NULL) {
528 mMediaBuffers[i]->release();
529 mMediaBuffers.editItemAt(i) = NULL;
530 }
531 }
532 mMediaBuffers.resize(mInputBuffers.size());
533 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
534 mMediaBuffers.editItemAt(i) = NULL;
535 }
536 mInputBufferIsDequeued.clear();
537 mInputBufferIsDequeued.resize(mInputBuffers.size());
538 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
539 mInputBufferIsDequeued.editItemAt(i) = false;
540 }
541
542 mPendingInputMessages.clear();
543 mDequeuedInputBuffers.clear();
544 mSkipRenderingUntilMediaTimeUs = -1;
545}
546
547void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800548 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800549 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800550 reply->setInt32("generation", mBufferGeneration);
551 mCodec->requestActivityNotification(reply);
552 }
553}
554
555bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
556 int32_t generation;
557 CHECK(msg->findInt32("generation", &generation));
558 return generation != mBufferGeneration;
559}
560
561status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
562 sp<ABuffer> accessUnit;
563 bool dropAccessUnit;
564 do {
565 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
566
567 if (err == -EWOULDBLOCK) {
568 return err;
569 } else if (err != OK) {
570 if (err == INFO_DISCONTINUITY) {
571 int32_t type;
572 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
573
574 bool formatChange =
575 (mIsAudio &&
576 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
577 || (!mIsAudio &&
578 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
579
580 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
581
582 ALOGI("%s discontinuity (format=%d, time=%d)",
583 mIsAudio ? "audio" : "video", formatChange, timeChange);
584
585 bool seamlessFormatChange = false;
586 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
587 if (formatChange) {
588 seamlessFormatChange =
589 supportsSeamlessFormatChange(newFormat);
590 // treat seamless format change separately
591 formatChange = !seamlessFormatChange;
592 }
593
Chong Zhang66704af2015-03-03 19:32:35 -0800594 // For format or time change, return EOS to queue EOS input,
595 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800596 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800597 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800598 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800599 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800600 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800601 mTimeChangePending = true;
602 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800603 } else if (seamlessFormatChange) {
604 // reuse existing decoder and don't flush
605 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800606 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800607 } else {
608 // This stream is unaffected by the discontinuity
609 return -EWOULDBLOCK;
610 }
611 }
612
Chong Zhang66704af2015-03-03 19:32:35 -0800613 // reply should only be returned without a buffer set
614 // when there is an error (including EOS)
615 CHECK(err != OK);
616
Chong Zhang7137ec72014-11-12 16:41:05 -0800617 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800618 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800619 }
620
621 if (!mIsAudio) {
622 ++mNumFramesTotal;
623 }
624
625 dropAccessUnit = false;
626 if (!mIsAudio
627 && !mIsSecure
628 && mRenderer->getVideoLateByUs() > 100000ll
629 && mIsVideoAVC
630 && !IsAVCReferenceFrame(accessUnit)) {
631 dropAccessUnit = true;
632 ++mNumFramesDropped;
633 }
634 } while (dropAccessUnit);
635
636 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
637#if 0
638 int64_t mediaTimeUs;
639 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang66704af2015-03-03 19:32:35 -0800640 ALOGV("[%s] feeding input buffer at media time %" PRId64,
Chong Zhang7137ec72014-11-12 16:41:05 -0800641 mIsAudio ? "audio" : "video",
642 mediaTimeUs / 1E6);
643#endif
644
645 if (mCCDecoder != NULL) {
646 mCCDecoder->decode(accessUnit);
647 }
648
649 reply->setBuffer("buffer", accessUnit);
650
651 return OK;
652}
653
654bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800655 size_t bufferIx;
656 CHECK(msg->findSize("buffer-ix", &bufferIx));
657 CHECK_LT(bufferIx, mInputBuffers.size());
658 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
659
660 sp<ABuffer> buffer;
661 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700662
663 // handle widevine classic source - that fills an arbitrary input buffer
664 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700665 if (hasBuffer) {
666 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
667 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700668 // likely filled another buffer than we requested: adjust buffer index
669 size_t ix;
670 for (ix = 0; ix < mInputBuffers.size(); ix++) {
671 const sp<ABuffer> &buf = mInputBuffers[ix];
672 if (buf->data() == mediaBuffer->data()) {
673 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700674 if (!mInputBufferIsDequeued[ix]) {
675 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
676 mComponentName.c_str(), ix, bufferIx);
677 mediaBuffer->release();
678 return false;
679 }
Lajos Molnar09524832014-07-17 14:29:51 -0700680
681 // TRICKY: need buffer for the metadata, so instead, set
682 // codecBuffer to the same (though incorrect) buffer to
683 // avoid a memcpy into the codecBuffer
684 codecBuffer = buffer;
685 codecBuffer->setRange(
686 mediaBuffer->range_offset(),
687 mediaBuffer->range_length());
688 bufferIx = ix;
689 break;
690 }
691 }
692 CHECK(ix < mInputBuffers.size());
693 }
694 }
695
Lajos Molnar1cd13982014-01-17 15:12:51 -0800696 if (buffer == NULL /* includes !hasBuffer */) {
697 int32_t streamErr = ERROR_END_OF_STREAM;
698 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
699
Chong Zhang66704af2015-03-03 19:32:35 -0800700 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800701
702 // attempt to queue EOS
703 status_t err = mCodec->queueInputBuffer(
704 bufferIx,
705 0,
706 0,
707 0,
708 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700709 if (err == OK) {
710 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
711 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800712 streamErr = err;
713 // err will not be ERROR_END_OF_STREAM
714 }
715
716 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700717 ALOGE("Stream error for %s (err=%d), EOS %s queued",
718 mComponentName.c_str(),
719 streamErr,
720 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800721 handleError(streamErr);
722 }
723 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800724 sp<AMessage> extra;
725 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
726 int64_t resumeAtMediaTimeUs;
727 if (extra->findInt64(
728 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
729 ALOGI("[%s] suppressing rendering until %lld us",
730 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
731 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
732 }
733 }
734
Lajos Molnar1cd13982014-01-17 15:12:51 -0800735 int64_t timeUs = 0;
736 uint32_t flags = 0;
737 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
738
Lajos Molnar87603c02014-08-20 19:25:30 -0700739 int32_t eos, csd;
740 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800741 if (buffer->meta()->findInt32("eos", &eos) && eos) {
742 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700743 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
744 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800745 }
746
747 // copy into codec buffer
748 if (buffer != codecBuffer) {
749 CHECK_LE(buffer->size(), codecBuffer->capacity());
750 codecBuffer->setRange(0, buffer->size());
751 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
752 }
753
754 status_t err = mCodec->queueInputBuffer(
755 bufferIx,
756 codecBuffer->offset(),
757 codecBuffer->size(),
758 timeUs,
759 flags);
760 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700761 if (mediaBuffer != NULL) {
762 mediaBuffer->release();
763 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800764 ALOGE("Failed to queue input buffer for %s (err=%d)",
765 mComponentName.c_str(), err);
766 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700767 } else {
768 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
769 if (mediaBuffer != NULL) {
770 CHECK(mMediaBuffers[bufferIx] == NULL);
771 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
772 }
Lajos Molnar09524832014-07-17 14:29:51 -0700773 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800774 }
Wei Jia2245fc62014-10-02 15:12:25 -0700775 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800776}
777
Lajos Molnar1cd13982014-01-17 15:12:51 -0800778void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
779 status_t err;
780 int32_t render;
781 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800782 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800783 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800784
Chong Zhang7137ec72014-11-12 16:41:05 -0800785 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800786 int64_t timeUs;
787 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
788 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800789
790 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
791 mCCDecoder->display(timeUs);
792 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800793 }
794
Lajos Molnar1cd13982014-01-17 15:12:51 -0800795 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700796 int64_t timestampNs;
797 CHECK(msg->findInt64("timestampNs", &timestampNs));
798 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800799 } else {
800 err = mCodec->releaseOutputBuffer(bufferIx);
801 }
802 if (err != OK) {
803 ALOGE("failed to release output buffer for %s (err=%d)",
804 mComponentName.c_str(), err);
805 handleError(err);
806 }
Chong Zhang66704af2015-03-03 19:32:35 -0800807 if (msg->findInt32("eos", &eos) && eos
808 && isDiscontinuityPending()) {
809 finishHandleDiscontinuity(true /* flushOnTimeChange */);
810 }
811}
812
813bool NuPlayer::Decoder::isDiscontinuityPending() const {
814 return mFormatChangePending || mTimeChangePending;
815}
816
817void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
818 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
819 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
820
821 // If we have format change, pause and wait to be killed;
822 // If we have time change only, flush and restart fetching.
823
824 if (mFormatChangePending) {
825 mPaused = true;
826 } else if (mTimeChangePending) {
827 if (flushOnTimeChange) {
828 doFlush(false /*notifyComplete*/);
829 }
830
831 // restart fetching input
832 scheduleRequestBuffers();
833 }
834
835 // Notify NuPlayer to either shutdown decoder, or rescan sources
836 sp<AMessage> msg = mNotify->dup();
837 msg->setInt32("what", kWhatInputDiscontinuity);
838 msg->setInt32("formatChange", mFormatChangePending);
839 msg->post();
840
841 mFormatChangePending = false;
842 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800843}
844
Chong Zhang7137ec72014-11-12 16:41:05 -0800845bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
846 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800847 if (targetFormat == NULL) {
848 return true;
849 }
850
851 AString mime;
852 if (!targetFormat->findString("mime", &mime)) {
853 return false;
854 }
855
856 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
857 // field-by-field comparison
858 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
859 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
860 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100861 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800862 !targetFormat->findInt32(keys[i], &newVal) ||
863 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800864 return false;
865 }
866 }
867
868 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100869 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800870 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800871 if (oldBuf->size() != newBuf->size()) {
872 return false;
873 }
874 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
875 }
876 }
877 return false;
878}
879
880bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +0100881 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800882 return false;
883 }
884
885 if (targetFormat == NULL) {
886 return true;
887 }
888
889 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100890 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800891 || !targetFormat->findString("mime", &newMime)
892 || !(oldMime == newMime)) {
893 return false;
894 }
895
896 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
897 bool seamless;
898 if (audio) {
899 seamless = supportsSeamlessAudioFormatChange(targetFormat);
900 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800901 int32_t isAdaptive;
902 seamless = (mCodec != NULL &&
903 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
904 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800905 }
906
907 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
908 return seamless;
909}
910
Chong Zhang7137ec72014-11-12 16:41:05 -0800911void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
912 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -0700913 return;
914 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800915 mCSDsForCurrentFormat.clear();
916 for (int32_t i = 0; ; ++i) {
917 AString tag = "csd-";
918 tag.append(i);
919 sp<ABuffer> buffer;
920 if (!format->findBuffer(tag.c_str(), &buffer)) {
921 break;
922 }
923 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700924 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700925}
926
Chong Zhangf8d71772014-11-26 15:08:34 -0800927void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
928 if (mResumePending) {
929 mResumePending = false;
930
931 sp<AMessage> notify = mNotify->dup();
932 notify->setInt32("what", kWhatResumeCompleted);
933 notify->post();
934 }
935}
936
Andreas Huberf9334412010-12-15 15:17:42 -0800937} // namespace android
938