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