NuPlayerDecoder.cpp revision 66704af4d82c2b6303609b29402641f861fdcb19
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 "NuPlayerCCDecoder.h"
23#include "NuPlayerDecoder.h"
24#include "NuPlayerRenderer.h"
25#include "NuPlayerSource.h"
26
27#include <media/ICrypto.h>
28#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/MediaBuffer.h>
32#include <media/stagefright/MediaCodec.h>
33#include <media/stagefright/MediaDefs.h>
34#include <media/stagefright/MediaErrors.h>
35
36#include "avc_utils.h"
37#include "ATSParser.h"
38
39namespace android {
40
41NuPlayer::Decoder::Decoder(
42        const sp<AMessage> &notify,
43        const sp<Source> &source,
44        const sp<Renderer> &renderer,
45        const sp<NativeWindowWrapper> &nativeWindow,
46        const sp<CCDecoder> &ccDecoder)
47    : DecoderBase(notify),
48      mNativeWindow(nativeWindow),
49      mSource(source),
50      mRenderer(renderer),
51      mCCDecoder(ccDecoder),
52      mSkipRenderingUntilMediaTimeUs(-1ll),
53      mNumFramesTotal(0ll),
54      mNumFramesDropped(0ll),
55      mIsAudio(true),
56      mIsVideoAVC(false),
57      mIsSecure(false),
58      mFormatChangePending(false),
59      mTimeChangePending(false),
60      mPaused(true),
61      mResumePending(false),
62      mComponentName("decoder") {
63    mCodecLooper = new ALooper;
64    mCodecLooper->setName("NPDecoder-CL");
65    mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
66}
67
68NuPlayer::Decoder::~Decoder() {
69    releaseAndResetMediaBuffers();
70}
71
72void NuPlayer::Decoder::getStats(
73        int64_t *numFramesTotal,
74        int64_t *numFramesDropped) const {
75    *numFramesTotal = mNumFramesTotal;
76    *numFramesDropped = mNumFramesDropped;
77}
78
79void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
80    ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
81
82    switch (msg->what()) {
83        case kWhatCodecNotify:
84        {
85            if (!isStaleReply(msg)) {
86                int32_t numInput, numOutput;
87
88                if (!msg->findInt32("input-buffers", &numInput)) {
89                    numInput = INT32_MAX;
90                }
91
92                if (!msg->findInt32("output-buffers", &numOutput)) {
93                    numOutput = INT32_MAX;
94                }
95
96                if (!mPaused) {
97                    while (numInput-- > 0 && handleAnInputBuffer()) {}
98                }
99
100                while (numOutput-- > 0 && handleAnOutputBuffer()) {}
101            }
102
103            requestCodecNotification();
104            break;
105        }
106
107        case kWhatRenderBuffer:
108        {
109            if (!isStaleReply(msg)) {
110                onRenderBuffer(msg);
111            }
112            break;
113        }
114
115        default:
116            DecoderBase::onMessageReceived(msg);
117            break;
118    }
119}
120
121void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
122    CHECK(mCodec == NULL);
123
124    mFormatChangePending = false;
125    mTimeChangePending = false;
126
127    ++mBufferGeneration;
128
129    AString mime;
130    CHECK(format->findString("mime", &mime));
131
132    mIsAudio = !strncasecmp("audio/", mime.c_str(), 6);
133    mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
134
135    sp<Surface> surface = NULL;
136    if (mNativeWindow != NULL) {
137        surface = mNativeWindow->getSurfaceTextureClient();
138    }
139
140    mComponentName = mime;
141    mComponentName.append(" decoder");
142    ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
143
144    mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
145    int32_t secure = 0;
146    if (format->findInt32("secure", &secure) && secure != 0) {
147        if (mCodec != NULL) {
148            mCodec->getName(&mComponentName);
149            mComponentName.append(".secure");
150            mCodec->release();
151            ALOGI("[%s] creating", mComponentName.c_str());
152            mCodec = MediaCodec::CreateByComponentName(
153                    mCodecLooper, mComponentName.c_str());
154        }
155    }
156    if (mCodec == NULL) {
157        ALOGE("Failed to create %s%s decoder",
158                (secure ? "secure " : ""), mime.c_str());
159        handleError(UNKNOWN_ERROR);
160        return;
161    }
162    mIsSecure = secure;
163
164    mCodec->getName(&mComponentName);
165
166    status_t err;
167    if (mNativeWindow != NULL) {
168        // disconnect from surface as MediaCodec will reconnect
169        err = native_window_api_disconnect(
170                surface.get(), NATIVE_WINDOW_API_MEDIA);
171        // We treat this as a warning, as this is a preparatory step.
172        // Codec will try to connect to the surface, which is where
173        // any error signaling will occur.
174        ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err);
175    }
176    err = mCodec->configure(
177            format, surface, NULL /* crypto */, 0 /* flags */);
178    if (err != OK) {
179        ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
180        mCodec->release();
181        mCodec.clear();
182        handleError(err);
183        return;
184    }
185    rememberCodecSpecificData(format);
186
187    // the following should work in configured state
188    CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
189    CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
190
191    err = mCodec->start();
192    if (err != OK) {
193        ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
194        mCodec->release();
195        mCodec.clear();
196        handleError(err);
197        return;
198    }
199
200    // the following should work after start
201    CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
202    releaseAndResetMediaBuffers();
203    CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
204    ALOGV("[%s] got %zu input and %zu output buffers",
205            mComponentName.c_str(),
206            mInputBuffers.size(),
207            mOutputBuffers.size());
208
209    if (mRenderer != NULL) {
210        requestCodecNotification();
211    }
212    mPaused = false;
213    mResumePending = false;
214}
215
216void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) {
217    bool hadNoRenderer = (mRenderer == NULL);
218    mRenderer = renderer;
219    if (hadNoRenderer && mRenderer != NULL) {
220        requestCodecNotification();
221    }
222}
223
224void NuPlayer::Decoder::onGetInputBuffers(
225        Vector<sp<ABuffer> > *dstBuffers) {
226    dstBuffers->clear();
227    for (size_t i = 0; i < mInputBuffers.size(); i++) {
228        dstBuffers->push(mInputBuffers[i]);
229    }
230}
231
232void NuPlayer::Decoder::onResume(bool notifyComplete) {
233    mPaused = false;
234
235    if (notifyComplete) {
236        mResumePending = true;
237    }
238}
239
240void NuPlayer::Decoder::doFlush(bool notifyComplete) {
241    if (mCCDecoder != NULL) {
242        mCCDecoder->flush();
243    }
244
245    if (mRenderer != NULL) {
246        mRenderer->flush(mIsAudio, notifyComplete);
247        mRenderer->signalTimeDiscontinuity();
248    }
249
250    status_t err = OK;
251    if (mCodec != NULL) {
252        err = mCodec->flush();
253        mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
254        ++mBufferGeneration;
255    }
256
257    if (err != OK) {
258        ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
259        handleError(err);
260        // finish with posting kWhatFlushCompleted.
261        // we attempt to release the buffers even if flush fails.
262    }
263    releaseAndResetMediaBuffers();
264}
265
266void NuPlayer::Decoder::onFlush() {
267    doFlush(true);
268
269    if (isDiscontinuityPending()) {
270        // This could happen if the client starts seeking/shutdown
271        // after we queued an EOS for discontinuities.
272        // We can consider discontinuity handled.
273        finishHandleDiscontinuity(false /* flushOnTimeChange */);
274    }
275
276    sp<AMessage> notify = mNotify->dup();
277    notify->setInt32("what", kWhatFlushCompleted);
278    notify->post();
279    mPaused = true;
280}
281
282void NuPlayer::Decoder::onShutdown(bool notifyComplete) {
283    status_t err = OK;
284
285    // if there is a pending resume request, notify complete now
286    notifyResumeCompleteIfNecessary();
287
288    if (mCodec != NULL) {
289        err = mCodec->release();
290        mCodec = NULL;
291        ++mBufferGeneration;
292
293        if (mNativeWindow != NULL) {
294            // reconnect to surface as MediaCodec disconnected from it
295            status_t error =
296                    native_window_api_connect(
297                            mNativeWindow->getNativeWindow().get(),
298                            NATIVE_WINDOW_API_MEDIA);
299            ALOGW_IF(error != NO_ERROR,
300                    "[%s] failed to connect to native window, error=%d",
301                    mComponentName.c_str(), error);
302        }
303        mComponentName = "decoder";
304    }
305
306    releaseAndResetMediaBuffers();
307
308    if (err != OK) {
309        ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
310        handleError(err);
311        // finish with posting kWhatShutdownCompleted.
312    }
313
314    if (notifyComplete) {
315        sp<AMessage> notify = mNotify->dup();
316        notify->setInt32("what", kWhatShutdownCompleted);
317        notify->post();
318        mPaused = true;
319    }
320}
321
322void NuPlayer::Decoder::doRequestBuffers() {
323    if (isDiscontinuityPending()) {
324        return;
325    }
326    status_t err = OK;
327    while (err == OK && !mDequeuedInputBuffers.empty()) {
328        size_t bufferIx = *mDequeuedInputBuffers.begin();
329        sp<AMessage> msg = new AMessage();
330        msg->setSize("buffer-ix", bufferIx);
331        err = fetchInputData(msg);
332        if (err != OK && err != ERROR_END_OF_STREAM) {
333            // if EOS, need to queue EOS buffer
334            break;
335        }
336        mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin());
337
338        if (!mPendingInputMessages.empty()
339                || !onInputBufferFetched(msg)) {
340            mPendingInputMessages.push_back(msg);
341        }
342    }
343
344    if (err == -EWOULDBLOCK
345            && mSource->feedMoreTSData() == OK) {
346        scheduleRequestBuffers();
347    }
348}
349
350bool NuPlayer::Decoder::handleAnInputBuffer() {
351    if (isDiscontinuityPending()) {
352        return false;
353    }
354    size_t bufferIx = -1;
355    status_t res = mCodec->dequeueInputBuffer(&bufferIx);
356    ALOGV("[%s] dequeued input: %d",
357            mComponentName.c_str(), res == OK ? (int)bufferIx : res);
358    if (res != OK) {
359        if (res != -EAGAIN) {
360            ALOGE("Failed to dequeue input buffer for %s (err=%d)",
361                    mComponentName.c_str(), res);
362            handleError(res);
363        }
364        return false;
365    }
366
367    CHECK_LT(bufferIx, mInputBuffers.size());
368
369    if (mMediaBuffers[bufferIx] != NULL) {
370        mMediaBuffers[bufferIx]->release();
371        mMediaBuffers.editItemAt(bufferIx) = NULL;
372    }
373    mInputBufferIsDequeued.editItemAt(bufferIx) = true;
374
375    if (!mCSDsToSubmit.isEmpty()) {
376        sp<AMessage> msg = new AMessage();
377        msg->setSize("buffer-ix", bufferIx);
378
379        sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
380        ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
381        msg->setBuffer("buffer", buffer);
382        mCSDsToSubmit.removeAt(0);
383        CHECK(onInputBufferFetched(msg));
384        return true;
385    }
386
387    while (!mPendingInputMessages.empty()) {
388        sp<AMessage> msg = *mPendingInputMessages.begin();
389        if (!onInputBufferFetched(msg)) {
390            break;
391        }
392        mPendingInputMessages.erase(mPendingInputMessages.begin());
393    }
394
395    if (!mInputBufferIsDequeued.editItemAt(bufferIx)) {
396        return true;
397    }
398
399    mDequeuedInputBuffers.push_back(bufferIx);
400
401    onRequestInputBuffers();
402    return true;
403}
404
405bool NuPlayer::Decoder::handleAnOutputBuffer() {
406    size_t bufferIx = -1;
407    size_t offset;
408    size_t size;
409    int64_t timeUs;
410    uint32_t flags;
411    status_t res = mCodec->dequeueOutputBuffer(
412            &bufferIx, &offset, &size, &timeUs, &flags);
413
414    if (res != OK) {
415        ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
416    } else {
417        ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
418                mComponentName.c_str(), (int)bufferIx, timeUs, flags);
419    }
420
421    if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
422        res = mCodec->getOutputBuffers(&mOutputBuffers);
423        if (res != OK) {
424            ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
425                    mComponentName.c_str(), res);
426            handleError(res);
427            return false;
428        }
429        // NuPlayer ignores this
430        return true;
431    } else if (res == INFO_FORMAT_CHANGED) {
432        sp<AMessage> format = new AMessage();
433        res = mCodec->getOutputFormat(&format);
434        if (res != OK) {
435            ALOGE("Failed to get output format for %s after INFO event (err=%d)",
436                    mComponentName.c_str(), res);
437            handleError(res);
438            return false;
439        }
440
441        if (!mIsAudio) {
442            sp<AMessage> notify = mNotify->dup();
443            notify->setInt32("what", kWhatVideoSizeChanged);
444            notify->setMessage("format", format);
445            notify->post();
446        } else if (mRenderer != NULL) {
447            uint32_t flags;
448            int64_t durationUs;
449            bool hasVideo = (mSource->getFormat(false /* audio */) != NULL);
450            if (!hasVideo &&
451                    mSource->getDuration(&durationUs) == OK &&
452                    durationUs
453                        > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
454                flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
455            } else {
456                flags = AUDIO_OUTPUT_FLAG_NONE;
457            }
458
459            res = mRenderer->openAudioSink(
460                    format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaded */);
461            if (res != OK) {
462                ALOGE("Failed to open AudioSink on format change for %s (err=%d)",
463                        mComponentName.c_str(), res);
464                handleError(res);
465                return false;
466            }
467        }
468        return true;
469    } else if (res == INFO_DISCONTINUITY) {
470        // nothing to do
471        return true;
472    } else if (res != OK) {
473        if (res != -EAGAIN) {
474            ALOGE("Failed to dequeue output buffer for %s (err=%d)",
475                    mComponentName.c_str(), res);
476            handleError(res);
477        }
478        return false;
479    }
480
481    CHECK_LT(bufferIx, mOutputBuffers.size());
482    sp<ABuffer> buffer = mOutputBuffers[bufferIx];
483    buffer->setRange(offset, size);
484    buffer->meta()->clear();
485    buffer->meta()->setInt64("timeUs", timeUs);
486
487    bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
488    // we do not expect CODECCONFIG or SYNCFRAME for decoder
489
490    sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this);
491    reply->setSize("buffer-ix", bufferIx);
492    reply->setInt32("generation", mBufferGeneration);
493
494    if (eos) {
495        ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video");
496
497        buffer->meta()->setInt32("eos", true);
498        reply->setInt32("eos", true);
499    } else if (mSkipRenderingUntilMediaTimeUs >= 0) {
500        if (timeUs < mSkipRenderingUntilMediaTimeUs) {
501            ALOGV("[%s] dropping buffer at time %lld as requested.",
502                     mComponentName.c_str(), (long long)timeUs);
503
504            reply->post();
505            return true;
506        }
507
508        mSkipRenderingUntilMediaTimeUs = -1;
509    }
510
511    // wait until 1st frame comes out to signal resume complete
512    notifyResumeCompleteIfNecessary();
513
514    if (mRenderer != NULL) {
515        // send the buffer to renderer.
516        mRenderer->queueBuffer(mIsAudio, buffer, reply);
517        if (eos && !isDiscontinuityPending()) {
518            mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM);
519        }
520    }
521
522    return true;
523}
524
525void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
526    for (size_t i = 0; i < mMediaBuffers.size(); i++) {
527        if (mMediaBuffers[i] != NULL) {
528            mMediaBuffers[i]->release();
529            mMediaBuffers.editItemAt(i) = NULL;
530        }
531    }
532    mMediaBuffers.resize(mInputBuffers.size());
533    for (size_t i = 0; i < mMediaBuffers.size(); i++) {
534        mMediaBuffers.editItemAt(i) = NULL;
535    }
536    mInputBufferIsDequeued.clear();
537    mInputBufferIsDequeued.resize(mInputBuffers.size());
538    for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
539        mInputBufferIsDequeued.editItemAt(i) = false;
540    }
541
542    mPendingInputMessages.clear();
543    mDequeuedInputBuffers.clear();
544    mSkipRenderingUntilMediaTimeUs = -1;
545}
546
547void NuPlayer::Decoder::requestCodecNotification() {
548    if (mCodec != NULL) {
549        sp<AMessage> reply = new AMessage(kWhatCodecNotify, this);
550        reply->setInt32("generation", mBufferGeneration);
551        mCodec->requestActivityNotification(reply);
552    }
553}
554
555bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
556    int32_t generation;
557    CHECK(msg->findInt32("generation", &generation));
558    return generation != mBufferGeneration;
559}
560
561status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) {
562    sp<ABuffer> accessUnit;
563    bool dropAccessUnit;
564    do {
565        status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit);
566
567        if (err == -EWOULDBLOCK) {
568            return err;
569        } else if (err != OK) {
570            if (err == INFO_DISCONTINUITY) {
571                int32_t type;
572                CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
573
574                bool formatChange =
575                    (mIsAudio &&
576                     (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
577                    || (!mIsAudio &&
578                            (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
579
580                bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
581
582                ALOGI("%s discontinuity (format=%d, time=%d)",
583                        mIsAudio ? "audio" : "video", formatChange, timeChange);
584
585                bool seamlessFormatChange = false;
586                sp<AMessage> newFormat = mSource->getFormat(mIsAudio);
587                if (formatChange) {
588                    seamlessFormatChange =
589                        supportsSeamlessFormatChange(newFormat);
590                    // treat seamless format change separately
591                    formatChange = !seamlessFormatChange;
592                }
593
594                // For format or time change, return EOS to queue EOS input,
595                // then wait for EOS on output.
596                if (formatChange /* not seamless */) {
597                    mFormatChangePending = true;
598                    err = ERROR_END_OF_STREAM;
599                } else if (timeChange) {
600                    rememberCodecSpecificData(newFormat);
601                    mTimeChangePending = true;
602                    err = ERROR_END_OF_STREAM;
603                } else if (seamlessFormatChange) {
604                    // reuse existing decoder and don't flush
605                    rememberCodecSpecificData(newFormat);
606                    continue;
607                } else {
608                    // This stream is unaffected by the discontinuity
609                    return -EWOULDBLOCK;
610                }
611            }
612
613            // reply should only be returned without a buffer set
614            // when there is an error (including EOS)
615            CHECK(err != OK);
616
617            reply->setInt32("err", err);
618            return ERROR_END_OF_STREAM;
619        }
620
621        if (!mIsAudio) {
622            ++mNumFramesTotal;
623        }
624
625        dropAccessUnit = false;
626        if (!mIsAudio
627                && !mIsSecure
628                && mRenderer->getVideoLateByUs() > 100000ll
629                && mIsVideoAVC
630                && !IsAVCReferenceFrame(accessUnit)) {
631            dropAccessUnit = true;
632            ++mNumFramesDropped;
633        }
634    } while (dropAccessUnit);
635
636    // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video");
637#if 0
638    int64_t mediaTimeUs;
639    CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
640    ALOGV("[%s] feeding input buffer at media time %" PRId64,
641         mIsAudio ? "audio" : "video",
642         mediaTimeUs / 1E6);
643#endif
644
645    if (mCCDecoder != NULL) {
646        mCCDecoder->decode(accessUnit);
647    }
648
649    reply->setBuffer("buffer", accessUnit);
650
651    return OK;
652}
653
654bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) {
655    size_t bufferIx;
656    CHECK(msg->findSize("buffer-ix", &bufferIx));
657    CHECK_LT(bufferIx, mInputBuffers.size());
658    sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
659
660    sp<ABuffer> buffer;
661    bool hasBuffer = msg->findBuffer("buffer", &buffer);
662
663    // handle widevine classic source - that fills an arbitrary input buffer
664    MediaBuffer *mediaBuffer = NULL;
665    if (hasBuffer) {
666        mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase());
667        if (mediaBuffer != NULL) {
668            // likely filled another buffer than we requested: adjust buffer index
669            size_t ix;
670            for (ix = 0; ix < mInputBuffers.size(); ix++) {
671                const sp<ABuffer> &buf = mInputBuffers[ix];
672                if (buf->data() == mediaBuffer->data()) {
673                    // all input buffers are dequeued on start, hence the check
674                    if (!mInputBufferIsDequeued[ix]) {
675                        ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
676                                mComponentName.c_str(), ix, bufferIx);
677                        mediaBuffer->release();
678                        return false;
679                    }
680
681                    // TRICKY: need buffer for the metadata, so instead, set
682                    // codecBuffer to the same (though incorrect) buffer to
683                    // avoid a memcpy into the codecBuffer
684                    codecBuffer = buffer;
685                    codecBuffer->setRange(
686                            mediaBuffer->range_offset(),
687                            mediaBuffer->range_length());
688                    bufferIx = ix;
689                    break;
690                }
691            }
692            CHECK(ix < mInputBuffers.size());
693        }
694    }
695
696    if (buffer == NULL /* includes !hasBuffer */) {
697        int32_t streamErr = ERROR_END_OF_STREAM;
698        CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
699
700        CHECK(streamErr != OK);
701
702        // attempt to queue EOS
703        status_t err = mCodec->queueInputBuffer(
704                bufferIx,
705                0,
706                0,
707                0,
708                MediaCodec::BUFFER_FLAG_EOS);
709        if (err == OK) {
710            mInputBufferIsDequeued.editItemAt(bufferIx) = false;
711        } else if (streamErr == ERROR_END_OF_STREAM) {
712            streamErr = err;
713            // err will not be ERROR_END_OF_STREAM
714        }
715
716        if (streamErr != ERROR_END_OF_STREAM) {
717            ALOGE("Stream error for %s (err=%d), EOS %s queued",
718                    mComponentName.c_str(),
719                    streamErr,
720                    err == OK ? "successfully" : "unsuccessfully");
721            handleError(streamErr);
722        }
723    } else {
724        sp<AMessage> extra;
725        if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) {
726            int64_t resumeAtMediaTimeUs;
727            if (extra->findInt64(
728                        "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) {
729                ALOGI("[%s] suppressing rendering until %lld us",
730                        mComponentName.c_str(), (long long)resumeAtMediaTimeUs);
731                mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs;
732            }
733        }
734
735        int64_t timeUs = 0;
736        uint32_t flags = 0;
737        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
738
739        int32_t eos, csd;
740        // we do not expect SYNCFRAME for decoder
741        if (buffer->meta()->findInt32("eos", &eos) && eos) {
742            flags |= MediaCodec::BUFFER_FLAG_EOS;
743        } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
744            flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
745        }
746
747        // copy into codec buffer
748        if (buffer != codecBuffer) {
749            CHECK_LE(buffer->size(), codecBuffer->capacity());
750            codecBuffer->setRange(0, buffer->size());
751            memcpy(codecBuffer->data(), buffer->data(), buffer->size());
752        }
753
754        status_t err = mCodec->queueInputBuffer(
755                        bufferIx,
756                        codecBuffer->offset(),
757                        codecBuffer->size(),
758                        timeUs,
759                        flags);
760        if (err != OK) {
761            if (mediaBuffer != NULL) {
762                mediaBuffer->release();
763            }
764            ALOGE("Failed to queue input buffer for %s (err=%d)",
765                    mComponentName.c_str(), err);
766            handleError(err);
767        } else {
768            mInputBufferIsDequeued.editItemAt(bufferIx) = false;
769            if (mediaBuffer != NULL) {
770                CHECK(mMediaBuffers[bufferIx] == NULL);
771                mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
772            }
773        }
774    }
775    return true;
776}
777
778void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
779    status_t err;
780    int32_t render;
781    size_t bufferIx;
782    int32_t eos;
783    CHECK(msg->findSize("buffer-ix", &bufferIx));
784
785    if (!mIsAudio) {
786        int64_t timeUs;
787        sp<ABuffer> buffer = mOutputBuffers[bufferIx];
788        buffer->meta()->findInt64("timeUs", &timeUs);
789
790        if (mCCDecoder != NULL && mCCDecoder->isSelected()) {
791            mCCDecoder->display(timeUs);
792        }
793    }
794
795    if (msg->findInt32("render", &render) && render) {
796        int64_t timestampNs;
797        CHECK(msg->findInt64("timestampNs", &timestampNs));
798        err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs);
799    } else {
800        err = mCodec->releaseOutputBuffer(bufferIx);
801    }
802    if (err != OK) {
803        ALOGE("failed to release output buffer for %s (err=%d)",
804                mComponentName.c_str(), err);
805        handleError(err);
806    }
807    if (msg->findInt32("eos", &eos) && eos
808            && isDiscontinuityPending()) {
809        finishHandleDiscontinuity(true /* flushOnTimeChange */);
810    }
811}
812
813bool NuPlayer::Decoder::isDiscontinuityPending() const {
814    return mFormatChangePending || mTimeChangePending;
815}
816
817void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) {
818    ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d",
819            mFormatChangePending, mTimeChangePending, flushOnTimeChange);
820
821    // If we have format change, pause and wait to be killed;
822    // If we have time change only, flush and restart fetching.
823
824    if (mFormatChangePending) {
825        mPaused = true;
826    } else if (mTimeChangePending) {
827        if (flushOnTimeChange) {
828            doFlush(false /*notifyComplete*/);
829        }
830
831        // restart fetching input
832        scheduleRequestBuffers();
833    }
834
835    // Notify NuPlayer to either shutdown decoder, or rescan sources
836    sp<AMessage> msg = mNotify->dup();
837    msg->setInt32("what", kWhatInputDiscontinuity);
838    msg->setInt32("formatChange", mFormatChangePending);
839    msg->post();
840
841    mFormatChangePending = false;
842    mTimeChangePending = false;
843}
844
845bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(
846        const sp<AMessage> &targetFormat) const {
847    if (targetFormat == NULL) {
848        return true;
849    }
850
851    AString mime;
852    if (!targetFormat->findString("mime", &mime)) {
853        return false;
854    }
855
856    if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
857        // field-by-field comparison
858        const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
859        for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
860            int32_t oldVal, newVal;
861            if (!mInputFormat->findInt32(keys[i], &oldVal) ||
862                    !targetFormat->findInt32(keys[i], &newVal) ||
863                    oldVal != newVal) {
864                return false;
865            }
866        }
867
868        sp<ABuffer> oldBuf, newBuf;
869        if (mInputFormat->findBuffer("csd-0", &oldBuf) &&
870                targetFormat->findBuffer("csd-0", &newBuf)) {
871            if (oldBuf->size() != newBuf->size()) {
872                return false;
873            }
874            return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
875        }
876    }
877    return false;
878}
879
880bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
881    if (mInputFormat == NULL) {
882        return false;
883    }
884
885    if (targetFormat == NULL) {
886        return true;
887    }
888
889    AString oldMime, newMime;
890    if (!mInputFormat->findString("mime", &oldMime)
891            || !targetFormat->findString("mime", &newMime)
892            || !(oldMime == newMime)) {
893        return false;
894    }
895
896    bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
897    bool seamless;
898    if (audio) {
899        seamless = supportsSeamlessAudioFormatChange(targetFormat);
900    } else {
901        int32_t isAdaptive;
902        seamless = (mCodec != NULL &&
903                mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
904                isAdaptive);
905    }
906
907    ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
908    return seamless;
909}
910
911void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
912    if (format == NULL) {
913        return;
914    }
915    mCSDsForCurrentFormat.clear();
916    for (int32_t i = 0; ; ++i) {
917        AString tag = "csd-";
918        tag.append(i);
919        sp<ABuffer> buffer;
920        if (!format->findBuffer(tag.c_str(), &buffer)) {
921            break;
922        }
923        mCSDsForCurrentFormat.push(buffer);
924    }
925}
926
927void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() {
928    if (mResumePending) {
929        mResumePending = false;
930
931        sp<AMessage> notify = mNotify->dup();
932        notify->setInt32("what", kWhatResumeCompleted);
933        notify->post();
934    }
935}
936
937}  // namespace android
938
939