blob: f4143da3b4e3281a1f23f11a4c36cda05c5c14c8 [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 }
guochuang484a7ed2023-05-23 21:32:09 +0800462
463 int32_t videoScalingMode;
464 if (params->findInt32("android._video-scaling", &videoScalingMode)
465 && mCodec != NULL) {
466 sp<AMessage> codecParams = new AMessage();
467 codecParams->setInt32("android._video-scaling", videoScalingMode);
468 mCodec->setParameters(codecParams);
469 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700470}
471
Chong Zhang7137ec72014-11-12 16:41:05 -0800472void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800473 mRenderer = renderer;
Chong Zhang7137ec72014-11-12 16:41:05 -0800474}
475
Chong Zhangf8d71772014-11-26 15:08:34 -0800476void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800477 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800478
479 if (notifyComplete) {
480 mResumePending = true;
481 }
Wei Jiafcd87872017-07-28 15:50:04 -0700482
483 if (mCodec == NULL) {
484 ALOGE("[%s] onResume without a valid codec", mComponentName.c_str());
485 handleError(NO_INIT);
486 return;
487 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700488 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800489}
490
Chong Zhang66704af2015-03-03 19:32:35 -0800491void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800492 if (mCCDecoder != NULL) {
493 mCCDecoder->flush();
494 }
495
496 if (mRenderer != NULL) {
497 mRenderer->flush(mIsAudio, notifyComplete);
498 mRenderer->signalTimeDiscontinuity();
499 }
500
501 status_t err = OK;
502 if (mCodec != NULL) {
503 err = mCodec->flush();
504 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
505 ++mBufferGeneration;
506 }
507
508 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700509 ALOGE("failed to flush [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800510 handleError(err);
511 // finish with posting kWhatFlushCompleted.
512 // we attempt to release the buffers even if flush fails.
513 }
514 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700515 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800516}
Chong Zhang7137ec72014-11-12 16:41:05 -0800517
Marco Nelissen421f47c2015-03-25 14:40:32 -0700518
Chong Zhang66704af2015-03-03 19:32:35 -0800519void NuPlayer::Decoder::onFlush() {
520 doFlush(true);
521
522 if (isDiscontinuityPending()) {
523 // This could happen if the client starts seeking/shutdown
524 // after we queued an EOS for discontinuities.
525 // We can consider discontinuity handled.
526 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800527 }
Chong Zhang66704af2015-03-03 19:32:35 -0800528
529 sp<AMessage> notify = mNotify->dup();
530 notify->setInt32("what", kWhatFlushCompleted);
531 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800532}
533
534void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
535 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800536
537 // if there is a pending resume request, notify complete now
538 notifyResumeCompleteIfNecessary();
539
Chong Zhang7137ec72014-11-12 16:41:05 -0800540 if (mCodec != NULL) {
541 err = mCodec->release();
542 mCodec = NULL;
543 ++mBufferGeneration;
544
Lajos Molnar1de1e252015-04-30 18:18:34 -0700545 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800546 // reconnect to surface as MediaCodec disconnected from it
Chong Zhang181fd9b2017-02-16 15:53:03 -0800547 status_t error = nativeWindowConnect(mSurface.get(), "onShutdown");
Chong Zhang7137ec72014-11-12 16:41:05 -0800548 ALOGW_IF(error != NO_ERROR,
549 "[%s] failed to connect to native window, error=%d",
550 mComponentName.c_str(), error);
551 }
552 mComponentName = "decoder";
553 }
554
555 releaseAndResetMediaBuffers();
556
557 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700558 ALOGE("failed to release [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800559 handleError(err);
560 // finish with posting kWhatShutdownCompleted.
561 }
562
563 if (notifyComplete) {
564 sp<AMessage> notify = mNotify->dup();
565 notify->setInt32("what", kWhatShutdownCompleted);
566 notify->post();
567 mPaused = true;
568 }
569}
570
Chong Zhang3b032b32015-04-17 15:49:06 -0700571/*
572 * returns true if we should request more data
573 */
574bool NuPlayer::Decoder::doRequestBuffers() {
Jeff Tinker29b7dcf2016-10-24 10:28:30 -0700575 if (isDiscontinuityPending()) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700576 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800577 }
578 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800579 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800580 size_t bufferIx = *mDequeuedInputBuffers.begin();
581 sp<AMessage> msg = new AMessage();
582 msg->setSize("buffer-ix", bufferIx);
583 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800584 if (err != OK && err != ERROR_END_OF_STREAM) {
585 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800586 break;
587 }
588 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
589
590 if (!mPendingInputMessages.empty()
591 || !onInputBufferFetched(msg)) {
592 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700593 }
594 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800595
Chong Zhang3b032b32015-04-17 15:49:06 -0700596 return err == -EWOULDBLOCK
597 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700598}
599
Marco Nelissen421f47c2015-03-25 14:40:32 -0700600void NuPlayer::Decoder::handleError(int32_t err)
601{
602 // We cannot immediately release the codec due to buffers still outstanding
603 // in the renderer. We signal to the player the error so it can shutdown/release the
604 // decoder after flushing and increment the generation to discard unnecessary messages.
605
606 ++mBufferGeneration;
607
608 sp<AMessage> notify = mNotify->dup();
609 notify->setInt32("what", kWhatError);
610 notify->setInt32("err", err);
611 notify->post();
612}
613
Hassan Shojaniacefac142017-02-06 21:02:02 -0800614status_t NuPlayer::Decoder::releaseCrypto()
615{
616 ALOGV("releaseCrypto");
617
618 sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this);
619
620 sp<AMessage> response;
621 status_t status = msg->postAndAwaitResponse(&response);
622 if (status == OK && response != NULL) {
623 CHECK(response->findInt32("status", &status));
624 ALOGV("releaseCrypto ret: %d ", status);
625 } else {
626 ALOGE("releaseCrypto err: %d", status);
627 }
628
629 return status;
630}
631
632void NuPlayer::Decoder::onReleaseCrypto(const sp<AMessage>& msg)
633{
634 status_t status = INVALID_OPERATION;
635 if (mCodec != NULL) {
636 status = mCodec->releaseCrypto();
637 } else {
638 // returning OK if the codec has been already released
639 status = OK;
640 ALOGE("onReleaseCrypto No mCodec. err: %d", status);
641 }
642
643 sp<AMessage> response = new AMessage;
644 response->setInt32("status", status);
Hassan Shojania62dec952017-05-18 10:45:27 -0700645 // Clearing the state as it's tied to crypto. mIsEncryptedObservedEarlier is sticky though
646 // and lasts for the lifetime of this codec. See its use in fetchInputData.
647 mIsEncrypted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -0800648
649 sp<AReplyToken> replyID;
650 CHECK(msg->senderAwaitsResponse(&replyID));
651 response->postReply(replyID);
652}
653
Marco Nelissen421f47c2015-03-25 14:40:32 -0700654bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800655 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800656 return false;
657 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700658
Wei Jiafcd87872017-07-28 15:50:04 -0700659 if (mCodec == NULL) {
660 ALOGE("[%s] handleAnInputBuffer without a valid codec", mComponentName.c_str());
661 handleError(NO_INIT);
662 return false;
663 }
664
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900665 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700666 mCodec->getInputBuffer(index, &buffer);
667
Wei Jia6301a5e2015-05-13 13:15:18 -0700668 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700669 ALOGE("[%s] handleAnInputBuffer, failed to get input buffer", mComponentName.c_str());
Wei Jia6301a5e2015-05-13 13:15:18 -0700670 handleError(UNKNOWN_ERROR);
671 return false;
672 }
673
Marco Nelissen421f47c2015-03-25 14:40:32 -0700674 if (index >= mInputBuffers.size()) {
675 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
676 mInputBuffers.add();
677 mMediaBuffers.add();
678 mInputBufferIsDequeued.add();
679 mMediaBuffers.editItemAt(i) = NULL;
680 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800681 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700682 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700683 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700684
Marco Nelissen421f47c2015-03-25 14:40:32 -0700685 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800686
Marco Nelissen421f47c2015-03-25 14:40:32 -0700687 if (mMediaBuffers[index] != NULL) {
688 mMediaBuffers[index]->release();
689 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700690 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700691 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700692
Lajos Molnar87603c02014-08-20 19:25:30 -0700693 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800694 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700695 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800696
Lajos Molnar87603c02014-08-20 19:25:30 -0700697 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800698 ALOGV("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800699 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700700 mCSDsToSubmit.removeAt(0);
Wei Jia56097a82016-01-07 16:03:03 -0800701 if (!onInputBufferFetched(msg)) {
702 handleError(UNKNOWN_ERROR);
703 return false;
704 }
Wei Jia2245fc62014-10-02 15:12:25 -0700705 return true;
706 }
707
708 while (!mPendingInputMessages.empty()) {
709 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800710 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700711 break;
712 }
713 mPendingInputMessages.erase(mPendingInputMessages.begin());
714 }
715
Marco Nelissen421f47c2015-03-25 14:40:32 -0700716 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700717 return true;
718 }
719
Marco Nelissen421f47c2015-03-25 14:40:32 -0700720 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800721
722 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800723 return true;
724}
725
Marco Nelissen421f47c2015-03-25 14:40:32 -0700726bool NuPlayer::Decoder::handleAnOutputBuffer(
727 size_t index,
728 size_t offset,
729 size_t size,
730 int64_t timeUs,
731 int32_t flags) {
Wei Jiafcd87872017-07-28 15:50:04 -0700732 if (mCodec == NULL) {
733 ALOGE("[%s] handleAnOutputBuffer without a valid codec", mComponentName.c_str());
734 handleError(NO_INIT);
735 return false;
736 }
737
Marco Nelissen421f47c2015-03-25 14:40:32 -0700738// CHECK_LT(bufferIx, mOutputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900739 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700740 mCodec->getOutputBuffer(index, &buffer);
741
Santhosh Behara3539def2015-10-09 17:32:58 -0700742 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700743 ALOGE("[%s] handleAnOutputBuffer, failed to get output buffer", mComponentName.c_str());
Santhosh Behara3539def2015-10-09 17:32:58 -0700744 handleError(UNKNOWN_ERROR);
745 return false;
746 }
747
Marco Nelissen421f47c2015-03-25 14:40:32 -0700748 if (index >= mOutputBuffers.size()) {
749 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
750 mOutputBuffers.add();
751 }
752 }
753
754 mOutputBuffers.editItemAt(index) = buffer;
755
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900756 int64_t frameIndex;
757 bool frameIndexFound = buffer->meta()->findInt64("frameIndex", &frameIndex);
758
Chong Zhang7137ec72014-11-12 16:41:05 -0800759 buffer->setRange(offset, size);
760 buffer->meta()->clear();
761 buffer->meta()->setInt64("timeUs", timeUs);
Byeongjo Park2eef13e2020-06-12 17:24:21 +0900762 if (frameIndexFound) {
763 buffer->meta()->setInt64("frameIndex", frameIndex);
764 }
Chong Zhang66704af2015-03-03 19:32:35 -0800765
766 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800767 // we do not expect CODECCONFIG or SYNCFRAME for decoder
768
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800769 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700770 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800771 reply->setInt32("generation", mBufferGeneration);
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -0700772 reply->setSize("size", size);
Chong Zhang7137ec72014-11-12 16:41:05 -0800773
Chong Zhang66704af2015-03-03 19:32:35 -0800774 if (eos) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800775 ALOGV("[%s] saw output EOS", mIsAudio ? "audio" : "video");
Chong Zhang66704af2015-03-03 19:32:35 -0800776
777 buffer->meta()->setInt32("eos", true);
778 reply->setInt32("eos", true);
Wei Jiaaec8d822017-08-25 15:27:57 -0700779 }
780
Ray Essickc6e9f6e2017-12-07 16:57:36 -0800781 mNumFramesTotal += !mIsAudio;
782
Wei Jiaaec8d822017-08-25 15:27:57 -0700783 if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800784 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
785 ALOGV("[%s] dropping buffer at time %lld as requested.",
786 mComponentName.c_str(), (long long)timeUs);
787
788 reply->post();
Wei Jiaaec8d822017-08-25 15:27:57 -0700789 if (eos) {
790 notifyResumeCompleteIfNecessary();
791 if (mRenderer != NULL && !isDiscontinuityPending()) {
792 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
793 }
794 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800795 return true;
796 }
797
798 mSkipRenderingUntilMediaTimeUs = -1;
799 }
800
Chong Zhangf8d71772014-11-26 15:08:34 -0800801 // wait until 1st frame comes out to signal resume complete
802 notifyResumeCompleteIfNecessary();
803
Chong Zhang7137ec72014-11-12 16:41:05 -0800804 if (mRenderer != NULL) {
805 // send the buffer to renderer.
806 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800807 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800808 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
809 }
810 }
811
812 return true;
813}
814
Marco Nelissen421f47c2015-03-25 14:40:32 -0700815void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
816 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700817 int32_t width, height;
818 if (format->findInt32("width", &width)
819 && format->findInt32("height", &height)) {
Ray Essick83f56b02019-06-23 15:13:46 -0700820 Mutex::Autolock autolock(mStatsLock);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700821 mStats->setInt32("width", width);
822 mStats->setInt32("height", height);
823 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700824 sp<AMessage> notify = mNotify->dup();
825 notify->setInt32("what", kWhatVideoSizeChanged);
826 notify->setMessage("format", format);
827 notify->post();
828 } else if (mRenderer != NULL) {
829 uint32_t flags;
830 int64_t durationUs;
831 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700832 if (getAudioDeepBufferSetting() // override regardless of source duration
Andy Hung71c8e5c2017-04-03 15:50:27 -0700833 || (mSource->getDuration(&durationUs) == OK
Andy Hung288da022015-05-31 22:55:59 -0700834 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700835 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
836 } else {
837 flags = AUDIO_OUTPUT_FLAG_NONE;
838 }
839
Wei Jia9a3101b2016-11-08 14:34:24 -0800840 sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this);
841 reply->setInt32("generation", mBufferGeneration);
842 mRenderer->changeAudioFormat(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +0530843 format, false /* offloadOnly */, hasVideo,
844 flags, mSource->isStreaming(), reply);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700845 }
846}
847
Chong Zhang7137ec72014-11-12 16:41:05 -0800848void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
849 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
850 if (mMediaBuffers[i] != NULL) {
851 mMediaBuffers[i]->release();
852 mMediaBuffers.editItemAt(i) = NULL;
853 }
854 }
855 mMediaBuffers.resize(mInputBuffers.size());
856 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
857 mMediaBuffers.editItemAt(i) = NULL;
858 }
859 mInputBufferIsDequeued.clear();
860 mInputBufferIsDequeued.resize(mInputBuffers.size());
861 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
862 mInputBufferIsDequeued.editItemAt(i) = false;
863 }
864
865 mPendingInputMessages.clear();
866 mDequeuedInputBuffers.clear();
867 mSkipRenderingUntilMediaTimeUs = -1;
868}
869
870void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800871 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800872 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800873 reply->setInt32("generation", mBufferGeneration);
874 mCodec->requestActivityNotification(reply);
875 }
876}
877
878bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
879 int32_t generation;
880 CHECK(msg->findInt32("generation", &generation));
881 return generation != mBufferGeneration;
882}
883
884status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
885 sp<ABuffer> accessUnit;
Robert Shih59e9ca72016-10-20 13:51:42 -0700886 bool dropAccessUnit = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800887 do {
888 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
889
890 if (err == -EWOULDBLOCK) {
891 return err;
892 } else if (err != OK) {
893 if (err == INFO_DISCONTINUITY) {
894 int32_t type;
895 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
896
897 bool formatChange =
898 (mIsAudio &&
899 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
900 || (!mIsAudio &&
901 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
902
903 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
904
905 ALOGI("%s discontinuity (format=%d, time=%d)",
906 mIsAudio ? "audio" : "video", formatChange, timeChange);
907
908 bool seamlessFormatChange = false;
909 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
910 if (formatChange) {
911 seamlessFormatChange =
912 supportsSeamlessFormatChange(newFormat);
913 // treat seamless format change separately
914 formatChange = !seamlessFormatChange;
915 }
916
Chong Zhang66704af2015-03-03 19:32:35 -0800917 // For format or time change, return EOS to queue EOS input,
918 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800919 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800920 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800921 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800922 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800923 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800924 mTimeChangePending = true;
925 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800926 } else if (seamlessFormatChange) {
927 // reuse existing decoder and don't flush
928 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800929 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800930 } else {
931 // This stream is unaffected by the discontinuity
932 return -EWOULDBLOCK;
933 }
934 }
935
Chong Zhang66704af2015-03-03 19:32:35 -0800936 // reply should only be returned without a buffer set
937 // when there is an error (including EOS)
938 CHECK(err != OK);
939
Chong Zhang7137ec72014-11-12 16:41:05 -0800940 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800941 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800942 }
943
Chong Zhang7137ec72014-11-12 16:41:05 -0800944 dropAccessUnit = false;
Hassan Shojania62dec952017-05-18 10:45:27 -0700945 if (!mIsAudio && !mIsEncrypted) {
946 // Extra safeguard if higher-level behavior changes. Otherwise, not required now.
947 // Preventing the buffer from being processed (and sent to codec) if this is a later
948 // round of playback but this time without prepareDrm. Or if there is a race between
949 // stop (which is not blocking) and releaseDrm allowing buffers being processed after
950 // Crypto has been released (GenericSource currently prevents this race though).
951 // Particularly doing this check before IsAVCReferenceFrame call to prevent parsing
952 // of encrypted data.
953 if (mIsEncryptedObservedEarlier) {
954 ALOGE("fetchInputData: mismatched mIsEncrypted/mIsEncryptedObservedEarlier (0/1)");
955
956 return INVALID_OPERATION;
957 }
958
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700959 int32_t layerId = 0;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700960 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800961 if (mRenderer->getVideoLateByUs() > 100000LL
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700962 && mIsVideoAVC
963 && !IsAVCReferenceFrame(accessUnit)) {
964 dropAccessUnit = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700965 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700966 // Add only one layer each time.
967 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
968 || layerId >= mNumVideoTemporalLayerAllowed) {
969 dropAccessUnit = true;
970 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
971 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
972 mCurrentMaxVideoTemporalLayerId);
973 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
974 mCurrentMaxVideoTemporalLayerId = layerId;
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700975 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1
976 && IsIDR(accessUnit->data(), accessUnit->size())) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700977 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700978 }
979 }
980 if (dropAccessUnit) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700981 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
982 mCurrentMaxVideoTemporalLayerId = layerId - 1;
983 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700984 ++mNumInputFramesDropped;
985 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800986 }
987 } while (dropAccessUnit);
988
989 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
990#if 0
991 int64_t mediaTimeUs;
992 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700993 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800994 mIsAudio ? "audio" : "video",
995 mediaTimeUs / 1E6);
996#endif
997
998 if (mCCDecoder != NULL) {
999 mCCDecoder->decode(accessUnit);
1000 }
1001
1002 reply->setBuffer("buffer", accessUnit);
1003
1004 return OK;
1005}
1006
1007bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Wei Jiafcd87872017-07-28 15:50:04 -07001008 if (mCodec == NULL) {
1009 ALOGE("[%s] onInputBufferFetched without a valid codec", mComponentName.c_str());
1010 handleError(NO_INIT);
1011 return false;
1012 }
1013
Lajos Molnar1cd13982014-01-17 15:12:51 -08001014 size_t bufferIx;
1015 CHECK(msg->findSize("buffer-ix", &bufferIx));
1016 CHECK_LT(bufferIx, mInputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001017 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
Lajos Molnar1cd13982014-01-17 15:12:51 -08001018
1019 sp<ABuffer> buffer;
1020 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001021 bool needsCopy = true;
Lajos Molnar09524832014-07-17 14:29:51 -07001022
Lajos Molnar1cd13982014-01-17 15:12:51 -08001023 if (buffer == NULL /* includes !hasBuffer */) {
1024 int32_t streamErr = ERROR_END_OF_STREAM;
1025 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
1026
Chong Zhang66704af2015-03-03 19:32:35 -08001027 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001028
1029 // attempt to queue EOS
1030 status_t err = mCodec->queueInputBuffer(
1031 bufferIx,
1032 0,
1033 0,
1034 0,
1035 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001036 if (err == OK) {
1037 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
1038 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001039 streamErr = err;
1040 // err will not be ERROR_END_OF_STREAM
1041 }
1042
1043 if (streamErr != ERROR_END_OF_STREAM) {
Wei Jiafcd87872017-07-28 15:50:04 -07001044 ALOGE("Stream error for [%s] (err=%d), EOS %s queued",
Andy Hungcf31f1e2014-09-23 14:59:01 -07001045 mComponentName.c_str(),
1046 streamErr,
1047 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -08001048 handleError(streamErr);
1049 }
1050 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001051 sp<AMessage> extra;
1052 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
1053 int64_t resumeAtMediaTimeUs;
1054 if (extra->findInt64(
1055 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -08001056 ALOGV("[%s] suppressing rendering until %lld us",
Wei Jiac6cfd702014-11-11 16:33:20 -08001057 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
1058 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
1059 }
1060 }
1061
Lajos Molnar1cd13982014-01-17 15:12:51 -08001062 int64_t timeUs = 0;
1063 uint32_t flags = 0;
1064 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1065
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09001066 int32_t eos, csd, cvo;
Lajos Molnar87603c02014-08-20 19:25:30 -07001067 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -08001068 if (buffer->meta()->findInt32("eos", &eos) && eos) {
1069 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -07001070 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
1071 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001072 }
1073
Byeongjo Parkb0225aa2018-04-20 14:00:15 +09001074 if (buffer->meta()->findInt32("cvo", (int32_t*)&cvo)) {
1075 ALOGV("[%s] cvo(%d) found at %lld us", mComponentName.c_str(), cvo, (long long)timeUs);
1076 switch (cvo) {
1077 case 0:
1078 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_0);
1079 break;
1080 case 1:
1081 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_90);
1082 break;
1083 case 2:
1084 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_180);
1085 break;
1086 case 3:
1087 codecBuffer->meta()->setInt32("cvo", MediaCodec::CVO_DEGREE_270);
1088 break;
1089 }
1090 }
1091
Hassan Shojaniacefac142017-02-06 21:02:02 -08001092 // Modular DRM
Dongwon Kang1889c3e2018-02-01 13:44:57 -08001093 MediaBufferBase *mediaBuf = NULL;
Hassan Shojaniacefac142017-02-06 21:02:02 -08001094 NuPlayerDrm::CryptoInfo *cryptInfo = NULL;
1095
Lajos Molnar1cd13982014-01-17 15:12:51 -08001096 // copy into codec buffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001097 if (needsCopy) {
Wei Jia56097a82016-01-07 16:03:03 -08001098 if (buffer->size() > codecBuffer->capacity()) {
1099 handleError(ERROR_BUFFER_TOO_SMALL);
1100 mDequeuedInputBuffers.push_back(bufferIx);
1101 return false;
1102 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001103
Hassan Shojaniacefac142017-02-06 21:02:02 -08001104 if (buffer->data() != NULL) {
1105 codecBuffer->setRange(0, buffer->size());
1106 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
1107 } else { // No buffer->data()
1108 //Modular DRM
Dongwon Kangbc8f53b2018-01-25 17:01:44 -08001109 sp<RefBase> holder;
1110 if (buffer->meta()->findObject("mediaBufferHolder", &holder)) {
1111 mediaBuf = (holder != nullptr) ?
1112 static_cast<MediaBufferHolder*>(holder.get())->mediaBuffer() : nullptr;
1113 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001114 if (mediaBuf != NULL) {
Jeffrey Kardatzkefa07e2a2023-03-21 11:38:33 -07001115 if (mediaBuf->range_length() > codecBuffer->capacity()) {
Wei Jia9b5a7ad2018-07-27 17:33:24 -07001116 handleError(ERROR_BUFFER_TOO_SMALL);
1117 mDequeuedInputBuffers.push_back(bufferIx);
1118 return false;
1119 }
1120
Jeffrey Kardatzkefa07e2a2023-03-21 11:38:33 -07001121 codecBuffer->setRange(0, mediaBuf->range_length());
1122 memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->range_length());
Hassan Shojaniacefac142017-02-06 21:02:02 -08001123
Marco Nelissen3d21ae32018-02-16 08:24:08 -08001124 MetaDataBase &meta_data = mediaBuf->meta_data();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001125 cryptInfo = NuPlayerDrm::getSampleCryptoInfo(meta_data);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001126 } else { // No mediaBuf
1127 ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p",
1128 buffer.get());
1129 handleError(UNKNOWN_ERROR);
1130 return false;
1131 }
1132 } // buffer->data()
1133 } // needsCopy
1134
1135 status_t err;
1136 AString errorDetailMsg;
1137 if (cryptInfo != NULL) {
1138 err = mCodec->queueSecureInputBuffer(
1139 bufferIx,
1140 codecBuffer->offset(),
1141 cryptInfo->subSamples,
1142 cryptInfo->numSubSamples,
1143 cryptInfo->key,
1144 cryptInfo->iv,
1145 cryptInfo->mode,
1146 cryptInfo->pattern,
1147 timeUs,
1148 flags,
1149 &errorDetailMsg);
1150 // synchronous call so done with cryptInfo here
1151 free(cryptInfo);
1152 } else {
1153 err = mCodec->queueInputBuffer(
1154 bufferIx,
1155 codecBuffer->offset(),
1156 codecBuffer->size(),
1157 timeUs,
1158 flags,
1159 &errorDetailMsg);
1160 } // no cryptInfo
1161
Lajos Molnar1cd13982014-01-17 15:12:51 -08001162 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001163 ALOGE("onInputBufferFetched: queue%sInputBuffer failed for [%s] (err=%d, %s)",
Hassan Shojaniacefac142017-02-06 21:02:02 -08001164 (cryptInfo != NULL ? "Secure" : ""),
1165 mComponentName.c_str(), err, errorDetailMsg.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -08001166 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001167 } else {
1168 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
Lajos Molnar09524832014-07-17 14:29:51 -07001169 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001170
1171 } // buffer != NULL
Wei Jia2245fc62014-10-02 15:12:25 -07001172 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001173}
1174
Lajos Molnar1cd13982014-01-17 15:12:51 -08001175void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
1176 status_t err;
1177 int32_t render;
1178 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -08001179 int32_t eos;
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001180 size_t size;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001181 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -08001182
Chong Zhang7137ec72014-11-12 16:41:05 -08001183 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001184 int64_t timeUs;
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001185 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
Wei Jiac6cfd702014-11-11 16:33:20 -08001186 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -08001187
1188 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1189 mCCDecoder->display(timeUs);
1190 }
Wei Jiac6cfd702014-11-11 16:33:20 -08001191 }
1192
Wei Jiafcd87872017-07-28 15:50:04 -07001193 if (mCodec == NULL) {
1194 err = NO_INIT;
1195 } else if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -07001196 int64_t timestampNs;
1197 CHECK(msg->findInt64("timestampNs", &timestampNs));
1198 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001199 } else {
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001200 if (!msg->findInt32("eos", &eos) || !eos ||
1201 !msg->findSize("size", &size) || size) {
1202 mNumOutputFramesDropped += !mIsAudio;
1203 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001204 err = mCodec->releaseOutputBuffer(bufferIx);
1205 }
1206 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001207 ALOGE("failed to release output buffer for [%s] (err=%d)",
Lajos Molnar1cd13982014-01-17 15:12:51 -08001208 mComponentName.c_str(), err);
1209 handleError(err);
1210 }
Chong Zhang66704af2015-03-03 19:32:35 -08001211 if (msg->findInt32("eos", &eos) && eos
1212 && isDiscontinuityPending()) {
1213 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1214 }
1215}
1216
1217bool NuPlayer::Decoder::isDiscontinuityPending() const {
1218 return mFormatChangePending || mTimeChangePending;
1219}
1220
1221void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1222 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1223 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1224
1225 // If we have format change, pause and wait to be killed;
1226 // If we have time change only, flush and restart fetching.
1227
1228 if (mFormatChangePending) {
1229 mPaused = true;
1230 } else if (mTimeChangePending) {
1231 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -07001232 doFlush(false /* notifyComplete */);
1233 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -08001234 }
Chong Zhang66704af2015-03-03 19:32:35 -08001235 }
1236
1237 // Notify NuPlayer to either shutdown decoder, or rescan sources
1238 sp<AMessage> msg = mNotify->dup();
1239 msg->setInt32("what", kWhatInputDiscontinuity);
1240 msg->setInt32("formatChange", mFormatChangePending);
1241 msg->post();
1242
1243 mFormatChangePending = false;
1244 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001245}
1246
Chong Zhang7137ec72014-11-12 16:41:05 -08001247bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
1248 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001249 if (targetFormat == NULL) {
1250 return true;
1251 }
1252
1253 AString mime;
1254 if (!targetFormat->findString("mime", &mime)) {
1255 return false;
1256 }
1257
1258 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1259 // field-by-field comparison
1260 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1261 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1262 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001263 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -08001264 !targetFormat->findInt32(keys[i], &newVal) ||
1265 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001266 return false;
1267 }
1268 }
1269
1270 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001271 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -08001272 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001273 if (oldBuf->size() != newBuf->size()) {
1274 return false;
1275 }
1276 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1277 }
1278 }
1279 return false;
1280}
1281
1282bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001283 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001284 return false;
1285 }
1286
1287 if (targetFormat == NULL) {
1288 return true;
1289 }
1290
1291 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001292 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001293 || !targetFormat->findString("mime", &newMime)
1294 || !(oldMime == newMime)) {
1295 return false;
1296 }
1297
1298 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1299 bool seamless;
1300 if (audio) {
1301 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1302 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001303 int32_t isAdaptive;
1304 seamless = (mCodec != NULL &&
1305 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1306 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001307 }
1308
1309 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1310 return seamless;
1311}
1312
Chong Zhang7137ec72014-11-12 16:41:05 -08001313void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1314 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001315 return;
1316 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001317 mCSDsForCurrentFormat.clear();
1318 for (int32_t i = 0; ; ++i) {
1319 AString tag = "csd-";
1320 tag.append(i);
1321 sp<ABuffer> buffer;
1322 if (!format->findBuffer(tag.c_str(), &buffer)) {
1323 break;
1324 }
1325 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001326 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001327}
1328
Chong Zhangf8d71772014-11-26 15:08:34 -08001329void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1330 if (mResumePending) {
1331 mResumePending = false;
1332
1333 sp<AMessage> notify = mNotify->dup();
1334 notify->setInt32("what", kWhatResumeCompleted);
1335 notify->post();
1336 }
1337}
1338
Andreas Huberf9334412010-12-15 15:17:42 -08001339} // namespace android
1340