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