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