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