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