blob: ce87f874b1290fd7c6ef0c2f2c7a53ab8f11bda5 [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,
Ronghua Wu68845c12015-07-21 09:50:48 -070051 pid_t pid,
Wei Jiac6cfd702014-11-11 16:33:20 -080052 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070053 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080054 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080055 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070056 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080057 mSource(source),
58 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080059 mCCDecoder(ccDecoder),
Ronghua Wu68845c12015-07-21 09:50:48 -070060 mPid(pid),
Wei Jiac6cfd702014-11-11 16:33:20 -080061 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080062 mNumFramesTotal(0ll),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070063 mNumInputFramesDropped(0ll),
64 mNumOutputFramesDropped(0ll),
65 mVideoWidth(0),
66 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080067 mIsAudio(true),
68 mIsVideoAVC(false),
69 mIsSecure(false),
70 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080071 mTimeChangePending(false),
Chong Zhangf8d71772014-11-26 15:08:34 -080072 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080073 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080074 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070075 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080076 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080077}
78
79NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070080 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070081 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080082}
83
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070084sp<AMessage> NuPlayer::Decoder::getStats() const {
85 mStats->setInt64("frames-total", mNumFramesTotal);
86 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
87 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
88 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -070089}
90
Lajos Molnara81c6222015-07-10 19:17:45 -070091status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
92 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
93 return BAD_VALUE;
94 }
95
96 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
97
98 msg->setObject("surface", surface);
99 sp<AMessage> response;
100 status_t err = msg->postAndAwaitResponse(&response);
101 if (err == OK && response != NULL) {
102 CHECK(response->findInt32("err", &err));
103 }
104 return err;
105}
106
Chong Zhang7137ec72014-11-12 16:41:05 -0800107void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
108 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
109
110 switch (msg->what()) {
111 case kWhatCodecNotify:
112 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700113 int32_t cbID;
114 CHECK(msg->findInt32("callbackID", &cbID));
115
116 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
117 mIsAudio ? "audio" : "video", cbID, mPaused);
118
Marco Nelissen421f47c2015-03-25 14:40:32 -0700119 if (mPaused) {
120 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800121 }
122
Marco Nelissen421f47c2015-03-25 14:40:32 -0700123 switch (cbID) {
124 case MediaCodec::CB_INPUT_AVAILABLE:
125 {
126 int32_t index;
127 CHECK(msg->findInt32("index", &index));
128
129 handleAnInputBuffer(index);
130 break;
131 }
132
133 case MediaCodec::CB_OUTPUT_AVAILABLE:
134 {
135 int32_t index;
136 size_t offset;
137 size_t size;
138 int64_t timeUs;
139 int32_t flags;
140
141 CHECK(msg->findInt32("index", &index));
142 CHECK(msg->findSize("offset", &offset));
143 CHECK(msg->findSize("size", &size));
144 CHECK(msg->findInt64("timeUs", &timeUs));
145 CHECK(msg->findInt32("flags", &flags));
146
147 handleAnOutputBuffer(index, offset, size, timeUs, flags);
148 break;
149 }
150
151 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
152 {
153 sp<AMessage> format;
154 CHECK(msg->findMessage("format", &format));
155
156 handleOutputFormatChange(format);
157 break;
158 }
159
160 case MediaCodec::CB_ERROR:
161 {
162 status_t err;
163 CHECK(msg->findInt32("err", &err));
164 ALOGE("Decoder (%s) reported error : 0x%x",
165 mIsAudio ? "audio" : "video", err);
166
167 handleError(err);
168 break;
169 }
170
171 default:
172 {
173 TRESPASS();
174 break;
175 }
176 }
177
Lajos Molnar87603c02014-08-20 19:25:30 -0700178 break;
179 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800180
181 case kWhatRenderBuffer:
182 {
183 if (!isStaleReply(msg)) {
184 onRenderBuffer(msg);
185 }
186 break;
187 }
188
Lajos Molnara81c6222015-07-10 19:17:45 -0700189 case kWhatSetVideoSurface:
190 {
191 sp<AReplyToken> replyID;
192 CHECK(msg->senderAwaitsResponse(&replyID));
193
194 sp<RefBase> obj;
195 CHECK(msg->findObject("surface", &obj));
196 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
197 int32_t err = INVALID_OPERATION;
198 // NOTE: in practice mSurface is always non-null, but checking here for completeness
199 if (mCodec != NULL && mSurface != NULL) {
200 // TODO: once AwesomePlayer is removed, remove this automatic connecting
201 // to the surface by MediaPlayerService.
202 //
203 // at this point MediaPlayerService::client has already connected to the
204 // surface, which MediaCodec does not expect
205 err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
206 if (err == OK) {
207 err = mCodec->setSurface(surface);
208 ALOGI_IF(err, "codec setSurface returned: %d", err);
209 if (err == OK) {
210 // reconnect to the old surface as MPS::Client will expect to
211 // be able to disconnect from it.
212 (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
213 mSurface = surface;
214 }
215 }
216 if (err != OK) {
217 // reconnect to the new surface on error as MPS::Client will expect to
218 // be able to disconnect from it.
219 (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
220 }
221 }
222
223 sp<AMessage> response = new AMessage;
224 response->setInt32("err", err);
225 response->postReply(replyID);
226 break;
227 }
228
Chong Zhang7137ec72014-11-12 16:41:05 -0800229 default:
230 DecoderBase::onMessageReceived(msg);
231 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700232 }
233}
234
Lajos Molnar1cd13982014-01-17 15:12:51 -0800235void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800236 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800237
Chong Zhang7137ec72014-11-12 16:41:05 -0800238 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800239 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800240
Lajos Molnar1cd13982014-01-17 15:12:51 -0800241 ++mBufferGeneration;
242
Andreas Huber84066782011-08-16 09:34:26 -0700243 AString mime;
244 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800245
Chong Zhang7137ec72014-11-12 16:41:05 -0800246 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
247 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
248
Lajos Molnar1cd13982014-01-17 15:12:51 -0800249 mComponentName = mime;
250 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700251 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800252
Ronghua Wu68845c12015-07-21 09:50:48 -0700253 mCodec = MediaCodec::CreateByType(
254 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid);
Lajos Molnar09524832014-07-17 14:29:51 -0700255 int32_t secure = 0;
256 if (format->findInt32("secure", &secure) && secure != 0) {
257 if (mCodec != NULL) {
258 mCodec->getName(&mComponentName);
259 mComponentName.append(".secure");
260 mCodec->release();
261 ALOGI("[%s] creating", mComponentName.c_str());
262 mCodec = MediaCodec::CreateByComponentName(
Ronghua Wu68845c12015-07-21 09:50:48 -0700263 mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid);
Lajos Molnar09524832014-07-17 14:29:51 -0700264 }
265 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800266 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700267 ALOGE("Failed to create %s%s decoder",
268 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800269 handleError(UNKNOWN_ERROR);
270 return;
271 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800272 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800273
274 mCodec->getName(&mComponentName);
275
Lajos Molnar14986f62014-09-15 11:04:44 -0700276 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700277 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800278 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700279 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700280 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700281 // We treat this as a warning, as this is a preparatory step.
282 // Codec will try to connect to the surface, which is where
283 // any error signaling will occur.
284 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800285 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700286 err = mCodec->configure(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700287 format, mSurface, NULL /* crypto */, 0 /* flags */);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800288 if (err != OK) {
289 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700290 mCodec->release();
291 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800292 handleError(err);
293 return;
294 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700295 rememberCodecSpecificData(format);
296
Lajos Molnar1cd13982014-01-17 15:12:51 -0800297 // the following should work in configured state
298 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
299 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
300
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700301 mStats->setString("mime", mime.c_str());
302 mStats->setString("component-name", mComponentName.c_str());
303
304 if (!mIsAudio) {
305 int32_t width, height;
306 if (mOutputFormat->findInt32("width", &width)
307 && mOutputFormat->findInt32("height", &height)) {
308 mStats->setInt32("width", width);
309 mStats->setInt32("height", height);
310 }
311 }
312
Marco Nelissen421f47c2015-03-25 14:40:32 -0700313 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
314 mCodec->setCallback(reply);
315
Lajos Molnar1cd13982014-01-17 15:12:51 -0800316 err = mCodec->start();
317 if (err != OK) {
318 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700319 mCodec->release();
320 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800321 handleError(err);
322 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800323 }
324
Lajos Molnar09524832014-07-17 14:29:51 -0700325 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700326
Wei Jia704e7262014-06-04 16:21:56 -0700327 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800328 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800329}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700330
Ronghua Wu8db88132015-04-22 13:51:35 -0700331void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
332 if (mCodec == NULL) {
333 ALOGW("onSetParameters called before codec is created.");
334 return;
335 }
336 mCodec->setParameters(params);
337}
338
Chong Zhang7137ec72014-11-12 16:41:05 -0800339void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
340 bool hadNoRenderer = (mRenderer == NULL);
341 mRenderer = renderer;
342 if (hadNoRenderer && mRenderer != NULL) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700343 // this means that the widevine legacy source is ready
344 onRequestInputBuffers();
Chong Zhang7137ec72014-11-12 16:41:05 -0800345 }
346}
347
348void NuPlayer::Decoder::onGetInputBuffers(
349 Vector<sp<ABuffer> > *dstBuffers) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700350 CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers));
Chong Zhang7137ec72014-11-12 16:41:05 -0800351}
352
Chong Zhangf8d71772014-11-26 15:08:34 -0800353void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800354 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800355
356 if (notifyComplete) {
357 mResumePending = true;
358 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700359 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800360}
361
Chong Zhang66704af2015-03-03 19:32:35 -0800362void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800363 if (mCCDecoder != NULL) {
364 mCCDecoder->flush();
365 }
366
367 if (mRenderer != NULL) {
368 mRenderer->flush(mIsAudio, notifyComplete);
369 mRenderer->signalTimeDiscontinuity();
370 }
371
372 status_t err = OK;
373 if (mCodec != NULL) {
374 err = mCodec->flush();
375 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
376 ++mBufferGeneration;
377 }
378
379 if (err != OK) {
380 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
381 handleError(err);
382 // finish with posting kWhatFlushCompleted.
383 // we attempt to release the buffers even if flush fails.
384 }
385 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700386 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800387}
Chong Zhang7137ec72014-11-12 16:41:05 -0800388
Marco Nelissen421f47c2015-03-25 14:40:32 -0700389
Chong Zhang66704af2015-03-03 19:32:35 -0800390void NuPlayer::Decoder::onFlush() {
391 doFlush(true);
392
393 if (isDiscontinuityPending()) {
394 // This could happen if the client starts seeking/shutdown
395 // after we queued an EOS for discontinuities.
396 // We can consider discontinuity handled.
397 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800398 }
Chong Zhang66704af2015-03-03 19:32:35 -0800399
400 sp<AMessage> notify = mNotify->dup();
401 notify->setInt32("what", kWhatFlushCompleted);
402 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800403}
404
405void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
406 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800407
408 // if there is a pending resume request, notify complete now
409 notifyResumeCompleteIfNecessary();
410
Chong Zhang7137ec72014-11-12 16:41:05 -0800411 if (mCodec != NULL) {
412 err = mCodec->release();
413 mCodec = NULL;
414 ++mBufferGeneration;
415
Lajos Molnar1de1e252015-04-30 18:18:34 -0700416 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800417 // reconnect to surface as MediaCodec disconnected from it
418 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700419 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800420 ALOGW_IF(error != NO_ERROR,
421 "[%s] failed to connect to native window, error=%d",
422 mComponentName.c_str(), error);
423 }
424 mComponentName = "decoder";
425 }
426
427 releaseAndResetMediaBuffers();
428
429 if (err != OK) {
430 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
431 handleError(err);
432 // finish with posting kWhatShutdownCompleted.
433 }
434
435 if (notifyComplete) {
436 sp<AMessage> notify = mNotify->dup();
437 notify->setInt32("what", kWhatShutdownCompleted);
438 notify->post();
439 mPaused = true;
440 }
441}
442
Chong Zhang3b032b32015-04-17 15:49:06 -0700443/*
444 * returns true if we should request more data
445 */
446bool NuPlayer::Decoder::doRequestBuffers() {
Lajos Molnare6109e22015-04-10 17:18:22 -0700447 // mRenderer is only NULL if we have a legacy widevine source that
448 // is not yet ready. In this case we must not fetch input.
449 if (isDiscontinuityPending() || mRenderer == NULL) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700450 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800451 }
452 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800453 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800454 size_t bufferIx = *mDequeuedInputBuffers.begin();
455 sp<AMessage> msg = new AMessage();
456 msg->setSize("buffer-ix", bufferIx);
457 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800458 if (err != OK && err != ERROR_END_OF_STREAM) {
459 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800460 break;
461 }
462 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
463
464 if (!mPendingInputMessages.empty()
465 || !onInputBufferFetched(msg)) {
466 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700467 }
468 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800469
Chong Zhang3b032b32015-04-17 15:49:06 -0700470 return err == -EWOULDBLOCK
471 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700472}
473
Marco Nelissen421f47c2015-03-25 14:40:32 -0700474void NuPlayer::Decoder::handleError(int32_t err)
475{
476 // We cannot immediately release the codec due to buffers still outstanding
477 // in the renderer. We signal to the player the error so it can shutdown/release the
478 // decoder after flushing and increment the generation to discard unnecessary messages.
479
480 ++mBufferGeneration;
481
482 sp<AMessage> notify = mNotify->dup();
483 notify->setInt32("what", kWhatError);
484 notify->setInt32("err", err);
485 notify->post();
486}
487
488bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800489 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800490 return false;
491 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700492
493 sp<ABuffer> buffer;
494 mCodec->getInputBuffer(index, &buffer);
495
Wei Jia6301a5e2015-05-13 13:15:18 -0700496 if (buffer == NULL) {
497 handleError(UNKNOWN_ERROR);
498 return false;
499 }
500
Marco Nelissen421f47c2015-03-25 14:40:32 -0700501 if (index >= mInputBuffers.size()) {
502 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
503 mInputBuffers.add();
504 mMediaBuffers.add();
505 mInputBufferIsDequeued.add();
506 mMediaBuffers.editItemAt(i) = NULL;
507 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800508 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700509 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700510 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700511
Marco Nelissen421f47c2015-03-25 14:40:32 -0700512 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800513
Marco Nelissen421f47c2015-03-25 14:40:32 -0700514 if (mMediaBuffers[index] != NULL) {
515 mMediaBuffers[index]->release();
516 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700517 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700518 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700519
Lajos Molnar87603c02014-08-20 19:25:30 -0700520 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800521 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700522 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800523
Lajos Molnar87603c02014-08-20 19:25:30 -0700524 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
525 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800526 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700527 mCSDsToSubmit.removeAt(0);
Chong Zhang7137ec72014-11-12 16:41:05 -0800528 CHECK(onInputBufferFetched(msg));
Wei Jia2245fc62014-10-02 15:12:25 -0700529 return true;
530 }
531
532 while (!mPendingInputMessages.empty()) {
533 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800534 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700535 break;
536 }
537 mPendingInputMessages.erase(mPendingInputMessages.begin());
538 }
539
Marco Nelissen421f47c2015-03-25 14:40:32 -0700540 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700541 return true;
542 }
543
Marco Nelissen421f47c2015-03-25 14:40:32 -0700544 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800545
546 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800547 return true;
548}
549
Marco Nelissen421f47c2015-03-25 14:40:32 -0700550bool NuPlayer::Decoder::handleAnOutputBuffer(
551 size_t index,
552 size_t offset,
553 size_t size,
554 int64_t timeUs,
555 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700556// CHECK_LT(bufferIx, mOutputBuffers.size());
557 sp<ABuffer> buffer;
558 mCodec->getOutputBuffer(index, &buffer);
559
560 if (index >= mOutputBuffers.size()) {
561 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
562 mOutputBuffers.add();
563 }
564 }
565
566 mOutputBuffers.editItemAt(index) = buffer;
567
Chong Zhang7137ec72014-11-12 16:41:05 -0800568 buffer->setRange(offset, size);
569 buffer->meta()->clear();
570 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800571
572 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800573 // we do not expect CODECCONFIG or SYNCFRAME for decoder
574
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800575 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700576 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800577 reply->setInt32("generation", mBufferGeneration);
578
Chong Zhang66704af2015-03-03 19:32:35 -0800579 if (eos) {
580 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
581
582 buffer->meta()->setInt32("eos", true);
583 reply->setInt32("eos", true);
584 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800585 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
586 ALOGV("[%s] dropping buffer at time %lld as requested.",
587 mComponentName.c_str(), (long long)timeUs);
588
589 reply->post();
590 return true;
591 }
592
593 mSkipRenderingUntilMediaTimeUs = -1;
594 }
595
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700596 mNumFramesTotal += !mIsAudio;
597
Chong Zhangf8d71772014-11-26 15:08:34 -0800598 // wait until 1st frame comes out to signal resume complete
599 notifyResumeCompleteIfNecessary();
600
Chong Zhang7137ec72014-11-12 16:41:05 -0800601 if (mRenderer != NULL) {
602 // send the buffer to renderer.
603 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800604 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800605 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
606 }
607 }
608
609 return true;
610}
611
Marco Nelissen421f47c2015-03-25 14:40:32 -0700612void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
613 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700614 int32_t width, height;
615 if (format->findInt32("width", &width)
616 && format->findInt32("height", &height)) {
617 mStats->setInt32("width", width);
618 mStats->setInt32("height", height);
619 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700620 sp<AMessage> notify = mNotify->dup();
621 notify->setInt32("what", kWhatVideoSizeChanged);
622 notify->setMessage("format", format);
623 notify->post();
624 } else if (mRenderer != NULL) {
625 uint32_t flags;
626 int64_t durationUs;
627 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700628 if (getAudioDeepBufferSetting() // override regardless of source duration
629 || (!hasVideo
630 && mSource->getDuration(&durationUs) == OK
631 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700632 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
633 } else {
634 flags = AUDIO_OUTPUT_FLAG_NONE;
635 }
636
Eric Laurent216f0172015-08-20 18:40:24 -0700637 status_t err = mRenderer->openAudioSink(
Marco Nelissen421f47c2015-03-25 14:40:32 -0700638 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
Eric Laurent216f0172015-08-20 18:40:24 -0700639 if (err != OK) {
640 handleError(err);
641 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700642 }
643}
644
Chong Zhang7137ec72014-11-12 16:41:05 -0800645void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
646 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
647 if (mMediaBuffers[i] != NULL) {
648 mMediaBuffers[i]->release();
649 mMediaBuffers.editItemAt(i) = NULL;
650 }
651 }
652 mMediaBuffers.resize(mInputBuffers.size());
653 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
654 mMediaBuffers.editItemAt(i) = NULL;
655 }
656 mInputBufferIsDequeued.clear();
657 mInputBufferIsDequeued.resize(mInputBuffers.size());
658 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
659 mInputBufferIsDequeued.editItemAt(i) = false;
660 }
661
662 mPendingInputMessages.clear();
663 mDequeuedInputBuffers.clear();
664 mSkipRenderingUntilMediaTimeUs = -1;
665}
666
667void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800668 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800669 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800670 reply->setInt32("generation", mBufferGeneration);
671 mCodec->requestActivityNotification(reply);
672 }
673}
674
675bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
676 int32_t generation;
677 CHECK(msg->findInt32("generation", &generation));
678 return generation != mBufferGeneration;
679}
680
681status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
682 sp<ABuffer> accessUnit;
683 bool dropAccessUnit;
684 do {
685 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
686
687 if (err == -EWOULDBLOCK) {
688 return err;
689 } else if (err != OK) {
690 if (err == INFO_DISCONTINUITY) {
691 int32_t type;
692 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
693
694 bool formatChange =
695 (mIsAudio &&
696 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
697 || (!mIsAudio &&
698 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
699
700 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
701
702 ALOGI("%s discontinuity (format=%d, time=%d)",
703 mIsAudio ? "audio" : "video", formatChange, timeChange);
704
705 bool seamlessFormatChange = false;
706 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
707 if (formatChange) {
708 seamlessFormatChange =
709 supportsSeamlessFormatChange(newFormat);
710 // treat seamless format change separately
711 formatChange = !seamlessFormatChange;
712 }
713
Chong Zhang66704af2015-03-03 19:32:35 -0800714 // For format or time change, return EOS to queue EOS input,
715 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800716 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800717 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800718 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800719 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800720 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800721 mTimeChangePending = true;
722 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800723 } else if (seamlessFormatChange) {
724 // reuse existing decoder and don't flush
725 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800726 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800727 } else {
728 // This stream is unaffected by the discontinuity
729 return -EWOULDBLOCK;
730 }
731 }
732
Chong Zhang66704af2015-03-03 19:32:35 -0800733 // reply should only be returned without a buffer set
734 // when there is an error (including EOS)
735 CHECK(err != OK);
736
Chong Zhang7137ec72014-11-12 16:41:05 -0800737 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800738 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800739 }
740
Chong Zhang7137ec72014-11-12 16:41:05 -0800741 dropAccessUnit = false;
742 if (!mIsAudio
743 && !mIsSecure
744 && mRenderer->getVideoLateByUs() > 100000ll
745 && mIsVideoAVC
746 && !IsAVCReferenceFrame(accessUnit)) {
747 dropAccessUnit = true;
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700748 ++mNumInputFramesDropped;
Chong Zhang7137ec72014-11-12 16:41:05 -0800749 }
750 } while (dropAccessUnit);
751
752 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
753#if 0
754 int64_t mediaTimeUs;
755 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700756 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800757 mIsAudio ? "audio" : "video",
758 mediaTimeUs / 1E6);
759#endif
760
761 if (mCCDecoder != NULL) {
762 mCCDecoder->decode(accessUnit);
763 }
764
765 reply->setBuffer("buffer", accessUnit);
766
767 return OK;
768}
769
770bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800771 size_t bufferIx;
772 CHECK(msg->findSize("buffer-ix", &bufferIx));
773 CHECK_LT(bufferIx, mInputBuffers.size());
774 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
775
776 sp<ABuffer> buffer;
777 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700778
779 // handle widevine classic source - that fills an arbitrary input buffer
780 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700781 if (hasBuffer) {
782 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
783 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700784 // likely filled another buffer than we requested: adjust buffer index
785 size_t ix;
786 for (ix = 0; ix < mInputBuffers.size(); ix++) {
787 const sp<ABuffer> &buf = mInputBuffers[ix];
788 if (buf->data() == mediaBuffer->data()) {
789 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700790 if (!mInputBufferIsDequeued[ix]) {
791 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
792 mComponentName.c_str(), ix, bufferIx);
793 mediaBuffer->release();
794 return false;
795 }
Lajos Molnar09524832014-07-17 14:29:51 -0700796
797 // TRICKY: need buffer for the metadata, so instead, set
798 // codecBuffer to the same (though incorrect) buffer to
799 // avoid a memcpy into the codecBuffer
800 codecBuffer = buffer;
801 codecBuffer->setRange(
802 mediaBuffer->range_offset(),
803 mediaBuffer->range_length());
804 bufferIx = ix;
805 break;
806 }
807 }
808 CHECK(ix < mInputBuffers.size());
809 }
810 }
811
Lajos Molnar1cd13982014-01-17 15:12:51 -0800812 if (buffer == NULL /* includes !hasBuffer */) {
813 int32_t streamErr = ERROR_END_OF_STREAM;
814 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
815
Chong Zhang66704af2015-03-03 19:32:35 -0800816 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800817
818 // attempt to queue EOS
819 status_t err = mCodec->queueInputBuffer(
820 bufferIx,
821 0,
822 0,
823 0,
824 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700825 if (err == OK) {
826 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
827 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800828 streamErr = err;
829 // err will not be ERROR_END_OF_STREAM
830 }
831
832 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700833 ALOGE("Stream error for %s (err=%d), EOS %s queued",
834 mComponentName.c_str(),
835 streamErr,
836 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800837 handleError(streamErr);
838 }
839 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800840 sp<AMessage> extra;
841 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
842 int64_t resumeAtMediaTimeUs;
843 if (extra->findInt64(
844 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
845 ALOGI("[%s] suppressing rendering until %lld us",
846 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
847 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
848 }
849 }
850
Lajos Molnar1cd13982014-01-17 15:12:51 -0800851 int64_t timeUs = 0;
852 uint32_t flags = 0;
853 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
854
Lajos Molnar87603c02014-08-20 19:25:30 -0700855 int32_t eos, csd;
856 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800857 if (buffer->meta()->findInt32("eos", &eos) && eos) {
858 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700859 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
860 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800861 }
862
863 // copy into codec buffer
864 if (buffer != codecBuffer) {
865 CHECK_LE(buffer->size(), codecBuffer->capacity());
866 codecBuffer->setRange(0, buffer->size());
867 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
868 }
869
870 status_t err = mCodec->queueInputBuffer(
871 bufferIx,
872 codecBuffer->offset(),
873 codecBuffer->size(),
874 timeUs,
875 flags);
876 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700877 if (mediaBuffer != NULL) {
878 mediaBuffer->release();
879 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800880 ALOGE("Failed to queue input buffer for %s (err=%d)",
881 mComponentName.c_str(), err);
882 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700883 } else {
884 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
885 if (mediaBuffer != NULL) {
886 CHECK(mMediaBuffers[bufferIx] == NULL);
887 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
888 }
Lajos Molnar09524832014-07-17 14:29:51 -0700889 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800890 }
Wei Jia2245fc62014-10-02 15:12:25 -0700891 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800892}
893
Lajos Molnar1cd13982014-01-17 15:12:51 -0800894void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
895 status_t err;
896 int32_t render;
897 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800898 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800899 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800900
Chong Zhang7137ec72014-11-12 16:41:05 -0800901 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800902 int64_t timeUs;
903 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
904 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800905
906 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
907 mCCDecoder->display(timeUs);
908 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800909 }
910
Lajos Molnar1cd13982014-01-17 15:12:51 -0800911 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700912 int64_t timestampNs;
913 CHECK(msg->findInt64("timestampNs", &timestampNs));
914 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800915 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700916 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800917 err = mCodec->releaseOutputBuffer(bufferIx);
918 }
919 if (err != OK) {
920 ALOGE("failed to release output buffer for %s (err=%d)",
921 mComponentName.c_str(), err);
922 handleError(err);
923 }
Chong Zhang66704af2015-03-03 19:32:35 -0800924 if (msg->findInt32("eos", &eos) && eos
925 && isDiscontinuityPending()) {
926 finishHandleDiscontinuity(true /* flushOnTimeChange */);
927 }
928}
929
930bool NuPlayer::Decoder::isDiscontinuityPending() const {
931 return mFormatChangePending || mTimeChangePending;
932}
933
934void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
935 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
936 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
937
938 // If we have format change, pause and wait to be killed;
939 // If we have time change only, flush and restart fetching.
940
941 if (mFormatChangePending) {
942 mPaused = true;
943 } else if (mTimeChangePending) {
944 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700945 doFlush(false /* notifyComplete */);
946 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -0800947 }
Chong Zhang66704af2015-03-03 19:32:35 -0800948 }
949
950 // Notify NuPlayer to either shutdown decoder, or rescan sources
951 sp<AMessage> msg = mNotify->dup();
952 msg->setInt32("what", kWhatInputDiscontinuity);
953 msg->setInt32("formatChange", mFormatChangePending);
954 msg->post();
955
956 mFormatChangePending = false;
957 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800958}
959
Chong Zhang7137ec72014-11-12 16:41:05 -0800960bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
961 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800962 if (targetFormat == NULL) {
963 return true;
964 }
965
966 AString mime;
967 if (!targetFormat->findString("mime", &mime)) {
968 return false;
969 }
970
971 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
972 // field-by-field comparison
973 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
974 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
975 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100976 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800977 !targetFormat->findInt32(keys[i], &newVal) ||
978 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800979 return false;
980 }
981 }
982
983 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100984 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800985 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800986 if (oldBuf->size() != newBuf->size()) {
987 return false;
988 }
989 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
990 }
991 }
992 return false;
993}
994
995bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +0100996 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800997 return false;
998 }
999
1000 if (targetFormat == NULL) {
1001 return true;
1002 }
1003
1004 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001005 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001006 || !targetFormat->findString("mime", &newMime)
1007 || !(oldMime == newMime)) {
1008 return false;
1009 }
1010
1011 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1012 bool seamless;
1013 if (audio) {
1014 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1015 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001016 int32_t isAdaptive;
1017 seamless = (mCodec != NULL &&
1018 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1019 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001020 }
1021
1022 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1023 return seamless;
1024}
1025
Chong Zhang7137ec72014-11-12 16:41:05 -08001026void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1027 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001028 return;
1029 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001030 mCSDsForCurrentFormat.clear();
1031 for (int32_t i = 0; ; ++i) {
1032 AString tag = "csd-";
1033 tag.append(i);
1034 sp<ABuffer> buffer;
1035 if (!format->findBuffer(tag.c_str(), &buffer)) {
1036 break;
1037 }
1038 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001039 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001040}
1041
Chong Zhangf8d71772014-11-26 15:08:34 -08001042void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1043 if (mResumePending) {
1044 mResumePending = false;
1045
1046 sp<AMessage> notify = mNotify->dup();
1047 notify->setInt32("what", kWhatResumeCompleted);
1048 notify->post();
1049 }
1050}
1051
Andreas Huberf9334412010-12-15 15:17:42 -08001052} // namespace android
1053