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