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 #include <utils/Log.h>
20 
21 #include "NuPlayer.h"
22 
23 #include "HTTPLiveSource.h"
24 #include "NuPlayerCCDecoder.h"
25 #include "NuPlayerDecoder.h"
26 #include "NuPlayerDecoderBase.h"
27 #include "NuPlayerDecoderPassThrough.h"
28 #include "NuPlayerDriver.h"
29 #include "NuPlayerRenderer.h"
30 #include "NuPlayerSource.h"
31 #include "RTSPSource.h"
32 #include "StreamingSource.h"
33 #include "GenericSource.h"
34 #include "TextDescriptions.h"
35 
36 #include "ATSParser.h"
37 
38 #include <cutils/properties.h>
39 
40 #include <media/stagefright/foundation/hexdump.h>
41 #include <media/stagefright/foundation/ABuffer.h>
42 #include <media/stagefright/foundation/ADebug.h>
43 #include <media/stagefright/foundation/AMessage.h>
44 #include <media/stagefright/MediaBuffer.h>
45 #include <media/stagefright/MediaDefs.h>
46 #include <media/stagefright/MediaErrors.h>
47 #include <media/stagefright/MetaData.h>
48 #include <gui/IGraphicBufferProducer.h>
49 
50 #include "avc_utils.h"
51 
52 #include "ESDS.h"
53 #include <media/stagefright/Utils.h>
54 
55 namespace android {
56 
57 struct NuPlayer::Action : public RefBase {
Actionandroid::NuPlayer::Action58     Action() {}
59 
60     virtual void execute(NuPlayer *player) = 0;
61 
62 private:
63     DISALLOW_EVIL_CONSTRUCTORS(Action);
64 };
65 
66 struct NuPlayer::SeekAction : public Action {
SeekActionandroid::NuPlayer::SeekAction67     SeekAction(int64_t seekTimeUs, bool needNotify)
68         : mSeekTimeUs(seekTimeUs),
69           mNeedNotify(needNotify) {
70     }
71 
executeandroid::NuPlayer::SeekAction72     virtual void execute(NuPlayer *player) {
73         player->performSeek(mSeekTimeUs, mNeedNotify);
74     }
75 
76 private:
77     int64_t mSeekTimeUs;
78     bool mNeedNotify;
79 
80     DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
81 };
82 
83 struct NuPlayer::ResumeDecoderAction : public Action {
ResumeDecoderActionandroid::NuPlayer::ResumeDecoderAction84     ResumeDecoderAction(bool needNotify)
85         : mNeedNotify(needNotify) {
86     }
87 
executeandroid::NuPlayer::ResumeDecoderAction88     virtual void execute(NuPlayer *player) {
89         player->performResumeDecoders(mNeedNotify);
90     }
91 
92 private:
93     bool mNeedNotify;
94 
95     DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction);
96 };
97 
98 struct NuPlayer::SetSurfaceAction : public Action {
SetSurfaceActionandroid::NuPlayer::SetSurfaceAction99     SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
100         : mWrapper(wrapper) {
101     }
102 
executeandroid::NuPlayer::SetSurfaceAction103     virtual void execute(NuPlayer *player) {
104         player->performSetSurface(mWrapper);
105     }
106 
107 private:
108     sp<NativeWindowWrapper> mWrapper;
109 
110     DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
111 };
112 
113 struct NuPlayer::FlushDecoderAction : public Action {
FlushDecoderActionandroid::NuPlayer::FlushDecoderAction114     FlushDecoderAction(FlushCommand audio, FlushCommand video)
115         : mAudio(audio),
116           mVideo(video) {
117     }
118 
executeandroid::NuPlayer::FlushDecoderAction119     virtual void execute(NuPlayer *player) {
120         player->performDecoderFlush(mAudio, mVideo);
121     }
122 
123 private:
124     FlushCommand mAudio;
125     FlushCommand mVideo;
126 
127     DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction);
128 };
129 
130 struct NuPlayer::PostMessageAction : public Action {
PostMessageActionandroid::NuPlayer::PostMessageAction131     PostMessageAction(const sp<AMessage> &msg)
132         : mMessage(msg) {
133     }
134 
executeandroid::NuPlayer::PostMessageAction135     virtual void execute(NuPlayer *) {
136         mMessage->post();
137     }
138 
139 private:
140     sp<AMessage> mMessage;
141 
142     DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
143 };
144 
145 // Use this if there's no state necessary to save in order to execute
146 // the action.
147 struct NuPlayer::SimpleAction : public Action {
148     typedef void (NuPlayer::*ActionFunc)();
149 
SimpleActionandroid::NuPlayer::SimpleAction150     SimpleAction(ActionFunc func)
151         : mFunc(func) {
152     }
153 
executeandroid::NuPlayer::SimpleAction154     virtual void execute(NuPlayer *player) {
155         (player->*mFunc)();
156     }
157 
158 private:
159     ActionFunc mFunc;
160 
161     DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
162 };
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 
NuPlayer()166 NuPlayer::NuPlayer()
167     : mUIDValid(false),
168       mSourceFlags(0),
169       mOffloadAudio(false),
170       mAudioDecoderGeneration(0),
171       mVideoDecoderGeneration(0),
172       mRendererGeneration(0),
173       mAudioEOS(false),
174       mVideoEOS(false),
175       mScanSourcesPending(false),
176       mScanSourcesGeneration(0),
177       mPollDurationGeneration(0),
178       mTimedTextGeneration(0),
179       mFlushingAudio(NONE),
180       mFlushingVideo(NONE),
181       mResumePending(false),
182       mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
183       mStarted(false),
184       mPaused(false),
185       mPausedByClient(false) {
186     clearFlushComplete();
187 }
188 
~NuPlayer()189 NuPlayer::~NuPlayer() {
190 }
191 
setUID(uid_t uid)192 void NuPlayer::setUID(uid_t uid) {
193     mUIDValid = true;
194     mUID = uid;
195 }
196 
setDriver(const wp<NuPlayerDriver> & driver)197 void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
198     mDriver = driver;
199 }
200 
setDataSourceAsync(const sp<IStreamSource> & source)201 void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
202     sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
203 
204     sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
205 
206     msg->setObject("source", new StreamingSource(notify, source));
207     msg->post();
208 }
209 
IsHTTPLiveURL(const char * url)210 static bool IsHTTPLiveURL(const char *url) {
211     if (!strncasecmp("http://", url, 7)
212             || !strncasecmp("https://", url, 8)
213             || !strncasecmp("file://", url, 7)) {
214         size_t len = strlen(url);
215         if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
216             return true;
217         }
218 
219         if (strstr(url,"m3u8")) {
220             return true;
221         }
222     }
223 
224     return false;
225 }
226 
setDataSourceAsync(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)227 void NuPlayer::setDataSourceAsync(
228         const sp<IMediaHTTPService> &httpService,
229         const char *url,
230         const KeyedVector<String8, String8> *headers) {
231 
232     sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
233     size_t len = strlen(url);
234 
235     sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
236 
237     sp<Source> source;
238     if (IsHTTPLiveURL(url)) {
239         source = new HTTPLiveSource(notify, httpService, url, headers);
240     } else if (!strncasecmp(url, "rtsp://", 7)) {
241         source = new RTSPSource(
242                 notify, httpService, url, headers, mUIDValid, mUID);
243     } else if ((!strncasecmp(url, "http://", 7)
244                 || !strncasecmp(url, "https://", 8))
245                     && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
246                     || strstr(url, ".sdp?"))) {
247         source = new RTSPSource(
248                 notify, httpService, url, headers, mUIDValid, mUID, true);
249     } else {
250         sp<GenericSource> genericSource =
251                 new GenericSource(notify, mUIDValid, mUID);
252         // Don't set FLAG_SECURE on mSourceFlags here for widevine.
253         // The correct flags will be updated in Source::kWhatFlagsChanged
254         // handler when  GenericSource is prepared.
255 
256         status_t err = genericSource->setDataSource(httpService, url, headers);
257 
258         if (err == OK) {
259             source = genericSource;
260         } else {
261             ALOGE("Failed to set data source!");
262         }
263     }
264     msg->setObject("source", source);
265     msg->post();
266 }
267 
setDataSourceAsync(int fd,int64_t offset,int64_t length)268 void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
269     sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
270 
271     sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
272 
273     sp<GenericSource> source =
274             new GenericSource(notify, mUIDValid, mUID);
275 
276     status_t err = source->setDataSource(fd, offset, length);
277 
278     if (err != OK) {
279         ALOGE("Failed to set data source!");
280         source = NULL;
281     }
282 
283     msg->setObject("source", source);
284     msg->post();
285 }
286 
prepareAsync()287 void NuPlayer::prepareAsync() {
288     (new AMessage(kWhatPrepare, id()))->post();
289 }
290 
setVideoSurfaceTextureAsync(const sp<IGraphicBufferProducer> & bufferProducer)291 void NuPlayer::setVideoSurfaceTextureAsync(
292         const sp<IGraphicBufferProducer> &bufferProducer) {
293     sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
294 
295     if (bufferProducer == NULL) {
296         msg->setObject("native-window", NULL);
297     } else {
298         msg->setObject(
299                 "native-window",
300                 new NativeWindowWrapper(
301                     new Surface(bufferProducer, true /* controlledByApp */)));
302     }
303 
304     msg->post();
305 }
306 
setAudioSink(const sp<MediaPlayerBase::AudioSink> & sink)307 void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
308     sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
309     msg->setObject("sink", sink);
310     msg->post();
311 }
312 
start()313 void NuPlayer::start() {
314     (new AMessage(kWhatStart, id()))->post();
315 }
316 
pause()317 void NuPlayer::pause() {
318     (new AMessage(kWhatPause, id()))->post();
319 }
320 
resetAsync()321 void NuPlayer::resetAsync() {
322     if (mSource != NULL) {
323         // During a reset, the data source might be unresponsive already, we need to
324         // disconnect explicitly so that reads exit promptly.
325         // We can't queue the disconnect request to the looper, as it might be
326         // queued behind a stuck read and never gets processed.
327         // Doing a disconnect outside the looper to allows the pending reads to exit
328         // (either successfully or with error).
329         mSource->disconnect();
330     }
331 
332     (new AMessage(kWhatReset, id()))->post();
333 }
334 
seekToAsync(int64_t seekTimeUs,bool needNotify)335 void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) {
336     sp<AMessage> msg = new AMessage(kWhatSeek, id());
337     msg->setInt64("seekTimeUs", seekTimeUs);
338     msg->setInt32("needNotify", needNotify);
339     msg->post();
340 }
341 
342 
writeTrackInfo(Parcel * reply,const sp<AMessage> format) const343 void NuPlayer::writeTrackInfo(
344         Parcel* reply, const sp<AMessage> format) const {
345     int32_t trackType;
346     CHECK(format->findInt32("type", &trackType));
347 
348     AString lang;
349     CHECK(format->findString("language", &lang));
350 
351     reply->writeInt32(2); // write something non-zero
352     reply->writeInt32(trackType);
353     reply->writeString16(String16(lang.c_str()));
354 
355     if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
356         AString mime;
357         CHECK(format->findString("mime", &mime));
358 
359         int32_t isAuto, isDefault, isForced;
360         CHECK(format->findInt32("auto", &isAuto));
361         CHECK(format->findInt32("default", &isDefault));
362         CHECK(format->findInt32("forced", &isForced));
363 
364         reply->writeString16(String16(mime.c_str()));
365         reply->writeInt32(isAuto);
366         reply->writeInt32(isDefault);
367         reply->writeInt32(isForced);
368     }
369 }
370 
onMessageReceived(const sp<AMessage> & msg)371 void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
372     switch (msg->what()) {
373         case kWhatSetDataSource:
374         {
375             ALOGV("kWhatSetDataSource");
376 
377             CHECK(mSource == NULL);
378 
379             status_t err = OK;
380             sp<RefBase> obj;
381             CHECK(msg->findObject("source", &obj));
382             if (obj != NULL) {
383                 mSource = static_cast<Source *>(obj.get());
384             } else {
385                 err = UNKNOWN_ERROR;
386             }
387 
388             CHECK(mDriver != NULL);
389             sp<NuPlayerDriver> driver = mDriver.promote();
390             if (driver != NULL) {
391                 driver->notifySetDataSourceCompleted(err);
392             }
393             break;
394         }
395 
396         case kWhatPrepare:
397         {
398             mSource->prepareAsync();
399             break;
400         }
401 
402         case kWhatGetTrackInfo:
403         {
404             uint32_t replyID;
405             CHECK(msg->senderAwaitsResponse(&replyID));
406 
407             Parcel* reply;
408             CHECK(msg->findPointer("reply", (void**)&reply));
409 
410             size_t inbandTracks = 0;
411             if (mSource != NULL) {
412                 inbandTracks = mSource->getTrackCount();
413             }
414 
415             size_t ccTracks = 0;
416             if (mCCDecoder != NULL) {
417                 ccTracks = mCCDecoder->getTrackCount();
418             }
419 
420             // total track count
421             reply->writeInt32(inbandTracks + ccTracks);
422 
423             // write inband tracks
424             for (size_t i = 0; i < inbandTracks; ++i) {
425                 writeTrackInfo(reply, mSource->getTrackInfo(i));
426             }
427 
428             // write CC track
429             for (size_t i = 0; i < ccTracks; ++i) {
430                 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i));
431             }
432 
433             sp<AMessage> response = new AMessage;
434             response->postReply(replyID);
435             break;
436         }
437 
438         case kWhatGetSelectedTrack:
439         {
440             status_t err = INVALID_OPERATION;
441             if (mSource != NULL) {
442                 err = OK;
443 
444                 int32_t type32;
445                 CHECK(msg->findInt32("type", (int32_t*)&type32));
446                 media_track_type type = (media_track_type)type32;
447                 ssize_t selectedTrack = mSource->getSelectedTrack(type);
448 
449                 Parcel* reply;
450                 CHECK(msg->findPointer("reply", (void**)&reply));
451                 reply->writeInt32(selectedTrack);
452             }
453 
454             sp<AMessage> response = new AMessage;
455             response->setInt32("err", err);
456 
457             uint32_t replyID;
458             CHECK(msg->senderAwaitsResponse(&replyID));
459             response->postReply(replyID);
460             break;
461         }
462 
463         case kWhatSelectTrack:
464         {
465             uint32_t replyID;
466             CHECK(msg->senderAwaitsResponse(&replyID));
467 
468             size_t trackIndex;
469             int32_t select;
470             int64_t timeUs;
471             CHECK(msg->findSize("trackIndex", &trackIndex));
472             CHECK(msg->findInt32("select", &select));
473             CHECK(msg->findInt64("timeUs", &timeUs));
474 
475             status_t err = INVALID_OPERATION;
476 
477             size_t inbandTracks = 0;
478             if (mSource != NULL) {
479                 inbandTracks = mSource->getTrackCount();
480             }
481             size_t ccTracks = 0;
482             if (mCCDecoder != NULL) {
483                 ccTracks = mCCDecoder->getTrackCount();
484             }
485 
486             if (trackIndex < inbandTracks) {
487                 err = mSource->selectTrack(trackIndex, select, timeUs);
488 
489                 if (!select && err == OK) {
490                     int32_t type;
491                     sp<AMessage> info = mSource->getTrackInfo(trackIndex);
492                     if (info != NULL
493                             && info->findInt32("type", &type)
494                             && type == MEDIA_TRACK_TYPE_TIMEDTEXT) {
495                         ++mTimedTextGeneration;
496                     }
497                 }
498             } else {
499                 trackIndex -= inbandTracks;
500 
501                 if (trackIndex < ccTracks) {
502                     err = mCCDecoder->selectTrack(trackIndex, select);
503                 }
504             }
505 
506             sp<AMessage> response = new AMessage;
507             response->setInt32("err", err);
508 
509             response->postReply(replyID);
510             break;
511         }
512 
513         case kWhatPollDuration:
514         {
515             int32_t generation;
516             CHECK(msg->findInt32("generation", &generation));
517 
518             if (generation != mPollDurationGeneration) {
519                 // stale
520                 break;
521             }
522 
523             int64_t durationUs;
524             if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
525                 sp<NuPlayerDriver> driver = mDriver.promote();
526                 if (driver != NULL) {
527                     driver->notifyDuration(durationUs);
528                 }
529             }
530 
531             msg->post(1000000ll);  // poll again in a second.
532             break;
533         }
534 
535         case kWhatSetVideoNativeWindow:
536         {
537             ALOGV("kWhatSetVideoNativeWindow");
538 
539             sp<RefBase> obj;
540             CHECK(msg->findObject("native-window", &obj));
541 
542             if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) {
543                 performSetSurface(static_cast<NativeWindowWrapper *>(obj.get()));
544                 break;
545             }
546 
547             mDeferredActions.push_back(
548                     new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
549                                            FLUSH_CMD_SHUTDOWN /* video */));
550 
551             mDeferredActions.push_back(
552                     new SetSurfaceAction(
553                         static_cast<NativeWindowWrapper *>(obj.get())));
554 
555             if (obj != NULL) {
556                 if (mStarted) {
557                     // Issue a seek to refresh the video screen only if started otherwise
558                     // the extractor may not yet be started and will assert.
559                     // If the video decoder is not set (perhaps audio only in this case)
560                     // do not perform a seek as it is not needed.
561                     int64_t currentPositionUs = 0;
562                     if (getCurrentPosition(&currentPositionUs) == OK) {
563                         mDeferredActions.push_back(
564                                 new SeekAction(currentPositionUs, false /* needNotify */));
565                     }
566                 }
567 
568                 // If there is a new surface texture, instantiate decoders
569                 // again if possible.
570                 mDeferredActions.push_back(
571                         new SimpleAction(&NuPlayer::performScanSources));
572             }
573 
574             // After a flush without shutdown, decoder is paused.
575             // Don't resume it until source seek is done, otherwise it could
576             // start pulling stale data too soon.
577             mDeferredActions.push_back(
578                     new ResumeDecoderAction(false /* needNotify */));
579 
580             processDeferredActions();
581             break;
582         }
583 
584         case kWhatSetAudioSink:
585         {
586             ALOGV("kWhatSetAudioSink");
587 
588             sp<RefBase> obj;
589             CHECK(msg->findObject("sink", &obj));
590 
591             mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
592             break;
593         }
594 
595         case kWhatStart:
596         {
597             ALOGV("kWhatStart");
598             if (mStarted) {
599                 onResume();
600             } else {
601                 onStart();
602             }
603             mPausedByClient = false;
604             break;
605         }
606 
607         case kWhatScanSources:
608         {
609             int32_t generation;
610             CHECK(msg->findInt32("generation", &generation));
611             if (generation != mScanSourcesGeneration) {
612                 // Drop obsolete msg.
613                 break;
614             }
615 
616             mScanSourcesPending = false;
617 
618             ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
619                  mAudioDecoder != NULL, mVideoDecoder != NULL);
620 
621             bool mHadAnySourcesBefore =
622                 (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
623 
624             // initialize video before audio because successful initialization of
625             // video may change deep buffer mode of audio.
626             if (mNativeWindow != NULL) {
627                 instantiateDecoder(false, &mVideoDecoder);
628             }
629 
630             // Don't try to re-open audio sink if there's an existing decoder.
631             if (mAudioSink != NULL && mAudioDecoder == NULL) {
632                 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
633                 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
634                 audio_stream_type_t streamType = mAudioSink->getAudioStreamType();
635                 const bool hasVideo = (videoFormat != NULL);
636                 const bool canOffload = canOffloadStream(
637                         audioMeta, hasVideo, true /* is_streaming */, streamType);
638                 if (canOffload) {
639                     if (!mOffloadAudio) {
640                         mRenderer->signalEnableOffloadAudio();
641                     }
642                     // open audio sink early under offload mode.
643                     sp<AMessage> format = mSource->getFormat(true /*audio*/);
644                     tryOpenAudioSinkForOffload(format, hasVideo);
645                 }
646                 instantiateDecoder(true, &mAudioDecoder);
647             }
648 
649             if (!mHadAnySourcesBefore
650                     && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
651                 // This is the first time we've found anything playable.
652 
653                 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
654                     schedulePollDuration();
655                 }
656             }
657 
658             status_t err;
659             if ((err = mSource->feedMoreTSData()) != OK) {
660                 if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
661                     // We're not currently decoding anything (no audio or
662                     // video tracks found) and we just ran out of input data.
663 
664                     if (err == ERROR_END_OF_STREAM) {
665                         notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
666                     } else {
667                         notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
668                     }
669                 }
670                 break;
671             }
672 
673             if ((mAudioDecoder == NULL && mAudioSink != NULL)
674                     || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
675                 msg->post(100000ll);
676                 mScanSourcesPending = true;
677             }
678             break;
679         }
680 
681         case kWhatVideoNotify:
682         case kWhatAudioNotify:
683         {
684             bool audio = msg->what() == kWhatAudioNotify;
685 
686             int32_t currentDecoderGeneration =
687                 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration);
688             int32_t requesterGeneration = currentDecoderGeneration - 1;
689             CHECK(msg->findInt32("generation", &requesterGeneration));
690 
691             if (requesterGeneration != currentDecoderGeneration) {
692                 ALOGV("got message from old %s decoder, generation(%d:%d)",
693                         audio ? "audio" : "video", requesterGeneration,
694                         currentDecoderGeneration);
695                 sp<AMessage> reply;
696                 if (!(msg->findMessage("reply", &reply))) {
697                     return;
698                 }
699 
700                 reply->setInt32("err", INFO_DISCONTINUITY);
701                 reply->post();
702                 return;
703             }
704 
705             int32_t what;
706             CHECK(msg->findInt32("what", &what));
707 
708             if (what == DecoderBase::kWhatInputDiscontinuity) {
709                 int32_t formatChange;
710                 CHECK(msg->findInt32("formatChange", &formatChange));
711 
712                 ALOGV("%s discontinuity: formatChange %d",
713                         audio ? "audio" : "video", formatChange);
714 
715                 if (formatChange) {
716                     mDeferredActions.push_back(
717                             new FlushDecoderAction(
718                                 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
719                                 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
720                 }
721 
722                 mDeferredActions.push_back(
723                         new SimpleAction(
724                                 &NuPlayer::performScanSources));
725 
726                 processDeferredActions();
727             } else if (what == DecoderBase::kWhatEOS) {
728                 int32_t err;
729                 CHECK(msg->findInt32("err", &err));
730 
731                 if (err == ERROR_END_OF_STREAM) {
732                     ALOGV("got %s decoder EOS", audio ? "audio" : "video");
733                 } else {
734                     ALOGV("got %s decoder EOS w/ error %d",
735                          audio ? "audio" : "video",
736                          err);
737                 }
738 
739                 mRenderer->queueEOS(audio, err);
740             } else if (what == DecoderBase::kWhatFlushCompleted) {
741                 ALOGV("decoder %s flush completed", audio ? "audio" : "video");
742 
743                 handleFlushComplete(audio, true /* isDecoder */);
744                 finishFlushIfPossible();
745             } else if (what == DecoderBase::kWhatVideoSizeChanged) {
746                 sp<AMessage> format;
747                 CHECK(msg->findMessage("format", &format));
748 
749                 sp<AMessage> inputFormat =
750                         mSource->getFormat(false /* audio */);
751 
752                 updateVideoSize(inputFormat, format);
753             } else if (what == DecoderBase::kWhatShutdownCompleted) {
754                 ALOGV("%s shutdown completed", audio ? "audio" : "video");
755                 if (audio) {
756                     mAudioDecoder.clear();
757                     ++mAudioDecoderGeneration;
758 
759                     CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
760                     mFlushingAudio = SHUT_DOWN;
761                 } else {
762                     mVideoDecoder.clear();
763                     ++mVideoDecoderGeneration;
764 
765                     CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
766                     mFlushingVideo = SHUT_DOWN;
767                 }
768 
769                 finishFlushIfPossible();
770             } else if (what == DecoderBase::kWhatResumeCompleted) {
771                 finishResume();
772             } else if (what == DecoderBase::kWhatError) {
773                 status_t err;
774                 if (!msg->findInt32("err", &err) || err == OK) {
775                     err = UNKNOWN_ERROR;
776                 }
777 
778                 // Decoder errors can be due to Source (e.g. from streaming),
779                 // or from decoding corrupted bitstreams, or from other decoder
780                 // MediaCodec operations (e.g. from an ongoing reset or seek).
781                 // They may also be due to openAudioSink failure at
782                 // decoder start or after a format change.
783                 //
784                 // We try to gracefully shut down the affected decoder if possible,
785                 // rather than trying to force the shutdown with something
786                 // similar to performReset(). This method can lead to a hang
787                 // if MediaCodec functions block after an error, but they should
788                 // typically return INVALID_OPERATION instead of blocking.
789 
790                 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo;
791                 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down",
792                         err, audio ? "audio" : "video", *flushing);
793 
794                 switch (*flushing) {
795                     case NONE:
796                         mDeferredActions.push_back(
797                                 new FlushDecoderAction(
798                                     audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
799                                     audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN));
800                         processDeferredActions();
801                         break;
802                     case FLUSHING_DECODER:
803                         *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush.
804                         break; // Wait for flush to complete.
805                     case FLUSHING_DECODER_SHUTDOWN:
806                         break; // Wait for flush to complete.
807                     case SHUTTING_DOWN_DECODER:
808                         break; // Wait for shutdown to complete.
809                     case FLUSHED:
810                         // Widevine source reads must stop before releasing the video decoder.
811                         if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
812                             mSource->stop();
813                         }
814                         getDecoder(audio)->initiateShutdown(); // In the middle of a seek.
815                         *flushing = SHUTTING_DOWN_DECODER;     // Shut down.
816                         break;
817                     case SHUT_DOWN:
818                         finishFlushIfPossible();  // Should not occur.
819                         break;                    // Finish anyways.
820                 }
821                 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
822             } else {
823                 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.",
824                       what,
825                       what >> 24,
826                       (what >> 16) & 0xff,
827                       (what >> 8) & 0xff,
828                       what & 0xff);
829             }
830 
831             break;
832         }
833 
834         case kWhatRendererNotify:
835         {
836             int32_t requesterGeneration = mRendererGeneration - 1;
837             CHECK(msg->findInt32("generation", &requesterGeneration));
838             if (requesterGeneration != mRendererGeneration) {
839                 ALOGV("got message from old renderer, generation(%d:%d)",
840                         requesterGeneration, mRendererGeneration);
841                 return;
842             }
843 
844             int32_t what;
845             CHECK(msg->findInt32("what", &what));
846 
847             if (what == Renderer::kWhatEOS) {
848                 int32_t audio;
849                 CHECK(msg->findInt32("audio", &audio));
850 
851                 int32_t finalResult;
852                 CHECK(msg->findInt32("finalResult", &finalResult));
853 
854                 if (audio) {
855                     mAudioEOS = true;
856                 } else {
857                     mVideoEOS = true;
858                 }
859 
860                 if (finalResult == ERROR_END_OF_STREAM) {
861                     ALOGV("reached %s EOS", audio ? "audio" : "video");
862                 } else {
863                     ALOGE("%s track encountered an error (%d)",
864                          audio ? "audio" : "video", finalResult);
865 
866                     notifyListener(
867                             MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
868                 }
869 
870                 if ((mAudioEOS || mAudioDecoder == NULL)
871                         && (mVideoEOS || mVideoDecoder == NULL)) {
872                     notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
873                 }
874             } else if (what == Renderer::kWhatFlushComplete) {
875                 int32_t audio;
876                 CHECK(msg->findInt32("audio", &audio));
877 
878                 ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
879                 handleFlushComplete(audio, false /* isDecoder */);
880                 finishFlushIfPossible();
881             } else if (what == Renderer::kWhatVideoRenderingStart) {
882                 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
883             } else if (what == Renderer::kWhatMediaRenderingStart) {
884                 ALOGV("media rendering started");
885                 notifyListener(MEDIA_STARTED, 0, 0);
886             } else if (what == Renderer::kWhatAudioOffloadTearDown) {
887                 ALOGV("Tear down audio offload, fall back to s/w path if due to error.");
888                 int64_t positionUs;
889                 CHECK(msg->findInt64("positionUs", &positionUs));
890                 int32_t reason;
891                 CHECK(msg->findInt32("reason", &reason));
892                 closeAudioSink();
893                 mAudioDecoder.clear();
894                 ++mAudioDecoderGeneration;
895                 mRenderer->flush(
896                         true /* audio */, false /* notifyComplete */);
897                 if (mVideoDecoder != NULL) {
898                     mRenderer->flush(
899                             false /* audio */, false /* notifyComplete */);
900                 }
901 
902                 performSeek(positionUs, false /* needNotify */);
903                 if (reason == Renderer::kDueToError) {
904                     mRenderer->signalDisableOffloadAudio();
905                     mOffloadAudio = false;
906                     instantiateDecoder(true /* audio */, &mAudioDecoder);
907                 }
908             }
909             break;
910         }
911 
912         case kWhatMoreDataQueued:
913         {
914             break;
915         }
916 
917         case kWhatReset:
918         {
919             ALOGV("kWhatReset");
920 
921             mDeferredActions.push_back(
922                     new FlushDecoderAction(
923                         FLUSH_CMD_SHUTDOWN /* audio */,
924                         FLUSH_CMD_SHUTDOWN /* video */));
925 
926             mDeferredActions.push_back(
927                     new SimpleAction(&NuPlayer::performReset));
928 
929             processDeferredActions();
930             break;
931         }
932 
933         case kWhatSeek:
934         {
935             int64_t seekTimeUs;
936             int32_t needNotify;
937             CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
938             CHECK(msg->findInt32("needNotify", &needNotify));
939 
940             ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d",
941                     seekTimeUs, needNotify);
942 
943             mDeferredActions.push_back(
944                     new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */,
945                                            FLUSH_CMD_FLUSH /* video */));
946 
947             mDeferredActions.push_back(
948                     new SeekAction(seekTimeUs, needNotify));
949 
950             // After a flush without shutdown, decoder is paused.
951             // Don't resume it until source seek is done, otherwise it could
952             // start pulling stale data too soon.
953             mDeferredActions.push_back(
954                     new ResumeDecoderAction(needNotify));
955 
956             processDeferredActions();
957             break;
958         }
959 
960         case kWhatPause:
961         {
962             onPause();
963             mPausedByClient = true;
964             break;
965         }
966 
967         case kWhatSourceNotify:
968         {
969             onSourceNotify(msg);
970             break;
971         }
972 
973         case kWhatClosedCaptionNotify:
974         {
975             onClosedCaptionNotify(msg);
976             break;
977         }
978 
979         default:
980             TRESPASS();
981             break;
982     }
983 }
984 
onResume()985 void NuPlayer::onResume() {
986     if (!mPaused) {
987         return;
988     }
989     mPaused = false;
990     if (mSource != NULL) {
991         mSource->resume();
992     } else {
993         ALOGW("resume called when source is gone or not set");
994     }
995     // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if
996     // needed.
997     if (audioDecoderStillNeeded() && mAudioDecoder == NULL) {
998         instantiateDecoder(true /* audio */, &mAudioDecoder);
999     }
1000     if (mRenderer != NULL) {
1001         mRenderer->resume();
1002     } else {
1003         ALOGW("resume called when renderer is gone or not set");
1004     }
1005 }
1006 
onStart()1007 void NuPlayer::onStart() {
1008     mOffloadAudio = false;
1009     mAudioEOS = false;
1010     mVideoEOS = false;
1011     mStarted = true;
1012 
1013     /* instantiate decoders now for secure playback */
1014     if (mSourceFlags & Source::FLAG_SECURE) {
1015         if (mNativeWindow != NULL) {
1016             instantiateDecoder(false, &mVideoDecoder);
1017         }
1018 
1019         if (mAudioSink != NULL) {
1020             instantiateDecoder(true, &mAudioDecoder);
1021         }
1022     }
1023 
1024     mSource->start();
1025 
1026     uint32_t flags = 0;
1027 
1028     if (mSource->isRealTime()) {
1029         flags |= Renderer::FLAG_REAL_TIME;
1030     }
1031 
1032     sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */);
1033     audio_stream_type_t streamType = AUDIO_STREAM_MUSIC;
1034     if (mAudioSink != NULL) {
1035         streamType = mAudioSink->getAudioStreamType();
1036     }
1037 
1038     sp<AMessage> videoFormat = mSource->getFormat(false /* audio */);
1039 
1040     mOffloadAudio =
1041         canOffloadStream(audioMeta, (videoFormat != NULL),
1042                          true /* is_streaming */, streamType);
1043     if (mOffloadAudio) {
1044         flags |= Renderer::FLAG_OFFLOAD_AUDIO;
1045     }
1046 
1047     sp<AMessage> notify = new AMessage(kWhatRendererNotify, id());
1048     ++mRendererGeneration;
1049     notify->setInt32("generation", mRendererGeneration);
1050     mRenderer = new Renderer(mAudioSink, notify, flags);
1051 
1052     mRendererLooper = new ALooper;
1053     mRendererLooper->setName("NuPlayerRenderer");
1054     mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
1055     mRendererLooper->registerHandler(mRenderer);
1056 
1057     sp<MetaData> meta = getFileMeta();
1058     int32_t rate;
1059     if (meta != NULL
1060             && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) {
1061         mRenderer->setVideoFrameRate(rate);
1062     }
1063 
1064     if (mVideoDecoder != NULL) {
1065         mVideoDecoder->setRenderer(mRenderer);
1066     }
1067     if (mAudioDecoder != NULL) {
1068         mAudioDecoder->setRenderer(mRenderer);
1069     }
1070 
1071     postScanSources();
1072 }
1073 
onPause()1074 void NuPlayer::onPause() {
1075     if (mPaused) {
1076         return;
1077     }
1078     mPaused = true;
1079     if (mSource != NULL) {
1080         mSource->pause();
1081     } else {
1082         ALOGW("pause called when source is gone or not set");
1083     }
1084     if (mRenderer != NULL) {
1085         mRenderer->pause();
1086     } else {
1087         ALOGW("pause called when renderer is gone or not set");
1088     }
1089 }
1090 
audioDecoderStillNeeded()1091 bool NuPlayer::audioDecoderStillNeeded() {
1092     // Audio decoder is no longer needed if it's in shut/shutting down status.
1093     return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER));
1094 }
1095 
handleFlushComplete(bool audio,bool isDecoder)1096 void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) {
1097     // We wait for both the decoder flush and the renderer flush to complete
1098     // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state.
1099 
1100     mFlushComplete[audio][isDecoder] = true;
1101     if (!mFlushComplete[audio][!isDecoder]) {
1102         return;
1103     }
1104 
1105     FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo;
1106     switch (*state) {
1107         case FLUSHING_DECODER:
1108         {
1109             *state = FLUSHED;
1110             break;
1111         }
1112 
1113         case FLUSHING_DECODER_SHUTDOWN:
1114         {
1115             *state = SHUTTING_DOWN_DECODER;
1116 
1117             ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video");
1118             if (!audio) {
1119                 // Widevine source reads must stop before releasing the video decoder.
1120                 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) {
1121                     mSource->stop();
1122                 }
1123             }
1124             getDecoder(audio)->initiateShutdown();
1125             break;
1126         }
1127 
1128         default:
1129             // decoder flush completes only occur in a flushing state.
1130             LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state);
1131             break;
1132     }
1133 }
1134 
finishFlushIfPossible()1135 void NuPlayer::finishFlushIfPossible() {
1136     if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED
1137             && mFlushingAudio != SHUT_DOWN) {
1138         return;
1139     }
1140 
1141     if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED
1142             && mFlushingVideo != SHUT_DOWN) {
1143         return;
1144     }
1145 
1146     ALOGV("both audio and video are flushed now.");
1147 
1148     mFlushingAudio = NONE;
1149     mFlushingVideo = NONE;
1150 
1151     clearFlushComplete();
1152 
1153     processDeferredActions();
1154 }
1155 
postScanSources()1156 void NuPlayer::postScanSources() {
1157     if (mScanSourcesPending) {
1158         return;
1159     }
1160 
1161     sp<AMessage> msg = new AMessage(kWhatScanSources, id());
1162     msg->setInt32("generation", mScanSourcesGeneration);
1163     msg->post();
1164 
1165     mScanSourcesPending = true;
1166 }
1167 
tryOpenAudioSinkForOffload(const sp<AMessage> & format,bool hasVideo)1168 void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) {
1169     // Note: This is called early in NuPlayer to determine whether offloading
1170     // is possible; otherwise the decoders call the renderer openAudioSink directly.
1171 
1172     status_t err = mRenderer->openAudioSink(
1173             format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio);
1174     if (err != OK) {
1175         // Any failure we turn off mOffloadAudio.
1176         mOffloadAudio = false;
1177     } else if (mOffloadAudio) {
1178         sp<MetaData> audioMeta =
1179                 mSource->getFormatMeta(true /* audio */);
1180         sendMetaDataToHal(mAudioSink, audioMeta);
1181     }
1182 }
1183 
closeAudioSink()1184 void NuPlayer::closeAudioSink() {
1185     mRenderer->closeAudioSink();
1186 }
1187 
instantiateDecoder(bool audio,sp<DecoderBase> * decoder)1188 status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) {
1189     if (*decoder != NULL) {
1190         return OK;
1191     }
1192 
1193     sp<AMessage> format = mSource->getFormat(audio);
1194 
1195     if (format == NULL) {
1196         return -EWOULDBLOCK;
1197     }
1198 
1199     if (!audio) {
1200         AString mime;
1201         CHECK(format->findString("mime", &mime));
1202 
1203         sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id());
1204         if (mCCDecoder == NULL) {
1205             mCCDecoder = new CCDecoder(ccNotify);
1206         }
1207 
1208         if (mSourceFlags & Source::FLAG_SECURE) {
1209             format->setInt32("secure", true);
1210         }
1211 
1212         if (mSourceFlags & Source::FLAG_PROTECTED) {
1213             format->setInt32("protected", true);
1214         }
1215     }
1216 
1217     if (audio) {
1218         sp<AMessage> notify = new AMessage(kWhatAudioNotify, id());
1219         ++mAudioDecoderGeneration;
1220         notify->setInt32("generation", mAudioDecoderGeneration);
1221 
1222         if (mOffloadAudio) {
1223             *decoder = new DecoderPassThrough(notify, mSource, mRenderer);
1224         } else {
1225             *decoder = new Decoder(notify, mSource, mRenderer);
1226         }
1227     } else {
1228         sp<AMessage> notify = new AMessage(kWhatVideoNotify, id());
1229         ++mVideoDecoderGeneration;
1230         notify->setInt32("generation", mVideoDecoderGeneration);
1231 
1232         *decoder = new Decoder(
1233                 notify, mSource, mRenderer, mNativeWindow, mCCDecoder);
1234 
1235         // enable FRC if high-quality AV sync is requested, even if not
1236         // queuing to native window, as this will even improve textureview
1237         // playback.
1238         {
1239             char value[PROPERTY_VALUE_MAX];
1240             if (property_get("persist.sys.media.avsync", value, NULL) &&
1241                     (!strcmp("1", value) || !strcasecmp("true", value))) {
1242                 format->setInt32("auto-frc", 1);
1243             }
1244         }
1245     }
1246     (*decoder)->init();
1247     (*decoder)->configure(format);
1248 
1249     // allocate buffers to decrypt widevine source buffers
1250     if (!audio && (mSourceFlags & Source::FLAG_SECURE)) {
1251         Vector<sp<ABuffer> > inputBufs;
1252         CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK);
1253 
1254         Vector<MediaBuffer *> mediaBufs;
1255         for (size_t i = 0; i < inputBufs.size(); i++) {
1256             const sp<ABuffer> &buffer = inputBufs[i];
1257             MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size());
1258             mediaBufs.push(mbuf);
1259         }
1260 
1261         status_t err = mSource->setBuffers(audio, mediaBufs);
1262         if (err != OK) {
1263             for (size_t i = 0; i < mediaBufs.size(); ++i) {
1264                 mediaBufs[i]->release();
1265             }
1266             mediaBufs.clear();
1267             ALOGE("Secure source didn't support secure mediaBufs.");
1268             return err;
1269         }
1270     }
1271     return OK;
1272 }
1273 
updateVideoSize(const sp<AMessage> & inputFormat,const sp<AMessage> & outputFormat)1274 void NuPlayer::updateVideoSize(
1275         const sp<AMessage> &inputFormat,
1276         const sp<AMessage> &outputFormat) {
1277     if (inputFormat == NULL) {
1278         ALOGW("Unknown video size, reporting 0x0!");
1279         notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0);
1280         return;
1281     }
1282 
1283     int32_t displayWidth, displayHeight;
1284     int32_t cropLeft, cropTop, cropRight, cropBottom;
1285 
1286     if (outputFormat != NULL) {
1287         int32_t width, height;
1288         CHECK(outputFormat->findInt32("width", &width));
1289         CHECK(outputFormat->findInt32("height", &height));
1290 
1291         int32_t cropLeft, cropTop, cropRight, cropBottom;
1292         CHECK(outputFormat->findRect(
1293                     "crop",
1294                     &cropLeft, &cropTop, &cropRight, &cropBottom));
1295 
1296         displayWidth = cropRight - cropLeft + 1;
1297         displayHeight = cropBottom - cropTop + 1;
1298 
1299         ALOGV("Video output format changed to %d x %d "
1300              "(crop: %d x %d @ (%d, %d))",
1301              width, height,
1302              displayWidth,
1303              displayHeight,
1304              cropLeft, cropTop);
1305     } else {
1306         CHECK(inputFormat->findInt32("width", &displayWidth));
1307         CHECK(inputFormat->findInt32("height", &displayHeight));
1308 
1309         ALOGV("Video input format %d x %d", displayWidth, displayHeight);
1310     }
1311 
1312     // Take into account sample aspect ratio if necessary:
1313     int32_t sarWidth, sarHeight;
1314     if (inputFormat->findInt32("sar-width", &sarWidth)
1315             && inputFormat->findInt32("sar-height", &sarHeight)) {
1316         ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight);
1317 
1318         displayWidth = (displayWidth * sarWidth) / sarHeight;
1319 
1320         ALOGV("display dimensions %d x %d", displayWidth, displayHeight);
1321     }
1322 
1323     int32_t rotationDegrees;
1324     if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) {
1325         rotationDegrees = 0;
1326     }
1327 
1328     if (rotationDegrees == 90 || rotationDegrees == 270) {
1329         int32_t tmp = displayWidth;
1330         displayWidth = displayHeight;
1331         displayHeight = tmp;
1332     }
1333 
1334     notifyListener(
1335             MEDIA_SET_VIDEO_SIZE,
1336             displayWidth,
1337             displayHeight);
1338 }
1339 
notifyListener(int msg,int ext1,int ext2,const Parcel * in)1340 void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
1341     if (mDriver == NULL) {
1342         return;
1343     }
1344 
1345     sp<NuPlayerDriver> driver = mDriver.promote();
1346 
1347     if (driver == NULL) {
1348         return;
1349     }
1350 
1351     driver->notifyListener(msg, ext1, ext2, in);
1352 }
1353 
flushDecoder(bool audio,bool needShutdown)1354 void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
1355     ALOGV("[%s] flushDecoder needShutdown=%d",
1356           audio ? "audio" : "video", needShutdown);
1357 
1358     const sp<DecoderBase> &decoder = getDecoder(audio);
1359     if (decoder == NULL) {
1360         ALOGI("flushDecoder %s without decoder present",
1361              audio ? "audio" : "video");
1362         return;
1363     }
1364 
1365     // Make sure we don't continue to scan sources until we finish flushing.
1366     ++mScanSourcesGeneration;
1367     mScanSourcesPending = false;
1368 
1369     decoder->signalFlush();
1370 
1371     FlushStatus newStatus =
1372         needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1373 
1374     mFlushComplete[audio][false /* isDecoder */] = false;
1375     mFlushComplete[audio][true /* isDecoder */] = false;
1376     if (audio) {
1377         ALOGE_IF(mFlushingAudio != NONE,
1378                 "audio flushDecoder() is called in state %d", mFlushingAudio);
1379         mFlushingAudio = newStatus;
1380     } else {
1381         ALOGE_IF(mFlushingVideo != NONE,
1382                 "video flushDecoder() is called in state %d", mFlushingVideo);
1383         mFlushingVideo = newStatus;
1384     }
1385 }
1386 
queueDecoderShutdown(bool audio,bool video,const sp<AMessage> & reply)1387 void NuPlayer::queueDecoderShutdown(
1388         bool audio, bool video, const sp<AMessage> &reply) {
1389     ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1390 
1391     mDeferredActions.push_back(
1392             new FlushDecoderAction(
1393                 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE,
1394                 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE));
1395 
1396     mDeferredActions.push_back(
1397             new SimpleAction(&NuPlayer::performScanSources));
1398 
1399     mDeferredActions.push_back(new PostMessageAction(reply));
1400 
1401     processDeferredActions();
1402 }
1403 
setVideoScalingMode(int32_t mode)1404 status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1405     mVideoScalingMode = mode;
1406     if (mNativeWindow != NULL) {
1407         status_t ret = native_window_set_scaling_mode(
1408                 mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1409         if (ret != OK) {
1410             ALOGE("Failed to set scaling mode (%d): %s",
1411                 -ret, strerror(-ret));
1412             return ret;
1413         }
1414     }
1415     return OK;
1416 }
1417 
getTrackInfo(Parcel * reply) const1418 status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1419     sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1420     msg->setPointer("reply", reply);
1421 
1422     sp<AMessage> response;
1423     status_t err = msg->postAndAwaitResponse(&response);
1424     return err;
1425 }
1426 
getSelectedTrack(int32_t type,Parcel * reply) const1427 status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const {
1428     sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1429     msg->setPointer("reply", reply);
1430     msg->setInt32("type", type);
1431 
1432     sp<AMessage> response;
1433     status_t err = msg->postAndAwaitResponse(&response);
1434     if (err == OK && response != NULL) {
1435         CHECK(response->findInt32("err", &err));
1436     }
1437     return err;
1438 }
1439 
selectTrack(size_t trackIndex,bool select,int64_t timeUs)1440 status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
1441     sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1442     msg->setSize("trackIndex", trackIndex);
1443     msg->setInt32("select", select);
1444     msg->setInt64("timeUs", timeUs);
1445 
1446     sp<AMessage> response;
1447     status_t err = msg->postAndAwaitResponse(&response);
1448 
1449     if (err != OK) {
1450         return err;
1451     }
1452 
1453     if (!response->findInt32("err", &err)) {
1454         err = OK;
1455     }
1456 
1457     return err;
1458 }
1459 
getCurrentPosition(int64_t * mediaUs)1460 status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) {
1461     sp<Renderer> renderer = mRenderer;
1462     if (renderer == NULL) {
1463         return NO_INIT;
1464     }
1465 
1466     return renderer->getCurrentPosition(mediaUs);
1467 }
1468 
getStats(int64_t * numFramesTotal,int64_t * numFramesDropped)1469 void NuPlayer::getStats(int64_t *numFramesTotal, int64_t *numFramesDropped) {
1470     sp<DecoderBase> decoder = getDecoder(false /* audio */);
1471     if (decoder != NULL) {
1472         decoder->getStats(numFramesTotal, numFramesDropped);
1473     } else {
1474         *numFramesTotal = 0;
1475         *numFramesDropped = 0;
1476     }
1477 }
1478 
getFileMeta()1479 sp<MetaData> NuPlayer::getFileMeta() {
1480     return mSource->getFileFormatMeta();
1481 }
1482 
schedulePollDuration()1483 void NuPlayer::schedulePollDuration() {
1484     sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1485     msg->setInt32("generation", mPollDurationGeneration);
1486     msg->post();
1487 }
1488 
cancelPollDuration()1489 void NuPlayer::cancelPollDuration() {
1490     ++mPollDurationGeneration;
1491 }
1492 
processDeferredActions()1493 void NuPlayer::processDeferredActions() {
1494     while (!mDeferredActions.empty()) {
1495         // We won't execute any deferred actions until we're no longer in
1496         // an intermediate state, i.e. one more more decoders are currently
1497         // flushing or shutting down.
1498 
1499         if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1500             // We're currently flushing, postpone the reset until that's
1501             // completed.
1502 
1503             ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1504                   mFlushingAudio, mFlushingVideo);
1505 
1506             break;
1507         }
1508 
1509         sp<Action> action = *mDeferredActions.begin();
1510         mDeferredActions.erase(mDeferredActions.begin());
1511 
1512         action->execute(this);
1513     }
1514 }
1515 
performSeek(int64_t seekTimeUs,bool needNotify)1516 void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) {
1517     ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)",
1518           seekTimeUs,
1519           seekTimeUs / 1E6,
1520           needNotify);
1521 
1522     if (mSource == NULL) {
1523         // This happens when reset occurs right before the loop mode
1524         // asynchronously seeks to the start of the stream.
1525         LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL,
1526                 "mSource is NULL and decoders not NULL audio(%p) video(%p)",
1527                 mAudioDecoder.get(), mVideoDecoder.get());
1528         return;
1529     }
1530     mSource->seekTo(seekTimeUs);
1531     ++mTimedTextGeneration;
1532 
1533     // everything's flushed, continue playback.
1534 }
1535 
performDecoderFlush(FlushCommand audio,FlushCommand video)1536 void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) {
1537     ALOGV("performDecoderFlush audio=%d, video=%d", audio, video);
1538 
1539     if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL)
1540             && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) {
1541         return;
1542     }
1543 
1544     if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) {
1545         flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN));
1546     }
1547 
1548     if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) {
1549         flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN));
1550     }
1551 }
1552 
performReset()1553 void NuPlayer::performReset() {
1554     ALOGV("performReset");
1555 
1556     CHECK(mAudioDecoder == NULL);
1557     CHECK(mVideoDecoder == NULL);
1558 
1559     cancelPollDuration();
1560 
1561     ++mScanSourcesGeneration;
1562     mScanSourcesPending = false;
1563 
1564     if (mRendererLooper != NULL) {
1565         if (mRenderer != NULL) {
1566             mRendererLooper->unregisterHandler(mRenderer->id());
1567         }
1568         mRendererLooper->stop();
1569         mRendererLooper.clear();
1570     }
1571     mRenderer.clear();
1572     ++mRendererGeneration;
1573 
1574     if (mSource != NULL) {
1575         mSource->stop();
1576 
1577         mSource.clear();
1578     }
1579 
1580     if (mDriver != NULL) {
1581         sp<NuPlayerDriver> driver = mDriver.promote();
1582         if (driver != NULL) {
1583             driver->notifyResetComplete();
1584         }
1585     }
1586 
1587     mStarted = false;
1588 }
1589 
performScanSources()1590 void NuPlayer::performScanSources() {
1591     ALOGV("performScanSources");
1592 
1593     if (!mStarted) {
1594         return;
1595     }
1596 
1597     if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1598         postScanSources();
1599     }
1600 }
1601 
performSetSurface(const sp<NativeWindowWrapper> & wrapper)1602 void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1603     ALOGV("performSetSurface");
1604 
1605     mNativeWindow = wrapper;
1606 
1607     // XXX - ignore error from setVideoScalingMode for now
1608     setVideoScalingMode(mVideoScalingMode);
1609 
1610     if (mDriver != NULL) {
1611         sp<NuPlayerDriver> driver = mDriver.promote();
1612         if (driver != NULL) {
1613             driver->notifySetSurfaceComplete();
1614         }
1615     }
1616 }
1617 
performResumeDecoders(bool needNotify)1618 void NuPlayer::performResumeDecoders(bool needNotify) {
1619     if (needNotify) {
1620         mResumePending = true;
1621         if (mVideoDecoder == NULL) {
1622             // if audio-only, we can notify seek complete now,
1623             // as the resume operation will be relatively fast.
1624             finishResume();
1625         }
1626     }
1627 
1628     if (mVideoDecoder != NULL) {
1629         // When there is continuous seek, MediaPlayer will cache the seek
1630         // position, and send down new seek request when previous seek is
1631         // complete. Let's wait for at least one video output frame before
1632         // notifying seek complete, so that the video thumbnail gets updated
1633         // when seekbar is dragged.
1634         mVideoDecoder->signalResume(needNotify);
1635     }
1636 
1637     if (mAudioDecoder != NULL) {
1638         mAudioDecoder->signalResume(false /* needNotify */);
1639     }
1640 }
1641 
finishResume()1642 void NuPlayer::finishResume() {
1643     if (mResumePending) {
1644         mResumePending = false;
1645         if (mDriver != NULL) {
1646             sp<NuPlayerDriver> driver = mDriver.promote();
1647             if (driver != NULL) {
1648                 driver->notifySeekComplete();
1649             }
1650         }
1651     }
1652 }
1653 
onSourceNotify(const sp<AMessage> & msg)1654 void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1655     int32_t what;
1656     CHECK(msg->findInt32("what", &what));
1657 
1658     switch (what) {
1659         case Source::kWhatPrepared:
1660         {
1661             if (mSource == NULL) {
1662                 // This is a stale notification from a source that was
1663                 // asynchronously preparing when the client called reset().
1664                 // We handled the reset, the source is gone.
1665                 break;
1666             }
1667 
1668             int32_t err;
1669             CHECK(msg->findInt32("err", &err));
1670 
1671             sp<NuPlayerDriver> driver = mDriver.promote();
1672             if (driver != NULL) {
1673                 // notify duration first, so that it's definitely set when
1674                 // the app received the "prepare complete" callback.
1675                 int64_t durationUs;
1676                 if (mSource->getDuration(&durationUs) == OK) {
1677                     driver->notifyDuration(durationUs);
1678                 }
1679                 driver->notifyPrepareCompleted(err);
1680             }
1681 
1682             break;
1683         }
1684 
1685         case Source::kWhatFlagsChanged:
1686         {
1687             uint32_t flags;
1688             CHECK(msg->findInt32("flags", (int32_t *)&flags));
1689 
1690             sp<NuPlayerDriver> driver = mDriver.promote();
1691             if (driver != NULL) {
1692                 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) {
1693                     driver->notifyListener(
1694                             MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0);
1695                 }
1696                 driver->notifyFlagsChanged(flags);
1697             }
1698 
1699             if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1700                     && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1701                 cancelPollDuration();
1702             } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1703                     && (flags & Source::FLAG_DYNAMIC_DURATION)
1704                     && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1705                 schedulePollDuration();
1706             }
1707 
1708             mSourceFlags = flags;
1709             break;
1710         }
1711 
1712         case Source::kWhatVideoSizeChanged:
1713         {
1714             sp<AMessage> format;
1715             CHECK(msg->findMessage("format", &format));
1716 
1717             updateVideoSize(format);
1718             break;
1719         }
1720 
1721         case Source::kWhatBufferingUpdate:
1722         {
1723             int32_t percentage;
1724             CHECK(msg->findInt32("percentage", &percentage));
1725 
1726             notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0);
1727             break;
1728         }
1729 
1730         case Source::kWhatPauseOnBufferingStart:
1731         {
1732             // ignore if not playing
1733             if (mStarted && !mPausedByClient) {
1734                 ALOGI("buffer low, pausing...");
1735 
1736                 onPause();
1737             }
1738             // fall-thru
1739         }
1740 
1741         case Source::kWhatBufferingStart:
1742         {
1743             notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1744             break;
1745         }
1746 
1747         case Source::kWhatResumeOnBufferingEnd:
1748         {
1749             // ignore if not playing
1750             if (mStarted && !mPausedByClient) {
1751                 ALOGI("buffer ready, resuming...");
1752 
1753                 onResume();
1754             }
1755             // fall-thru
1756         }
1757 
1758         case Source::kWhatBufferingEnd:
1759         {
1760             notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1761             break;
1762         }
1763 
1764         case Source::kWhatCacheStats:
1765         {
1766             int32_t kbps;
1767             CHECK(msg->findInt32("bandwidth", &kbps));
1768 
1769             notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps);
1770             break;
1771         }
1772 
1773         case Source::kWhatSubtitleData:
1774         {
1775             sp<ABuffer> buffer;
1776             CHECK(msg->findBuffer("buffer", &buffer));
1777 
1778             sendSubtitleData(buffer, 0 /* baseIndex */);
1779             break;
1780         }
1781 
1782         case Source::kWhatTimedTextData:
1783         {
1784             int32_t generation;
1785             if (msg->findInt32("generation", &generation)
1786                     && generation != mTimedTextGeneration) {
1787                 break;
1788             }
1789 
1790             sp<ABuffer> buffer;
1791             CHECK(msg->findBuffer("buffer", &buffer));
1792 
1793             sp<NuPlayerDriver> driver = mDriver.promote();
1794             if (driver == NULL) {
1795                 break;
1796             }
1797 
1798             int posMs;
1799             int64_t timeUs, posUs;
1800             driver->getCurrentPosition(&posMs);
1801             posUs = posMs * 1000;
1802             CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1803 
1804             if (posUs < timeUs) {
1805                 if (!msg->findInt32("generation", &generation)) {
1806                     msg->setInt32("generation", mTimedTextGeneration);
1807                 }
1808                 msg->post(timeUs - posUs);
1809             } else {
1810                 sendTimedTextData(buffer);
1811             }
1812             break;
1813         }
1814 
1815         case Source::kWhatQueueDecoderShutdown:
1816         {
1817             int32_t audio, video;
1818             CHECK(msg->findInt32("audio", &audio));
1819             CHECK(msg->findInt32("video", &video));
1820 
1821             sp<AMessage> reply;
1822             CHECK(msg->findMessage("reply", &reply));
1823 
1824             queueDecoderShutdown(audio, video, reply);
1825             break;
1826         }
1827 
1828         case Source::kWhatDrmNoLicense:
1829         {
1830             notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE);
1831             break;
1832         }
1833 
1834         default:
1835             TRESPASS();
1836     }
1837 }
1838 
onClosedCaptionNotify(const sp<AMessage> & msg)1839 void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) {
1840     int32_t what;
1841     CHECK(msg->findInt32("what", &what));
1842 
1843     switch (what) {
1844         case NuPlayer::CCDecoder::kWhatClosedCaptionData:
1845         {
1846             sp<ABuffer> buffer;
1847             CHECK(msg->findBuffer("buffer", &buffer));
1848 
1849             size_t inbandTracks = 0;
1850             if (mSource != NULL) {
1851                 inbandTracks = mSource->getTrackCount();
1852             }
1853 
1854             sendSubtitleData(buffer, inbandTracks);
1855             break;
1856         }
1857 
1858         case NuPlayer::CCDecoder::kWhatTrackAdded:
1859         {
1860             notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0);
1861 
1862             break;
1863         }
1864 
1865         default:
1866             TRESPASS();
1867     }
1868 
1869 
1870 }
1871 
sendSubtitleData(const sp<ABuffer> & buffer,int32_t baseIndex)1872 void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) {
1873     int32_t trackIndex;
1874     int64_t timeUs, durationUs;
1875     CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1876     CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1877     CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1878 
1879     Parcel in;
1880     in.writeInt32(trackIndex + baseIndex);
1881     in.writeInt64(timeUs);
1882     in.writeInt64(durationUs);
1883     in.writeInt32(buffer->size());
1884     in.writeInt32(buffer->size());
1885     in.write(buffer->data(), buffer->size());
1886 
1887     notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1888 }
1889 
sendTimedTextData(const sp<ABuffer> & buffer)1890 void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) {
1891     const void *data;
1892     size_t size = 0;
1893     int64_t timeUs;
1894     int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
1895 
1896     AString mime;
1897     CHECK(buffer->meta()->findString("mime", &mime));
1898     CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0);
1899 
1900     data = buffer->data();
1901     size = buffer->size();
1902 
1903     Parcel parcel;
1904     if (size > 0) {
1905         CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1906         flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
1907         TextDescriptions::getParcelOfDescriptions(
1908                 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel);
1909     }
1910 
1911     if ((parcel.dataSize() > 0)) {
1912         notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel);
1913     } else {  // send an empty timed text
1914         notifyListener(MEDIA_TIMED_TEXT, 0, 0);
1915     }
1916 }
1917 ////////////////////////////////////////////////////////////////////////////////
1918 
getFormat(bool audio)1919 sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1920     sp<MetaData> meta = getFormatMeta(audio);
1921 
1922     if (meta == NULL) {
1923         return NULL;
1924     }
1925 
1926     sp<AMessage> msg = new AMessage;
1927 
1928     if(convertMetaDataToMessage(meta, &msg) == OK) {
1929         return msg;
1930     }
1931     return NULL;
1932 }
1933 
notifyFlagsChanged(uint32_t flags)1934 void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1935     sp<AMessage> notify = dupNotify();
1936     notify->setInt32("what", kWhatFlagsChanged);
1937     notify->setInt32("flags", flags);
1938     notify->post();
1939 }
1940 
notifyVideoSizeChanged(const sp<AMessage> & format)1941 void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) {
1942     sp<AMessage> notify = dupNotify();
1943     notify->setInt32("what", kWhatVideoSizeChanged);
1944     notify->setMessage("format", format);
1945     notify->post();
1946 }
1947 
notifyPrepared(status_t err)1948 void NuPlayer::Source::notifyPrepared(status_t err) {
1949     sp<AMessage> notify = dupNotify();
1950     notify->setInt32("what", kWhatPrepared);
1951     notify->setInt32("err", err);
1952     notify->post();
1953 }
1954 
onMessageReceived(const sp<AMessage> &)1955 void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
1956     TRESPASS();
1957 }
1958 
1959 }  // namespace android
1960