blob: 25d41f379bc17d536f1d91027f06af6ead3cda43 [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 The Android Open Source Project
3 *
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 "NuPlayer2Decoder"
19#include <utils/Log.h>
20#include <inttypes.h>
21
22#include <algorithm>
23
Wei Jia53692fa2017-12-11 10:33:46 -080024#include "NuPlayer2CCDecoder.h"
25#include "NuPlayer2Decoder.h"
26#include "NuPlayer2Drm.h"
27#include "NuPlayer2Renderer.h"
28#include "NuPlayer2Source.h"
29
30#include <cutils/properties.h>
31#include <media/MediaCodecBuffer.h>
32#include <media/NdkMediaCodec.h>
Wei Jia28288fb2017-12-15 13:45:29 -080033#include <media/NdkWrapper.h>
Wei Jia53692fa2017-12-11 10:33:46 -080034#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ADebug.h>
36#include <media/stagefright/foundation/AMessage.h>
37#include <media/stagefright/foundation/avc_utils.h>
38#include <media/stagefright/MediaBuffer.h>
39#include <media/stagefright/MediaDefs.h>
40#include <media/stagefright/MediaErrors.h>
41#include <media/stagefright/SurfaceUtils.h>
Wei Jia53692fa2017-12-11 10:33:46 -080042
43#include "ATSParser.h"
44
45namespace android {
46
47static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display
48
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
53static inline bool getAudioDeepBufferSetting() {
54 return property_get_bool("media.stagefright.audio.deep", false /* default_value */);
55}
56
57NuPlayer2::Decoder::Decoder(
58 const sp<AMessage> &notify,
59 const sp<Source> &source,
60 pid_t pid,
61 uid_t uid,
62 const sp<Renderer> &renderer,
Wei Jia28288fb2017-12-15 13:45:29 -080063 const sp<ANativeWindowWrapper> &nww,
Wei Jia53692fa2017-12-11 10:33:46 -080064 const sp<CCDecoder> &ccDecoder)
65 : DecoderBase(notify),
Wei Jia28288fb2017-12-15 13:45:29 -080066 mNativeWindow(nww),
Wei Jia53692fa2017-12-11 10:33:46 -080067 mSource(source),
68 mRenderer(renderer),
69 mCCDecoder(ccDecoder),
70 mPid(pid),
71 mUid(uid),
72 mSkipRenderingUntilMediaTimeUs(-1ll),
73 mNumFramesTotal(0ll),
74 mNumInputFramesDropped(0ll),
75 mNumOutputFramesDropped(0ll),
76 mVideoWidth(0),
77 mVideoHeight(0),
78 mIsAudio(true),
79 mIsVideoAVC(false),
80 mIsSecure(false),
81 mIsEncrypted(false),
82 mIsEncryptedObservedEarlier(false),
83 mFormatChangePending(false),
84 mTimeChangePending(false),
85 mFrameRateTotal(kDefaultVideoFrameRateTotal),
86 mPlaybackSpeed(1.0f),
87 mNumVideoTemporalLayerTotal(1), // decode all layers
88 mNumVideoTemporalLayerAllowed(1),
89 mCurrentMaxVideoTemporalLayerId(0),
90 mResumePending(false),
91 mComponentName("decoder") {
92 mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal;
93}
94
95NuPlayer2::Decoder::~Decoder() {
96 // Need to stop looper first since mCodec could be accessed on the mDecoderLooper.
97 stopLooper();
98 if (mCodec != NULL) {
99 mCodec->release();
100 }
101 releaseAndResetMediaBuffers();
102}
103
104sp<AMessage> NuPlayer2::Decoder::getStats() const {
105 mStats->setInt64("frames-total", mNumFramesTotal);
106 mStats->setInt64("frames-dropped-input", mNumInputFramesDropped);
107 mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped);
108 return mStats;
109}
110
Wei Jia28288fb2017-12-15 13:45:29 -0800111status_t NuPlayer2::Decoder::setVideoSurface(const sp<ANativeWindowWrapper> &nww) {
112 if (nww == NULL || nww->getANativeWindow() == NULL
113 || ADebug::isExperimentEnabled("legacy-setsurface")) {
Wei Jia53692fa2017-12-11 10:33:46 -0800114 return BAD_VALUE;
115 }
116
117 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this);
118
Wei Jia28288fb2017-12-15 13:45:29 -0800119 msg->setObject("surface", nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800120 sp<AMessage> response;
121 status_t err = msg->postAndAwaitResponse(&response);
122 if (err == OK && response != NULL) {
123 CHECK(response->findInt32("err", &err));
124 }
125 return err;
126}
127
128void NuPlayer2::Decoder::onMessageReceived(const sp<AMessage> &msg) {
129 ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
130
131 switch (msg->what()) {
132 case kWhatCodecNotify:
133 {
134 int32_t cbID;
135 CHECK(msg->findInt32("callbackID", &cbID));
136
137 ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d",
138 mIsAudio ? "audio" : "video", cbID, mPaused);
139
140 if (mPaused) {
141 break;
142 }
143
144 switch (cbID) {
145 case AMediaCodecWrapper::CB_INPUT_AVAILABLE:
146 {
147 int32_t index;
148 CHECK(msg->findInt32("index", &index));
149
150 handleAnInputBuffer(index);
151 break;
152 }
153
154 case AMediaCodecWrapper::CB_OUTPUT_AVAILABLE:
155 {
156 int32_t index;
157 size_t offset;
158 size_t size;
159 int64_t timeUs;
160 int32_t flags;
161
162 CHECK(msg->findInt32("index", &index));
163 CHECK(msg->findSize("offset", &offset));
164 CHECK(msg->findSize("size", &size));
165 CHECK(msg->findInt64("timeUs", &timeUs));
166 CHECK(msg->findInt32("flags", &flags));
167
168 handleAnOutputBuffer(index, offset, size, timeUs, flags);
169 break;
170 }
171
172 case AMediaCodecWrapper::CB_OUTPUT_FORMAT_CHANGED:
173 {
174 sp<AMessage> format;
175 CHECK(msg->findMessage("format", &format));
176
177 handleOutputFormatChange(format);
178 break;
179 }
180
181 case AMediaCodecWrapper::CB_ERROR:
182 {
183 status_t err;
184 CHECK(msg->findInt32("err", &err));
185 ALOGE("Decoder (%s) reported error : 0x%x",
186 mIsAudio ? "audio" : "video", err);
187
188 handleError(err);
189 break;
190 }
191
192 default:
193 {
194 TRESPASS();
195 break;
196 }
197 }
198
199 break;
200 }
201
202 case kWhatRenderBuffer:
203 {
204 if (!isStaleReply(msg)) {
205 onRenderBuffer(msg);
206 }
207 break;
208 }
209
210 case kWhatAudioOutputFormatChanged:
211 {
212 if (!isStaleReply(msg)) {
213 status_t err;
214 if (msg->findInt32("err", &err) && err != OK) {
215 ALOGE("Renderer reported 0x%x when changing audio output format", err);
216 handleError(err);
217 }
218 }
219 break;
220 }
221
222 case kWhatSetVideoSurface:
223 {
224 sp<AReplyToken> replyID;
225 CHECK(msg->senderAwaitsResponse(&replyID));
226
227 sp<RefBase> obj;
228 CHECK(msg->findObject("surface", &obj));
Wei Jia28288fb2017-12-15 13:45:29 -0800229 sp<ANativeWindowWrapper> nww =
230 static_cast<ANativeWindowWrapper *>(obj.get()); // non-null
231 if (nww == NULL || nww->getANativeWindow() == NULL) {
232 break;
233 }
Wei Jia53692fa2017-12-11 10:33:46 -0800234 int32_t err = INVALID_OPERATION;
Wei Jia28288fb2017-12-15 13:45:29 -0800235 // NOTE: in practice mNativeWindow is always non-null,
236 // but checking here for completeness
237 if (mCodec != NULL
238 && mNativeWindow != NULL && mNativeWindow->getANativeWindow() != NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -0800239 // TODO: once AwesomePlayer is removed, remove this automatic connecting
240 // to the surface by MediaPlayerService.
241 //
Wei Jia28288fb2017-12-15 13:45:29 -0800242 // at this point MediaPlayer2Manager::client has already connected to the
Wei Jia53692fa2017-12-11 10:33:46 -0800243 // surface, which MediaCodec does not expect
Wei Jia28288fb2017-12-15 13:45:29 -0800244 err = nativeWindowDisconnect(nww->getANativeWindow(), "kWhatSetVideoSurface(nww)");
Wei Jia53692fa2017-12-11 10:33:46 -0800245 if (err == OK) {
Wei Jia28288fb2017-12-15 13:45:29 -0800246 err = mCodec->setOutputSurface(nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800247 ALOGI_IF(err, "codec setOutputSurface returned: %d", err);
248 if (err == OK) {
249 // reconnect to the old surface as MPS::Client will expect to
250 // be able to disconnect from it.
Wei Jia28288fb2017-12-15 13:45:29 -0800251 (void)nativeWindowConnect(mNativeWindow->getANativeWindow(),
252 "kWhatSetVideoSurface(mNativeWindow)");
253 mNativeWindow = nww;
Wei Jia53692fa2017-12-11 10:33:46 -0800254 }
255 }
256 if (err != OK) {
257 // reconnect to the new surface on error as MPS::Client will expect to
258 // be able to disconnect from it.
Wei Jia28288fb2017-12-15 13:45:29 -0800259 (void)nativeWindowConnect(nww->getANativeWindow(), "kWhatSetVideoSurface(err)");
Wei Jia53692fa2017-12-11 10:33:46 -0800260 }
261 }
262
263 sp<AMessage> response = new AMessage;
264 response->setInt32("err", err);
265 response->postReply(replyID);
266 break;
267 }
268
269 case kWhatDrmReleaseCrypto:
270 {
271 ALOGV("kWhatDrmReleaseCrypto");
272 onReleaseCrypto(msg);
273 break;
274 }
275
276 default:
277 DecoderBase::onMessageReceived(msg);
278 break;
279 }
280}
281
282void NuPlayer2::Decoder::onConfigure(const sp<AMessage> &format) {
283 ALOGV("[%s] onConfigure (format=%s)", mComponentName.c_str(), format->debugString().c_str());
284 CHECK(mCodec == NULL);
285
286 mFormatChangePending = false;
287 mTimeChangePending = false;
288
289 ++mBufferGeneration;
290
291 AString mime;
292 CHECK(format->findString("mime", &mime));
293
294 mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
295 mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
296
297 mComponentName = mime;
298 mComponentName.append(" decoder");
Wei Jia28288fb2017-12-15 13:45:29 -0800299 ALOGV("[%s] onConfigure (nww=%p)", mComponentName.c_str(),
300 (mNativeWindow == NULL ? NULL : mNativeWindow->getANativeWindow()));
Wei Jia53692fa2017-12-11 10:33:46 -0800301
302 mCodec = AMediaCodecWrapper::CreateDecoderByType(mime);
303 int32_t secure = 0;
304 if (format->findInt32("secure", &secure) && secure != 0) {
305 if (mCodec != NULL) {
306 if (mCodec->getName(&mComponentName) == OK) {
307 mComponentName.append(".secure");
308 mCodec->release();
309 ALOGI("[%s] creating", mComponentName.c_str());
310 mCodec = AMediaCodecWrapper::CreateCodecByName(mComponentName);
311 } else {
312 mCodec = NULL;
313 }
314 }
315 }
316 if (mCodec == NULL) {
317 ALOGE("Failed to create %s%s decoder",
318 (secure ? "secure " : ""), mime.c_str());
319 handleError(NO_INIT);
320 return;
321 }
322 mIsSecure = secure;
323
324 mCodec->getName(&mComponentName);
325
326 status_t err;
Wei Jia28288fb2017-12-15 13:45:29 -0800327 if (mNativeWindow != NULL && mNativeWindow->getANativeWindow() != NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -0800328 // disconnect from surface as MediaCodec will reconnect
Wei Jia28288fb2017-12-15 13:45:29 -0800329 err = nativeWindowDisconnect(mNativeWindow->getANativeWindow(), "onConfigure");
Wei Jia53692fa2017-12-11 10:33:46 -0800330 // We treat this as a warning, as this is a preparatory step.
331 // Codec will try to connect to the surface, which is where
332 // any error signaling will occur.
333 ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
334 }
335
336 // Modular DRM
337 sp<RefBase> objCrypto;
338 format->findObject("crypto", &objCrypto);
339 sp<AMediaCryptoWrapper> crypto = static_cast<AMediaCryptoWrapper *>(objCrypto.get());
340 // non-encrypted source won't have a crypto
341 mIsEncrypted = (crypto != NULL);
342 // configure is called once; still using OR in case the behavior changes.
343 mIsEncryptedObservedEarlier = mIsEncryptedObservedEarlier || mIsEncrypted;
344 ALOGV("onConfigure mCrypto: %p, mIsSecure: %d", crypto.get(), mIsSecure);
345
346 err = mCodec->configure(
347 AMediaFormatWrapper::Create(format),
Wei Jia28288fb2017-12-15 13:45:29 -0800348 mNativeWindow,
Wei Jia53692fa2017-12-11 10:33:46 -0800349 crypto,
350 0 /* flags */);
351
352 if (err != OK) {
353 ALOGE("Failed to configure [%s] decoder (err=%d)", mComponentName.c_str(), err);
354 mCodec->release();
355 mCodec.clear();
356 handleError(err);
357 return;
358 }
359 rememberCodecSpecificData(format);
360
361 // the following should work in configured state
362 sp<AMediaFormatWrapper> outputFormat = mCodec->getOutputFormat();
363 if (outputFormat == NULL) {
364 handleError(INVALID_OPERATION);
365 return;
366 }
367 mInputFormat = mCodec->getInputFormat();
368 if (mInputFormat == NULL) {
369 handleError(INVALID_OPERATION);
370 return;
371 }
372
373 mStats->setString("mime", mime.c_str());
374 mStats->setString("component-name", mComponentName.c_str());
375
376 if (!mIsAudio) {
377 int32_t width, height;
378 if (outputFormat->getInt32("width", &width)
379 && outputFormat->getInt32("height", &height)) {
380 mStats->setInt32("width", width);
381 mStats->setInt32("height", height);
382 }
383 }
384
385 sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
386 mCodec->setCallback(reply);
387
388 err = mCodec->start();
389 if (err != OK) {
390 ALOGE("Failed to start [%s] decoder (err=%d)", mComponentName.c_str(), err);
391 mCodec->release();
392 mCodec.clear();
393 handleError(err);
394 return;
395 }
396
397 releaseAndResetMediaBuffers();
398
399 mPaused = false;
400 mResumePending = false;
401}
402
403void NuPlayer2::Decoder::onSetParameters(const sp<AMessage> &params) {
404 bool needAdjustLayers = false;
405 float frameRateTotal;
406 if (params->findFloat("frame-rate-total", &frameRateTotal)
407 && mFrameRateTotal != frameRateTotal) {
408 needAdjustLayers = true;
409 mFrameRateTotal = frameRateTotal;
410 }
411
412 int32_t numVideoTemporalLayerTotal;
413 if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal)
414 && numVideoTemporalLayerTotal >= 0
415 && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers
416 && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) {
417 needAdjustLayers = true;
418 mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1);
419 }
420
421 if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) {
422 // TODO: For now, layer fps is calculated for some specific architectures.
423 // But it really should be extracted from the stream.
424 mVideoTemporalLayerAggregateFps[0] =
425 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - 1));
426 for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) {
427 mVideoTemporalLayerAggregateFps[i] =
428 mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - i))
429 + mVideoTemporalLayerAggregateFps[i - 1];
430 }
431 }
432
433 float playbackSpeed;
434 if (params->findFloat("playback-speed", &playbackSpeed)
435 && mPlaybackSpeed != playbackSpeed) {
436 needAdjustLayers = true;
437 mPlaybackSpeed = playbackSpeed;
438 }
439
440 if (needAdjustLayers) {
441 float decodeFrameRate = mFrameRateTotal;
442 // enable temporal layering optimization only if we know the layering depth
443 if (mNumVideoTemporalLayerTotal > 1) {
444 int32_t layerId;
445 for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) {
446 if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed
447 >= kDisplayRefreshingRate * 0.9) {
448 break;
449 }
450 }
451 mNumVideoTemporalLayerAllowed = layerId + 1;
452 decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId];
453 }
454 ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g",
455 mNumVideoTemporalLayerAllowed, decodeFrameRate);
456
457 if (mCodec == NULL) {
458 ALOGW("onSetParameters called before codec is created.");
459 return;
460 }
461
462 sp<AMediaFormatWrapper> codecParams = new AMediaFormatWrapper();
463 codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed);
464 mCodec->setParameters(codecParams);
465 }
466}
467
468void NuPlayer2::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
469 mRenderer = renderer;
470}
471
472void NuPlayer2::Decoder::onResume(bool notifyComplete) {
473 mPaused = false;
474
475 if (notifyComplete) {
476 mResumePending = true;
477 }
478
479 if (mCodec == NULL) {
480 ALOGE("[%s] onResume without a valid codec", mComponentName.c_str());
481 handleError(NO_INIT);
482 return;
483 }
484 mCodec->start();
485}
486
487void NuPlayer2::Decoder::doFlush(bool notifyComplete) {
488 if (mCCDecoder != NULL) {
489 mCCDecoder->flush();
490 }
491
492 if (mRenderer != NULL) {
493 mRenderer->flush(mIsAudio, notifyComplete);
494 mRenderer->signalTimeDiscontinuity();
495 }
496
497 status_t err = OK;
498 if (mCodec != NULL) {
499 err = mCodec->flush();
500 mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
501 ++mBufferGeneration;
502 }
503
504 if (err != OK) {
505 ALOGE("failed to flush [%s] (err=%d)", mComponentName.c_str(), err);
506 handleError(err);
507 // finish with posting kWhatFlushCompleted.
508 // we attempt to release the buffers even if flush fails.
509 }
510 releaseAndResetMediaBuffers();
511 mPaused = true;
512}
513
514
515void NuPlayer2::Decoder::onFlush() {
516 doFlush(true);
517
518 if (isDiscontinuityPending()) {
519 // This could happen if the client starts seeking/shutdown
520 // after we queued an EOS for discontinuities.
521 // We can consider discontinuity handled.
522 finishHandleDiscontinuity(false /* flushOnTimeChange */);
523 }
524
525 sp<AMessage> notify = mNotify->dup();
526 notify->setInt32("what", kWhatFlushCompleted);
527 notify->post();
528}
529
530void NuPlayer2::Decoder::onShutdown(bool notifyComplete) {
531 status_t err = OK;
532
533 // if there is a pending resume request, notify complete now
534 notifyResumeCompleteIfNecessary();
535
536 if (mCodec != NULL) {
537 err = mCodec->release();
538 mCodec = NULL;
539 ++mBufferGeneration;
540
Wei Jia28288fb2017-12-15 13:45:29 -0800541 if (mNativeWindow != NULL && mNativeWindow->getANativeWindow() != NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -0800542 // reconnect to surface as MediaCodec disconnected from it
Wei Jia28288fb2017-12-15 13:45:29 -0800543 status_t error = nativeWindowConnect(mNativeWindow->getANativeWindow(), "onShutdown");
Wei Jia53692fa2017-12-11 10:33:46 -0800544 ALOGW_IF(error != NO_ERROR,
545 "[%s] failed to connect to native window, error=%d",
546 mComponentName.c_str(), error);
547 }
548 mComponentName = "decoder";
549 }
550
551 releaseAndResetMediaBuffers();
552
553 if (err != OK) {
554 ALOGE("failed to release [%s] (err=%d)", mComponentName.c_str(), err);
555 handleError(err);
556 // finish with posting kWhatShutdownCompleted.
557 }
558
559 if (notifyComplete) {
560 sp<AMessage> notify = mNotify->dup();
561 notify->setInt32("what", kWhatShutdownCompleted);
562 notify->post();
563 mPaused = true;
564 }
565}
566
567/*
568 * returns true if we should request more data
569 */
570bool NuPlayer2::Decoder::doRequestBuffers() {
571 if (isDiscontinuityPending()) {
572 return false;
573 }
574 status_t err = OK;
575 while (err == OK && !mDequeuedInputBuffers.empty()) {
576 size_t bufferIx = *mDequeuedInputBuffers.begin();
577 sp<AMessage> msg = new AMessage();
578 msg->setSize("buffer-ix", bufferIx);
579 err = fetchInputData(msg);
580 if (err != OK && err != ERROR_END_OF_STREAM) {
581 // if EOS, need to queue EOS buffer
582 break;
583 }
584 mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
585
586 if (!mPendingInputMessages.empty()
587 || !onInputBufferFetched(msg)) {
588 mPendingInputMessages.push_back(msg);
589 }
590 }
591
592 return err == -EWOULDBLOCK
593 && mSource->feedMoreTSData() == OK;
594}
595
596void NuPlayer2::Decoder::handleError(int32_t err)
597{
598 // We cannot immediately release the codec due to buffers still outstanding
599 // in the renderer. We signal to the player the error so it can shutdown/release the
600 // decoder after flushing and increment the generation to discard unnecessary messages.
601
602 ++mBufferGeneration;
603
604 sp<AMessage> notify = mNotify->dup();
605 notify->setInt32("what", kWhatError);
606 notify->setInt32("err", err);
607 notify->post();
608}
609
610status_t NuPlayer2::Decoder::releaseCrypto()
611{
612 ALOGV("releaseCrypto");
613
614 sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this);
615
616 sp<AMessage> response;
617 status_t status = msg->postAndAwaitResponse(&response);
618 if (status == OK && response != NULL) {
619 CHECK(response->findInt32("status", &status));
620 ALOGV("releaseCrypto ret: %d ", status);
621 } else {
622 ALOGE("releaseCrypto err: %d", status);
623 }
624
625 return status;
626}
627
628void NuPlayer2::Decoder::onReleaseCrypto(const sp<AMessage>& msg)
629{
630 status_t status = INVALID_OPERATION;
631 if (mCodec != NULL) {
632 status = mCodec->releaseCrypto();
633 } else {
634 // returning OK if the codec has been already released
635 status = OK;
636 ALOGE("onReleaseCrypto No mCodec. err: %d", status);
637 }
638
639 sp<AMessage> response = new AMessage;
640 response->setInt32("status", status);
641 // Clearing the state as it's tied to crypto. mIsEncryptedObservedEarlier is sticky though
642 // and lasts for the lifetime of this codec. See its use in fetchInputData.
643 mIsEncrypted = false;
644
645 sp<AReplyToken> replyID;
646 CHECK(msg->senderAwaitsResponse(&replyID));
647 response->postReply(replyID);
648}
649
650bool NuPlayer2::Decoder::handleAnInputBuffer(size_t index) {
651 if (isDiscontinuityPending()) {
652 return false;
653 }
654
655 if (mCodec == NULL) {
656 ALOGE("[%s] handleAnInputBuffer without a valid codec", mComponentName.c_str());
657 handleError(NO_INIT);
658 return false;
659 }
660
661 size_t bufferSize = 0;
662 uint8_t *bufferBase = mCodec->getInputBuffer(index, &bufferSize);
663
664 if (bufferBase == NULL) {
665 ALOGE("[%s] handleAnInputBuffer, failed to get input buffer", mComponentName.c_str());
666 handleError(UNKNOWN_ERROR);
667 return false;
668 }
669
670 sp<MediaCodecBuffer> buffer =
671 new MediaCodecBuffer(NULL /* format */, new ABuffer(bufferBase, bufferSize));
672
673 if (index >= mInputBuffers.size()) {
674 for (size_t i = mInputBuffers.size(); i <= index; ++i) {
675 mInputBuffers.add();
676 mMediaBuffers.add();
677 mInputBufferIsDequeued.add();
678 mMediaBuffers.editItemAt(i) = NULL;
679 mInputBufferIsDequeued.editItemAt(i) = false;
680 }
681 }
682 mInputBuffers.editItemAt(index) = buffer;
683
684 //CHECK_LT(bufferIx, mInputBuffers.size());
685
686 if (mMediaBuffers[index] != NULL) {
687 mMediaBuffers[index]->release();
688 mMediaBuffers.editItemAt(index) = NULL;
689 }
690 mInputBufferIsDequeued.editItemAt(index) = true;
691
692 if (!mCSDsToSubmit.isEmpty()) {
693 sp<AMessage> msg = new AMessage();
694 msg->setSize("buffer-ix", index);
695
696 sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
697 ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
698 msg->setBuffer("buffer", buffer);
699 mCSDsToSubmit.removeAt(0);
700 if (!onInputBufferFetched(msg)) {
701 handleError(UNKNOWN_ERROR);
702 return false;
703 }
704 return true;
705 }
706
707 while (!mPendingInputMessages.empty()) {
708 sp<AMessage> msg = *mPendingInputMessages.begin();
709 if (!onInputBufferFetched(msg)) {
710 break;
711 }
712 mPendingInputMessages.erase(mPendingInputMessages.begin());
713 }
714
715 if (!mInputBufferIsDequeued.editItemAt(index)) {
716 return true;
717 }
718
719 mDequeuedInputBuffers.push_back(index);
720
721 onRequestInputBuffers();
722 return true;
723}
724
725bool NuPlayer2::Decoder::handleAnOutputBuffer(
726 size_t index,
727 size_t offset,
728 size_t size,
729 int64_t timeUs,
730 int32_t flags) {
731 if (mCodec == NULL) {
732 ALOGE("[%s] handleAnOutputBuffer without a valid codec", mComponentName.c_str());
733 handleError(NO_INIT);
734 return false;
735 }
736
737// CHECK_LT(bufferIx, mOutputBuffers.size());
738
739 size_t bufferSize = 0;
740 uint8_t *bufferBase = mCodec->getOutputBuffer(index, &bufferSize);
741
742 if (bufferBase == NULL) {
743 ALOGE("[%s] handleAnOutputBuffer, failed to get output buffer", mComponentName.c_str());
744 handleError(UNKNOWN_ERROR);
745 return false;
746 }
747
748 sp<MediaCodecBuffer> buffer =
749 new MediaCodecBuffer(NULL /* format */, new ABuffer(bufferBase, bufferSize));
750
751 if (index >= mOutputBuffers.size()) {
752 for (size_t i = mOutputBuffers.size(); i <= index; ++i) {
753 mOutputBuffers.add();
754 }
755 }
756
757 mOutputBuffers.editItemAt(index) = buffer;
758
759 buffer->setRange(offset, size);
760 buffer->meta()->clear();
761 buffer->meta()->setInt64("timeUs", timeUs);
762
763 bool eos = flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
764 // we do not expect CODECCONFIG or SYNCFRAME for decoder
765
766 sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
767 reply->setSize("buffer-ix", index);
768 reply->setInt32("generation", mBufferGeneration);
769
770 if (eos) {
771 ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
772
773 buffer->meta()->setInt32("eos", true);
774 reply->setInt32("eos", true);
775 }
776
Wei Jia5ed2faf2017-12-14 16:07:21 -0800777 mNumFramesTotal += !mIsAudio;
778
Wei Jia53692fa2017-12-11 10:33:46 -0800779 if (mSkipRenderingUntilMediaTimeUs >= 0) {
780 if (timeUs < mSkipRenderingUntilMediaTimeUs) {
781 ALOGV("[%s] dropping buffer at time %lld as requested.",
782 mComponentName.c_str(), (long long)timeUs);
783
784 reply->post();
785 if (eos) {
786 notifyResumeCompleteIfNecessary();
787 if (mRenderer != NULL && !isDiscontinuityPending()) {
788 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
789 }
790 }
791 return true;
792 }
793
794 mSkipRenderingUntilMediaTimeUs = -1;
795 }
796
Wei Jia53692fa2017-12-11 10:33:46 -0800797 // wait until 1st frame comes out to signal resume complete
798 notifyResumeCompleteIfNecessary();
799
800 if (mRenderer != NULL) {
801 // send the buffer to renderer.
802 mRenderer->queueBuffer(mIsAudio, buffer, reply);
803 if (eos && !isDiscontinuityPending()) {
804 mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
805 }
806 }
807
808 return true;
809}
810
811void NuPlayer2::Decoder::handleOutputFormatChange(const sp<AMessage> &format) {
812 if (!mIsAudio) {
813 int32_t width, height;
814 if (format->findInt32("width", &width)
815 && format->findInt32("height", &height)) {
816 mStats->setInt32("width", width);
817 mStats->setInt32("height", height);
818 }
819 sp<AMessage> notify = mNotify->dup();
820 notify->setInt32("what", kWhatVideoSizeChanged);
821 notify->setMessage("format", format);
822 notify->post();
823 } else if (mRenderer != NULL) {
824 uint32_t flags;
825 int64_t durationUs;
826 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
827 if (getAudioDeepBufferSetting() // override regardless of source duration
828 || (mSource->getDuration(&durationUs) == OK
829 && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) {
830 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
831 } else {
832 flags = AUDIO_OUTPUT_FLAG_NONE;
833 }
834
835 sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this);
836 reply->setInt32("generation", mBufferGeneration);
837 mRenderer->changeAudioFormat(
838 format, false /* offloadOnly */, hasVideo,
839 flags, mSource->isStreaming(), reply);
840 }
841}
842
843void NuPlayer2::Decoder::releaseAndResetMediaBuffers() {
844 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
845 if (mMediaBuffers[i] != NULL) {
846 mMediaBuffers[i]->release();
847 mMediaBuffers.editItemAt(i) = NULL;
848 }
849 }
850 mMediaBuffers.resize(mInputBuffers.size());
851 for (size_t i = 0; i < mMediaBuffers.size(); i++) {
852 mMediaBuffers.editItemAt(i) = NULL;
853 }
854 mInputBufferIsDequeued.clear();
855 mInputBufferIsDequeued.resize(mInputBuffers.size());
856 for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
857 mInputBufferIsDequeued.editItemAt(i) = false;
858 }
859
860 mPendingInputMessages.clear();
861 mDequeuedInputBuffers.clear();
862 mSkipRenderingUntilMediaTimeUs = -1;
863}
864
865bool NuPlayer2::Decoder::isStaleReply(const sp<AMessage> &msg) {
866 int32_t generation;
867 CHECK(msg->findInt32("generation", &generation));
868 return generation != mBufferGeneration;
869}
870
871status_t NuPlayer2::Decoder::fetchInputData(sp<AMessage> &reply) {
872 sp<ABuffer> accessUnit;
873 bool dropAccessUnit = true;
874 do {
875 status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
876
877 if (err == -EWOULDBLOCK) {
878 return err;
879 } else if (err != OK) {
880 if (err == INFO_DISCONTINUITY) {
881 int32_t type;
882 CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
883
884 bool formatChange =
885 (mIsAudio &&
886 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
887 || (!mIsAudio &&
888 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
889
890 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
891
892 ALOGI("%s discontinuity (format=%d, time=%d)",
893 mIsAudio ? "audio" : "video", formatChange, timeChange);
894
895 bool seamlessFormatChange = false;
896 sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
897 if (formatChange) {
898 seamlessFormatChange =
899 supportsSeamlessFormatChange(newFormat);
900 // treat seamless format change separately
901 formatChange = !seamlessFormatChange;
902 }
903
904 // For format or time change, return EOS to queue EOS input,
905 // then wait for EOS on output.
906 if (formatChange /* not seamless */) {
907 mFormatChangePending = true;
908 err = ERROR_END_OF_STREAM;
909 } else if (timeChange) {
910 rememberCodecSpecificData(newFormat);
911 mTimeChangePending = true;
912 err = ERROR_END_OF_STREAM;
913 } else if (seamlessFormatChange) {
914 // reuse existing decoder and don't flush
915 rememberCodecSpecificData(newFormat);
916 continue;
917 } else {
918 // This stream is unaffected by the discontinuity
919 return -EWOULDBLOCK;
920 }
921 }
922
923 // reply should only be returned without a buffer set
924 // when there is an error (including EOS)
925 CHECK(err != OK);
926
927 reply->setInt32("err", err);
928 return ERROR_END_OF_STREAM;
929 }
930
931 dropAccessUnit = false;
932 if (!mIsAudio && !mIsEncrypted) {
933 // Extra safeguard if higher-level behavior changes. Otherwise, not required now.
934 // Preventing the buffer from being processed (and sent to codec) if this is a later
935 // round of playback but this time without prepareDrm. Or if there is a race between
936 // stop (which is not blocking) and releaseDrm allowing buffers being processed after
937 // Crypto has been released (GenericSource currently prevents this race though).
938 // Particularly doing this check before IsAVCReferenceFrame call to prevent parsing
939 // of encrypted data.
940 if (mIsEncryptedObservedEarlier) {
941 ALOGE("fetchInputData: mismatched mIsEncrypted/mIsEncryptedObservedEarlier (0/1)");
942
943 return INVALID_OPERATION;
944 }
945
946 int32_t layerId = 0;
947 bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId);
948 if (mRenderer->getVideoLateByUs() > 100000ll
949 && mIsVideoAVC
950 && !IsAVCReferenceFrame(accessUnit)) {
951 dropAccessUnit = true;
952 } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) {
953 // Add only one layer each time.
954 if (layerId > mCurrentMaxVideoTemporalLayerId + 1
955 || layerId >= mNumVideoTemporalLayerAllowed) {
956 dropAccessUnit = true;
957 ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d",
958 layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed,
959 mCurrentMaxVideoTemporalLayerId);
960 } else if (layerId > mCurrentMaxVideoTemporalLayerId) {
961 mCurrentMaxVideoTemporalLayerId = layerId;
962 } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1
963 && IsIDR(accessUnit->data(), accessUnit->size())) {
964 mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1;
965 }
966 }
967 if (dropAccessUnit) {
968 if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) {
969 mCurrentMaxVideoTemporalLayerId = layerId - 1;
970 }
971 ++mNumInputFramesDropped;
972 }
973 }
974 } while (dropAccessUnit);
975
976 // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
977#if 0
978 int64_t mediaTimeUs;
979 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
980 ALOGV("[%s] feeding input buffer at media time %.3f",
981 mIsAudio ? "audio" : "video",
982 mediaTimeUs / 1E6);
983#endif
984
985 if (mCCDecoder != NULL) {
986 mCCDecoder->decode(accessUnit);
987 }
988
989 reply->setBuffer("buffer", accessUnit);
990
991 return OK;
992}
993
994bool NuPlayer2::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
995 if (mCodec == NULL) {
996 ALOGE("[%s] onInputBufferFetched without a valid codec", mComponentName.c_str());
997 handleError(NO_INIT);
998 return false;
999 }
1000
1001 size_t bufferIx;
1002 CHECK(msg->findSize("buffer-ix", &bufferIx));
1003 CHECK_LT(bufferIx, mInputBuffers.size());
1004 sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx];
1005
1006 sp<ABuffer> buffer;
1007 bool hasBuffer = msg->findBuffer("buffer", &buffer);
1008 bool needsCopy = true;
1009
1010 if (buffer == NULL /* includes !hasBuffer */) {
1011 int32_t streamErr = ERROR_END_OF_STREAM;
1012 CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
1013
1014 CHECK(streamErr != OK);
1015
1016 // attempt to queue EOS
1017 status_t err = mCodec->queueInputBuffer(
1018 bufferIx,
1019 0,
1020 0,
1021 0,
1022 AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM);
1023 if (err == OK) {
1024 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
1025 } else if (streamErr == ERROR_END_OF_STREAM) {
1026 streamErr = err;
1027 // err will not be ERROR_END_OF_STREAM
1028 }
1029
1030 if (streamErr != ERROR_END_OF_STREAM) {
1031 ALOGE("Stream error for [%s] (err=%d), EOS %s queued",
1032 mComponentName.c_str(),
1033 streamErr,
1034 err == OK ? "successfully" : "unsuccessfully");
1035 handleError(streamErr);
1036 }
1037 } else {
1038 sp<AMessage> extra;
1039 if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
1040 int64_t resumeAtMediaTimeUs;
1041 if (extra->findInt64(
1042 "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
1043 ALOGI("[%s] suppressing rendering until %lld us",
1044 mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
1045 mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
1046 }
1047 }
1048
1049 int64_t timeUs = 0;
1050 uint32_t flags = 0;
1051 CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1052
1053 int32_t eos, csd;
1054 // we do not expect SYNCFRAME for decoder
1055 if (buffer->meta()->findInt32("eos", &eos) && eos) {
1056 flags |= AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
1057 } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
1058 flags |= AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG;
1059 }
1060
1061 // Modular DRM
1062 MediaBuffer *mediaBuf = NULL;
1063 sp<AMediaCodecCryptoInfoWrapper> cryptInfo;
1064
1065 // copy into codec buffer
1066 if (needsCopy) {
1067 if (buffer->size() > codecBuffer->capacity()) {
1068 handleError(ERROR_BUFFER_TOO_SMALL);
1069 mDequeuedInputBuffers.push_back(bufferIx);
1070 return false;
1071 }
1072
1073 if (buffer->data() != NULL) {
1074 codecBuffer->setRange(0, buffer->size());
1075 memcpy(codecBuffer->data(), buffer->data(), buffer->size());
1076 } else { // No buffer->data()
1077 //Modular DRM
1078 mediaBuf = (MediaBuffer*)buffer->getMediaBufferBase();
1079 if (mediaBuf != NULL) {
1080 codecBuffer->setRange(0, mediaBuf->size());
1081 memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->size());
1082
1083 sp<MetaData> meta_data = mediaBuf->meta_data();
1084 cryptInfo = AMediaCodecCryptoInfoWrapper::Create(meta_data);
1085
1086 // since getMediaBuffer() has incremented the refCount
1087 mediaBuf->release();
1088 } else { // No mediaBuf
1089 ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p",
1090 buffer.get());
1091 handleError(UNKNOWN_ERROR);
1092 return false;
1093 }
1094 } // buffer->data()
1095 } // needsCopy
1096
1097 status_t err;
1098 if (cryptInfo != NULL) {
1099 err = mCodec->queueSecureInputBuffer(
1100 bufferIx,
1101 codecBuffer->offset(),
1102 cryptInfo,
1103 timeUs,
1104 flags);
1105 // synchronous call so done with cryptInfo here
1106 } else {
1107 err = mCodec->queueInputBuffer(
1108 bufferIx,
1109 codecBuffer->offset(),
1110 codecBuffer->size(),
1111 timeUs,
1112 flags);
1113 } // no cryptInfo
1114
1115 if (err != OK) {
1116 ALOGE("onInputBufferFetched: queue%sInputBuffer failed for [%s] (err=%d)",
1117 (cryptInfo != NULL ? "Secure" : ""),
1118 mComponentName.c_str(), err);
1119 handleError(err);
1120 } else {
1121 mInputBufferIsDequeued.editItemAt(bufferIx) = false;
1122 }
1123
1124 } // buffer != NULL
1125 return true;
1126}
1127
1128void NuPlayer2::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
1129 status_t err;
1130 int32_t render;
1131 size_t bufferIx;
1132 int32_t eos;
1133 CHECK(msg->findSize("buffer-ix", &bufferIx));
1134
1135 if (!mIsAudio) {
1136 int64_t timeUs;
1137 sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx];
1138 buffer->meta()->findInt64("timeUs", &timeUs);
1139
1140 if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
1141 mCCDecoder->display(timeUs);
1142 }
1143 }
1144
1145 if (mCodec == NULL) {
1146 err = NO_INIT;
1147 } else if (msg->findInt32("render", &render) && render) {
1148 int64_t timestampNs;
1149 CHECK(msg->findInt64("timestampNs", &timestampNs));
1150 err = mCodec->releaseOutputBufferAtTime(bufferIx, timestampNs);
1151 } else {
1152 mNumOutputFramesDropped += !mIsAudio;
1153 err = mCodec->releaseOutputBuffer(bufferIx, false /* render */);
1154 }
1155 if (err != OK) {
1156 ALOGE("failed to release output buffer for [%s] (err=%d)",
1157 mComponentName.c_str(), err);
1158 handleError(err);
1159 }
1160 if (msg->findInt32("eos", &eos) && eos
1161 && isDiscontinuityPending()) {
1162 finishHandleDiscontinuity(true /* flushOnTimeChange */);
1163 }
1164}
1165
1166bool NuPlayer2::Decoder::isDiscontinuityPending() const {
1167 return mFormatChangePending || mTimeChangePending;
1168}
1169
1170void NuPlayer2::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
1171 ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
1172 mFormatChangePending, mTimeChangePending, flushOnTimeChange);
1173
1174 // If we have format change, pause and wait to be killed;
1175 // If we have time change only, flush and restart fetching.
1176
1177 if (mFormatChangePending) {
1178 mPaused = true;
1179 } else if (mTimeChangePending) {
1180 if (flushOnTimeChange) {
1181 doFlush(false /* notifyComplete */);
1182 signalResume(false /* notifyComplete */);
1183 }
1184 }
1185
1186 // Notify NuPlayer2 to either shutdown decoder, or rescan sources
1187 sp<AMessage> msg = mNotify->dup();
1188 msg->setInt32("what", kWhatInputDiscontinuity);
1189 msg->setInt32("formatChange", mFormatChangePending);
1190 msg->post();
1191
1192 mFormatChangePending = false;
1193 mTimeChangePending = false;
1194}
1195
1196bool NuPlayer2::Decoder::supportsSeamlessAudioFormatChange(
1197 const sp<AMessage> &targetFormat) const {
1198 if (targetFormat == NULL) {
1199 return true;
1200 }
1201
1202 AString mime;
1203 if (!targetFormat->findString("mime", &mime)) {
1204 return false;
1205 }
1206
1207 if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
1208 // field-by-field comparison
1209 const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
1210 for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
1211 int32_t oldVal, newVal;
1212 if (!mInputFormat->getInt32(keys[i], &oldVal) ||
1213 !targetFormat->findInt32(keys[i], &newVal) ||
1214 oldVal != newVal) {
1215 return false;
1216 }
1217 }
1218
1219 sp<ABuffer> newBuf;
1220 uint8_t *oldBufData = NULL;
1221 size_t oldBufSize = 0;
1222 if (mInputFormat->getBuffer("csd-0", (void**)&oldBufData, &oldBufSize) &&
1223 targetFormat->findBuffer("csd-0", &newBuf)) {
1224 if (oldBufSize != newBuf->size()) {
1225 return false;
1226 }
1227 return !memcmp(oldBufData, newBuf->data(), oldBufSize);
1228 }
1229 }
1230 return false;
1231}
1232
1233bool NuPlayer2::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
1234 if (mInputFormat == NULL) {
1235 return false;
1236 }
1237
1238 if (targetFormat == NULL) {
1239 return true;
1240 }
1241
1242 AString oldMime, newMime;
1243 if (!mInputFormat->getString("mime", &oldMime)
1244 || !targetFormat->findString("mime", &newMime)
1245 || !(oldMime == newMime)) {
1246 return false;
1247 }
1248
1249 bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
1250 bool seamless;
1251 if (audio) {
1252 seamless = supportsSeamlessAudioFormatChange(targetFormat);
1253 } else {
1254 int32_t isAdaptive;
1255 seamless = (mCodec != NULL &&
1256 mInputFormat->getInt32("adaptive-playback", &isAdaptive) &&
1257 isAdaptive);
1258 }
1259
1260 ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
1261 return seamless;
1262}
1263
1264void NuPlayer2::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
1265 if (format == NULL) {
1266 return;
1267 }
1268 mCSDsForCurrentFormat.clear();
1269 for (int32_t i = 0; ; ++i) {
1270 AString tag = "csd-";
1271 tag.append(i);
1272 sp<ABuffer> buffer;
1273 if (!format->findBuffer(tag.c_str(), &buffer)) {
1274 break;
1275 }
1276 mCSDsForCurrentFormat.push(buffer);
1277 }
1278}
1279
1280void NuPlayer2::Decoder::notifyResumeCompleteIfNecessary() {
1281 if (mResumePending) {
1282 mResumePending = false;
1283
1284 sp<AMessage> notify = mNotify->dup();
1285 notify->setInt32("what", kWhatResumeCompleted);
1286 notify->post();
1287 }
1288}
1289
1290} // namespace android
1291