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