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