blob: 2f0da2d7830de48c0ff0f8c32be87a17ced2dcaa [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>
Lajos Molnar1cd13982014-01-17 15:12:51 -080031#include <media/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>
Lajos Molnar1de1e252015-04-30 18:18:34 -070043#include <gui/Surface.h>
44
Chong Zhang7137ec72014-11-12 16:41:05 -080045#include "ATSParser.h"
46
Andreas Huberf9334412010-12-15 15:17:42 -080047namespace android {
48
Lajos Molnar9fb81522016-07-14 07:33:43 -070049static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display
Praveen Chavanbbaa1442016-04-08 13:33:49 -070050
51// The default total video frame rate of a stream when that info is not available from
52// the source.
53static float kDefaultVideoFrameRateTotal = 30.f;
54
Andy Hung288da022015-05-31 22:55:59 -070055static inline bool getAudioDeepBufferSetting() {
56 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
57}
58
Andreas Huberf9334412010-12-15 15:17:42 -080059NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080060 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080061 const sp<Source> &source,
Ronghua Wu68845c12015-07-21 09:50:48 -070062 pid_t pid,
Wei Jiaf2ae3e12016-10-27 17:10:59 -070063 uid_t uid,
Wei Jiac6cfd702014-11-11 16:33:20 -080064 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070065 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080066 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080067 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070068 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080069 mSource(source),
70 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080071 mCCDecoder(ccDecoder),
Ronghua Wu68845c12015-07-21 09:50:48 -070072 mPid(pid),
Wei Jiaf2ae3e12016-10-27 17:10:59 -070073 mUid(uid),
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -080074 mSkipRenderingUntilMediaTimeUs(-1LL),
75 mNumFramesTotal(0LL),
76 mNumInputFramesDropped(0LL),
77 mNumOutputFramesDropped(0LL),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070078 mVideoWidth(0),
79 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080080 mIsAudio(true),
81 mIsVideoAVC(false),
82 mIsSecure(false),
Hassan Shojania62dec952017-05-18 10:45:27 -070083 mIsEncrypted(false),
84 mIsEncryptedObservedEarlier(false),
Chong Zhang7137ec72014-11-12 16:41:05 -080085 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080086 mTimeChangePending(false),
Praveen Chavanbbaa1442016-04-08 13:33:49 -070087 mFrameRateTotal(kDefaultVideoFrameRateTotal),
88 mPlaybackSpeed(1.0f),
Lajos Molnar9fb81522016-07-14 07:33:43 -070089 mNumVideoTemporalLayerTotal(1), // decode all layers
Praveen Chavanbbaa1442016-04-08 13:33:49 -070090 mNumVideoTemporalLayerAllowed(1),
91 mCurrentMaxVideoTemporalLayerId(0),
Chong Zhangf8d71772014-11-26 15:08:34 -080092 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080093 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080094 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070095 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080096 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Praveen Chavanbbaa1442016-04-08 13:33:49 -070097 mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
Andreas Huberf9334412010-12-15 15:17:42 -080098}
99
100NuPlayer::Decoder::~Decoder() {
Wei Jia37ff0e62017-04-21 11:24:45 -0700101 // Need to stop looper first since mCodec could be accessed on the mDecoderLooper.
102 stopLooper();
103 if (mCodec != NULL) {
104 mCodec->release();
105 }
Wei Jia4923cee2014-09-24 14:25:19 -0700106 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -0800107}
108
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700109sp<AMessage> NuPlayer::Decoder::getStats() const {
Ray Essick372e8f22019-02-11 11:38:31 -0800110
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
116 // i'm mutexed right now.
117 // make our own copy, so we aren't victim to any later changes.
118 sp<AMessage> copiedStats = mStats->dup();
119 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(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700304 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid, mUid);
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
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700365 mStats->setString("mime", mime.c_str());
366 mStats->setString("component-name", mComponentName.c_str());
367
368 if (!mIsAudio) {
369 int32_t width, height;
370 if (mOutputFormat->findInt32("width", &width)
371 && mOutputFormat->findInt32("height", &height)) {
372 mStats->setInt32("width", width);
373 mStats->setInt32("height", height);
374 }
375 }
376
Marco Nelissen421f47c2015-03-25 14:40:32 -0700377 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
378 mCodec->setCallback(reply);
379
Lajos Molnar1cd13982014-01-17 15:12:51 -0800380 err = mCodec->start();
381 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700382 ALOGE("Failed to start [%s] decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700383 mCodec->release();
384 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800385 handleError(err);
386 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800387 }
388
Lajos Molnar09524832014-07-17 14:29:51 -0700389 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700390
Wei Jia704e7262014-06-04 16:21:56 -0700391 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800392 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800393}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700394
Ronghua Wu8db88132015-04-22 13:51:35 -0700395void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700396 bool needAdjustLayers = false;
397 float frameRateTotal;
398 if (params->findFloat("frame-rate-total", &frameRateTotal)
399 && mFrameRateTotal != frameRateTotal) {
400 needAdjustLayers = true;
401 mFrameRateTotal = frameRateTotal;
Ronghua Wu8db88132015-04-22 13:51:35 -0700402 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700403
404 int32_t numVideoTemporalLayerTotal;
405 if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
Lajos Molnar9fb81522016-07-14 07:33:43 -0700406 && numVideoTemporalLayerTotal >= 0
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700407 && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
408 && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
409 needAdjustLayers = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700410 mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700411 }
412
Lajos Molnar9fb81522016-07-14 07:33:43 -0700413 if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700414 // TODO: For now, layer fps is calculated for some specific architectures.
415 // But it really should be extracted from the stream.
416 mVideoTemporalLayerAggregateFps[0] =
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800417 mFrameRateTotal / (float)(1LL << (mNumVideoTemporalLayerTotal - 1));
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700418 for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
419 mVideoTemporalLayerAggregateFps[i] =
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800420 mFrameRateTotal / (float)(1LL << (mNumVideoTemporalLayerTotal - i))
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700421 + mVideoTemporalLayerAggregateFps[i - 1];
422 }
423 }
424
425 float playbackSpeed;
426 if (params->findFloat("playback-speed", &playbackSpeed)
427 && mPlaybackSpeed != playbackSpeed) {
428 needAdjustLayers = true;
429 mPlaybackSpeed = playbackSpeed;
430 }
431
432 if (needAdjustLayers) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700433 float decodeFrameRate = mFrameRateTotal;
434 // enable temporal layering optimization only if we know the layering depth
435 if (mNumVideoTemporalLayerTotal > 1) {
436 int32_t layerId;
437 for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
438 if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
439 >= kDisplayRefreshingRate * 0.9) {
440 break;
441 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700442 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700443 mNumVideoTemporalLayerAllowed = layerId + 1;
444 decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700445 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700446 ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
447 mNumVideoTemporalLayerAllowed, decodeFrameRate);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700448
449 if (mCodec == NULL) {
450 ALOGW("onSetParameters called before codec is created.");
451 return;
452 }
453
454 sp<AMessage> codecParams = new AMessage();
Lajos Molnar9fb81522016-07-14 07:33:43 -0700455 codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700456 mCodec->setParameters(codecParams);
457 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700458}
459
Chong Zhang7137ec72014-11-12 16:41:05 -0800460void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800461 mRenderer = renderer;
Chong Zhang7137ec72014-11-12 16:41:05 -0800462}
463
Chong Zhangf8d71772014-11-26 15:08:34 -0800464void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800465 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800466
467 if (notifyComplete) {
468 mResumePending = true;
469 }
Wei Jiafcd87872017-07-28 15:50:04 -0700470
471 if (mCodec == NULL) {
472 ALOGE("[%s] onResume without a valid codec", mComponentName.c_str());
473 handleError(NO_INIT);
474 return;
475 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700476 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800477}
478
Chong Zhang66704af2015-03-03 19:32:35 -0800479void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800480 if (mCCDecoder != NULL) {
481 mCCDecoder->flush();
482 }
483
484 if (mRenderer != NULL) {
485 mRenderer->flush(mIsAudio, notifyComplete);
486 mRenderer->signalTimeDiscontinuity();
487 }
488
489 status_t err = OK;
490 if (mCodec != NULL) {
491 err = mCodec->flush();
492 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
493 ++mBufferGeneration;
494 }
495
496 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700497 ALOGE("failed to flush [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800498 handleError(err);
499 // finish with posting kWhatFlushCompleted.
500 // we attempt to release the buffers even if flush fails.
501 }
502 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700503 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800504}
Chong Zhang7137ec72014-11-12 16:41:05 -0800505
Marco Nelissen421f47c2015-03-25 14:40:32 -0700506
Chong Zhang66704af2015-03-03 19:32:35 -0800507void NuPlayer::Decoder::onFlush() {
508 doFlush(true);
509
510 if (isDiscontinuityPending()) {
511 // This could happen if the client starts seeking/shutdown
512 // after we queued an EOS for discontinuities.
513 // We can consider discontinuity handled.
514 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800515 }
Chong Zhang66704af2015-03-03 19:32:35 -0800516
517 sp<AMessage> notify = mNotify->dup();
518 notify->setInt32("what", kWhatFlushCompleted);
519 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800520}
521
522void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
523 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800524
525 // if there is a pending resume request, notify complete now
526 notifyResumeCompleteIfNecessary();
527
Chong Zhang7137ec72014-11-12 16:41:05 -0800528 if (mCodec != NULL) {
529 err = mCodec->release();
530 mCodec = NULL;
531 ++mBufferGeneration;
532
Lajos Molnar1de1e252015-04-30 18:18:34 -0700533 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800534 // reconnect to surface as MediaCodec disconnected from it
Chong Zhang181fd9b2017-02-16 15:53:03 -0800535 status_t error = nativeWindowConnect(mSurface.get(), "onShutdown");
Chong Zhang7137ec72014-11-12 16:41:05 -0800536 ALOGW_IF(error != NO_ERROR,
537 "[%s] failed to connect to native window, error=%d",
538 mComponentName.c_str(), error);
539 }
540 mComponentName = "decoder";
541 }
542
543 releaseAndResetMediaBuffers();
544
545 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -0700546 ALOGE("failed to release [%s] (err=%d)", mComponentName.c_str(), err);
Chong Zhang7137ec72014-11-12 16:41:05 -0800547 handleError(err);
548 // finish with posting kWhatShutdownCompleted.
549 }
550
551 if (notifyComplete) {
552 sp<AMessage> notify = mNotify->dup();
553 notify->setInt32("what", kWhatShutdownCompleted);
554 notify->post();
555 mPaused = true;
556 }
557}
558
Chong Zhang3b032b32015-04-17 15:49:06 -0700559/*
560 * returns true if we should request more data
561 */
562bool NuPlayer::Decoder::doRequestBuffers() {
Jeff Tinker29b7dcf2016-10-24 10:28:30 -0700563 if (isDiscontinuityPending()) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700564 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800565 }
566 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800567 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800568 size_t bufferIx = *mDequeuedInputBuffers.begin();
569 sp<AMessage> msg = new AMessage();
570 msg->setSize("buffer-ix", bufferIx);
571 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800572 if (err != OK && err != ERROR_END_OF_STREAM) {
573 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800574 break;
575 }
576 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
577
578 if (!mPendingInputMessages.empty()
579 || !onInputBufferFetched(msg)) {
580 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700581 }
582 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800583
Chong Zhang3b032b32015-04-17 15:49:06 -0700584 return err == -EWOULDBLOCK
585 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700586}
587
Marco Nelissen421f47c2015-03-25 14:40:32 -0700588void NuPlayer::Decoder::handleError(int32_t err)
589{
590 // We cannot immediately release the codec due to buffers still outstanding
591 // in the renderer. We signal to the player the error so it can shutdown/release the
592 // decoder after flushing and increment the generation to discard unnecessary messages.
593
594 ++mBufferGeneration;
595
596 sp<AMessage> notify = mNotify->dup();
597 notify->setInt32("what", kWhatError);
598 notify->setInt32("err", err);
599 notify->post();
600}
601
Hassan Shojaniacefac142017-02-06 21:02:02 -0800602status_t NuPlayer::Decoder::releaseCrypto()
603{
604 ALOGV("releaseCrypto");
605
606 sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this);
607
608 sp<AMessage> response;
609 status_t status = msg->postAndAwaitResponse(&response);
610 if (status == OK && response != NULL) {
611 CHECK(response->findInt32("status", &status));
612 ALOGV("releaseCrypto ret: %d ", status);
613 } else {
614 ALOGE("releaseCrypto err: %d", status);
615 }
616
617 return status;
618}
619
620void NuPlayer::Decoder::onReleaseCrypto(const sp<AMessage>& msg)
621{
622 status_t status = INVALID_OPERATION;
623 if (mCodec != NULL) {
624 status = mCodec->releaseCrypto();
625 } else {
626 // returning OK if the codec has been already released
627 status = OK;
628 ALOGE("onReleaseCrypto No mCodec. err: %d", status);
629 }
630
631 sp<AMessage> response = new AMessage;
632 response->setInt32("status", status);
Hassan Shojania62dec952017-05-18 10:45:27 -0700633 // Clearing the state as it's tied to crypto. mIsEncryptedObservedEarlier is sticky though
634 // and lasts for the lifetime of this codec. See its use in fetchInputData.
635 mIsEncrypted = false;
Hassan Shojaniacefac142017-02-06 21:02:02 -0800636
637 sp<AReplyToken> replyID;
638 CHECK(msg->senderAwaitsResponse(&replyID));
639 response->postReply(replyID);
640}
641
Marco Nelissen421f47c2015-03-25 14:40:32 -0700642bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800643 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800644 return false;
645 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700646
Wei Jiafcd87872017-07-28 15:50:04 -0700647 if (mCodec == NULL) {
648 ALOGE("[%s] handleAnInputBuffer without a valid codec", mComponentName.c_str());
649 handleError(NO_INIT);
650 return false;
651 }
652
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900653 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700654 mCodec->getInputBuffer(index, &buffer);
655
Wei Jia6301a5e2015-05-13 13:15:18 -0700656 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700657 ALOGE("[%s] handleAnInputBuffer, failed to get input buffer", mComponentName.c_str());
Wei Jia6301a5e2015-05-13 13:15:18 -0700658 handleError(UNKNOWN_ERROR);
659 return false;
660 }
661
Marco Nelissen421f47c2015-03-25 14:40:32 -0700662 if (index >= mInputBuffers.size()) {
663 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
664 mInputBuffers.add();
665 mMediaBuffers.add();
666 mInputBufferIsDequeued.add();
667 mMediaBuffers.editItemAt(i) = NULL;
668 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800669 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700670 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700671 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700672
Marco Nelissen421f47c2015-03-25 14:40:32 -0700673 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800674
Marco Nelissen421f47c2015-03-25 14:40:32 -0700675 if (mMediaBuffers[index] != NULL) {
676 mMediaBuffers[index]->release();
677 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700678 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700679 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700680
Lajos Molnar87603c02014-08-20 19:25:30 -0700681 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800682 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700683 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800684
Lajos Molnar87603c02014-08-20 19:25:30 -0700685 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800686 ALOGV("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800687 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700688 mCSDsToSubmit.removeAt(0);
Wei Jia56097a82016-01-07 16:03:03 -0800689 if (!onInputBufferFetched(msg)) {
690 handleError(UNKNOWN_ERROR);
691 return false;
692 }
Wei Jia2245fc62014-10-02 15:12:25 -0700693 return true;
694 }
695
696 while (!mPendingInputMessages.empty()) {
697 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800698 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700699 break;
700 }
701 mPendingInputMessages.erase(mPendingInputMessages.begin());
702 }
703
Marco Nelissen421f47c2015-03-25 14:40:32 -0700704 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700705 return true;
706 }
707
Marco Nelissen421f47c2015-03-25 14:40:32 -0700708 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800709
710 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800711 return true;
712}
713
Marco Nelissen421f47c2015-03-25 14:40:32 -0700714bool NuPlayer::Decoder::handleAnOutputBuffer(
715 size_t index,
716 size_t offset,
717 size_t size,
718 int64_t timeUs,
719 int32_t flags) {
Wei Jiafcd87872017-07-28 15:50:04 -0700720 if (mCodec == NULL) {
721 ALOGE("[%s] handleAnOutputBuffer without a valid codec", mComponentName.c_str());
722 handleError(NO_INIT);
723 return false;
724 }
725
Marco Nelissen421f47c2015-03-25 14:40:32 -0700726// CHECK_LT(bufferIx, mOutputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900727 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700728 mCodec->getOutputBuffer(index, &buffer);
729
Santhosh Behara3539def2015-10-09 17:32:58 -0700730 if (buffer == NULL) {
Wei Jiafcd87872017-07-28 15:50:04 -0700731 ALOGE("[%s] handleAnOutputBuffer, failed to get output buffer", mComponentName.c_str());
Santhosh Behara3539def2015-10-09 17:32:58 -0700732 handleError(UNKNOWN_ERROR);
733 return false;
734 }
735
Marco Nelissen421f47c2015-03-25 14:40:32 -0700736 if (index >= mOutputBuffers.size()) {
737 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
738 mOutputBuffers.add();
739 }
740 }
741
742 mOutputBuffers.editItemAt(index) = buffer;
743
Chong Zhang7137ec72014-11-12 16:41:05 -0800744 buffer->setRange(offset, size);
745 buffer->meta()->clear();
746 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800747
748 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800749 // we do not expect CODECCONFIG or SYNCFRAME for decoder
750
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800751 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700752 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800753 reply->setInt32("generation", mBufferGeneration);
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -0700754 reply->setSize("size", size);
Chong Zhang7137ec72014-11-12 16:41:05 -0800755
Chong Zhang66704af2015-03-03 19:32:35 -0800756 if (eos) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -0800757 ALOGV("[%s] saw output EOS", mIsAudio ? "audio" : "video");
Chong Zhang66704af2015-03-03 19:32:35 -0800758
759 buffer->meta()->setInt32("eos", true);
760 reply->setInt32("eos", true);
Wei Jiaaec8d822017-08-25 15:27:57 -0700761 }
762
Ray Essickc6e9f6e2017-12-07 16:57:36 -0800763 mNumFramesTotal += !mIsAudio;
764
Wei Jiaaec8d822017-08-25 15:27:57 -0700765 if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800766 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
767 ALOGV("[%s] dropping buffer at time %lld as requested.",
768 mComponentName.c_str(), (long long)timeUs);
769
770 reply->post();
Wei Jiaaec8d822017-08-25 15:27:57 -0700771 if (eos) {
772 notifyResumeCompleteIfNecessary();
773 if (mRenderer != NULL && !isDiscontinuityPending()) {
774 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
775 }
776 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800777 return true;
778 }
779
780 mSkipRenderingUntilMediaTimeUs = -1;
781 }
782
Chong Zhangf8d71772014-11-26 15:08:34 -0800783 // wait until 1st frame comes out to signal resume complete
784 notifyResumeCompleteIfNecessary();
785
Chong Zhang7137ec72014-11-12 16:41:05 -0800786 if (mRenderer != NULL) {
787 // send the buffer to renderer.
788 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800789 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800790 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
791 }
792 }
793
794 return true;
795}
796
Marco Nelissen421f47c2015-03-25 14:40:32 -0700797void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
798 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700799 int32_t width, height;
800 if (format->findInt32("width", &width)
801 && format->findInt32("height", &height)) {
802 mStats->setInt32("width", width);
803 mStats->setInt32("height", height);
804 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700805 sp<AMessage> notify = mNotify->dup();
806 notify->setInt32("what", kWhatVideoSizeChanged);
807 notify->setMessage("format", format);
808 notify->post();
809 } else if (mRenderer != NULL) {
810 uint32_t flags;
811 int64_t durationUs;
812 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700813 if (getAudioDeepBufferSetting() // override regardless of source duration
Andy Hung71c8e5c2017-04-03 15:50:27 -0700814 || (mSource->getDuration(&durationUs) == OK
Andy Hung288da022015-05-31 22:55:59 -0700815 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700816 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
817 } else {
818 flags = AUDIO_OUTPUT_FLAG_NONE;
819 }
820
Wei Jia9a3101b2016-11-08 14:34:24 -0800821 sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this);
822 reply->setInt32("generation", mBufferGeneration);
823 mRenderer->changeAudioFormat(
Dhananjay Kumarc387f2b2015-08-06 10:43:16 +0530824 format, false /* offloadOnly */, hasVideo,
825 flags, mSource->isStreaming(), reply);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700826 }
827}
828
Chong Zhang7137ec72014-11-12 16:41:05 -0800829void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
830 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
831 if (mMediaBuffers[i] != NULL) {
832 mMediaBuffers[i]->release();
833 mMediaBuffers.editItemAt(i) = NULL;
834 }
835 }
836 mMediaBuffers.resize(mInputBuffers.size());
837 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
838 mMediaBuffers.editItemAt(i) = NULL;
839 }
840 mInputBufferIsDequeued.clear();
841 mInputBufferIsDequeued.resize(mInputBuffers.size());
842 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
843 mInputBufferIsDequeued.editItemAt(i) = false;
844 }
845
846 mPendingInputMessages.clear();
847 mDequeuedInputBuffers.clear();
848 mSkipRenderingUntilMediaTimeUs = -1;
849}
850
851void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800852 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800853 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800854 reply->setInt32("generation", mBufferGeneration);
855 mCodec->requestActivityNotification(reply);
856 }
857}
858
859bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
860 int32_t generation;
861 CHECK(msg->findInt32("generation", &generation));
862 return generation != mBufferGeneration;
863}
864
865status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
866 sp<ABuffer> accessUnit;
Robert Shih59e9ca72016-10-20 13:51:42 -0700867 bool dropAccessUnit = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800868 do {
869 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
870
871 if (err == -EWOULDBLOCK) {
872 return err;
873 } else if (err != OK) {
874 if (err == INFO_DISCONTINUITY) {
875 int32_t type;
876 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
877
878 bool formatChange =
879 (mIsAudio &&
880 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
881 || (!mIsAudio &&
882 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
883
884 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
885
886 ALOGI("%s discontinuity (format=%d, time=%d)",
887 mIsAudio ? "audio" : "video", formatChange, timeChange);
888
889 bool seamlessFormatChange = false;
890 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
891 if (formatChange) {
892 seamlessFormatChange =
893 supportsSeamlessFormatChange(newFormat);
894 // treat seamless format change separately
895 formatChange = !seamlessFormatChange;
896 }
897
Chong Zhang66704af2015-03-03 19:32:35 -0800898 // For format or time change, return EOS to queue EOS input,
899 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800900 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800901 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800902 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800903 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800904 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800905 mTimeChangePending = true;
906 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800907 } else if (seamlessFormatChange) {
908 // reuse existing decoder and don't flush
909 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800910 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800911 } else {
912 // This stream is unaffected by the discontinuity
913 return -EWOULDBLOCK;
914 }
915 }
916
Chong Zhang66704af2015-03-03 19:32:35 -0800917 // reply should only be returned without a buffer set
918 // when there is an error (including EOS)
919 CHECK(err != OK);
920
Chong Zhang7137ec72014-11-12 16:41:05 -0800921 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800922 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800923 }
924
Chong Zhang7137ec72014-11-12 16:41:05 -0800925 dropAccessUnit = false;
Hassan Shojania62dec952017-05-18 10:45:27 -0700926 if (!mIsAudio && !mIsEncrypted) {
927 // Extra safeguard if higher-level behavior changes. Otherwise, not required now.
928 // Preventing the buffer from being processed (and sent to codec) if this is a later
929 // round of playback but this time without prepareDrm. Or if there is a race between
930 // stop (which is not blocking) and releaseDrm allowing buffers being processed after
931 // Crypto has been released (GenericSource currently prevents this race though).
932 // Particularly doing this check before IsAVCReferenceFrame call to prevent parsing
933 // of encrypted data.
934 if (mIsEncryptedObservedEarlier) {
935 ALOGE("fetchInputData: mismatched mIsEncrypted/mIsEncryptedObservedEarlier (0/1)");
936
937 return INVALID_OPERATION;
938 }
939
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700940 int32_t layerId = 0;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700941 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
Chih-Hung Hsieh62309d52018-12-11 13:54:02 -0800942 if (mRenderer->getVideoLateByUs() > 100000LL
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700943 && mIsVideoAVC
944 && !IsAVCReferenceFrame(accessUnit)) {
945 dropAccessUnit = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700946 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700947 // Add only one layer each time.
948 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
949 || layerId >= mNumVideoTemporalLayerAllowed) {
950 dropAccessUnit = true;
951 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
952 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
953 mCurrentMaxVideoTemporalLayerId);
954 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
955 mCurrentMaxVideoTemporalLayerId = layerId;
Dongwon Kangd91dc5a2017-10-10 00:07:09 -0700956 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1
957 && IsIDR(accessUnit->data(), accessUnit->size())) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700958 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700959 }
960 }
961 if (dropAccessUnit) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700962 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
963 mCurrentMaxVideoTemporalLayerId = layerId - 1;
964 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700965 ++mNumInputFramesDropped;
966 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800967 }
968 } while (dropAccessUnit);
969
970 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
971#if 0
972 int64_t mediaTimeUs;
973 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700974 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800975 mIsAudio ? "audio" : "video",
976 mediaTimeUs / 1E6);
977#endif
978
979 if (mCCDecoder != NULL) {
980 mCCDecoder->decode(accessUnit);
981 }
982
983 reply->setBuffer("buffer", accessUnit);
984
985 return OK;
986}
987
988bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Wei Jiafcd87872017-07-28 15:50:04 -0700989 if (mCodec == NULL) {
990 ALOGE("[%s] onInputBufferFetched without a valid codec", mComponentName.c_str());
991 handleError(NO_INIT);
992 return false;
993 }
994
Lajos Molnar1cd13982014-01-17 15:12:51 -0800995 size_t bufferIx;
996 CHECK(msg->findSize("buffer-ix", &bufferIx));
997 CHECK_LT(bufferIx, mInputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900998 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
Lajos Molnar1cd13982014-01-17 15:12:51 -0800999
1000 sp<ABuffer> buffer;
1001 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001002 bool needsCopy = true;
Lajos Molnar09524832014-07-17 14:29:51 -07001003
Lajos Molnar1cd13982014-01-17 15:12:51 -08001004 if (buffer == NULL /* includes !hasBuffer */) {
1005 int32_t streamErr = ERROR_END_OF_STREAM;
1006 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
1007
Chong Zhang66704af2015-03-03 19:32:35 -08001008 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001009
1010 // attempt to queue EOS
1011 status_t err = mCodec->queueInputBuffer(
1012 bufferIx,
1013 0,
1014 0,
1015 0,
1016 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001017 if (err == OK) {
1018 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
1019 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001020 streamErr = err;
1021 // err will not be ERROR_END_OF_STREAM
1022 }
1023
1024 if (streamErr != ERROR_END_OF_STREAM) {
Wei Jiafcd87872017-07-28 15:50:04 -07001025 ALOGE("Stream error for [%s] (err=%d), EOS %s queued",
Andy Hungcf31f1e2014-09-23 14:59:01 -07001026 mComponentName.c_str(),
1027 streamErr,
1028 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -08001029 handleError(streamErr);
1030 }
1031 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -08001032 sp<AMessage> extra;
1033 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
1034 int64_t resumeAtMediaTimeUs;
1035 if (extra->findInt64(
1036 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
Siarhei Vishniakou6b0b5262019-01-25 19:48:31 -08001037 ALOGV("[%s] suppressing rendering until %lld us",
Wei Jiac6cfd702014-11-11 16:33:20 -08001038 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
1039 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
1040 }
1041 }
1042
Lajos Molnar1cd13982014-01-17 15:12:51 -08001043 int64_t timeUs = 0;
1044 uint32_t flags = 0;
1045 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1046
Lajos Molnar87603c02014-08-20 19:25:30 -07001047 int32_t eos, csd;
1048 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -08001049 if (buffer->meta()->findInt32("eos", &eos) && eos) {
1050 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -07001051 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
1052 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001053 }
1054
Hassan Shojaniacefac142017-02-06 21:02:02 -08001055 // Modular DRM
Dongwon Kang1889c3e2018-02-01 13:44:57 -08001056 MediaBufferBase *mediaBuf = NULL;
Hassan Shojaniacefac142017-02-06 21:02:02 -08001057 NuPlayerDrm::CryptoInfo *cryptInfo = NULL;
1058
Lajos Molnar1cd13982014-01-17 15:12:51 -08001059 // copy into codec buffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001060 if (needsCopy) {
Wei Jia56097a82016-01-07 16:03:03 -08001061 if (buffer->size() > codecBuffer->capacity()) {
1062 handleError(ERROR_BUFFER_TOO_SMALL);
1063 mDequeuedInputBuffers.push_back(bufferIx);
1064 return false;
1065 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001066
Hassan Shojaniacefac142017-02-06 21:02:02 -08001067 if (buffer->data() != NULL) {
1068 codecBuffer->setRange(0, buffer->size());
1069 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
1070 } else { // No buffer->data()
1071 //Modular DRM
Dongwon Kangbc8f53b2018-01-25 17:01:44 -08001072 sp<RefBase> holder;
1073 if (buffer->meta()->findObject("mediaBufferHolder", &holder)) {
1074 mediaBuf = (holder != nullptr) ?
1075 static_cast<MediaBufferHolder*>(holder.get())->mediaBuffer() : nullptr;
1076 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001077 if (mediaBuf != NULL) {
Wei Jia9b5a7ad2018-07-27 17:33:24 -07001078 if (mediaBuf->size() > codecBuffer->capacity()) {
1079 handleError(ERROR_BUFFER_TOO_SMALL);
1080 mDequeuedInputBuffers.push_back(bufferIx);
1081 return false;
1082 }
1083
Hassan Shojaniacefac142017-02-06 21:02:02 -08001084 codecBuffer->setRange(0, mediaBuf->size());
1085 memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->size());
1086
Marco Nelissen3d21ae32018-02-16 08:24:08 -08001087 MetaDataBase &meta_data = mediaBuf->meta_data();
Hassan Shojaniacefac142017-02-06 21:02:02 -08001088 cryptInfo = NuPlayerDrm::getSampleCryptoInfo(meta_data);
Hassan Shojaniacefac142017-02-06 21:02:02 -08001089 } else { // No mediaBuf
1090 ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p",
1091 buffer.get());
1092 handleError(UNKNOWN_ERROR);
1093 return false;
1094 }
1095 } // buffer->data()
1096 } // needsCopy
1097
1098 status_t err;
1099 AString errorDetailMsg;
1100 if (cryptInfo != NULL) {
1101 err = mCodec->queueSecureInputBuffer(
1102 bufferIx,
1103 codecBuffer->offset(),
1104 cryptInfo->subSamples,
1105 cryptInfo->numSubSamples,
1106 cryptInfo->key,
1107 cryptInfo->iv,
1108 cryptInfo->mode,
1109 cryptInfo->pattern,
1110 timeUs,
1111 flags,
1112 &errorDetailMsg);
1113 // synchronous call so done with cryptInfo here
1114 free(cryptInfo);
1115 } else {
1116 err = mCodec->queueInputBuffer(
1117 bufferIx,
1118 codecBuffer->offset(),
1119 codecBuffer->size(),
1120 timeUs,
1121 flags,
1122 &errorDetailMsg);
1123 } // no cryptInfo
1124
Lajos Molnar1cd13982014-01-17 15:12:51 -08001125 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001126 ALOGE("onInputBufferFetched: queue%sInputBuffer failed for [%s] (err=%d, %s)",
Hassan Shojaniacefac142017-02-06 21:02:02 -08001127 (cryptInfo != NULL ? "Secure" : ""),
1128 mComponentName.c_str(), err, errorDetailMsg.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -08001129 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001130 } else {
1131 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
Lajos Molnar09524832014-07-17 14:29:51 -07001132 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001133
1134 } // buffer != NULL
Wei Jia2245fc62014-10-02 15:12:25 -07001135 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001136}
1137
Lajos Molnar1cd13982014-01-17 15:12:51 -08001138void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
1139 status_t err;
1140 int32_t render;
1141 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -08001142 int32_t eos;
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001143 size_t size;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001144 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -08001145
Chong Zhang7137ec72014-11-12 16:41:05 -08001146 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001147 int64_t timeUs;
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001148 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
Wei Jiac6cfd702014-11-11 16:33:20 -08001149 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -08001150
1151 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1152 mCCDecoder->display(timeUs);
1153 }
Wei Jiac6cfd702014-11-11 16:33:20 -08001154 }
1155
Wei Jiafcd87872017-07-28 15:50:04 -07001156 if (mCodec == NULL) {
1157 err = NO_INIT;
1158 } else if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -07001159 int64_t timestampNs;
1160 CHECK(msg->findInt64("timestampNs", &timestampNs));
1161 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001162 } else {
Praveen Chavanbc5fe9a2018-04-27 16:18:11 -07001163 if (!msg->findInt32("eos", &eos) || !eos ||
1164 !msg->findSize("size", &size) || size) {
1165 mNumOutputFramesDropped += !mIsAudio;
1166 }
Lajos Molnar1cd13982014-01-17 15:12:51 -08001167 err = mCodec->releaseOutputBuffer(bufferIx);
1168 }
1169 if (err != OK) {
Wei Jiafcd87872017-07-28 15:50:04 -07001170 ALOGE("failed to release output buffer for [%s] (err=%d)",
Lajos Molnar1cd13982014-01-17 15:12:51 -08001171 mComponentName.c_str(), err);
1172 handleError(err);
1173 }
Chong Zhang66704af2015-03-03 19:32:35 -08001174 if (msg->findInt32("eos", &eos) && eos
1175 && isDiscontinuityPending()) {
1176 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1177 }
1178}
1179
1180bool NuPlayer::Decoder::isDiscontinuityPending() const {
1181 return mFormatChangePending || mTimeChangePending;
1182}
1183
1184void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1185 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1186 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1187
1188 // If we have format change, pause and wait to be killed;
1189 // If we have time change only, flush and restart fetching.
1190
1191 if (mFormatChangePending) {
1192 mPaused = true;
1193 } else if (mTimeChangePending) {
1194 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -07001195 doFlush(false /* notifyComplete */);
1196 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -08001197 }
Chong Zhang66704af2015-03-03 19:32:35 -08001198 }
1199
1200 // Notify NuPlayer to either shutdown decoder, or rescan sources
1201 sp<AMessage> msg = mNotify->dup();
1202 msg->setInt32("what", kWhatInputDiscontinuity);
1203 msg->setInt32("formatChange", mFormatChangePending);
1204 msg->post();
1205
1206 mFormatChangePending = false;
1207 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001208}
1209
Chong Zhang7137ec72014-11-12 16:41:05 -08001210bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
1211 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001212 if (targetFormat == NULL) {
1213 return true;
1214 }
1215
1216 AString mime;
1217 if (!targetFormat->findString("mime", &mime)) {
1218 return false;
1219 }
1220
1221 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1222 // field-by-field comparison
1223 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1224 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1225 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001226 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -08001227 !targetFormat->findInt32(keys[i], &newVal) ||
1228 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001229 return false;
1230 }
1231 }
1232
1233 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001234 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -08001235 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001236 if (oldBuf->size() != newBuf->size()) {
1237 return false;
1238 }
1239 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1240 }
1241 }
1242 return false;
1243}
1244
1245bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001246 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001247 return false;
1248 }
1249
1250 if (targetFormat == NULL) {
1251 return true;
1252 }
1253
1254 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001255 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001256 || !targetFormat->findString("mime", &newMime)
1257 || !(oldMime == newMime)) {
1258 return false;
1259 }
1260
1261 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1262 bool seamless;
1263 if (audio) {
1264 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1265 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001266 int32_t isAdaptive;
1267 seamless = (mCodec != NULL &&
1268 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1269 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001270 }
1271
1272 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1273 return seamless;
1274}
1275
Chong Zhang7137ec72014-11-12 16:41:05 -08001276void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1277 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001278 return;
1279 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001280 mCSDsForCurrentFormat.clear();
1281 for (int32_t i = 0; ; ++i) {
1282 AString tag = "csd-";
1283 tag.append(i);
1284 sp<ABuffer> buffer;
1285 if (!format->findBuffer(tag.c_str(), &buffer)) {
1286 break;
1287 }
1288 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001289 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001290}
1291
Chong Zhangf8d71772014-11-26 15:08:34 -08001292void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1293 if (mResumePending) {
1294 mResumePending = false;
1295
1296 sp<AMessage> notify = mNotify->dup();
1297 notify->setInt32("what", kWhatResumeCompleted);
1298 notify->post();
1299 }
1300}
1301
Andreas Huberf9334412010-12-15 15:17:42 -08001302} // namespace android
1303