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