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