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