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