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