blob: d20172a8b4a9de7f58580b4890bf98affcb7fd25 [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),
Wei Jia704e7262014-06-04 16:21:56 -070072 mPaused(true),
Chong Zhangf8d71772014-11-26 15:08:34 -080073 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080074 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080075 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070076 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080077 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080078}
79
80NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070081 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070082 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080083}
84
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070085sp<AMessage> NuPlayer::Decoder::getStats() const {
86 mStats->setInt64("frames-total", mNumFramesTotal);
87 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
88 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
89 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -070090}
91
Lajos Molnara81c6222015-07-10 19:17:45 -070092status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
93 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
94 return BAD_VALUE;
95 }
96
97 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
98
99 msg->setObject("surface", surface);
100 sp<AMessage> response;
101 status_t err = msg->postAndAwaitResponse(&response);
102 if (err == OK && response != NULL) {
103 CHECK(response->findInt32("err", &err));
104 }
105 return err;
106}
107
Chong Zhang7137ec72014-11-12 16:41:05 -0800108void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
109 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
110
111 switch (msg->what()) {
112 case kWhatCodecNotify:
113 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700114 int32_t cbID;
115 CHECK(msg->findInt32("callbackID", &cbID));
116
117 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
118 mIsAudio ? "audio" : "video", cbID, mPaused);
119
Marco Nelissen421f47c2015-03-25 14:40:32 -0700120 if (mPaused) {
121 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800122 }
123
Marco Nelissen421f47c2015-03-25 14:40:32 -0700124 switch (cbID) {
125 case MediaCodec::CB_INPUT_AVAILABLE:
126 {
127 int32_t index;
128 CHECK(msg->findInt32("index", &index));
129
130 handleAnInputBuffer(index);
131 break;
132 }
133
134 case MediaCodec::CB_OUTPUT_AVAILABLE:
135 {
136 int32_t index;
137 size_t offset;
138 size_t size;
139 int64_t timeUs;
140 int32_t flags;
141
142 CHECK(msg->findInt32("index", &index));
143 CHECK(msg->findSize("offset", &offset));
144 CHECK(msg->findSize("size", &size));
145 CHECK(msg->findInt64("timeUs", &timeUs));
146 CHECK(msg->findInt32("flags", &flags));
147
148 handleAnOutputBuffer(index, offset, size, timeUs, flags);
149 break;
150 }
151
152 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
153 {
154 sp<AMessage> format;
155 CHECK(msg->findMessage("format", &format));
156
157 handleOutputFormatChange(format);
158 break;
159 }
160
161 case MediaCodec::CB_ERROR:
162 {
163 status_t err;
164 CHECK(msg->findInt32("err", &err));
165 ALOGE("Decoder (%s) reported error : 0x%x",
166 mIsAudio ? "audio" : "video", err);
167
168 handleError(err);
169 break;
170 }
171
172 default:
173 {
174 TRESPASS();
175 break;
176 }
177 }
178
Lajos Molnar87603c02014-08-20 19:25:30 -0700179 break;
180 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800181
182 case kWhatRenderBuffer:
183 {
184 if (!isStaleReply(msg)) {
185 onRenderBuffer(msg);
186 }
187 break;
188 }
189
Lajos Molnara81c6222015-07-10 19:17:45 -0700190 case kWhatSetVideoSurface:
191 {
192 sp<AReplyToken> replyID;
193 CHECK(msg->senderAwaitsResponse(&replyID));
194
195 sp<RefBase> obj;
196 CHECK(msg->findObject("surface", &obj));
197 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
198 int32_t err = INVALID_OPERATION;
199 // NOTE: in practice mSurface is always non-null, but checking here for completeness
200 if (mCodec != NULL && mSurface != NULL) {
201 // TODO: once AwesomePlayer is removed, remove this automatic connecting
202 // to the surface by MediaPlayerService.
203 //
204 // at this point MediaPlayerService::client has already connected to the
205 // surface, which MediaCodec does not expect
206 err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
207 if (err == OK) {
208 err = mCodec->setSurface(surface);
209 ALOGI_IF(err, "codec setSurface returned: %d", err);
210 if (err == OK) {
211 // reconnect to the old surface as MPS::Client will expect to
212 // be able to disconnect from it.
213 (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
214 mSurface = surface;
215 }
216 }
217 if (err != OK) {
218 // reconnect to the new surface on error as MPS::Client will expect to
219 // be able to disconnect from it.
220 (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
221 }
222 }
223
224 sp<AMessage> response = new AMessage;
225 response->setInt32("err", err);
226 response->postReply(replyID);
227 break;
228 }
229
Chong Zhang7137ec72014-11-12 16:41:05 -0800230 default:
231 DecoderBase::onMessageReceived(msg);
232 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700233 }
234}
235
Lajos Molnar1cd13982014-01-17 15:12:51 -0800236void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800237 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800238
Chong Zhang7137ec72014-11-12 16:41:05 -0800239 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800240 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800241
Lajos Molnar1cd13982014-01-17 15:12:51 -0800242 ++mBufferGeneration;
243
Andreas Huber84066782011-08-16 09:34:26 -0700244 AString mime;
245 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800246
Chong Zhang7137ec72014-11-12 16:41:05 -0800247 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
248 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
249
Lajos Molnar1cd13982014-01-17 15:12:51 -0800250 mComponentName = mime;
251 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700252 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800253
Ronghua Wu68845c12015-07-21 09:50:48 -0700254 mCodec = MediaCodec::CreateByType(
255 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid);
Lajos Molnar09524832014-07-17 14:29:51 -0700256 int32_t secure = 0;
257 if (format->findInt32("secure", &secure) && secure != 0) {
258 if (mCodec != NULL) {
259 mCodec->getName(&mComponentName);
260 mComponentName.append(".secure");
261 mCodec->release();
262 ALOGI("[%s] creating", mComponentName.c_str());
263 mCodec = MediaCodec::CreateByComponentName(
Ronghua Wu68845c12015-07-21 09:50:48 -0700264 mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid);
Lajos Molnar09524832014-07-17 14:29:51 -0700265 }
266 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800267 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700268 ALOGE("Failed to create %s%s decoder",
269 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800270 handleError(UNKNOWN_ERROR);
271 return;
272 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800273 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800274
275 mCodec->getName(&mComponentName);
276
Lajos Molnar14986f62014-09-15 11:04:44 -0700277 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700278 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800279 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700280 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700281 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700282 // We treat this as a warning, as this is a preparatory step.
283 // Codec will try to connect to the surface, which is where
284 // any error signaling will occur.
285 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800286 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700287 err = mCodec->configure(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700288 format, mSurface, NULL /* crypto */, 0 /* flags */);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800289 if (err != OK) {
290 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700291 mCodec->release();
292 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800293 handleError(err);
294 return;
295 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700296 rememberCodecSpecificData(format);
297
Lajos Molnar1cd13982014-01-17 15:12:51 -0800298 // the following should work in configured state
299 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
300 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
301
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700302 mStats->setString("mime", mime.c_str());
303 mStats->setString("component-name", mComponentName.c_str());
304
305 if (!mIsAudio) {
306 int32_t width, height;
307 if (mOutputFormat->findInt32("width", &width)
308 && mOutputFormat->findInt32("height", &height)) {
309 mStats->setInt32("width", width);
310 mStats->setInt32("height", height);
311 }
312 }
313
Marco Nelissen421f47c2015-03-25 14:40:32 -0700314 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
315 mCodec->setCallback(reply);
316
Lajos Molnar1cd13982014-01-17 15:12:51 -0800317 err = mCodec->start();
318 if (err != OK) {
319 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700320 mCodec->release();
321 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800322 handleError(err);
323 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800324 }
325
Lajos Molnar09524832014-07-17 14:29:51 -0700326 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700327
Wei Jia704e7262014-06-04 16:21:56 -0700328 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800329 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800330}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700331
Ronghua Wu8db88132015-04-22 13:51:35 -0700332void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
333 if (mCodec == NULL) {
334 ALOGW("onSetParameters called before codec is created.");
335 return;
336 }
337 mCodec->setParameters(params);
338}
339
Chong Zhang7137ec72014-11-12 16:41:05 -0800340void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
341 bool hadNoRenderer = (mRenderer == NULL);
342 mRenderer = renderer;
343 if (hadNoRenderer && mRenderer != NULL) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700344 // this means that the widevine legacy source is ready
345 onRequestInputBuffers();
Chong Zhang7137ec72014-11-12 16:41:05 -0800346 }
347}
348
349void NuPlayer::Decoder::onGetInputBuffers(
350 Vector<sp<ABuffer> > *dstBuffers) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700351 CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers));
Chong Zhang7137ec72014-11-12 16:41:05 -0800352}
353
Chong Zhangf8d71772014-11-26 15:08:34 -0800354void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800355 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800356
357 if (notifyComplete) {
358 mResumePending = true;
359 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700360 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800361}
362
Chong Zhang66704af2015-03-03 19:32:35 -0800363void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800364 if (mCCDecoder != NULL) {
365 mCCDecoder->flush();
366 }
367
368 if (mRenderer != NULL) {
369 mRenderer->flush(mIsAudio, notifyComplete);
370 mRenderer->signalTimeDiscontinuity();
371 }
372
373 status_t err = OK;
374 if (mCodec != NULL) {
375 err = mCodec->flush();
376 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
377 ++mBufferGeneration;
378 }
379
380 if (err != OK) {
381 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
382 handleError(err);
383 // finish with posting kWhatFlushCompleted.
384 // we attempt to release the buffers even if flush fails.
385 }
386 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700387 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800388}
Chong Zhang7137ec72014-11-12 16:41:05 -0800389
Marco Nelissen421f47c2015-03-25 14:40:32 -0700390
Chong Zhang66704af2015-03-03 19:32:35 -0800391void NuPlayer::Decoder::onFlush() {
392 doFlush(true);
393
394 if (isDiscontinuityPending()) {
395 // This could happen if the client starts seeking/shutdown
396 // after we queued an EOS for discontinuities.
397 // We can consider discontinuity handled.
398 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800399 }
Chong Zhang66704af2015-03-03 19:32:35 -0800400
401 sp<AMessage> notify = mNotify->dup();
402 notify->setInt32("what", kWhatFlushCompleted);
403 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800404}
405
406void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
407 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800408
409 // if there is a pending resume request, notify complete now
410 notifyResumeCompleteIfNecessary();
411
Chong Zhang7137ec72014-11-12 16:41:05 -0800412 if (mCodec != NULL) {
413 err = mCodec->release();
414 mCodec = NULL;
415 ++mBufferGeneration;
416
Lajos Molnar1de1e252015-04-30 18:18:34 -0700417 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800418 // reconnect to surface as MediaCodec disconnected from it
419 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700420 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800421 ALOGW_IF(error != NO_ERROR,
422 "[%s] failed to connect to native window, error=%d",
423 mComponentName.c_str(), error);
424 }
425 mComponentName = "decoder";
426 }
427
428 releaseAndResetMediaBuffers();
429
430 if (err != OK) {
431 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
432 handleError(err);
433 // finish with posting kWhatShutdownCompleted.
434 }
435
436 if (notifyComplete) {
437 sp<AMessage> notify = mNotify->dup();
438 notify->setInt32("what", kWhatShutdownCompleted);
439 notify->post();
440 mPaused = true;
441 }
442}
443
Chong Zhang3b032b32015-04-17 15:49:06 -0700444/*
445 * returns true if we should request more data
446 */
447bool NuPlayer::Decoder::doRequestBuffers() {
Lajos Molnare6109e22015-04-10 17:18:22 -0700448 // mRenderer is only NULL if we have a legacy widevine source that
449 // is not yet ready. In this case we must not fetch input.
450 if (isDiscontinuityPending() || mRenderer == NULL) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700451 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800452 }
453 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800454 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800455 size_t bufferIx = *mDequeuedInputBuffers.begin();
456 sp<AMessage> msg = new AMessage();
457 msg->setSize("buffer-ix", bufferIx);
458 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800459 if (err != OK && err != ERROR_END_OF_STREAM) {
460 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800461 break;
462 }
463 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
464
465 if (!mPendingInputMessages.empty()
466 || !onInputBufferFetched(msg)) {
467 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700468 }
469 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800470
Chong Zhang3b032b32015-04-17 15:49:06 -0700471 return err == -EWOULDBLOCK
472 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700473}
474
Marco Nelissen421f47c2015-03-25 14:40:32 -0700475void NuPlayer::Decoder::handleError(int32_t err)
476{
477 // We cannot immediately release the codec due to buffers still outstanding
478 // in the renderer. We signal to the player the error so it can shutdown/release the
479 // decoder after flushing and increment the generation to discard unnecessary messages.
480
481 ++mBufferGeneration;
482
483 sp<AMessage> notify = mNotify->dup();
484 notify->setInt32("what", kWhatError);
485 notify->setInt32("err", err);
486 notify->post();
487}
488
489bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800490 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800491 return false;
492 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700493
494 sp<ABuffer> buffer;
495 mCodec->getInputBuffer(index, &buffer);
496
Wei Jia6301a5e2015-05-13 13:15:18 -0700497 if (buffer == NULL) {
498 handleError(UNKNOWN_ERROR);
499 return false;
500 }
501
Marco Nelissen421f47c2015-03-25 14:40:32 -0700502 if (index >= mInputBuffers.size()) {
503 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
504 mInputBuffers.add();
505 mMediaBuffers.add();
506 mInputBufferIsDequeued.add();
507 mMediaBuffers.editItemAt(i) = NULL;
508 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800509 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700510 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700511 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700512
Marco Nelissen421f47c2015-03-25 14:40:32 -0700513 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800514
Marco Nelissen421f47c2015-03-25 14:40:32 -0700515 if (mMediaBuffers[index] != NULL) {
516 mMediaBuffers[index]->release();
517 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700518 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700519 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700520
Lajos Molnar87603c02014-08-20 19:25:30 -0700521 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800522 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700523 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800524
Lajos Molnar87603c02014-08-20 19:25:30 -0700525 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
526 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800527 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700528 mCSDsToSubmit.removeAt(0);
Wei Jia15c37402016-01-07 16:03:03 -0800529 if (!onInputBufferFetched(msg)) {
530 handleError(UNKNOWN_ERROR);
531 return false;
532 }
Wei Jia2245fc62014-10-02 15:12:25 -0700533 return true;
534 }
535
536 while (!mPendingInputMessages.empty()) {
537 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800538 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700539 break;
540 }
541 mPendingInputMessages.erase(mPendingInputMessages.begin());
542 }
543
Marco Nelissen421f47c2015-03-25 14:40:32 -0700544 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700545 return true;
546 }
547
Marco Nelissen421f47c2015-03-25 14:40:32 -0700548 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800549
550 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800551 return true;
552}
553
Marco Nelissen421f47c2015-03-25 14:40:32 -0700554bool NuPlayer::Decoder::handleAnOutputBuffer(
555 size_t index,
556 size_t offset,
557 size_t size,
558 int64_t timeUs,
559 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700560// CHECK_LT(bufferIx, mOutputBuffers.size());
561 sp<ABuffer> buffer;
562 mCodec->getOutputBuffer(index, &buffer);
563
Santhosh Behara065e2292015-10-09 17:32:58 -0700564 if (buffer == NULL) {
565 handleError(UNKNOWN_ERROR);
566 return false;
567 }
568
Marco Nelissen421f47c2015-03-25 14:40:32 -0700569 if (index >= mOutputBuffers.size()) {
570 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
571 mOutputBuffers.add();
572 }
573 }
574
575 mOutputBuffers.editItemAt(index) = buffer;
576
Chong Zhang7137ec72014-11-12 16:41:05 -0800577 buffer->setRange(offset, size);
578 buffer->meta()->clear();
579 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800580
581 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800582 // we do not expect CODECCONFIG or SYNCFRAME for decoder
583
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800584 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700585 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800586 reply->setInt32("generation", mBufferGeneration);
587
Chong Zhang66704af2015-03-03 19:32:35 -0800588 if (eos) {
589 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
590
591 buffer->meta()->setInt32("eos", true);
592 reply->setInt32("eos", true);
593 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800594 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
595 ALOGV("[%s] dropping buffer at time %lld as requested.",
596 mComponentName.c_str(), (long long)timeUs);
597
598 reply->post();
599 return true;
600 }
601
602 mSkipRenderingUntilMediaTimeUs = -1;
603 }
604
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700605 mNumFramesTotal += !mIsAudio;
606
Chong Zhangf8d71772014-11-26 15:08:34 -0800607 // wait until 1st frame comes out to signal resume complete
608 notifyResumeCompleteIfNecessary();
609
Chong Zhang7137ec72014-11-12 16:41:05 -0800610 if (mRenderer != NULL) {
611 // send the buffer to renderer.
612 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800613 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800614 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
615 }
616 }
617
618 return true;
619}
620
Marco Nelissen421f47c2015-03-25 14:40:32 -0700621void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
622 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700623 int32_t width, height;
624 if (format->findInt32("width", &width)
625 && format->findInt32("height", &height)) {
626 mStats->setInt32("width", width);
627 mStats->setInt32("height", height);
628 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700629 sp<AMessage> notify = mNotify->dup();
630 notify->setInt32("what", kWhatVideoSizeChanged);
631 notify->setMessage("format", format);
632 notify->post();
633 } else if (mRenderer != NULL) {
634 uint32_t flags;
635 int64_t durationUs;
636 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700637 if (getAudioDeepBufferSetting() // override regardless of source duration
638 || (!hasVideo
639 && mSource->getDuration(&durationUs) == OK
640 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700641 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
642 } else {
643 flags = AUDIO_OUTPUT_FLAG_NONE;
644 }
645
646 mRenderer->openAudioSink(
647 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
648 }
649}
650
Chong Zhang7137ec72014-11-12 16:41:05 -0800651void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
652 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
653 if (mMediaBuffers[i] != NULL) {
654 mMediaBuffers[i]->release();
655 mMediaBuffers.editItemAt(i) = NULL;
656 }
657 }
658 mMediaBuffers.resize(mInputBuffers.size());
659 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
660 mMediaBuffers.editItemAt(i) = NULL;
661 }
662 mInputBufferIsDequeued.clear();
663 mInputBufferIsDequeued.resize(mInputBuffers.size());
664 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
665 mInputBufferIsDequeued.editItemAt(i) = false;
666 }
667
668 mPendingInputMessages.clear();
669 mDequeuedInputBuffers.clear();
670 mSkipRenderingUntilMediaTimeUs = -1;
671}
672
673void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800674 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800675 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800676 reply->setInt32("generation", mBufferGeneration);
677 mCodec->requestActivityNotification(reply);
678 }
679}
680
681bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
682 int32_t generation;
683 CHECK(msg->findInt32("generation", &generation));
684 return generation != mBufferGeneration;
685}
686
687status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
688 sp<ABuffer> accessUnit;
689 bool dropAccessUnit;
690 do {
691 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
692
693 if (err == -EWOULDBLOCK) {
694 return err;
695 } else if (err != OK) {
696 if (err == INFO_DISCONTINUITY) {
697 int32_t type;
698 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
699
700 bool formatChange =
701 (mIsAudio &&
702 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
703 || (!mIsAudio &&
704 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
705
706 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
707
708 ALOGI("%s discontinuity (format=%d, time=%d)",
709 mIsAudio ? "audio" : "video", formatChange, timeChange);
710
711 bool seamlessFormatChange = false;
712 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
713 if (formatChange) {
714 seamlessFormatChange =
715 supportsSeamlessFormatChange(newFormat);
716 // treat seamless format change separately
717 formatChange = !seamlessFormatChange;
718 }
719
Chong Zhang66704af2015-03-03 19:32:35 -0800720 // For format or time change, return EOS to queue EOS input,
721 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800722 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800723 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800724 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800725 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800726 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800727 mTimeChangePending = true;
728 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800729 } else if (seamlessFormatChange) {
730 // reuse existing decoder and don't flush
731 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800732 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800733 } else {
734 // This stream is unaffected by the discontinuity
735 return -EWOULDBLOCK;
736 }
737 }
738
Chong Zhang66704af2015-03-03 19:32:35 -0800739 // reply should only be returned without a buffer set
740 // when there is an error (including EOS)
741 CHECK(err != OK);
742
Chong Zhang7137ec72014-11-12 16:41:05 -0800743 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800744 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800745 }
746
Chong Zhang7137ec72014-11-12 16:41:05 -0800747 dropAccessUnit = false;
748 if (!mIsAudio
749 && !mIsSecure
750 && mRenderer->getVideoLateByUs() > 100000ll
751 && mIsVideoAVC
752 && !IsAVCReferenceFrame(accessUnit)) {
753 dropAccessUnit = true;
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700754 ++mNumInputFramesDropped;
Chong Zhang7137ec72014-11-12 16:41:05 -0800755 }
756 } while (dropAccessUnit);
757
758 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
759#if 0
760 int64_t mediaTimeUs;
761 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700762 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800763 mIsAudio ? "audio" : "video",
764 mediaTimeUs / 1E6);
765#endif
766
767 if (mCCDecoder != NULL) {
768 mCCDecoder->decode(accessUnit);
769 }
770
771 reply->setBuffer("buffer", accessUnit);
772
773 return OK;
774}
775
776bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800777 size_t bufferIx;
778 CHECK(msg->findSize("buffer-ix", &bufferIx));
779 CHECK_LT(bufferIx, mInputBuffers.size());
780 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
781
782 sp<ABuffer> buffer;
783 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700784
785 // handle widevine classic source - that fills an arbitrary input buffer
786 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700787 if (hasBuffer) {
788 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
789 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700790 // likely filled another buffer than we requested: adjust buffer index
791 size_t ix;
792 for (ix = 0; ix < mInputBuffers.size(); ix++) {
793 const sp<ABuffer> &buf = mInputBuffers[ix];
794 if (buf->data() == mediaBuffer->data()) {
795 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700796 if (!mInputBufferIsDequeued[ix]) {
797 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
798 mComponentName.c_str(), ix, bufferIx);
799 mediaBuffer->release();
800 return false;
801 }
Lajos Molnar09524832014-07-17 14:29:51 -0700802
803 // TRICKY: need buffer for the metadata, so instead, set
804 // codecBuffer to the same (though incorrect) buffer to
805 // avoid a memcpy into the codecBuffer
806 codecBuffer = buffer;
807 codecBuffer->setRange(
808 mediaBuffer->range_offset(),
809 mediaBuffer->range_length());
810 bufferIx = ix;
811 break;
812 }
813 }
814 CHECK(ix < mInputBuffers.size());
815 }
816 }
817
Lajos Molnar1cd13982014-01-17 15:12:51 -0800818 if (buffer == NULL /* includes !hasBuffer */) {
819 int32_t streamErr = ERROR_END_OF_STREAM;
820 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
821
Chong Zhang66704af2015-03-03 19:32:35 -0800822 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800823
824 // attempt to queue EOS
825 status_t err = mCodec->queueInputBuffer(
826 bufferIx,
827 0,
828 0,
829 0,
830 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700831 if (err == OK) {
832 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
833 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800834 streamErr = err;
835 // err will not be ERROR_END_OF_STREAM
836 }
837
838 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700839 ALOGE("Stream error for %s (err=%d), EOS %s queued",
840 mComponentName.c_str(),
841 streamErr,
842 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800843 handleError(streamErr);
844 }
845 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800846 sp<AMessage> extra;
847 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
848 int64_t resumeAtMediaTimeUs;
849 if (extra->findInt64(
850 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
851 ALOGI("[%s] suppressing rendering until %lld us",
852 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
853 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
854 }
855 }
856
Lajos Molnar1cd13982014-01-17 15:12:51 -0800857 int64_t timeUs = 0;
858 uint32_t flags = 0;
859 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
860
Lajos Molnar87603c02014-08-20 19:25:30 -0700861 int32_t eos, csd;
862 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800863 if (buffer->meta()->findInt32("eos", &eos) && eos) {
864 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700865 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
866 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800867 }
868
869 // copy into codec buffer
870 if (buffer != codecBuffer) {
Wei Jia15c37402016-01-07 16:03:03 -0800871 if (buffer->size() > codecBuffer->capacity()) {
872 handleError(ERROR_BUFFER_TOO_SMALL);
873 mDequeuedInputBuffers.push_back(bufferIx);
874 return false;
875 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800876 codecBuffer->setRange(0, buffer->size());
877 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
878 }
879
880 status_t err = mCodec->queueInputBuffer(
881 bufferIx,
882 codecBuffer->offset(),
883 codecBuffer->size(),
884 timeUs,
885 flags);
886 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700887 if (mediaBuffer != NULL) {
888 mediaBuffer->release();
889 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800890 ALOGE("Failed to queue input buffer for %s (err=%d)",
891 mComponentName.c_str(), err);
892 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700893 } else {
894 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
895 if (mediaBuffer != NULL) {
896 CHECK(mMediaBuffers[bufferIx] == NULL);
897 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
898 }
Lajos Molnar09524832014-07-17 14:29:51 -0700899 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800900 }
Wei Jia2245fc62014-10-02 15:12:25 -0700901 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800902}
903
Lajos Molnar1cd13982014-01-17 15:12:51 -0800904void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
905 status_t err;
906 int32_t render;
907 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800908 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800909 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800910
Chong Zhang7137ec72014-11-12 16:41:05 -0800911 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800912 int64_t timeUs;
913 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
914 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800915
916 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
917 mCCDecoder->display(timeUs);
918 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800919 }
920
Lajos Molnar1cd13982014-01-17 15:12:51 -0800921 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700922 int64_t timestampNs;
923 CHECK(msg->findInt64("timestampNs", &timestampNs));
924 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800925 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700926 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800927 err = mCodec->releaseOutputBuffer(bufferIx);
928 }
929 if (err != OK) {
930 ALOGE("failed to release output buffer for %s (err=%d)",
931 mComponentName.c_str(), err);
932 handleError(err);
933 }
Chong Zhang66704af2015-03-03 19:32:35 -0800934 if (msg->findInt32("eos", &eos) && eos
935 && isDiscontinuityPending()) {
936 finishHandleDiscontinuity(true /* flushOnTimeChange */);
937 }
938}
939
940bool NuPlayer::Decoder::isDiscontinuityPending() const {
941 return mFormatChangePending || mTimeChangePending;
942}
943
944void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
945 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
946 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
947
948 // If we have format change, pause and wait to be killed;
949 // If we have time change only, flush and restart fetching.
950
951 if (mFormatChangePending) {
952 mPaused = true;
953 } else if (mTimeChangePending) {
954 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700955 doFlush(false /* notifyComplete */);
956 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -0800957 }
Chong Zhang66704af2015-03-03 19:32:35 -0800958 }
959
960 // Notify NuPlayer to either shutdown decoder, or rescan sources
961 sp<AMessage> msg = mNotify->dup();
962 msg->setInt32("what", kWhatInputDiscontinuity);
963 msg->setInt32("formatChange", mFormatChangePending);
964 msg->post();
965
966 mFormatChangePending = false;
967 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800968}
969
Chong Zhang7137ec72014-11-12 16:41:05 -0800970bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
971 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800972 if (targetFormat == NULL) {
973 return true;
974 }
975
976 AString mime;
977 if (!targetFormat->findString("mime", &mime)) {
978 return false;
979 }
980
981 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
982 // field-by-field comparison
983 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
984 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
985 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100986 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800987 !targetFormat->findInt32(keys[i], &newVal) ||
988 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800989 return false;
990 }
991 }
992
993 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100994 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800995 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800996 if (oldBuf->size() != newBuf->size()) {
997 return false;
998 }
999 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1000 }
1001 }
1002 return false;
1003}
1004
1005bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001006 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001007 return false;
1008 }
1009
1010 if (targetFormat == NULL) {
1011 return true;
1012 }
1013
1014 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001015 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001016 || !targetFormat->findString("mime", &newMime)
1017 || !(oldMime == newMime)) {
1018 return false;
1019 }
1020
1021 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1022 bool seamless;
1023 if (audio) {
1024 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1025 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001026 int32_t isAdaptive;
1027 seamless = (mCodec != NULL &&
1028 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1029 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001030 }
1031
1032 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1033 return seamless;
1034}
1035
Chong Zhang7137ec72014-11-12 16:41:05 -08001036void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1037 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001038 return;
1039 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001040 mCSDsForCurrentFormat.clear();
1041 for (int32_t i = 0; ; ++i) {
1042 AString tag = "csd-";
1043 tag.append(i);
1044 sp<ABuffer> buffer;
1045 if (!format->findBuffer(tag.c_str(), &buffer)) {
1046 break;
1047 }
1048 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001049 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001050}
1051
Chong Zhangf8d71772014-11-26 15:08:34 -08001052void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1053 if (mResumePending) {
1054 mResumePending = false;
1055
1056 sp<AMessage> notify = mNotify->dup();
1057 notify->setInt32("what", kWhatResumeCompleted);
1058 notify->post();
1059 }
1060}
1061
Andreas Huberf9334412010-12-15 15:17:42 -08001062} // namespace android
1063