NuPlayerDecoder.cpp revision b86e68f834b7040518b99d1d0245d5f2e5cb9c86
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            }
599            break;
600        }
601
602        case kWhatRenderBuffer:
603        {
604            if (!isStaleReply(msg)) {
605                onRenderBuffer(msg);
606            }
607            break;
608        }
609
610        case kWhatFlush:
611        {
612            sp<AMessage> format;
613            if (msg->findMessage("new-format", &format)) {
614                rememberCodecSpecificData(format);
615            }
616            onFlush();
617            break;
618        }
619
620        case kWhatResume:
621        {
622            onResume();
623            break;
624        }
625
626        case kWhatShutdown:
627        {
628            onShutdown();
629            break;
630        }
631
632        default:
633            TRESPASS();
634            break;
635    }
636}
637
638void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) {
639    sp<AMessage> msg = new AMessage(kWhatFlush, id());
640    if (format != NULL) {
641        msg->setMessage("new-format", format);
642    }
643    msg->post();
644}
645
646void NuPlayer::Decoder::signalResume() {
647    (new AMessage(kWhatResume, id()))->post();
648}
649
650void NuPlayer::Decoder::initiateShutdown() {
651    (new AMessage(kWhatShutdown, id()))->post();
652}
653
654bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const {
655    if (targetFormat == NULL) {
656        return true;
657    }
658
659    AString mime;
660    if (!targetFormat->findString("mime", &mime)) {
661        return false;
662    }
663
664    if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) {
665        // field-by-field comparison
666        const char * keys[] = { "channel-count", "sample-rate", "is-adts" };
667        for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
668            int32_t oldVal, newVal;
669            if (!mOutputFormat->findInt32(keys[i], &oldVal) ||
670                    !targetFormat->findInt32(keys[i], &newVal) ||
671                    oldVal != newVal) {
672                return false;
673            }
674        }
675
676        sp<ABuffer> oldBuf, newBuf;
677        if (mOutputFormat->findBuffer("csd-0", &oldBuf) &&
678                targetFormat->findBuffer("csd-0", &newBuf)) {
679            if (oldBuf->size() != newBuf->size()) {
680                return false;
681            }
682            return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size());
683        }
684    }
685    return false;
686}
687
688bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const {
689    if (mOutputFormat == NULL) {
690        return false;
691    }
692
693    if (targetFormat == NULL) {
694        return true;
695    }
696
697    AString oldMime, newMime;
698    if (!mOutputFormat->findString("mime", &oldMime)
699            || !targetFormat->findString("mime", &newMime)
700            || !(oldMime == newMime)) {
701        return false;
702    }
703
704    bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/"));
705    bool seamless;
706    if (audio) {
707        seamless = supportsSeamlessAudioFormatChange(targetFormat);
708    } else {
709        int32_t isAdaptive;
710        seamless = (mCodec != NULL &&
711                mInputFormat->findInt32("adaptive-playback", &isAdaptive) &&
712                isAdaptive);
713    }
714
715    ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str());
716    return seamless;
717}
718
719struct CCData {
720    CCData(uint8_t type, uint8_t data1, uint8_t data2)
721        : mType(type), mData1(data1), mData2(data2) {
722    }
723    bool getChannel(size_t *channel) const {
724        if (mData1 >= 0x10 && mData1 <= 0x1f) {
725            *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0);
726            return true;
727        }
728        return false;
729    }
730
731    uint8_t mType;
732    uint8_t mData1;
733    uint8_t mData2;
734};
735
736static bool isNullPad(CCData *cc) {
737    return cc->mData1 < 0x10 && cc->mData2 < 0x10;
738}
739
740static void dumpBytePair(const sp<ABuffer> &ccBuf) {
741    size_t offset = 0;
742    AString out;
743
744    while (offset < ccBuf->size()) {
745        char tmp[128];
746
747        CCData *cc = (CCData *) (ccBuf->data() + offset);
748
749        if (isNullPad(cc)) {
750            // 1 null pad or XDS metadata, ignore
751            offset += sizeof(CCData);
752            continue;
753        }
754
755        if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) {
756            // 2 basic chars
757            sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2);
758        } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
759                 && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) {
760            // 1 special char
761            sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2);
762        } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A)
763                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
764            // 1 Spanish/French char
765            sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2);
766        } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B)
767                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){
768            // 1 Portuguese/German/Danish char
769            sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2);
770        } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19)
771                 && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){
772            // Mid-Row Codes (Table 69)
773            sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2);
774        } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c)
775                  && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f)
776                  ||
777                   ((cc->mData1 == 0x17 || cc->mData1 == 0x1f)
778                  && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){
779            // Misc Control Codes (Table 70)
780            sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2);
781        } else if ((cc->mData1 & 0x70) == 0x10
782                && (cc->mData2 & 0x40) == 0x40
783                && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) {
784            // Preamble Address Codes (Table 71)
785            sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2);
786        } else {
787            sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2);
788        }
789
790        if (out.size() > 0) {
791            out.append(", ");
792        }
793
794        out.append(tmp);
795
796        offset += sizeof(CCData);
797    }
798
799    ALOGI("%s", out.c_str());
800}
801
802NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> &notify)
803    : mNotify(notify),
804      mCurrentChannel(0),
805      mSelectedTrack(-1) {
806      for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) {
807          mTrackIndices[i] = -1;
808      }
809}
810
811size_t NuPlayer::CCDecoder::getTrackCount() const {
812    return mFoundChannels.size();
813}
814
815sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const {
816    if (!isTrackValid(index)) {
817        return NULL;
818    }
819
820    sp<AMessage> format = new AMessage();
821
822    format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE);
823    format->setString("language", "und");
824    format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608);
825    //CC1, field 0 channel 0
826    bool isDefaultAuto = (mFoundChannels[index] == 0);
827    format->setInt32("auto", isDefaultAuto);
828    format->setInt32("default", isDefaultAuto);
829    format->setInt32("forced", 0);
830
831    return format;
832}
833
834status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) {
835    if (!isTrackValid(index)) {
836        return BAD_VALUE;
837    }
838
839    if (select) {
840        if (mSelectedTrack == (ssize_t)index) {
841            ALOGE("track %zu already selected", index);
842            return BAD_VALUE;
843        }
844        ALOGV("selected track %zu", index);
845        mSelectedTrack = index;
846    } else {
847        if (mSelectedTrack != (ssize_t)index) {
848            ALOGE("track %zu is not selected", index);
849            return BAD_VALUE;
850        }
851        ALOGV("unselected track %zu", index);
852        mSelectedTrack = -1;
853    }
854
855    return OK;
856}
857
858bool NuPlayer::CCDecoder::isSelected() const {
859    return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount();
860}
861
862bool NuPlayer::CCDecoder::isTrackValid(size_t index) const {
863    return index < getTrackCount();
864}
865
866int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const {
867    if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) {
868        return mTrackIndices[channel];
869    }
870    return -1;
871}
872
873// returns true if a new CC track is found
874bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) {
875    int64_t timeUs;
876    CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
877
878    sp<ABuffer> sei;
879    if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) {
880        return false;
881    }
882
883    bool trackAdded = false;
884
885    NALBitReader br(sei->data() + 1, sei->size() - 1);
886    // sei_message()
887    while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message()
888        uint32_t payload_type = 0;
889        size_t payload_size = 0;
890        uint8_t last_byte;
891
892        do {
893            last_byte = br.getBits(8);
894            payload_type += last_byte;
895        } while (last_byte == 0xFF);
896
897        do {
898            last_byte = br.getBits(8);
899            payload_size += last_byte;
900        } while (last_byte == 0xFF);
901
902        // sei_payload()
903        if (payload_type == 4) {
904            // user_data_registered_itu_t_t35()
905
906            // ATSC A/72: 6.4.2
907            uint8_t itu_t_t35_country_code = br.getBits(8);
908            uint16_t itu_t_t35_provider_code = br.getBits(16);
909            uint32_t user_identifier = br.getBits(32);
910            uint8_t user_data_type_code = br.getBits(8);
911
912            payload_size -= 1 + 2 + 4 + 1;
913
914            if (itu_t_t35_country_code == 0xB5
915                    && itu_t_t35_provider_code == 0x0031
916                    && user_identifier == 'GA94'
917                    && user_data_type_code == 0x3) {
918                // MPEG_cc_data()
919                // ATSC A/53 Part 4: 6.2.3.1
920                br.skipBits(1); //process_em_data_flag
921                bool process_cc_data_flag = br.getBits(1);
922                br.skipBits(1); //additional_data_flag
923                size_t cc_count = br.getBits(5);
924                br.skipBits(8); // em_data;
925                payload_size -= 2;
926
927                if (process_cc_data_flag) {
928                    AString out;
929
930                    sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData));
931                    ccBuf->setRange(0, 0);
932
933                    for (size_t i = 0; i < cc_count; i++) {
934                        uint8_t marker = br.getBits(5);
935                        CHECK_EQ(marker, 0x1f);
936
937                        bool cc_valid = br.getBits(1);
938                        uint8_t cc_type = br.getBits(2);
939                        // remove odd parity bit
940                        uint8_t cc_data_1 = br.getBits(8) & 0x7f;
941                        uint8_t cc_data_2 = br.getBits(8) & 0x7f;
942
943                        if (cc_valid
944                                && (cc_type == 0 || cc_type == 1)) {
945                            CCData cc(cc_type, cc_data_1, cc_data_2);
946                            if (!isNullPad(&cc)) {
947                                size_t channel;
948                                if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) {
949                                    mTrackIndices[channel] = mFoundChannels.size();
950                                    mFoundChannels.push_back(channel);
951                                    trackAdded = true;
952                                }
953                                memcpy(ccBuf->data() + ccBuf->size(),
954                                        (void *)&cc, sizeof(cc));
955                                ccBuf->setRange(0, ccBuf->size() + sizeof(CCData));
956                            }
957                        }
958                    }
959                    payload_size -= cc_count * 3;
960
961                    mCCMap.add(timeUs, ccBuf);
962                    break;
963                }
964            } else {
965                ALOGV("Malformed SEI payload type 4");
966            }
967        } else {
968            ALOGV("Unsupported SEI payload type %d", payload_type);
969        }
970
971        // skipping remaining bits of this payload
972        br.skipBits(payload_size * 8);
973    }
974
975    return trackAdded;
976}
977
978sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf(
979        const sp<ABuffer> &ccBuf, size_t index) {
980    sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size());
981    filteredCCBuf->setRange(0, 0);
982
983    size_t cc_count = ccBuf->size() / sizeof(CCData);
984    const CCData* cc_data = (const CCData*)ccBuf->data();
985    for (size_t i = 0; i < cc_count; ++i) {
986        size_t channel;
987        if (cc_data[i].getChannel(&channel)) {
988            mCurrentChannel = channel;
989        }
990        if (mCurrentChannel == mFoundChannels[index]) {
991            memcpy(filteredCCBuf->data() + filteredCCBuf->size(),
992                    (void *)&cc_data[i], sizeof(CCData));
993            filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData));
994        }
995    }
996
997    return filteredCCBuf;
998}
999
1000void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) {
1001    if (extractFromSEI(accessUnit)) {
1002        ALOGI("Found CEA-608 track");
1003        sp<AMessage> msg = mNotify->dup();
1004        msg->setInt32("what", kWhatTrackAdded);
1005        msg->post();
1006    }
1007    // TODO: extract CC from other sources
1008}
1009
1010void NuPlayer::CCDecoder::display(int64_t timeUs) {
1011    if (!isTrackValid(mSelectedTrack)) {
1012        ALOGE("Could not find current track(index=%d)", mSelectedTrack);
1013        return;
1014    }
1015
1016    ssize_t index = mCCMap.indexOfKey(timeUs);
1017    if (index < 0) {
1018        ALOGV("cc for timestamp %" PRId64 " not found", timeUs);
1019        return;
1020    }
1021
1022    sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack);
1023
1024    if (ccBuf->size() > 0) {
1025#if 0
1026        dumpBytePair(ccBuf);
1027#endif
1028
1029        ccBuf->meta()->setInt32("trackIndex", mSelectedTrack);
1030        ccBuf->meta()->setInt64("timeUs", timeUs);
1031        ccBuf->meta()->setInt64("durationUs", 0ll);
1032
1033        sp<AMessage> msg = mNotify->dup();
1034        msg->setInt32("what", kWhatClosedCaptionData);
1035        msg->setBuffer("buffer", ccBuf);
1036        msg->post();
1037    }
1038
1039    // remove all entries before timeUs
1040    mCCMap.removeItemsAt(0, index + 1);
1041}
1042
1043void NuPlayer::CCDecoder::flush() {
1044    mCCMap.clear();
1045}
1046
1047}  // namespace android
1048
1049