blob: 99a2a841f58f92a377eb33993cabd729d1d34f1f [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
Andy Hung288da022015-05-31 22:55:59 -070027#include <cutils/properties.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080028#include <media/ICrypto.h>
Andreas Huberf9334412010-12-15 15:17:42 -080029#include <media/stagefright/foundation/ABuffer.h>
30#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080031#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070032#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080033#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080034#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080035#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080036
Lajos Molnar1de1e252015-04-30 18:18:34 -070037#include <gui/Surface.h>
38
Chong Zhang7137ec72014-11-12 16:41:05 -080039#include "avc_utils.h"
40#include "ATSParser.h"
41
Andreas Huberf9334412010-12-15 15:17:42 -080042namespace android {
43
Andy Hung288da022015-05-31 22:55:59 -070044static inline bool getAudioDeepBufferSetting() {
45 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
46}
47
Andreas Huberf9334412010-12-15 15:17:42 -080048NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080049 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080050 const sp<Source> &source,
51 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070052 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080053 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080054 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070055 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080056 mSource(source),
57 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080058 mCCDecoder(ccDecoder),
Wei Jiac6cfd702014-11-11 16:33:20 -080059 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080060 mNumFramesTotal(0ll),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070061 mNumInputFramesDropped(0ll),
62 mNumOutputFramesDropped(0ll),
63 mVideoWidth(0),
64 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080065 mIsAudio(true),
66 mIsVideoAVC(false),
67 mIsSecure(false),
68 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080069 mTimeChangePending(false),
Wei Jia704e7262014-06-04 16:21:56 -070070 mPaused(true),
Chong Zhangf8d71772014-11-26 15:08:34 -080071 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080072 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080073 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070074 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080075 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080076}
77
78NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070079 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070080 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080081}
82
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070083sp<AMessage> NuPlayer::Decoder::getStats() const {
84 mStats->setInt64("frames-total", mNumFramesTotal);
85 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
86 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
87 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -070088}
89
Chong Zhang7137ec72014-11-12 16:41:05 -080090void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
91 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
92
93 switch (msg->what()) {
94 case kWhatCodecNotify:
95 {
Chong Zhang3b032b32015-04-17 15:49:06 -070096 int32_t cbID;
97 CHECK(msg->findInt32("callbackID", &cbID));
98
99 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
100 mIsAudio ? "audio" : "video", cbID, mPaused);
101
Marco Nelissen421f47c2015-03-25 14:40:32 -0700102 if (mPaused) {
103 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800104 }
105
Marco Nelissen421f47c2015-03-25 14:40:32 -0700106 switch (cbID) {
107 case MediaCodec::CB_INPUT_AVAILABLE:
108 {
109 int32_t index;
110 CHECK(msg->findInt32("index", &index));
111
112 handleAnInputBuffer(index);
113 break;
114 }
115
116 case MediaCodec::CB_OUTPUT_AVAILABLE:
117 {
118 int32_t index;
119 size_t offset;
120 size_t size;
121 int64_t timeUs;
122 int32_t flags;
123
124 CHECK(msg->findInt32("index", &index));
125 CHECK(msg->findSize("offset", &offset));
126 CHECK(msg->findSize("size", &size));
127 CHECK(msg->findInt64("timeUs", &timeUs));
128 CHECK(msg->findInt32("flags", &flags));
129
130 handleAnOutputBuffer(index, offset, size, timeUs, flags);
131 break;
132 }
133
134 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
135 {
136 sp<AMessage> format;
137 CHECK(msg->findMessage("format", &format));
138
139 handleOutputFormatChange(format);
140 break;
141 }
142
143 case MediaCodec::CB_ERROR:
144 {
145 status_t err;
146 CHECK(msg->findInt32("err", &err));
147 ALOGE("Decoder (%s) reported error : 0x%x",
148 mIsAudio ? "audio" : "video", err);
149
150 handleError(err);
151 break;
152 }
153
154 default:
155 {
156 TRESPASS();
157 break;
158 }
159 }
160
Lajos Molnar87603c02014-08-20 19:25:30 -0700161 break;
162 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800163
164 case kWhatRenderBuffer:
165 {
166 if (!isStaleReply(msg)) {
167 onRenderBuffer(msg);
168 }
169 break;
170 }
171
172 default:
173 DecoderBase::onMessageReceived(msg);
174 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700175 }
176}
177
Lajos Molnar1cd13982014-01-17 15:12:51 -0800178void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800179 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800180
Chong Zhang7137ec72014-11-12 16:41:05 -0800181 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800182 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800183
Lajos Molnar1cd13982014-01-17 15:12:51 -0800184 ++mBufferGeneration;
185
Andreas Huber84066782011-08-16 09:34:26 -0700186 AString mime;
187 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800188
Chong Zhang7137ec72014-11-12 16:41:05 -0800189 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
190 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
191
Lajos Molnar1cd13982014-01-17 15:12:51 -0800192 mComponentName = mime;
193 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700194 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800195
196 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700197 int32_t secure = 0;
198 if (format->findInt32("secure", &secure) && secure != 0) {
199 if (mCodec != NULL) {
200 mCodec->getName(&mComponentName);
201 mComponentName.append(".secure");
202 mCodec->release();
203 ALOGI("[%s] creating", mComponentName.c_str());
204 mCodec = MediaCodec::CreateByComponentName(
205 mCodecLooper, mComponentName.c_str());
206 }
207 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800208 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700209 ALOGE("Failed to create %s%s decoder",
210 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800211 handleError(UNKNOWN_ERROR);
212 return;
213 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800214 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800215
216 mCodec->getName(&mComponentName);
217
Lajos Molnar14986f62014-09-15 11:04:44 -0700218 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700219 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800220 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700221 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700222 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700223 // We treat this as a warning, as this is a preparatory step.
224 // Codec will try to connect to the surface, which is where
225 // any error signaling will occur.
226 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800227 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700228 err = mCodec->configure(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700229 format, mSurface, NULL /* crypto */, 0 /* flags */);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800230 if (err != OK) {
231 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700232 mCodec->release();
233 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800234 handleError(err);
235 return;
236 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700237 rememberCodecSpecificData(format);
238
Lajos Molnar1cd13982014-01-17 15:12:51 -0800239 // the following should work in configured state
240 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
241 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
242
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700243 mStats->setString("mime", mime.c_str());
244 mStats->setString("component-name", mComponentName.c_str());
245
246 if (!mIsAudio) {
247 int32_t width, height;
248 if (mOutputFormat->findInt32("width", &width)
249 && mOutputFormat->findInt32("height", &height)) {
250 mStats->setInt32("width", width);
251 mStats->setInt32("height", height);
252 }
253 }
254
Marco Nelissen421f47c2015-03-25 14:40:32 -0700255 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
256 mCodec->setCallback(reply);
257
Lajos Molnar1cd13982014-01-17 15:12:51 -0800258 err = mCodec->start();
259 if (err != OK) {
260 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700261 mCodec->release();
262 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800263 handleError(err);
264 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800265 }
266
Lajos Molnar09524832014-07-17 14:29:51 -0700267 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700268
Wei Jia704e7262014-06-04 16:21:56 -0700269 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800270 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800271}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700272
Ronghua Wu8db88132015-04-22 13:51:35 -0700273void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
274 if (mCodec == NULL) {
275 ALOGW("onSetParameters called before codec is created.");
276 return;
277 }
278 mCodec->setParameters(params);
279}
280
Chong Zhang7137ec72014-11-12 16:41:05 -0800281void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
282 bool hadNoRenderer = (mRenderer == NULL);
283 mRenderer = renderer;
284 if (hadNoRenderer && mRenderer != NULL) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700285 // this means that the widevine legacy source is ready
286 onRequestInputBuffers();
Chong Zhang7137ec72014-11-12 16:41:05 -0800287 }
288}
289
290void NuPlayer::Decoder::onGetInputBuffers(
291 Vector<sp<ABuffer> > *dstBuffers) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700292 CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers));
Chong Zhang7137ec72014-11-12 16:41:05 -0800293}
294
Chong Zhangf8d71772014-11-26 15:08:34 -0800295void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800296 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800297
298 if (notifyComplete) {
299 mResumePending = true;
300 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700301 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800302}
303
Chong Zhang66704af2015-03-03 19:32:35 -0800304void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800305 if (mCCDecoder != NULL) {
306 mCCDecoder->flush();
307 }
308
309 if (mRenderer != NULL) {
310 mRenderer->flush(mIsAudio, notifyComplete);
311 mRenderer->signalTimeDiscontinuity();
312 }
313
314 status_t err = OK;
315 if (mCodec != NULL) {
316 err = mCodec->flush();
317 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
318 ++mBufferGeneration;
319 }
320
321 if (err != OK) {
322 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
323 handleError(err);
324 // finish with posting kWhatFlushCompleted.
325 // we attempt to release the buffers even if flush fails.
326 }
327 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700328 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800329}
Chong Zhang7137ec72014-11-12 16:41:05 -0800330
Marco Nelissen421f47c2015-03-25 14:40:32 -0700331
Chong Zhang66704af2015-03-03 19:32:35 -0800332void NuPlayer::Decoder::onFlush() {
333 doFlush(true);
334
335 if (isDiscontinuityPending()) {
336 // This could happen if the client starts seeking/shutdown
337 // after we queued an EOS for discontinuities.
338 // We can consider discontinuity handled.
339 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800340 }
Chong Zhang66704af2015-03-03 19:32:35 -0800341
342 sp<AMessage> notify = mNotify->dup();
343 notify->setInt32("what", kWhatFlushCompleted);
344 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800345}
346
347void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
348 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800349
350 // if there is a pending resume request, notify complete now
351 notifyResumeCompleteIfNecessary();
352
Chong Zhang7137ec72014-11-12 16:41:05 -0800353 if (mCodec != NULL) {
354 err = mCodec->release();
355 mCodec = NULL;
356 ++mBufferGeneration;
357
Lajos Molnar1de1e252015-04-30 18:18:34 -0700358 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800359 // reconnect to surface as MediaCodec disconnected from it
360 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700361 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800362 ALOGW_IF(error != NO_ERROR,
363 "[%s] failed to connect to native window, error=%d",
364 mComponentName.c_str(), error);
365 }
366 mComponentName = "decoder";
367 }
368
369 releaseAndResetMediaBuffers();
370
371 if (err != OK) {
372 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
373 handleError(err);
374 // finish with posting kWhatShutdownCompleted.
375 }
376
377 if (notifyComplete) {
378 sp<AMessage> notify = mNotify->dup();
379 notify->setInt32("what", kWhatShutdownCompleted);
380 notify->post();
381 mPaused = true;
382 }
383}
384
Chong Zhang3b032b32015-04-17 15:49:06 -0700385/*
386 * returns true if we should request more data
387 */
388bool NuPlayer::Decoder::doRequestBuffers() {
Lajos Molnare6109e22015-04-10 17:18:22 -0700389 // mRenderer is only NULL if we have a legacy widevine source that
390 // is not yet ready. In this case we must not fetch input.
391 if (isDiscontinuityPending() || mRenderer == NULL) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700392 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800393 }
394 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800395 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800396 size_t bufferIx = *mDequeuedInputBuffers.begin();
397 sp<AMessage> msg = new AMessage();
398 msg->setSize("buffer-ix", bufferIx);
399 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800400 if (err != OK && err != ERROR_END_OF_STREAM) {
401 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800402 break;
403 }
404 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
405
406 if (!mPendingInputMessages.empty()
407 || !onInputBufferFetched(msg)) {
408 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700409 }
410 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800411
Chong Zhang3b032b32015-04-17 15:49:06 -0700412 return err == -EWOULDBLOCK
413 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700414}
415
Marco Nelissen421f47c2015-03-25 14:40:32 -0700416void NuPlayer::Decoder::handleError(int32_t err)
417{
418 // We cannot immediately release the codec due to buffers still outstanding
419 // in the renderer. We signal to the player the error so it can shutdown/release the
420 // decoder after flushing and increment the generation to discard unnecessary messages.
421
422 ++mBufferGeneration;
423
424 sp<AMessage> notify = mNotify->dup();
425 notify->setInt32("what", kWhatError);
426 notify->setInt32("err", err);
427 notify->post();
428}
429
430bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800431 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800432 return false;
433 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700434
435 sp<ABuffer> buffer;
436 mCodec->getInputBuffer(index, &buffer);
437
Wei Jia6301a5e2015-05-13 13:15:18 -0700438 if (buffer == NULL) {
439 handleError(UNKNOWN_ERROR);
440 return false;
441 }
442
Marco Nelissen421f47c2015-03-25 14:40:32 -0700443 if (index >= mInputBuffers.size()) {
444 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
445 mInputBuffers.add();
446 mMediaBuffers.add();
447 mInputBufferIsDequeued.add();
448 mMediaBuffers.editItemAt(i) = NULL;
449 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800450 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700451 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700452 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700453
Marco Nelissen421f47c2015-03-25 14:40:32 -0700454 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800455
Marco Nelissen421f47c2015-03-25 14:40:32 -0700456 if (mMediaBuffers[index] != NULL) {
457 mMediaBuffers[index]->release();
458 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700459 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700460 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700461
Lajos Molnar87603c02014-08-20 19:25:30 -0700462 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800463 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700464 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800465
Lajos Molnar87603c02014-08-20 19:25:30 -0700466 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
467 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800468 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700469 mCSDsToSubmit.removeAt(0);
Chong Zhang7137ec72014-11-12 16:41:05 -0800470 CHECK(onInputBufferFetched(msg));
Wei Jia2245fc62014-10-02 15:12:25 -0700471 return true;
472 }
473
474 while (!mPendingInputMessages.empty()) {
475 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800476 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700477 break;
478 }
479 mPendingInputMessages.erase(mPendingInputMessages.begin());
480 }
481
Marco Nelissen421f47c2015-03-25 14:40:32 -0700482 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700483 return true;
484 }
485
Marco Nelissen421f47c2015-03-25 14:40:32 -0700486 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800487
488 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800489 return true;
490}
491
Marco Nelissen421f47c2015-03-25 14:40:32 -0700492bool NuPlayer::Decoder::handleAnOutputBuffer(
493 size_t index,
494 size_t offset,
495 size_t size,
496 int64_t timeUs,
497 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700498// CHECK_LT(bufferIx, mOutputBuffers.size());
499 sp<ABuffer> buffer;
500 mCodec->getOutputBuffer(index, &buffer);
501
502 if (index >= mOutputBuffers.size()) {
503 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
504 mOutputBuffers.add();
505 }
506 }
507
508 mOutputBuffers.editItemAt(index) = buffer;
509
Chong Zhang7137ec72014-11-12 16:41:05 -0800510 buffer->setRange(offset, size);
511 buffer->meta()->clear();
512 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800513
514 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800515 // we do not expect CODECCONFIG or SYNCFRAME for decoder
516
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800517 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700518 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800519 reply->setInt32("generation", mBufferGeneration);
520
Chong Zhang66704af2015-03-03 19:32:35 -0800521 if (eos) {
522 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
523
524 buffer->meta()->setInt32("eos", true);
525 reply->setInt32("eos", true);
526 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800527 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
528 ALOGV("[%s] dropping buffer at time %lld as requested.",
529 mComponentName.c_str(), (long long)timeUs);
530
531 reply->post();
532 return true;
533 }
534
535 mSkipRenderingUntilMediaTimeUs = -1;
536 }
537
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700538 mNumFramesTotal += !mIsAudio;
539
Chong Zhangf8d71772014-11-26 15:08:34 -0800540 // wait until 1st frame comes out to signal resume complete
541 notifyResumeCompleteIfNecessary();
542
Chong Zhang7137ec72014-11-12 16:41:05 -0800543 if (mRenderer != NULL) {
544 // send the buffer to renderer.
545 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800546 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800547 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
548 }
549 }
550
551 return true;
552}
553
Marco Nelissen421f47c2015-03-25 14:40:32 -0700554void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
555 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700556 int32_t width, height;
557 if (format->findInt32("width", &width)
558 && format->findInt32("height", &height)) {
559 mStats->setInt32("width", width);
560 mStats->setInt32("height", height);
561 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700562 sp<AMessage> notify = mNotify->dup();
563 notify->setInt32("what", kWhatVideoSizeChanged);
564 notify->setMessage("format", format);
565 notify->post();
566 } else if (mRenderer != NULL) {
567 uint32_t flags;
568 int64_t durationUs;
569 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700570 if (getAudioDeepBufferSetting() // override regardless of source duration
571 || (!hasVideo
572 && mSource->getDuration(&durationUs) == OK
573 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700574 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
575 } else {
576 flags = AUDIO_OUTPUT_FLAG_NONE;
577 }
578
579 mRenderer->openAudioSink(
580 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
581 }
582}
583
Chong Zhang7137ec72014-11-12 16:41:05 -0800584void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
585 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
586 if (mMediaBuffers[i] != NULL) {
587 mMediaBuffers[i]->release();
588 mMediaBuffers.editItemAt(i) = NULL;
589 }
590 }
591 mMediaBuffers.resize(mInputBuffers.size());
592 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
593 mMediaBuffers.editItemAt(i) = NULL;
594 }
595 mInputBufferIsDequeued.clear();
596 mInputBufferIsDequeued.resize(mInputBuffers.size());
597 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
598 mInputBufferIsDequeued.editItemAt(i) = false;
599 }
600
601 mPendingInputMessages.clear();
602 mDequeuedInputBuffers.clear();
603 mSkipRenderingUntilMediaTimeUs = -1;
604}
605
606void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800607 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800608 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800609 reply->setInt32("generation", mBufferGeneration);
610 mCodec->requestActivityNotification(reply);
611 }
612}
613
614bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
615 int32_t generation;
616 CHECK(msg->findInt32("generation", &generation));
617 return generation != mBufferGeneration;
618}
619
620status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
621 sp<ABuffer> accessUnit;
622 bool dropAccessUnit;
623 do {
624 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
625
626 if (err == -EWOULDBLOCK) {
627 return err;
628 } else if (err != OK) {
629 if (err == INFO_DISCONTINUITY) {
630 int32_t type;
631 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
632
633 bool formatChange =
634 (mIsAudio &&
635 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
636 || (!mIsAudio &&
637 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
638
639 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
640
641 ALOGI("%s discontinuity (format=%d, time=%d)",
642 mIsAudio ? "audio" : "video", formatChange, timeChange);
643
644 bool seamlessFormatChange = false;
645 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
646 if (formatChange) {
647 seamlessFormatChange =
648 supportsSeamlessFormatChange(newFormat);
649 // treat seamless format change separately
650 formatChange = !seamlessFormatChange;
651 }
652
Chong Zhang66704af2015-03-03 19:32:35 -0800653 // For format or time change, return EOS to queue EOS input,
654 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800655 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800656 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800657 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800658 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800659 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800660 mTimeChangePending = true;
661 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800662 } else if (seamlessFormatChange) {
663 // reuse existing decoder and don't flush
664 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800665 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800666 } else {
667 // This stream is unaffected by the discontinuity
668 return -EWOULDBLOCK;
669 }
670 }
671
Chong Zhang66704af2015-03-03 19:32:35 -0800672 // reply should only be returned without a buffer set
673 // when there is an error (including EOS)
674 CHECK(err != OK);
675
Chong Zhang7137ec72014-11-12 16:41:05 -0800676 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800677 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800678 }
679
Chong Zhang7137ec72014-11-12 16:41:05 -0800680 dropAccessUnit = false;
681 if (!mIsAudio
682 && !mIsSecure
683 && mRenderer->getVideoLateByUs() > 100000ll
684 && mIsVideoAVC
685 && !IsAVCReferenceFrame(accessUnit)) {
686 dropAccessUnit = true;
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700687 ++mNumInputFramesDropped;
Chong Zhang7137ec72014-11-12 16:41:05 -0800688 }
689 } while (dropAccessUnit);
690
691 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
692#if 0
693 int64_t mediaTimeUs;
694 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700695 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800696 mIsAudio ? "audio" : "video",
697 mediaTimeUs / 1E6);
698#endif
699
700 if (mCCDecoder != NULL) {
701 mCCDecoder->decode(accessUnit);
702 }
703
704 reply->setBuffer("buffer", accessUnit);
705
706 return OK;
707}
708
709bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800710 size_t bufferIx;
711 CHECK(msg->findSize("buffer-ix", &bufferIx));
712 CHECK_LT(bufferIx, mInputBuffers.size());
713 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
714
715 sp<ABuffer> buffer;
716 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700717
718 // handle widevine classic source - that fills an arbitrary input buffer
719 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700720 if (hasBuffer) {
721 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
722 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700723 // likely filled another buffer than we requested: adjust buffer index
724 size_t ix;
725 for (ix = 0; ix < mInputBuffers.size(); ix++) {
726 const sp<ABuffer> &buf = mInputBuffers[ix];
727 if (buf->data() == mediaBuffer->data()) {
728 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700729 if (!mInputBufferIsDequeued[ix]) {
730 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
731 mComponentName.c_str(), ix, bufferIx);
732 mediaBuffer->release();
733 return false;
734 }
Lajos Molnar09524832014-07-17 14:29:51 -0700735
736 // TRICKY: need buffer for the metadata, so instead, set
737 // codecBuffer to the same (though incorrect) buffer to
738 // avoid a memcpy into the codecBuffer
739 codecBuffer = buffer;
740 codecBuffer->setRange(
741 mediaBuffer->range_offset(),
742 mediaBuffer->range_length());
743 bufferIx = ix;
744 break;
745 }
746 }
747 CHECK(ix < mInputBuffers.size());
748 }
749 }
750
Lajos Molnar1cd13982014-01-17 15:12:51 -0800751 if (buffer == NULL /* includes !hasBuffer */) {
752 int32_t streamErr = ERROR_END_OF_STREAM;
753 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
754
Chong Zhang66704af2015-03-03 19:32:35 -0800755 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800756
757 // attempt to queue EOS
758 status_t err = mCodec->queueInputBuffer(
759 bufferIx,
760 0,
761 0,
762 0,
763 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700764 if (err == OK) {
765 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
766 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800767 streamErr = err;
768 // err will not be ERROR_END_OF_STREAM
769 }
770
771 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700772 ALOGE("Stream error for %s (err=%d), EOS %s queued",
773 mComponentName.c_str(),
774 streamErr,
775 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800776 handleError(streamErr);
777 }
778 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800779 sp<AMessage> extra;
780 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
781 int64_t resumeAtMediaTimeUs;
782 if (extra->findInt64(
783 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
784 ALOGI("[%s] suppressing rendering until %lld us",
785 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
786 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
787 }
788 }
789
Lajos Molnar1cd13982014-01-17 15:12:51 -0800790 int64_t timeUs = 0;
791 uint32_t flags = 0;
792 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
793
Lajos Molnar87603c02014-08-20 19:25:30 -0700794 int32_t eos, csd;
795 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800796 if (buffer->meta()->findInt32("eos", &eos) && eos) {
797 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700798 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
799 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800800 }
801
802 // copy into codec buffer
803 if (buffer != codecBuffer) {
804 CHECK_LE(buffer->size(), codecBuffer->capacity());
805 codecBuffer->setRange(0, buffer->size());
806 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
807 }
808
809 status_t err = mCodec->queueInputBuffer(
810 bufferIx,
811 codecBuffer->offset(),
812 codecBuffer->size(),
813 timeUs,
814 flags);
815 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700816 if (mediaBuffer != NULL) {
817 mediaBuffer->release();
818 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800819 ALOGE("Failed to queue input buffer for %s (err=%d)",
820 mComponentName.c_str(), err);
821 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700822 } else {
823 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
824 if (mediaBuffer != NULL) {
825 CHECK(mMediaBuffers[bufferIx] == NULL);
826 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
827 }
Lajos Molnar09524832014-07-17 14:29:51 -0700828 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800829 }
Wei Jia2245fc62014-10-02 15:12:25 -0700830 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800831}
832
Lajos Molnar1cd13982014-01-17 15:12:51 -0800833void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
834 status_t err;
835 int32_t render;
836 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800837 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800838 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800839
Chong Zhang7137ec72014-11-12 16:41:05 -0800840 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800841 int64_t timeUs;
842 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
843 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800844
845 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
846 mCCDecoder->display(timeUs);
847 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800848 }
849
Lajos Molnar1cd13982014-01-17 15:12:51 -0800850 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700851 int64_t timestampNs;
852 CHECK(msg->findInt64("timestampNs", &timestampNs));
853 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800854 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700855 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800856 err = mCodec->releaseOutputBuffer(bufferIx);
857 }
858 if (err != OK) {
859 ALOGE("failed to release output buffer for %s (err=%d)",
860 mComponentName.c_str(), err);
861 handleError(err);
862 }
Chong Zhang66704af2015-03-03 19:32:35 -0800863 if (msg->findInt32("eos", &eos) && eos
864 && isDiscontinuityPending()) {
865 finishHandleDiscontinuity(true /* flushOnTimeChange */);
866 }
867}
868
869bool NuPlayer::Decoder::isDiscontinuityPending() const {
870 return mFormatChangePending || mTimeChangePending;
871}
872
873void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
874 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
875 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
876
877 // If we have format change, pause and wait to be killed;
878 // If we have time change only, flush and restart fetching.
879
880 if (mFormatChangePending) {
881 mPaused = true;
882 } else if (mTimeChangePending) {
883 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700884 doFlush(false /* notifyComplete */);
885 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -0800886 }
Chong Zhang66704af2015-03-03 19:32:35 -0800887 }
888
889 // Notify NuPlayer to either shutdown decoder, or rescan sources
890 sp<AMessage> msg = mNotify->dup();
891 msg->setInt32("what", kWhatInputDiscontinuity);
892 msg->setInt32("formatChange", mFormatChangePending);
893 msg->post();
894
895 mFormatChangePending = false;
896 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800897}
898
Chong Zhang7137ec72014-11-12 16:41:05 -0800899bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
900 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800901 if (targetFormat == NULL) {
902 return true;
903 }
904
905 AString mime;
906 if (!targetFormat->findString("mime", &mime)) {
907 return false;
908 }
909
910 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
911 // field-by-field comparison
912 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
913 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
914 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100915 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800916 !targetFormat->findInt32(keys[i], &newVal) ||
917 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800918 return false;
919 }
920 }
921
922 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100923 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800924 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800925 if (oldBuf->size() != newBuf->size()) {
926 return false;
927 }
928 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
929 }
930 }
931 return false;
932}
933
934bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +0100935 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800936 return false;
937 }
938
939 if (targetFormat == NULL) {
940 return true;
941 }
942
943 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100944 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -0800945 || !targetFormat->findString("mime", &newMime)
946 || !(oldMime == newMime)) {
947 return false;
948 }
949
950 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
951 bool seamless;
952 if (audio) {
953 seamless = supportsSeamlessAudioFormatChange(targetFormat);
954 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800955 int32_t isAdaptive;
956 seamless = (mCodec != NULL &&
957 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
958 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -0800959 }
960
961 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
962 return seamless;
963}
964
Chong Zhang7137ec72014-11-12 16:41:05 -0800965void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
966 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -0700967 return;
968 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800969 mCSDsForCurrentFormat.clear();
970 for (int32_t i = 0; ; ++i) {
971 AString tag = "csd-";
972 tag.append(i);
973 sp<ABuffer> buffer;
974 if (!format->findBuffer(tag.c_str(), &buffer)) {
975 break;
976 }
977 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700978 }
Chong Zhangb86e68f2014-08-01 13:46:53 -0700979}
980
Chong Zhangf8d71772014-11-26 15:08:34 -0800981void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
982 if (mResumePending) {
983 mResumePending = false;
984
985 sp<AMessage> notify = mNotify->dup();
986 notify->setInt32("what", kWhatResumeCompleted);
987 notify->post();
988 }
989}
990
Andreas Huberf9334412010-12-15 15:17:42 -0800991} // namespace android
992