blob: b6a2360b18fc83bc105bf82e0f04f54c8dc60006 [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);
Wei Jia56097a82016-01-07 16:03:03 -0800528 if (!onInputBufferFetched(msg)) {
529 handleError(UNKNOWN_ERROR);
530 return false;
531 }
Wei Jia2245fc62014-10-02 15:12:25 -0700532 return true;
533 }
534
535 while (!mPendingInputMessages.empty()) {
536 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800537 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700538 break;
539 }
540 mPendingInputMessages.erase(mPendingInputMessages.begin());
541 }
542
Marco Nelissen421f47c2015-03-25 14:40:32 -0700543 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700544 return true;
545 }
546
Marco Nelissen421f47c2015-03-25 14:40:32 -0700547 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800548
549 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800550 return true;
551}
552
Marco Nelissen421f47c2015-03-25 14:40:32 -0700553bool NuPlayer::Decoder::handleAnOutputBuffer(
554 size_t index,
555 size_t offset,
556 size_t size,
557 int64_t timeUs,
558 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700559// CHECK_LT(bufferIx, mOutputBuffers.size());
560 sp<ABuffer> buffer;
561 mCodec->getOutputBuffer(index, &buffer);
562
Santhosh Behara065e2292015-10-09 17:32:58 -0700563 if (buffer == NULL) {
564 handleError(UNKNOWN_ERROR);
565 return false;
566 }
567
Marco Nelissen421f47c2015-03-25 14:40:32 -0700568 if (index >= mOutputBuffers.size()) {
569 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
570 mOutputBuffers.add();
571 }
572 }
573
574 mOutputBuffers.editItemAt(index) = buffer;
575
Chong Zhang7137ec72014-11-12 16:41:05 -0800576 buffer->setRange(offset, size);
577 buffer->meta()->clear();
578 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800579
580 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800581 // we do not expect CODECCONFIG or SYNCFRAME for decoder
582
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800583 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700584 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800585 reply->setInt32("generation", mBufferGeneration);
586
Chong Zhang66704af2015-03-03 19:32:35 -0800587 if (eos) {
588 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
589
590 buffer->meta()->setInt32("eos", true);
591 reply->setInt32("eos", true);
592 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800593 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
594 ALOGV("[%s] dropping buffer at time %lld as requested.",
595 mComponentName.c_str(), (long long)timeUs);
596
597 reply->post();
598 return true;
599 }
600
601 mSkipRenderingUntilMediaTimeUs = -1;
602 }
603
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700604 mNumFramesTotal += !mIsAudio;
605
Chong Zhangf8d71772014-11-26 15:08:34 -0800606 // wait until 1st frame comes out to signal resume complete
607 notifyResumeCompleteIfNecessary();
608
Chong Zhang7137ec72014-11-12 16:41:05 -0800609 if (mRenderer != NULL) {
610 // send the buffer to renderer.
611 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800612 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800613 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
614 }
615 }
616
617 return true;
618}
619
Marco Nelissen421f47c2015-03-25 14:40:32 -0700620void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
621 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700622 int32_t width, height;
623 if (format->findInt32("width", &width)
624 && format->findInt32("height", &height)) {
625 mStats->setInt32("width", width);
626 mStats->setInt32("height", height);
627 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700628 sp<AMessage> notify = mNotify->dup();
629 notify->setInt32("what", kWhatVideoSizeChanged);
630 notify->setMessage("format", format);
631 notify->post();
632 } else if (mRenderer != NULL) {
633 uint32_t flags;
634 int64_t durationUs;
635 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700636 if (getAudioDeepBufferSetting() // override regardless of source duration
637 || (!hasVideo
638 && mSource->getDuration(&durationUs) == OK
639 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700640 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
641 } else {
642 flags = AUDIO_OUTPUT_FLAG_NONE;
643 }
644
Eric Laurent216f0172015-08-20 18:40:24 -0700645 status_t err = mRenderer->openAudioSink(
Marco Nelissen421f47c2015-03-25 14:40:32 -0700646 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
Eric Laurent216f0172015-08-20 18:40:24 -0700647 if (err != OK) {
648 handleError(err);
649 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700650 }
651}
652
Chong Zhang7137ec72014-11-12 16:41:05 -0800653void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
654 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
655 if (mMediaBuffers[i] != NULL) {
656 mMediaBuffers[i]->release();
657 mMediaBuffers.editItemAt(i) = NULL;
658 }
659 }
660 mMediaBuffers.resize(mInputBuffers.size());
661 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
662 mMediaBuffers.editItemAt(i) = NULL;
663 }
664 mInputBufferIsDequeued.clear();
665 mInputBufferIsDequeued.resize(mInputBuffers.size());
666 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
667 mInputBufferIsDequeued.editItemAt(i) = false;
668 }
669
670 mPendingInputMessages.clear();
671 mDequeuedInputBuffers.clear();
672 mSkipRenderingUntilMediaTimeUs = -1;
673}
674
675void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800676 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800677 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800678 reply->setInt32("generation", mBufferGeneration);
679 mCodec->requestActivityNotification(reply);
680 }
681}
682
683bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
684 int32_t generation;
685 CHECK(msg->findInt32("generation", &generation));
686 return generation != mBufferGeneration;
687}
688
689status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
690 sp<ABuffer> accessUnit;
691 bool dropAccessUnit;
692 do {
693 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
694
695 if (err == -EWOULDBLOCK) {
696 return err;
697 } else if (err != OK) {
698 if (err == INFO_DISCONTINUITY) {
699 int32_t type;
700 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
701
702 bool formatChange =
703 (mIsAudio &&
704 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
705 || (!mIsAudio &&
706 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
707
708 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
709
710 ALOGI("%s discontinuity (format=%d, time=%d)",
711 mIsAudio ? "audio" : "video", formatChange, timeChange);
712
713 bool seamlessFormatChange = false;
714 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
715 if (formatChange) {
716 seamlessFormatChange =
717 supportsSeamlessFormatChange(newFormat);
718 // treat seamless format change separately
719 formatChange = !seamlessFormatChange;
720 }
721
Chong Zhang66704af2015-03-03 19:32:35 -0800722 // For format or time change, return EOS to queue EOS input,
723 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800724 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800725 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800726 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800727 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800728 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800729 mTimeChangePending = true;
730 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800731 } else if (seamlessFormatChange) {
732 // reuse existing decoder and don't flush
733 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800734 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800735 } else {
736 // This stream is unaffected by the discontinuity
737 return -EWOULDBLOCK;
738 }
739 }
740
Chong Zhang66704af2015-03-03 19:32:35 -0800741 // reply should only be returned without a buffer set
742 // when there is an error (including EOS)
743 CHECK(err != OK);
744
Chong Zhang7137ec72014-11-12 16:41:05 -0800745 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800746 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800747 }
748
Chong Zhang7137ec72014-11-12 16:41:05 -0800749 dropAccessUnit = false;
750 if (!mIsAudio
751 && !mIsSecure
752 && mRenderer->getVideoLateByUs() > 100000ll
753 && mIsVideoAVC
754 && !IsAVCReferenceFrame(accessUnit)) {
755 dropAccessUnit = true;
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700756 ++mNumInputFramesDropped;
Chong Zhang7137ec72014-11-12 16:41:05 -0800757 }
758 } while (dropAccessUnit);
759
760 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
761#if 0
762 int64_t mediaTimeUs;
763 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700764 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800765 mIsAudio ? "audio" : "video",
766 mediaTimeUs / 1E6);
767#endif
768
769 if (mCCDecoder != NULL) {
770 mCCDecoder->decode(accessUnit);
771 }
772
773 reply->setBuffer("buffer", accessUnit);
774
775 return OK;
776}
777
778bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800779 size_t bufferIx;
780 CHECK(msg->findSize("buffer-ix", &bufferIx));
781 CHECK_LT(bufferIx, mInputBuffers.size());
782 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
783
784 sp<ABuffer> buffer;
785 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700786
787 // handle widevine classic source - that fills an arbitrary input buffer
788 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700789 if (hasBuffer) {
790 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
791 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700792 // likely filled another buffer than we requested: adjust buffer index
793 size_t ix;
794 for (ix = 0; ix < mInputBuffers.size(); ix++) {
795 const sp<ABuffer> &buf = mInputBuffers[ix];
796 if (buf->data() == mediaBuffer->data()) {
797 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700798 if (!mInputBufferIsDequeued[ix]) {
799 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
800 mComponentName.c_str(), ix, bufferIx);
801 mediaBuffer->release();
802 return false;
803 }
Lajos Molnar09524832014-07-17 14:29:51 -0700804
805 // TRICKY: need buffer for the metadata, so instead, set
806 // codecBuffer to the same (though incorrect) buffer to
807 // avoid a memcpy into the codecBuffer
808 codecBuffer = buffer;
809 codecBuffer->setRange(
810 mediaBuffer->range_offset(),
811 mediaBuffer->range_length());
812 bufferIx = ix;
813 break;
814 }
815 }
816 CHECK(ix < mInputBuffers.size());
817 }
818 }
819
Lajos Molnar1cd13982014-01-17 15:12:51 -0800820 if (buffer == NULL /* includes !hasBuffer */) {
821 int32_t streamErr = ERROR_END_OF_STREAM;
822 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
823
Chong Zhang66704af2015-03-03 19:32:35 -0800824 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800825
826 // attempt to queue EOS
827 status_t err = mCodec->queueInputBuffer(
828 bufferIx,
829 0,
830 0,
831 0,
832 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700833 if (err == OK) {
834 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
835 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800836 streamErr = err;
837 // err will not be ERROR_END_OF_STREAM
838 }
839
840 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700841 ALOGE("Stream error for %s (err=%d), EOS %s queued",
842 mComponentName.c_str(),
843 streamErr,
844 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800845 handleError(streamErr);
846 }
847 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800848 sp<AMessage> extra;
849 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
850 int64_t resumeAtMediaTimeUs;
851 if (extra->findInt64(
852 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
853 ALOGI("[%s] suppressing rendering until %lld us",
854 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
855 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
856 }
857 }
858
Lajos Molnar1cd13982014-01-17 15:12:51 -0800859 int64_t timeUs = 0;
860 uint32_t flags = 0;
861 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
862
Lajos Molnar87603c02014-08-20 19:25:30 -0700863 int32_t eos, csd;
864 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800865 if (buffer->meta()->findInt32("eos", &eos) && eos) {
866 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700867 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
868 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800869 }
870
871 // copy into codec buffer
872 if (buffer != codecBuffer) {
Wei Jia56097a82016-01-07 16:03:03 -0800873 if (buffer->size() > codecBuffer->capacity()) {
874 handleError(ERROR_BUFFER_TOO_SMALL);
875 mDequeuedInputBuffers.push_back(bufferIx);
876 return false;
877 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800878 codecBuffer->setRange(0, buffer->size());
879 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
880 }
881
882 status_t err = mCodec->queueInputBuffer(
883 bufferIx,
884 codecBuffer->offset(),
885 codecBuffer->size(),
886 timeUs,
887 flags);
888 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700889 if (mediaBuffer != NULL) {
890 mediaBuffer->release();
891 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800892 ALOGE("Failed to queue input buffer for %s (err=%d)",
893 mComponentName.c_str(), err);
894 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700895 } else {
896 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
897 if (mediaBuffer != NULL) {
898 CHECK(mMediaBuffers[bufferIx] == NULL);
899 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
900 }
Lajos Molnar09524832014-07-17 14:29:51 -0700901 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800902 }
Wei Jia2245fc62014-10-02 15:12:25 -0700903 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800904}
905
Lajos Molnar1cd13982014-01-17 15:12:51 -0800906void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
907 status_t err;
908 int32_t render;
909 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800910 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800911 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800912
Chong Zhang7137ec72014-11-12 16:41:05 -0800913 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800914 int64_t timeUs;
915 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
916 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800917
918 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
919 mCCDecoder->display(timeUs);
920 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800921 }
922
Lajos Molnar1cd13982014-01-17 15:12:51 -0800923 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700924 int64_t timestampNs;
925 CHECK(msg->findInt64("timestampNs", &timestampNs));
926 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800927 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700928 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800929 err = mCodec->releaseOutputBuffer(bufferIx);
930 }
931 if (err != OK) {
932 ALOGE("failed to release output buffer for %s (err=%d)",
933 mComponentName.c_str(), err);
934 handleError(err);
935 }
Chong Zhang66704af2015-03-03 19:32:35 -0800936 if (msg->findInt32("eos", &eos) && eos
937 && isDiscontinuityPending()) {
938 finishHandleDiscontinuity(true /* flushOnTimeChange */);
939 }
940}
941
942bool NuPlayer::Decoder::isDiscontinuityPending() const {
943 return mFormatChangePending || mTimeChangePending;
944}
945
946void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
947 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
948 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
949
950 // If we have format change, pause and wait to be killed;
951 // If we have time change only, flush and restart fetching.
952
953 if (mFormatChangePending) {
954 mPaused = true;
955 } else if (mTimeChangePending) {
956 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700957 doFlush(false /* notifyComplete */);
958 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -0800959 }
Chong Zhang66704af2015-03-03 19:32:35 -0800960 }
961
962 // Notify NuPlayer to either shutdown decoder, or rescan sources
963 sp<AMessage> msg = mNotify->dup();
964 msg->setInt32("what", kWhatInputDiscontinuity);
965 msg->setInt32("formatChange", mFormatChangePending);
966 msg->post();
967
968 mFormatChangePending = false;
969 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800970}
971
Chong Zhang7137ec72014-11-12 16:41:05 -0800972bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
973 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800974 if (targetFormat == NULL) {
975 return true;
976 }
977
978 AString mime;
979 if (!targetFormat->findString("mime", &mime)) {
980 return false;
981 }
982
983 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
984 // field-by-field comparison
985 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
986 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
987 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100988 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800989 !targetFormat->findInt32(keys[i], &newVal) ||
990 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800991 return false;
992 }
993 }
994
995 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100996 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800997 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800998 if (oldBuf->size() != newBuf->size()) {
999 return false;
1000 }
1001 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1002 }
1003 }
1004 return false;
1005}
1006
1007bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001008 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001009 return false;
1010 }
1011
1012 if (targetFormat == NULL) {
1013 return true;
1014 }
1015
1016 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001017 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001018 || !targetFormat->findString("mime", &newMime)
1019 || !(oldMime == newMime)) {
1020 return false;
1021 }
1022
1023 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1024 bool seamless;
1025 if (audio) {
1026 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1027 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001028 int32_t isAdaptive;
1029 seamless = (mCodec != NULL &&
1030 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1031 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001032 }
1033
1034 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1035 return seamless;
1036}
1037
Chong Zhang7137ec72014-11-12 16:41:05 -08001038void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1039 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001040 return;
1041 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001042 mCSDsForCurrentFormat.clear();
1043 for (int32_t i = 0; ; ++i) {
1044 AString tag = "csd-";
1045 tag.append(i);
1046 sp<ABuffer> buffer;
1047 if (!format->findBuffer(tag.c_str(), &buffer)) {
1048 break;
1049 }
1050 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001051 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001052}
1053
Chong Zhangf8d71772014-11-26 15:08:34 -08001054void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1055 if (mResumePending) {
1056 mResumePending = false;
1057
1058 sp<AMessage> notify = mNotify->dup();
1059 notify->setInt32("what", kWhatResumeCompleted);
1060 notify->post();
1061 }
1062}
1063
Andreas Huberf9334412010-12-15 15:17:42 -08001064} // namespace android
1065