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