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