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