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