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