blob: 52b2041ea81565e89c0691575eae41ef9dbda179 [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
Lajos Molnar9fb81522016-07-14 07:33:43 -070022#include <algorithm>
23
Chong Zhang7137ec72014-11-12 16:41:05 -080024#include "NuPlayerCCDecoder.h"
Andreas Huberf9334412010-12-15 15:17:42 -080025#include "NuPlayerDecoder.h"
Hassan Shojaniacefac142017-02-06 21:02:02 -080026#include "NuPlayerDrm.h"
Wei Jiac6cfd702014-11-11 16:33:20 -080027#include "NuPlayerRenderer.h"
28#include "NuPlayerSource.h"
29
Andy Hung288da022015-05-31 22:55:59 -070030#include <cutils/properties.h>
Marco Nelissen13aa1a42019-09-27 10:21:55 -070031#include <mediadrm/ICrypto.h>
Dongwon Kangbc8f53b2018-01-25 17:01:44 -080032#include <media/MediaBufferHolder.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090033#include <media/MediaCodecBuffer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080034#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080036#include <media/stagefright/foundation/AMessage.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070037#include <media/stagefright/foundation/avc_utils.h>
Lajos Molnar09524832014-07-17 14:29:51 -070038#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080039#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080040#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080041#include <media/stagefright/MediaErrors.h>
Chong Zhang181fd9b2017-02-16 15:53:03 -080042#include <media/stagefright/SurfaceUtils.h>
Ray Essick64050722022-01-14 13:46:33 -080043#include <mpeg2ts/ATSParser.h>
Lajos Molnar1de1e252015-04-30 18:18:34 -070044#include <gui/Surface.h>
45
Andreas Huberf9334412010-12-15 15:17:42 -080046namespace android {
47
Lajos Molnar9fb81522016-07-14 07:33:43 -070048static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display
Praveen Chavanbbaa1442016-04-08 13:33:49 -070049
50// The default total video frame rate of a stream when that info is not available from
51// the source.
52static float kDefaultVideoFrameRateTotal = 30.f;
53
Andy Hung288da022015-05-31 22:55:59 -070054static inline bool getAudioDeepBufferSetting() {
55 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
56}
57
Andreas Huberf9334412010-12-15 15:17:42 -080058NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080059 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080060 const sp<Source> &source,
Ronghua Wu68845c12015-07-21 09:50:48 -070061 pid_t pid,
Wei Jiaf2ae3e12016-10-27 17:10:59 -070062 uid_t uid,
Wei Jiac6cfd702014-11-11 16:33:20 -080063 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070064 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080065 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080066 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070067 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080068 mSource(source),
69 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080070 mCCDecoder(ccDecoder),
Ronghua Wu68845c12015-07-21 09:50:48 -070071 mPid(pid),
Wei Jiaf2ae3e12016-10-27 17:10:59 -070072 mUid(uid),
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -080073 mSkipRenderingUntilMediaTimeUs(-1LL),
74 mNumFramesTotal(0LL),
75 mNumInputFramesDropped(0LL),
76 mNumOutputFramesDropped(0LL),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070077 mVideoWidth(0),
78 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080079 mIsAudio(true),
80 mIsVideoAVC(false),
81 mIsSecure(false),
Hassan Shojania62dec952017-05-18 10:45:27 -070082 mIsEncrypted(false),
83 mIsEncryptedObservedEarlier(false),
Chong Zhang7137ec72014-11-12 16:41:05 -080084 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080085 mTimeChangePending(false),
Praveen Chavanbbaa1442016-04-08 13:33:49 -070086 mFrameRateTotal(kDefaultVideoFrameRateTotal),
87 mPlaybackSpeed(1.0f),
Lajos Molnar9fb81522016-07-14 07:33:43 -070088 mNumVideoTemporalLayerTotal(1), // decode all layers
Praveen Chavanbbaa1442016-04-08 13:33:49 -070089 mNumVideoTemporalLayerAllowed(1),
90 mCurrentMaxVideoTemporalLayerId(0),
Chong Zhangf8d71772014-11-26 15:08:34 -080091 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080092 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080093 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070094 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080095 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Praveen Chavanbbaa1442016-04-08 13:33:49 -070096 mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
Andreas Huberf9334412010-12-15 15:17:42 -080097}
98
99NuPlayer::Decoder::~Decoder() {
Wei Jia37ff0e62017-04-21 11:24:45 -0700100 // Need to stop looper first since mCodec could be accessed on the mDecoderLooper.
101 stopLooper();
102 if (mCodec != NULL) {
103 mCodec->release();
104 }
Wei Jia4923cee2014-09-24 14:25:19 -0700105 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -0800106}
107
Ray Essick83f56b02019-06-23 15:13:46 -0700108sp<AMessage> NuPlayer::Decoder::getStats() {
Ray Essick372e8f22019-02-11 11:38:31 -0800109
Ray Essick83f56b02019-06-23 15:13:46 -0700110 Mutex::Autolock autolock(mStatsLock);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700111 mStats->setInt64("frames-total", mNumFramesTotal);
112 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
113 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
Ray Essick092f74a2018-11-20 10:25:13 -0800114 mStats->setFloat("frame-rate-total", mFrameRateTotal);
Ray Essick372e8f22019-02-11 11:38:31 -0800115
Ray Essick372e8f22019-02-11 11:38:31 -0800116 // make our own copy, so we aren't victim to any later changes.
117 sp<AMessage> copiedStats = mStats->dup();
Ray Essick83f56b02019-06-23 15:13:46 -0700118
Ray Essick372e8f22019-02-11 11:38:31 -0800119 return copiedStats;
Lajos Molnar09524832014-07-17 14:29:51 -0700120}
121
Lajos Molnara81c6222015-07-10 19:17:45 -0700122status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
123 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
124 return BAD_VALUE;
125 }
126
127 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
128
129 msg->setObject("surface", surface);
130 sp<AMessage> response;
131 status_t err = msg->postAndAwaitResponse(&response);
132 if (err == OK && response != NULL) {
133 CHECK(response->findInt32("err", &err));
134 }
135 return err;
136}
137
Chong Zhang7137ec72014-11-12 16:41:05 -0800138void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
139 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
140
141 switch (msg->what()) {
142 case kWhatCodecNotify:
143 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700144 int32_t cbID;
145 CHECK(msg->findInt32("callbackID", &cbID));
146
147 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
148 mIsAudio ? "audio" : "video", cbID, mPaused);
149
Marco Nelissen421f47c2015-03-25 14:40:32 -0700150 if (mPaused) {
151 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800152 }
153
Marco Nelissen421f47c2015-03-25 14:40:32 -0700154 switch (cbID) {
155 case MediaCodec::CB_INPUT_AVAILABLE:
156 {
157 int32_t index;
158 CHECK(msg->findInt32("index", &index));
159
160 handleAnInputBuffer(index);
161 break;
162 }
163
164 case MediaCodec::CB_OUTPUT_AVAILABLE:
165 {
166 int32_t index;
167 size_t offset;
168 size_t size;
169 int64_t timeUs;
170 int32_t flags;
171
172 CHECK(msg->findInt32("index", &index));
173 CHECK(msg->findSize("offset", &offset));
174 CHECK(msg->findSize("size", &size));
175 CHECK(msg->findInt64("timeUs", &timeUs));
176 CHECK(msg->findInt32("flags", &flags));
177
178 handleAnOutputBuffer(index, offset, size, timeUs, flags);
179 break;
180 }
181
182 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
183 {
184 sp<AMessage> format;
185 CHECK(msg->findMessage("format", &format));
186
187 handleOutputFormatChange(format);
188 break;
189 }
190
191 case MediaCodec::CB_ERROR:
192 {
193 status_t err;
194 CHECK(msg->findInt32("err", &err));
195 ALOGE("Decoder (%s) reported error : 0x%x",
196 mIsAudio ? "audio" : "video", err);
197
198 handleError(err);
199 break;
200 }
201
202 default:
203 {
204 TRESPASS();
205 break;
206 }
207 }
208
Lajos Molnar87603c02014-08-20 19:25:30 -0700209 break;
210 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800211
212 case kWhatRenderBuffer:
213 {
214 if (!isStaleReply(msg)) {
215 onRenderBuffer(msg);
216 }
217 break;
218 }
219
Wei Jia9a3101b2016-11-08 14:34:24 -0800220 case kWhatAudioOutputFormatChanged:
221 {
222 if (!isStaleReply(msg)) {
223 status_t err;
224 if (msg->findInt32("err", &err) && err != OK) {
225 ALOGE("Renderer reported 0x%x when changing audio output format", err);
226 handleError(err);
227 }
228 }
229 break;
230 }
231
Lajos Molnara81c6222015-07-10 19:17:45 -0700232 case kWhatSetVideoSurface:
233 {
234 sp<AReplyToken> replyID;
235 CHECK(msg->senderAwaitsResponse(&replyID));
236
237 sp<RefBase> obj;
238 CHECK(msg->findObject("surface", &obj));
239 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
240 int32_t err = INVALID_OPERATION;
241 // NOTE: in practice mSurface is always non-null, but checking here for completeness
242 if (mCodec != NULL && mSurface != NULL) {
243 // TODO: once AwesomePlayer is removed, remove this automatic connecting
244 // to the surface by MediaPlayerService.
245 //
246 // at this point MediaPlayerService::client has already connected to the
247 // surface, which MediaCodec does not expect
Chong Zhang181fd9b2017-02-16 15:53:03 -0800248 err = nativeWindowDisconnect(surface.get(), "kWhatSetVideoSurface(surface)");
Lajos Molnara81c6222015-07-10 19:17:45 -0700249 if (err == OK) {
250 err = mCodec->setSurface(surface);
251 ALOGI_IF(err, "codec setSurface returned: %d", err);
252 if (err == OK) {
253 // reconnect to the old surface as MPS::Client will expect to
254 // be able to disconnect from it.
Chong Zhang181fd9b2017-02-16 15:53:03 -0800255 (void)nativeWindowConnect(mSurface.get(), "kWhatSetVideoSurface(mSurface)");
Lajos Molnara81c6222015-07-10 19:17:45 -0700256 mSurface = surface;
257 }
258 }
259 if (err != OK) {
260 // reconnect to the new surface on error as MPS::Client will expect to
261 // be able to disconnect from it.
Chong Zhang181fd9b2017-02-16 15:53:03 -0800262 (void)nativeWindowConnect(surface.get(), "kWhatSetVideoSurface(err)");
Lajos Molnara81c6222015-07-10 19:17:45 -0700263 }
264 }
265
266 sp<AMessage> response = new AMessage;
267 response->setInt32("err", err);
268 response->postReply(replyID);
269 break;
270 }
271
Hassan Shojaniacefac142017-02-06 21:02:02 -0800272 case kWhatDrmReleaseCrypto:
273 {
274 ALOGV("kWhatDrmReleaseCrypto");
275 onReleaseCrypto(msg);
276 break;
277 }
278
Chong Zhang7137ec72014-11-12 16:41:05 -0800279 default:
280 DecoderBase::onMessageReceived(msg);
281 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700282 }
283}
284
Lajos Molnar1cd13982014-01-17 15:12:51 -0800285void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800286 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800287
Chong Zhang7137ec72014-11-12 16:41:05 -0800288 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800289 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800290
Lajos Molnar1cd13982014-01-17 15:12:51 -0800291 ++mBufferGeneration;
292
Andreas Huber84066782011-08-16 09:34:26 -0700293 AString mime;
294 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800295
Chong Zhang7137ec72014-11-12 16:41:05 -0800296 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
297 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
298
Lajos Molnar1cd13982014-01-17 15:12:51 -0800299 mComponentName = mime;
300 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700301 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800302
Ronghua Wu68845c12015-07-21 09:50:48 -0700303 mCodec = MediaCodec::CreateByType(
Ray Essick0342ef42021-01-12 16:57:58 -0800304 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid, mUid, format);
Lajos Molnar09524832014-07-17 14:29:51 -0700305 int32_t secure = 0;
306 if (format->findInt32("secure", &secure) && secure != 0) {
307 if (mCodec != NULL) {
308 mCodec->getName(&mComponentName);
309 mComponentName.append(".secure");
310 mCodec->release();
311 ALOGI("[%s] creating", mComponentName.c_str());
312 mCodec = MediaCodec::CreateByComponentName(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700313 mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid, mUid);
Lajos Molnar09524832014-07-17 14:29:51 -0700314 }
315 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800316 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700317 ALOGE("Failed to create %s%s decoder",
318 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800319 handleError(UNKNOWN_ERROR);
320 return;
321 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800322 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800323
324 mCodec->getName(&mComponentName);
325
Lajos Molnar14986f62014-09-15 11:04:44 -0700326 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700327 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800328 // disconnect from surface as MediaCodec will reconnect
Chong Zhang181fd9b2017-02-16 15:53:03 -0800329 err = nativeWindowDisconnect(mSurface.get(), "onConfigure");
Lajos Molnar14986f62014-09-15 11:04:44 -0700330 // We treat this as a warning, as this is a preparatory step.
331 // Codec will try to connect to the surface, which is where
332 // any error signaling will occur.
333 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800334 }
Hassan Shojaniacefac142017-02-06 21:02:02 -0800335
336 // Modular DRM
337 void *pCrypto;
338 if (!format->findPointer("crypto", &pCrypto)) {
339 pCrypto = NULL;
340 }
341 sp<ICrypto> crypto = (ICrypto*)pCrypto;
Hassan Shojania62dec952017-05-18 10:45:27 -0700342 // non-encrypted source won't have a crypto
343 mIsEncrypted = (crypto != NULL);
344 // configure is called once; still using OR in case the behavior changes.
345 mIsEncryptedObservedEarlier = mIsEncryptedObservedEarlier || mIsEncrypted;
Hassan Shojaniacefac142017-02-06 21:02:02 -0800346 ALOGV("onConfigure mCrypto: %p (%d) mIsSecure: %d",
347 crypto.get(), (crypto != NULL ? crypto->getStrongCount() : 0), mIsSecure);
348
Lajos Molnar14986f62014-09-15 11:04:44 -0700349 err = mCodec->configure(
Hassan Shojaniacefac142017-02-06 21:02:02 -0800350 format, mSurface, crypto, 0 /* flags */);
351
Lajos Molnar1cd13982014-01-17 15:12:51 -0800352 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700353 ALOGE("Failed to configure [%s] decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700354 mCodec->release();
355 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800356 handleError(err);
357 return;
358 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700359 rememberCodecSpecificData(format);
360
Lajos Molnar1cd13982014-01-17 15:12:51 -0800361 // the following should work in configured state
362 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
363 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
364
Ray Essick83f56b02019-06-23 15:13:46 -0700365 {
366 Mutex::Autolock autolock(mStatsLock);
367 mStats->setString("mime", mime.c_str());
368 mStats->setString("component-name", mComponentName.c_str());
369 }
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700370
371 if (!mIsAudio) {
372 int32_t width, height;
373 if (mOutputFormat->findInt32("width", &width)
374 && mOutputFormat->findInt32("height", &height)) {
Ray Essick83f56b02019-06-23 15:13:46 -0700375 Mutex::Autolock autolock(mStatsLock);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700376 mStats->setInt32("width", width);
377 mStats->setInt32("height", height);
378 }
379 }
380
Marco Nelissen421f47c2015-03-25 14:40:32 -0700381 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
382 mCodec->setCallback(reply);
383
Lajos Molnar1cd13982014-01-17 15:12:51 -0800384 err = mCodec->start();
385 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700386 ALOGE("Failed to start [%s] decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700387 mCodec->release();
388 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800389 handleError(err);
390 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800391 }
392
Lajos Molnar09524832014-07-17 14:29:51 -0700393 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700394
Wei Jia704e7262014-06-04 16:21:56 -0700395 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800396 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800397}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700398
Ronghua Wu8db88132015-04-22 13:51:35 -0700399void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700400 bool needAdjustLayers = false;
401 float frameRateTotal;
402 if (params->findFloat("frame-rate-total", &frameRateTotal)
403 && mFrameRateTotal != frameRateTotal) {
404 needAdjustLayers = true;
405 mFrameRateTotal = frameRateTotal;
Ronghua Wu8db88132015-04-22 13:51:35 -0700406 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700407
408 int32_t numVideoTemporalLayerTotal;
409 if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
Lajos Molnar9fb81522016-07-14 07:33:43 -0700410 && numVideoTemporalLayerTotal >= 0
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700411 && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
412 && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
413 needAdjustLayers = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700414 mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700415 }
416
Lajos Molnar9fb81522016-07-14 07:33:43 -0700417 if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700418 // TODO: For now, layer fps is calculated for some specific architectures.
419 // But it really should be extracted from the stream.
420 mVideoTemporalLayerAggregateFps[0] =
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800421 mFrameRateTotal / (float)(1LL << (mNumVideoTemporalLayerTotal - 1));
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700422 for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
423 mVideoTemporalLayerAggregateFps[i] =
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800424 mFrameRateTotal / (float)(1LL << (mNumVideoTemporalLayerTotal - i))
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700425 + mVideoTemporalLayerAggregateFps[i - 1];
426 }
427 }
428
429 float playbackSpeed;
430 if (params->findFloat("playback-speed", &playbackSpeed)
431 && mPlaybackSpeed != playbackSpeed) {
432 needAdjustLayers = true;
433 mPlaybackSpeed = playbackSpeed;
434 }
435
436 if (needAdjustLayers) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700437 float decodeFrameRate = mFrameRateTotal;
438 // enable temporal layering optimization only if we know the layering depth
439 if (mNumVideoTemporalLayerTotal > 1) {
440 int32_t layerId;
441 for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
442 if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
443 >= kDisplayRefreshingRate * 0.9) {
444 break;
445 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700446 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700447 mNumVideoTemporalLayerAllowed = layerId + 1;
448 decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700449 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700450 ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
451 mNumVideoTemporalLayerAllowed, decodeFrameRate);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700452
453 if (mCodec == NULL) {
454 ALOGW("onSetParameters called before codec is created.");
455 return;
456 }
457
458 sp<AMessage> codecParams = new AMessage();
Lajos Molnar9fb81522016-07-14 07:33:43 -0700459 codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700460 mCodec->setParameters(codecParams);
461 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700462}
463
Chong Zhang7137ec72014-11-12 16:41:05 -0800464void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800465 mRenderer = renderer;
Chong Zhang7137ec72014-11-12 16:41:05 -0800466}
467
Chong Zhangf8d71772014-11-26 15:08:34 -0800468void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800469 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800470
471 if (notifyComplete) {
472 mResumePending = true;
473 }
Wei Jiafcd87872017-07-28 15:50:04 -0700474
475 if (mCodec == NULL) {
476 ALOGE("[%s] onResume without a valid codec", mComponentName.c_str());
477 handleError(NO_INIT);
478 return;
479 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700480 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800481}
482
Chong Zhang66704af2015-03-03 19:32:35 -0800483void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800484 if (mCCDecoder != NULL) {
485 mCCDecoder->flush();
486 }
487
488 if (mRenderer != NULL) {
489 mRenderer->flush(mIsAudio, notifyComplete);
490 mRenderer->signalTimeDiscontinuity();
491 }
492
493 status_t err = OK;
494 if (mCodec != NULL) {
495 err = mCodec->flush();
496 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
497 ++mBufferGeneration;
498 }
499
500 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700501 ALOGE("failed to flush [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800502 handleError(err);
503 // finish with posting kWhatFlushCompleted.
504 // we attempt to release the buffers even if flush fails.
505 }
506 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700507 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800508}
Chong Zhang7137ec72014-11-12 16:41:05 -0800509
Marco Nelissen421f47c2015-03-25 14:40:32 -0700510
Chong Zhang66704af2015-03-03 19:32:35 -0800511void NuPlayer::Decoder::onFlush() {
512 doFlush(true);
513
514 if (isDiscontinuityPending()) {
515 // This could happen if the client starts seeking/shutdown
516 // after we queued an EOS for discontinuities.
517 // We can consider discontinuity handled.
518 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800519 }
Chong Zhang66704af2015-03-03 19:32:35 -0800520
521 sp<AMessage> notify = mNotify->dup();
522 notify->setInt32("what", kWhatFlushCompleted);
523 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800524}
525
526void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
527 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800528
529 // if there is a pending resume request, notify complete now
530 notifyResumeCompleteIfNecessary();
531
Chong Zhang7137ec72014-11-12 16:41:05 -0800532 if (mCodec != NULL) {
533 err = mCodec->release();
534 mCodec = NULL;
535 ++mBufferGeneration;
536
Lajos Molnar1de1e252015-04-30 18:18:34 -0700537 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800538 // reconnect to surface as MediaCodec disconnected from it
Chong Zhang181fd9b2017-02-16 15:53:03 -0800539 status_t error = nativeWindowConnect(mSurface.get(), "onShutdown");
Chong Zhang7137ec72014-11-12 16:41:05 -0800540 ALOGW_IF(error != NO_ERROR,
541 "[%s] failed to connect to native window, error=%d",
542 mComponentName.c_str(), error);
543 }
544 mComponentName = "decoder";
545 }
546
547 releaseAndResetMediaBuffers();
548
549 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700550 ALOGE("failed to release [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800551 handleError(err);
552 // finish with posting kWhatShutdownCompleted.
553 }
554
555 if (notifyComplete) {
556 sp<AMessage> notify = mNotify->dup();
557 notify->setInt32("what", kWhatShutdownCompleted);
558 notify->post();
559 mPaused = true;
560 }
561}
562
Chong Zhang3b032b32015-04-17 15:49:06 -0700563/*
564 * returns true if we should request more data
565 */
566bool NuPlayer::Decoder::doRequestBuffers() {
Jeff Tinker29b7dcf2016-10-24 10:28:30 -0700567 if (isDiscontinuityPending()) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700568 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800569 }
570 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800571 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800572 size_t bufferIx = *mDequeuedInputBuffers.begin();
573 sp<AMessage> msg = new AMessage();
574 msg->setSize("buffer-ix", bufferIx);
575 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800576 if (err != OK && err != ERROR_END_OF_STREAM) {
577 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800578 break;
579 }
580 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
581
582 if (!mPendingInputMessages.empty()
583 || !onInputBufferFetched(msg)) {
584 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700585 }
586 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800587
Chong Zhang3b032b32015-04-17 15:49:06 -0700588 return err == -EWOULDBLOCK
589 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700590}
591
Marco Nelissen421f47c2015-03-25 14:40:32 -0700592void NuPlayer::Decoder::handleError(int32_t err)
593{
594 // We cannot immediately release the codec due to buffers still outstanding
595 // in the renderer. We signal to the player the error so it can shutdown/release the
596 // decoder after flushing and increment the generation to discard unnecessary messages.
597
598 ++mBufferGeneration;
599
600 sp<AMessage> notify = mNotify->dup();
601 notify->setInt32("what", kWhatError);
602 notify->setInt32("err", err);
603 notify->post();
604}
605
Hassan Shojaniacefac142017-02-06 21:02:02 -0800606status_t NuPlayer::Decoder::releaseCrypto()
607{
608 ALOGV("releaseCrypto");
609
610 sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this);
611
612 sp<AMessage> response;
613 status_t status = msg->postAndAwaitResponse(&response);
614 if (status == OK && response != NULL) {
615 CHECK(response->findInt32("status", &status));
616 ALOGV("releaseCrypto ret: %d ", status);
617 } else {
618 ALOGE("releaseCrypto err: %d", status);
619 }
620
621 return status;
622}
623
624void NuPlayer::Decoder::onReleaseCrypto(const sp<AMessage>& msg)
625{
626 status_t status = INVALID_OPERATION;
627 if (mCodec != NULL) {
628 status = mCodec->releaseCrypto();
629 } else {
630 // returning OK if the codec has been already released
631 status = OK;
632 ALOGE("onReleaseCrypto No mCodec. err: %d", status);
633 }
634
635 sp<AMessage> response = new AMessage;
636 response->setInt32("status", status);
Hassan Shojania62dec952017-05-18 10:45:27 -0700637 // Clearing the state as it's tied to crypto. mIsEncryptedObservedEarlier is sticky though
638 // and lasts for the lifetime of this codec. See its use in fetchInputData.
639 mIsEncrypted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -0800640
641 sp<AReplyToken> replyID;
642 CHECK(msg->senderAwaitsResponse(&replyID));
643 response->postReply(replyID);
644}
645
Marco Nelissen421f47c2015-03-25 14:40:32 -0700646bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800647 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800648 return false;
649 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700650
Wei Jiafcd87872017-07-28 15:50:04 -0700651 if (mCodec == NULL) {
652 ALOGE("[%s] handleAnInputBuffer without a valid codec", mComponentName.c_str());
653 handleError(NO_INIT);
654 return false;
655 }
656
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900657 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700658 mCodec->getInputBuffer(index, &buffer);
659
Wei Jia6301a5e2015-05-13 13:15:18 -0700660 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700661 ALOGE("[%s] handleAnInputBuffer, failed to get input buffer", mComponentName.c_str());
Wei Jia6301a5e2015-05-13 13:15:18 -0700662 handleError(UNKNOWN_ERROR);
663 return false;
664 }
665
Marco Nelissen421f47c2015-03-25 14:40:32 -0700666 if (index >= mInputBuffers.size()) {
667 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
668 mInputBuffers.add();
669 mMediaBuffers.add();
670 mInputBufferIsDequeued.add();
671 mMediaBuffers.editItemAt(i) = NULL;
672 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800673 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700674 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700675 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700676
Marco Nelissen421f47c2015-03-25 14:40:32 -0700677 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800678
Marco Nelissen421f47c2015-03-25 14:40:32 -0700679 if (mMediaBuffers[index] != NULL) {
680 mMediaBuffers[index]->release();
681 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700682 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700683 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700684
Lajos Molnar87603c02014-08-20 19:25:30 -0700685 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800686 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700687 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800688
Lajos Molnar87603c02014-08-20 19:25:30 -0700689 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800690 ALOGV("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800691 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700692 mCSDsToSubmit.removeAt(0);
Wei Jia56097a82016-01-07 16:03:03 -0800693 if (!onInputBufferFetched(msg)) {
694 handleError(UNKNOWN_ERROR);
695 return false;
696 }
Wei Jia2245fc62014-10-02 15:12:25 -0700697 return true;
698 }
699
700 while (!mPendingInputMessages.empty()) {
701 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800702 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700703 break;
704 }
705 mPendingInputMessages.erase(mPendingInputMessages.begin());
706 }
707
Marco Nelissen421f47c2015-03-25 14:40:32 -0700708 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700709 return true;
710 }
711
Marco Nelissen421f47c2015-03-25 14:40:32 -0700712 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800713
714 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800715 return true;
716}
717
Marco Nelissen421f47c2015-03-25 14:40:32 -0700718bool NuPlayer::Decoder::handleAnOutputBuffer(
719 size_t index,
720 size_t offset,
721 size_t size,
722 int64_t timeUs,
723 int32_t flags) {
Wei Jiafcd87872017-07-28 15:50:04 -0700724 if (mCodec == NULL) {
725 ALOGE("[%s] handleAnOutputBuffer without a valid codec", mComponentName.c_str());
726 handleError(NO_INIT);
727 return false;
728 }
729
Marco Nelissen421f47c2015-03-25 14:40:32 -0700730// CHECK_LT(bufferIx, mOutputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900731 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700732 mCodec->getOutputBuffer(index, &buffer);
733
Santhosh Behara3539def2015-10-09 17:32:58 -0700734 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700735 ALOGE("[%s] handleAnOutputBuffer, failed to get output buffer", mComponentName.c_str());
Santhosh Behara3539def2015-10-09 17:32:58 -0700736 handleError(UNKNOWN_ERROR);
737 return false;
738 }
739
Marco Nelissen421f47c2015-03-25 14:40:32 -0700740 if (index >= mOutputBuffers.size()) {
741 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
742 mOutputBuffers.add();
743 }
744 }
745
746 mOutputBuffers.editItemAt(index) = buffer;
747
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900748 int64_t frameIndex;
749 bool frameIndexFound = buffer->meta()->findInt64("frameIndex", &frameIndex);
750
Chong Zhang7137ec72014-11-12 16:41:05 -0800751 buffer->setRange(offset, size);
752 buffer->meta()->clear();
753 buffer->meta()->setInt64("timeUs", timeUs);
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900754 if (frameIndexFound) {
755 buffer->meta()->setInt64("frameIndex", frameIndex);
756 }
Chong Zhang66704af2015-03-03 19:32:35 -0800757
758 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800759 // we do not expect CODECCONFIG or SYNCFRAME for decoder
760
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800761 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700762 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800763 reply->setInt32("generation", mBufferGeneration);
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -0700764 reply->setSize("size", size);
Chong Zhang7137ec72014-11-12 16:41:05 -0800765
Chong Zhang66704af2015-03-03 19:32:35 -0800766 if (eos) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800767 ALOGV("[%s] saw output EOS", mIsAudio ? "audio" : "video");
Chong Zhang66704af2015-03-03 19:32:35 -0800768
769 buffer->meta()->setInt32("eos", true);
770 reply->setInt32("eos", true);
Wei Jiaaec8d822017-08-25 15:27:57 -0700771 }
772
Ray Essickc6e9f6e2017-12-07 16:57:36 -0800773 mNumFramesTotal += !mIsAudio;
774
Wei Jiaaec8d822017-08-25 15:27:57 -0700775 if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800776 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
777 ALOGV("[%s] dropping buffer at time %lld as requested.",
778 mComponentName.c_str(), (long long)timeUs);
779
780 reply->post();
Wei Jiaaec8d822017-08-25 15:27:57 -0700781 if (eos) {
782 notifyResumeCompleteIfNecessary();
783 if (mRenderer != NULL && !isDiscontinuityPending()) {
784 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
785 }
786 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800787 return true;
788 }
789
790 mSkipRenderingUntilMediaTimeUs = -1;
791 }
792
Chong Zhangf8d71772014-11-26 15:08:34 -0800793 // wait until 1st frame comes out to signal resume complete
794 notifyResumeCompleteIfNecessary();
795
Chong Zhang7137ec72014-11-12 16:41:05 -0800796 if (mRenderer != NULL) {
797 // send the buffer to renderer.
798 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800799 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800800 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
801 }
802 }
803
804 return true;
805}
806
Marco Nelissen421f47c2015-03-25 14:40:32 -0700807void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
808 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700809 int32_t width, height;
810 if (format->findInt32("width", &width)
811 && format->findInt32("height", &height)) {
Ray Essick83f56b02019-06-23 15:13:46 -0700812 Mutex::Autolock autolock(mStatsLock);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700813 mStats->setInt32("width", width);
814 mStats->setInt32("height", height);
815 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700816 sp<AMessage> notify = mNotify->dup();
817 notify->setInt32("what", kWhatVideoSizeChanged);
818 notify->setMessage("format", format);
819 notify->post();
820 } else if (mRenderer != NULL) {
821 uint32_t flags;
822 int64_t durationUs;
823 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700824 if (getAudioDeepBufferSetting() // override regardless of source duration
Andy Hung71c8e5c2017-04-03 15:50:27 -0700825 || (mSource->getDuration(&durationUs) == OK
Andy Hung288da022015-05-31 22:55:59 -0700826 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700827 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
828 } else {
829 flags = AUDIO_OUTPUT_FLAG_NONE;
830 }
831
Wei Jia9a3101b2016-11-08 14:34:24 -0800832 sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this);
833 reply->setInt32("generation", mBufferGeneration);
834 mRenderer->changeAudioFormat(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +0530835 format, false /* offloadOnly */, hasVideo,
836 flags, mSource->isStreaming(), reply);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700837 }
838}
839
Chong Zhang7137ec72014-11-12 16:41:05 -0800840void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
841 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
842 if (mMediaBuffers[i] != NULL) {
843 mMediaBuffers[i]->release();
844 mMediaBuffers.editItemAt(i) = NULL;
845 }
846 }
847 mMediaBuffers.resize(mInputBuffers.size());
848 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
849 mMediaBuffers.editItemAt(i) = NULL;
850 }
851 mInputBufferIsDequeued.clear();
852 mInputBufferIsDequeued.resize(mInputBuffers.size());
853 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
854 mInputBufferIsDequeued.editItemAt(i) = false;
855 }
856
857 mPendingInputMessages.clear();
858 mDequeuedInputBuffers.clear();
859 mSkipRenderingUntilMediaTimeUs = -1;
860}
861
862void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800863 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800864 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800865 reply->setInt32("generation", mBufferGeneration);
866 mCodec->requestActivityNotification(reply);
867 }
868}
869
870bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
871 int32_t generation;
872 CHECK(msg->findInt32("generation", &generation));
873 return generation != mBufferGeneration;
874}
875
876status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
877 sp<ABuffer> accessUnit;
Robert Shih59e9ca72016-10-20 13:51:42 -0700878 bool dropAccessUnit = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800879 do {
880 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
881
882 if (err == -EWOULDBLOCK) {
883 return err;
884 } else if (err != OK) {
885 if (err == INFO_DISCONTINUITY) {
886 int32_t type;
887 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
888
889 bool formatChange =
890 (mIsAudio &&
891 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
892 || (!mIsAudio &&
893 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
894
895 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
896
897 ALOGI("%s discontinuity (format=%d, time=%d)",
898 mIsAudio ? "audio" : "video", formatChange, timeChange);
899
900 bool seamlessFormatChange = false;
901 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
902 if (formatChange) {
903 seamlessFormatChange =
904 supportsSeamlessFormatChange(newFormat);
905 // treat seamless format change separately
906 formatChange = !seamlessFormatChange;
907 }
908
Chong Zhang66704af2015-03-03 19:32:35 -0800909 // For format or time change, return EOS to queue EOS input,
910 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800911 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800912 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800913 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800914 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800915 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800916 mTimeChangePending = true;
917 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800918 } else if (seamlessFormatChange) {
919 // reuse existing decoder and don't flush
920 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800921 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800922 } else {
923 // This stream is unaffected by the discontinuity
924 return -EWOULDBLOCK;
925 }
926 }
927
Chong Zhang66704af2015-03-03 19:32:35 -0800928 // reply should only be returned without a buffer set
929 // when there is an error (including EOS)
930 CHECK(err != OK);
931
Chong Zhang7137ec72014-11-12 16:41:05 -0800932 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800933 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800934 }
935
Chong Zhang7137ec72014-11-12 16:41:05 -0800936 dropAccessUnit = false;
Hassan Shojania62dec952017-05-18 10:45:27 -0700937 if (!mIsAudio && !mIsEncrypted) {
938 // Extra safeguard if higher-level behavior changes. Otherwise, not required now.
939 // Preventing the buffer from being processed (and sent to codec) if this is a later
940 // round of playback but this time without prepareDrm. Or if there is a race between
941 // stop (which is not blocking) and releaseDrm allowing buffers being processed after
942 // Crypto has been released (GenericSource currently prevents this race though).
943 // Particularly doing this check before IsAVCReferenceFrame call to prevent parsing
944 // of encrypted data.
945 if (mIsEncryptedObservedEarlier) {
946 ALOGE("fetchInputData: mismatched mIsEncrypted/mIsEncryptedObservedEarlier (0/1)");
947
948 return INVALID_OPERATION;
949 }
950
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700951 int32_t layerId = 0;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700952 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800953 if (mRenderer->getVideoLateByUs() > 100000LL
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700954 && mIsVideoAVC
955 && !IsAVCReferenceFrame(accessUnit)) {
956 dropAccessUnit = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700957 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700958 // Add only one layer each time.
959 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
960 || layerId >= mNumVideoTemporalLayerAllowed) {
961 dropAccessUnit = true;
962 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
963 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
964 mCurrentMaxVideoTemporalLayerId);
965 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
966 mCurrentMaxVideoTemporalLayerId = layerId;
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700967 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1
968 && IsIDR(accessUnit->data(), accessUnit->size())) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700969 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700970 }
971 }
972 if (dropAccessUnit) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700973 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
974 mCurrentMaxVideoTemporalLayerId = layerId - 1;
975 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700976 ++mNumInputFramesDropped;
977 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800978 }
979 } while (dropAccessUnit);
980
981 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
982#if 0
983 int64_t mediaTimeUs;
984 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700985 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800986 mIsAudio ? "audio" : "video",
987 mediaTimeUs / 1E6);
988#endif
989
990 if (mCCDecoder != NULL) {
991 mCCDecoder->decode(accessUnit);
992 }
993
994 reply->setBuffer("buffer", accessUnit);
995
996 return OK;
997}
998
999bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Wei Jiafcd87872017-07-28 15:50:04 -07001000 if (mCodec == NULL) {
1001 ALOGE("[%s] onInputBufferFetched without a valid codec", mComponentName.c_str());
1002 handleError(NO_INIT);
1003 return false;
1004 }
1005
Lajos Molnar1cd13982014-01-17 15:12:51 -08001006 size_t bufferIx;
1007 CHECK(msg->findSize("buffer-ix", &bufferIx));
1008 CHECK_LT(bufferIx, mInputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001009 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
Lajos Molnar1cd13982014-01-17 15:12:51 -08001010
1011 sp<ABuffer> buffer;
1012 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001013 bool needsCopy = true;
Lajos Molnar09524832014-07-17 14:29:51 -07001014
Lajos Molnar1cd13982014-01-17 15:12:51 -08001015 if (buffer == NULL /* includes !hasBuffer */) {
1016 int32_t streamErr = ERROR_END_OF_STREAM;
1017 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
1018
Chong Zhang66704af2015-03-03 19:32:35 -08001019 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001020
1021 // attempt to queue EOS
1022 status_t err = mCodec->queueInputBuffer(
1023 bufferIx,
1024 0,
1025 0,
1026 0,
1027 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001028 if (err == OK) {
1029 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
1030 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001031 streamErr = err;
1032 // err will not be ERROR_END_OF_STREAM
1033 }
1034
1035 if (streamErr != ERROR_END_OF_STREAM) {
Wei Jiafcd87872017-07-28 15:50:04 -07001036 ALOGE("Stream error for [%s] (err=%d), EOS %s queued",
Andy Hungcf31f1e2014-09-23 14:59:01 -07001037 mComponentName.c_str(),
1038 streamErr,
1039 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -08001040 handleError(streamErr);
1041 }
1042 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001043 sp<AMessage> extra;
1044 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
1045 int64_t resumeAtMediaTimeUs;
1046 if (extra->findInt64(
1047 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -08001048 ALOGV("[%s] suppressing rendering until %lld us",
Wei Jiac6cfd702014-11-11 16:33:20 -08001049 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
1050 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
1051 }
1052 }
1053
Lajos Molnar1cd13982014-01-17 15:12:51 -08001054 int64_t timeUs = 0;
1055 uint32_t flags = 0;
1056 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1057
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09001058 int32_t eos, csd, cvo;
Lajos Molnar87603c02014-08-20 19:25:30 -07001059 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -08001060 if (buffer->meta()->findInt32("eos", &eos) && eos) {
1061 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -07001062 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
1063 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001064 }
1065
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09001066 if (buffer->meta()->findInt32("cvo", (int32_t*)&cvo)) {
1067 ALOGV("[%s] cvo(%d) found at %lld us", mComponentName.c_str(), cvo, (long long)timeUs);
1068 switch (cvo) {
1069 case 0:
1070 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_0);
1071 break;
1072 case 1:
1073 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_90);
1074 break;
1075 case 2:
1076 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_180);
1077 break;
1078 case 3:
1079 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_270);
1080 break;
1081 }
1082 }
1083
Hassan Shojaniacefac142017-02-06 21:02:02 -08001084 // Modular DRM
Dongwon Kang1889c3e2018-02-01 13:44:57 -08001085 MediaBufferBase *mediaBuf = NULL;
Hassan Shojaniacefac142017-02-06 21:02:02 -08001086 NuPlayerDrm::CryptoInfo *cryptInfo = NULL;
1087
Lajos Molnar1cd13982014-01-17 15:12:51 -08001088 // copy into codec buffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001089 if (needsCopy) {
Wei Jia56097a82016-01-07 16:03:03 -08001090 if (buffer->size() > codecBuffer->capacity()) {
1091 handleError(ERROR_BUFFER_TOO_SMALL);
1092 mDequeuedInputBuffers.push_back(bufferIx);
1093 return false;
1094 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001095
Hassan Shojaniacefac142017-02-06 21:02:02 -08001096 if (buffer->data() != NULL) {
1097 codecBuffer->setRange(0, buffer->size());
1098 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
1099 } else { // No buffer->data()
1100 //Modular DRM
Dongwon Kangbc8f53b2018-01-25 17:01:44 -08001101 sp<RefBase> holder;
1102 if (buffer->meta()->findObject("mediaBufferHolder", &holder)) {
1103 mediaBuf = (holder != nullptr) ?
1104 static_cast<MediaBufferHolder*>(holder.get())->mediaBuffer() : nullptr;
1105 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001106 if (mediaBuf != NULL) {
Wei Jia9b5a7ad2018-07-27 17:33:24 -07001107 if (mediaBuf->size() > codecBuffer->capacity()) {
1108 handleError(ERROR_BUFFER_TOO_SMALL);
1109 mDequeuedInputBuffers.push_back(bufferIx);
1110 return false;
1111 }
1112
Hassan Shojaniacefac142017-02-06 21:02:02 -08001113 codecBuffer->setRange(0, mediaBuf->size());
1114 memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->size());
1115
Marco Nelissen3d21ae32018-02-16 08:24:08 -08001116 MetaDataBase &meta_data = mediaBuf->meta_data();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001117 cryptInfo = NuPlayerDrm::getSampleCryptoInfo(meta_data);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001118 } else { // No mediaBuf
1119 ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p",
1120 buffer.get());
1121 handleError(UNKNOWN_ERROR);
1122 return false;
1123 }
1124 } // buffer->data()
1125 } // needsCopy
1126
1127 status_t err;
1128 AString errorDetailMsg;
1129 if (cryptInfo != NULL) {
1130 err = mCodec->queueSecureInputBuffer(
1131 bufferIx,
1132 codecBuffer->offset(),
1133 cryptInfo->subSamples,
1134 cryptInfo->numSubSamples,
1135 cryptInfo->key,
1136 cryptInfo->iv,
1137 cryptInfo->mode,
1138 cryptInfo->pattern,
1139 timeUs,
1140 flags,
1141 &errorDetailMsg);
1142 // synchronous call so done with cryptInfo here
1143 free(cryptInfo);
1144 } else {
1145 err = mCodec->queueInputBuffer(
1146 bufferIx,
1147 codecBuffer->offset(),
1148 codecBuffer->size(),
1149 timeUs,
1150 flags,
1151 &errorDetailMsg);
1152 } // no cryptInfo
1153
Lajos Molnar1cd13982014-01-17 15:12:51 -08001154 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001155 ALOGE("onInputBufferFetched: queue%sInputBuffer failed for [%s] (err=%d, %s)",
Hassan Shojaniacefac142017-02-06 21:02:02 -08001156 (cryptInfo != NULL ? "Secure" : ""),
1157 mComponentName.c_str(), err, errorDetailMsg.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -08001158 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001159 } else {
1160 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
Lajos Molnar09524832014-07-17 14:29:51 -07001161 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001162
1163 } // buffer != NULL
Wei Jia2245fc62014-10-02 15:12:25 -07001164 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001165}
1166
Lajos Molnar1cd13982014-01-17 15:12:51 -08001167void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
1168 status_t err;
1169 int32_t render;
1170 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -08001171 int32_t eos;
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001172 size_t size;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001173 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -08001174
Chong Zhang7137ec72014-11-12 16:41:05 -08001175 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001176 int64_t timeUs;
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001177 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
Wei Jiac6cfd702014-11-11 16:33:20 -08001178 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -08001179
1180 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1181 mCCDecoder->display(timeUs);
1182 }
Wei Jiac6cfd702014-11-11 16:33:20 -08001183 }
1184
Wei Jiafcd87872017-07-28 15:50:04 -07001185 if (mCodec == NULL) {
1186 err = NO_INIT;
1187 } else if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -07001188 int64_t timestampNs;
1189 CHECK(msg->findInt64("timestampNs", &timestampNs));
1190 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001191 } else {
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001192 if (!msg->findInt32("eos", &eos) || !eos ||
1193 !msg->findSize("size", &size) || size) {
1194 mNumOutputFramesDropped += !mIsAudio;
1195 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001196 err = mCodec->releaseOutputBuffer(bufferIx);
1197 }
1198 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001199 ALOGE("failed to release output buffer for [%s] (err=%d)",
Lajos Molnar1cd13982014-01-17 15:12:51 -08001200 mComponentName.c_str(), err);
1201 handleError(err);
1202 }
Chong Zhang66704af2015-03-03 19:32:35 -08001203 if (msg->findInt32("eos", &eos) && eos
1204 && isDiscontinuityPending()) {
1205 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1206 }
1207}
1208
1209bool NuPlayer::Decoder::isDiscontinuityPending() const {
1210 return mFormatChangePending || mTimeChangePending;
1211}
1212
1213void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1214 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1215 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1216
1217 // If we have format change, pause and wait to be killed;
1218 // If we have time change only, flush and restart fetching.
1219
1220 if (mFormatChangePending) {
1221 mPaused = true;
1222 } else if (mTimeChangePending) {
1223 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -07001224 doFlush(false /* notifyComplete */);
1225 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -08001226 }
Chong Zhang66704af2015-03-03 19:32:35 -08001227 }
1228
1229 // Notify NuPlayer to either shutdown decoder, or rescan sources
1230 sp<AMessage> msg = mNotify->dup();
1231 msg->setInt32("what", kWhatInputDiscontinuity);
1232 msg->setInt32("formatChange", mFormatChangePending);
1233 msg->post();
1234
1235 mFormatChangePending = false;
1236 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001237}
1238
Chong Zhang7137ec72014-11-12 16:41:05 -08001239bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
1240 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001241 if (targetFormat == NULL) {
1242 return true;
1243 }
1244
1245 AString mime;
1246 if (!targetFormat->findString("mime", &mime)) {
1247 return false;
1248 }
1249
1250 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1251 // field-by-field comparison
1252 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1253 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1254 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001255 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -08001256 !targetFormat->findInt32(keys[i], &newVal) ||
1257 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001258 return false;
1259 }
1260 }
1261
1262 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001263 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -08001264 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001265 if (oldBuf->size() != newBuf->size()) {
1266 return false;
1267 }
1268 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1269 }
1270 }
1271 return false;
1272}
1273
1274bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001275 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001276 return false;
1277 }
1278
1279 if (targetFormat == NULL) {
1280 return true;
1281 }
1282
1283 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001284 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001285 || !targetFormat->findString("mime", &newMime)
1286 || !(oldMime == newMime)) {
1287 return false;
1288 }
1289
1290 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1291 bool seamless;
1292 if (audio) {
1293 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1294 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001295 int32_t isAdaptive;
1296 seamless = (mCodec != NULL &&
1297 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1298 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001299 }
1300
1301 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1302 return seamless;
1303}
1304
Chong Zhang7137ec72014-11-12 16:41:05 -08001305void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1306 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001307 return;
1308 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001309 mCSDsForCurrentFormat.clear();
1310 for (int32_t i = 0; ; ++i) {
1311 AString tag = "csd-";
1312 tag.append(i);
1313 sp<ABuffer> buffer;
1314 if (!format->findBuffer(tag.c_str(), &buffer)) {
1315 break;
1316 }
1317 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001318 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001319}
1320
Chong Zhangf8d71772014-11-26 15:08:34 -08001321void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1322 if (mResumePending) {
1323 mResumePending = false;
1324
1325 sp<AMessage> notify = mNotify->dup();
1326 notify->setInt32("what", kWhatResumeCompleted);
1327 notify->post();
1328 }
1329}
1330
Andreas Huberf9334412010-12-15 15:17:42 -08001331} // namespace android
1332