NuPlayerDecoder.cpp revision c640e19c11b3f93e739dd1504159dbe9fe529f0c
1/*
2 * Copyright (C) 2010 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 "NuPlayerDecoder.h"
23
24#include <media/ICrypto.h>
25#include <media/stagefright/foundation/ABitReader.h>
26#include <media/stagefright/foundation/ABuffer.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/foundation/AMessage.h>
29#include <media/stagefright/MediaBuffer.h>
30#include <media/stagefright/MediaCodec.h>
31#include <media/stagefright/MediaDefs.h>
32#include <media/stagefright/MediaErrors.h>
33
34namespace android {
35
36NuPlayer::Decoder::Decoder(
37        const sp<AMessage> &notify,
38        const sp<NativeWindowWrapper> &nativeWindow)
39    : mNotify(notify),
40      mNativeWindow(nativeWindow),
41      mBufferGeneration(0),
42      mPaused(true),
43      mComponentName("decoder") {
44    // Every decoder has its own looper because MediaCodec operations
45    // are blocking, but NuPlayer needs asynchronous operations.
46    mDecoderLooper = new ALooper;
47    mDecoderLooper->setName("NPDecoder");
48    mDecoderLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
49
50    mCodecLooper = new ALooper;
51    mCodecLooper->setName("NPDecoder-CL");
52    mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
53}
54
55NuPlayer::Decoder::~Decoder() {
56}
57
58static
59status_t PostAndAwaitResponse(
60        const sp<AMessage> &msg, sp<AMessage> *response) {
61    status_t err = msg->postAndAwaitResponse(response);
62
63    if (err != OK) {
64        return err;
65    }
66
67    if (!(*response)->findInt32("err", &err)) {
68        err = OK;
69    }
70
71    return err;
72}
73
74void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) {
75    mCSDsForCurrentFormat.clear();
76    for (int32_t i = 0; ; ++i) {
77        AString tag = "csd-";
78        tag.append(i);
79        sp<ABuffer> buffer;
80        if (!format->findBuffer(tag.c_str(), &buffer)) {
81            break;
82        }
83        mCSDsForCurrentFormat.push(buffer);
84    }
85}
86
87void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
88    CHECK(mCodec == NULL);
89
90    ++mBufferGeneration;
91
92    AString mime;
93    CHECK(format->findString("mime", &mime));
94
95    sp<Surface> surface = NULL;
96    if (mNativeWindow != NULL) {
97        surface = mNativeWindow->getSurfaceTextureClient();
98    }
99
100    mComponentName = mime;
101    mComponentName.append(" decoder");
102    ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get());
103
104    mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */);
105    int32_t secure = 0;
106    if (format->findInt32("secure", &secure) && secure != 0) {
107        if (mCodec != NULL) {
108            mCodec->getName(&mComponentName);
109            mComponentName.append(".secure");
110            mCodec->release();
111            ALOGI("[%s] creating", mComponentName.c_str());
112            mCodec = MediaCodec::CreateByComponentName(
113                    mCodecLooper, mComponentName.c_str());
114        }
115    }
116    if (mCodec == NULL) {
117        ALOGE("Failed to create %s%s decoder",
118                (secure ? "secure " : ""), mime.c_str());
119        handleError(UNKNOWN_ERROR);
120        return;
121    }
122
123    mCodec->getName(&mComponentName);
124
125    if (mNativeWindow != NULL) {
126        // disconnect from surface as MediaCodec will reconnect
127        CHECK_EQ((int)NO_ERROR,
128                native_window_api_disconnect(
129                        surface.get(),
130                        NATIVE_WINDOW_API_MEDIA));
131    }
132    status_t err = mCodec->configure(
133            format, surface, NULL /* crypto */, 0 /* flags */);
134    if (err != OK) {
135        ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err);
136        handleError(err);
137        return;
138    }
139    rememberCodecSpecificData(format);
140
141    // the following should work in configured state
142    CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat));
143    CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat));
144
145    err = mCodec->start();
146    if (err != OK) {
147        ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err);
148        handleError(err);
149        return;
150    }
151
152    // the following should work after start
153    CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers));
154    releaseAndResetMediaBuffers();
155    CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers));
156    ALOGV("[%s] got %zu input and %zu output buffers",
157            mComponentName.c_str(),
158            mInputBuffers.size(),
159            mOutputBuffers.size());
160
161    requestCodecNotification();
162    mPaused = false;
163}
164
165void NuPlayer::Decoder::releaseAndResetMediaBuffers() {
166    for (size_t i = 0; i < mMediaBuffers.size(); i++) {
167        if (mMediaBuffers[i] != NULL) {
168            mMediaBuffers[i]->release();
169            mMediaBuffers.editItemAt(i) = NULL;
170        }
171    }
172    mMediaBuffers.resize(mInputBuffers.size());
173    for (size_t i = 0; i < mMediaBuffers.size(); i++) {
174        mMediaBuffers.editItemAt(i) = NULL;
175    }
176    mInputBufferIsDequeued.clear();
177    mInputBufferIsDequeued.resize(mInputBuffers.size());
178    for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) {
179        mInputBufferIsDequeued.editItemAt(i) = false;
180    }
181}
182
183void NuPlayer::Decoder::requestCodecNotification() {
184    if (mCodec != NULL) {
185        sp<AMessage> reply = new AMessage(kWhatCodecNotify, id());
186        reply->setInt32("generation", mBufferGeneration);
187        mCodec->requestActivityNotification(reply);
188    }
189}
190
191bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) {
192    int32_t generation;
193    CHECK(msg->findInt32("generation", &generation));
194    return generation != mBufferGeneration;
195}
196
197void NuPlayer::Decoder::init() {
198    mDecoderLooper->registerHandler(this);
199}
200
201void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
202    sp<AMessage> msg = new AMessage(kWhatConfigure, id());
203    msg->setMessage("format", format);
204    msg->post();
205}
206
207void NuPlayer::Decoder::signalUpdateFormat(const sp<AMessage> &format) {
208    sp<AMessage> msg = new AMessage(kWhatUpdateFormat, id());
209    msg->setMessage("format", format);
210    msg->post();
211}
212
213status_t NuPlayer::Decoder::getInputBuffers(Vector<sp<ABuffer> > *buffers) const {
214    sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, id());
215    msg->setPointer("buffers", buffers);
216
217    sp<AMessage> response;
218    return PostAndAwaitResponse(msg, &response);
219}
220
221void NuPlayer::Decoder::handleError(int32_t err)
222{
223    mCodec->release();
224
225    sp<AMessage> notify = mNotify->dup();
226    notify->setInt32("what", kWhatError);
227    notify->setInt32("err", err);
228    notify->post();
229}
230
231bool NuPlayer::Decoder::handleAnInputBuffer() {
232    size_t bufferIx = -1;
233    status_t res = mCodec->dequeueInputBuffer(&bufferIx);
234    ALOGV("[%s] dequeued input: %d",
235            mComponentName.c_str(), res == OK ? (int)bufferIx : res);
236    if (res != OK) {
237        if (res != -EAGAIN) {
238            handleError(res);
239        }
240        return false;
241    }
242
243    CHECK_LT(bufferIx, mInputBuffers.size());
244
245    if (mMediaBuffers[bufferIx] != NULL) {
246        mMediaBuffers[bufferIx]->release();
247        mMediaBuffers.editItemAt(bufferIx) = NULL;
248    }
249    mInputBufferIsDequeued.editItemAt(bufferIx) = true;
250
251    sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, id());
252    reply->setSize("buffer-ix", bufferIx);
253    reply->setInt32("generation", mBufferGeneration);
254
255    if (!mCSDsToSubmit.isEmpty()) {
256        sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0);
257        ALOGI("[%s] resubmitting CSD", mComponentName.c_str());
258        reply->setBuffer("buffer", buffer);
259        mCSDsToSubmit.removeAt(0);
260        reply->post();
261        return true;
262    }
263
264    sp<AMessage> notify = mNotify->dup();
265    notify->setInt32("what", kWhatFillThisBuffer);
266    notify->setBuffer("buffer", mInputBuffers[bufferIx]);
267    notify->setMessage("reply", reply);
268    notify->post();
269    return true;
270}
271
272void android::NuPlayer::Decoder::onInputBufferFilled(const sp<AMessage> &msg) {
273    size_t bufferIx;
274    CHECK(msg->findSize("buffer-ix", &bufferIx));
275    CHECK_LT(bufferIx, mInputBuffers.size());
276    sp<ABuffer> codecBuffer = mInputBuffers[bufferIx];
277
278    sp<ABuffer> buffer;
279    bool hasBuffer = msg->findBuffer("buffer", &buffer);
280
281    // handle widevine classic source - that fills an arbitrary input buffer
282    MediaBuffer *mediaBuffer = NULL;
283    if (hasBuffer && buffer->meta()->findPointer(
284            "mediaBuffer", (void **)&mediaBuffer)) {
285        if (mediaBuffer == NULL) {
286            // received no actual buffer
287            ALOGW("[%s] received null MediaBuffer %s",
288                    mComponentName.c_str(), msg->debugString().c_str());
289            buffer = NULL;
290        } else {
291            // likely filled another buffer than we requested: adjust buffer index
292            size_t ix;
293            for (ix = 0; ix < mInputBuffers.size(); ix++) {
294                const sp<ABuffer> &buf = mInputBuffers[ix];
295                if (buf->data() == mediaBuffer->data()) {
296                    // all input buffers are dequeued on start, hence the check
297                    CHECK(mInputBufferIsDequeued[ix]);
298                    ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu",
299                            mComponentName.c_str(), ix, bufferIx);
300
301                    // TRICKY: need buffer for the metadata, so instead, set
302                    // codecBuffer to the same (though incorrect) buffer to
303                    // avoid a memcpy into the codecBuffer
304                    codecBuffer = buffer;
305                    codecBuffer->setRange(
306                            mediaBuffer->range_offset(),
307                            mediaBuffer->range_length());
308                    bufferIx = ix;
309                    break;
310                }
311            }
312            CHECK(ix < mInputBuffers.size());
313        }
314    }
315
316    mInputBufferIsDequeued.editItemAt(bufferIx) = false;
317
318    if (buffer == NULL /* includes !hasBuffer */) {
319        int32_t streamErr = ERROR_END_OF_STREAM;
320        CHECK(msg->findInt32("err", &streamErr) || !hasBuffer);
321
322        if (streamErr == OK) {
323            /* buffers are returned to hold on to */
324            return;
325        }
326
327        // attempt to queue EOS
328        status_t err = mCodec->queueInputBuffer(
329                bufferIx,
330                0,
331                0,
332                0,
333                MediaCodec::BUFFER_FLAG_EOS);
334        if (streamErr == ERROR_END_OF_STREAM && err != OK) {
335            streamErr = err;
336            // err will not be ERROR_END_OF_STREAM
337        }
338
339        if (streamErr != ERROR_END_OF_STREAM) {
340            handleError(streamErr);
341        }
342    } else {
343        int64_t timeUs = 0;
344        uint32_t flags = 0;
345        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
346
347        int32_t eos, csd;
348        // we do not expect SYNCFRAME for decoder
349        if (buffer->meta()->findInt32("eos", &eos) && eos) {
350            flags |= MediaCodec::BUFFER_FLAG_EOS;
351        } else if (buffer->meta()->findInt32("csd", &csd) && csd) {
352            flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG;
353        }
354
355        // copy into codec buffer
356        if (buffer != codecBuffer) {
357            CHECK_LE(buffer->size(), codecBuffer->capacity());
358            codecBuffer->setRange(0, buffer->size());
359            memcpy(codecBuffer->data(), buffer->data(), buffer->size());
360        }
361
362        status_t err = mCodec->queueInputBuffer(
363                        bufferIx,
364                        codecBuffer->offset(),
365                        codecBuffer->size(),
366                        timeUs,
367                        flags);
368        if (err != OK) {
369            ALOGE("Failed to queue input buffer for %s (err=%d)",
370                    mComponentName.c_str(), err);
371            handleError(err);
372        }
373
374        if (mediaBuffer != NULL) {
375            CHECK(mMediaBuffers[bufferIx] == NULL);
376            mMediaBuffers.editItemAt(bufferIx) = mediaBuffer;
377        }
378    }
379}
380
381bool NuPlayer::Decoder::handleAnOutputBuffer() {
382    size_t bufferIx = -1;
383    size_t offset;
384    size_t size;
385    int64_t timeUs;
386    uint32_t flags;
387    status_t res = mCodec->dequeueOutputBuffer(
388            &bufferIx, &offset, &size, &timeUs, &flags);
389
390    if (res != OK) {
391        ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res);
392    } else {
393        ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")",
394                mComponentName.c_str(), (int)bufferIx, timeUs, flags);
395    }
396
397    if (res == INFO_OUTPUT_BUFFERS_CHANGED) {
398        res = mCodec->getOutputBuffers(&mOutputBuffers);
399        if (res != OK) {
400            ALOGE("Failed to get output buffers for %s after INFO event (err=%d)",
401                    mComponentName.c_str(), res);
402            handleError(res);
403            return false;
404        }
405        // NuPlayer ignores this
406        return true;
407    } else if (res == INFO_FORMAT_CHANGED) {
408        sp<AMessage> format = new AMessage();
409        res = mCodec->getOutputFormat(&format);
410        if (res != OK) {
411            ALOGE("Failed to get output format for %s after INFO event (err=%d)",
412                    mComponentName.c_str(), res);
413            handleError(res);
414            return false;
415        }
416
417        sp<AMessage> notify = mNotify->dup();
418        notify->setInt32("what", kWhatOutputFormatChanged);
419        notify->setMessage("format", format);
420        notify->post();
421        return true;
422    } else if (res == INFO_DISCONTINUITY) {
423        // nothing to do
424        return true;
425    } else if (res != OK) {
426        if (res != -EAGAIN) {
427            handleError(res);
428        }
429        return false;
430    }
431
432    CHECK_LT(bufferIx, mOutputBuffers.size());
433    sp<ABuffer> buffer = mOutputBuffers[bufferIx];
434    buffer->setRange(offset, size);
435    buffer->meta()->clear();
436    buffer->meta()->setInt64("timeUs", timeUs);
437    if (flags & MediaCodec::BUFFER_FLAG_EOS) {
438        buffer->meta()->setInt32("eos", true);
439    }
440    // we do not expect CODECCONFIG or SYNCFRAME for decoder
441
442    sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id());
443    reply->setSize("buffer-ix", bufferIx);
444    reply->setInt32("generation", mBufferGeneration);
445
446    sp<AMessage> notify = mNotify->dup();
447    notify->setInt32("what", kWhatDrainThisBuffer);
448    notify->setBuffer("buffer", buffer);
449    notify->setMessage("reply", reply);
450    notify->post();
451
452    // FIXME: This should be handled after rendering is complete,
453    // but Renderer needs it now
454    if (flags & MediaCodec::BUFFER_FLAG_EOS) {
455        ALOGV("queueing eos [%s]", mComponentName.c_str());
456        sp<AMessage> notify = mNotify->dup();
457        notify->setInt32("what", kWhatEOS);
458        notify->setInt32("err", ERROR_END_OF_STREAM);
459        notify->post();
460    }
461    return true;
462}
463
464void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) {
465    status_t err;
466    int32_t render;
467    size_t bufferIx;
468    CHECK(msg->findSize("buffer-ix", &bufferIx));
469    if (msg->findInt32("render", &render) && render) {
470        err = mCodec->renderOutputBufferAndRelease(bufferIx);
471    } else {
472        err = mCodec->releaseOutputBuffer(bufferIx);
473    }
474    if (err != OK) {
475        ALOGE("failed to release output buffer for %s (err=%d)",
476                mComponentName.c_str(), err);
477        handleError(err);
478    }
479}
480
481void NuPlayer::Decoder::onFlush() {
482    status_t err = OK;
483    if (mCodec != NULL) {
484        err = mCodec->flush();
485        mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator
486        ++mBufferGeneration;
487    }
488
489    if (err != OK) {
490        ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err);
491        handleError(err);
492        return;
493    }
494
495    releaseAndResetMediaBuffers();
496
497    sp<AMessage> notify = mNotify->dup();
498    notify->setInt32("what", kWhatFlushCompleted);
499    notify->post();
500    mPaused = true;
501}
502
503void NuPlayer::Decoder::onResume() {
504    mPaused = false;
505}
506
507void NuPlayer::Decoder::onShutdown() {
508    status_t err = OK;
509    if (mCodec != NULL) {
510        err = mCodec->release();
511        mCodec = NULL;
512        ++mBufferGeneration;
513
514        if (mNativeWindow != NULL) {
515            // reconnect to surface as MediaCodec disconnected from it
516            status_t error =
517                    native_window_api_connect(
518                            mNativeWindow->getNativeWindow().get(),
519                            NATIVE_WINDOW_API_MEDIA);
520            ALOGW_IF(error != NO_ERROR,
521                    "[%s] failed to connect to native window, error=%d",
522                    mComponentName.c_str(), error);
523        }
524        mComponentName = "decoder";
525    }
526
527    releaseAndResetMediaBuffers();
528
529    if (err != OK) {
530        ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err);
531        handleError(err);
532        return;
533    }
534
535    sp<AMessage> notify = mNotify->dup();
536    notify->setInt32("what", kWhatShutdownCompleted);
537    notify->post();
538    mPaused = true;
539}
540
541void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
542    ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str());
543
544    switch (msg->what()) {
545        case kWhatConfigure:
546        {
547            sp<AMessage> format;
548            CHECK(msg->findMessage("format", &format));
549            onConfigure(format);
550            break;
551        }
552
553        case kWhatUpdateFormat:
554        {
555            sp<AMessage> format;
556            CHECK(msg->findMessage("format", &format));
557            rememberCodecSpecificData(format);
558            break;
559        }
560
561        case kWhatGetInputBuffers:
562        {
563            uint32_t replyID;
564            CHECK(msg->senderAwaitsResponse(&replyID));
565
566            Vector<sp<ABuffer> > *dstBuffers;
567            CHECK(msg->findPointer("buffers", (void **)&dstBuffers));
568
569            dstBuffers->clear();
570            for (size_t i = 0; i < mInputBuffers.size(); i++) {
571                dstBuffers->push(mInputBuffers[i]);
572            }
573
574            (new AMessage)->postReply(replyID);
575            break;
576        }
577
578        case kWhatCodecNotify:
579        {
580            if (!isStaleReply(msg)) {
581                if (!mPaused) {
582                    while (handleAnInputBuffer()) {
583                    }
584                }
585
586                while (handleAnOutputBuffer()) {
587                }
588            }
589
590            requestCodecNotification();
591            break;
592        }
593
594        case kWhatInputBufferFilled:
595        {
596            if (!isStaleReply(msg)) {
597                onInputBufferFilled(msg);
598            } else {
599                /* release any MediaBuffer passed in the stale buffer */
600                sp<ABuffer> buffer;
601                MediaBuffer *mediaBuffer = NULL;
602                if (msg->findBuffer("buffer", &buffer) &&
603                    buffer->meta()->findPointer(
604                            "mediaBuffer", (void **)&mediaBuffer) &&
605                    mediaBuffer != NULL) {
606                    mediaBuffer->release();
607                }
608            }
609
610            break;
611        }
612
613        case kWhatRenderBuffer:
614        {
615            if (!isStaleReply(msg)) {
616                onRenderBuffer(msg);
617            }
618            break;
619        }
620
621        case kWhatFlush:
622        {
623            sp<AMessage> format;
624            if (msg->findMessage("new-format", &format)) {
625                rememberCodecSpecificData(format);
626            }
627            onFlush();
628            break;
629        }
630
631        case kWhatResume:
632        {
633            onResume();
634            break;
635        }
636
637        case kWhatShutdown:
638        {
639            onShutdown();
640            break;
641        }
642
643        default:
644            TRESPASS();
645            break;
646    }
647}
648
649void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) {
650    sp<AMessage> msg = new AMessage(kWhatFlush, id());
651    if (format != NULL) {
652        msg->setMessage("new-format", format);
653    }
654    msg->post();
655}
656
657void NuPlayer::Decoder::signalResume() {
658    (new AMessage(kWhatResume, id()))->post();
659}
660
661void NuPlayer::Decoder::initiateShutdown() {
662    (new AMessage(kWhatShutdown, id()))->post();
663}
664
665bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const {
666    if (targetFormat == NULL) {
667        return true;
668    }
669
670    AString mime;
671    if (!targetFormat->findString("mime", &mime)) {
672        return false;
673    }
674
675    if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
676        // field-by-field comparison
677        const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
678        for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
679            int32_t oldVal, newVal;
680            if (!mOutputFormat->findInt32(keys[i], &oldVal) ||
681                    !targetFormat->findInt32(keys[i], &newVal) ||
682                    oldVal != newVal) {
683                return false;
684            }
685        }
686
687        sp<ABuffer> oldBuf, newBuf;
688        if (mOutputFormat->findBuffer("csd-0", &oldBuf) &&
689                targetFormat->findBuffer("csd-0", &newBuf)) {
690            if (oldBuf->size() != newBuf->size()) {
691                return false;
692            }
693            return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
694        }
695    }
696    return false;
697}
698
699bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
700    if (mOutputFormat == NULL) {
701        return false;
702    }
703
704    if (targetFormat == NULL) {
705        return true;
706    }
707
708    AString oldMime, newMime;
709    if (!mOutputFormat->findString("mime", &oldMime)
710            || !targetFormat->findString("mime", &newMime)
711            || !(oldMime == newMime)) {
712        return false;
713    }
714
715    bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
716    bool seamless;
717    if (audio) {
718        seamless = supportsSeamlessAudioFormatChange(targetFormat);
719    } else {
720        int32_t isAdaptive;
721        seamless = (mCodec != NULL &&
722                mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
723                isAdaptive);
724    }
725
726    ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
727    return seamless;
728}
729
730struct CCData {
731    CCData(uint8_t type, uint8_t data1, uint8_t data2)
732        : mType(type), mData1(data1), mData2(data2) {
733    }
734    bool getChannel(size_t *channel) const {
735        if (mData1 >= 0x10 && mData1 <= 0x1f) {
736            *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
737            return true;
738        }
739        return false;
740    }
741
742    uint8_t mType;
743    uint8_t mData1;
744    uint8_t mData2;
745};
746
747static bool isNullPad(CCData *cc) {
748    return cc->mData1 < 0x10 && cc->mData2 < 0x10;
749}
750
751static void dumpBytePair(const sp<ABuffer> &ccBuf) {
752    size_t offset = 0;
753    AString out;
754
755    while (offset < ccBuf->size()) {
756        char tmp[128];
757
758        CCData *cc = (CCData *) (ccBuf->data() + offset);
759
760        if (isNullPad(cc)) {
761            // 1 null pad or XDS metadata, ignore
762            offset += sizeof(CCData);
763            continue;
764        }
765
766        if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
767            // 2 basic chars
768            sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
769        } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
770                 && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
771            // 1 special char
772            sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
773        } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
774                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
775            // 1 Spanish/French char
776            sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
777        } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
778                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
779            // 1 Portuguese/German/Danish char
780            sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
781        } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
782                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
783            // Mid-Row Codes (Table 69)
784            sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
785        } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
786                  && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
787                  ||
788                   ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
789                  && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
790            // Misc Control Codes (Table 70)
791            sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
792        } else if ((cc->mData1 & 0x70) == 0x10
793                && (cc->mData2 & 0x40) == 0x40
794                && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
795            // Preamble Address Codes (Table 71)
796            sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
797        } else {
798            sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
799        }
800
801        if (out.size() > 0) {
802            out.append(", ");
803        }
804
805        out.append(tmp);
806
807        offset += sizeof(CCData);
808    }
809
810    ALOGI("%s", out.c_str());
811}
812
813NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
814    : mNotify(notify),
815      mCurrentChannel(0),
816      mSelectedTrack(-1) {
817      for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
818          mTrackIndices[i] = -1;
819      }
820}
821
822size_t NuPlayer::CCDecoder::getTrackCount() const {
823    return mFoundChannels.size();
824}
825
826sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
827    if (!isTrackValid(index)) {
828        return NULL;
829    }
830
831    sp<AMessage> format = new AMessage();
832
833    format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
834    format->setString("language", "und");
835    format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
836    //CC1, field 0 channel 0
837    bool isDefaultAuto = (mFoundChannels[index] == 0);
838    format->setInt32("auto", isDefaultAuto);
839    format->setInt32("default", isDefaultAuto);
840    format->setInt32("forced", 0);
841
842    return format;
843}
844
845status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
846    if (!isTrackValid(index)) {
847        return BAD_VALUE;
848    }
849
850    if (select) {
851        if (mSelectedTrack == (ssize_t)index) {
852            ALOGE("track %zu already selected", index);
853            return BAD_VALUE;
854        }
855        ALOGV("selected track %zu", index);
856        mSelectedTrack = index;
857    } else {
858        if (mSelectedTrack != (ssize_t)index) {
859            ALOGE("track %zu is not selected", index);
860            return BAD_VALUE;
861        }
862        ALOGV("unselected track %zu", index);
863        mSelectedTrack = -1;
864    }
865
866    return OK;
867}
868
869bool NuPlayer::CCDecoder::isSelected() const {
870    return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
871}
872
873bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
874    return index < getTrackCount();
875}
876
877int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
878    if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
879        return mTrackIndices[channel];
880    }
881    return -1;
882}
883
884// returns true if a new CC track is found
885bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
886    int64_t timeUs;
887    CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
888
889    sp<ABuffer> sei;
890    if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
891        return false;
892    }
893
894    bool trackAdded = false;
895
896    NALBitReader br(sei->data() + 1, sei->size() - 1);
897    // sei_message()
898    while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
899        uint32_t payload_type = 0;
900        size_t payload_size = 0;
901        uint8_t last_byte;
902
903        do {
904            last_byte = br.getBits(8);
905            payload_type += last_byte;
906        } while (last_byte == 0xFF);
907
908        do {
909            last_byte = br.getBits(8);
910            payload_size += last_byte;
911        } while (last_byte == 0xFF);
912
913        // sei_payload()
914        if (payload_type == 4) {
915            // user_data_registered_itu_t_t35()
916
917            // ATSC A/72: 6.4.2
918            uint8_t itu_t_t35_country_code = br.getBits(8);
919            uint16_t itu_t_t35_provider_code = br.getBits(16);
920            uint32_t user_identifier = br.getBits(32);
921            uint8_t user_data_type_code = br.getBits(8);
922
923            payload_size -= 1 + 2 + 4 + 1;
924
925            if (itu_t_t35_country_code == 0xB5
926                    && itu_t_t35_provider_code == 0x0031
927                    && user_identifier == 'GA94'
928                    && user_data_type_code == 0x3) {
929                // MPEG_cc_data()
930                // ATSC A/53 Part 4: 6.2.3.1
931                br.skipBits(1); //process_em_data_flag
932                bool process_cc_data_flag = br.getBits(1);
933                br.skipBits(1); //additional_data_flag
934                size_t cc_count = br.getBits(5);
935                br.skipBits(8); // em_data;
936                payload_size -= 2;
937
938                if (process_cc_data_flag) {
939                    AString out;
940
941                    sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
942                    ccBuf->setRange(0, 0);
943
944                    for (size_t i = 0; i < cc_count; i++) {
945                        uint8_t marker = br.getBits(5);
946                        CHECK_EQ(marker, 0x1f);
947
948                        bool cc_valid = br.getBits(1);
949                        uint8_t cc_type = br.getBits(2);
950                        // remove odd parity bit
951                        uint8_t cc_data_1 = br.getBits(8) & 0x7f;
952                        uint8_t cc_data_2 = br.getBits(8) & 0x7f;
953
954                        if (cc_valid
955                                && (cc_type == 0 || cc_type == 1)) {
956                            CCData cc(cc_type, cc_data_1, cc_data_2);
957                            if (!isNullPad(&cc)) {
958                                size_t channel;
959                                if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
960                                    mTrackIndices[channel] = mFoundChannels.size();
961                                    mFoundChannels.push_back(channel);
962                                    trackAdded = true;
963                                }
964                                memcpy(ccBuf->data() + ccBuf->size(),
965                                        (void *)&cc, sizeof(cc));
966                                ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
967                            }
968                        }
969                    }
970                    payload_size -= cc_count * 3;
971
972                    mCCMap.add(timeUs, ccBuf);
973                    break;
974                }
975            } else {
976                ALOGV("Malformed SEI payload type 4");
977            }
978        } else {
979            ALOGV("Unsupported SEI payload type %d", payload_type);
980        }
981
982        // skipping remaining bits of this payload
983        br.skipBits(payload_size * 8);
984    }
985
986    return trackAdded;
987}
988
989sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
990        const sp<ABuffer> &ccBuf, size_t index) {
991    sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
992    filteredCCBuf->setRange(0, 0);
993
994    size_t cc_count = ccBuf->size() / sizeof(CCData);
995    const CCData* cc_data = (const CCData*)ccBuf->data();
996    for (size_t i = 0; i < cc_count; ++i) {
997        size_t channel;
998        if (cc_data[i].getChannel(&channel)) {
999            mCurrentChannel = channel;
1000        }
1001        if (mCurrentChannel == mFoundChannels[index]) {
1002            memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
1003                    (void *)&cc_data[i], sizeof(CCData));
1004            filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
1005        }
1006    }
1007
1008    return filteredCCBuf;
1009}
1010
1011void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
1012    if (extractFromSEI(accessUnit)) {
1013        ALOGI("Found CEA-608 track");
1014        sp<AMessage> msg = mNotify->dup();
1015        msg->setInt32("what", kWhatTrackAdded);
1016        msg->post();
1017    }
1018    // TODO: extract CC from other sources
1019}
1020
1021void NuPlayer::CCDecoder::display(int64_t timeUs) {
1022    if (!isTrackValid(mSelectedTrack)) {
1023        ALOGE("Could not find current track(index=%d)", mSelectedTrack);
1024        return;
1025    }
1026
1027    ssize_t index = mCCMap.indexOfKey(timeUs);
1028    if (index < 0) {
1029        ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
1030        return;
1031    }
1032
1033    sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
1034
1035    if (ccBuf->size() > 0) {
1036#if 0
1037        dumpBytePair(ccBuf);
1038#endif
1039
1040        ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
1041        ccBuf->meta()->setInt64("timeUs", timeUs);
1042        ccBuf->meta()->setInt64("durationUs", 0ll);
1043
1044        sp<AMessage> msg = mNotify->dup();
1045        msg->setInt32("what", kWhatClosedCaptionData);
1046        msg->setBuffer("buffer", ccBuf);
1047        msg->post();
1048    }
1049
1050    // remove all entries before timeUs
1051    mCCMap.removeItemsAt(0, index + 1);
1052}
1053
1054void NuPlayer::CCDecoder::flush() {
1055    mCCMap.clear();
1056}
1057
1058}  // namespace android
1059
1060