NuPlayer.cpp revision fe8fe7dc0316289e7f03690bcbe2332b395ba331
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    mPaused = false;
1327
1328    uint32_t flags = 0;
1329
1330    if (mSource->isRealTime()) {
1331        flags |= Renderer::FLAG_REAL_TIME;
1332    }
1333
1334    sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1335    ALOGV_IF(audioMeta == NULL, "no metadata for audio source");  // video only stream
1336    audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1337    if (mAudioSink != NULL) {
1338        streamType = mAudioSink->getAudioStreamType();
1339    }
1340
1341    sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1342
1343    mOffloadAudio =
1344        canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType)
1345                && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
1346    if (mOffloadAudio) {
1347        flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1348    }
1349
1350    sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
1351    ++mRendererGeneration;
1352    notify->setInt32("generation", mRendererGeneration);
1353    mRenderer = new Renderer(mAudioSink, notify, flags);
1354    mRendererLooper = new ALooper;
1355    mRendererLooper->setName("NuPlayerRenderer");
1356    mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1357    mRendererLooper->registerHandler(mRenderer);
1358
1359    status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1360    if (err != OK) {
1361        mSource->stop();
1362        mSourceStarted = false;
1363        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1364        return;
1365    }
1366
1367    float rate = getFrameRate();
1368    if (rate > 0) {
1369        mRenderer->setVideoFrameRate(rate);
1370    }
1371
1372    if (mVideoDecoder != NULL) {
1373        mVideoDecoder->setRenderer(mRenderer);
1374    }
1375    if (mAudioDecoder != NULL) {
1376        mAudioDecoder->setRenderer(mRenderer);
1377    }
1378
1379    postScanSources();
1380}
1381
1382void NuPlayer::onPause() {
1383    if (mPaused) {
1384        return;
1385    }
1386    mPaused = true;
1387    if (mSource != NULL) {
1388        mSource->pause();
1389    } else {
1390        ALOGW("pause called when source is gone or not set");
1391    }
1392    if (mRenderer != NULL) {
1393        mRenderer->pause();
1394    } else {
1395        ALOGW("pause called when renderer is gone or not set");
1396    }
1397}
1398
1399bool NuPlayer::audioDecoderStillNeeded() {
1400    // Audio decoder is no longer needed if it's in shut/shutting down status.
1401    return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1402}
1403
1404void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1405    // We wait for both the decoder flush and the renderer flush to complete
1406    // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1407
1408    mFlushComplete[audio][isDecoder] = true;
1409    if (!mFlushComplete[audio][!isDecoder]) {
1410        return;
1411    }
1412
1413    FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1414    switch (*state) {
1415        case FLUSHING_DECODER:
1416        {
1417            *state = FLUSHED;
1418            break;
1419        }
1420
1421        case FLUSHING_DECODER_SHUTDOWN:
1422        {
1423            *state = SHUTTING_DOWN_DECODER;
1424
1425            ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1426            if (!audio) {
1427                // Widevine source reads must stop before releasing the video decoder.
1428                if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1429                    mSource->stop();
1430                    mSourceStarted = false;
1431                }
1432            }
1433            getDecoder(audio)->initiateShutdown();
1434            break;
1435        }
1436
1437        default:
1438            // decoder flush completes only occur in a flushing state.
1439            LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1440            break;
1441    }
1442}
1443
1444void NuPlayer::finishFlushIfPossible() {
1445    if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1446            && mFlushingAudio != SHUT_DOWN) {
1447        return;
1448    }
1449
1450    if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1451            && mFlushingVideo != SHUT_DOWN) {
1452        return;
1453    }
1454
1455    ALOGV("both audio and video are flushed now.");
1456
1457    mFlushingAudio = NONE;
1458    mFlushingVideo = NONE;
1459
1460    clearFlushComplete();
1461
1462    processDeferredActions();
1463}
1464
1465void NuPlayer::postScanSources() {
1466    if (mScanSourcesPending) {
1467        return;
1468    }
1469
1470    sp<AMessage> msg = new AMessage(kWhatScanSources, this);
1471    msg->setInt32("generation", mScanSourcesGeneration);
1472    msg->post();
1473
1474    mScanSourcesPending = true;
1475}
1476
1477void NuPlayer::tryOpenAudioSinkForOffload(
1478        const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) {
1479    // Note: This is called early in NuPlayer to determine whether offloading
1480    // is possible; otherwise the decoders call the renderer openAudioSink directly.
1481
1482    status_t err = mRenderer->openAudioSink(
1483            format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1484    if (err != OK) {
1485        // Any failure we turn off mOffloadAudio.
1486        mOffloadAudio = false;
1487    } else if (mOffloadAudio) {
1488        sendMetaDataToHal(mAudioSink, audioMeta);
1489    }
1490}
1491
1492void NuPlayer::closeAudioSink() {
1493    mRenderer->closeAudioSink();
1494}
1495
1496void NuPlayer::restartAudio(
1497        int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) {
1498    if (mAudioDecoder != NULL) {
1499        mAudioDecoder->pause();
1500        mAudioDecoder.clear();
1501        ++mAudioDecoderGeneration;
1502    }
1503    if (mFlushingAudio == FLUSHING_DECODER) {
1504        mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1505        mFlushingAudio = FLUSHED;
1506        finishFlushIfPossible();
1507    } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1508            || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1509        mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1510        mFlushingAudio = SHUT_DOWN;
1511        finishFlushIfPossible();
1512        needsToCreateAudioDecoder = false;
1513    }
1514    if (mRenderer == NULL) {
1515        return;
1516    }
1517    closeAudioSink();
1518    mRenderer->flush(true /* audio */, false /* notifyComplete */);
1519    if (mVideoDecoder != NULL) {
1520        mRenderer->flush(false /* audio */, false /* notifyComplete */);
1521    }
1522
1523    performSeek(currentPositionUs);
1524
1525    if (forceNonOffload) {
1526        mRenderer->signalDisableOffloadAudio();
1527        mOffloadAudio = false;
1528    }
1529    if (needsToCreateAudioDecoder) {
1530        instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload);
1531    }
1532}
1533
1534void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) {
1535    if (mSource == NULL || mAudioSink == NULL) {
1536        return;
1537    }
1538
1539    if (mRenderer == NULL) {
1540        ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1541        mOffloadAudio = false;
1542        return;
1543    }
1544
1545    sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1546    sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1547    audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1548    const bool hasVideo = (videoFormat != NULL);
1549    const bool canOffload = canOffloadStream(
1550            audioMeta, hasVideo, mSource->isStreaming(), streamType)
1551                    && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f);
1552    if (canOffload) {
1553        if (!mOffloadAudio) {
1554            mRenderer->signalEnableOffloadAudio();
1555        }
1556        // open audio sink early under offload mode.
1557        tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo);
1558    } else {
1559        if (mOffloadAudio) {
1560            mRenderer->signalDisableOffloadAudio();
1561            mOffloadAudio = false;
1562        }
1563    }
1564}
1565
1566status_t NuPlayer::instantiateDecoder(
1567        bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) {
1568    // The audio decoder could be cleared by tear down. If still in shut down
1569    // process, no need to create a new audio decoder.
1570    if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
1571        return OK;
1572    }
1573
1574    sp<AMessage> format = mSource->getFormat(audio);
1575
1576    if (format == NULL) {
1577        return UNKNOWN_ERROR;
1578    } else {
1579        status_t err;
1580        if (format->findInt32("err", &err) && err) {
1581            return err;
1582        }
1583    }
1584
1585    format->setInt32("priority", 0 /* realtime */);
1586
1587    if (!audio) {
1588        AString mime;
1589        CHECK(format->findString("mime", &mime));
1590
1591        sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
1592        if (mCCDecoder == NULL) {
1593            mCCDecoder = new CCDecoder(ccNotify);
1594        }
1595
1596        if (mSourceFlags & Source::FLAG_SECURE) {
1597            format->setInt32("secure", true);
1598        }
1599
1600        if (mSourceFlags & Source::FLAG_PROTECTED) {
1601            format->setInt32("protected", true);
1602        }
1603
1604        float rate = getFrameRate();
1605        if (rate > 0) {
1606            format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
1607        }
1608    }
1609
1610    if (audio) {
1611        sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
1612        ++mAudioDecoderGeneration;
1613        notify->setInt32("generation", mAudioDecoderGeneration);
1614
1615        if (checkAudioModeChange) {
1616            determineAudioModeChange(format);
1617        }
1618        if (mOffloadAudio) {
1619            mSource->setOffloadAudio(true /* offload */);
1620
1621            const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1622            format->setInt32("has-video", hasVideo);
1623            *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
1624        } else {
1625            mSource->setOffloadAudio(false /* offload */);
1626
1627            *decoder = new Decoder(notify, mSource, mPID, mRenderer);
1628        }
1629    } else {
1630        sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
1631        ++mVideoDecoderGeneration;
1632        notify->setInt32("generation", mVideoDecoderGeneration);
1633
1634        *decoder = new Decoder(
1635                notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
1636
1637        // enable FRC if high-quality AV sync is requested, even if not
1638        // directly queuing to display, as this will even improve textureview
1639        // playback.
1640        {
1641            char value[PROPERTY_VALUE_MAX];
1642            if (property_get("persist.sys.media.avsync", value, NULL) &&
1643                    (!strcmp("1", value) || !strcasecmp("true", value))) {
1644                format->setInt32("auto-frc", 1);
1645            }
1646        }
1647    }
1648    (*decoder)->init();
1649    (*decoder)->configure(format);
1650
1651    // allocate buffers to decrypt widevine source buffers
1652    if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1653        Vector<sp<ABuffer> > inputBufs;
1654        CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1655
1656        Vector<MediaBuffer *> mediaBufs;
1657        for (size_t i = 0; i < inputBufs.size(); i++) {
1658            const sp<ABuffer> &buffer = inputBufs[i];
1659            MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1660            mediaBufs.push(mbuf);
1661        }
1662
1663        status_t err = mSource->setBuffers(audio, mediaBufs);
1664        if (err != OK) {
1665            for (size_t i = 0; i < mediaBufs.size(); ++i) {
1666                mediaBufs[i]->release();
1667            }
1668            mediaBufs.clear();
1669            ALOGE("Secure source didn't support secure mediaBufs.");
1670            return err;
1671        }
1672    }
1673    return OK;
1674}
1675
1676void NuPlayer::updateVideoSize(
1677        const sp<AMessage> &inputFormat,
1678        const sp<AMessage> &outputFormat) {
1679    if (inputFormat == NULL) {
1680        ALOGW("Unknown video size, reporting 0x0!");
1681        notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1682        return;
1683    }
1684
1685    int32_t displayWidth, displayHeight;
1686    if (outputFormat != NULL) {
1687        int32_t width, height;
1688        CHECK(outputFormat->findInt32("width", &width));
1689        CHECK(outputFormat->findInt32("height", &height));
1690
1691        int32_t cropLeft, cropTop, cropRight, cropBottom;
1692        CHECK(outputFormat->findRect(
1693                    "crop",
1694                    &cropLeft, &cropTop, &cropRight, &cropBottom));
1695
1696        displayWidth = cropRight - cropLeft + 1;
1697        displayHeight = cropBottom - cropTop + 1;
1698
1699        ALOGV("Video output format changed to %d x %d "
1700             "(crop: %d x %d @ (%d, %d))",
1701             width, height,
1702             displayWidth,
1703             displayHeight,
1704             cropLeft, cropTop);
1705    } else {
1706        CHECK(inputFormat->findInt32("width", &displayWidth));
1707        CHECK(inputFormat->findInt32("height", &displayHeight));
1708
1709        ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1710    }
1711
1712    // Take into account sample aspect ratio if necessary:
1713    int32_t sarWidth, sarHeight;
1714    if (inputFormat->findInt32("sar-width", &sarWidth)
1715            && inputFormat->findInt32("sar-height", &sarHeight)) {
1716        ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1717
1718        displayWidth = (displayWidth * sarWidth) / sarHeight;
1719
1720        ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1721    }
1722
1723    int32_t rotationDegrees;
1724    if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1725        rotationDegrees = 0;
1726    }
1727
1728    if (rotationDegrees == 90 || rotationDegrees == 270) {
1729        int32_t tmp = displayWidth;
1730        displayWidth = displayHeight;
1731        displayHeight = tmp;
1732    }
1733
1734    notifyListener(
1735            MEDIA_SET_VIDEO_SIZE,
1736            displayWidth,
1737            displayHeight);
1738}
1739
1740void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
1741    if (mDriver == NULL) {
1742        return;
1743    }
1744
1745    sp<NuPlayerDriver> driver = mDriver.promote();
1746
1747    if (driver == NULL) {
1748        return;
1749    }
1750
1751    driver->notifyListener(msg, ext1, ext2, in);
1752}
1753
1754void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
1755    ALOGV("[%s] flushDecoder needShutdown=%d",
1756          audio ? "audio" : "video", needShutdown);
1757
1758    const sp<DecoderBase> &decoder = getDecoder(audio);
1759    if (decoder == NULL) {
1760        ALOGI("flushDecoder %s without decoder present",
1761             audio ? "audio" : "video");
1762        return;
1763    }
1764
1765    // Make sure we don't continue to scan sources until we finish flushing.
1766    ++mScanSourcesGeneration;
1767    if (mScanSourcesPending) {
1768        mDeferredActions.push_back(
1769                new SimpleAction(&NuPlayer::performScanSources));
1770        mScanSourcesPending = false;
1771    }
1772
1773    decoder->signalFlush();
1774
1775    FlushStatus newStatus =
1776        needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1777
1778    mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
1779    mFlushComplete[audio][true /* isDecoder */] = false;
1780    if (audio) {
1781        ALOGE_IF(mFlushingAudio != NONE,
1782                "audio flushDecoder() is called in state %d", mFlushingAudio);
1783        mFlushingAudio = newStatus;
1784    } else {
1785        ALOGE_IF(mFlushingVideo != NONE,
1786                "video flushDecoder() is called in state %d", mFlushingVideo);
1787        mFlushingVideo = newStatus;
1788    }
1789}
1790
1791void NuPlayer::queueDecoderShutdown(
1792        bool audio, bool video, const sp<AMessage> &reply) {
1793    ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1794
1795    mDeferredActions.push_back(
1796            new FlushDecoderAction(
1797                audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1798                video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
1799
1800    mDeferredActions.push_back(
1801            new SimpleAction(&NuPlayer::performScanSources));
1802
1803    mDeferredActions.push_back(new PostMessageAction(reply));
1804
1805    processDeferredActions();
1806}
1807
1808status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1809    mVideoScalingMode = mode;
1810    if (mSurface != NULL) {
1811        status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
1812        if (ret != OK) {
1813            ALOGE("Failed to set scaling mode (%d): %s",
1814                -ret, strerror(-ret));
1815            return ret;
1816        }
1817    }
1818    return OK;
1819}
1820
1821status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1822    sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
1823    msg->setPointer("reply", reply);
1824
1825    sp<AMessage> response;
1826    status_t err = msg->postAndAwaitResponse(&response);
1827    return err;
1828}
1829
1830status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1831    sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
1832    msg->setPointer("reply", reply);
1833    msg->setInt32("type", type);
1834
1835    sp<AMessage> response;
1836    status_t err = msg->postAndAwaitResponse(&response);
1837    if (err == OK && response != NULL) {
1838        CHECK(response->findInt32("err", &err));
1839    }
1840    return err;
1841}
1842
1843status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
1844    sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
1845    msg->setSize("trackIndex", trackIndex);
1846    msg->setInt32("select", select);
1847    msg->setInt64("timeUs", timeUs);
1848
1849    sp<AMessage> response;
1850    status_t err = msg->postAndAwaitResponse(&response);
1851
1852    if (err != OK) {
1853        return err;
1854    }
1855
1856    if (!response->findInt32("err", &err)) {
1857        err = OK;
1858    }
1859
1860    return err;
1861}
1862
1863status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1864    sp<Renderer> renderer = mRenderer;
1865    if (renderer == NULL) {
1866        return NO_INIT;
1867    }
1868
1869    return renderer->getCurrentPosition(mediaUs);
1870}
1871
1872void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1873    CHECK(mTrackStats != NULL);
1874
1875    mTrackStats->clear();
1876    if (mVideoDecoder != NULL) {
1877        mTrackStats->push_back(mVideoDecoder->getStats());
1878    }
1879    if (mAudioDecoder != NULL) {
1880        mTrackStats->push_back(mAudioDecoder->getStats());
1881    }
1882}
1883
1884sp<MetaData> NuPlayer::getFileMeta() {
1885    return mSource->getFileFormatMeta();
1886}
1887
1888float NuPlayer::getFrameRate() {
1889    sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1890    if (meta == NULL) {
1891        return 0;
1892    }
1893    int32_t rate;
1894    if (!meta->findInt32(kKeyFrameRate, &rate)) {
1895        // fall back to try file meta
1896        sp<MetaData> fileMeta = getFileMeta();
1897        if (fileMeta == NULL) {
1898            ALOGW("source has video meta but not file meta");
1899            return -1;
1900        }
1901        int32_t fileMetaRate;
1902        if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1903            return -1;
1904        }
1905        return fileMetaRate;
1906    }
1907    return rate;
1908}
1909
1910void NuPlayer::schedulePollDuration() {
1911    sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
1912    msg->setInt32("generation", mPollDurationGeneration);
1913    msg->post();
1914}
1915
1916void NuPlayer::cancelPollDuration() {
1917    ++mPollDurationGeneration;
1918}
1919
1920void NuPlayer::processDeferredActions() {
1921    while (!mDeferredActions.empty()) {
1922        // We won't execute any deferred actions until we're no longer in
1923        // an intermediate state, i.e. one more more decoders are currently
1924        // flushing or shutting down.
1925
1926        if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1927            // We're currently flushing, postpone the reset until that's
1928            // completed.
1929
1930            ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1931                  mFlushingAudio, mFlushingVideo);
1932
1933            break;
1934        }
1935
1936        sp<Action> action = *mDeferredActions.begin();
1937        mDeferredActions.erase(mDeferredActions.begin());
1938
1939        action->execute(this);
1940    }
1941}
1942
1943void NuPlayer::performSeek(int64_t seekTimeUs) {
1944    ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1945          (long long)seekTimeUs,
1946          seekTimeUs / 1E6);
1947
1948    if (mSource == NULL) {
1949        // This happens when reset occurs right before the loop mode
1950        // asynchronously seeks to the start of the stream.
1951        LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1952                "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1953                mAudioDecoder.get(), mVideoDecoder.get());
1954        return;
1955    }
1956    mPreviousSeekTimeUs = seekTimeUs;
1957    mSource->seekTo(seekTimeUs);
1958    ++mTimedTextGeneration;
1959
1960    // everything's flushed, continue playback.
1961}
1962
1963void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1964    ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
1965
1966    if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1967            && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
1968        return;
1969    }
1970
1971    if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1972        flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
1973    }
1974
1975    if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1976        flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
1977    }
1978}
1979
1980void NuPlayer::performReset() {
1981    ALOGV("performReset");
1982
1983    CHECK(mAudioDecoder == NULL);
1984    CHECK(mVideoDecoder == NULL);
1985
1986    cancelPollDuration();
1987
1988    ++mScanSourcesGeneration;
1989    mScanSourcesPending = false;
1990
1991    if (mRendererLooper != NULL) {
1992        if (mRenderer != NULL) {
1993            mRendererLooper->unregisterHandler(mRenderer->id());
1994        }
1995        mRendererLooper->stop();
1996        mRendererLooper.clear();
1997    }
1998    mRenderer.clear();
1999    ++mRendererGeneration;
2000
2001    if (mSource != NULL) {
2002        mSource->stop();
2003
2004        Mutex::Autolock autoLock(mSourceLock);
2005        mSource.clear();
2006    }
2007
2008    if (mDriver != NULL) {
2009        sp<NuPlayerDriver> driver = mDriver.promote();
2010        if (driver != NULL) {
2011            driver->notifyResetComplete();
2012        }
2013    }
2014
2015    mStarted = false;
2016    mResetting = false;
2017    mSourceStarted = false;
2018}
2019
2020void NuPlayer::performScanSources() {
2021    ALOGV("performScanSources");
2022
2023    if (!mStarted) {
2024        return;
2025    }
2026
2027    if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
2028        postScanSources();
2029    }
2030}
2031
2032void NuPlayer::performSetSurface(const sp<Surface> &surface) {
2033    ALOGV("performSetSurface");
2034
2035    mSurface = surface;
2036
2037    // XXX - ignore error from setVideoScalingMode for now
2038    setVideoScalingMode(mVideoScalingMode);
2039
2040    if (mDriver != NULL) {
2041        sp<NuPlayerDriver> driver = mDriver.promote();
2042        if (driver != NULL) {
2043            driver->notifySetSurfaceComplete();
2044        }
2045    }
2046}
2047
2048void NuPlayer::performResumeDecoders(bool needNotify) {
2049    if (needNotify) {
2050        mResumePending = true;
2051        if (mVideoDecoder == NULL) {
2052            // if audio-only, we can notify seek complete now,
2053            // as the resume operation will be relatively fast.
2054            finishResume();
2055        }
2056    }
2057
2058    if (mVideoDecoder != NULL) {
2059        // When there is continuous seek, MediaPlayer will cache the seek
2060        // position, and send down new seek request when previous seek is
2061        // complete. Let's wait for at least one video output frame before
2062        // notifying seek complete, so that the video thumbnail gets updated
2063        // when seekbar is dragged.
2064        mVideoDecoder->signalResume(needNotify);
2065    }
2066
2067    if (mAudioDecoder != NULL) {
2068        mAudioDecoder->signalResume(false /* needNotify */);
2069    }
2070}
2071
2072void NuPlayer::finishResume() {
2073    if (mResumePending) {
2074        mResumePending = false;
2075        notifyDriverSeekComplete();
2076    }
2077}
2078
2079void NuPlayer::notifyDriverSeekComplete() {
2080    if (mDriver != NULL) {
2081        sp<NuPlayerDriver> driver = mDriver.promote();
2082        if (driver != NULL) {
2083            driver->notifySeekComplete();
2084        }
2085    }
2086}
2087
2088void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2089    int32_t what;
2090    CHECK(msg->findInt32("what", &what));
2091
2092    switch (what) {
2093        case Source::kWhatInstantiateSecureDecoders:
2094        {
2095            if (mSource == NULL) {
2096                // This is a stale notification from a source that was
2097                // asynchronously preparing when the client called reset().
2098                // We handled the reset, the source is gone.
2099                break;
2100            }
2101
2102            sp<AMessage> reply;
2103            CHECK(msg->findMessage("reply", &reply));
2104            status_t err = onInstantiateSecureDecoders();
2105            reply->setInt32("err", err);
2106            reply->post();
2107            break;
2108        }
2109
2110        case Source::kWhatPrepared:
2111        {
2112            if (mSource == NULL) {
2113                // This is a stale notification from a source that was
2114                // asynchronously preparing when the client called reset().
2115                // We handled the reset, the source is gone.
2116                break;
2117            }
2118
2119            int32_t err;
2120            CHECK(msg->findInt32("err", &err));
2121
2122            if (err != OK) {
2123                // shut down potential secure codecs in case client never calls reset
2124                mDeferredActions.push_back(
2125                        new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2126                                               FLUSH_CMD_SHUTDOWN /* video */));
2127                processDeferredActions();
2128            }
2129
2130            sp<NuPlayerDriver> driver = mDriver.promote();
2131            if (driver != NULL) {
2132                // notify duration first, so that it's definitely set when
2133                // the app received the "prepare complete" callback.
2134                int64_t durationUs;
2135                if (mSource->getDuration(&durationUs) == OK) {
2136                    driver->notifyDuration(durationUs);
2137                }
2138                driver->notifyPrepareCompleted(err);
2139            }
2140
2141            break;
2142        }
2143
2144        case Source::kWhatFlagsChanged:
2145        {
2146            uint32_t flags;
2147            CHECK(msg->findInt32("flags", (int32_t *)&flags));
2148
2149            sp<NuPlayerDriver> driver = mDriver.promote();
2150            if (driver != NULL) {
2151                if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2152                    driver->notifyListener(
2153                            MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2154                }
2155                driver->notifyFlagsChanged(flags);
2156            }
2157
2158            if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2159                    && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2160                cancelPollDuration();
2161            } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2162                    && (flags & Source::FLAG_DYNAMIC_DURATION)
2163                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2164                schedulePollDuration();
2165            }
2166
2167            mSourceFlags = flags;
2168            break;
2169        }
2170
2171        case Source::kWhatVideoSizeChanged:
2172        {
2173            sp<AMessage> format;
2174            CHECK(msg->findMessage("format", &format));
2175
2176            updateVideoSize(format);
2177            break;
2178        }
2179
2180        case Source::kWhatBufferingUpdate:
2181        {
2182            int32_t percentage;
2183            CHECK(msg->findInt32("percentage", &percentage));
2184
2185            notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2186            break;
2187        }
2188
2189        case Source::kWhatPauseOnBufferingStart:
2190        {
2191            // ignore if not playing
2192            if (mStarted) {
2193                ALOGI("buffer low, pausing...");
2194
2195                mPausedForBuffering = true;
2196                onPause();
2197            }
2198            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2199            break;
2200        }
2201
2202        case Source::kWhatResumeOnBufferingEnd:
2203        {
2204            // ignore if not playing
2205            if (mStarted) {
2206                ALOGI("buffer ready, resuming...");
2207
2208                mPausedForBuffering = false;
2209
2210                // do not resume yet if client didn't unpause
2211                if (!mPausedByClient) {
2212                    onResume();
2213                }
2214            }
2215            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2216            break;
2217        }
2218
2219        case Source::kWhatCacheStats:
2220        {
2221            int32_t kbps;
2222            CHECK(msg->findInt32("bandwidth", &kbps));
2223
2224            notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2225            break;
2226        }
2227
2228        case Source::kWhatSubtitleData:
2229        {
2230            sp<ABuffer> buffer;
2231            CHECK(msg->findBuffer("buffer", &buffer));
2232
2233            sendSubtitleData(buffer, 0 /* baseIndex */);
2234            break;
2235        }
2236
2237        case Source::kWhatTimedMetaData:
2238        {
2239            sp<ABuffer> buffer;
2240            if (!msg->findBuffer("buffer", &buffer)) {
2241                notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2242            } else {
2243                sendTimedMetaData(buffer);
2244            }
2245            break;
2246        }
2247
2248        case Source::kWhatTimedTextData:
2249        {
2250            int32_t generation;
2251            if (msg->findInt32("generation", &generation)
2252                    && generation != mTimedTextGeneration) {
2253                break;
2254            }
2255
2256            sp<ABuffer> buffer;
2257            CHECK(msg->findBuffer("buffer", &buffer));
2258
2259            sp<NuPlayerDriver> driver = mDriver.promote();
2260            if (driver == NULL) {
2261                break;
2262            }
2263
2264            int posMs;
2265            int64_t timeUs, posUs;
2266            driver->getCurrentPosition(&posMs);
2267            posUs = (int64_t) posMs * 1000ll;
2268            CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2269
2270            if (posUs < timeUs) {
2271                if (!msg->findInt32("generation", &generation)) {
2272                    msg->setInt32("generation", mTimedTextGeneration);
2273                }
2274                msg->post(timeUs - posUs);
2275            } else {
2276                sendTimedTextData(buffer);
2277            }
2278            break;
2279        }
2280
2281        case Source::kWhatQueueDecoderShutdown:
2282        {
2283            int32_t audio, video;
2284            CHECK(msg->findInt32("audio", &audio));
2285            CHECK(msg->findInt32("video", &video));
2286
2287            sp<AMessage> reply;
2288            CHECK(msg->findMessage("reply", &reply));
2289
2290            queueDecoderShutdown(audio, video, reply);
2291            break;
2292        }
2293
2294        case Source::kWhatDrmNoLicense:
2295        {
2296            notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2297            break;
2298        }
2299
2300        default:
2301            TRESPASS();
2302    }
2303}
2304
2305void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2306    int32_t what;
2307    CHECK(msg->findInt32("what", &what));
2308
2309    switch (what) {
2310        case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2311        {
2312            sp<ABuffer> buffer;
2313            CHECK(msg->findBuffer("buffer", &buffer));
2314
2315            size_t inbandTracks = 0;
2316            if (mSource != NULL) {
2317                inbandTracks = mSource->getTrackCount();
2318            }
2319
2320            sendSubtitleData(buffer, inbandTracks);
2321            break;
2322        }
2323
2324        case NuPlayer::CCDecoder::kWhatTrackAdded:
2325        {
2326            notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2327
2328            break;
2329        }
2330
2331        default:
2332            TRESPASS();
2333    }
2334
2335
2336}
2337
2338void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2339    int32_t trackIndex;
2340    int64_t timeUs, durationUs;
2341    CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2342    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2343    CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2344
2345    Parcel in;
2346    in.writeInt32(trackIndex + baseIndex);
2347    in.writeInt64(timeUs);
2348    in.writeInt64(durationUs);
2349    in.writeInt32(buffer->size());
2350    in.writeInt32(buffer->size());
2351    in.write(buffer->data(), buffer->size());
2352
2353    notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2354}
2355
2356void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2357    int64_t timeUs;
2358    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2359
2360    Parcel in;
2361    in.writeInt64(timeUs);
2362    in.writeInt32(buffer->size());
2363    in.writeInt32(buffer->size());
2364    in.write(buffer->data(), buffer->size());
2365
2366    notifyListener(MEDIA_META_DATA, 0, 0, &in);
2367}
2368
2369void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2370    const void *data;
2371    size_t size = 0;
2372    int64_t timeUs;
2373    int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
2374
2375    AString mime;
2376    CHECK(buffer->meta()->findString("mime", &mime));
2377    CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2378
2379    data = buffer->data();
2380    size = buffer->size();
2381
2382    Parcel parcel;
2383    if (size > 0) {
2384        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2385        int32_t global = 0;
2386        if (buffer->meta()->findInt32("global", &global) && global) {
2387            flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2388        } else {
2389            flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2390        }
2391        TextDescriptions::getParcelOfDescriptions(
2392                (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2393    }
2394
2395    if ((parcel.dataSize() > 0)) {
2396        notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2397    } else {  // send an empty timed text
2398        notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2399    }
2400}
2401////////////////////////////////////////////////////////////////////////////////
2402
2403sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2404    sp<MetaData> meta = getFormatMeta(audio);
2405
2406    if (meta == NULL) {
2407        return NULL;
2408    }
2409
2410    sp<AMessage> msg = new AMessage;
2411
2412    if(convertMetaDataToMessage(meta, &msg) == OK) {
2413        return msg;
2414    }
2415    return NULL;
2416}
2417
2418void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2419    sp<AMessage> notify = dupNotify();
2420    notify->setInt32("what", kWhatFlagsChanged);
2421    notify->setInt32("flags", flags);
2422    notify->post();
2423}
2424
2425void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
2426    sp<AMessage> notify = dupNotify();
2427    notify->setInt32("what", kWhatVideoSizeChanged);
2428    notify->setMessage("format", format);
2429    notify->post();
2430}
2431
2432void NuPlayer::Source::notifyPrepared(status_t err) {
2433    sp<AMessage> notify = dupNotify();
2434    notify->setInt32("what", kWhatPrepared);
2435    notify->setInt32("err", err);
2436    notify->post();
2437}
2438
2439void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2440    sp<AMessage> notify = dupNotify();
2441    notify->setInt32("what", kWhatInstantiateSecureDecoders);
2442    notify->setMessage("reply", reply);
2443    notify->post();
2444}
2445
2446void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
2447    TRESPASS();
2448}
2449
2450}  // namespace android
2451