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