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