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