blob: 6974207c7692386b092848c163e767c96ebc63ab [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"
Wei Jiac6cfd702014-11-11 16:33:20 -080026#include "NuPlayerRenderer.h"
27#include "NuPlayerSource.h"
28
Andy Hung288da022015-05-31 22:55:59 -070029#include <cutils/properties.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080030#include <media/ICrypto.h>
Wonsik Kim7e34bf52016-08-23 00:09:18 +090031#include <media/MediaCodecBuffer.h>
Andreas Huberf9334412010-12-15 15:17:42 -080032#include <media/stagefright/foundation/ABuffer.h>
33#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080034#include <media/stagefright/foundation/AMessage.h>
Lajos Molnar09524832014-07-17 14:29:51 -070035#include <media/stagefright/MediaBuffer.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080036#include <media/stagefright/MediaCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080037#include <media/stagefright/MediaDefs.h>
Lajos Molnar1cd13982014-01-17 15:12:51 -080038#include <media/stagefright/MediaErrors.h>
Andreas Huberf9334412010-12-15 15:17:42 -080039
Lajos Molnar1de1e252015-04-30 18:18:34 -070040#include <gui/Surface.h>
41
Chong Zhang7137ec72014-11-12 16:41:05 -080042#include "avc_utils.h"
43#include "ATSParser.h"
44
Andreas Huberf9334412010-12-15 15:17:42 -080045namespace android {
46
Lajos Molnar9fb81522016-07-14 07:33:43 -070047static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display
Praveen Chavanbbaa1442016-04-08 13:33:49 -070048
49// The default total video frame rate of a stream when that info is not available from
50// the source.
51static float kDefaultVideoFrameRateTotal = 30.f;
52
Andy Hung288da022015-05-31 22:55:59 -070053static inline bool getAudioDeepBufferSetting() {
54 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
55}
56
Andreas Huberf9334412010-12-15 15:17:42 -080057NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080058 const sp<AMessage> &notify,
Wei Jiac6cfd702014-11-11 16:33:20 -080059 const sp<Source> &source,
Ronghua Wu68845c12015-07-21 09:50:48 -070060 pid_t pid,
Wei Jiaf2ae3e12016-10-27 17:10:59 -070061 uid_t uid,
Wei Jiac6cfd702014-11-11 16:33:20 -080062 const sp<Renderer> &renderer,
Lajos Molnar1de1e252015-04-30 18:18:34 -070063 const sp<Surface> &surface,
Chong Zhang7137ec72014-11-12 16:41:05 -080064 const sp<CCDecoder> &ccDecoder)
Andy Hung202bce12014-12-03 11:47:36 -080065 : DecoderBase(notify),
Lajos Molnar1de1e252015-04-30 18:18:34 -070066 mSurface(surface),
Wei Jiac6cfd702014-11-11 16:33:20 -080067 mSource(source),
68 mRenderer(renderer),
Chong Zhang7137ec72014-11-12 16:41:05 -080069 mCCDecoder(ccDecoder),
Ronghua Wu68845c12015-07-21 09:50:48 -070070 mPid(pid),
Wei Jiaf2ae3e12016-10-27 17:10:59 -070071 mUid(uid),
Wei Jiac6cfd702014-11-11 16:33:20 -080072 mSkipRenderingUntilMediaTimeUs(-1ll),
Chong Zhang7137ec72014-11-12 16:41:05 -080073 mNumFramesTotal(0ll),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070074 mNumInputFramesDropped(0ll),
75 mNumOutputFramesDropped(0ll),
76 mVideoWidth(0),
77 mVideoHeight(0),
Chong Zhang7137ec72014-11-12 16:41:05 -080078 mIsAudio(true),
79 mIsVideoAVC(false),
80 mIsSecure(false),
81 mFormatChangePending(false),
Chong Zhang66704af2015-03-03 19:32:35 -080082 mTimeChangePending(false),
Praveen Chavanbbaa1442016-04-08 13:33:49 -070083 mFrameRateTotal(kDefaultVideoFrameRateTotal),
84 mPlaybackSpeed(1.0f),
Lajos Molnar9fb81522016-07-14 07:33:43 -070085 mNumVideoTemporalLayerTotal(1), // decode all layers
Praveen Chavanbbaa1442016-04-08 13:33:49 -070086 mNumVideoTemporalLayerAllowed(1),
87 mCurrentMaxVideoTemporalLayerId(0),
Chong Zhangf8d71772014-11-26 15:08:34 -080088 mResumePending(false),
Lajos Molnar1cd13982014-01-17 15:12:51 -080089 mComponentName("decoder") {
Lajos Molnar1cd13982014-01-17 15:12:51 -080090 mCodecLooper = new ALooper;
Marco Nelissen9e2b7912014-08-18 16:13:03 -070091 mCodecLooper->setName("NPDecoder-CL");
Lajos Molnar1cd13982014-01-17 15:12:51 -080092 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
Praveen Chavanbbaa1442016-04-08 13:33:49 -070093 mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
Andreas Huberf9334412010-12-15 15:17:42 -080094}
95
96NuPlayer::Decoder::~Decoder() {
Ronghua Wufaeb0f22015-05-21 12:20:21 -070097 mCodec->release();
Wei Jia4923cee2014-09-24 14:25:19 -070098 releaseAndResetMediaBuffers();
Andreas Huberf9334412010-12-15 15:17:42 -080099}
100
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700101sp<AMessage> NuPlayer::Decoder::getStats() const {
102 mStats->setInt64("frames-total", mNumFramesTotal);
103 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
104 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
105 return mStats;
Lajos Molnar09524832014-07-17 14:29:51 -0700106}
107
Lajos Molnara81c6222015-07-10 19:17:45 -0700108status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) {
109 if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) {
110 return BAD_VALUE;
111 }
112
113 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
114
115 msg->setObject("surface", surface);
116 sp<AMessage> response;
117 status_t err = msg->postAndAwaitResponse(&response);
118 if (err == OK && response != NULL) {
119 CHECK(response->findInt32("err", &err));
120 }
121 return err;
122}
123
Chong Zhang7137ec72014-11-12 16:41:05 -0800124void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
125 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
126
127 switch (msg->what()) {
128 case kWhatCodecNotify:
129 {
Chong Zhang3b032b32015-04-17 15:49:06 -0700130 int32_t cbID;
131 CHECK(msg->findInt32("callbackID", &cbID));
132
133 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
134 mIsAudio ? "audio" : "video", cbID, mPaused);
135
Marco Nelissen421f47c2015-03-25 14:40:32 -0700136 if (mPaused) {
137 break;
Chong Zhang7137ec72014-11-12 16:41:05 -0800138 }
139
Marco Nelissen421f47c2015-03-25 14:40:32 -0700140 switch (cbID) {
141 case MediaCodec::CB_INPUT_AVAILABLE:
142 {
143 int32_t index;
144 CHECK(msg->findInt32("index", &index));
145
146 handleAnInputBuffer(index);
147 break;
148 }
149
150 case MediaCodec::CB_OUTPUT_AVAILABLE:
151 {
152 int32_t index;
153 size_t offset;
154 size_t size;
155 int64_t timeUs;
156 int32_t flags;
157
158 CHECK(msg->findInt32("index", &index));
159 CHECK(msg->findSize("offset", &offset));
160 CHECK(msg->findSize("size", &size));
161 CHECK(msg->findInt64("timeUs", &timeUs));
162 CHECK(msg->findInt32("flags", &flags));
163
164 handleAnOutputBuffer(index, offset, size, timeUs, flags);
165 break;
166 }
167
168 case MediaCodec::CB_OUTPUT_FORMAT_CHANGED:
169 {
170 sp<AMessage> format;
171 CHECK(msg->findMessage("format", &format));
172
173 handleOutputFormatChange(format);
174 break;
175 }
176
177 case MediaCodec::CB_ERROR:
178 {
179 status_t err;
180 CHECK(msg->findInt32("err", &err));
181 ALOGE("Decoder (%s) reported error : 0x%x",
182 mIsAudio ? "audio" : "video", err);
183
184 handleError(err);
185 break;
186 }
187
188 default:
189 {
190 TRESPASS();
191 break;
192 }
193 }
194
Lajos Molnar87603c02014-08-20 19:25:30 -0700195 break;
196 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800197
198 case kWhatRenderBuffer:
199 {
200 if (!isStaleReply(msg)) {
201 onRenderBuffer(msg);
202 }
203 break;
204 }
205
Lajos Molnara81c6222015-07-10 19:17:45 -0700206 case kWhatSetVideoSurface:
207 {
208 sp<AReplyToken> replyID;
209 CHECK(msg->senderAwaitsResponse(&replyID));
210
211 sp<RefBase> obj;
212 CHECK(msg->findObject("surface", &obj));
213 sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null
214 int32_t err = INVALID_OPERATION;
215 // NOTE: in practice mSurface is always non-null, but checking here for completeness
216 if (mCodec != NULL && mSurface != NULL) {
217 // TODO: once AwesomePlayer is removed, remove this automatic connecting
218 // to the surface by MediaPlayerService.
219 //
220 // at this point MediaPlayerService::client has already connected to the
221 // surface, which MediaCodec does not expect
222 err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA);
223 if (err == OK) {
224 err = mCodec->setSurface(surface);
225 ALOGI_IF(err, "codec setSurface returned: %d", err);
226 if (err == OK) {
227 // reconnect to the old surface as MPS::Client will expect to
228 // be able to disconnect from it.
229 (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
230 mSurface = surface;
231 }
232 }
233 if (err != OK) {
234 // reconnect to the new surface on error as MPS::Client will expect to
235 // be able to disconnect from it.
236 (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA);
237 }
238 }
239
240 sp<AMessage> response = new AMessage;
241 response->setInt32("err", err);
242 response->postReply(replyID);
243 break;
244 }
245
Chong Zhang7137ec72014-11-12 16:41:05 -0800246 default:
247 DecoderBase::onMessageReceived(msg);
248 break;
Lajos Molnar87603c02014-08-20 19:25:30 -0700249 }
250}
251
Lajos Molnar1cd13982014-01-17 15:12:51 -0800252void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -0800253 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800254
Chong Zhang7137ec72014-11-12 16:41:05 -0800255 mFormatChangePending = false;
Chong Zhang66704af2015-03-03 19:32:35 -0800256 mTimeChangePending = false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800257
Lajos Molnar1cd13982014-01-17 15:12:51 -0800258 ++mBufferGeneration;
259
Andreas Huber84066782011-08-16 09:34:26 -0700260 AString mime;
261 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -0800262
Chong Zhang7137ec72014-11-12 16:41:05 -0800263 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
264 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
265
Lajos Molnar1cd13982014-01-17 15:12:51 -0800266 mComponentName = mime;
267 mComponentName.append(" decoder");
Lajos Molnar1de1e252015-04-30 18:18:34 -0700268 ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800269
Ronghua Wu68845c12015-07-21 09:50:48 -0700270 mCodec = MediaCodec::CreateByType(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700271 mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid, mUid);
Lajos Molnar09524832014-07-17 14:29:51 -0700272 int32_t secure = 0;
273 if (format->findInt32("secure", &secure) && secure != 0) {
274 if (mCodec != NULL) {
275 mCodec->getName(&mComponentName);
276 mComponentName.append(".secure");
277 mCodec->release();
278 ALOGI("[%s] creating", mComponentName.c_str());
279 mCodec = MediaCodec::CreateByComponentName(
Wei Jiaf2ae3e12016-10-27 17:10:59 -0700280 mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid, mUid);
Lajos Molnar09524832014-07-17 14:29:51 -0700281 }
282 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800283 if (mCodec == NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700284 ALOGE("Failed to create %s%s decoder",
285 (secure ? "secure " : ""), mime.c_str());
Lajos Molnar1cd13982014-01-17 15:12:51 -0800286 handleError(UNKNOWN_ERROR);
287 return;
288 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800289 mIsSecure = secure;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800290
291 mCodec->getName(&mComponentName);
292
Lajos Molnar14986f62014-09-15 11:04:44 -0700293 status_t err;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700294 if (mSurface != NULL) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800295 // disconnect from surface as MediaCodec will reconnect
Lajos Molnar14986f62014-09-15 11:04:44 -0700296 err = native_window_api_disconnect(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700297 mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Lajos Molnar14986f62014-09-15 11:04:44 -0700298 // We treat this as a warning, as this is a preparatory step.
299 // Codec will try to connect to the surface, which is where
300 // any error signaling will occur.
301 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800302 }
Lajos Molnar14986f62014-09-15 11:04:44 -0700303 err = mCodec->configure(
Lajos Molnar1de1e252015-04-30 18:18:34 -0700304 format, mSurface, NULL /* crypto */, 0 /* flags */);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800305 if (err != OK) {
306 ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700307 mCodec->release();
308 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800309 handleError(err);
310 return;
311 }
Lajos Molnar87603c02014-08-20 19:25:30 -0700312 rememberCodecSpecificData(format);
313
Lajos Molnar1cd13982014-01-17 15:12:51 -0800314 // the following should work in configured state
315 CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
316 CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
317
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700318 mStats->setString("mime", mime.c_str());
319 mStats->setString("component-name", mComponentName.c_str());
320
321 if (!mIsAudio) {
322 int32_t width, height;
323 if (mOutputFormat->findInt32("width", &width)
324 && mOutputFormat->findInt32("height", &height)) {
325 mStats->setInt32("width", width);
326 mStats->setInt32("height", height);
327 }
328 }
329
Marco Nelissen421f47c2015-03-25 14:40:32 -0700330 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
331 mCodec->setCallback(reply);
332
Lajos Molnar1cd13982014-01-17 15:12:51 -0800333 err = mCodec->start();
334 if (err != OK) {
335 ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
Andy Hung2abde2c2014-09-30 14:40:32 -0700336 mCodec->release();
337 mCodec.clear();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800338 handleError(err);
339 return;
Andreas Huberf9334412010-12-15 15:17:42 -0800340 }
341
Lajos Molnar09524832014-07-17 14:29:51 -0700342 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700343
Wei Jia704e7262014-06-04 16:21:56 -0700344 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800345 mResumePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800346}
Andreas Huber078cfcf2011-09-15 12:25:04 -0700347
Ronghua Wu8db88132015-04-22 13:51:35 -0700348void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700349 bool needAdjustLayers = false;
350 float frameRateTotal;
351 if (params->findFloat("frame-rate-total", &frameRateTotal)
352 && mFrameRateTotal != frameRateTotal) {
353 needAdjustLayers = true;
354 mFrameRateTotal = frameRateTotal;
Ronghua Wu8db88132015-04-22 13:51:35 -0700355 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700356
357 int32_t numVideoTemporalLayerTotal;
358 if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
Lajos Molnar9fb81522016-07-14 07:33:43 -0700359 && numVideoTemporalLayerTotal >= 0
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700360 && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
361 && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
362 needAdjustLayers = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700363 mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700364 }
365
Lajos Molnar9fb81522016-07-14 07:33:43 -0700366 if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700367 // TODO: For now, layer fps is calculated for some specific architectures.
368 // But it really should be extracted from the stream.
369 mVideoTemporalLayerAggregateFps[0] =
370 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - 1));
371 for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
372 mVideoTemporalLayerAggregateFps[i] =
373 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - i))
374 + mVideoTemporalLayerAggregateFps[i - 1];
375 }
376 }
377
378 float playbackSpeed;
379 if (params->findFloat("playback-speed", &playbackSpeed)
380 && mPlaybackSpeed != playbackSpeed) {
381 needAdjustLayers = true;
382 mPlaybackSpeed = playbackSpeed;
383 }
384
385 if (needAdjustLayers) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700386 float decodeFrameRate = mFrameRateTotal;
387 // enable temporal layering optimization only if we know the layering depth
388 if (mNumVideoTemporalLayerTotal > 1) {
389 int32_t layerId;
390 for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
391 if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
392 >= kDisplayRefreshingRate * 0.9) {
393 break;
394 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700395 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700396 mNumVideoTemporalLayerAllowed = layerId + 1;
397 decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700398 }
Lajos Molnar9fb81522016-07-14 07:33:43 -0700399 ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
400 mNumVideoTemporalLayerAllowed, decodeFrameRate);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700401
402 if (mCodec == NULL) {
403 ALOGW("onSetParameters called before codec is created.");
404 return;
405 }
406
407 sp<AMessage> codecParams = new AMessage();
Lajos Molnar9fb81522016-07-14 07:33:43 -0700408 codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700409 mCodec->setParameters(codecParams);
410 }
Ronghua Wu8db88132015-04-22 13:51:35 -0700411}
412
Chong Zhang7137ec72014-11-12 16:41:05 -0800413void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
414 bool hadNoRenderer = (mRenderer == NULL);
415 mRenderer = renderer;
416 if (hadNoRenderer && mRenderer != NULL) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700417 // this means that the widevine legacy source is ready
418 onRequestInputBuffers();
Chong Zhang7137ec72014-11-12 16:41:05 -0800419 }
420}
421
422void NuPlayer::Decoder::onGetInputBuffers(
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900423 Vector<sp<MediaCodecBuffer> > *dstBuffers) {
Lajos Molnare6109e22015-04-10 17:18:22 -0700424 CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers));
Chong Zhang7137ec72014-11-12 16:41:05 -0800425}
426
Chong Zhangf8d71772014-11-26 15:08:34 -0800427void NuPlayer::Decoder::onResume(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800428 mPaused = false;
Chong Zhangf8d71772014-11-26 15:08:34 -0800429
430 if (notifyComplete) {
431 mResumePending = true;
432 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700433 mCodec->start();
Chong Zhang7137ec72014-11-12 16:41:05 -0800434}
435
Chong Zhang66704af2015-03-03 19:32:35 -0800436void NuPlayer::Decoder::doFlush(bool notifyComplete) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800437 if (mCCDecoder != NULL) {
438 mCCDecoder->flush();
439 }
440
441 if (mRenderer != NULL) {
442 mRenderer->flush(mIsAudio, notifyComplete);
443 mRenderer->signalTimeDiscontinuity();
444 }
445
446 status_t err = OK;
447 if (mCodec != NULL) {
448 err = mCodec->flush();
449 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
450 ++mBufferGeneration;
451 }
452
453 if (err != OK) {
454 ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
455 handleError(err);
456 // finish with posting kWhatFlushCompleted.
457 // we attempt to release the buffers even if flush fails.
458 }
459 releaseAndResetMediaBuffers();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700460 mPaused = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800461}
Chong Zhang7137ec72014-11-12 16:41:05 -0800462
Marco Nelissen421f47c2015-03-25 14:40:32 -0700463
Chong Zhang66704af2015-03-03 19:32:35 -0800464void NuPlayer::Decoder::onFlush() {
465 doFlush(true);
466
467 if (isDiscontinuityPending()) {
468 // This could happen if the client starts seeking/shutdown
469 // after we queued an EOS for discontinuities.
470 // We can consider discontinuity handled.
471 finishHandleDiscontinuity(false /* flushOnTimeChange */);
Chong Zhang7137ec72014-11-12 16:41:05 -0800472 }
Chong Zhang66704af2015-03-03 19:32:35 -0800473
474 sp<AMessage> notify = mNotify->dup();
475 notify->setInt32("what", kWhatFlushCompleted);
476 notify->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800477}
478
479void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
480 status_t err = OK;
Chong Zhangf8d71772014-11-26 15:08:34 -0800481
482 // if there is a pending resume request, notify complete now
483 notifyResumeCompleteIfNecessary();
484
Chong Zhang7137ec72014-11-12 16:41:05 -0800485 if (mCodec != NULL) {
486 err = mCodec->release();
487 mCodec = NULL;
488 ++mBufferGeneration;
489
Lajos Molnar1de1e252015-04-30 18:18:34 -0700490 if (mSurface != NULL) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800491 // reconnect to surface as MediaCodec disconnected from it
492 status_t error =
Lajos Molnar1de1e252015-04-30 18:18:34 -0700493 native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA);
Chong Zhang7137ec72014-11-12 16:41:05 -0800494 ALOGW_IF(error != NO_ERROR,
495 "[%s] failed to connect to native window, error=%d",
496 mComponentName.c_str(), error);
497 }
498 mComponentName = "decoder";
499 }
500
501 releaseAndResetMediaBuffers();
502
503 if (err != OK) {
504 ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
505 handleError(err);
506 // finish with posting kWhatShutdownCompleted.
507 }
508
509 if (notifyComplete) {
510 sp<AMessage> notify = mNotify->dup();
511 notify->setInt32("what", kWhatShutdownCompleted);
512 notify->post();
513 mPaused = true;
514 }
515}
516
Chong Zhang3b032b32015-04-17 15:49:06 -0700517/*
518 * returns true if we should request more data
519 */
520bool NuPlayer::Decoder::doRequestBuffers() {
Lajos Molnare6109e22015-04-10 17:18:22 -0700521 // mRenderer is only NULL if we have a legacy widevine source that
522 // is not yet ready. In this case we must not fetch input.
523 if (isDiscontinuityPending() || mRenderer == NULL) {
Chong Zhang3b032b32015-04-17 15:49:06 -0700524 return false;
Chong Zhang7137ec72014-11-12 16:41:05 -0800525 }
526 status_t err = OK;
Chong Zhang66704af2015-03-03 19:32:35 -0800527 while (err == OK && !mDequeuedInputBuffers.empty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800528 size_t bufferIx = *mDequeuedInputBuffers.begin();
529 sp<AMessage> msg = new AMessage();
530 msg->setSize("buffer-ix", bufferIx);
531 err = fetchInputData(msg);
Chong Zhang66704af2015-03-03 19:32:35 -0800532 if (err != OK && err != ERROR_END_OF_STREAM) {
533 // if EOS, need to queue EOS buffer
Chong Zhang7137ec72014-11-12 16:41:05 -0800534 break;
535 }
536 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
537
538 if (!mPendingInputMessages.empty()
539 || !onInputBufferFetched(msg)) {
540 mPendingInputMessages.push_back(msg);
Lajos Molnar09524832014-07-17 14:29:51 -0700541 }
542 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800543
Chong Zhang3b032b32015-04-17 15:49:06 -0700544 return err == -EWOULDBLOCK
545 && mSource->feedMoreTSData() == OK;
Lajos Molnar09524832014-07-17 14:29:51 -0700546}
547
Marco Nelissen421f47c2015-03-25 14:40:32 -0700548void NuPlayer::Decoder::handleError(int32_t err)
549{
550 // We cannot immediately release the codec due to buffers still outstanding
551 // in the renderer. We signal to the player the error so it can shutdown/release the
552 // decoder after flushing and increment the generation to discard unnecessary messages.
553
554 ++mBufferGeneration;
555
556 sp<AMessage> notify = mNotify->dup();
557 notify->setInt32("what", kWhatError);
558 notify->setInt32("err", err);
559 notify->post();
560}
561
562bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) {
Chong Zhang66704af2015-03-03 19:32:35 -0800563 if (isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800564 return false;
565 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700566
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900567 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700568 mCodec->getInputBuffer(index, &buffer);
569
Wei Jia6301a5e2015-05-13 13:15:18 -0700570 if (buffer == NULL) {
571 handleError(UNKNOWN_ERROR);
572 return false;
573 }
574
Marco Nelissen421f47c2015-03-25 14:40:32 -0700575 if (index >= mInputBuffers.size()) {
576 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
577 mInputBuffers.add();
578 mMediaBuffers.add();
579 mInputBufferIsDequeued.add();
580 mMediaBuffers.editItemAt(i) = NULL;
581 mInputBufferIsDequeued.editItemAt(i) = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800582 }
Andreas Huber078cfcf2011-09-15 12:25:04 -0700583 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700584 mInputBuffers.editItemAt(index) = buffer;
Andreas Huber078cfcf2011-09-15 12:25:04 -0700585
Marco Nelissen421f47c2015-03-25 14:40:32 -0700586 //CHECK_LT(bufferIx, mInputBuffers.size());
Andreas Huberf9334412010-12-15 15:17:42 -0800587
Marco Nelissen421f47c2015-03-25 14:40:32 -0700588 if (mMediaBuffers[index] != NULL) {
589 mMediaBuffers[index]->release();
590 mMediaBuffers.editItemAt(index) = NULL;
Lajos Molnar09524832014-07-17 14:29:51 -0700591 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700592 mInputBufferIsDequeued.editItemAt(index) = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700593
Lajos Molnar87603c02014-08-20 19:25:30 -0700594 if (!mCSDsToSubmit.isEmpty()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800595 sp<AMessage> msg = new AMessage();
Marco Nelissen421f47c2015-03-25 14:40:32 -0700596 msg->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800597
Lajos Molnar87603c02014-08-20 19:25:30 -0700598 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
599 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
Chong Zhang7137ec72014-11-12 16:41:05 -0800600 msg->setBuffer("buffer", buffer);
Lajos Molnar87603c02014-08-20 19:25:30 -0700601 mCSDsToSubmit.removeAt(0);
Wei Jia56097a82016-01-07 16:03:03 -0800602 if (!onInputBufferFetched(msg)) {
603 handleError(UNKNOWN_ERROR);
604 return false;
605 }
Wei Jia2245fc62014-10-02 15:12:25 -0700606 return true;
607 }
608
609 while (!mPendingInputMessages.empty()) {
610 sp<AMessage> msg = *mPendingInputMessages.begin();
Chong Zhang7137ec72014-11-12 16:41:05 -0800611 if (!onInputBufferFetched(msg)) {
Wei Jia2245fc62014-10-02 15:12:25 -0700612 break;
613 }
614 mPendingInputMessages.erase(mPendingInputMessages.begin());
615 }
616
Marco Nelissen421f47c2015-03-25 14:40:32 -0700617 if (!mInputBufferIsDequeued.editItemAt(index)) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700618 return true;
619 }
620
Marco Nelissen421f47c2015-03-25 14:40:32 -0700621 mDequeuedInputBuffers.push_back(index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800622
623 onRequestInputBuffers();
Lajos Molnar1cd13982014-01-17 15:12:51 -0800624 return true;
625}
626
Marco Nelissen421f47c2015-03-25 14:40:32 -0700627bool NuPlayer::Decoder::handleAnOutputBuffer(
628 size_t index,
629 size_t offset,
630 size_t size,
631 int64_t timeUs,
632 int32_t flags) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700633// CHECK_LT(bufferIx, mOutputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900634 sp<MediaCodecBuffer> buffer;
Marco Nelissen421f47c2015-03-25 14:40:32 -0700635 mCodec->getOutputBuffer(index, &buffer);
636
637 if (index >= mOutputBuffers.size()) {
638 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
639 mOutputBuffers.add();
640 }
641 }
642
643 mOutputBuffers.editItemAt(index) = buffer;
644
Chong Zhang7137ec72014-11-12 16:41:05 -0800645 buffer->setRange(offset, size);
646 buffer->meta()->clear();
647 buffer->meta()->setInt64("timeUs", timeUs);
Chong Zhang66704af2015-03-03 19:32:35 -0800648
649 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
Chong Zhang7137ec72014-11-12 16:41:05 -0800650 // we do not expect CODECCONFIG or SYNCFRAME for decoder
651
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800652 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
Marco Nelissen421f47c2015-03-25 14:40:32 -0700653 reply->setSize("buffer-ix", index);
Chong Zhang7137ec72014-11-12 16:41:05 -0800654 reply->setInt32("generation", mBufferGeneration);
655
Chong Zhang66704af2015-03-03 19:32:35 -0800656 if (eos) {
657 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
658
659 buffer->meta()->setInt32("eos", true);
660 reply->setInt32("eos", true);
661 } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800662 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
663 ALOGV("[%s] dropping buffer at time %lld as requested.",
664 mComponentName.c_str(), (long long)timeUs);
665
666 reply->post();
667 return true;
668 }
669
670 mSkipRenderingUntilMediaTimeUs = -1;
671 }
672
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700673 mNumFramesTotal += !mIsAudio;
674
Chong Zhangf8d71772014-11-26 15:08:34 -0800675 // wait until 1st frame comes out to signal resume complete
676 notifyResumeCompleteIfNecessary();
677
Chong Zhang7137ec72014-11-12 16:41:05 -0800678 if (mRenderer != NULL) {
679 // send the buffer to renderer.
680 mRenderer->queueBuffer(mIsAudio, buffer, reply);
Chong Zhang66704af2015-03-03 19:32:35 -0800681 if (eos && !isDiscontinuityPending()) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800682 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
683 }
684 }
685
686 return true;
687}
688
Marco Nelissen421f47c2015-03-25 14:40:32 -0700689void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
690 if (!mIsAudio) {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -0700691 int32_t width, height;
692 if (format->findInt32("width", &width)
693 && format->findInt32("height", &height)) {
694 mStats->setInt32("width", width);
695 mStats->setInt32("height", height);
696 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700697 sp<AMessage> notify = mNotify->dup();
698 notify->setInt32("what", kWhatVideoSizeChanged);
699 notify->setMessage("format", format);
700 notify->post();
701 } else if (mRenderer != NULL) {
702 uint32_t flags;
703 int64_t durationUs;
704 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
Andy Hung288da022015-05-31 22:55:59 -0700705 if (getAudioDeepBufferSetting() // override regardless of source duration
706 || (!hasVideo
707 && mSource->getDuration(&durationUs) == OK
708 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
Marco Nelissen421f47c2015-03-25 14:40:32 -0700709 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
710 } else {
711 flags = AUDIO_OUTPUT_FLAG_NONE;
712 }
713
Eric Laurent216f0172015-08-20 18:40:24 -0700714 status_t err = mRenderer->openAudioSink(
Marco Nelissen421f47c2015-03-25 14:40:32 -0700715 format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */);
Eric Laurent216f0172015-08-20 18:40:24 -0700716 if (err != OK) {
717 handleError(err);
718 }
Marco Nelissen421f47c2015-03-25 14:40:32 -0700719 }
720}
721
Chong Zhang7137ec72014-11-12 16:41:05 -0800722void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
723 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
724 if (mMediaBuffers[i] != NULL) {
725 mMediaBuffers[i]->release();
726 mMediaBuffers.editItemAt(i) = NULL;
727 }
728 }
729 mMediaBuffers.resize(mInputBuffers.size());
730 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
731 mMediaBuffers.editItemAt(i) = NULL;
732 }
733 mInputBufferIsDequeued.clear();
734 mInputBufferIsDequeued.resize(mInputBuffers.size());
735 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
736 mInputBufferIsDequeued.editItemAt(i) = false;
737 }
738
739 mPendingInputMessages.clear();
740 mDequeuedInputBuffers.clear();
741 mSkipRenderingUntilMediaTimeUs = -1;
742}
743
744void NuPlayer::Decoder::requestCodecNotification() {
Chong Zhang7137ec72014-11-12 16:41:05 -0800745 if (mCodec != NULL) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800746 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
Chong Zhang7137ec72014-11-12 16:41:05 -0800747 reply->setInt32("generation", mBufferGeneration);
748 mCodec->requestActivityNotification(reply);
749 }
750}
751
752bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
753 int32_t generation;
754 CHECK(msg->findInt32("generation", &generation));
755 return generation != mBufferGeneration;
756}
757
758status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
759 sp<ABuffer> accessUnit;
Robert Shih59e9ca72016-10-20 13:51:42 -0700760 bool dropAccessUnit = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800761 do {
762 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
763
764 if (err == -EWOULDBLOCK) {
765 return err;
766 } else if (err != OK) {
767 if (err == INFO_DISCONTINUITY) {
768 int32_t type;
769 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
770
771 bool formatChange =
772 (mIsAudio &&
773 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
774 || (!mIsAudio &&
775 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
776
777 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
778
779 ALOGI("%s discontinuity (format=%d, time=%d)",
780 mIsAudio ? "audio" : "video", formatChange, timeChange);
781
782 bool seamlessFormatChange = false;
783 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
784 if (formatChange) {
785 seamlessFormatChange =
786 supportsSeamlessFormatChange(newFormat);
787 // treat seamless format change separately
788 formatChange = !seamlessFormatChange;
789 }
790
Chong Zhang66704af2015-03-03 19:32:35 -0800791 // For format or time change, return EOS to queue EOS input,
792 // then wait for EOS on output.
Chong Zhang7137ec72014-11-12 16:41:05 -0800793 if (formatChange /* not seamless */) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800794 mFormatChangePending = true;
Chong Zhang66704af2015-03-03 19:32:35 -0800795 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800796 } else if (timeChange) {
Chong Zhang7137ec72014-11-12 16:41:05 -0800797 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800798 mTimeChangePending = true;
799 err = ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800800 } else if (seamlessFormatChange) {
801 // reuse existing decoder and don't flush
802 rememberCodecSpecificData(newFormat);
Chong Zhang66704af2015-03-03 19:32:35 -0800803 continue;
Chong Zhang7137ec72014-11-12 16:41:05 -0800804 } else {
805 // This stream is unaffected by the discontinuity
806 return -EWOULDBLOCK;
807 }
808 }
809
Chong Zhang66704af2015-03-03 19:32:35 -0800810 // reply should only be returned without a buffer set
811 // when there is an error (including EOS)
812 CHECK(err != OK);
813
Chong Zhang7137ec72014-11-12 16:41:05 -0800814 reply->setInt32("err", err);
Chong Zhang66704af2015-03-03 19:32:35 -0800815 return ERROR_END_OF_STREAM;
Chong Zhang7137ec72014-11-12 16:41:05 -0800816 }
817
Chong Zhang7137ec72014-11-12 16:41:05 -0800818 dropAccessUnit = false;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700819 if (!mIsAudio && !mIsSecure) {
820 int32_t layerId = 0;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700821 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700822 if (mRenderer->getVideoLateByUs() > 100000ll
823 && mIsVideoAVC
824 && !IsAVCReferenceFrame(accessUnit)) {
825 dropAccessUnit = true;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700826 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700827 // Add only one layer each time.
828 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
829 || layerId >= mNumVideoTemporalLayerAllowed) {
830 dropAccessUnit = true;
831 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
832 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
833 mCurrentMaxVideoTemporalLayerId);
834 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
835 mCurrentMaxVideoTemporalLayerId = layerId;
Lajos Molnar9fb81522016-07-14 07:33:43 -0700836 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1 && IsIDR(accessUnit)) {
837 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700838 }
839 }
840 if (dropAccessUnit) {
Lajos Molnar9fb81522016-07-14 07:33:43 -0700841 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
842 mCurrentMaxVideoTemporalLayerId = layerId - 1;
843 }
Praveen Chavanbbaa1442016-04-08 13:33:49 -0700844 ++mNumInputFramesDropped;
845 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800846 }
847 } while (dropAccessUnit);
848
849 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
850#if 0
851 int64_t mediaTimeUs;
852 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
Chong Zhang5abbd3d2015-04-20 16:03:00 -0700853 ALOGV("[%s] feeding input buffer at media time %.3f",
Chong Zhang7137ec72014-11-12 16:41:05 -0800854 mIsAudio ? "audio" : "video",
855 mediaTimeUs / 1E6);
856#endif
857
858 if (mCCDecoder != NULL) {
859 mCCDecoder->decode(accessUnit);
860 }
861
862 reply->setBuffer("buffer", accessUnit);
863
864 return OK;
865}
866
867bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800868 size_t bufferIx;
869 CHECK(msg->findSize("buffer-ix", &bufferIx));
870 CHECK_LT(bufferIx, mInputBuffers.size());
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900871 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
Lajos Molnar1cd13982014-01-17 15:12:51 -0800872
873 sp<ABuffer> buffer;
874 bool hasBuffer = msg->findBuffer("buffer", &buffer);
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900875 bool needsCopy = true;
Lajos Molnar09524832014-07-17 14:29:51 -0700876
877 // handle widevine classic source - that fills an arbitrary input buffer
878 MediaBuffer *mediaBuffer = NULL;
Wei Jia96e92b52014-09-18 17:36:20 -0700879 if (hasBuffer) {
880 mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
881 if (mediaBuffer != NULL) {
Lajos Molnar09524832014-07-17 14:29:51 -0700882 // likely filled another buffer than we requested: adjust buffer index
883 size_t ix;
884 for (ix = 0; ix < mInputBuffers.size(); ix++) {
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900885 const sp<MediaCodecBuffer> &buf = mInputBuffers[ix];
Lajos Molnar09524832014-07-17 14:29:51 -0700886 if (buf->data() == mediaBuffer->data()) {
887 // all input buffers are dequeued on start, hence the check
Wei Jia2245fc62014-10-02 15:12:25 -0700888 if (!mInputBufferIsDequeued[ix]) {
889 ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
890 mComponentName.c_str(), ix, bufferIx);
891 mediaBuffer->release();
892 return false;
893 }
Lajos Molnar09524832014-07-17 14:29:51 -0700894
895 // TRICKY: need buffer for the metadata, so instead, set
896 // codecBuffer to the same (though incorrect) buffer to
897 // avoid a memcpy into the codecBuffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900898 codecBuffer = new MediaCodecBuffer(codecBuffer->format(), buffer);
Lajos Molnar09524832014-07-17 14:29:51 -0700899 codecBuffer->setRange(
900 mediaBuffer->range_offset(),
901 mediaBuffer->range_length());
902 bufferIx = ix;
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900903 needsCopy = false;
Lajos Molnar09524832014-07-17 14:29:51 -0700904 break;
905 }
906 }
907 CHECK(ix < mInputBuffers.size());
908 }
909 }
910
Lajos Molnar1cd13982014-01-17 15:12:51 -0800911 if (buffer == NULL /* includes !hasBuffer */) {
912 int32_t streamErr = ERROR_END_OF_STREAM;
913 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
914
Chong Zhang66704af2015-03-03 19:32:35 -0800915 CHECK(streamErr != OK);
Lajos Molnar1cd13982014-01-17 15:12:51 -0800916
917 // attempt to queue EOS
918 status_t err = mCodec->queueInputBuffer(
919 bufferIx,
920 0,
921 0,
922 0,
923 MediaCodec::BUFFER_FLAG_EOS);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700924 if (err == OK) {
925 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
926 } else if (streamErr == ERROR_END_OF_STREAM) {
Lajos Molnar1cd13982014-01-17 15:12:51 -0800927 streamErr = err;
928 // err will not be ERROR_END_OF_STREAM
929 }
930
931 if (streamErr != ERROR_END_OF_STREAM) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700932 ALOGE("Stream error for %s (err=%d), EOS %s queued",
933 mComponentName.c_str(),
934 streamErr,
935 err == OK ? "successfully" : "unsuccessfully");
Lajos Molnar1cd13982014-01-17 15:12:51 -0800936 handleError(streamErr);
937 }
938 } else {
Wei Jiac6cfd702014-11-11 16:33:20 -0800939 sp<AMessage> extra;
940 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
941 int64_t resumeAtMediaTimeUs;
942 if (extra->findInt64(
943 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
944 ALOGI("[%s] suppressing rendering until %lld us",
945 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
946 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
947 }
948 }
949
Lajos Molnar1cd13982014-01-17 15:12:51 -0800950 int64_t timeUs = 0;
951 uint32_t flags = 0;
952 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
953
Lajos Molnar87603c02014-08-20 19:25:30 -0700954 int32_t eos, csd;
955 // we do not expect SYNCFRAME for decoder
Lajos Molnar1cd13982014-01-17 15:12:51 -0800956 if (buffer->meta()->findInt32("eos", &eos) && eos) {
957 flags |= MediaCodec::BUFFER_FLAG_EOS;
Lajos Molnar87603c02014-08-20 19:25:30 -0700958 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
959 flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800960 }
961
962 // copy into codec buffer
Wonsik Kim7e34bf52016-08-23 00:09:18 +0900963 if (needsCopy) {
Wei Jia56097a82016-01-07 16:03:03 -0800964 if (buffer->size() > codecBuffer->capacity()) {
965 handleError(ERROR_BUFFER_TOO_SMALL);
966 mDequeuedInputBuffers.push_back(bufferIx);
967 return false;
968 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800969 codecBuffer->setRange(0, buffer->size());
970 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
971 }
972
973 status_t err = mCodec->queueInputBuffer(
974 bufferIx,
975 codecBuffer->offset(),
976 codecBuffer->size(),
977 timeUs,
978 flags);
979 if (err != OK) {
Andy Hungcf31f1e2014-09-23 14:59:01 -0700980 if (mediaBuffer != NULL) {
981 mediaBuffer->release();
982 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800983 ALOGE("Failed to queue input buffer for %s (err=%d)",
984 mComponentName.c_str(), err);
985 handleError(err);
Andy Hungcf31f1e2014-09-23 14:59:01 -0700986 } else {
987 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
988 if (mediaBuffer != NULL) {
989 CHECK(mMediaBuffers[bufferIx] == NULL);
990 mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
991 }
Lajos Molnar09524832014-07-17 14:29:51 -0700992 }
Lajos Molnar1cd13982014-01-17 15:12:51 -0800993 }
Wei Jia2245fc62014-10-02 15:12:25 -0700994 return true;
Lajos Molnar1cd13982014-01-17 15:12:51 -0800995}
996
Lajos Molnar1cd13982014-01-17 15:12:51 -0800997void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
998 status_t err;
999 int32_t render;
1000 size_t bufferIx;
Chong Zhang66704af2015-03-03 19:32:35 -08001001 int32_t eos;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001002 CHECK(msg->findSize("buffer-ix", &bufferIx));
Wei Jiac6cfd702014-11-11 16:33:20 -08001003
Chong Zhang7137ec72014-11-12 16:41:05 -08001004 if (!mIsAudio) {
Wei Jiac6cfd702014-11-11 16:33:20 -08001005 int64_t timeUs;
Wonsik Kim7e34bf52016-08-23 00:09:18 +09001006 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
Wei Jiac6cfd702014-11-11 16:33:20 -08001007 buffer->meta()->findInt64("timeUs", &timeUs);
Chong Zhang7137ec72014-11-12 16:41:05 -08001008
1009 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1010 mCCDecoder->display(timeUs);
1011 }
Wei Jiac6cfd702014-11-11 16:33:20 -08001012 }
1013
Lajos Molnar1cd13982014-01-17 15:12:51 -08001014 if (msg->findInt32("render", &render) && render) {
Lajos Molnardc43dfa2014-05-07 15:33:04 -07001015 int64_t timestampNs;
1016 CHECK(msg->findInt64("timestampNs", &timestampNs));
1017 err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
Lajos Molnar1cd13982014-01-17 15:12:51 -08001018 } else {
Praveen Chavane1e5d7a2015-05-19 19:09:48 -07001019 mNumOutputFramesDropped += !mIsAudio;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001020 err = mCodec->releaseOutputBuffer(bufferIx);
1021 }
1022 if (err != OK) {
1023 ALOGE("failed to release output buffer for %s (err=%d)",
1024 mComponentName.c_str(), err);
1025 handleError(err);
1026 }
Chong Zhang66704af2015-03-03 19:32:35 -08001027 if (msg->findInt32("eos", &eos) && eos
1028 && isDiscontinuityPending()) {
1029 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1030 }
1031}
1032
1033bool NuPlayer::Decoder::isDiscontinuityPending() const {
1034 return mFormatChangePending || mTimeChangePending;
1035}
1036
1037void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1038 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1039 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1040
1041 // If we have format change, pause and wait to be killed;
1042 // If we have time change only, flush and restart fetching.
1043
1044 if (mFormatChangePending) {
1045 mPaused = true;
1046 } else if (mTimeChangePending) {
1047 if (flushOnTimeChange) {
Marco Nelissen421f47c2015-03-25 14:40:32 -07001048 doFlush(false /* notifyComplete */);
1049 signalResume(false /* notifyComplete */);
Chong Zhang66704af2015-03-03 19:32:35 -08001050 }
Chong Zhang66704af2015-03-03 19:32:35 -08001051 }
1052
1053 // Notify NuPlayer to either shutdown decoder, or rescan sources
1054 sp<AMessage> msg = mNotify->dup();
1055 msg->setInt32("what", kWhatInputDiscontinuity);
1056 msg->setInt32("formatChange", mFormatChangePending);
1057 msg->post();
1058
1059 mFormatChangePending = false;
1060 mTimeChangePending = false;
Lajos Molnar1cd13982014-01-17 15:12:51 -08001061}
1062
Chong Zhang7137ec72014-11-12 16:41:05 -08001063bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
1064 const sp<AMessage> &targetFormat) const {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001065 if (targetFormat == NULL) {
1066 return true;
1067 }
1068
1069 AString mime;
1070 if (!targetFormat->findString("mime", &mime)) {
1071 return false;
1072 }
1073
1074 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1075 // field-by-field comparison
1076 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1077 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1078 int32_t oldVal, newVal;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001079 if (!mInputFormat->findInt32(keys[i], &oldVal) ||
Lajos Molnar1cd13982014-01-17 15:12:51 -08001080 !targetFormat->findInt32(keys[i], &newVal) ||
1081 oldVal != newVal) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001082 return false;
1083 }
1084 }
1085
1086 sp<ABuffer> oldBuf, newBuf;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001087 if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
Lajos Molnar1cd13982014-01-17 15:12:51 -08001088 targetFormat->findBuffer("csd-0", &newBuf)) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001089 if (oldBuf->size() != newBuf->size()) {
1090 return false;
1091 }
1092 return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
1093 }
1094 }
1095 return false;
1096}
1097
1098bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
joakim johansson7abbd4c2015-01-30 14:16:03 +01001099 if (mInputFormat == NULL) {
Robert Shih6d0a94e2014-01-23 16:18:22 -08001100 return false;
1101 }
1102
1103 if (targetFormat == NULL) {
1104 return true;
1105 }
1106
1107 AString oldMime, newMime;
joakim johansson7abbd4c2015-01-30 14:16:03 +01001108 if (!mInputFormat->findString("mime", &oldMime)
Robert Shih6d0a94e2014-01-23 16:18:22 -08001109 || !targetFormat->findString("mime", &newMime)
1110 || !(oldMime == newMime)) {
1111 return false;
1112 }
1113
1114 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1115 bool seamless;
1116 if (audio) {
1117 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1118 } else {
Lajos Molnar1cd13982014-01-17 15:12:51 -08001119 int32_t isAdaptive;
1120 seamless = (mCodec != NULL &&
1121 mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
1122 isAdaptive);
Robert Shih6d0a94e2014-01-23 16:18:22 -08001123 }
1124
1125 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1126 return seamless;
1127}
1128
Chong Zhang7137ec72014-11-12 16:41:05 -08001129void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1130 if (format == NULL) {
Chong Zhangb86e68f2014-08-01 13:46:53 -07001131 return;
1132 }
Chong Zhang7137ec72014-11-12 16:41:05 -08001133 mCSDsForCurrentFormat.clear();
1134 for (int32_t i = 0; ; ++i) {
1135 AString tag = "csd-";
1136 tag.append(i);
1137 sp<ABuffer> buffer;
1138 if (!format->findBuffer(tag.c_str(), &buffer)) {
1139 break;
1140 }
1141 mCSDsForCurrentFormat.push(buffer);
Chong Zhanga7fa1d92014-06-11 14:49:23 -07001142 }
Chong Zhangb86e68f2014-08-01 13:46:53 -07001143}
1144
Chong Zhangf8d71772014-11-26 15:08:34 -08001145void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
1146 if (mResumePending) {
1147 mResumePending = false;
1148
1149 sp<AMessage> notify = mNotify->dup();
1150 notify->setInt32("what", kWhatResumeCompleted);
1151 notify->post();
1152 }
1153}
1154
Andreas Huberf9334412010-12-15 15:17:42 -08001155} // namespace android
1156