NuPlayer.cpp revision 351ce871cee5da00340cf448e8d73b9e22cea078
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
22
23#include "HTTPLiveSource.h"
24#include "NuPlayerCCDecoder.h"
25#include "NuPlayerDecoder.h"
26#include "NuPlayerDecoderBase.h"
27#include "NuPlayerDecoderPassThrough.h"
28#include "NuPlayerDriver.h"
29#include "NuPlayerRenderer.h"
30#include "NuPlayerSource.h"
31#include "RTSPSource.h"
32#include "StreamingSource.h"
33#include "GenericSource.h"
34#include "TextDescriptions.h"
35
36#include "ATSParser.h"
37
38#include <cutils/properties.h>
39
40#include <media/AudioResamplerPublic.h>
41#include <media/AVSyncSettings.h>
42
43#include <media/stagefright/foundation/hexdump.h>
44#include <media/stagefright/foundation/ABuffer.h>
45#include <media/stagefright/foundation/ADebug.h>
46#include <media/stagefright/foundation/AMessage.h>
47#include <media/stagefright/MediaBuffer.h>
48#include <media/stagefright/MediaDefs.h>
49#include <media/stagefright/MediaErrors.h>
50#include <media/stagefright/MetaData.h>
51
52#include <gui/IGraphicBufferProducer.h>
53#include <gui/Surface.h>
54
55#include "avc_utils.h"
56
57#include "ESDS.h"
58#include <media/stagefright/Utils.h>
59
60namespace android {
61
62struct NuPlayer::Action : public RefBase {
63    Action() {}
64
65    virtual void execute(NuPlayer *player) = 0;
66
67private:
68    DISALLOW_EVIL_CONSTRUCTORS(Action);
69};
70
71struct NuPlayer::SeekAction : public Action {
72    SeekAction(int64_t seekTimeUs)
73        : mSeekTimeUs(seekTimeUs) {
74    }
75
76    virtual void execute(NuPlayer *player) {
77        player->performSeek(mSeekTimeUs);
78    }
79
80private:
81    int64_t mSeekTimeUs;
82
83    DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
84};
85
86struct NuPlayer::ResumeDecoderAction : public Action {
87    ResumeDecoderAction(bool needNotify)
88        : mNeedNotify(needNotify) {
89    }
90
91    virtual void execute(NuPlayer *player) {
92        player->performResumeDecoders(mNeedNotify);
93    }
94
95private:
96    bool mNeedNotify;
97
98    DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
99};
100
101struct NuPlayer::SetSurfaceAction : public Action {
102    SetSurfaceAction(const sp<Surface> &surface)
103        : mSurface(surface) {
104    }
105
106    virtual void execute(NuPlayer *player) {
107        player->performSetSurface(mSurface);
108    }
109
110private:
111    sp<Surface> mSurface;
112
113    DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
114};
115
116struct NuPlayer::FlushDecoderAction : public Action {
117    FlushDecoderAction(FlushCommand audio, FlushCommand video)
118        : mAudio(audio),
119          mVideo(video) {
120    }
121
122    virtual void execute(NuPlayer *player) {
123        player->performDecoderFlush(mAudio, mVideo);
124    }
125
126private:
127    FlushCommand mAudio;
128    FlushCommand mVideo;
129
130    DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
131};
132
133struct NuPlayer::PostMessageAction : public Action {
134    PostMessageAction(const sp<AMessage> &msg)
135        : mMessage(msg) {
136    }
137
138    virtual void execute(NuPlayer *) {
139        mMessage->post();
140    }
141
142private:
143    sp<AMessage> mMessage;
144
145    DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
146};
147
148// Use this if there's no state necessary to save in order to execute
149// the action.
150struct NuPlayer::SimpleAction : public Action {
151    typedef void (NuPlayer::*ActionFunc)();
152
153    SimpleAction(ActionFunc func)
154        : mFunc(func) {
155    }
156
157    virtual void execute(NuPlayer *player) {
158        (player->*mFunc)();
159    }
160
161private:
162    ActionFunc mFunc;
163
164    DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
165};
166
167////////////////////////////////////////////////////////////////////////////////
168
169NuPlayer::NuPlayer(pid_t pid)
170    : mUIDValid(false),
171      mPID(pid),
172      mSourceFlags(0),
173      mOffloadAudio(false),
174      mAudioDecoderGeneration(0),
175      mVideoDecoderGeneration(0),
176      mRendererGeneration(0),
177      mPreviousSeekTimeUs(0),
178      mAudioEOS(false),
179      mVideoEOS(false),
180      mScanSourcesPending(false),
181      mScanSourcesGeneration(0),
182      mPollDurationGeneration(0),
183      mTimedTextGeneration(0),
184      mFlushingAudio(NONE),
185      mFlushingVideo(NONE),
186      mResumePending(false),
187      mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
188      mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
189      mVideoFpsHint(-1.f),
190      mStarted(false),
191      mResetting(false),
192      mSourceStarted(false),
193      mPaused(false),
194      mPausedByClient(true),
195      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                    mPausedByClient = true;
740                    // save all other settings (using non-paused speed)
741                    // so we can restore them on start
742                    AudioPlaybackRate newRate = rate;
743                    newRate.mSpeed = mPlaybackSettings.mSpeed;
744                    mPlaybackSettings = newRate;
745                } else { /* rate.mSpeed != 0.f */
746                    onResume();
747                    mPausedByClient = false;
748                    mPlaybackSettings = rate;
749                }
750            }
751
752            if (mVideoDecoder != NULL) {
753                float rate = getFrameRate();
754                if (rate > 0) {
755                    sp<AMessage> params = new AMessage();
756                    params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
757                    mVideoDecoder->setParameters(params);
758                }
759            }
760
761            sp<AMessage> response = new AMessage;
762            response->setInt32("err", err);
763            response->postReply(replyID);
764            break;
765        }
766
767        case kWhatGetPlaybackSettings:
768        {
769            sp<AReplyToken> replyID;
770            CHECK(msg->senderAwaitsResponse(&replyID));
771            AudioPlaybackRate rate = mPlaybackSettings;
772            status_t err = OK;
773            if (mRenderer != NULL) {
774                err = mRenderer->getPlaybackSettings(&rate);
775            }
776            if (err == OK) {
777                // get playback settings used by renderer, as it may be
778                // slightly off due to audiosink not taking small changes.
779                mPlaybackSettings = rate;
780                if (mPaused) {
781                    rate.mSpeed = 0.f;
782                }
783            }
784            sp<AMessage> response = new AMessage;
785            if (err == OK) {
786                writeToAMessage(response, rate);
787            }
788            response->setInt32("err", err);
789            response->postReply(replyID);
790            break;
791        }
792
793        case kWhatConfigSync:
794        {
795            sp<AReplyToken> replyID;
796            CHECK(msg->senderAwaitsResponse(&replyID));
797
798            ALOGV("kWhatConfigSync");
799            AVSyncSettings sync;
800            float videoFpsHint;
801            readFromAMessage(msg, &sync, &videoFpsHint);
802            status_t err = OK;
803            if (mRenderer != NULL) {
804                err = mRenderer->setSyncSettings(sync, videoFpsHint);
805            }
806            if (err == OK) {
807                mSyncSettings = sync;
808                mVideoFpsHint = videoFpsHint;
809            }
810            sp<AMessage> response = new AMessage;
811            response->setInt32("err", err);
812            response->postReply(replyID);
813            break;
814        }
815
816        case kWhatGetSyncSettings:
817        {
818            sp<AReplyToken> replyID;
819            CHECK(msg->senderAwaitsResponse(&replyID));
820            AVSyncSettings sync = mSyncSettings;
821            float videoFps = mVideoFpsHint;
822            status_t err = OK;
823            if (mRenderer != NULL) {
824                err = mRenderer->getSyncSettings(&sync, &videoFps);
825                if (err == OK) {
826                    mSyncSettings = sync;
827                    mVideoFpsHint = videoFps;
828                }
829            }
830            sp<AMessage> response = new AMessage;
831            if (err == OK) {
832                writeToAMessage(response, sync, videoFps);
833            }
834            response->setInt32("err", err);
835            response->postReply(replyID);
836            break;
837        }
838
839        case kWhatScanSources:
840        {
841            int32_t generation;
842            CHECK(msg->findInt32("generation", &generation));
843            if (generation != mScanSourcesGeneration) {
844                // Drop obsolete msg.
845                break;
846            }
847
848            mScanSourcesPending = false;
849
850            ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
851                 mAudioDecoder != NULL, mVideoDecoder != NULL);
852
853            bool mHadAnySourcesBefore =
854                (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
855            bool rescan = false;
856
857            // initialize video before audio because successful initialization of
858            // video may change deep buffer mode of audio.
859            if (mSurface != NULL) {
860                if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) {
861                    rescan = true;
862                }
863            }
864
865            // Don't try to re-open audio sink if there's an existing decoder.
866            if (mAudioSink != NULL && mAudioDecoder == NULL) {
867                if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) {
868                    rescan = true;
869                }
870            }
871
872            if (!mHadAnySourcesBefore
873                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
874                // This is the first time we've found anything playable.
875
876                if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
877                    schedulePollDuration();
878                }
879            }
880
881            status_t err;
882            if ((err = mSource->feedMoreTSData()) != OK) {
883                if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
884                    // We're not currently decoding anything (no audio or
885                    // video tracks found) and we just ran out of input data.
886
887                    if (err == ERROR_END_OF_STREAM) {
888                        notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
889                    } else {
890                        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
891                    }
892                }
893                break;
894            }
895
896            if (rescan) {
897                msg->post(100000ll);
898                mScanSourcesPending = true;
899            }
900            break;
901        }
902
903        case kWhatVideoNotify:
904        case kWhatAudioNotify:
905        {
906            bool audio = msg->what() == kWhatAudioNotify;
907
908            int32_t currentDecoderGeneration =
909                (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
910            int32_t requesterGeneration = currentDecoderGeneration - 1;
911            CHECK(msg->findInt32("generation", &requesterGeneration));
912
913            if (requesterGeneration != currentDecoderGeneration) {
914                ALOGV("got message from old %s decoder, generation(%d:%d)",
915                        audio ? "audio" : "video", requesterGeneration,
916                        currentDecoderGeneration);
917                sp<AMessage> reply;
918                if (!(msg->findMessage("reply", &reply))) {
919                    return;
920                }
921
922                reply->setInt32("err", INFO_DISCONTINUITY);
923                reply->post();
924                return;
925            }
926
927            int32_t what;
928            CHECK(msg->findInt32("what", &what));
929
930            if (what == DecoderBase::kWhatInputDiscontinuity) {
931                int32_t formatChange;
932                CHECK(msg->findInt32("formatChange", &formatChange));
933
934                ALOGV("%s discontinuity: formatChange %d",
935                        audio ? "audio" : "video", formatChange);
936
937                if (formatChange) {
938                    mDeferredActions.push_back(
939                            new FlushDecoderAction(
940                                audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
941                                audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
942                }
943
944                mDeferredActions.push_back(
945                        new SimpleAction(
946                                &NuPlayer::performScanSources));
947
948                processDeferredActions();
949            } else if (what == DecoderBase::kWhatEOS) {
950                int32_t err;
951                CHECK(msg->findInt32("err", &err));
952
953                if (err == ERROR_END_OF_STREAM) {
954                    ALOGV("got %s decoder EOS", audio ? "audio" : "video");
955                } else {
956                    ALOGV("got %s decoder EOS w/ error %d",
957                         audio ? "audio" : "video",
958                         err);
959                }
960
961                mRenderer->queueEOS(audio, err);
962            } else if (what == DecoderBase::kWhatFlushCompleted) {
963                ALOGV("decoder %s flush completed", audio ? "audio" : "video");
964
965                handleFlushComplete(audio, true /* isDecoder */);
966                finishFlushIfPossible();
967            } else if (what == DecoderBase::kWhatVideoSizeChanged) {
968                sp<AMessage> format;
969                CHECK(msg->findMessage("format", &format));
970
971                sp<AMessage> inputFormat =
972                        mSource->getFormat(false /* audio */);
973
974                updateVideoSize(inputFormat, format);
975            } else if (what == DecoderBase::kWhatShutdownCompleted) {
976                ALOGV("%s shutdown completed", audio ? "audio" : "video");
977                if (audio) {
978                    mAudioDecoder.clear();
979                    ++mAudioDecoderGeneration;
980
981                    CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
982                    mFlushingAudio = SHUT_DOWN;
983                } else {
984                    mVideoDecoder.clear();
985                    ++mVideoDecoderGeneration;
986
987                    CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
988                    mFlushingVideo = SHUT_DOWN;
989                }
990
991                finishFlushIfPossible();
992            } else if (what == DecoderBase::kWhatResumeCompleted) {
993                finishResume();
994            } else if (what == DecoderBase::kWhatError) {
995                status_t err;
996                if (!msg->findInt32("err", &err) || err == OK) {
997                    err = UNKNOWN_ERROR;
998                }
999
1000                // Decoder errors can be due to Source (e.g. from streaming),
1001                // or from decoding corrupted bitstreams, or from other decoder
1002                // MediaCodec operations (e.g. from an ongoing reset or seek).
1003                // They may also be due to openAudioSink failure at
1004                // decoder start or after a format change.
1005                //
1006                // We try to gracefully shut down the affected decoder if possible,
1007                // rather than trying to force the shutdown with something
1008                // similar to performReset(). This method can lead to a hang
1009                // if MediaCodec functions block after an error, but they should
1010                // typically return INVALID_OPERATION instead of blocking.
1011
1012                FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
1013                ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
1014                        err, audio ? "audio" : "video", *flushing);
1015
1016                switch (*flushing) {
1017                    case NONE:
1018                        mDeferredActions.push_back(
1019                                new FlushDecoderAction(
1020                                    audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1021                                    audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
1022                        processDeferredActions();
1023                        break;
1024                    case FLUSHING_DECODER:
1025                        *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
1026                        break; // Wait for flush to complete.
1027                    case FLUSHING_DECODER_SHUTDOWN:
1028                        break; // Wait for flush to complete.
1029                    case SHUTTING_DOWN_DECODER:
1030                        break; // Wait for shutdown to complete.
1031                    case FLUSHED:
1032                        // Widevine source reads must stop before releasing the video decoder.
1033                        if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1034                            mSource->stop();
1035                            mSourceStarted = false;
1036                        }
1037                        getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
1038                        *flushing = SHUTTING_DOWN_DECODER;     // Shut down.
1039                        break;
1040                    case SHUT_DOWN:
1041                        finishFlushIfPossible();  // Should not occur.
1042                        break;                    // Finish anyways.
1043                }
1044                notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1045            } else {
1046                ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
1047                      what,
1048                      what >> 24,
1049                      (what >> 16) & 0xff,
1050                      (what >> 8) & 0xff,
1051                      what & 0xff);
1052            }
1053
1054            break;
1055        }
1056
1057        case kWhatRendererNotify:
1058        {
1059            int32_t requesterGeneration = mRendererGeneration - 1;
1060            CHECK(msg->findInt32("generation", &requesterGeneration));
1061            if (requesterGeneration != mRendererGeneration) {
1062                ALOGV("got message from old renderer, generation(%d:%d)",
1063                        requesterGeneration, mRendererGeneration);
1064                return;
1065            }
1066
1067            int32_t what;
1068            CHECK(msg->findInt32("what", &what));
1069
1070            if (what == Renderer::kWhatEOS) {
1071                int32_t audio;
1072                CHECK(msg->findInt32("audio", &audio));
1073
1074                int32_t finalResult;
1075                CHECK(msg->findInt32("finalResult", &finalResult));
1076
1077                if (audio) {
1078                    mAudioEOS = true;
1079                } else {
1080                    mVideoEOS = true;
1081                }
1082
1083                if (finalResult == ERROR_END_OF_STREAM) {
1084                    ALOGV("reached %s EOS", audio ? "audio" : "video");
1085                } else {
1086                    ALOGE("%s track encountered an error (%d)",
1087                         audio ? "audio" : "video", finalResult);
1088
1089                    notifyListener(
1090                            MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
1091                }
1092
1093                if ((mAudioEOS || mAudioDecoder == NULL)
1094                        && (mVideoEOS || mVideoDecoder == NULL)) {
1095                    notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
1096                }
1097            } else if (what == Renderer::kWhatFlushComplete) {
1098                int32_t audio;
1099                CHECK(msg->findInt32("audio", &audio));
1100
1101                if (audio) {
1102                    mAudioEOS = false;
1103                } else {
1104                    mVideoEOS = false;
1105                }
1106
1107                ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
1108                if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED
1109                        || mFlushingAudio == SHUT_DOWN)) {
1110                    // Flush has been handled by tear down.
1111                    break;
1112                }
1113                handleFlushComplete(audio, false /* isDecoder */);
1114                finishFlushIfPossible();
1115            } else if (what == Renderer::kWhatVideoRenderingStart) {
1116                notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
1117            } else if (what == Renderer::kWhatMediaRenderingStart) {
1118                ALOGV("media rendering started");
1119                notifyListener(MEDIA_STARTED, 0, 0);
1120            } else if (what == Renderer::kWhatAudioTearDown) {
1121                int32_t reason;
1122                CHECK(msg->findInt32("reason", &reason));
1123                ALOGV("Tear down audio with reason %d.", reason);
1124                mAudioDecoder->pause();
1125                mAudioDecoder.clear();
1126                ++mAudioDecoderGeneration;
1127                bool needsToCreateAudioDecoder = true;
1128                if (mFlushingAudio == FLUSHING_DECODER) {
1129                    mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1130                    mFlushingAudio = FLUSHED;
1131                    finishFlushIfPossible();
1132                } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN
1133                        || mFlushingAudio == SHUTTING_DOWN_DECODER) {
1134                    mFlushComplete[1 /* audio */][1 /* isDecoder */] = true;
1135                    mFlushingAudio = SHUT_DOWN;
1136                    finishFlushIfPossible();
1137                    needsToCreateAudioDecoder = false;
1138                }
1139                if (mRenderer == NULL) {
1140                    break;
1141                }
1142                closeAudioSink();
1143                mRenderer->flush(
1144                        true /* audio */, false /* notifyComplete */);
1145                if (mVideoDecoder != NULL) {
1146                    mRenderer->flush(
1147                            false /* audio */, false /* notifyComplete */);
1148                }
1149
1150                int64_t positionUs;
1151                if (!msg->findInt64("positionUs", &positionUs)) {
1152                    positionUs = mPreviousSeekTimeUs;
1153                }
1154                performSeek(positionUs);
1155
1156                if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) {
1157                    instantiateDecoder(true /* audio */, &mAudioDecoder);
1158                }
1159            }
1160            break;
1161        }
1162
1163        case kWhatMoreDataQueued:
1164        {
1165            break;
1166        }
1167
1168        case kWhatReset:
1169        {
1170            ALOGV("kWhatReset");
1171
1172            mResetting = true;
1173
1174            mDeferredActions.push_back(
1175                    new FlushDecoderAction(
1176                        FLUSH_CMD_SHUTDOWN /* audio */,
1177                        FLUSH_CMD_SHUTDOWN /* video */));
1178
1179            mDeferredActions.push_back(
1180                    new SimpleAction(&NuPlayer::performReset));
1181
1182            processDeferredActions();
1183            break;
1184        }
1185
1186        case kWhatSeek:
1187        {
1188            int64_t seekTimeUs;
1189            int32_t needNotify;
1190            CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1191            CHECK(msg->findInt32("needNotify", &needNotify));
1192
1193            ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
1194                    (long long)seekTimeUs, needNotify);
1195
1196            if (!mStarted) {
1197                // Seek before the player is started. In order to preview video,
1198                // need to start the player and pause it. This branch is called
1199                // only once if needed. After the player is started, any seek
1200                // operation will go through normal path.
1201                // Audio-only cases are handled separately.
1202                onStart(seekTimeUs);
1203                if (mStarted) {
1204                    onPause();
1205                    mPausedByClient = true;
1206                }
1207                if (needNotify) {
1208                    notifyDriverSeekComplete();
1209                }
1210                break;
1211            }
1212
1213            mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
1214
1215            mDeferredActions.push_back(
1216                    new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
1217                                           FLUSH_CMD_FLUSH /* video */));
1218
1219            mDeferredActions.push_back(
1220                    new SeekAction(seekTimeUs));
1221
1222            // After a flush without shutdown, decoder is paused.
1223            // Don't resume it until source seek is done, otherwise it could
1224            // start pulling stale data too soon.
1225            mDeferredActions.push_back(
1226                    new ResumeDecoderAction(needNotify));
1227
1228            processDeferredActions();
1229            break;
1230        }
1231
1232        case kWhatPause:
1233        {
1234            onPause();
1235            mPausedByClient = true;
1236            break;
1237        }
1238
1239        case kWhatSourceNotify:
1240        {
1241            onSourceNotify(msg);
1242            break;
1243        }
1244
1245        case kWhatClosedCaptionNotify:
1246        {
1247            onClosedCaptionNotify(msg);
1248            break;
1249        }
1250
1251        default:
1252            TRESPASS();
1253            break;
1254    }
1255}
1256
1257void NuPlayer::onResume() {
1258    if (!mPaused || mResetting) {
1259        ALOGD_IF(mResetting, "resetting, onResume discarded");
1260        return;
1261    }
1262    mPaused = false;
1263    if (mSource != NULL) {
1264        mSource->resume();
1265    } else {
1266        ALOGW("resume called when source is gone or not set");
1267    }
1268    // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
1269    // needed.
1270    if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
1271        instantiateDecoder(true /* audio */, &mAudioDecoder);
1272    }
1273    if (mRenderer != NULL) {
1274        mRenderer->resume();
1275    } else {
1276        ALOGW("resume called when renderer is gone or not set");
1277    }
1278}
1279
1280status_t NuPlayer::onInstantiateSecureDecoders() {
1281    status_t err;
1282    if (!(mSourceFlags & Source::FLAG_SECURE)) {
1283        return BAD_TYPE;
1284    }
1285
1286    if (mRenderer != NULL) {
1287        ALOGE("renderer should not be set when instantiating secure decoders");
1288        return UNKNOWN_ERROR;
1289    }
1290
1291    // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting
1292    // data on instantiation.
1293    if (mSurface != NULL) {
1294        err = instantiateDecoder(false, &mVideoDecoder);
1295        if (err != OK) {
1296            return err;
1297        }
1298    }
1299
1300    if (mAudioSink != NULL) {
1301        err = instantiateDecoder(true, &mAudioDecoder);
1302        if (err != OK) {
1303            return err;
1304        }
1305    }
1306    return OK;
1307}
1308
1309void NuPlayer::onStart(int64_t startPositionUs) {
1310    if (!mSourceStarted) {
1311        mSourceStarted = true;
1312        mSource->start();
1313    }
1314    if (startPositionUs > 0) {
1315        performSeek(startPositionUs);
1316        if (mSource->getFormat(false /* audio */) == NULL) {
1317            return;
1318        }
1319    }
1320
1321    mOffloadAudio = false;
1322    mAudioEOS = false;
1323    mVideoEOS = false;
1324    mStarted = true;
1325
1326    uint32_t flags = 0;
1327
1328    if (mSource->isRealTime()) {
1329        flags |= Renderer::FLAG_REAL_TIME;
1330    }
1331
1332    sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1333    ALOGV_IF(audioMeta == NULL, "no metadata for audio source");  // video only stream
1334    audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1335    if (mAudioSink != NULL) {
1336        streamType = mAudioSink->getAudioStreamType();
1337    }
1338
1339    sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1340
1341    mOffloadAudio =
1342        canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType);
1343    if (mOffloadAudio) {
1344        flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1345    }
1346
1347    sp<AMessage> notify = new AMessage(kWhatRendererNotify, this);
1348    ++mRendererGeneration;
1349    notify->setInt32("generation", mRendererGeneration);
1350    mRenderer = new Renderer(mAudioSink, notify, flags);
1351    mRendererLooper = new ALooper;
1352    mRendererLooper->setName("NuPlayerRenderer");
1353    mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1354    mRendererLooper->registerHandler(mRenderer);
1355
1356    status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings);
1357    if (err != OK) {
1358        mSource->stop();
1359        mSourceStarted = false;
1360        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
1361        return;
1362    }
1363
1364    float rate = getFrameRate();
1365    if (rate > 0) {
1366        mRenderer->setVideoFrameRate(rate);
1367    }
1368
1369    if (mVideoDecoder != NULL) {
1370        mVideoDecoder->setRenderer(mRenderer);
1371    }
1372    if (mAudioDecoder != NULL) {
1373        mAudioDecoder->setRenderer(mRenderer);
1374    }
1375
1376    postScanSources();
1377}
1378
1379void NuPlayer::onPause() {
1380    if (mPaused) {
1381        return;
1382    }
1383    mPaused = true;
1384    if (mSource != NULL) {
1385        mSource->pause();
1386    } else {
1387        ALOGW("pause called when source is gone or not set");
1388    }
1389    if (mRenderer != NULL) {
1390        mRenderer->pause();
1391    } else {
1392        ALOGW("pause called when renderer is gone or not set");
1393    }
1394}
1395
1396bool NuPlayer::audioDecoderStillNeeded() {
1397    // Audio decoder is no longer needed if it's in shut/shutting down status.
1398    return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1399}
1400
1401void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1402    // We wait for both the decoder flush and the renderer flush to complete
1403    // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1404
1405    mFlushComplete[audio][isDecoder] = true;
1406    if (!mFlushComplete[audio][!isDecoder]) {
1407        return;
1408    }
1409
1410    FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1411    switch (*state) {
1412        case FLUSHING_DECODER:
1413        {
1414            *state = FLUSHED;
1415            break;
1416        }
1417
1418        case FLUSHING_DECODER_SHUTDOWN:
1419        {
1420            *state = SHUTTING_DOWN_DECODER;
1421
1422            ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1423            if (!audio) {
1424                // Widevine source reads must stop before releasing the video decoder.
1425                if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1426                    mSource->stop();
1427                    mSourceStarted = false;
1428                }
1429            }
1430            getDecoder(audio)->initiateShutdown();
1431            break;
1432        }
1433
1434        default:
1435            // decoder flush completes only occur in a flushing state.
1436            LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1437            break;
1438    }
1439}
1440
1441void NuPlayer::finishFlushIfPossible() {
1442    if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1443            && mFlushingAudio != SHUT_DOWN) {
1444        return;
1445    }
1446
1447    if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1448            && mFlushingVideo != SHUT_DOWN) {
1449        return;
1450    }
1451
1452    ALOGV("both audio and video are flushed now.");
1453
1454    mFlushingAudio = NONE;
1455    mFlushingVideo = NONE;
1456
1457    clearFlushComplete();
1458
1459    processDeferredActions();
1460}
1461
1462void NuPlayer::postScanSources() {
1463    if (mScanSourcesPending) {
1464        return;
1465    }
1466
1467    sp<AMessage> msg = new AMessage(kWhatScanSources, this);
1468    msg->setInt32("generation", mScanSourcesGeneration);
1469    msg->post();
1470
1471    mScanSourcesPending = true;
1472}
1473
1474void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1475    // Note: This is called early in NuPlayer to determine whether offloading
1476    // is possible; otherwise the decoders call the renderer openAudioSink directly.
1477
1478    status_t err = mRenderer->openAudioSink(
1479            format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1480    if (err != OK) {
1481        // Any failure we turn off mOffloadAudio.
1482        mOffloadAudio = false;
1483    } else if (mOffloadAudio) {
1484        sp<MetaData> audioMeta =
1485                mSource->getFormatMeta(true /* audio */);
1486        sendMetaDataToHal(mAudioSink, audioMeta);
1487    }
1488}
1489
1490void NuPlayer::closeAudioSink() {
1491    mRenderer->closeAudioSink();
1492}
1493
1494void NuPlayer::determineAudioModeChange() {
1495    if (mSource == NULL || mAudioSink == NULL) {
1496        return;
1497    }
1498
1499    if (mRenderer == NULL) {
1500        ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety.");
1501        mOffloadAudio = false;
1502        return;
1503    }
1504
1505    sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1506    sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1507    audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
1508    const bool hasVideo = (videoFormat != NULL);
1509    const bool canOffload = canOffloadStream(
1510            audioMeta, hasVideo, mSource->isStreaming(), streamType);
1511    if (canOffload) {
1512        if (!mOffloadAudio) {
1513            mRenderer->signalEnableOffloadAudio();
1514        }
1515        // open audio sink early under offload mode.
1516        sp<AMessage> format = mSource->getFormat(true /*audio*/);
1517        tryOpenAudioSinkForOffload(format, hasVideo);
1518    } else {
1519        if (mOffloadAudio) {
1520            mRenderer->signalDisableOffloadAudio();
1521            mOffloadAudio = false;
1522        }
1523    }
1524}
1525
1526status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
1527    // The audio decoder could be cleared by tear down. If still in shut down
1528    // process, no need to create a new audio decoder.
1529    if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) {
1530        return OK;
1531    }
1532
1533    sp<AMessage> format = mSource->getFormat(audio);
1534
1535    if (format == NULL) {
1536        return UNKNOWN_ERROR;
1537    } else {
1538        status_t err;
1539        if (format->findInt32("err", &err) && err) {
1540            return err;
1541        }
1542    }
1543
1544    format->setInt32("priority", 0 /* realtime */);
1545
1546    if (!audio) {
1547        AString mime;
1548        CHECK(format->findString("mime", &mime));
1549
1550        sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this);
1551        if (mCCDecoder == NULL) {
1552            mCCDecoder = new CCDecoder(ccNotify);
1553        }
1554
1555        if (mSourceFlags & Source::FLAG_SECURE) {
1556            format->setInt32("secure", true);
1557        }
1558
1559        if (mSourceFlags & Source::FLAG_PROTECTED) {
1560            format->setInt32("protected", true);
1561        }
1562
1563        float rate = getFrameRate();
1564        if (rate > 0) {
1565            format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed);
1566        }
1567    }
1568
1569    if (audio) {
1570        sp<AMessage> notify = new AMessage(kWhatAudioNotify, this);
1571        ++mAudioDecoderGeneration;
1572        notify->setInt32("generation", mAudioDecoderGeneration);
1573
1574        determineAudioModeChange();
1575        if (mOffloadAudio) {
1576            mSource->setOffloadAudio(true /* offload */);
1577
1578            const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL);
1579            format->setInt32("has-video", hasVideo);
1580            *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
1581        } else {
1582            mSource->setOffloadAudio(false /* offload */);
1583
1584            *decoder = new Decoder(notify, mSource, mPID, mRenderer);
1585        }
1586    } else {
1587        sp<AMessage> notify = new AMessage(kWhatVideoNotify, this);
1588        ++mVideoDecoderGeneration;
1589        notify->setInt32("generation", mVideoDecoderGeneration);
1590
1591        *decoder = new Decoder(
1592                notify, mSource, mPID, mRenderer, mSurface, mCCDecoder);
1593
1594        // enable FRC if high-quality AV sync is requested, even if not
1595        // directly queuing to display, as this will even improve textureview
1596        // playback.
1597        {
1598            char value[PROPERTY_VALUE_MAX];
1599            if (property_get("persist.sys.media.avsync", value, NULL) &&
1600                    (!strcmp("1", value) || !strcasecmp("true", value))) {
1601                format->setInt32("auto-frc", 1);
1602            }
1603        }
1604    }
1605    (*decoder)->init();
1606    (*decoder)->configure(format);
1607
1608    // allocate buffers to decrypt widevine source buffers
1609    if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1610        Vector<sp<ABuffer> > inputBufs;
1611        CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1612
1613        Vector<MediaBuffer *> mediaBufs;
1614        for (size_t i = 0; i < inputBufs.size(); i++) {
1615            const sp<ABuffer> &buffer = inputBufs[i];
1616            MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1617            mediaBufs.push(mbuf);
1618        }
1619
1620        status_t err = mSource->setBuffers(audio, mediaBufs);
1621        if (err != OK) {
1622            for (size_t i = 0; i < mediaBufs.size(); ++i) {
1623                mediaBufs[i]->release();
1624            }
1625            mediaBufs.clear();
1626            ALOGE("Secure source didn't support secure mediaBufs.");
1627            return err;
1628        }
1629    }
1630    return OK;
1631}
1632
1633void NuPlayer::updateVideoSize(
1634        const sp<AMessage> &inputFormat,
1635        const sp<AMessage> &outputFormat) {
1636    if (inputFormat == NULL) {
1637        ALOGW("Unknown video size, reporting 0x0!");
1638        notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1639        return;
1640    }
1641
1642    int32_t displayWidth, displayHeight;
1643    if (outputFormat != NULL) {
1644        int32_t width, height;
1645        CHECK(outputFormat->findInt32("width", &width));
1646        CHECK(outputFormat->findInt32("height", &height));
1647
1648        int32_t cropLeft, cropTop, cropRight, cropBottom;
1649        CHECK(outputFormat->findRect(
1650                    "crop",
1651                    &cropLeft, &cropTop, &cropRight, &cropBottom));
1652
1653        displayWidth = cropRight - cropLeft + 1;
1654        displayHeight = cropBottom - cropTop + 1;
1655
1656        ALOGV("Video output format changed to %d x %d "
1657             "(crop: %d x %d @ (%d, %d))",
1658             width, height,
1659             displayWidth,
1660             displayHeight,
1661             cropLeft, cropTop);
1662    } else {
1663        CHECK(inputFormat->findInt32("width", &displayWidth));
1664        CHECK(inputFormat->findInt32("height", &displayHeight));
1665
1666        ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1667    }
1668
1669    // Take into account sample aspect ratio if necessary:
1670    int32_t sarWidth, sarHeight;
1671    if (inputFormat->findInt32("sar-width", &sarWidth)
1672            && inputFormat->findInt32("sar-height", &sarHeight)) {
1673        ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1674
1675        displayWidth = (displayWidth * sarWidth) / sarHeight;
1676
1677        ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1678    }
1679
1680    int32_t rotationDegrees;
1681    if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1682        rotationDegrees = 0;
1683    }
1684
1685    if (rotationDegrees == 90 || rotationDegrees == 270) {
1686        int32_t tmp = displayWidth;
1687        displayWidth = displayHeight;
1688        displayHeight = tmp;
1689    }
1690
1691    notifyListener(
1692            MEDIA_SET_VIDEO_SIZE,
1693            displayWidth,
1694            displayHeight);
1695}
1696
1697void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
1698    if (mDriver == NULL) {
1699        return;
1700    }
1701
1702    sp<NuPlayerDriver> driver = mDriver.promote();
1703
1704    if (driver == NULL) {
1705        return;
1706    }
1707
1708    driver->notifyListener(msg, ext1, ext2, in);
1709}
1710
1711void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
1712    ALOGV("[%s] flushDecoder needShutdown=%d",
1713          audio ? "audio" : "video", needShutdown);
1714
1715    const sp<DecoderBase> &decoder = getDecoder(audio);
1716    if (decoder == NULL) {
1717        ALOGI("flushDecoder %s without decoder present",
1718             audio ? "audio" : "video");
1719        return;
1720    }
1721
1722    // Make sure we don't continue to scan sources until we finish flushing.
1723    ++mScanSourcesGeneration;
1724    if (mScanSourcesPending) {
1725        mDeferredActions.push_back(
1726                new SimpleAction(&NuPlayer::performScanSources));
1727        mScanSourcesPending = false;
1728    }
1729
1730    decoder->signalFlush();
1731
1732    FlushStatus newStatus =
1733        needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1734
1735    mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL);
1736    mFlushComplete[audio][true /* isDecoder */] = false;
1737    if (audio) {
1738        ALOGE_IF(mFlushingAudio != NONE,
1739                "audio flushDecoder() is called in state %d", mFlushingAudio);
1740        mFlushingAudio = newStatus;
1741    } else {
1742        ALOGE_IF(mFlushingVideo != NONE,
1743                "video flushDecoder() is called in state %d", mFlushingVideo);
1744        mFlushingVideo = newStatus;
1745    }
1746}
1747
1748void NuPlayer::queueDecoderShutdown(
1749        bool audio, bool video, const sp<AMessage> &reply) {
1750    ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1751
1752    mDeferredActions.push_back(
1753            new FlushDecoderAction(
1754                audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1755                video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
1756
1757    mDeferredActions.push_back(
1758            new SimpleAction(&NuPlayer::performScanSources));
1759
1760    mDeferredActions.push_back(new PostMessageAction(reply));
1761
1762    processDeferredActions();
1763}
1764
1765status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1766    mVideoScalingMode = mode;
1767    if (mSurface != NULL) {
1768        status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode);
1769        if (ret != OK) {
1770            ALOGE("Failed to set scaling mode (%d): %s",
1771                -ret, strerror(-ret));
1772            return ret;
1773        }
1774    }
1775    return OK;
1776}
1777
1778status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1779    sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this);
1780    msg->setPointer("reply", reply);
1781
1782    sp<AMessage> response;
1783    status_t err = msg->postAndAwaitResponse(&response);
1784    return err;
1785}
1786
1787status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1788    sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
1789    msg->setPointer("reply", reply);
1790    msg->setInt32("type", type);
1791
1792    sp<AMessage> response;
1793    status_t err = msg->postAndAwaitResponse(&response);
1794    if (err == OK && response != NULL) {
1795        CHECK(response->findInt32("err", &err));
1796    }
1797    return err;
1798}
1799
1800status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
1801    sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
1802    msg->setSize("trackIndex", trackIndex);
1803    msg->setInt32("select", select);
1804    msg->setInt64("timeUs", timeUs);
1805
1806    sp<AMessage> response;
1807    status_t err = msg->postAndAwaitResponse(&response);
1808
1809    if (err != OK) {
1810        return err;
1811    }
1812
1813    if (!response->findInt32("err", &err)) {
1814        err = OK;
1815    }
1816
1817    return err;
1818}
1819
1820status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1821    sp<Renderer> renderer = mRenderer;
1822    if (renderer == NULL) {
1823        return NO_INIT;
1824    }
1825
1826    return renderer->getCurrentPosition(mediaUs);
1827}
1828
1829void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) {
1830    CHECK(mTrackStats != NULL);
1831
1832    mTrackStats->clear();
1833    if (mVideoDecoder != NULL) {
1834        mTrackStats->push_back(mVideoDecoder->getStats());
1835    }
1836    if (mAudioDecoder != NULL) {
1837        mTrackStats->push_back(mAudioDecoder->getStats());
1838    }
1839}
1840
1841sp<MetaData> NuPlayer::getFileMeta() {
1842    return mSource->getFileFormatMeta();
1843}
1844
1845float NuPlayer::getFrameRate() {
1846    sp<MetaData> meta = mSource->getFormatMeta(false /* audio */);
1847    if (meta == NULL) {
1848        return 0;
1849    }
1850    int32_t rate;
1851    if (!meta->findInt32(kKeyFrameRate, &rate)) {
1852        // fall back to try file meta
1853        sp<MetaData> fileMeta = getFileMeta();
1854        if (fileMeta == NULL) {
1855            ALOGW("source has video meta but not file meta");
1856            return -1;
1857        }
1858        int32_t fileMetaRate;
1859        if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) {
1860            return -1;
1861        }
1862        return fileMetaRate;
1863    }
1864    return rate;
1865}
1866
1867void NuPlayer::schedulePollDuration() {
1868    sp<AMessage> msg = new AMessage(kWhatPollDuration, this);
1869    msg->setInt32("generation", mPollDurationGeneration);
1870    msg->post();
1871}
1872
1873void NuPlayer::cancelPollDuration() {
1874    ++mPollDurationGeneration;
1875}
1876
1877void NuPlayer::processDeferredActions() {
1878    while (!mDeferredActions.empty()) {
1879        // We won't execute any deferred actions until we're no longer in
1880        // an intermediate state, i.e. one more more decoders are currently
1881        // flushing or shutting down.
1882
1883        if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1884            // We're currently flushing, postpone the reset until that's
1885            // completed.
1886
1887            ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1888                  mFlushingAudio, mFlushingVideo);
1889
1890            break;
1891        }
1892
1893        sp<Action> action = *mDeferredActions.begin();
1894        mDeferredActions.erase(mDeferredActions.begin());
1895
1896        action->execute(this);
1897    }
1898}
1899
1900void NuPlayer::performSeek(int64_t seekTimeUs) {
1901    ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1902          (long long)seekTimeUs,
1903          seekTimeUs / 1E6);
1904
1905    if (mSource == NULL) {
1906        // This happens when reset occurs right before the loop mode
1907        // asynchronously seeks to the start of the stream.
1908        LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1909                "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1910                mAudioDecoder.get(), mVideoDecoder.get());
1911        return;
1912    }
1913    mPreviousSeekTimeUs = seekTimeUs;
1914    mSource->seekTo(seekTimeUs);
1915    mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
1916    ++mTimedTextGeneration;
1917
1918    // everything's flushed, continue playback.
1919}
1920
1921void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1922    ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
1923
1924    if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1925            && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
1926        return;
1927    }
1928
1929    if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1930        flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
1931    }
1932
1933    if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1934        flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
1935    }
1936}
1937
1938void NuPlayer::performReset() {
1939    ALOGV("performReset");
1940
1941    CHECK(mAudioDecoder == NULL);
1942    CHECK(mVideoDecoder == NULL);
1943
1944    cancelPollDuration();
1945
1946    ++mScanSourcesGeneration;
1947    mScanSourcesPending = false;
1948
1949    if (mRendererLooper != NULL) {
1950        if (mRenderer != NULL) {
1951            mRendererLooper->unregisterHandler(mRenderer->id());
1952        }
1953        mRendererLooper->stop();
1954        mRendererLooper.clear();
1955    }
1956    mRenderer.clear();
1957    ++mRendererGeneration;
1958
1959    if (mSource != NULL) {
1960        mSource->stop();
1961
1962        mSource.clear();
1963    }
1964
1965    if (mDriver != NULL) {
1966        sp<NuPlayerDriver> driver = mDriver.promote();
1967        if (driver != NULL) {
1968            driver->notifyResetComplete();
1969        }
1970    }
1971
1972    mStarted = false;
1973    mResetting = false;
1974    mSourceStarted = false;
1975}
1976
1977void NuPlayer::performScanSources() {
1978    ALOGV("performScanSources");
1979
1980    if (!mStarted) {
1981        return;
1982    }
1983
1984    if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1985        postScanSources();
1986    }
1987}
1988
1989void NuPlayer::performSetSurface(const sp<Surface> &surface) {
1990    ALOGV("performSetSurface");
1991
1992    mSurface = surface;
1993
1994    // XXX - ignore error from setVideoScalingMode for now
1995    setVideoScalingMode(mVideoScalingMode);
1996
1997    if (mDriver != NULL) {
1998        sp<NuPlayerDriver> driver = mDriver.promote();
1999        if (driver != NULL) {
2000            driver->notifySetSurfaceComplete();
2001        }
2002    }
2003}
2004
2005void NuPlayer::performResumeDecoders(bool needNotify) {
2006    if (needNotify) {
2007        mResumePending = true;
2008        if (mVideoDecoder == NULL) {
2009            // if audio-only, we can notify seek complete now,
2010            // as the resume operation will be relatively fast.
2011            finishResume();
2012        }
2013    }
2014
2015    if (mVideoDecoder != NULL) {
2016        // When there is continuous seek, MediaPlayer will cache the seek
2017        // position, and send down new seek request when previous seek is
2018        // complete. Let's wait for at least one video output frame before
2019        // notifying seek complete, so that the video thumbnail gets updated
2020        // when seekbar is dragged.
2021        mVideoDecoder->signalResume(needNotify);
2022    }
2023
2024    if (mAudioDecoder != NULL) {
2025        mAudioDecoder->signalResume(false /* needNotify */);
2026    }
2027}
2028
2029void NuPlayer::finishResume() {
2030    if (mResumePending) {
2031        mResumePending = false;
2032        notifyDriverSeekComplete();
2033    }
2034}
2035
2036void NuPlayer::notifyDriverSeekComplete() {
2037    if (mDriver != NULL) {
2038        sp<NuPlayerDriver> driver = mDriver.promote();
2039        if (driver != NULL) {
2040            driver->notifySeekComplete();
2041        }
2042    }
2043}
2044
2045void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
2046    int32_t what;
2047    CHECK(msg->findInt32("what", &what));
2048
2049    switch (what) {
2050        case Source::kWhatInstantiateSecureDecoders:
2051        {
2052            if (mSource == NULL) {
2053                // This is a stale notification from a source that was
2054                // asynchronously preparing when the client called reset().
2055                // We handled the reset, the source is gone.
2056                break;
2057            }
2058
2059            sp<AMessage> reply;
2060            CHECK(msg->findMessage("reply", &reply));
2061            status_t err = onInstantiateSecureDecoders();
2062            reply->setInt32("err", err);
2063            reply->post();
2064            break;
2065        }
2066
2067        case Source::kWhatPrepared:
2068        {
2069            if (mSource == NULL) {
2070                // This is a stale notification from a source that was
2071                // asynchronously preparing when the client called reset().
2072                // We handled the reset, the source is gone.
2073                break;
2074            }
2075
2076            int32_t err;
2077            CHECK(msg->findInt32("err", &err));
2078
2079            if (err != OK) {
2080                // shut down potential secure codecs in case client never calls reset
2081                mDeferredActions.push_back(
2082                        new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */,
2083                                               FLUSH_CMD_SHUTDOWN /* video */));
2084                processDeferredActions();
2085            }
2086
2087            sp<NuPlayerDriver> driver = mDriver.promote();
2088            if (driver != NULL) {
2089                // notify duration first, so that it's definitely set when
2090                // the app received the "prepare complete" callback.
2091                int64_t durationUs;
2092                if (mSource->getDuration(&durationUs) == OK) {
2093                    driver->notifyDuration(durationUs);
2094                }
2095                driver->notifyPrepareCompleted(err);
2096            }
2097
2098            break;
2099        }
2100
2101        case Source::kWhatFlagsChanged:
2102        {
2103            uint32_t flags;
2104            CHECK(msg->findInt32("flags", (int32_t *)&flags));
2105
2106            sp<NuPlayerDriver> driver = mDriver.promote();
2107            if (driver != NULL) {
2108                if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
2109                    driver->notifyListener(
2110                            MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
2111                }
2112                driver->notifyFlagsChanged(flags);
2113            }
2114
2115            if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2116                    && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
2117                cancelPollDuration();
2118            } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
2119                    && (flags & Source::FLAG_DYNAMIC_DURATION)
2120                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
2121                schedulePollDuration();
2122            }
2123
2124            mSourceFlags = flags;
2125            break;
2126        }
2127
2128        case Source::kWhatVideoSizeChanged:
2129        {
2130            sp<AMessage> format;
2131            CHECK(msg->findMessage("format", &format));
2132
2133            updateVideoSize(format);
2134            break;
2135        }
2136
2137        case Source::kWhatBufferingUpdate:
2138        {
2139            int32_t percentage;
2140            CHECK(msg->findInt32("percentage", &percentage));
2141
2142            notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
2143            break;
2144        }
2145
2146        case Source::kWhatPauseOnBufferingStart:
2147        {
2148            // ignore if not playing
2149            if (mStarted) {
2150                ALOGI("buffer low, pausing...");
2151
2152                mPausedForBuffering = true;
2153                onPause();
2154            }
2155            // fall-thru
2156        }
2157
2158        case Source::kWhatBufferingStart:
2159        {
2160            if (mPausedByClient) {
2161                mPendingBufferingFlag = PENDING_BUFFERING_FLAG_START;
2162            } else {
2163                notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
2164                mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
2165            }
2166            break;
2167        }
2168
2169        case Source::kWhatResumeOnBufferingEnd:
2170        {
2171            // ignore if not playing
2172            if (mStarted) {
2173                ALOGI("buffer ready, resuming...");
2174
2175                mPausedForBuffering = false;
2176
2177                // do not resume yet if client didn't unpause
2178                if (!mPausedByClient) {
2179                    onResume();
2180                }
2181            }
2182            // fall-thru
2183        }
2184
2185        case Source::kWhatBufferingEnd:
2186        {
2187            if (mPausedByClient) {
2188                mPendingBufferingFlag = PENDING_BUFFERING_FLAG_END;
2189            } else {
2190                notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
2191                mPendingBufferingFlag = PENDING_BUFFERING_FLAG_NONE;
2192            }
2193            break;
2194        }
2195
2196        case Source::kWhatCacheStats:
2197        {
2198            int32_t kbps;
2199            CHECK(msg->findInt32("bandwidth", &kbps));
2200
2201            notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
2202            break;
2203        }
2204
2205        case Source::kWhatSubtitleData:
2206        {
2207            sp<ABuffer> buffer;
2208            CHECK(msg->findBuffer("buffer", &buffer));
2209
2210            sendSubtitleData(buffer, 0 /* baseIndex */);
2211            break;
2212        }
2213
2214        case Source::kWhatTimedMetaData:
2215        {
2216            sp<ABuffer> buffer;
2217            if (!msg->findBuffer("buffer", &buffer)) {
2218                notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2219            } else {
2220                sendTimedMetaData(buffer);
2221            }
2222            break;
2223        }
2224
2225        case Source::kWhatTimedTextData:
2226        {
2227            int32_t generation;
2228            if (msg->findInt32("generation", &generation)
2229                    && generation != mTimedTextGeneration) {
2230                break;
2231            }
2232
2233            sp<ABuffer> buffer;
2234            CHECK(msg->findBuffer("buffer", &buffer));
2235
2236            sp<NuPlayerDriver> driver = mDriver.promote();
2237            if (driver == NULL) {
2238                break;
2239            }
2240
2241            int posMs;
2242            int64_t timeUs, posUs;
2243            driver->getCurrentPosition(&posMs);
2244            posUs = (int64_t) posMs * 1000ll;
2245            CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2246
2247            if (posUs < timeUs) {
2248                if (!msg->findInt32("generation", &generation)) {
2249                    msg->setInt32("generation", mTimedTextGeneration);
2250                }
2251                msg->post(timeUs - posUs);
2252            } else {
2253                sendTimedTextData(buffer);
2254            }
2255            break;
2256        }
2257
2258        case Source::kWhatQueueDecoderShutdown:
2259        {
2260            int32_t audio, video;
2261            CHECK(msg->findInt32("audio", &audio));
2262            CHECK(msg->findInt32("video", &video));
2263
2264            sp<AMessage> reply;
2265            CHECK(msg->findMessage("reply", &reply));
2266
2267            queueDecoderShutdown(audio, video, reply);
2268            break;
2269        }
2270
2271        case Source::kWhatDrmNoLicense:
2272        {
2273            notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
2274            break;
2275        }
2276
2277        default:
2278            TRESPASS();
2279    }
2280}
2281
2282void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
2283    int32_t what;
2284    CHECK(msg->findInt32("what", &what));
2285
2286    switch (what) {
2287        case NuPlayer::CCDecoder::kWhatClosedCaptionData:
2288        {
2289            sp<ABuffer> buffer;
2290            CHECK(msg->findBuffer("buffer", &buffer));
2291
2292            size_t inbandTracks = 0;
2293            if (mSource != NULL) {
2294                inbandTracks = mSource->getTrackCount();
2295            }
2296
2297            sendSubtitleData(buffer, inbandTracks);
2298            break;
2299        }
2300
2301        case NuPlayer::CCDecoder::kWhatTrackAdded:
2302        {
2303            notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
2304
2305            break;
2306        }
2307
2308        default:
2309            TRESPASS();
2310    }
2311
2312
2313}
2314
2315void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
2316    int32_t trackIndex;
2317    int64_t timeUs, durationUs;
2318    CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
2319    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2320    CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
2321
2322    Parcel in;
2323    in.writeInt32(trackIndex + baseIndex);
2324    in.writeInt64(timeUs);
2325    in.writeInt64(durationUs);
2326    in.writeInt32(buffer->size());
2327    in.writeInt32(buffer->size());
2328    in.write(buffer->data(), buffer->size());
2329
2330    notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
2331}
2332
2333void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) {
2334    int64_t timeUs;
2335    CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2336
2337    Parcel in;
2338    in.writeInt64(timeUs);
2339    in.writeInt32(buffer->size());
2340    in.writeInt32(buffer->size());
2341    in.write(buffer->data(), buffer->size());
2342
2343    notifyListener(MEDIA_META_DATA, 0, 0, &in);
2344}
2345
2346void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
2347    const void *data;
2348    size_t size = 0;
2349    int64_t timeUs;
2350    int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP;
2351
2352    AString mime;
2353    CHECK(buffer->meta()->findString("mime", &mime));
2354    CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
2355
2356    data = buffer->data();
2357    size = buffer->size();
2358
2359    Parcel parcel;
2360    if (size > 0) {
2361        CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
2362        int32_t global = 0;
2363        if (buffer->meta()->findInt32("global", &global) && global) {
2364            flag |= TextDescriptions::GLOBAL_DESCRIPTIONS;
2365        } else {
2366            flag |= TextDescriptions::LOCAL_DESCRIPTIONS;
2367        }
2368        TextDescriptions::getParcelOfDescriptions(
2369                (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
2370    }
2371
2372    if ((parcel.dataSize() > 0)) {
2373        notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
2374    } else {  // send an empty timed text
2375        notifyListener(MEDIA_TIMED_TEXT, 0, 0);
2376    }
2377}
2378////////////////////////////////////////////////////////////////////////////////
2379
2380sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
2381    sp<MetaData> meta = getFormatMeta(audio);
2382
2383    if (meta == NULL) {
2384        return NULL;
2385    }
2386
2387    sp<AMessage> msg = new AMessage;
2388
2389    if(convertMetaDataToMessage(meta, &msg) == OK) {
2390        return msg;
2391    }
2392    return NULL;
2393}
2394
2395void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
2396    sp<AMessage> notify = dupNotify();
2397    notify->setInt32("what", kWhatFlagsChanged);
2398    notify->setInt32("flags", flags);
2399    notify->post();
2400}
2401
2402void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
2403    sp<AMessage> notify = dupNotify();
2404    notify->setInt32("what", kWhatVideoSizeChanged);
2405    notify->setMessage("format", format);
2406    notify->post();
2407}
2408
2409void NuPlayer::Source::notifyPrepared(status_t err) {
2410    sp<AMessage> notify = dupNotify();
2411    notify->setInt32("what", kWhatPrepared);
2412    notify->setInt32("err", err);
2413    notify->post();
2414}
2415
2416void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) {
2417    sp<AMessage> notify = dupNotify();
2418    notify->setInt32("what", kWhatInstantiateSecureDecoders);
2419    notify->setMessage("reply", reply);
2420    notify->post();
2421}
2422
2423void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
2424    TRESPASS();
2425}
2426
2427}  // namespace android
2428