NuPlayer.cpp revision 48296b792a8d68358de74141fa80bd5bd84d0307
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 "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
22
23#include "HTTPLiveSource.h"
24#include "NuPlayerDecoder.h"
25#include "NuPlayerDecoderPassThrough.h"
26#include "NuPlayerDriver.h"
27#include "NuPlayerRenderer.h"
28#include "NuPlayerSource.h"
29#include "RTSPSource.h"
30#include "StreamingSource.h"
31#include "GenericSource.h"
32#include "TextDescriptions.h"
33
34#include "ATSParser.h"
35
36#include <media/stagefright/foundation/hexdump.h>
37#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/MediaBuffer.h>
41#include <media/stagefright/MediaDefs.h>
42#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
44#include <gui/IGraphicBufferProducer.h>
45
46#include "avc_utils.h"
47
48#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
51namespace android {
52
53struct NuPlayer::Action : public RefBase {
54    Action() {}
55
56    virtual void execute(NuPlayer *player) = 0;
57
58private:
59    DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63    SeekAction(int64_t seekTimeUs)
64        : mSeekTimeUs(seekTimeUs) {
65    }
66
67    virtual void execute(NuPlayer *player) {
68        player->performSeek(mSeekTimeUs);
69    }
70
71private:
72    int64_t mSeekTimeUs;
73
74    DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
77struct NuPlayer::SetSurfaceAction : public Action {
78    SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79        : mWrapper(wrapper) {
80    }
81
82    virtual void execute(NuPlayer *player) {
83        player->performSetSurface(mWrapper);
84    }
85
86private:
87    sp<NativeWindowWrapper> mWrapper;
88
89    DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
92struct NuPlayer::ShutdownDecoderAction : public Action {
93    ShutdownDecoderAction(bool audio, bool video)
94        : mAudio(audio),
95          mVideo(video) {
96    }
97
98    virtual void execute(NuPlayer *player) {
99        player->performDecoderShutdown(mAudio, mVideo);
100    }
101
102private:
103    bool mAudio;
104    bool mVideo;
105
106    DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110    PostMessageAction(const sp<AMessage> &msg)
111        : mMessage(msg) {
112    }
113
114    virtual void execute(NuPlayer *) {
115        mMessage->post();
116    }
117
118private:
119    sp<AMessage> mMessage;
120
121    DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127    typedef void (NuPlayer::*ActionFunc)();
128
129    SimpleAction(ActionFunc func)
130        : mFunc(func) {
131    }
132
133    virtual void execute(NuPlayer *player) {
134        (player->*mFunc)();
135    }
136
137private:
138    ActionFunc mFunc;
139
140    DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
146    : mUIDValid(false),
147      mSourceFlags(0),
148      mCurrentPositionUs(0),
149      mVideoIsAVC(false),
150      mOffloadAudio(false),
151      mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
152      mAudioDecoderGeneration(0),
153      mVideoDecoderGeneration(0),
154      mAudioEOS(false),
155      mVideoEOS(false),
156      mScanSourcesPending(false),
157      mScanSourcesGeneration(0),
158      mPollDurationGeneration(0),
159      mTimedTextGeneration(0),
160      mTimeDiscontinuityPending(false),
161      mFlushingAudio(NONE),
162      mFlushingVideo(NONE),
163      mSkipRenderingAudioUntilMediaTimeUs(-1ll),
164      mSkipRenderingVideoUntilMediaTimeUs(-1ll),
165      mVideoLateByUs(0ll),
166      mNumFramesTotal(0ll),
167      mNumFramesDropped(0ll),
168      mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
169      mStarted(false) {
170}
171
172NuPlayer::~NuPlayer() {
173}
174
175void NuPlayer::setUID(uid_t uid) {
176    mUIDValid = true;
177    mUID = uid;
178}
179
180void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
181    mDriver = driver;
182}
183
184void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
185    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
186
187    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
188
189    msg->setObject("source", new StreamingSource(notify, source));
190    msg->post();
191}
192
193static bool IsHTTPLiveURL(const char *url) {
194    if (!strncasecmp("http://", url, 7)
195            || !strncasecmp("https://", url, 8)
196            || !strncasecmp("file://", url, 7)) {
197        size_t len = strlen(url);
198        if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
199            return true;
200        }
201
202        if (strstr(url,"m3u8")) {
203            return true;
204        }
205    }
206
207    return false;
208}
209
210void NuPlayer::setDataSourceAsync(
211        const sp<IMediaHTTPService> &httpService,
212        const char *url,
213        const KeyedVector<String8, String8> *headers) {
214
215    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
216    size_t len = strlen(url);
217
218    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
219
220    sp<Source> source;
221    if (IsHTTPLiveURL(url)) {
222        source = new HTTPLiveSource(notify, httpService, url, headers);
223    } else if (!strncasecmp(url, "rtsp://", 7)) {
224        source = new RTSPSource(
225                notify, httpService, url, headers, mUIDValid, mUID);
226    } else if ((!strncasecmp(url, "http://", 7)
227                || !strncasecmp(url, "https://", 8))
228                    && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
229                    || strstr(url, ".sdp?"))) {
230        source = new RTSPSource(
231                notify, httpService, url, headers, mUIDValid, mUID, true);
232    } else {
233        sp<GenericSource> genericSource =
234                new GenericSource(notify, mUIDValid, mUID);
235        // Don't set FLAG_SECURE on mSourceFlags here for widevine.
236        // The correct flags will be updated in Source::kWhatFlagsChanged
237        // handler when  GenericSource is prepared.
238
239        status_t err = genericSource->setDataSource(httpService, url, headers);
240
241        if (err == OK) {
242            source = genericSource;
243        } else {
244            ALOGE("Failed to set data source!");
245        }
246    }
247    msg->setObject("source", source);
248    msg->post();
249}
250
251void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
252    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
253
254    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
255
256    sp<GenericSource> source =
257            new GenericSource(notify, mUIDValid, mUID);
258
259    status_t err = source->setDataSource(fd, offset, length);
260
261    if (err != OK) {
262        ALOGE("Failed to set data source!");
263        source = NULL;
264    }
265
266    msg->setObject("source", source);
267    msg->post();
268}
269
270void NuPlayer::prepareAsync() {
271    (new AMessage(kWhatPrepare, id()))->post();
272}
273
274void NuPlayer::setVideoSurfaceTextureAsync(
275        const sp<IGraphicBufferProducer> &bufferProducer) {
276    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
277
278    if (bufferProducer == NULL) {
279        msg->setObject("native-window", NULL);
280    } else {
281        msg->setObject(
282                "native-window",
283                new NativeWindowWrapper(
284                    new Surface(bufferProducer, true /* controlledByApp */)));
285    }
286
287    msg->post();
288}
289
290void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
291    sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
292    msg->setObject("sink", sink);
293    msg->post();
294}
295
296void NuPlayer::start() {
297    (new AMessage(kWhatStart, id()))->post();
298}
299
300void NuPlayer::pause() {
301    (new AMessage(kWhatPause, id()))->post();
302}
303
304void NuPlayer::resume() {
305    (new AMessage(kWhatResume, id()))->post();
306}
307
308void NuPlayer::resetAsync() {
309    if (mSource != NULL) {
310        // During a reset, the data source might be unresponsive already, we need to
311        // disconnect explicitly so that reads exit promptly.
312        // We can't queue the disconnect request to the looper, as it might be
313        // queued behind a stuck read and never gets processed.
314        // Doing a disconnect outside the looper to allows the pending reads to exit
315        // (either successfully or with error).
316        mSource->disconnect();
317    }
318
319    (new AMessage(kWhatReset, id()))->post();
320}
321
322void NuPlayer::seekToAsync(int64_t seekTimeUs) {
323    sp<AMessage> msg = new AMessage(kWhatSeek, id());
324    msg->setInt64("seekTimeUs", seekTimeUs);
325    msg->post();
326}
327
328// static
329bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
330    switch (state) {
331        case FLUSHING_DECODER:
332            if (needShutdown != NULL) {
333                *needShutdown = false;
334            }
335            return true;
336
337        case FLUSHING_DECODER_SHUTDOWN:
338            if (needShutdown != NULL) {
339                *needShutdown = true;
340            }
341            return true;
342
343        default:
344            return false;
345    }
346}
347
348void NuPlayer::writeTrackInfo(
349        Parcel* reply, const sp<AMessage> format) const {
350    int32_t trackType;
351    CHECK(format->findInt32("type", &trackType));
352
353    AString lang;
354    CHECK(format->findString("language", &lang));
355
356    reply->writeInt32(2); // write something non-zero
357    reply->writeInt32(trackType);
358    reply->writeString16(String16(lang.c_str()));
359
360    if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
361        AString mime;
362        CHECK(format->findString("mime", &mime));
363
364        int32_t isAuto, isDefault, isForced;
365        CHECK(format->findInt32("auto", &isAuto));
366        CHECK(format->findInt32("default", &isDefault));
367        CHECK(format->findInt32("forced", &isForced));
368
369        reply->writeString16(String16(mime.c_str()));
370        reply->writeInt32(isAuto);
371        reply->writeInt32(isDefault);
372        reply->writeInt32(isForced);
373    }
374}
375
376void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
377    switch (msg->what()) {
378        case kWhatSetDataSource:
379        {
380            ALOGV("kWhatSetDataSource");
381
382            CHECK(mSource == NULL);
383
384            status_t err = OK;
385            sp<RefBase> obj;
386            CHECK(msg->findObject("source", &obj));
387            if (obj != NULL) {
388                mSource = static_cast<Source *>(obj.get());
389            } else {
390                err = UNKNOWN_ERROR;
391            }
392
393            CHECK(mDriver != NULL);
394            sp<NuPlayerDriver> driver = mDriver.promote();
395            if (driver != NULL) {
396                driver->notifySetDataSourceCompleted(err);
397            }
398            break;
399        }
400
401        case kWhatPrepare:
402        {
403            mSource->prepareAsync();
404            break;
405        }
406
407        case kWhatGetTrackInfo:
408        {
409            uint32_t replyID;
410            CHECK(msg->senderAwaitsResponse(&replyID));
411
412            Parcel* reply;
413            CHECK(msg->findPointer("reply", (void**)&reply));
414
415            size_t inbandTracks = 0;
416            if (mSource != NULL) {
417                inbandTracks = mSource->getTrackCount();
418            }
419
420            size_t ccTracks = 0;
421            if (mCCDecoder != NULL) {
422                ccTracks = mCCDecoder->getTrackCount();
423            }
424
425            // total track count
426            reply->writeInt32(inbandTracks + ccTracks);
427
428            // write inband tracks
429            for (size_t i = 0; i < inbandTracks; ++i) {
430                writeTrackInfo(reply, mSource->getTrackInfo(i));
431            }
432
433            // write CC track
434            for (size_t i = 0; i < ccTracks; ++i) {
435                writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
436            }
437
438            sp<AMessage> response = new AMessage;
439            response->postReply(replyID);
440            break;
441        }
442
443        case kWhatGetSelectedTrack:
444        {
445            status_t err = INVALID_OPERATION;
446            if (mSource != NULL) {
447                err = OK;
448
449                int32_t type32;
450                CHECK(msg->findInt32("type", (int32_t*)&type32));
451                media_track_type type = (media_track_type)type32;
452                ssize_t selectedTrack = mSource->getSelectedTrack(type);
453
454                Parcel* reply;
455                CHECK(msg->findPointer("reply", (void**)&reply));
456                reply->writeInt32(selectedTrack);
457            }
458
459            sp<AMessage> response = new AMessage;
460            response->setInt32("err", err);
461
462            uint32_t replyID;
463            CHECK(msg->senderAwaitsResponse(&replyID));
464            response->postReply(replyID);
465            break;
466        }
467
468        case kWhatSelectTrack:
469        {
470            uint32_t replyID;
471            CHECK(msg->senderAwaitsResponse(&replyID));
472
473            size_t trackIndex;
474            int32_t select;
475            CHECK(msg->findSize("trackIndex", &trackIndex));
476            CHECK(msg->findInt32("select", &select));
477
478            status_t err = INVALID_OPERATION;
479
480            size_t inbandTracks = 0;
481            if (mSource != NULL) {
482                inbandTracks = mSource->getTrackCount();
483            }
484            size_t ccTracks = 0;
485            if (mCCDecoder != NULL) {
486                ccTracks = mCCDecoder->getTrackCount();
487            }
488
489            if (trackIndex < inbandTracks) {
490                err = mSource->selectTrack(trackIndex, select);
491
492                if (!select && err == OK) {
493                    int32_t type;
494                    sp<AMessage> info = mSource->getTrackInfo(trackIndex);
495                    if (info != NULL
496                            && info->findInt32("type", &type)
497                            && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
498                        ++mTimedTextGeneration;
499                    }
500                }
501            } else {
502                trackIndex -= inbandTracks;
503
504                if (trackIndex < ccTracks) {
505                    err = mCCDecoder->selectTrack(trackIndex, select);
506                }
507            }
508
509            sp<AMessage> response = new AMessage;
510            response->setInt32("err", err);
511
512            response->postReply(replyID);
513            break;
514        }
515
516        case kWhatPollDuration:
517        {
518            int32_t generation;
519            CHECK(msg->findInt32("generation", &generation));
520
521            if (generation != mPollDurationGeneration) {
522                // stale
523                break;
524            }
525
526            int64_t durationUs;
527            if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
528                sp<NuPlayerDriver> driver = mDriver.promote();
529                if (driver != NULL) {
530                    driver->notifyDuration(durationUs);
531                }
532            }
533
534            msg->post(1000000ll);  // poll again in a second.
535            break;
536        }
537
538        case kWhatSetVideoNativeWindow:
539        {
540            ALOGV("kWhatSetVideoNativeWindow");
541
542            mDeferredActions.push_back(
543                    new ShutdownDecoderAction(
544                        false /* audio */, true /* video */));
545
546            sp<RefBase> obj;
547            CHECK(msg->findObject("native-window", &obj));
548
549            mDeferredActions.push_back(
550                    new SetSurfaceAction(
551                        static_cast<NativeWindowWrapper *>(obj.get())));
552
553            if (obj != NULL) {
554                if (mStarted && mVideoDecoder != NULL) {
555                    // Issue a seek to refresh the video screen only if started otherwise
556                    // the extractor may not yet be started and will assert.
557                    // If the video decoder is not set (perhaps audio only in this case)
558                    // do not perform a seek as it is not needed.
559                    mDeferredActions.push_back(new SeekAction(mCurrentPositionUs));
560                }
561
562                // If there is a new surface texture, instantiate decoders
563                // again if possible.
564                mDeferredActions.push_back(
565                        new SimpleAction(&NuPlayer::performScanSources));
566            }
567
568            processDeferredActions();
569            break;
570        }
571
572        case kWhatSetAudioSink:
573        {
574            ALOGV("kWhatSetAudioSink");
575
576            sp<RefBase> obj;
577            CHECK(msg->findObject("sink", &obj));
578
579            mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
580            break;
581        }
582
583        case kWhatStart:
584        {
585            ALOGV("kWhatStart");
586
587            mVideoIsAVC = false;
588            mOffloadAudio = false;
589            mAudioEOS = false;
590            mVideoEOS = false;
591            mSkipRenderingAudioUntilMediaTimeUs = -1;
592            mSkipRenderingVideoUntilMediaTimeUs = -1;
593            mVideoLateByUs = 0;
594            mNumFramesTotal = 0;
595            mNumFramesDropped = 0;
596            mStarted = true;
597
598            /* instantiate decoders now for secure playback */
599            if (mSourceFlags & Source::FLAG_SECURE) {
600                if (mNativeWindow != NULL) {
601                    instantiateDecoder(false, &mVideoDecoder);
602                }
603
604                if (mAudioSink != NULL) {
605                    instantiateDecoder(true, &mAudioDecoder);
606                }
607            }
608
609            mSource->start();
610
611            uint32_t flags = 0;
612
613            if (mSource->isRealTime()) {
614                flags |= Renderer::FLAG_REAL_TIME;
615            }
616
617            sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
618            audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
619            if (mAudioSink != NULL) {
620                streamType = mAudioSink->getAudioStreamType();
621            }
622
623            sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
624
625            mOffloadAudio =
626                canOffloadStream(audioMeta, (videoFormat != NULL),
627                                 true /* is_streaming */, streamType);
628            if (mOffloadAudio) {
629                flags |= Renderer::FLAG_OFFLOAD_AUDIO;
630            }
631
632            mRenderer = new Renderer(
633                    mAudioSink,
634                    new AMessage(kWhatRendererNotify, id()),
635                    flags);
636
637            mRendererLooper = new ALooper;
638            mRendererLooper->setName("NuPlayerRenderer");
639            mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
640            mRendererLooper->registerHandler(mRenderer);
641
642            postScanSources();
643            break;
644        }
645
646        case kWhatScanSources:
647        {
648            int32_t generation;
649            CHECK(msg->findInt32("generation", &generation));
650            if (generation != mScanSourcesGeneration) {
651                // Drop obsolete msg.
652                break;
653            }
654
655            mScanSourcesPending = false;
656
657            ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
658                 mAudioDecoder != NULL, mVideoDecoder != NULL);
659
660            bool mHadAnySourcesBefore =
661                (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
662
663            // initialize video before audio because successful initialization of
664            // video may change deep buffer mode of audio.
665            if (mNativeWindow != NULL) {
666                instantiateDecoder(false, &mVideoDecoder);
667            }
668
669            if (mAudioSink != NULL) {
670                if (mOffloadAudio) {
671                    // open audio sink early under offload mode.
672                    sp<AMessage> format = mSource->getFormat(true /*audio*/);
673                    openAudioSink(format, true /*offloadOnly*/);
674                }
675                instantiateDecoder(true, &mAudioDecoder);
676            }
677
678            if (!mHadAnySourcesBefore
679                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
680                // This is the first time we've found anything playable.
681
682                if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
683                    schedulePollDuration();
684                }
685            }
686
687            status_t err;
688            if ((err = mSource->feedMoreTSData()) != OK) {
689                if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
690                    // We're not currently decoding anything (no audio or
691                    // video tracks found) and we just ran out of input data.
692
693                    if (err == ERROR_END_OF_STREAM) {
694                        notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
695                    } else {
696                        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
697                    }
698                }
699                break;
700            }
701
702            if ((mAudioDecoder == NULL && mAudioSink != NULL)
703                    || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
704                msg->post(100000ll);
705                mScanSourcesPending = true;
706            }
707            break;
708        }
709
710        case kWhatVideoNotify:
711        case kWhatAudioNotify:
712        {
713            bool audio = msg->what() == kWhatAudioNotify;
714
715            int32_t currentDecoderGeneration =
716                (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
717            int32_t requesterGeneration = currentDecoderGeneration - 1;
718            CHECK(msg->findInt32("generation", &requesterGeneration));
719
720            if (requesterGeneration != currentDecoderGeneration) {
721                ALOGV("got message from old %s decoder, generation(%d:%d)",
722                        audio ? "audio" : "video", requesterGeneration,
723                        currentDecoderGeneration);
724                sp<AMessage> reply;
725                if (!(msg->findMessage("reply", &reply))) {
726                    return;
727                }
728
729                reply->setInt32("err", INFO_DISCONTINUITY);
730                reply->post();
731                return;
732            }
733
734            int32_t what;
735            CHECK(msg->findInt32("what", &what));
736
737            if (what == Decoder::kWhatFillThisBuffer) {
738                status_t err = feedDecoderInputData(
739                        audio, msg);
740
741                if (err == -EWOULDBLOCK) {
742                    if (mSource->feedMoreTSData() == OK) {
743                        msg->post(10000ll);
744                    }
745                }
746            } else if (what == Decoder::kWhatEOS) {
747                int32_t err;
748                CHECK(msg->findInt32("err", &err));
749
750                if (err == ERROR_END_OF_STREAM) {
751                    ALOGV("got %s decoder EOS", audio ? "audio" : "video");
752                } else {
753                    ALOGV("got %s decoder EOS w/ error %d",
754                         audio ? "audio" : "video",
755                         err);
756                }
757
758                mRenderer->queueEOS(audio, err);
759            } else if (what == Decoder::kWhatFlushCompleted) {
760                bool needShutdown;
761
762                if (audio) {
763                    CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
764                    mFlushingAudio = FLUSHED;
765                } else {
766                    CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
767                    mFlushingVideo = FLUSHED;
768
769                    mVideoLateByUs = 0;
770                }
771
772                ALOGV("decoder %s flush completed", audio ? "audio" : "video");
773
774                if (needShutdown) {
775                    ALOGV("initiating %s decoder shutdown",
776                         audio ? "audio" : "video");
777
778                    getDecoder(audio)->initiateShutdown();
779
780                    if (audio) {
781                        mFlushingAudio = SHUTTING_DOWN_DECODER;
782                    } else {
783                        mFlushingVideo = SHUTTING_DOWN_DECODER;
784                    }
785                }
786
787                finishFlushIfPossible();
788            } else if (what == Decoder::kWhatOutputFormatChanged) {
789                sp<AMessage> format;
790                CHECK(msg->findMessage("format", &format));
791
792                if (audio) {
793                    openAudioSink(format, false /*offloadOnly*/);
794                } else {
795                    // video
796                    sp<AMessage> inputFormat =
797                            mSource->getFormat(false /* audio */);
798
799                    updateVideoSize(inputFormat, format);
800                }
801            } else if (what == Decoder::kWhatShutdownCompleted) {
802                ALOGV("%s shutdown completed", audio ? "audio" : "video");
803                if (audio) {
804                    mAudioDecoder.clear();
805
806                    CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
807                    mFlushingAudio = SHUT_DOWN;
808                } else {
809                    mVideoDecoder.clear();
810
811                    CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
812                    mFlushingVideo = SHUT_DOWN;
813                }
814
815                finishFlushIfPossible();
816            } else if (what == Decoder::kWhatError) {
817                ALOGE("Received error from %s decoder, aborting playback.",
818                     audio ? "audio" : "video");
819
820                status_t err;
821                if (!msg->findInt32("err", &err)) {
822                    err = UNKNOWN_ERROR;
823                }
824                mRenderer->queueEOS(audio, err);
825                if (audio && mFlushingAudio != NONE) {
826                    mAudioDecoder.clear();
827                    mFlushingAudio = SHUT_DOWN;
828                } else if (!audio && mFlushingVideo != NONE){
829                    mVideoDecoder.clear();
830                    mFlushingVideo = SHUT_DOWN;
831                }
832                finishFlushIfPossible();
833            } else if (what == Decoder::kWhatDrainThisBuffer) {
834                renderBuffer(audio, msg);
835            } else {
836                ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
837                      what,
838                      what >> 24,
839                      (what >> 16) & 0xff,
840                      (what >> 8) & 0xff,
841                      what & 0xff);
842            }
843
844            break;
845        }
846
847        case kWhatRendererNotify:
848        {
849            int32_t what;
850            CHECK(msg->findInt32("what", &what));
851
852            if (what == Renderer::kWhatEOS) {
853                int32_t audio;
854                CHECK(msg->findInt32("audio", &audio));
855
856                int32_t finalResult;
857                CHECK(msg->findInt32("finalResult", &finalResult));
858
859                if (audio) {
860                    mAudioEOS = true;
861                } else {
862                    mVideoEOS = true;
863                }
864
865                if (finalResult == ERROR_END_OF_STREAM) {
866                    ALOGV("reached %s EOS", audio ? "audio" : "video");
867                } else {
868                    ALOGE("%s track encountered an error (%d)",
869                         audio ? "audio" : "video", finalResult);
870
871                    notifyListener(
872                            MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
873                }
874
875                if ((mAudioEOS || mAudioDecoder == NULL)
876                        && (mVideoEOS || mVideoDecoder == NULL)) {
877                    notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
878                }
879            } else if (what == Renderer::kWhatPosition) {
880                int64_t positionUs;
881                CHECK(msg->findInt64("positionUs", &positionUs));
882                mCurrentPositionUs = positionUs;
883
884                CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
885
886                if (mDriver != NULL) {
887                    sp<NuPlayerDriver> driver = mDriver.promote();
888                    if (driver != NULL) {
889                        driver->notifyPosition(positionUs);
890
891                        driver->notifyFrameStats(
892                                mNumFramesTotal, mNumFramesDropped);
893                    }
894                }
895            } else if (what == Renderer::kWhatFlushComplete) {
896                int32_t audio;
897                CHECK(msg->findInt32("audio", &audio));
898
899                ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
900            } else if (what == Renderer::kWhatVideoRenderingStart) {
901                notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
902            } else if (what == Renderer::kWhatMediaRenderingStart) {
903                ALOGV("media rendering started");
904                notifyListener(MEDIA_STARTED, 0, 0);
905            } else if (what == Renderer::kWhatAudioOffloadTearDown) {
906                ALOGV("Tear down audio offload, fall back to s/w path");
907                int64_t positionUs;
908                CHECK(msg->findInt64("positionUs", &positionUs));
909                closeAudioSink();
910                mAudioDecoder.clear();
911                mRenderer->flush(true /* audio */);
912                if (mVideoDecoder != NULL) {
913                    mRenderer->flush(false /* audio */);
914                }
915                mRenderer->signalDisableOffloadAudio();
916                mOffloadAudio = false;
917
918                performSeek(positionUs);
919                instantiateDecoder(true /* audio */, &mAudioDecoder);
920            }
921            break;
922        }
923
924        case kWhatMoreDataQueued:
925        {
926            break;
927        }
928
929        case kWhatReset:
930        {
931            ALOGV("kWhatReset");
932
933            mDeferredActions.push_back(
934                    new ShutdownDecoderAction(
935                        true /* audio */, true /* video */));
936
937            mDeferredActions.push_back(
938                    new SimpleAction(&NuPlayer::performReset));
939
940            processDeferredActions();
941            break;
942        }
943
944        case kWhatSeek:
945        {
946            int64_t seekTimeUs;
947            CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
948
949            ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
950
951            mDeferredActions.push_back(
952                    new SimpleAction(&NuPlayer::performDecoderFlush));
953
954            mDeferredActions.push_back(new SeekAction(seekTimeUs));
955
956            processDeferredActions();
957            break;
958        }
959
960        case kWhatPause:
961        {
962            CHECK(mRenderer != NULL);
963            mSource->pause();
964            mRenderer->pause();
965            break;
966        }
967
968        case kWhatResume:
969        {
970            CHECK(mRenderer != NULL);
971            mSource->resume();
972            mRenderer->resume();
973            break;
974        }
975
976        case kWhatSourceNotify:
977        {
978            onSourceNotify(msg);
979            break;
980        }
981
982        case kWhatClosedCaptionNotify:
983        {
984            onClosedCaptionNotify(msg);
985            break;
986        }
987
988        default:
989            TRESPASS();
990            break;
991    }
992}
993
994void NuPlayer::finishFlushIfPossible() {
995    if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
996            && mFlushingAudio != SHUT_DOWN) {
997        return;
998    }
999
1000    if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1001            && mFlushingVideo != SHUT_DOWN) {
1002        return;
1003    }
1004
1005    ALOGV("both audio and video are flushed now.");
1006
1007    mPendingAudioAccessUnit.clear();
1008
1009    if (mTimeDiscontinuityPending) {
1010        mRenderer->signalTimeDiscontinuity();
1011        mTimeDiscontinuityPending = false;
1012    }
1013
1014    if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) {
1015        mAudioDecoder->signalResume();
1016    }
1017
1018    if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) {
1019        mVideoDecoder->signalResume();
1020    }
1021
1022    mFlushingAudio = NONE;
1023    mFlushingVideo = NONE;
1024
1025    processDeferredActions();
1026}
1027
1028void NuPlayer::postScanSources() {
1029    if (mScanSourcesPending) {
1030        return;
1031    }
1032
1033    sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1034    msg->setInt32("generation", mScanSourcesGeneration);
1035    msg->post();
1036
1037    mScanSourcesPending = true;
1038}
1039
1040void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) {
1041    ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)",
1042            offloadOnly, mOffloadAudio);
1043    bool audioSinkChanged = false;
1044
1045    int32_t numChannels;
1046    CHECK(format->findInt32("channel-count", &numChannels));
1047
1048    int32_t channelMask;
1049    if (!format->findInt32("channel-mask", &channelMask)) {
1050        // signal to the AudioSink to derive the mask from count.
1051        channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1052    }
1053
1054    int32_t sampleRate;
1055    CHECK(format->findInt32("sample-rate", &sampleRate));
1056
1057    uint32_t flags;
1058    int64_t durationUs;
1059    // FIXME: we should handle the case where the video decoder
1060    // is created after we receive the format change indication.
1061    // Current code will just make that we select deep buffer
1062    // with video which should not be a problem as it should
1063    // not prevent from keeping A/V sync.
1064    if (mVideoDecoder == NULL &&
1065            mSource->getDuration(&durationUs) == OK &&
1066            durationUs
1067                > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
1068        flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1069    } else {
1070        flags = AUDIO_OUTPUT_FLAG_NONE;
1071    }
1072
1073    if (mOffloadAudio) {
1074        audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1075        AString mime;
1076        CHECK(format->findString("mime", &mime));
1077        status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1078
1079        if (err != OK) {
1080            ALOGE("Couldn't map mime \"%s\" to a valid "
1081                    "audio_format", mime.c_str());
1082            mOffloadAudio = false;
1083        } else {
1084            ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1085                    mime.c_str(), audioFormat);
1086
1087            int avgBitRate = -1;
1088            format->findInt32("bit-rate", &avgBitRate);
1089
1090            int32_t aacProfile = -1;
1091            if (audioFormat == AUDIO_FORMAT_AAC
1092                    && format->findInt32("aac-profile", &aacProfile)) {
1093                // Redefine AAC format as per aac profile
1094                mapAACProfileToAudioFormat(
1095                        audioFormat,
1096                        aacProfile);
1097            }
1098
1099            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1100            offloadInfo.duration_us = -1;
1101            format->findInt64(
1102                    "durationUs", &offloadInfo.duration_us);
1103            offloadInfo.sample_rate = sampleRate;
1104            offloadInfo.channel_mask = channelMask;
1105            offloadInfo.format = audioFormat;
1106            offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1107            offloadInfo.bit_rate = avgBitRate;
1108            offloadInfo.has_video = (mVideoDecoder != NULL);
1109            offloadInfo.is_streaming = true;
1110
1111            if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1112                ALOGV("openAudioSink: no change in offload mode");
1113                return;  // no change from previous configuration, everything ok.
1114            }
1115            ALOGV("openAudioSink: try to open AudioSink in offload mode");
1116            flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1117            flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1118            audioSinkChanged = true;
1119            mAudioSink->close();
1120            err = mAudioSink->open(
1121                    sampleRate,
1122                    numChannels,
1123                    (audio_channel_mask_t)channelMask,
1124                    audioFormat,
1125                    8 /* bufferCount */,
1126                    &NuPlayer::Renderer::AudioSinkCallback,
1127                    mRenderer.get(),
1128                    (audio_output_flags_t)flags,
1129                    &offloadInfo);
1130
1131            if (err == OK) {
1132                // If the playback is offloaded to h/w, we pass
1133                // the HAL some metadata information.
1134                // We don't want to do this for PCM because it
1135                // will be going through the AudioFlinger mixer
1136                // before reaching the hardware.
1137                sp<MetaData> audioMeta =
1138                        mSource->getFormatMeta(true /* audio */);
1139                sendMetaDataToHal(mAudioSink, audioMeta);
1140                mCurrentOffloadInfo = offloadInfo;
1141                err = mAudioSink->start();
1142                ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1143            }
1144            if (err != OK) {
1145                // Clean up, fall back to non offload mode.
1146                mAudioSink->close();
1147                mRenderer->signalDisableOffloadAudio();
1148                mOffloadAudio = false;
1149                mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1150                ALOGV("openAudioSink: offload failed");
1151            }
1152        }
1153    }
1154    if (!offloadOnly && !mOffloadAudio) {
1155        flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1156        ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1157
1158        audioSinkChanged = true;
1159        mAudioSink->close();
1160        mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1161        CHECK_EQ(mAudioSink->open(
1162                    sampleRate,
1163                    numChannels,
1164                    (audio_channel_mask_t)channelMask,
1165                    AUDIO_FORMAT_PCM_16_BIT,
1166                    8 /* bufferCount */,
1167                    NULL,
1168                    NULL,
1169                    (audio_output_flags_t)flags),
1170                 (status_t)OK);
1171        mAudioSink->start();
1172    }
1173    if (audioSinkChanged) {
1174        mRenderer->signalAudioSinkChanged();
1175    }
1176}
1177
1178void NuPlayer::closeAudioSink() {
1179    mAudioSink->close();
1180    mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1181}
1182
1183status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
1184    if (*decoder != NULL) {
1185        return OK;
1186    }
1187
1188    sp<AMessage> format = mSource->getFormat(audio);
1189
1190    if (format == NULL) {
1191        return -EWOULDBLOCK;
1192    }
1193
1194    if (!audio) {
1195        AString mime;
1196        CHECK(format->findString("mime", &mime));
1197        mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
1198
1199        sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1200        mCCDecoder = new CCDecoder(ccNotify);
1201
1202        if (mSourceFlags & Source::FLAG_SECURE) {
1203            format->setInt32("secure", true);
1204        }
1205    }
1206
1207    if (audio) {
1208        sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1209        ++mAudioDecoderGeneration;
1210        notify->setInt32("generation", mAudioDecoderGeneration);
1211
1212        if (mOffloadAudio) {
1213            *decoder = new DecoderPassThrough(notify);
1214        } else {
1215            *decoder = new Decoder(notify);
1216        }
1217    } else {
1218        sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1219        ++mVideoDecoderGeneration;
1220        notify->setInt32("generation", mVideoDecoderGeneration);
1221
1222        *decoder = new Decoder(notify, mNativeWindow);
1223    }
1224    (*decoder)->init();
1225    (*decoder)->configure(format);
1226
1227    // allocate buffers to decrypt widevine source buffers
1228    if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1229        Vector<sp<ABuffer> > inputBufs;
1230        CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1231
1232        Vector<MediaBuffer *> mediaBufs;
1233        for (size_t i = 0; i < inputBufs.size(); i++) {
1234            const sp<ABuffer> &buffer = inputBufs[i];
1235            MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1236            mediaBufs.push(mbuf);
1237        }
1238
1239        status_t err = mSource->setBuffers(audio, mediaBufs);
1240        if (err != OK) {
1241            for (size_t i = 0; i < mediaBufs.size(); ++i) {
1242                mediaBufs[i]->release();
1243            }
1244            mediaBufs.clear();
1245            ALOGE("Secure source didn't support secure mediaBufs.");
1246            return err;
1247        }
1248    }
1249    return OK;
1250}
1251
1252status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
1253    sp<AMessage> reply;
1254    CHECK(msg->findMessage("reply", &reply));
1255
1256    if ((audio && mFlushingAudio != NONE)
1257            || (!audio && mFlushingVideo != NONE)
1258            || mSource == NULL) {
1259        reply->setInt32("err", INFO_DISCONTINUITY);
1260        reply->post();
1261        return OK;
1262    }
1263
1264    sp<ABuffer> accessUnit;
1265
1266    // Aggregate smaller buffers into a larger buffer.
1267    // The goal is to reduce power consumption.
1268    // Unfortunately this does not work with the software AAC decoder.
1269    // TODO optimize buffer size for power consumption
1270    // The offload read buffer size is 32 KB but 24 KB uses less power.
1271    const int kAudioBigBufferSizeBytes = 24 * 1024;
1272    bool doBufferAggregation = (audio && mOffloadAudio);
1273    sp<ABuffer> biggerBuffer;
1274    bool needMoreData = false;
1275    int numSmallBuffers = 0;
1276    bool gotTime = false;
1277
1278    bool dropAccessUnit;
1279    do {
1280        status_t err;
1281        // Did we save an accessUnit earlier because of a discontinuity?
1282        if (audio && (mPendingAudioAccessUnit != NULL)) {
1283            accessUnit = mPendingAudioAccessUnit;
1284            mPendingAudioAccessUnit.clear();
1285            err = mPendingAudioErr;
1286            ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit");
1287        } else {
1288            err = mSource->dequeueAccessUnit(audio, &accessUnit);
1289        }
1290
1291        if (err == -EWOULDBLOCK) {
1292            if (biggerBuffer == NULL) {
1293                return err;
1294            } else {
1295                break; // Reply with data that we already have.
1296            }
1297        } else if (err != OK) {
1298            if (err == INFO_DISCONTINUITY) {
1299                if (biggerBuffer != NULL) {
1300                    // We already have some data so save this for later.
1301                    mPendingAudioErr = err;
1302                    mPendingAudioAccessUnit = accessUnit;
1303                    accessUnit.clear();
1304                    ALOGD("feedDecoderInputData() save discontinuity for later");
1305                    break;
1306                }
1307                int32_t type;
1308                CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
1309
1310                bool formatChange =
1311                    (audio &&
1312                     (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
1313                    || (!audio &&
1314                            (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
1315
1316                bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
1317
1318                ALOGI("%s discontinuity (formatChange=%d, time=%d)",
1319                     audio ? "audio" : "video", formatChange, timeChange);
1320
1321                if (audio) {
1322                    mSkipRenderingAudioUntilMediaTimeUs = -1;
1323                } else {
1324                    mSkipRenderingVideoUntilMediaTimeUs = -1;
1325                }
1326
1327                if (timeChange) {
1328                    sp<AMessage> extra;
1329                    if (accessUnit->meta()->findMessage("extra", &extra)
1330                            && extra != NULL) {
1331                        int64_t resumeAtMediaTimeUs;
1332                        if (extra->findInt64(
1333                                    "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
1334                            ALOGI("suppressing rendering of %s until %lld us",
1335                                    audio ? "audio" : "video", resumeAtMediaTimeUs);
1336
1337                            if (audio) {
1338                                mSkipRenderingAudioUntilMediaTimeUs =
1339                                    resumeAtMediaTimeUs;
1340                            } else {
1341                                mSkipRenderingVideoUntilMediaTimeUs =
1342                                    resumeAtMediaTimeUs;
1343                            }
1344                        }
1345                    }
1346                }
1347
1348                mTimeDiscontinuityPending =
1349                    mTimeDiscontinuityPending || timeChange;
1350
1351                bool seamlessFormatChange = false;
1352                sp<AMessage> newFormat = mSource->getFormat(audio);
1353                if (formatChange) {
1354                    seamlessFormatChange =
1355                        getDecoder(audio)->supportsSeamlessFormatChange(newFormat);
1356                    // treat seamless format change separately
1357                    formatChange = !seamlessFormatChange;
1358                }
1359                bool shutdownOrFlush = formatChange || timeChange;
1360
1361                // We want to queue up scan-sources only once per discontinuity.
1362                // We control this by doing it only if neither audio nor video are
1363                // flushing or shutting down.  (After handling 1st discontinuity, one
1364                // of the flushing states will not be NONE.)
1365                // No need to scan sources if this discontinuity does not result
1366                // in a flush or shutdown, as the flushing state will stay NONE.
1367                if (mFlushingAudio == NONE && mFlushingVideo == NONE &&
1368                        shutdownOrFlush) {
1369                    // And we'll resume scanning sources once we're done
1370                    // flushing.
1371                    mDeferredActions.push_front(
1372                            new SimpleAction(
1373                                &NuPlayer::performScanSources));
1374                }
1375
1376                if (formatChange /* not seamless */) {
1377                    // must change decoder
1378                    flushDecoder(audio, /* needShutdown = */ true);
1379                } else if (timeChange) {
1380                    // need to flush
1381                    flushDecoder(audio, /* needShutdown = */ false, newFormat);
1382                    err = OK;
1383                } else if (seamlessFormatChange) {
1384                    // reuse existing decoder and don't flush
1385                    updateDecoderFormatWithoutFlush(audio, newFormat);
1386                    err = OK;
1387                } else {
1388                    // This stream is unaffected by the discontinuity
1389                    return -EWOULDBLOCK;
1390                }
1391            }
1392
1393            reply->setInt32("err", err);
1394            reply->post();
1395            return OK;
1396        }
1397
1398        if (!audio) {
1399            ++mNumFramesTotal;
1400        }
1401
1402        dropAccessUnit = false;
1403        if (!audio
1404                && !(mSourceFlags & Source::FLAG_SECURE)
1405                && mVideoLateByUs > 100000ll
1406                && mVideoIsAVC
1407                && !IsAVCReferenceFrame(accessUnit)) {
1408            dropAccessUnit = true;
1409            ++mNumFramesDropped;
1410        }
1411
1412        size_t smallSize = accessUnit->size();
1413        needMoreData = false;
1414        if (doBufferAggregation && (biggerBuffer == NULL)
1415                // Don't bother if only room for a few small buffers.
1416                && (smallSize < (kAudioBigBufferSizeBytes / 3))) {
1417            // Create a larger buffer for combining smaller buffers from the extractor.
1418            biggerBuffer = new ABuffer(kAudioBigBufferSizeBytes);
1419            biggerBuffer->setRange(0, 0); // start empty
1420        }
1421
1422        if (biggerBuffer != NULL) {
1423            int64_t timeUs;
1424            bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs);
1425            // Will the smaller buffer fit?
1426            size_t bigSize = biggerBuffer->size();
1427            size_t roomLeft = biggerBuffer->capacity() - bigSize;
1428            // Should we save this small buffer for the next big buffer?
1429            // If the first small buffer did not have a timestamp then save
1430            // any buffer that does have a timestamp until the next big buffer.
1431            if ((smallSize > roomLeft)
1432                || (!gotTime && (numSmallBuffers > 0) && smallTimestampValid)) {
1433                mPendingAudioErr = err;
1434                mPendingAudioAccessUnit = accessUnit;
1435                accessUnit.clear();
1436            } else {
1437                // Append small buffer to the bigger buffer.
1438                memcpy(biggerBuffer->base() + bigSize, accessUnit->data(), smallSize);
1439                bigSize += smallSize;
1440                biggerBuffer->setRange(0, bigSize);
1441
1442                // Keep looping until we run out of room in the biggerBuffer.
1443                needMoreData = true;
1444
1445                // Grab time from first small buffer if available.
1446                if ((numSmallBuffers == 0) && smallTimestampValid) {
1447                    biggerBuffer->meta()->setInt64("timeUs", timeUs);
1448                    gotTime = true;
1449                }
1450
1451                ALOGV("feedDecoderInputData() #%d, smallSize = %zu, bigSize = %zu, capacity = %zu",
1452                        numSmallBuffers, smallSize, bigSize, biggerBuffer->capacity());
1453                numSmallBuffers++;
1454            }
1455        }
1456    } while (dropAccessUnit || needMoreData);
1457
1458    // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
1459
1460#if 0
1461    int64_t mediaTimeUs;
1462    CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
1463    ALOGV("feeding %s input buffer at media time %.2f secs",
1464         audio ? "audio" : "video",
1465         mediaTimeUs / 1E6);
1466#endif
1467
1468    if (!audio) {
1469        mCCDecoder->decode(accessUnit);
1470    }
1471
1472    if (biggerBuffer != NULL) {
1473        ALOGV("feedDecoderInputData() reply with aggregated buffer, %d", numSmallBuffers);
1474        reply->setBuffer("buffer", biggerBuffer);
1475    } else {
1476        reply->setBuffer("buffer", accessUnit);
1477    }
1478
1479    reply->post();
1480
1481    return OK;
1482}
1483
1484void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
1485    // ALOGV("renderBuffer %s", audio ? "audio" : "video");
1486
1487    sp<AMessage> reply;
1488    CHECK(msg->findMessage("reply", &reply));
1489
1490    if ((audio && mFlushingAudio != NONE)
1491            || (!audio && mFlushingVideo != NONE)) {
1492        // We're currently attempting to flush the decoder, in order
1493        // to complete this, the decoder wants all its buffers back,
1494        // so we don't want any output buffers it sent us (from before
1495        // we initiated the flush) to be stuck in the renderer's queue.
1496
1497        ALOGV("we're still flushing the %s decoder, sending its output buffer"
1498             " right back.", audio ? "audio" : "video");
1499
1500        reply->post();
1501        return;
1502    }
1503
1504    sp<ABuffer> buffer;
1505    CHECK(msg->findBuffer("buffer", &buffer));
1506
1507    int64_t mediaTimeUs;
1508    CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1509
1510    int64_t &skipUntilMediaTimeUs =
1511        audio
1512            ? mSkipRenderingAudioUntilMediaTimeUs
1513            : mSkipRenderingVideoUntilMediaTimeUs;
1514
1515    if (skipUntilMediaTimeUs >= 0) {
1516
1517        if (mediaTimeUs < skipUntilMediaTimeUs) {
1518            ALOGV("dropping %s buffer at time %lld as requested.",
1519                 audio ? "audio" : "video",
1520                 mediaTimeUs);
1521
1522            reply->post();
1523            return;
1524        }
1525
1526        skipUntilMediaTimeUs = -1;
1527    }
1528
1529    if (!audio && mCCDecoder->isSelected()) {
1530        mCCDecoder->display(mediaTimeUs);
1531    }
1532
1533    mRenderer->queueBuffer(audio, buffer, reply);
1534}
1535
1536void NuPlayer::updateVideoSize(
1537        const sp<AMessage> &inputFormat,
1538        const sp<AMessage> &outputFormat) {
1539    if (inputFormat == NULL) {
1540        ALOGW("Unknown video size, reporting 0x0!");
1541        notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1542        return;
1543    }
1544
1545    int32_t displayWidth, displayHeight;
1546    int32_t cropLeft, cropTop, cropRight, cropBottom;
1547
1548    if (outputFormat != NULL) {
1549        int32_t width, height;
1550        CHECK(outputFormat->findInt32("width", &width));
1551        CHECK(outputFormat->findInt32("height", &height));
1552
1553        int32_t cropLeft, cropTop, cropRight, cropBottom;
1554        CHECK(outputFormat->findRect(
1555                    "crop",
1556                    &cropLeft, &cropTop, &cropRight, &cropBottom));
1557
1558        displayWidth = cropRight - cropLeft + 1;
1559        displayHeight = cropBottom - cropTop + 1;
1560
1561        ALOGV("Video output format changed to %d x %d "
1562             "(crop: %d x %d @ (%d, %d))",
1563             width, height,
1564             displayWidth,
1565             displayHeight,
1566             cropLeft, cropTop);
1567    } else {
1568        CHECK(inputFormat->findInt32("width", &displayWidth));
1569        CHECK(inputFormat->findInt32("height", &displayHeight));
1570
1571        ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1572    }
1573
1574    // Take into account sample aspect ratio if necessary:
1575    int32_t sarWidth, sarHeight;
1576    if (inputFormat->findInt32("sar-width", &sarWidth)
1577            && inputFormat->findInt32("sar-height", &sarHeight)) {
1578        ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1579
1580        displayWidth = (displayWidth * sarWidth) / sarHeight;
1581
1582        ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1583    }
1584
1585    int32_t rotationDegrees;
1586    if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1587        rotationDegrees = 0;
1588    }
1589
1590    if (rotationDegrees == 90 || rotationDegrees == 270) {
1591        int32_t tmp = displayWidth;
1592        displayWidth = displayHeight;
1593        displayHeight = tmp;
1594    }
1595
1596    notifyListener(
1597            MEDIA_SET_VIDEO_SIZE,
1598            displayWidth,
1599            displayHeight);
1600}
1601
1602void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
1603    if (mDriver == NULL) {
1604        return;
1605    }
1606
1607    sp<NuPlayerDriver> driver = mDriver.promote();
1608
1609    if (driver == NULL) {
1610        return;
1611    }
1612
1613    driver->notifyListener(msg, ext1, ext2, in);
1614}
1615
1616void NuPlayer::flushDecoder(
1617        bool audio, bool needShutdown, const sp<AMessage> &newFormat) {
1618    ALOGV("[%s] flushDecoder needShutdown=%d",
1619          audio ? "audio" : "video", needShutdown);
1620
1621    const sp<Decoder> &decoder = getDecoder(audio);
1622    if (decoder == NULL) {
1623        ALOGI("flushDecoder %s without decoder present",
1624             audio ? "audio" : "video");
1625        return;
1626    }
1627
1628    // Make sure we don't continue to scan sources until we finish flushing.
1629    ++mScanSourcesGeneration;
1630    mScanSourcesPending = false;
1631
1632    decoder->signalFlush(newFormat);
1633    mRenderer->flush(audio);
1634
1635    FlushStatus newStatus =
1636        needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1637
1638    if (audio) {
1639        ALOGE_IF(mFlushingAudio != NONE,
1640                "audio flushDecoder() is called in state %d", mFlushingAudio);
1641        mFlushingAudio = newStatus;
1642    } else {
1643        ALOGE_IF(mFlushingVideo != NONE,
1644                "video flushDecoder() is called in state %d", mFlushingVideo);
1645        mFlushingVideo = newStatus;
1646
1647        if (mCCDecoder != NULL) {
1648            mCCDecoder->flush();
1649        }
1650    }
1651}
1652
1653void NuPlayer::updateDecoderFormatWithoutFlush(
1654        bool audio, const sp<AMessage> &format) {
1655    ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video");
1656
1657    const sp<Decoder> &decoder = getDecoder(audio);
1658    if (decoder == NULL) {
1659        ALOGI("updateDecoderFormatWithoutFlush %s without decoder present",
1660             audio ? "audio" : "video");
1661        return;
1662    }
1663
1664    decoder->signalUpdateFormat(format);
1665}
1666
1667void NuPlayer::queueDecoderShutdown(
1668        bool audio, bool video, const sp<AMessage> &reply) {
1669    ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1670
1671    mDeferredActions.push_back(
1672            new ShutdownDecoderAction(audio, video));
1673
1674    mDeferredActions.push_back(
1675            new SimpleAction(&NuPlayer::performScanSources));
1676
1677    mDeferredActions.push_back(new PostMessageAction(reply));
1678
1679    processDeferredActions();
1680}
1681
1682status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1683    mVideoScalingMode = mode;
1684    if (mNativeWindow != NULL) {
1685        status_t ret = native_window_set_scaling_mode(
1686                mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1687        if (ret != OK) {
1688            ALOGE("Failed to set scaling mode (%d): %s",
1689                -ret, strerror(-ret));
1690            return ret;
1691        }
1692    }
1693    return OK;
1694}
1695
1696status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1697    sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1698    msg->setPointer("reply", reply);
1699
1700    sp<AMessage> response;
1701    status_t err = msg->postAndAwaitResponse(&response);
1702    return err;
1703}
1704
1705status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1706    sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1707    msg->setPointer("reply", reply);
1708    msg->setInt32("type", type);
1709
1710    sp<AMessage> response;
1711    status_t err = msg->postAndAwaitResponse(&response);
1712    if (err == OK && response != NULL) {
1713        CHECK(response->findInt32("err", &err));
1714    }
1715    return err;
1716}
1717
1718status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1719    sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1720    msg->setSize("trackIndex", trackIndex);
1721    msg->setInt32("select", select);
1722
1723    sp<AMessage> response;
1724    status_t err = msg->postAndAwaitResponse(&response);
1725
1726    if (err != OK) {
1727        return err;
1728    }
1729
1730    if (!response->findInt32("err", &err)) {
1731        err = OK;
1732    }
1733
1734    return err;
1735}
1736
1737void NuPlayer::schedulePollDuration() {
1738    sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1739    msg->setInt32("generation", mPollDurationGeneration);
1740    msg->post();
1741}
1742
1743void NuPlayer::cancelPollDuration() {
1744    ++mPollDurationGeneration;
1745}
1746
1747void NuPlayer::processDeferredActions() {
1748    while (!mDeferredActions.empty()) {
1749        // We won't execute any deferred actions until we're no longer in
1750        // an intermediate state, i.e. one more more decoders are currently
1751        // flushing or shutting down.
1752
1753        if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1754            // We're currently flushing, postpone the reset until that's
1755            // completed.
1756
1757            ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1758                  mFlushingAudio, mFlushingVideo);
1759
1760            break;
1761        }
1762
1763        sp<Action> action = *mDeferredActions.begin();
1764        mDeferredActions.erase(mDeferredActions.begin());
1765
1766        action->execute(this);
1767    }
1768}
1769
1770void NuPlayer::performSeek(int64_t seekTimeUs) {
1771    ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1772          seekTimeUs,
1773          seekTimeUs / 1E6);
1774
1775    if (mSource == NULL) {
1776        // This happens when reset occurs right before the loop mode
1777        // asynchronously seeks to the start of the stream.
1778        LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1779                "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1780                mAudioDecoder.get(), mVideoDecoder.get());
1781        return;
1782    }
1783    mSource->seekTo(seekTimeUs);
1784    ++mTimedTextGeneration;
1785
1786    if (mDriver != NULL) {
1787        sp<NuPlayerDriver> driver = mDriver.promote();
1788        if (driver != NULL) {
1789            driver->notifyPosition(seekTimeUs);
1790            driver->notifySeekComplete();
1791        }
1792    }
1793
1794    // everything's flushed, continue playback.
1795}
1796
1797void NuPlayer::performDecoderFlush() {
1798    ALOGV("performDecoderFlush");
1799
1800    if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1801        return;
1802    }
1803
1804    mTimeDiscontinuityPending = true;
1805
1806    if (mAudioDecoder != NULL) {
1807        flushDecoder(true /* audio */, false /* needShutdown */);
1808    }
1809
1810    if (mVideoDecoder != NULL) {
1811        flushDecoder(false /* audio */, false /* needShutdown */);
1812    }
1813}
1814
1815void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1816    ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
1817
1818    if ((!audio || mAudioDecoder == NULL)
1819            && (!video || mVideoDecoder == NULL)) {
1820        return;
1821    }
1822
1823    mTimeDiscontinuityPending = true;
1824
1825    if (audio && mAudioDecoder != NULL) {
1826        flushDecoder(true /* audio */, true /* needShutdown */);
1827    }
1828
1829    if (video && mVideoDecoder != NULL) {
1830        flushDecoder(false /* audio */, true /* needShutdown */);
1831    }
1832}
1833
1834void NuPlayer::performReset() {
1835    ALOGV("performReset");
1836
1837    CHECK(mAudioDecoder == NULL);
1838    CHECK(mVideoDecoder == NULL);
1839
1840    cancelPollDuration();
1841
1842    ++mScanSourcesGeneration;
1843    mScanSourcesPending = false;
1844
1845    ++mAudioDecoderGeneration;
1846    ++mVideoDecoderGeneration;
1847
1848    if (mRendererLooper != NULL) {
1849        if (mRenderer != NULL) {
1850            mRendererLooper->unregisterHandler(mRenderer->id());
1851        }
1852        mRendererLooper->stop();
1853        mRendererLooper.clear();
1854    }
1855    mRenderer.clear();
1856
1857    if (mSource != NULL) {
1858        mSource->stop();
1859
1860        mSource.clear();
1861    }
1862
1863    if (mDriver != NULL) {
1864        sp<NuPlayerDriver> driver = mDriver.promote();
1865        if (driver != NULL) {
1866            driver->notifyResetComplete();
1867        }
1868    }
1869
1870    mStarted = false;
1871}
1872
1873void NuPlayer::performScanSources() {
1874    ALOGV("performScanSources");
1875
1876    if (!mStarted) {
1877        return;
1878    }
1879
1880    if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1881        postScanSources();
1882    }
1883}
1884
1885void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1886    ALOGV("performSetSurface");
1887
1888    mNativeWindow = wrapper;
1889
1890    // XXX - ignore error from setVideoScalingMode for now
1891    setVideoScalingMode(mVideoScalingMode);
1892
1893    if (mDriver != NULL) {
1894        sp<NuPlayerDriver> driver = mDriver.promote();
1895        if (driver != NULL) {
1896            driver->notifySetSurfaceComplete();
1897        }
1898    }
1899}
1900
1901void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1902    int32_t what;
1903    CHECK(msg->findInt32("what", &what));
1904
1905    switch (what) {
1906        case Source::kWhatPrepared:
1907        {
1908            if (mSource == NULL) {
1909                // This is a stale notification from a source that was
1910                // asynchronously preparing when the client called reset().
1911                // We handled the reset, the source is gone.
1912                break;
1913            }
1914
1915            int32_t err;
1916            CHECK(msg->findInt32("err", &err));
1917
1918            sp<NuPlayerDriver> driver = mDriver.promote();
1919            if (driver != NULL) {
1920                // notify duration first, so that it's definitely set when
1921                // the app received the "prepare complete" callback.
1922                int64_t durationUs;
1923                if (mSource->getDuration(&durationUs) == OK) {
1924                    driver->notifyDuration(durationUs);
1925                }
1926                driver->notifyPrepareCompleted(err);
1927            }
1928
1929            break;
1930        }
1931
1932        case Source::kWhatFlagsChanged:
1933        {
1934            uint32_t flags;
1935            CHECK(msg->findInt32("flags", (int32_t *)&flags));
1936
1937            sp<NuPlayerDriver> driver = mDriver.promote();
1938            if (driver != NULL) {
1939                driver->notifyFlagsChanged(flags);
1940            }
1941
1942            if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1943                    && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1944                cancelPollDuration();
1945            } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1946                    && (flags & Source::FLAG_DYNAMIC_DURATION)
1947                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1948                schedulePollDuration();
1949            }
1950
1951            mSourceFlags = flags;
1952            break;
1953        }
1954
1955        case Source::kWhatVideoSizeChanged:
1956        {
1957            sp<AMessage> format;
1958            CHECK(msg->findMessage("format", &format));
1959
1960            updateVideoSize(format);
1961            break;
1962        }
1963
1964        case Source::kWhatBufferingUpdate:
1965        {
1966            int32_t percentage;
1967            CHECK(msg->findInt32("percentage", &percentage));
1968
1969            notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1970            break;
1971        }
1972
1973        case Source::kWhatBufferingStart:
1974        {
1975            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1976            break;
1977        }
1978
1979        case Source::kWhatBufferingEnd:
1980        {
1981            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1982            break;
1983        }
1984
1985        case Source::kWhatSubtitleData:
1986        {
1987            sp<ABuffer> buffer;
1988            CHECK(msg->findBuffer("buffer", &buffer));
1989
1990            sendSubtitleData(buffer, 0 /* baseIndex */);
1991            break;
1992        }
1993
1994        case Source::kWhatTimedTextData:
1995        {
1996            int32_t generation;
1997            if (msg->findInt32("generation", &generation)
1998                    && generation != mTimedTextGeneration) {
1999                break;
2000            }
2001
2002            sp<ABuffer> buffer;
2003            CHECK(msg->findBuffer("buffer", &buffer));
2004
2005            sp<NuPlayerDriver> driver = mDriver.promote();
2006            if (driver == NULL) {
2007                break;
2008            }
2009
2010            int posMs;
2011            int64_t timeUs, posUs;
2012            driver->getCurrentPosition(&posMs);
2013            posUs = posMs * 1000;
2014            CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2015
2016            if (posUs < timeUs) {
2017                if (!msg->findInt32("generation", &generation)) {
2018                    msg->setInt32("generation", mTimedTextGeneration);
2019                }
2020                msg->post(timeUs - posUs);
2021            } else {
2022                sendTimedTextData(buffer);
2023            }
2024            break;
2025        }
2026
2027        case Source::kWhatQueueDecoderShutdown:
2028        {
2029            int32_t audio, video;
2030            CHECK(msg->findInt32("audio", &audio));
2031            CHECK(msg->findInt32("video", &video));
2032
2033            sp<AMessage> reply;
2034            CHECK(msg->findMessage("reply", &reply));
2035
2036            queueDecoderShutdown(audio, video, reply);
2037            break;
2038        }
2039
2040        case Source::kWhatDrmNoLicense:
2041        {
2042            notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2043            break;
2044        }
2045
2046        default:
2047            TRESPASS();
2048    }
2049}
2050
2051void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2052    int32_t what;
2053    CHECK(msg->findInt32("what", &what));
2054
2055    switch (what) {
2056        case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2057        {
2058            sp<ABuffer> buffer;
2059            CHECK(msg->findBuffer("buffer", &buffer));
2060
2061            size_t inbandTracks = 0;
2062            if (mSource != NULL) {
2063                inbandTracks = mSource->getTrackCount();
2064            }
2065
2066            sendSubtitleData(buffer, inbandTracks);
2067            break;
2068        }
2069
2070        case NuPlayer::CCDecoder::kWhatTrackAdded:
2071        {
2072            notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2073
2074            break;
2075        }
2076
2077        default:
2078            TRESPASS();
2079    }
2080
2081
2082}
2083
2084void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2085    int32_t trackIndex;
2086    int64_t timeUs, durationUs;
2087    CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2088    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2089    CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2090
2091    Parcel in;
2092    in.writeInt32(trackIndex + baseIndex);
2093    in.writeInt64(timeUs);
2094    in.writeInt64(durationUs);
2095    in.writeInt32(buffer->size());
2096    in.writeInt32(buffer->size());
2097    in.write(buffer->data(), buffer->size());
2098
2099    notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2100}
2101
2102void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2103    const void *data;
2104    size_t size = 0;
2105    int64_t timeUs;
2106    int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
2107
2108    AString mime;
2109    CHECK(buffer->meta()->findString("mime", &mime));
2110    CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2111
2112    data = buffer->data();
2113    size = buffer->size();
2114
2115    Parcel parcel;
2116    if (size > 0) {
2117        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2118        flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
2119        TextDescriptions::getParcelOfDescriptions(
2120                (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2121    }
2122
2123    if ((parcel.dataSize() > 0)) {
2124        notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2125    } else {  // send an empty timed text
2126        notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2127    }
2128}
2129////////////////////////////////////////////////////////////////////////////////
2130
2131sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2132    sp<MetaData> meta = getFormatMeta(audio);
2133
2134    if (meta == NULL) {
2135        return NULL;
2136    }
2137
2138    sp<AMessage> msg = new AMessage;
2139
2140    if(convertMetaDataToMessage(meta, &msg) == OK) {
2141        return msg;
2142    }
2143    return NULL;
2144}
2145
2146void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2147    sp<AMessage> notify = dupNotify();
2148    notify->setInt32("what", kWhatFlagsChanged);
2149    notify->setInt32("flags", flags);
2150    notify->post();
2151}
2152
2153void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
2154    sp<AMessage> notify = dupNotify();
2155    notify->setInt32("what", kWhatVideoSizeChanged);
2156    notify->setMessage("format", format);
2157    notify->post();
2158}
2159
2160void NuPlayer::Source::notifyPrepared(status_t err) {
2161    sp<AMessage> notify = dupNotify();
2162    notify->setInt32("what", kWhatPrepared);
2163    notify->setInt32("err", err);
2164    notify->post();
2165}
2166
2167void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
2168    TRESPASS();
2169}
2170
2171}  // namespace android
2172