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