blob: dcc28c4925a146b6484cfd797b14041a21ef7faa [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
Chong Zhang7137ec72014-11-12 16:41:05 -08002 * Copyright 2014 The Android Open Source Project
Andreas Huberf9334412010-12-15 15:17:42 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerDecoder"
19#include <utils/Log.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080020#include <inttypes.h>
Andreas Huberf9334412010-12-15 15:17:42 -080021
Chong Zhang7137ec72014-11-12 16:41:05 -080022#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080023#include "NuPlayerDecoder.h"
Wei Jiac6cfd702014-11-11 16:33:20 -080024#include "NuPlayerRenderer.h"
25#include "NuPlayerSource.h"
26
Andy Hung288da022015-05-31 22:55:59 -070027#include <cutils/properties.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080028#include <media/ICrypto.h>
Andreas Huberf9334412010-12-15 15:17:42 -080029#include <media/stagefright/foundation/ABuffer.h>
30#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080031#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070032#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080033#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080034#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080035#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080036
Lajos Molnar1de1e252015-04-30 18:18:34 -070037#include <gui/Surface.h>
38
Chong Zhang7137ec72014-11-12 16:41:05 -080039#include "avc_utils.h"
40#include "ATSParser.h"
41
Andreas Huberf9334412010-12-15 15:17:42 -080042namespace android {
43
Andy Hung288da022015-05-31 22:55:59 -070044static inline bool getAudioDeepBufferSetting() {
45 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
46}
47
Andreas Huberf9334412010-12-15 15:17:42 -080048NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080049 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080050 const sp<Source> &source,
51 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070052 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080053 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080054 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070055 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080056 mSource(source),
57 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080058 mCCDecoder(ccDecoder),
Wei Jiac6cfd702014-11-11 16:33:20 -080059 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080060 mNumFramesTotal(0ll),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070061 mNumInputFramesDropped(0ll),
62 mNumOutputFramesDropped(0ll),
63 mVideoWidth(0),
64 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080065 mIsAudio(true),
66 mIsVideoAVC(false),
67 mIsSecure(false),
68 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080069 mTimeChangePending(false),
Wei Jia704e7262014-06-04 16:21:56 -070070 mPaused(true),
Chong Zhangf8d71772014-11-26 15:08:34 -080071 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080072 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080073 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070074 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080075 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Andreas Huberf9334412010-12-15 15:17:42 -080076}
77
78NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070079 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070080 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080081}
82
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070083sp<AMessage> NuPlayer::Decoder::getStats() const {
84 mStats->setInt64("frames-total", mNumFramesTotal);
85 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
86 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
87 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -070088}
89
Lajos Molnara81c6222015-07-10 19:17:45 -070090status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
91 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
92 return BAD_VALUE;
93 }
94
95 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
96
97 msg->setObject("surface", surface);
98 sp<AMessage> response;
99 status_t err = msg->postAndAwaitResponse(&response);
100 if (err == OK && response != NULL) {
101 CHECK(response->findInt32("err", &err));
102 }
103 return err;
104}
105
Chong Zhang7137ec72014-11-12 16:41:05 -0800106void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
107 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
108
109 switch (msg->what()) {
110 case kWhatCodecNotify:
111 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700112 int32_t cbID;
113 CHECK(msg->findInt32("callbackID", &cbID));
114
115 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
116 mIsAudio ? "audio" : "video", cbID, mPaused);
117
Marco Nelissen421f47c2015-03-25 14:40:32 -0700118 if (mPaused) {
119 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800120 }
121
Marco Nelissen421f47c2015-03-25 14:40:32 -0700122 switch (cbID) {
123 case MediaCodec::CB_INPUT_AVAILABLE:
124 {
125 int32_t index;
126 CHECK(msg->findInt32("index", &index));
127
128 handleAnInputBuffer(index);
129 break;
130 }
131
132 case MediaCodec::CB_OUTPUT_AVAILABLE:
133 {
134 int32_t index;
135 size_t offset;
136 size_t size;
137 int64_t timeUs;
138 int32_t flags;
139
140 CHECK(msg->findInt32("index", &index));
141 CHECK(msg->findSize("offset", &offset));
142 CHECK(msg->findSize("size", &size));
143 CHECK(msg->findInt64("timeUs", &timeUs));
144 CHECK(msg->findInt32("flags", &flags));
145
146 handleAnOutputBuffer(index, offset, size, timeUs, flags);
147 break;
148 }
149
150 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
151 {
152 sp<AMessage> format;
153 CHECK(msg->findMessage("format", &format));
154
155 handleOutputFormatChange(format);
156 break;
157 }
158
159 case MediaCodec::CB_ERROR:
160 {
161 status_t err;
162 CHECK(msg->findInt32("err", &err));
163 ALOGE("Decoder (%s) reported error : 0x%x",
164 mIsAudio ? "audio" : "video", err);
165
166 handleError(err);
167 break;
168 }
169
170 default:
171 {
172 TRESPASS();
173 break;
174 }
175 }
176
Lajos Molnar87603c02014-08-20 19:25:30 -0700177 break;
178 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800179
180 case kWhatRenderBuffer:
181 {
182 if (!isStaleReply(msg)) {
183 onRenderBuffer(msg);
184 }
185 break;
186 }
187
Lajos Molnara81c6222015-07-10 19:17:45 -0700188 case kWhatSetVideoSurface:
189 {
190 sp<AReplyToken> replyID;
191 CHECK(msg->senderAwaitsResponse(&replyID));
192
193 sp<RefBase> obj;
194 CHECK(msg->findObject("surface", &obj));
195 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
196 int32_t err = INVALID_OPERATION;
197 // NOTE: in practice mSurface is always non-null, but checking here for completeness
198 if (mCodec != NULL && mSurface != NULL) {
199 // TODO: once AwesomePlayer is removed, remove this automatic connecting
200 // to the surface by MediaPlayerService.
201 //
202 // at this point MediaPlayerService::client has already connected to the
203 // surface, which MediaCodec does not expect
204 err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
205 if (err == OK) {
206 err = mCodec->setSurface(surface);
207 ALOGI_IF(err, "codec setSurface returned: %d", err);
208 if (err == OK) {
209 // reconnect to the old surface as MPS::Client will expect to
210 // be able to disconnect from it.
211 (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
212 mSurface = surface;
213 }
214 }
215 if (err != OK) {
216 // reconnect to the new surface on error as MPS::Client will expect to
217 // be able to disconnect from it.
218 (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
219 }
220 }
221
222 sp<AMessage> response = new AMessage;
223 response->setInt32("err", err);
224 response->postReply(replyID);
225 break;
226 }
227
Chong Zhang7137ec72014-11-12 16:41:05 -0800228 default:
229 DecoderBase::onMessageReceived(msg);
230 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700231 }
232}
233
Lajos Molnar1cd13982014-01-17 15:12:51 -0800234void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800235 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800236
Chong Zhang7137ec72014-11-12 16:41:05 -0800237 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800238 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800239
Lajos Molnar1cd13982014-01-17 15:12:51 -0800240 ++mBufferGeneration;
241
Andreas Huber84066782011-08-16 09:34:26 -0700242 AString mime;
243 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800244
Chong Zhang7137ec72014-11-12 16:41:05 -0800245 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
246 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
247
Lajos Molnar1cd13982014-01-17 15:12:51 -0800248 mComponentName = mime;
249 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700250 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800251
252 mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
Lajos Molnar09524832014-07-17 14:29:51 -0700253 int32_t secure = 0;
254 if (format->findInt32("secure", &secure) && secure != 0) {
255 if (mCodec != NULL) {
256 mCodec->getName(&mComponentName);
257 mComponentName.append(".secure");
258 mCodec->release();
259 ALOGI("[%s] creating", mComponentName.c_str());
260 mCodec = MediaCodec::CreateByComponentName(
261 mCodecLooper, mComponentName.c_str());
262 }
263 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800264 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700265 ALOGE("Failed to create %s%s decoder",
266 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800267 handleError(UNKNOWN_ERROR);
268 return;
269 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800270 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800271
272 mCodec->getName(&mComponentName);
273
Lajos Molnar14986f62014-09-15 11:04:44 -0700274 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700275 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800276 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700277 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700278 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700279 // We treat this as a warning, as this is a preparatory step.
280 // Codec will try to connect to the surface, which is where
281 // any error signaling will occur.
282 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800283 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700284 err = mCodec->configure(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700285 format, mSurface, NULL /* crypto */, 0 /* flags */);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800286 if (err != OK) {
287 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700288 mCodec->release();
289 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800290 handleError(err);
291 return;
292 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700293 rememberCodecSpecificData(format);
294
Lajos Molnar1cd13982014-01-17 15:12:51 -0800295 // the following should work in configured state
296 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
297 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
298
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700299 mStats->setString("mime", mime.c_str());
300 mStats->setString("component-name", mComponentName.c_str());
301
302 if (!mIsAudio) {
303 int32_t width, height;
304 if (mOutputFormat->findInt32("width", &width)
305 && mOutputFormat->findInt32("height", &height)) {
306 mStats->setInt32("width", width);
307 mStats->setInt32("height", height);
308 }
309 }
310
Marco Nelissen421f47c2015-03-25 14:40:32 -0700311 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
312 mCodec->setCallback(reply);
313
Lajos Molnar1cd13982014-01-17 15:12:51 -0800314 err = mCodec->start();
315 if (err != OK) {
316 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700317 mCodec->release();
318 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800319 handleError(err);
320 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800321 }
322
Lajos Molnar09524832014-07-17 14:29:51 -0700323 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700324
Wei Jia704e7262014-06-04 16:21:56 -0700325 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800326 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800327}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700328
Ronghua Wu8db88132015-04-22 13:51:35 -0700329void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
330 if (mCodec == NULL) {
331 ALOGW("onSetParameters called before codec is created.");
332 return;
333 }
334 mCodec->setParameters(params);
335}
336
Chong Zhang7137ec72014-11-12 16:41:05 -0800337void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
338 bool hadNoRenderer = (mRenderer == NULL);
339 mRenderer = renderer;
340 if (hadNoRenderer && mRenderer != NULL) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700341 // this means that the widevine legacy source is ready
342 onRequestInputBuffers();
Chong Zhang7137ec72014-11-12 16:41:05 -0800343 }
344}
345
346void NuPlayer::Decoder::onGetInputBuffers(
347 Vector<sp<ABuffer> > *dstBuffers) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700348 CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers));
Chong Zhang7137ec72014-11-12 16:41:05 -0800349}
350
Chong Zhangf8d71772014-11-26 15:08:34 -0800351void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800352 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800353
354 if (notifyComplete) {
355 mResumePending = true;
356 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700357 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800358}
359
Chong Zhang66704af2015-03-03 19:32:35 -0800360void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800361 if (mCCDecoder != NULL) {
362 mCCDecoder->flush();
363 }
364
365 if (mRenderer != NULL) {
366 mRenderer->flush(mIsAudio, notifyComplete);
367 mRenderer->signalTimeDiscontinuity();
368 }
369
370 status_t err = OK;
371 if (mCodec != NULL) {
372 err = mCodec->flush();
373 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
374 ++mBufferGeneration;
375 }
376
377 if (err != OK) {
378 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
379 handleError(err);
380 // finish with posting kWhatFlushCompleted.
381 // we attempt to release the buffers even if flush fails.
382 }
383 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700384 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800385}
Chong Zhang7137ec72014-11-12 16:41:05 -0800386
Marco Nelissen421f47c2015-03-25 14:40:32 -0700387
Chong Zhang66704af2015-03-03 19:32:35 -0800388void NuPlayer::Decoder::onFlush() {
389 doFlush(true);
390
391 if (isDiscontinuityPending()) {
392 // This could happen if the client starts seeking/shutdown
393 // after we queued an EOS for discontinuities.
394 // We can consider discontinuity handled.
395 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800396 }
Chong Zhang66704af2015-03-03 19:32:35 -0800397
398 sp<AMessage> notify = mNotify->dup();
399 notify->setInt32("what", kWhatFlushCompleted);
400 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800401}
402
403void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
404 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800405
406 // if there is a pending resume request, notify complete now
407 notifyResumeCompleteIfNecessary();
408
Chong Zhang7137ec72014-11-12 16:41:05 -0800409 if (mCodec != NULL) {
410 err = mCodec->release();
411 mCodec = NULL;
412 ++mBufferGeneration;
413
Lajos Molnar1de1e252015-04-30 18:18:34 -0700414 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800415 // reconnect to surface as MediaCodec disconnected from it
416 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700417 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800418 ALOGW_IF(error != NO_ERROR,
419 "[%s] failed to connect to native window, error=%d",
420 mComponentName.c_str(), error);
421 }
422 mComponentName = "decoder";
423 }
424
425 releaseAndResetMediaBuffers();
426
427 if (err != OK) {
428 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
429 handleError(err);
430 // finish with posting kWhatShutdownCompleted.
431 }
432
433 if (notifyComplete) {
434 sp<AMessage> notify = mNotify->dup();
435 notify->setInt32("what", kWhatShutdownCompleted);
436 notify->post();
437 mPaused = true;
438 }
439}
440
Chong Zhang3b032b32015-04-17 15:49:06 -0700441/*
442 * returns true if we should request more data
443 */
444bool NuPlayer::Decoder::doRequestBuffers() {
Lajos Molnare6109e22015-04-10 17:18:22 -0700445 // mRenderer is only NULL if we have a legacy widevine source that
446 // is not yet ready. In this case we must not fetch input.
447 if (isDiscontinuityPending() || mRenderer == NULL) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700448 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800449 }
450 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800451 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800452 size_t bufferIx = *mDequeuedInputBuffers.begin();
453 sp<AMessage> msg = new AMessage();
454 msg->setSize("buffer-ix", bufferIx);
455 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800456 if (err != OK && err != ERROR_END_OF_STREAM) {
457 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800458 break;
459 }
460 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
461
462 if (!mPendingInputMessages.empty()
463 || !onInputBufferFetched(msg)) {
464 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700465 }
466 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800467
Chong Zhang3b032b32015-04-17 15:49:06 -0700468 return err == -EWOULDBLOCK
469 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700470}
471
Marco Nelissen421f47c2015-03-25 14:40:32 -0700472void NuPlayer::Decoder::handleError(int32_t err)
473{
474 // We cannot immediately release the codec due to buffers still outstanding
475 // in the renderer. We signal to the player the error so it can shutdown/release the
476 // decoder after flushing and increment the generation to discard unnecessary messages.
477
478 ++mBufferGeneration;
479
480 sp<AMessage> notify = mNotify->dup();
481 notify->setInt32("what", kWhatError);
482 notify->setInt32("err", err);
483 notify->post();
484}
485
486bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800487 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800488 return false;
489 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700490
491 sp<ABuffer> buffer;
492 mCodec->getInputBuffer(index, &buffer);
493
Wei Jia6301a5e2015-05-13 13:15:18 -0700494 if (buffer == NULL) {
495 handleError(UNKNOWN_ERROR);
496 return false;
497 }
498
Marco Nelissen421f47c2015-03-25 14:40:32 -0700499 if (index >= mInputBuffers.size()) {
500 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
501 mInputBuffers.add();
502 mMediaBuffers.add();
503 mInputBufferIsDequeued.add();
504 mMediaBuffers.editItemAt(i) = NULL;
505 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800506 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700507 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700508 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700509
Marco Nelissen421f47c2015-03-25 14:40:32 -0700510 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800511
Marco Nelissen421f47c2015-03-25 14:40:32 -0700512 if (mMediaBuffers[index] != NULL) {
513 mMediaBuffers[index]->release();
514 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700515 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700516 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700517
Lajos Molnar87603c02014-08-20 19:25:30 -0700518 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800519 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700520 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800521
Lajos Molnar87603c02014-08-20 19:25:30 -0700522 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
523 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800524 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700525 mCSDsToSubmit.removeAt(0);
Chong Zhang7137ec72014-11-12 16:41:05 -0800526 CHECK(onInputBufferFetched(msg));
Wei Jia2245fc62014-10-02 15:12:25 -0700527 return true;
528 }
529
530 while (!mPendingInputMessages.empty()) {
531 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800532 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700533 break;
534 }
535 mPendingInputMessages.erase(mPendingInputMessages.begin());
536 }
537
Marco Nelissen421f47c2015-03-25 14:40:32 -0700538 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700539 return true;
540 }
541
Marco Nelissen421f47c2015-03-25 14:40:32 -0700542 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800543
544 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800545 return true;
546}
547
Marco Nelissen421f47c2015-03-25 14:40:32 -0700548bool NuPlayer::Decoder::handleAnOutputBuffer(
549 size_t index,
550 size_t offset,
551 size_t size,
552 int64_t timeUs,
553 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700554// CHECK_LT(bufferIx, mOutputBuffers.size());
555 sp<ABuffer> buffer;
556 mCodec->getOutputBuffer(index, &buffer);
557
558 if (index >= mOutputBuffers.size()) {
559 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
560 mOutputBuffers.add();
561 }
562 }
563
564 mOutputBuffers.editItemAt(index) = buffer;
565
Chong Zhang7137ec72014-11-12 16:41:05 -0800566 buffer->setRange(offset, size);
567 buffer->meta()->clear();
568 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800569
570 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800571 // we do not expect CODECCONFIG or SYNCFRAME for decoder
572
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800573 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700574 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800575 reply->setInt32("generation", mBufferGeneration);
576
Chong Zhang66704af2015-03-03 19:32:35 -0800577 if (eos) {
578 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
579
580 buffer->meta()->setInt32("eos", true);
581 reply->setInt32("eos", true);
582 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800583 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
584 ALOGV("[%s] dropping buffer at time %lld as requested.",
585 mComponentName.c_str(), (long long)timeUs);
586
587 reply->post();
588 return true;
589 }
590
591 mSkipRenderingUntilMediaTimeUs = -1;
592 }
593
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700594 mNumFramesTotal += !mIsAudio;
595
Chong Zhangf8d71772014-11-26 15:08:34 -0800596 // wait until 1st frame comes out to signal resume complete
597 notifyResumeCompleteIfNecessary();
598
Chong Zhang7137ec72014-11-12 16:41:05 -0800599 if (mRenderer != NULL) {
600 // send the buffer to renderer.
601 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800602 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800603 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
604 }
605 }
606
607 return true;
608}
609
Marco Nelissen421f47c2015-03-25 14:40:32 -0700610void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
611 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700612 int32_t width, height;
613 if (format->findInt32("width", &width)
614 && format->findInt32("height", &height)) {
615 mStats->setInt32("width", width);
616 mStats->setInt32("height", height);
617 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700618 sp<AMessage> notify = mNotify->dup();
619 notify->setInt32("what", kWhatVideoSizeChanged);
620 notify->setMessage("format", format);
621 notify->post();
622 } else if (mRenderer != NULL) {
623 uint32_t flags;
624 int64_t durationUs;
625 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700626 if (getAudioDeepBufferSetting() // override regardless of source duration
627 || (!hasVideo
628 && mSource->getDuration(&durationUs) == OK
629 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700630 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
631 } else {
632 flags = AUDIO_OUTPUT_FLAG_NONE;
633 }
634
635 mRenderer->openAudioSink(
636 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
637 }
638}
639
Chong Zhang7137ec72014-11-12 16:41:05 -0800640void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
641 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
642 if (mMediaBuffers[i] != NULL) {
643 mMediaBuffers[i]->release();
644 mMediaBuffers.editItemAt(i) = NULL;
645 }
646 }
647 mMediaBuffers.resize(mInputBuffers.size());
648 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
649 mMediaBuffers.editItemAt(i) = NULL;
650 }
651 mInputBufferIsDequeued.clear();
652 mInputBufferIsDequeued.resize(mInputBuffers.size());
653 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
654 mInputBufferIsDequeued.editItemAt(i) = false;
655 }
656
657 mPendingInputMessages.clear();
658 mDequeuedInputBuffers.clear();
659 mSkipRenderingUntilMediaTimeUs = -1;
660}
661
662void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800663 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800664 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800665 reply->setInt32("generation", mBufferGeneration);
666 mCodec->requestActivityNotification(reply);
667 }
668}
669
670bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
671 int32_t generation;
672 CHECK(msg->findInt32("generation", &generation));
673 return generation != mBufferGeneration;
674}
675
676status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
677 sp<ABuffer> accessUnit;
678 bool dropAccessUnit;
679 do {
680 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
681
682 if (err == -EWOULDBLOCK) {
683 return err;
684 } else if (err != OK) {
685 if (err == INFO_DISCONTINUITY) {
686 int32_t type;
687 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
688
689 bool formatChange =
690 (mIsAudio &&
691 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
692 || (!mIsAudio &&
693 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
694
695 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
696
697 ALOGI("%s discontinuity (format=%d, time=%d)",
698 mIsAudio ? "audio" : "video", formatChange, timeChange);
699
700 bool seamlessFormatChange = false;
701 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
702 if (formatChange) {
703 seamlessFormatChange =
704 supportsSeamlessFormatChange(newFormat);
705 // treat seamless format change separately
706 formatChange = !seamlessFormatChange;
707 }
708
Chong Zhang66704af2015-03-03 19:32:35 -0800709 // For format or time change, return EOS to queue EOS input,
710 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800711 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800712 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800713 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800714 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800715 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800716 mTimeChangePending = true;
717 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800718 } else if (seamlessFormatChange) {
719 // reuse existing decoder and don't flush
720 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800721 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800722 } else {
723 // This stream is unaffected by the discontinuity
724 return -EWOULDBLOCK;
725 }
726 }
727
Chong Zhang66704af2015-03-03 19:32:35 -0800728 // reply should only be returned without a buffer set
729 // when there is an error (including EOS)
730 CHECK(err != OK);
731
Chong Zhang7137ec72014-11-12 16:41:05 -0800732 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800733 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800734 }
735
Chong Zhang7137ec72014-11-12 16:41:05 -0800736 dropAccessUnit = false;
737 if (!mIsAudio
738 && !mIsSecure
739 && mRenderer->getVideoLateByUs() > 100000ll
740 && mIsVideoAVC
741 && !IsAVCReferenceFrame(accessUnit)) {
742 dropAccessUnit = true;
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700743 ++mNumInputFramesDropped;
Chong Zhang7137ec72014-11-12 16:41:05 -0800744 }
745 } while (dropAccessUnit);
746
747 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
748#if 0
749 int64_t mediaTimeUs;
750 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700751 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800752 mIsAudio ? "audio" : "video",
753 mediaTimeUs / 1E6);
754#endif
755
756 if (mCCDecoder != NULL) {
757 mCCDecoder->decode(accessUnit);
758 }
759
760 reply->setBuffer("buffer", accessUnit);
761
762 return OK;
763}
764
765bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800766 size_t bufferIx;
767 CHECK(msg->findSize("buffer-ix", &bufferIx));
768 CHECK_LT(bufferIx, mInputBuffers.size());
769 sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
770
771 sp<ABuffer> buffer;
772 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700773
774 // handle widevine classic source - that fills an arbitrary input buffer
775 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700776 if (hasBuffer) {
777 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
778 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700779 // likely filled another buffer than we requested: adjust buffer index
780 size_t ix;
781 for (ix = 0; ix < mInputBuffers.size(); ix++) {
782 const sp<ABuffer> &buf = mInputBuffers[ix];
783 if (buf->data() == mediaBuffer->data()) {
784 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700785 if (!mInputBufferIsDequeued[ix]) {
786 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
787 mComponentName.c_str(), ix, bufferIx);
788 mediaBuffer->release();
789 return false;
790 }
Lajos Molnar09524832014-07-17 14:29:51 -0700791
792 // TRICKY: need buffer for the metadata, so instead, set
793 // codecBuffer to the same (though incorrect) buffer to
794 // avoid a memcpy into the codecBuffer
795 codecBuffer = buffer;
796 codecBuffer->setRange(
797 mediaBuffer->range_offset(),
798 mediaBuffer->range_length());
799 bufferIx = ix;
800 break;
801 }
802 }
803 CHECK(ix < mInputBuffers.size());
804 }
805 }
806
Lajos Molnar1cd13982014-01-17 15:12:51 -0800807 if (buffer == NULL /* includes !hasBuffer */) {
808 int32_t streamErr = ERROR_END_OF_STREAM;
809 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
810
Chong Zhang66704af2015-03-03 19:32:35 -0800811 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800812
813 // attempt to queue EOS
814 status_t err = mCodec->queueInputBuffer(
815 bufferIx,
816 0,
817 0,
818 0,
819 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700820 if (err == OK) {
821 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
822 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800823 streamErr = err;
824 // err will not be ERROR_END_OF_STREAM
825 }
826
827 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700828 ALOGE("Stream error for %s (err=%d), EOS %s queued",
829 mComponentName.c_str(),
830 streamErr,
831 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800832 handleError(streamErr);
833 }
834 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800835 sp<AMessage> extra;
836 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
837 int64_t resumeAtMediaTimeUs;
838 if (extra->findInt64(
839 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
840 ALOGI("[%s] suppressing rendering until %lld us",
841 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
842 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
843 }
844 }
845
Lajos Molnar1cd13982014-01-17 15:12:51 -0800846 int64_t timeUs = 0;
847 uint32_t flags = 0;
848 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
849
Lajos Molnar87603c02014-08-20 19:25:30 -0700850 int32_t eos, csd;
851 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800852 if (buffer->meta()->findInt32("eos", &eos) && eos) {
853 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700854 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
855 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800856 }
857
858 // copy into codec buffer
859 if (buffer != codecBuffer) {
860 CHECK_LE(buffer->size(), codecBuffer->capacity());
861 codecBuffer->setRange(0, buffer->size());
862 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
863 }
864
865 status_t err = mCodec->queueInputBuffer(
866 bufferIx,
867 codecBuffer->offset(),
868 codecBuffer->size(),
869 timeUs,
870 flags);
871 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700872 if (mediaBuffer != NULL) {
873 mediaBuffer->release();
874 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800875 ALOGE("Failed to queue input buffer for %s (err=%d)",
876 mComponentName.c_str(), err);
877 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700878 } else {
879 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
880 if (mediaBuffer != NULL) {
881 CHECK(mMediaBuffers[bufferIx] == NULL);
882 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
883 }
Lajos Molnar09524832014-07-17 14:29:51 -0700884 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800885 }
Wei Jia2245fc62014-10-02 15:12:25 -0700886 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800887}
888
Lajos Molnar1cd13982014-01-17 15:12:51 -0800889void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
890 status_t err;
891 int32_t render;
892 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -0800893 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800894 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -0800895
Chong Zhang7137ec72014-11-12 16:41:05 -0800896 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -0800897 int64_t timeUs;
898 sp<ABuffer> buffer = mOutputBuffers[bufferIx];
899 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -0800900
901 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
902 mCCDecoder->display(timeUs);
903 }
Wei Jiac6cfd702014-11-11 16:33:20 -0800904 }
905
Lajos Molnar1cd13982014-01-17 15:12:51 -0800906 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -0700907 int64_t timestampNs;
908 CHECK(msg->findInt64("timestampNs", &timestampNs));
909 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800910 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700911 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800912 err = mCodec->releaseOutputBuffer(bufferIx);
913 }
914 if (err != OK) {
915 ALOGE("failed to release output buffer for %s (err=%d)",
916 mComponentName.c_str(), err);
917 handleError(err);
918 }
Chong Zhang66704af2015-03-03 19:32:35 -0800919 if (msg->findInt32("eos", &eos) && eos
920 && isDiscontinuityPending()) {
921 finishHandleDiscontinuity(true /* flushOnTimeChange */);
922 }
923}
924
925bool NuPlayer::Decoder::isDiscontinuityPending() const {
926 return mFormatChangePending || mTimeChangePending;
927}
928
929void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
930 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
931 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
932
933 // If we have format change, pause and wait to be killed;
934 // If we have time change only, flush and restart fetching.
935
936 if (mFormatChangePending) {
937 mPaused = true;
938 } else if (mTimeChangePending) {
939 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700940 doFlush(false /* notifyComplete */);
941 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -0800942 }
Chong Zhang66704af2015-03-03 19:32:35 -0800943 }
944
945 // Notify NuPlayer to either shutdown decoder, or rescan sources
946 sp<AMessage> msg = mNotify->dup();
947 msg->setInt32("what", kWhatInputDiscontinuity);
948 msg->setInt32("formatChange", mFormatChangePending);
949 msg->post();
950
951 mFormatChangePending = false;
952 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800953}
954
Chong Zhang7137ec72014-11-12 16:41:05 -0800955bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
956 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800957 if (targetFormat == NULL) {
958 return true;
959 }
960
961 AString mime;
962 if (!targetFormat->findString("mime", &mime)) {
963 return false;
964 }
965
966 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
967 // field-by-field comparison
968 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
969 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
970 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100971 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -0800972 !targetFormat->findInt32(keys[i], &newVal) ||
973 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800974 return false;
975 }
976 }
977
978 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +0100979 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -0800980 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800981 if (oldBuf->size() != newBuf->size()) {
982 return false;
983 }
984 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
985 }
986 }
987 return false;
988}
989
990bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +0100991 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -0800992 return false;
993 }
994
995 if (targetFormat == NULL) {
996 return true;
997 }
998
999 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001000 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001001 || !targetFormat->findString("mime", &newMime)
1002 || !(oldMime == newMime)) {
1003 return false;
1004 }
1005
1006 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1007 bool seamless;
1008 if (audio) {
1009 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1010 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001011 int32_t isAdaptive;
1012 seamless = (mCodec != NULL &&
1013 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1014 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001015 }
1016
1017 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1018 return seamless;
1019}
1020
Chong Zhang7137ec72014-11-12 16:41:05 -08001021void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1022 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001023 return;
1024 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001025 mCSDsForCurrentFormat.clear();
1026 for (int32_t i = 0; ; ++i) {
1027 AString tag = "csd-";
1028 tag.append(i);
1029 sp<ABuffer> buffer;
1030 if (!format->findBuffer(tag.c_str(), &buffer)) {
1031 break;
1032 }
1033 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001034 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001035}
1036
Chong Zhangf8d71772014-11-26 15:08:34 -08001037void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1038 if (mResumePending) {
1039 mResumePending = false;
1040
1041 sp<AMessage> notify = mNotify->dup();
1042 notify->setInt32("what", kWhatResumeCompleted);
1043 notify->post();
1044 }
1045}
1046
Andreas Huberf9334412010-12-15 15:17:42 -08001047} // namespace android
1048