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