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