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