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