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