blob: 5689e95d9bffb10e8f8a5e143dcf8e5d7308b2c4 [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>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090032#include <media/MediaCodecBuffer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080033#include <media/stagefright/foundation/ABuffer.h>
34#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080035#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070036#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080037#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080038#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080039#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080040
Lajos Molnar1de1e252015-04-30 18:18:34 -070041#include <gui/Surface.h>
42
Chong Zhang7137ec72014-11-12 16:41:05 -080043#include "avc_utils.h"
44#include "ATSParser.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),
Wei Jiac6cfd702014-11-11 16:33:20 -080073 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080074 mNumFramesTotal(0ll),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070075 mNumInputFramesDropped(0ll),
76 mNumOutputFramesDropped(0ll),
77 mVideoWidth(0),
78 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080079 mIsAudio(true),
80 mIsVideoAVC(false),
81 mIsSecure(false),
82 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080083 mTimeChangePending(false),
Praveen Chavanbbaa1442016-04-08 13:33:49 -070084 mFrameRateTotal(kDefaultVideoFrameRateTotal),
85 mPlaybackSpeed(1.0f),
Lajos Molnar9fb81522016-07-14 07:33:43 -070086 mNumVideoTemporalLayerTotal(1), // decode all layers
Praveen Chavanbbaa1442016-04-08 13:33:49 -070087 mNumVideoTemporalLayerAllowed(1),
88 mCurrentMaxVideoTemporalLayerId(0),
Chong Zhangf8d71772014-11-26 15:08:34 -080089 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080090 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080091 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070092 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080093 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Praveen Chavanbbaa1442016-04-08 13:33:49 -070094 mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
Andreas Huberf9334412010-12-15 15:17:42 -080095}
96
97NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070098 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070099 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -0800100}
101
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700102sp<AMessage> NuPlayer::Decoder::getStats() const {
103 mStats->setInt64("frames-total", mNumFramesTotal);
104 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
105 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
106 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -0700107}
108
Lajos Molnara81c6222015-07-10 19:17:45 -0700109status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
110 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
111 return BAD_VALUE;
112 }
113
114 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
115
116 msg->setObject("surface", surface);
117 sp<AMessage> response;
118 status_t err = msg->postAndAwaitResponse(&response);
119 if (err == OK && response != NULL) {
120 CHECK(response->findInt32("err", &err));
121 }
122 return err;
123}
124
Chong Zhang7137ec72014-11-12 16:41:05 -0800125void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
126 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
127
128 switch (msg->what()) {
129 case kWhatCodecNotify:
130 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700131 int32_t cbID;
132 CHECK(msg->findInt32("callbackID", &cbID));
133
134 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
135 mIsAudio ? "audio" : "video", cbID, mPaused);
136
Marco Nelissen421f47c2015-03-25 14:40:32 -0700137 if (mPaused) {
138 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800139 }
140
Marco Nelissen421f47c2015-03-25 14:40:32 -0700141 switch (cbID) {
142 case MediaCodec::CB_INPUT_AVAILABLE:
143 {
144 int32_t index;
145 CHECK(msg->findInt32("index", &index));
146
147 handleAnInputBuffer(index);
148 break;
149 }
150
151 case MediaCodec::CB_OUTPUT_AVAILABLE:
152 {
153 int32_t index;
154 size_t offset;
155 size_t size;
156 int64_t timeUs;
157 int32_t flags;
158
159 CHECK(msg->findInt32("index", &index));
160 CHECK(msg->findSize("offset", &offset));
161 CHECK(msg->findSize("size", &size));
162 CHECK(msg->findInt64("timeUs", &timeUs));
163 CHECK(msg->findInt32("flags", &flags));
164
165 handleAnOutputBuffer(index, offset, size, timeUs, flags);
166 break;
167 }
168
169 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
170 {
171 sp<AMessage> format;
172 CHECK(msg->findMessage("format", &format));
173
174 handleOutputFormatChange(format);
175 break;
176 }
177
178 case MediaCodec::CB_ERROR:
179 {
180 status_t err;
181 CHECK(msg->findInt32("err", &err));
182 ALOGE("Decoder (%s) reported error : 0x%x",
183 mIsAudio ? "audio" : "video", err);
184
185 handleError(err);
186 break;
187 }
188
189 default:
190 {
191 TRESPASS();
192 break;
193 }
194 }
195
Lajos Molnar87603c02014-08-20 19:25:30 -0700196 break;
197 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800198
199 case kWhatRenderBuffer:
200 {
201 if (!isStaleReply(msg)) {
202 onRenderBuffer(msg);
203 }
204 break;
205 }
206
Wei Jia9a3101b2016-11-08 14:34:24 -0800207 case kWhatAudioOutputFormatChanged:
208 {
209 if (!isStaleReply(msg)) {
210 status_t err;
211 if (msg->findInt32("err", &err) && err != OK) {
212 ALOGE("Renderer reported 0x%x when changing audio output format", err);
213 handleError(err);
214 }
215 }
216 break;
217 }
218
Lajos Molnara81c6222015-07-10 19:17:45 -0700219 case kWhatSetVideoSurface:
220 {
221 sp<AReplyToken> replyID;
222 CHECK(msg->senderAwaitsResponse(&replyID));
223
224 sp<RefBase> obj;
225 CHECK(msg->findObject("surface", &obj));
226 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
227 int32_t err = INVALID_OPERATION;
228 // NOTE: in practice mSurface is always non-null, but checking here for completeness
229 if (mCodec != NULL && mSurface != NULL) {
230 // TODO: once AwesomePlayer is removed, remove this automatic connecting
231 // to the surface by MediaPlayerService.
232 //
233 // at this point MediaPlayerService::client has already connected to the
234 // surface, which MediaCodec does not expect
235 err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
236 if (err == OK) {
237 err = mCodec->setSurface(surface);
238 ALOGI_IF(err, "codec setSurface returned: %d", err);
239 if (err == OK) {
240 // reconnect to the old surface as MPS::Client will expect to
241 // be able to disconnect from it.
242 (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
243 mSurface = surface;
244 }
245 }
246 if (err != OK) {
247 // reconnect to the new surface on error as MPS::Client will expect to
248 // be able to disconnect from it.
249 (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
250 }
251 }
252
253 sp<AMessage> response = new AMessage;
254 response->setInt32("err", err);
255 response->postReply(replyID);
256 break;
257 }
258
Hassan Shojaniacefac142017-02-06 21:02:02 -0800259 case kWhatDrmReleaseCrypto:
260 {
261 ALOGV("kWhatDrmReleaseCrypto");
262 onReleaseCrypto(msg);
263 break;
264 }
265
Chong Zhang7137ec72014-11-12 16:41:05 -0800266 default:
267 DecoderBase::onMessageReceived(msg);
268 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700269 }
270}
271
Lajos Molnar1cd13982014-01-17 15:12:51 -0800272void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800273 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800274
Chong Zhang7137ec72014-11-12 16:41:05 -0800275 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800276 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800277
Lajos Molnar1cd13982014-01-17 15:12:51 -0800278 ++mBufferGeneration;
279
Andreas Huber84066782011-08-16 09:34:26 -0700280 AString mime;
281 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800282
Chong Zhang7137ec72014-11-12 16:41:05 -0800283 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
284 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
285
Lajos Molnar1cd13982014-01-17 15:12:51 -0800286 mComponentName = mime;
287 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700288 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800289
Ronghua Wu68845c12015-07-21 09:50:48 -0700290 mCodec = MediaCodec::CreateByType(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700291 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid, mUid);
Lajos Molnar09524832014-07-17 14:29:51 -0700292 int32_t secure = 0;
293 if (format->findInt32("secure", &secure) && secure != 0) {
294 if (mCodec != NULL) {
295 mCodec->getName(&mComponentName);
296 mComponentName.append(".secure");
297 mCodec->release();
298 ALOGI("[%s] creating", mComponentName.c_str());
299 mCodec = MediaCodec::CreateByComponentName(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700300 mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid, mUid);
Lajos Molnar09524832014-07-17 14:29:51 -0700301 }
302 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800303 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700304 ALOGE("Failed to create %s%s decoder",
305 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800306 handleError(UNKNOWN_ERROR);
307 return;
308 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800309 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800310
311 mCodec->getName(&mComponentName);
312
Lajos Molnar14986f62014-09-15 11:04:44 -0700313 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700314 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800315 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700316 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700317 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700318 // We treat this as a warning, as this is a preparatory step.
319 // Codec will try to connect to the surface, which is where
320 // any error signaling will occur.
321 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800322 }
Hassan Shojaniacefac142017-02-06 21:02:02 -0800323
324 // Modular DRM
325 void *pCrypto;
326 if (!format->findPointer("crypto", &pCrypto)) {
327 pCrypto = NULL;
328 }
329 sp<ICrypto> crypto = (ICrypto*)pCrypto;
330 ALOGV("onConfigure mCrypto: %p (%d) mIsSecure: %d",
331 crypto.get(), (crypto != NULL ? crypto->getStrongCount() : 0), mIsSecure);
332
Lajos Molnar14986f62014-09-15 11:04:44 -0700333 err = mCodec->configure(
Hassan Shojaniacefac142017-02-06 21:02:02 -0800334 format, mSurface, crypto, 0 /* flags */);
335
Lajos Molnar1cd13982014-01-17 15:12:51 -0800336 if (err != OK) {
337 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700338 mCodec->release();
339 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800340 handleError(err);
341 return;
342 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700343 rememberCodecSpecificData(format);
344
Lajos Molnar1cd13982014-01-17 15:12:51 -0800345 // the following should work in configured state
346 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
347 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
348
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700349 mStats->setString("mime", mime.c_str());
350 mStats->setString("component-name", mComponentName.c_str());
351
352 if (!mIsAudio) {
353 int32_t width, height;
354 if (mOutputFormat->findInt32("width", &width)
355 && mOutputFormat->findInt32("height", &height)) {
356 mStats->setInt32("width", width);
357 mStats->setInt32("height", height);
358 }
359 }
360
Marco Nelissen421f47c2015-03-25 14:40:32 -0700361 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
362 mCodec->setCallback(reply);
363
Lajos Molnar1cd13982014-01-17 15:12:51 -0800364 err = mCodec->start();
365 if (err != OK) {
366 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700367 mCodec->release();
368 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800369 handleError(err);
370 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800371 }
372
Lajos Molnar09524832014-07-17 14:29:51 -0700373 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700374
Wei Jia704e7262014-06-04 16:21:56 -0700375 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800376 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800377}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700378
Ronghua Wu8db88132015-04-22 13:51:35 -0700379void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700380 bool needAdjustLayers = false;
381 float frameRateTotal;
382 if (params->findFloat("frame-rate-total", &frameRateTotal)
383 && mFrameRateTotal != frameRateTotal) {
384 needAdjustLayers = true;
385 mFrameRateTotal = frameRateTotal;
Ronghua Wu8db88132015-04-22 13:51:35 -0700386 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700387
388 int32_t numVideoTemporalLayerTotal;
389 if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
Lajos Molnar9fb81522016-07-14 07:33:43 -0700390 && numVideoTemporalLayerTotal >= 0
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700391 && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
392 && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
393 needAdjustLayers = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700394 mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700395 }
396
Lajos Molnar9fb81522016-07-14 07:33:43 -0700397 if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700398 // TODO: For now, layer fps is calculated for some specific architectures.
399 // But it really should be extracted from the stream.
400 mVideoTemporalLayerAggregateFps[0] =
401 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - 1));
402 for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
403 mVideoTemporalLayerAggregateFps[i] =
404 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - i))
405 + mVideoTemporalLayerAggregateFps[i - 1];
406 }
407 }
408
409 float playbackSpeed;
410 if (params->findFloat("playback-speed", &playbackSpeed)
411 && mPlaybackSpeed != playbackSpeed) {
412 needAdjustLayers = true;
413 mPlaybackSpeed = playbackSpeed;
414 }
415
416 if (needAdjustLayers) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700417 float decodeFrameRate = mFrameRateTotal;
418 // enable temporal layering optimization only if we know the layering depth
419 if (mNumVideoTemporalLayerTotal > 1) {
420 int32_t layerId;
421 for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
422 if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
423 >= kDisplayRefreshingRate * 0.9) {
424 break;
425 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700426 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700427 mNumVideoTemporalLayerAllowed = layerId + 1;
428 decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700429 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700430 ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
431 mNumVideoTemporalLayerAllowed, decodeFrameRate);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700432
433 if (mCodec == NULL) {
434 ALOGW("onSetParameters called before codec is created.");
435 return;
436 }
437
438 sp<AMessage> codecParams = new AMessage();
Lajos Molnar9fb81522016-07-14 07:33:43 -0700439 codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700440 mCodec->setParameters(codecParams);
441 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700442}
443
Chong Zhang7137ec72014-11-12 16:41:05 -0800444void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800445 mRenderer = renderer;
Chong Zhang7137ec72014-11-12 16:41:05 -0800446}
447
Chong Zhangf8d71772014-11-26 15:08:34 -0800448void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800449 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800450
451 if (notifyComplete) {
452 mResumePending = true;
453 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700454 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800455}
456
Chong Zhang66704af2015-03-03 19:32:35 -0800457void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800458 if (mCCDecoder != NULL) {
459 mCCDecoder->flush();
460 }
461
462 if (mRenderer != NULL) {
463 mRenderer->flush(mIsAudio, notifyComplete);
464 mRenderer->signalTimeDiscontinuity();
465 }
466
467 status_t err = OK;
468 if (mCodec != NULL) {
469 err = mCodec->flush();
470 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
471 ++mBufferGeneration;
472 }
473
474 if (err != OK) {
475 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
476 handleError(err);
477 // finish with posting kWhatFlushCompleted.
478 // we attempt to release the buffers even if flush fails.
479 }
480 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700481 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800482}
Chong Zhang7137ec72014-11-12 16:41:05 -0800483
Marco Nelissen421f47c2015-03-25 14:40:32 -0700484
Chong Zhang66704af2015-03-03 19:32:35 -0800485void NuPlayer::Decoder::onFlush() {
486 doFlush(true);
487
488 if (isDiscontinuityPending()) {
489 // This could happen if the client starts seeking/shutdown
490 // after we queued an EOS for discontinuities.
491 // We can consider discontinuity handled.
492 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800493 }
Chong Zhang66704af2015-03-03 19:32:35 -0800494
495 sp<AMessage> notify = mNotify->dup();
496 notify->setInt32("what", kWhatFlushCompleted);
497 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800498}
499
500void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
501 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800502
503 // if there is a pending resume request, notify complete now
504 notifyResumeCompleteIfNecessary();
505
Chong Zhang7137ec72014-11-12 16:41:05 -0800506 if (mCodec != NULL) {
507 err = mCodec->release();
508 mCodec = NULL;
509 ++mBufferGeneration;
510
Lajos Molnar1de1e252015-04-30 18:18:34 -0700511 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800512 // reconnect to surface as MediaCodec disconnected from it
513 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700514 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800515 ALOGW_IF(error != NO_ERROR,
516 "[%s] failed to connect to native window, error=%d",
517 mComponentName.c_str(), error);
518 }
519 mComponentName = "decoder";
520 }
521
522 releaseAndResetMediaBuffers();
523
524 if (err != OK) {
525 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
526 handleError(err);
527 // finish with posting kWhatShutdownCompleted.
528 }
529
530 if (notifyComplete) {
531 sp<AMessage> notify = mNotify->dup();
532 notify->setInt32("what", kWhatShutdownCompleted);
533 notify->post();
534 mPaused = true;
535 }
536}
537
Chong Zhang3b032b32015-04-17 15:49:06 -0700538/*
539 * returns true if we should request more data
540 */
541bool NuPlayer::Decoder::doRequestBuffers() {
Jeff Tinker29b7dcf2016-10-24 10:28:30 -0700542 if (isDiscontinuityPending()) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700543 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800544 }
545 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800546 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800547 size_t bufferIx = *mDequeuedInputBuffers.begin();
548 sp<AMessage> msg = new AMessage();
549 msg->setSize("buffer-ix", bufferIx);
550 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800551 if (err != OK && err != ERROR_END_OF_STREAM) {
552 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800553 break;
554 }
555 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
556
557 if (!mPendingInputMessages.empty()
558 || !onInputBufferFetched(msg)) {
559 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700560 }
561 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800562
Chong Zhang3b032b32015-04-17 15:49:06 -0700563 return err == -EWOULDBLOCK
564 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700565}
566
Marco Nelissen421f47c2015-03-25 14:40:32 -0700567void NuPlayer::Decoder::handleError(int32_t err)
568{
569 // We cannot immediately release the codec due to buffers still outstanding
570 // in the renderer. We signal to the player the error so it can shutdown/release the
571 // decoder after flushing and increment the generation to discard unnecessary messages.
572
573 ++mBufferGeneration;
574
575 sp<AMessage> notify = mNotify->dup();
576 notify->setInt32("what", kWhatError);
577 notify->setInt32("err", err);
578 notify->post();
579}
580
Hassan Shojaniacefac142017-02-06 21:02:02 -0800581status_t NuPlayer::Decoder::releaseCrypto()
582{
583 ALOGV("releaseCrypto");
584
585 sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this);
586
587 sp<AMessage> response;
588 status_t status = msg->postAndAwaitResponse(&response);
589 if (status == OK && response != NULL) {
590 CHECK(response->findInt32("status", &status));
591 ALOGV("releaseCrypto ret: %d ", status);
592 } else {
593 ALOGE("releaseCrypto err: %d", status);
594 }
595
596 return status;
597}
598
599void NuPlayer::Decoder::onReleaseCrypto(const sp<AMessage>& msg)
600{
601 status_t status = INVALID_OPERATION;
602 if (mCodec != NULL) {
603 status = mCodec->releaseCrypto();
604 } else {
605 // returning OK if the codec has been already released
606 status = OK;
607 ALOGE("onReleaseCrypto No mCodec. err: %d", status);
608 }
609
610 sp<AMessage> response = new AMessage;
611 response->setInt32("status", status);
612
613 sp<AReplyToken> replyID;
614 CHECK(msg->senderAwaitsResponse(&replyID));
615 response->postReply(replyID);
616}
617
Marco Nelissen421f47c2015-03-25 14:40:32 -0700618bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800619 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800620 return false;
621 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700622
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900623 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700624 mCodec->getInputBuffer(index, &buffer);
625
Wei Jia6301a5e2015-05-13 13:15:18 -0700626 if (buffer == NULL) {
627 handleError(UNKNOWN_ERROR);
628 return false;
629 }
630
Marco Nelissen421f47c2015-03-25 14:40:32 -0700631 if (index >= mInputBuffers.size()) {
632 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
633 mInputBuffers.add();
634 mMediaBuffers.add();
635 mInputBufferIsDequeued.add();
636 mMediaBuffers.editItemAt(i) = NULL;
637 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800638 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700639 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700640 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700641
Marco Nelissen421f47c2015-03-25 14:40:32 -0700642 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800643
Marco Nelissen421f47c2015-03-25 14:40:32 -0700644 if (mMediaBuffers[index] != NULL) {
645 mMediaBuffers[index]->release();
646 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700647 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700648 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700649
Lajos Molnar87603c02014-08-20 19:25:30 -0700650 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800651 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700652 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800653
Lajos Molnar87603c02014-08-20 19:25:30 -0700654 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
655 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800656 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700657 mCSDsToSubmit.removeAt(0);
Wei Jia56097a82016-01-07 16:03:03 -0800658 if (!onInputBufferFetched(msg)) {
659 handleError(UNKNOWN_ERROR);
660 return false;
661 }
Wei Jia2245fc62014-10-02 15:12:25 -0700662 return true;
663 }
664
665 while (!mPendingInputMessages.empty()) {
666 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800667 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700668 break;
669 }
670 mPendingInputMessages.erase(mPendingInputMessages.begin());
671 }
672
Marco Nelissen421f47c2015-03-25 14:40:32 -0700673 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700674 return true;
675 }
676
Marco Nelissen421f47c2015-03-25 14:40:32 -0700677 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800678
679 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800680 return true;
681}
682
Marco Nelissen421f47c2015-03-25 14:40:32 -0700683bool NuPlayer::Decoder::handleAnOutputBuffer(
684 size_t index,
685 size_t offset,
686 size_t size,
687 int64_t timeUs,
688 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700689// CHECK_LT(bufferIx, mOutputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900690 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700691 mCodec->getOutputBuffer(index, &buffer);
692
Santhosh Behara3539def2015-10-09 17:32:58 -0700693 if (buffer == NULL) {
694 handleError(UNKNOWN_ERROR);
695 return false;
696 }
697
Marco Nelissen421f47c2015-03-25 14:40:32 -0700698 if (index >= mOutputBuffers.size()) {
699 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
700 mOutputBuffers.add();
701 }
702 }
703
704 mOutputBuffers.editItemAt(index) = buffer;
705
Chong Zhang7137ec72014-11-12 16:41:05 -0800706 buffer->setRange(offset, size);
707 buffer->meta()->clear();
708 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800709
710 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800711 // we do not expect CODECCONFIG or SYNCFRAME for decoder
712
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800713 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700714 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800715 reply->setInt32("generation", mBufferGeneration);
716
Chong Zhang66704af2015-03-03 19:32:35 -0800717 if (eos) {
718 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
719
720 buffer->meta()->setInt32("eos", true);
721 reply->setInt32("eos", true);
722 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800723 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
724 ALOGV("[%s] dropping buffer at time %lld as requested.",
725 mComponentName.c_str(), (long long)timeUs);
726
727 reply->post();
728 return true;
729 }
730
731 mSkipRenderingUntilMediaTimeUs = -1;
732 }
733
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700734 mNumFramesTotal += !mIsAudio;
735
Chong Zhangf8d71772014-11-26 15:08:34 -0800736 // wait until 1st frame comes out to signal resume complete
737 notifyResumeCompleteIfNecessary();
738
Chong Zhang7137ec72014-11-12 16:41:05 -0800739 if (mRenderer != NULL) {
740 // send the buffer to renderer.
741 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800742 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800743 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
744 }
745 }
746
747 return true;
748}
749
Marco Nelissen421f47c2015-03-25 14:40:32 -0700750void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
751 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700752 int32_t width, height;
753 if (format->findInt32("width", &width)
754 && format->findInt32("height", &height)) {
755 mStats->setInt32("width", width);
756 mStats->setInt32("height", height);
757 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700758 sp<AMessage> notify = mNotify->dup();
759 notify->setInt32("what", kWhatVideoSizeChanged);
760 notify->setMessage("format", format);
761 notify->post();
762 } else if (mRenderer != NULL) {
763 uint32_t flags;
764 int64_t durationUs;
765 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700766 if (getAudioDeepBufferSetting() // override regardless of source duration
767 || (!hasVideo
768 && mSource->getDuration(&durationUs) == OK
769 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700770 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
771 } else {
772 flags = AUDIO_OUTPUT_FLAG_NONE;
773 }
774
Wei Jia9a3101b2016-11-08 14:34:24 -0800775 sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this);
776 reply->setInt32("generation", mBufferGeneration);
777 mRenderer->changeAudioFormat(
778 format, false /* offloadOnly */, hasVideo, flags, reply);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700779 }
780}
781
Chong Zhang7137ec72014-11-12 16:41:05 -0800782void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
783 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
784 if (mMediaBuffers[i] != NULL) {
785 mMediaBuffers[i]->release();
786 mMediaBuffers.editItemAt(i) = NULL;
787 }
788 }
789 mMediaBuffers.resize(mInputBuffers.size());
790 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
791 mMediaBuffers.editItemAt(i) = NULL;
792 }
793 mInputBufferIsDequeued.clear();
794 mInputBufferIsDequeued.resize(mInputBuffers.size());
795 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
796 mInputBufferIsDequeued.editItemAt(i) = false;
797 }
798
799 mPendingInputMessages.clear();
800 mDequeuedInputBuffers.clear();
801 mSkipRenderingUntilMediaTimeUs = -1;
802}
803
804void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800805 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800806 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800807 reply->setInt32("generation", mBufferGeneration);
808 mCodec->requestActivityNotification(reply);
809 }
810}
811
812bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
813 int32_t generation;
814 CHECK(msg->findInt32("generation", &generation));
815 return generation != mBufferGeneration;
816}
817
818status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
819 sp<ABuffer> accessUnit;
Robert Shih59e9ca72016-10-20 13:51:42 -0700820 bool dropAccessUnit = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800821 do {
822 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
823
824 if (err == -EWOULDBLOCK) {
825 return err;
826 } else if (err != OK) {
827 if (err == INFO_DISCONTINUITY) {
828 int32_t type;
829 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
830
831 bool formatChange =
832 (mIsAudio &&
833 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
834 || (!mIsAudio &&
835 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
836
837 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
838
839 ALOGI("%s discontinuity (format=%d, time=%d)",
840 mIsAudio ? "audio" : "video", formatChange, timeChange);
841
842 bool seamlessFormatChange = false;
843 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
844 if (formatChange) {
845 seamlessFormatChange =
846 supportsSeamlessFormatChange(newFormat);
847 // treat seamless format change separately
848 formatChange = !seamlessFormatChange;
849 }
850
Chong Zhang66704af2015-03-03 19:32:35 -0800851 // For format or time change, return EOS to queue EOS input,
852 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800853 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800854 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800855 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800856 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800857 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800858 mTimeChangePending = true;
859 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800860 } else if (seamlessFormatChange) {
861 // reuse existing decoder and don't flush
862 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800863 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800864 } else {
865 // This stream is unaffected by the discontinuity
866 return -EWOULDBLOCK;
867 }
868 }
869
Chong Zhang66704af2015-03-03 19:32:35 -0800870 // reply should only be returned without a buffer set
871 // when there is an error (including EOS)
872 CHECK(err != OK);
873
Chong Zhang7137ec72014-11-12 16:41:05 -0800874 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800875 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800876 }
877
Chong Zhang7137ec72014-11-12 16:41:05 -0800878 dropAccessUnit = false;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700879 if (!mIsAudio && !mIsSecure) {
880 int32_t layerId = 0;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700881 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700882 if (mRenderer->getVideoLateByUs() > 100000ll
883 && mIsVideoAVC
884 && !IsAVCReferenceFrame(accessUnit)) {
885 dropAccessUnit = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700886 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700887 // Add only one layer each time.
888 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
889 || layerId >= mNumVideoTemporalLayerAllowed) {
890 dropAccessUnit = true;
891 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
892 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
893 mCurrentMaxVideoTemporalLayerId);
894 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
895 mCurrentMaxVideoTemporalLayerId = layerId;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700896 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1 && IsIDR(accessUnit)) {
897 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700898 }
899 }
900 if (dropAccessUnit) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700901 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
902 mCurrentMaxVideoTemporalLayerId = layerId - 1;
903 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700904 ++mNumInputFramesDropped;
905 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800906 }
907 } while (dropAccessUnit);
908
909 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
910#if 0
911 int64_t mediaTimeUs;
912 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700913 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800914 mIsAudio ? "audio" : "video",
915 mediaTimeUs / 1E6);
916#endif
917
918 if (mCCDecoder != NULL) {
919 mCCDecoder->decode(accessUnit);
920 }
921
922 reply->setBuffer("buffer", accessUnit);
923
924 return OK;
925}
926
927bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800928 size_t bufferIx;
929 CHECK(msg->findSize("buffer-ix", &bufferIx));
930 CHECK_LT(bufferIx, mInputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900931 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
Lajos Molnar1cd13982014-01-17 15:12:51 -0800932
933 sp<ABuffer> buffer;
934 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900935 bool needsCopy = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700936
Lajos Molnar1cd13982014-01-17 15:12:51 -0800937 if (buffer == NULL /* includes !hasBuffer */) {
938 int32_t streamErr = ERROR_END_OF_STREAM;
939 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
940
Chong Zhang66704af2015-03-03 19:32:35 -0800941 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800942
943 // attempt to queue EOS
944 status_t err = mCodec->queueInputBuffer(
945 bufferIx,
946 0,
947 0,
948 0,
949 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700950 if (err == OK) {
951 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
952 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800953 streamErr = err;
954 // err will not be ERROR_END_OF_STREAM
955 }
956
957 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700958 ALOGE("Stream error for %s (err=%d), EOS %s queued",
959 mComponentName.c_str(),
960 streamErr,
961 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800962 handleError(streamErr);
963 }
964 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800965 sp<AMessage> extra;
966 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
967 int64_t resumeAtMediaTimeUs;
968 if (extra->findInt64(
969 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
970 ALOGI("[%s] suppressing rendering until %lld us",
971 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
972 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
973 }
974 }
975
Lajos Molnar1cd13982014-01-17 15:12:51 -0800976 int64_t timeUs = 0;
977 uint32_t flags = 0;
978 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
979
Lajos Molnar87603c02014-08-20 19:25:30 -0700980 int32_t eos, csd;
981 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800982 if (buffer->meta()->findInt32("eos", &eos) && eos) {
983 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700984 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
985 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800986 }
987
Hassan Shojaniacefac142017-02-06 21:02:02 -0800988 // Modular DRM
989 MediaBuffer *mediaBuf = NULL;
990 NuPlayerDrm::CryptoInfo *cryptInfo = NULL;
991
Lajos Molnar1cd13982014-01-17 15:12:51 -0800992 // copy into codec buffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900993 if (needsCopy) {
Wei Jia56097a82016-01-07 16:03:03 -0800994 if (buffer->size() > codecBuffer->capacity()) {
995 handleError(ERROR_BUFFER_TOO_SMALL);
996 mDequeuedInputBuffers.push_back(bufferIx);
997 return false;
998 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800999
Hassan Shojaniacefac142017-02-06 21:02:02 -08001000 if (buffer->data() != NULL) {
1001 codecBuffer->setRange(0, buffer->size());
1002 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
1003 } else { // No buffer->data()
1004 //Modular DRM
1005 mediaBuf = (MediaBuffer*)buffer->getMediaBufferBase();
1006 if (mediaBuf != NULL) {
1007 codecBuffer->setRange(0, mediaBuf->size());
1008 memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->size());
1009
1010 sp<MetaData> meta_data = mediaBuf->meta_data();
1011 cryptInfo = NuPlayerDrm::getSampleCryptoInfo(meta_data);
1012
1013 // since getMediaBuffer() has incremented the refCount
1014 mediaBuf->release();
1015 } else { // No mediaBuf
1016 ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p",
1017 buffer.get());
1018 handleError(UNKNOWN_ERROR);
1019 return false;
1020 }
1021 } // buffer->data()
1022 } // needsCopy
1023
1024 status_t err;
1025 AString errorDetailMsg;
1026 if (cryptInfo != NULL) {
1027 err = mCodec->queueSecureInputBuffer(
1028 bufferIx,
1029 codecBuffer->offset(),
1030 cryptInfo->subSamples,
1031 cryptInfo->numSubSamples,
1032 cryptInfo->key,
1033 cryptInfo->iv,
1034 cryptInfo->mode,
1035 cryptInfo->pattern,
1036 timeUs,
1037 flags,
1038 &errorDetailMsg);
1039 // synchronous call so done with cryptInfo here
1040 free(cryptInfo);
1041 } else {
1042 err = mCodec->queueInputBuffer(
1043 bufferIx,
1044 codecBuffer->offset(),
1045 codecBuffer->size(),
1046 timeUs,
1047 flags,
1048 &errorDetailMsg);
1049 } // no cryptInfo
1050
Lajos Molnar1cd13982014-01-17 15:12:51 -08001051 if (err != OK) {
Hassan Shojaniacefac142017-02-06 21:02:02 -08001052 ALOGE("onInputBufferFetched: queue%sInputBuffer failed for %s (err=%d, %s)",
1053 (cryptInfo != NULL ? "Secure" : ""),
1054 mComponentName.c_str(), err, errorDetailMsg.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -08001055 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -07001056 } else {
1057 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
Lajos Molnar09524832014-07-17 14:29:51 -07001058 }
Hassan Shojaniacefac142017-02-06 21:02:02 -08001059
1060 } // buffer != NULL
Wei Jia2245fc62014-10-02 15:12:25 -07001061 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001062}
1063
Lajos Molnar1cd13982014-01-17 15:12:51 -08001064void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
1065 status_t err;
1066 int32_t render;
1067 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -08001068 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001069 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -08001070
Chong Zhang7137ec72014-11-12 16:41:05 -08001071 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001072 int64_t timeUs;
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001073 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
Wei Jiac6cfd702014-11-11 16:33:20 -08001074 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -08001075
1076 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1077 mCCDecoder->display(timeUs);
1078 }
Wei Jiac6cfd702014-11-11 16:33:20 -08001079 }
1080
Lajos Molnar1cd13982014-01-17 15:12:51 -08001081 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -07001082 int64_t timestampNs;
1083 CHECK(msg->findInt64("timestampNs", &timestampNs));
1084 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001085 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001086 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001087 err = mCodec->releaseOutputBuffer(bufferIx);
1088 }
1089 if (err != OK) {
1090 ALOGE("failed to release output buffer for %s (err=%d)",
1091 mComponentName.c_str(), err);
1092 handleError(err);
1093 }
Chong Zhang66704af2015-03-03 19:32:35 -08001094 if (msg->findInt32("eos", &eos) && eos
1095 && isDiscontinuityPending()) {
1096 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1097 }
1098}
1099
1100bool NuPlayer::Decoder::isDiscontinuityPending() const {
1101 return mFormatChangePending || mTimeChangePending;
1102}
1103
1104void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1105 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1106 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1107
1108 // If we have format change, pause and wait to be killed;
1109 // If we have time change only, flush and restart fetching.
1110
1111 if (mFormatChangePending) {
1112 mPaused = true;
1113 } else if (mTimeChangePending) {
1114 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -07001115 doFlush(false /* notifyComplete */);
1116 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -08001117 }
Chong Zhang66704af2015-03-03 19:32:35 -08001118 }
1119
1120 // Notify NuPlayer to either shutdown decoder, or rescan sources
1121 sp<AMessage> msg = mNotify->dup();
1122 msg->setInt32("what", kWhatInputDiscontinuity);
1123 msg->setInt32("formatChange", mFormatChangePending);
1124 msg->post();
1125
1126 mFormatChangePending = false;
1127 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001128}
1129
Chong Zhang7137ec72014-11-12 16:41:05 -08001130bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
1131 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001132 if (targetFormat == NULL) {
1133 return true;
1134 }
1135
1136 AString mime;
1137 if (!targetFormat->findString("mime", &mime)) {
1138 return false;
1139 }
1140
1141 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1142 // field-by-field comparison
1143 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1144 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1145 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001146 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -08001147 !targetFormat->findInt32(keys[i], &newVal) ||
1148 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001149 return false;
1150 }
1151 }
1152
1153 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001154 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -08001155 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001156 if (oldBuf->size() != newBuf->size()) {
1157 return false;
1158 }
1159 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1160 }
1161 }
1162 return false;
1163}
1164
1165bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001166 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001167 return false;
1168 }
1169
1170 if (targetFormat == NULL) {
1171 return true;
1172 }
1173
1174 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001175 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001176 || !targetFormat->findString("mime", &newMime)
1177 || !(oldMime == newMime)) {
1178 return false;
1179 }
1180
1181 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1182 bool seamless;
1183 if (audio) {
1184 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1185 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001186 int32_t isAdaptive;
1187 seamless = (mCodec != NULL &&
1188 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1189 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001190 }
1191
1192 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1193 return seamless;
1194}
1195
Chong Zhang7137ec72014-11-12 16:41:05 -08001196void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1197 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001198 return;
1199 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001200 mCSDsForCurrentFormat.clear();
1201 for (int32_t i = 0; ; ++i) {
1202 AString tag = "csd-";
1203 tag.append(i);
1204 sp<ABuffer> buffer;
1205 if (!format->findBuffer(tag.c_str(), &buffer)) {
1206 break;
1207 }
1208 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001209 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001210}
1211
Chong Zhangf8d71772014-11-26 15:08:34 -08001212void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1213 if (mResumePending) {
1214 mResumePending = false;
1215
1216 sp<AMessage> notify = mNotify->dup();
1217 notify->setInt32("what", kWhatResumeCompleted);
1218 notify->post();
1219 }
1220}
1221
Andreas Huberf9334412010-12-15 15:17:42 -08001222} // namespace android
1223