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