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