1 /*
2  * Copyright (C) 2012 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 "GenericSource"
19 
20 #include "GenericSource.h"
21 #include "NuPlayerDrm.h"
22 
23 #include <binder/IServiceManager.h>
24 #include <cutils/properties.h>
25 #include <datasource/PlayerServiceDataSourceFactory.h>
26 #include <datasource/PlayerServiceFileSource.h>
27 #include <datasource/HTTPBase.h>
28 #include <datasource/NuCachedSource2.h>
29 #include <media/DataSource.h>
30 #include <media/MediaBufferHolder.h>
31 #include <media/stagefright/MediaSource.h>
32 #include <android/IMediaExtractorService.h>
33 #include <media/IMediaHTTPService.h>
34 #include <media/stagefright/foundation/ABuffer.h>
35 #include <media/stagefright/foundation/ADebug.h>
36 #include <media/stagefright/foundation/AMessage.h>
37 #include <media/stagefright/InterfaceUtils.h>
38 #include <media/stagefright/FoundationUtils.h>
39 #include <media/stagefright/MediaBuffer.h>
40 #include <media/stagefright/MediaClock.h>
41 #include <media/stagefright/MediaDefs.h>
42 #include <media/stagefright/MediaExtractor.h>
43 #include <media/stagefright/MediaExtractorFactory.h>
44 #include <media/stagefright/MetaData.h>
45 #include <media/stagefright/Utils.h>
46 #include <mpeg2ts/AnotherPacketSource.h>
47 
48 namespace android {
49 
50 static const int kInitialMarkMs        = 5000;  // 5secs
51 
52 //static const int kPausePlaybackMarkMs  = 2000;  // 2secs
53 static const int kResumePlaybackMarkMs = 15000;  // 15secs
54 
GenericSource(const sp<AMessage> & notify,bool uidValid,uid_t uid,const sp<MediaClock> & mediaClock)55 NuPlayer::GenericSource::GenericSource(
56         const sp<AMessage> &notify,
57         bool uidValid,
58         uid_t uid,
59         const sp<MediaClock> &mediaClock)
60     : Source(notify),
61       mAudioTimeUs(0),
62       mAudioLastDequeueTimeUs(0),
63       mVideoTimeUs(0),
64       mVideoLastDequeueTimeUs(0),
65       mPrevBufferPercentage(-1),
66       mPollBufferingGeneration(0),
67       mSentPauseOnBuffering(false),
68       mAudioDataGeneration(0),
69       mVideoDataGeneration(0),
70       mFetchSubtitleDataGeneration(0),
71       mFetchTimedTextDataGeneration(0),
72       mDurationUs(-1LL),
73       mAudioIsVorbis(false),
74       mIsSecure(false),
75       mIsStreaming(false),
76       mUIDValid(uidValid),
77       mUID(uid),
78       mMediaClock(mediaClock),
79       mBitrate(-1LL),
80       mPendingReadBufferTypes(0) {
81     ALOGV("GenericSource");
82     CHECK(mediaClock != NULL);
83 
84     mBufferingSettings.mInitialMarkMs = kInitialMarkMs;
85     mBufferingSettings.mResumePlaybackMarkMs = kResumePlaybackMarkMs;
86     resetDataSource();
87 }
88 
resetDataSource()89 void NuPlayer::GenericSource::resetDataSource() {
90     ALOGV("resetDataSource");
91 
92     mHTTPService.clear();
93     {
94         Mutex::Autolock _l_d(mDisconnectLock);
95         mHttpSource.clear();
96         mDisconnected = false;
97     }
98     mUri.clear();
99     mUriHeaders.clear();
100     mSources.clear();
101     mFd.reset();
102     mOffset = 0;
103     mLength = 0;
104     mStarted = false;
105     mPreparing = false;
106 
107     mIsDrmProtected = false;
108     mIsDrmReleased = false;
109     mIsSecure = false;
110     mMimes.clear();
111 }
112 
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)113 status_t NuPlayer::GenericSource::setDataSource(
114         const sp<IMediaHTTPService> &httpService,
115         const char *url,
116         const KeyedVector<String8, String8> *headers) {
117     Mutex::Autolock _l(mLock);
118     ALOGV("setDataSource url: %s", url);
119 
120     resetDataSource();
121 
122     mHTTPService = httpService;
123     mUri = url;
124 
125     if (headers) {
126         mUriHeaders = *headers;
127     }
128 
129     // delay data source creation to prepareAsync() to avoid blocking
130     // the calling thread in setDataSource for any significant time.
131     return OK;
132 }
133 
setDataSource(int fd,int64_t offset,int64_t length)134 status_t NuPlayer::GenericSource::setDataSource(
135         int fd, int64_t offset, int64_t length) {
136     Mutex::Autolock _l(mLock);
137     ALOGV("setDataSource %d/%lld/%lld (%s)", fd, (long long)offset, (long long)length, nameForFd(fd).c_str());
138 
139     resetDataSource();
140 
141     mFd.reset(dup(fd));
142     mOffset = offset;
143     mLength = length;
144 
145     // delay data source creation to prepareAsync() to avoid blocking
146     // the calling thread in setDataSource for any significant time.
147     return OK;
148 }
149 
setDataSource(const sp<DataSource> & source)150 status_t NuPlayer::GenericSource::setDataSource(const sp<DataSource>& source) {
151     Mutex::Autolock _l(mLock);
152     ALOGV("setDataSource (source: %p)", source.get());
153 
154     resetDataSource();
155     {
156         Mutex::Autolock _l_d(mDisconnectLock);
157         mDataSource = source;
158     }
159     return OK;
160 }
161 
getFileFormatMeta() const162 sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const {
163     Mutex::Autolock _l(mLock);
164     return mFileMeta;
165 }
166 
initFromDataSource()167 status_t NuPlayer::GenericSource::initFromDataSource() {
168     sp<IMediaExtractor> extractor;
169     sp<DataSource> dataSource;
170     {
171         Mutex::Autolock _l_d(mDisconnectLock);
172         dataSource = mDataSource;
173     }
174     CHECK(dataSource != NULL);
175 
176     mLock.unlock();
177     // This might take long time if data source is not reliable.
178     extractor = MediaExtractorFactory::Create(dataSource, NULL);
179 
180     if (extractor == NULL) {
181         ALOGE("initFromDataSource, cannot create extractor!");
182         mLock.lock();
183         return UNKNOWN_ERROR;
184     }
185 
186     sp<MetaData> fileMeta = extractor->getMetaData();
187 
188     size_t numtracks = extractor->countTracks();
189     if (numtracks == 0) {
190         ALOGE("initFromDataSource, source has no track!");
191         mLock.lock();
192         return UNKNOWN_ERROR;
193     }
194 
195     mLock.lock();
196     mFileMeta = fileMeta;
197     if (mFileMeta != NULL) {
198         int64_t duration;
199         if (mFileMeta->findInt64(kKeyDuration, &duration)) {
200             mDurationUs = duration;
201         }
202     }
203 
204     int32_t totalBitrate = 0;
205 
206     mMimes.clear();
207 
208     for (size_t i = 0; i < numtracks; ++i) {
209         sp<IMediaSource> track = extractor->getTrack(i);
210         if (track == NULL) {
211             continue;
212         }
213 
214         sp<MetaData> meta = extractor->getTrackMetaData(i);
215         if (meta == NULL) {
216             ALOGE("no metadata for track %zu", i);
217             return UNKNOWN_ERROR;
218         }
219 
220         const char *mime;
221         CHECK(meta->findCString(kKeyMIMEType, &mime));
222 
223         ALOGV("initFromDataSource track[%zu]: %s", i, mime);
224 
225         // Do the string compare immediately with "mime",
226         // we can't assume "mime" would stay valid after another
227         // extractor operation, some extractors might modify meta
228         // during getTrack() and make it invalid.
229         if (!strncasecmp(mime, "audio/", 6)) {
230             if (mAudioTrack.mSource == NULL) {
231                 mAudioTrack.mIndex = i;
232                 mAudioTrack.mSource = track;
233                 mAudioTrack.mPackets =
234                     new AnotherPacketSource(mAudioTrack.mSource->getFormat());
235 
236                 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
237                     mAudioIsVorbis = true;
238                 } else {
239                     mAudioIsVorbis = false;
240                 }
241 
242                 mMimes.add(String8(mime));
243             }
244         } else if (!strncasecmp(mime, "video/", 6)) {
245             if (mVideoTrack.mSource == NULL) {
246                 mVideoTrack.mIndex = i;
247                 mVideoTrack.mSource = track;
248                 mVideoTrack.mPackets =
249                     new AnotherPacketSource(mVideoTrack.mSource->getFormat());
250 
251                 // video always at the beginning
252                 mMimes.insertAt(String8(mime), 0);
253             }
254         }
255 
256         mSources.push(track);
257         int64_t durationUs;
258         if (meta->findInt64(kKeyDuration, &durationUs)) {
259             if (durationUs > mDurationUs) {
260                 mDurationUs = durationUs;
261             }
262         }
263 
264         int32_t bitrate;
265         if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
266             totalBitrate += bitrate;
267         } else {
268             totalBitrate = -1;
269         }
270     }
271 
272     ALOGV("initFromDataSource mSources.size(): %zu  mIsSecure: %d  mime[0]: %s", mSources.size(),
273             mIsSecure, (mMimes.isEmpty() ? "NONE" : mMimes[0].c_str()));
274 
275     if (mSources.size() == 0) {
276         ALOGE("b/23705695");
277         return UNKNOWN_ERROR;
278     }
279 
280     // Modular DRM: The return value doesn't affect source initialization.
281     (void)checkDrmInfo();
282 
283     mBitrate = totalBitrate;
284 
285     return OK;
286 }
287 
getBufferingSettings(BufferingSettings * buffering)288 status_t NuPlayer::GenericSource::getBufferingSettings(
289         BufferingSettings* buffering /* nonnull */) {
290     {
291         Mutex::Autolock _l(mLock);
292         *buffering = mBufferingSettings;
293     }
294 
295     ALOGV("getBufferingSettings{%s}", buffering->toString().c_str());
296     return OK;
297 }
298 
setBufferingSettings(const BufferingSettings & buffering)299 status_t NuPlayer::GenericSource::setBufferingSettings(const BufferingSettings& buffering) {
300     ALOGV("setBufferingSettings{%s}", buffering.toString().c_str());
301 
302     Mutex::Autolock _l(mLock);
303     mBufferingSettings = buffering;
304     return OK;
305 }
306 
startSources()307 status_t NuPlayer::GenericSource::startSources() {
308     // Start the selected A/V tracks now before we start buffering.
309     // Widevine sources might re-initialize crypto when starting, if we delay
310     // this to start(), all data buffered during prepare would be wasted.
311     // (We don't actually start reading until start().)
312     //
313     // TODO: this logic may no longer be relevant after the removal of widevine
314     // support
315     if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
316         ALOGE("failed to start audio track!");
317         return UNKNOWN_ERROR;
318     }
319 
320     if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
321         ALOGE("failed to start video track!");
322         return UNKNOWN_ERROR;
323     }
324 
325     return OK;
326 }
327 
getLastReadPosition()328 int64_t NuPlayer::GenericSource::getLastReadPosition() {
329     if (mAudioTrack.mSource != NULL) {
330         return mAudioTimeUs;
331     } else if (mVideoTrack.mSource != NULL) {
332         return mVideoTimeUs;
333     } else {
334         return 0;
335     }
336 }
337 
isStreaming() const338 bool NuPlayer::GenericSource::isStreaming() const {
339     Mutex::Autolock _l(mLock);
340     return mIsStreaming;
341 }
342 
~GenericSource()343 NuPlayer::GenericSource::~GenericSource() {
344     ALOGV("~GenericSource");
345     if (mLooper != NULL) {
346         mLooper->unregisterHandler(id());
347         mLooper->stop();
348     }
349     resetDataSource();
350 }
351 
prepareAsync()352 void NuPlayer::GenericSource::prepareAsync() {
353     Mutex::Autolock _l(mLock);
354     ALOGV("prepareAsync: (looper: %d)", (mLooper != NULL));
355 
356     if (mLooper == NULL) {
357         mLooper = new ALooper;
358         mLooper->setName("generic");
359         mLooper->start();
360 
361         mLooper->registerHandler(this);
362     }
363 
364     sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
365     msg->post();
366 }
367 
onPrepareAsync()368 void NuPlayer::GenericSource::onPrepareAsync() {
369     mDisconnectLock.lock();
370     ALOGV("onPrepareAsync: mDataSource: %d", (mDataSource != NULL));
371 
372     // delayed data source creation
373     if (mDataSource == NULL) {
374         // set to false first, if the extractor
375         // comes back as secure, set it to true then.
376         mIsSecure = false;
377 
378         if (!mUri.empty()) {
379             const char* uri = mUri.c_str();
380             String8 contentType;
381 
382             if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) {
383                 sp<DataSource> httpSource;
384                 mDisconnectLock.unlock();
385                 httpSource = PlayerServiceDataSourceFactory::getInstance()
386                         ->CreateMediaHTTP(mHTTPService);
387                 if (httpSource == NULL) {
388                     ALOGE("Failed to create http source!");
389                     notifyPreparedAndCleanup(UNKNOWN_ERROR);
390                     return;
391                 }
392                 mDisconnectLock.lock();
393 
394                 if (!mDisconnected) {
395                     mHttpSource = httpSource;
396                 }
397             }
398 
399             mLock.unlock();
400             mDisconnectLock.unlock();
401             // This might take long time if connection has some issue.
402             sp<DataSource> dataSource = PlayerServiceDataSourceFactory::getInstance()
403                     ->CreateFromURI(mHTTPService, uri, &mUriHeaders, &contentType,
404                             static_cast<HTTPBase *>(mHttpSource.get()));
405             mDisconnectLock.lock();
406             mLock.lock();
407             if (!mDisconnected) {
408                 mDataSource = dataSource;
409             }
410         } else {
411             if (property_get_bool("media.stagefright.extractremote", true) &&
412                     !PlayerServiceFileSource::requiresDrm(
413                             mFd.get(), mOffset, mLength, nullptr /* mime */)) {
414                 sp<IBinder> binder =
415                         defaultServiceManager()->getService(String16("media.extractor"));
416                 if (binder != nullptr) {
417                     ALOGD("FileSource remote");
418                     sp<IMediaExtractorService> mediaExService(
419                             interface_cast<IMediaExtractorService>(binder));
420                     sp<IDataSource> source;
421                     mediaExService->makeIDataSource(base::unique_fd(dup(mFd.get())), mOffset, mLength, &source);
422                     ALOGV("IDataSource(FileSource): %p %d %lld %lld",
423                             source.get(), mFd.get(), (long long)mOffset, (long long)mLength);
424                     if (source.get() != nullptr) {
425                         mDataSource = CreateDataSourceFromIDataSource(source);
426                     } else {
427                         ALOGW("extractor service cannot make data source");
428                     }
429                 } else {
430                     ALOGW("extractor service not running");
431                 }
432             }
433             if (mDataSource == nullptr) {
434                 ALOGD("FileSource local");
435                 mDataSource = new PlayerServiceFileSource(dup(mFd.get()), mOffset, mLength);
436             }
437         }
438 
439         if (mDataSource == NULL) {
440             ALOGE("Failed to create data source!");
441             mDisconnectLock.unlock();
442             notifyPreparedAndCleanup(UNKNOWN_ERROR);
443             return;
444         }
445     }
446 
447     if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
448         mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
449     }
450 
451     mDisconnectLock.unlock();
452 
453     // For cached streaming cases, we need to wait for enough
454     // buffering before reporting prepared.
455     mIsStreaming = (mCachedSource != NULL);
456 
457     // init extractor from data source
458     status_t err = initFromDataSource();
459 
460     if (err != OK) {
461         ALOGE("Failed to init from data source!");
462         notifyPreparedAndCleanup(err);
463         return;
464     }
465 
466     if (mVideoTrack.mSource != NULL) {
467         sp<MetaData> meta = getFormatMeta_l(false /* audio */);
468         sp<AMessage> msg = new AMessage;
469         err = convertMetaDataToMessage(meta, &msg);
470         if(err != OK) {
471             notifyPreparedAndCleanup(err);
472             return;
473         }
474         notifyVideoSizeChanged(msg);
475     }
476 
477     notifyFlagsChanged(
478             // FLAG_SECURE will be known if/when prepareDrm is called by the app
479             // FLAG_PROTECTED will be known if/when prepareDrm is called by the app
480             FLAG_CAN_PAUSE |
481             FLAG_CAN_SEEK_BACKWARD |
482             FLAG_CAN_SEEK_FORWARD |
483             FLAG_CAN_SEEK);
484 
485     finishPrepareAsync();
486 
487     ALOGV("onPrepareAsync: Done");
488 }
489 
finishPrepareAsync()490 void NuPlayer::GenericSource::finishPrepareAsync() {
491     ALOGV("finishPrepareAsync");
492 
493     status_t err = startSources();
494     if (err != OK) {
495         ALOGE("Failed to init start data source!");
496         notifyPreparedAndCleanup(err);
497         return;
498     }
499 
500     if (mIsStreaming) {
501         mCachedSource->resumeFetchingIfNecessary();
502         mPreparing = true;
503         schedulePollBuffering();
504     } else {
505         notifyPrepared();
506     }
507 
508     if (mAudioTrack.mSource != NULL) {
509         postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
510     }
511 
512     if (mVideoTrack.mSource != NULL) {
513         postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
514     }
515 }
516 
notifyPreparedAndCleanup(status_t err)517 void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
518     if (err != OK) {
519         {
520             Mutex::Autolock _l_d(mDisconnectLock);
521             mDataSource.clear();
522             mHttpSource.clear();
523         }
524 
525         mCachedSource.clear();
526 
527         mBitrate = -1;
528         mPrevBufferPercentage = -1;
529         ++mPollBufferingGeneration;
530     }
531     notifyPrepared(err);
532 }
533 
start()534 void NuPlayer::GenericSource::start() {
535     Mutex::Autolock _l(mLock);
536     ALOGI("start");
537 
538     if (mAudioTrack.mSource != NULL) {
539         postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
540     }
541 
542     if (mVideoTrack.mSource != NULL) {
543         postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
544     }
545 
546     mStarted = true;
547 }
548 
stop()549 void NuPlayer::GenericSource::stop() {
550     Mutex::Autolock _l(mLock);
551     mStarted = false;
552 }
553 
pause()554 void NuPlayer::GenericSource::pause() {
555     Mutex::Autolock _l(mLock);
556     mStarted = false;
557 }
558 
resume()559 void NuPlayer::GenericSource::resume() {
560     Mutex::Autolock _l(mLock);
561     mStarted = true;
562 }
563 
disconnect()564 void NuPlayer::GenericSource::disconnect() {
565     sp<DataSource> dataSource, httpSource;
566     {
567         Mutex::Autolock _l_d(mDisconnectLock);
568         dataSource = mDataSource;
569         httpSource = mHttpSource;
570         mDisconnected = true;
571     }
572 
573     if (dataSource != NULL) {
574         // disconnect data source
575         if (dataSource->flags() & DataSource::kIsCachingDataSource) {
576             static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
577         }
578     } else if (httpSource != NULL) {
579         static_cast<HTTPBase *>(httpSource.get())->disconnect();
580     }
581 }
582 
feedMoreTSData()583 status_t NuPlayer::GenericSource::feedMoreTSData() {
584     return OK;
585 }
586 
sendCacheStats()587 void NuPlayer::GenericSource::sendCacheStats() {
588     int32_t kbps = 0;
589     status_t err = UNKNOWN_ERROR;
590 
591     if (mCachedSource != NULL) {
592         err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
593     }
594 
595     if (err == OK) {
596         sp<AMessage> notify = dupNotify();
597         notify->setInt32("what", kWhatCacheStats);
598         notify->setInt32("bandwidth", kbps);
599         notify->post();
600     }
601 }
602 
onMessageReceived(const sp<AMessage> & msg)603 void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
604     Mutex::Autolock _l(mLock);
605     switch (msg->what()) {
606       case kWhatPrepareAsync:
607       {
608           onPrepareAsync();
609           break;
610       }
611       case kWhatFetchSubtitleData:
612       {
613           fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
614                   mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
615           break;
616       }
617 
618       case kWhatFetchTimedTextData:
619       {
620           fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
621                   mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
622           break;
623       }
624 
625       case kWhatSendSubtitleData:
626       {
627           sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
628                   mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
629           break;
630       }
631 
632       case kWhatSendGlobalTimedTextData:
633       {
634           sendGlobalTextData(kWhatTimedTextData, mFetchTimedTextDataGeneration, msg);
635           break;
636       }
637       case kWhatSendTimedTextData:
638       {
639           sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
640                   mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
641           break;
642       }
643 
644       case kWhatChangeAVSource:
645       {
646           int32_t trackIndex;
647           CHECK(msg->findInt32("trackIndex", &trackIndex));
648           const sp<IMediaSource> source = mSources.itemAt(trackIndex);
649 
650           Track* track;
651           const char *mime;
652           media_track_type trackType, counterpartType;
653           sp<MetaData> meta = source->getFormat();
654           meta->findCString(kKeyMIMEType, &mime);
655           if (!strncasecmp(mime, "audio/", 6)) {
656               track = &mAudioTrack;
657               trackType = MEDIA_TRACK_TYPE_AUDIO;
658               counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
659           } else {
660               CHECK(!strncasecmp(mime, "video/", 6));
661               track = &mVideoTrack;
662               trackType = MEDIA_TRACK_TYPE_VIDEO;
663               counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
664           }
665 
666 
667           if (track->mSource != NULL) {
668               track->mSource->stop();
669           }
670           track->mSource = source;
671           track->mSource->start();
672           track->mIndex = trackIndex;
673           ++mAudioDataGeneration;
674           ++mVideoDataGeneration;
675 
676           int64_t timeUs, actualTimeUs;
677           const bool formatChange = true;
678           if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
679               timeUs = mAudioLastDequeueTimeUs;
680           } else {
681               timeUs = mVideoLastDequeueTimeUs;
682           }
683           readBuffer(trackType, timeUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */,
684                   &actualTimeUs, formatChange);
685           readBuffer(counterpartType, -1, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */,
686                   NULL, !formatChange);
687           ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
688 
689           break;
690       }
691 
692       case kWhatSeek:
693       {
694           onSeek(msg);
695           break;
696       }
697 
698       case kWhatReadBuffer:
699       {
700           onReadBuffer(msg);
701           break;
702       }
703 
704       case kWhatPollBuffering:
705       {
706           int32_t generation;
707           CHECK(msg->findInt32("generation", &generation));
708           if (generation == mPollBufferingGeneration) {
709               onPollBuffering();
710           }
711           break;
712       }
713 
714       default:
715           Source::onMessageReceived(msg);
716           break;
717     }
718 }
719 
fetchTextData(uint32_t sendWhat,media_track_type type,int32_t curGen,const sp<AnotherPacketSource> & packets,const sp<AMessage> & msg)720 void NuPlayer::GenericSource::fetchTextData(
721         uint32_t sendWhat,
722         media_track_type type,
723         int32_t curGen,
724         const sp<AnotherPacketSource>& packets,
725         const sp<AMessage>& msg) {
726     int32_t msgGeneration;
727     CHECK(msg->findInt32("generation", &msgGeneration));
728     if (msgGeneration != curGen) {
729         // stale
730         return;
731     }
732 
733     int32_t avail;
734     if (packets->hasBufferAvailable(&avail)) {
735         return;
736     }
737 
738     int64_t timeUs;
739     CHECK(msg->findInt64("timeUs", &timeUs));
740 
741     int64_t subTimeUs = 0;
742     readBuffer(type, timeUs, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */, &subTimeUs);
743 
744     status_t eosResult;
745     if (!packets->hasBufferAvailable(&eosResult)) {
746         return;
747     }
748 
749     if (msg->what() == kWhatFetchSubtitleData) {
750         subTimeUs -= 1000000LL;  // send subtile data one second earlier
751     }
752     sp<AMessage> msg2 = new AMessage(sendWhat, this);
753     msg2->setInt32("generation", msgGeneration);
754     mMediaClock->addTimer(msg2, subTimeUs);
755 }
756 
sendTextData(uint32_t what,media_track_type type,int32_t curGen,const sp<AnotherPacketSource> & packets,const sp<AMessage> & msg)757 void NuPlayer::GenericSource::sendTextData(
758         uint32_t what,
759         media_track_type type,
760         int32_t curGen,
761         const sp<AnotherPacketSource>& packets,
762         const sp<AMessage>& msg) {
763     int32_t msgGeneration;
764     CHECK(msg->findInt32("generation", &msgGeneration));
765     if (msgGeneration != curGen) {
766         // stale
767         return;
768     }
769 
770     int64_t subTimeUs;
771     if (packets->nextBufferTime(&subTimeUs) != OK) {
772         return;
773     }
774 
775     int64_t nextSubTimeUs = 0;
776     readBuffer(type, -1, MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC /* mode */, &nextSubTimeUs);
777 
778     sp<ABuffer> buffer;
779     status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
780     if (dequeueStatus == OK) {
781         sp<AMessage> notify = dupNotify();
782         notify->setInt32("what", what);
783         notify->setBuffer("buffer", buffer);
784         notify->post();
785 
786         if (msg->what() == kWhatSendSubtitleData) {
787             nextSubTimeUs -= 1000000LL;  // send subtile data one second earlier
788         }
789         mMediaClock->addTimer(msg, nextSubTimeUs);
790     }
791 }
792 
sendGlobalTextData(uint32_t what,int32_t curGen,sp<AMessage> msg)793 void NuPlayer::GenericSource::sendGlobalTextData(
794         uint32_t what,
795         int32_t curGen,
796         sp<AMessage> msg) {
797     int32_t msgGeneration;
798     CHECK(msg->findInt32("generation", &msgGeneration));
799     if (msgGeneration != curGen) {
800         // stale
801         return;
802     }
803 
804     uint32_t textType;
805     const void *data;
806     size_t size = 0;
807     if (mTimedTextTrack.mSource->getFormat()->findData(
808                     kKeyTextFormatData, &textType, &data, &size)) {
809         mGlobalTimedText = new ABuffer(size);
810         if (mGlobalTimedText->data()) {
811             memcpy(mGlobalTimedText->data(), data, size);
812             sp<AMessage> globalMeta = mGlobalTimedText->meta();
813             globalMeta->setInt64("timeUs", 0);
814             globalMeta->setString("mime", MEDIA_MIMETYPE_TEXT_3GPP);
815             globalMeta->setInt32("global", 1);
816             sp<AMessage> notify = dupNotify();
817             notify->setInt32("what", what);
818             notify->setBuffer("buffer", mGlobalTimedText);
819             notify->post();
820         }
821     }
822 }
823 
getFormatMeta(bool audio)824 sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
825     Mutex::Autolock _l(mLock);
826     return getFormatMeta_l(audio);
827 }
828 
getFormatMeta_l(bool audio)829 sp<MetaData> NuPlayer::GenericSource::getFormatMeta_l(bool audio) {
830     sp<IMediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
831 
832     if (source == NULL) {
833         return NULL;
834     }
835 
836     return source->getFormat();
837 }
838 
dequeueAccessUnit(bool audio,sp<ABuffer> * accessUnit)839 status_t NuPlayer::GenericSource::dequeueAccessUnit(
840         bool audio, sp<ABuffer> *accessUnit) {
841     Mutex::Autolock _l(mLock);
842     // If has gone through stop/releaseDrm sequence, we no longer send down any buffer b/c
843     // the codec's crypto object has gone away (b/37960096).
844     // Note: This will be unnecessary when stop() changes behavior and releases codec (b/35248283).
845     if (!mStarted && mIsDrmReleased) {
846         return -EWOULDBLOCK;
847     }
848 
849     Track *track = audio ? &mAudioTrack : &mVideoTrack;
850 
851     if (track->mSource == NULL) {
852         return -EWOULDBLOCK;
853     }
854 
855     status_t finalResult;
856     if (!track->mPackets->hasBufferAvailable(&finalResult)) {
857         if (finalResult == OK) {
858             postReadBuffer(
859                     audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
860             return -EWOULDBLOCK;
861         }
862         return finalResult;
863     }
864 
865     status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
866 
867     // start pulling in more buffers if cache is running low
868     // so that decoder has less chance of being starved
869     if (!mIsStreaming) {
870         if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
871             postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
872         }
873     } else {
874         int64_t durationUs = track->mPackets->getBufferedDurationUs(&finalResult);
875         // TODO: maxRebufferingMarkMs could be larger than
876         // mBufferingSettings.mResumePlaybackMarkMs
877         int64_t restartBufferingMarkUs =
878              mBufferingSettings.mResumePlaybackMarkMs * 1000LL / 2;
879         if (finalResult == OK) {
880             if (durationUs < restartBufferingMarkUs) {
881                 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
882             }
883             if (track->mPackets->getAvailableBufferCount(&finalResult) < 2
884                 && !mSentPauseOnBuffering && !mPreparing) {
885                 mCachedSource->resumeFetchingIfNecessary();
886                 sendCacheStats();
887                 mSentPauseOnBuffering = true;
888                 sp<AMessage> notify = dupNotify();
889                 notify->setInt32("what", kWhatPauseOnBufferingStart);
890                 notify->post();
891             }
892         }
893     }
894 
895     if (result != OK) {
896         if (mSubtitleTrack.mSource != NULL) {
897             mSubtitleTrack.mPackets->clear();
898             mFetchSubtitleDataGeneration++;
899         }
900         if (mTimedTextTrack.mSource != NULL) {
901             mTimedTextTrack.mPackets->clear();
902             mFetchTimedTextDataGeneration++;
903         }
904         return result;
905     }
906 
907     int64_t timeUs;
908     status_t eosResult; // ignored
909     CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
910     if (audio) {
911         mAudioLastDequeueTimeUs = timeUs;
912     } else {
913         mVideoLastDequeueTimeUs = timeUs;
914     }
915 
916     if (mSubtitleTrack.mSource != NULL
917             && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
918         sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
919         msg->setInt64("timeUs", timeUs);
920         msg->setInt32("generation", mFetchSubtitleDataGeneration);
921         msg->post();
922     }
923 
924     if (mTimedTextTrack.mSource != NULL
925             && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
926         sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
927         msg->setInt64("timeUs", timeUs);
928         msg->setInt32("generation", mFetchTimedTextDataGeneration);
929         msg->post();
930     }
931 
932     return result;
933 }
934 
getDuration(int64_t * durationUs)935 status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
936     Mutex::Autolock _l(mLock);
937     *durationUs = mDurationUs;
938     return OK;
939 }
940 
getTrackCount() const941 size_t NuPlayer::GenericSource::getTrackCount() const {
942     Mutex::Autolock _l(mLock);
943     return mSources.size();
944 }
945 
getTrackInfo(size_t trackIndex) const946 sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
947     Mutex::Autolock _l(mLock);
948     size_t trackCount = mSources.size();
949     if (trackIndex >= trackCount) {
950         return NULL;
951     }
952 
953     sp<AMessage> format = new AMessage();
954     sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
955     if (meta == NULL) {
956         ALOGE("no metadata for track %zu", trackIndex);
957         format->setInt32("type", MEDIA_TRACK_TYPE_UNKNOWN);
958         format->setString("mime", "application/octet-stream");
959         format->setString("language", "und");
960 
961         return format;
962     }
963 
964     const char *mime;
965     CHECK(meta->findCString(kKeyMIMEType, &mime));
966     format->setString("mime", mime);
967 
968     int32_t trackType;
969     if (!strncasecmp(mime, "video/", 6)) {
970         trackType = MEDIA_TRACK_TYPE_VIDEO;
971     } else if (!strncasecmp(mime, "audio/", 6)) {
972         trackType = MEDIA_TRACK_TYPE_AUDIO;
973     } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
974         trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
975     } else {
976         trackType = MEDIA_TRACK_TYPE_UNKNOWN;
977     }
978     format->setInt32("type", trackType);
979 
980     const char *lang;
981     if (!meta->findCString(kKeyMediaLanguage, &lang)) {
982         lang = "und";
983     }
984     format->setString("language", lang);
985 
986     if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
987         int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
988         meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
989         meta->findInt32(kKeyTrackIsDefault, &isDefault);
990         meta->findInt32(kKeyTrackIsForced, &isForced);
991 
992         format->setInt32("auto", !!isAutoselect);
993         format->setInt32("default", !!isDefault);
994         format->setInt32("forced", !!isForced);
995     } else if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
996         int32_t hapticChannelCount;
997         if (meta->findInt32(kKeyHapticChannelCount, &hapticChannelCount)) {
998             format->setInt32("haptic-channel-count", hapticChannelCount);
999         }
1000     }
1001 
1002     return format;
1003 }
1004 
getSelectedTrack(media_track_type type) const1005 ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
1006     Mutex::Autolock _l(mLock);
1007     const Track *track = NULL;
1008     switch (type) {
1009     case MEDIA_TRACK_TYPE_VIDEO:
1010         track = &mVideoTrack;
1011         break;
1012     case MEDIA_TRACK_TYPE_AUDIO:
1013         track = &mAudioTrack;
1014         break;
1015     case MEDIA_TRACK_TYPE_TIMEDTEXT:
1016         track = &mTimedTextTrack;
1017         break;
1018     case MEDIA_TRACK_TYPE_SUBTITLE:
1019         track = &mSubtitleTrack;
1020         break;
1021     default:
1022         break;
1023     }
1024 
1025     if (track != NULL && track->mSource != NULL) {
1026         return track->mIndex;
1027     }
1028 
1029     return -1;
1030 }
1031 
selectTrack(size_t trackIndex,bool select,int64_t timeUs)1032 status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
1033     Mutex::Autolock _l(mLock);
1034     ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
1035 
1036     if (trackIndex >= mSources.size()) {
1037         return BAD_INDEX;
1038     }
1039 
1040     if (!select) {
1041         Track* track = NULL;
1042         if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1043             track = &mSubtitleTrack;
1044             mFetchSubtitleDataGeneration++;
1045         } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1046             track = &mTimedTextTrack;
1047             mFetchTimedTextDataGeneration++;
1048         }
1049         if (track == NULL) {
1050             return INVALID_OPERATION;
1051         }
1052         track->mSource->stop();
1053         track->mSource = NULL;
1054         track->mPackets->clear();
1055         return OK;
1056     }
1057 
1058     const sp<IMediaSource> source = mSources.itemAt(trackIndex);
1059     sp<MetaData> meta = source->getFormat();
1060     const char *mime;
1061     CHECK(meta->findCString(kKeyMIMEType, &mime));
1062     if (!strncasecmp(mime, "text/", 5)) {
1063         bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1064         Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1065         if (track->mSource != NULL && track->mIndex == trackIndex) {
1066             return OK;
1067         }
1068         track->mIndex = trackIndex;
1069         if (track->mSource != NULL) {
1070             track->mSource->stop();
1071         }
1072         track->mSource = mSources.itemAt(trackIndex);
1073         track->mSource->start();
1074         if (track->mPackets == NULL) {
1075             track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
1076         } else {
1077             track->mPackets->clear();
1078             track->mPackets->setFormat(track->mSource->getFormat());
1079 
1080         }
1081 
1082         if (isSubtitle) {
1083             mFetchSubtitleDataGeneration++;
1084         } else {
1085             mFetchTimedTextDataGeneration++;
1086         }
1087 
1088         status_t eosResult; // ignored
1089         if (mSubtitleTrack.mSource != NULL
1090                 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1091             sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
1092             msg->setInt64("timeUs", timeUs);
1093             msg->setInt32("generation", mFetchSubtitleDataGeneration);
1094             msg->post();
1095         }
1096 
1097         sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this);
1098         msg2->setInt32("generation", mFetchTimedTextDataGeneration);
1099         msg2->post();
1100 
1101         if (mTimedTextTrack.mSource != NULL
1102                 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1103             sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
1104             msg->setInt64("timeUs", timeUs);
1105             msg->setInt32("generation", mFetchTimedTextDataGeneration);
1106             msg->post();
1107         }
1108 
1109         return OK;
1110     } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1111         bool audio = !strncasecmp(mime, "audio/", 6);
1112         Track *track = audio ? &mAudioTrack : &mVideoTrack;
1113         if (track->mSource != NULL && track->mIndex == trackIndex) {
1114             return OK;
1115         }
1116 
1117         sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
1118         msg->setInt32("trackIndex", trackIndex);
1119         msg->post();
1120         return OK;
1121     }
1122 
1123     return INVALID_OPERATION;
1124 }
1125 
seekTo(int64_t seekTimeUs,MediaPlayerSeekMode mode)1126 status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1127     ALOGV("seekTo: %lld, %d", (long long)seekTimeUs, mode);
1128     sp<AMessage> msg = new AMessage(kWhatSeek, this);
1129     msg->setInt64("seekTimeUs", seekTimeUs);
1130     msg->setInt32("mode", mode);
1131 
1132     // Need to call readBuffer on |mLooper| to ensure the calls to
1133     // IMediaSource::read* are serialized. Note that IMediaSource::read*
1134     // is called without |mLock| acquired and MediaSource is not thread safe.
1135     sp<AMessage> response;
1136     status_t err = msg->postAndAwaitResponse(&response);
1137     if (err == OK && response != NULL) {
1138         CHECK(response->findInt32("err", &err));
1139     }
1140 
1141     return err;
1142 }
1143 
onSeek(const sp<AMessage> & msg)1144 void NuPlayer::GenericSource::onSeek(const sp<AMessage>& msg) {
1145     int64_t seekTimeUs;
1146     int32_t mode;
1147     CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1148     CHECK(msg->findInt32("mode", &mode));
1149 
1150     sp<AMessage> response = new AMessage;
1151     status_t err = doSeek(seekTimeUs, (MediaPlayerSeekMode)mode);
1152     response->setInt32("err", err);
1153 
1154     sp<AReplyToken> replyID;
1155     CHECK(msg->senderAwaitsResponse(&replyID));
1156     response->postReply(replyID);
1157 }
1158 
doSeek(int64_t seekTimeUs,MediaPlayerSeekMode mode)1159 status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1160     if (mVideoTrack.mSource != NULL) {
1161         ++mVideoDataGeneration;
1162 
1163         int64_t actualTimeUs;
1164         readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, mode, &actualTimeUs);
1165 
1166         if (mode != MediaPlayerSeekMode::SEEK_CLOSEST) {
1167             seekTimeUs = std::max<int64_t>(0, actualTimeUs);
1168         }
1169         mVideoLastDequeueTimeUs = actualTimeUs;
1170     }
1171 
1172     if (mAudioTrack.mSource != NULL) {
1173         ++mAudioDataGeneration;
1174         readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs, MediaPlayerSeekMode::SEEK_CLOSEST);
1175         mAudioLastDequeueTimeUs = seekTimeUs;
1176     }
1177 
1178     if (mSubtitleTrack.mSource != NULL) {
1179         mSubtitleTrack.mPackets->clear();
1180         mFetchSubtitleDataGeneration++;
1181     }
1182 
1183     if (mTimedTextTrack.mSource != NULL) {
1184         mTimedTextTrack.mPackets->clear();
1185         mFetchTimedTextDataGeneration++;
1186     }
1187 
1188     ++mPollBufferingGeneration;
1189     schedulePollBuffering();
1190     return OK;
1191 }
1192 
mediaBufferToABuffer(MediaBufferBase * mb,media_track_type trackType)1193 sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1194         MediaBufferBase* mb,
1195         media_track_type trackType) {
1196     bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1197     size_t outLength = mb->range_length();
1198 
1199     if (audio && mAudioIsVorbis) {
1200         outLength += sizeof(int32_t);
1201     }
1202 
1203     sp<ABuffer> ab;
1204 
1205     if (mIsDrmProtected)   {
1206         // Modular DRM
1207         // Enabled for both video/audio so 1) media buffer is reused without extra copying
1208         // 2) meta data can be retrieved in onInputBufferFetched for calling queueSecureInputBuffer.
1209 
1210         // data is already provided in the buffer
1211         ab = new ABuffer(NULL, mb->range_length());
1212         ab->meta()->setObject("mediaBufferHolder", new MediaBufferHolder(mb));
1213 
1214         // Modular DRM: Required b/c of the above add_ref.
1215         // If ref>0, there must be an observer, or it'll crash at release().
1216         // TODO: MediaBuffer might need to be revised to ease such need.
1217         mb->setObserver(this);
1218         // Extra increment (since we want to keep mb alive and attached to ab beyond this function
1219         // call. This is to counter the effect of mb->release() towards the end.
1220         mb->add_ref();
1221 
1222     } else {
1223         ab = new ABuffer(outLength);
1224         memcpy(ab->data(),
1225                (const uint8_t *)mb->data() + mb->range_offset(),
1226                mb->range_length());
1227     }
1228 
1229     if (audio && mAudioIsVorbis) {
1230         int32_t numPageSamples;
1231         if (!mb->meta_data().findInt32(kKeyValidSamples, &numPageSamples)) {
1232             numPageSamples = -1;
1233         }
1234 
1235         uint8_t* abEnd = ab->data() + mb->range_length();
1236         memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1237     }
1238 
1239     sp<AMessage> meta = ab->meta();
1240 
1241     int64_t timeUs;
1242     CHECK(mb->meta_data().findInt64(kKeyTime, &timeUs));
1243     meta->setInt64("timeUs", timeUs);
1244 
1245     if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1246         int32_t layerId;
1247         if (mb->meta_data().findInt32(kKeyTemporalLayerId, &layerId)) {
1248             meta->setInt32("temporal-layer-id", layerId);
1249         }
1250     }
1251 
1252     if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1253         const char *mime;
1254         CHECK(mTimedTextTrack.mSource != NULL
1255                 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1256         meta->setString("mime", mime);
1257     }
1258 
1259     int64_t durationUs;
1260     if (mb->meta_data().findInt64(kKeyDuration, &durationUs)) {
1261         meta->setInt64("durationUs", durationUs);
1262     }
1263 
1264     if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1265         meta->setInt32("track-index", mSubtitleTrack.mIndex);
1266     }
1267 
1268     uint32_t dataType; // unused
1269     const void *seiData;
1270     size_t seiLength;
1271     if (mb->meta_data().findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1272         sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1273         meta->setBuffer("sei", sei);
1274     }
1275 
1276     const void *mpegUserDataPointer;
1277     size_t mpegUserDataLength;
1278     if (mb->meta_data().findData(
1279             kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
1280         sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
1281         meta->setBuffer("mpeg-user-data", mpegUserData);
1282     }
1283 
1284     mb->release();
1285     mb = NULL;
1286 
1287     return ab;
1288 }
1289 
getDataGeneration(media_track_type type) const1290 int32_t NuPlayer::GenericSource::getDataGeneration(media_track_type type) const {
1291     int32_t generation = -1;
1292     switch (type) {
1293     case MEDIA_TRACK_TYPE_VIDEO:
1294         generation = mVideoDataGeneration;
1295         break;
1296     case MEDIA_TRACK_TYPE_AUDIO:
1297         generation = mAudioDataGeneration;
1298         break;
1299     case MEDIA_TRACK_TYPE_TIMEDTEXT:
1300         generation = mFetchTimedTextDataGeneration;
1301         break;
1302     case MEDIA_TRACK_TYPE_SUBTITLE:
1303         generation = mFetchSubtitleDataGeneration;
1304         break;
1305     default:
1306         break;
1307     }
1308 
1309     return generation;
1310 }
1311 
postReadBuffer(media_track_type trackType)1312 void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
1313     if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1314         mPendingReadBufferTypes |= (1 << trackType);
1315         sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
1316         msg->setInt32("trackType", trackType);
1317         msg->post();
1318     }
1319 }
1320 
onReadBuffer(const sp<AMessage> & msg)1321 void NuPlayer::GenericSource::onReadBuffer(const sp<AMessage>& msg) {
1322     int32_t tmpType;
1323     CHECK(msg->findInt32("trackType", &tmpType));
1324     media_track_type trackType = (media_track_type)tmpType;
1325     mPendingReadBufferTypes &= ~(1 << trackType);
1326     readBuffer(trackType);
1327 }
1328 
readBuffer(media_track_type trackType,int64_t seekTimeUs,MediaPlayerSeekMode mode,int64_t * actualTimeUs,bool formatChange)1329 void NuPlayer::GenericSource::readBuffer(
1330         media_track_type trackType, int64_t seekTimeUs, MediaPlayerSeekMode mode,
1331         int64_t *actualTimeUs, bool formatChange) {
1332     Track *track;
1333     size_t maxBuffers = 1;
1334     switch (trackType) {
1335         case MEDIA_TRACK_TYPE_VIDEO:
1336             track = &mVideoTrack;
1337             maxBuffers = 8;  // too large of a number may influence seeks
1338             break;
1339         case MEDIA_TRACK_TYPE_AUDIO:
1340             track = &mAudioTrack;
1341             maxBuffers = 64;
1342             break;
1343         case MEDIA_TRACK_TYPE_SUBTITLE:
1344             track = &mSubtitleTrack;
1345             break;
1346         case MEDIA_TRACK_TYPE_TIMEDTEXT:
1347             track = &mTimedTextTrack;
1348             break;
1349         default:
1350             TRESPASS();
1351     }
1352 
1353     if (track->mSource == NULL) {
1354         return;
1355     }
1356 
1357     if (actualTimeUs) {
1358         *actualTimeUs = seekTimeUs;
1359     }
1360 
1361     MediaSource::ReadOptions options;
1362 
1363     bool seeking = false;
1364     if (seekTimeUs >= 0) {
1365         options.setSeekTo(seekTimeUs, mode);
1366         seeking = true;
1367     }
1368 
1369     const bool couldReadMultiple = (track->mSource->supportReadMultiple());
1370 
1371     if (couldReadMultiple) {
1372         options.setNonBlocking();
1373     }
1374 
1375     int32_t generation = getDataGeneration(trackType);
1376     for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
1377         Vector<MediaBufferBase *> mediaBuffers;
1378         status_t err = NO_ERROR;
1379 
1380         sp<IMediaSource> source = track->mSource;
1381         mLock.unlock();
1382         if (couldReadMultiple) {
1383             err = source->readMultiple(
1384                     &mediaBuffers, maxBuffers - numBuffers, &options);
1385         } else {
1386             MediaBufferBase *mbuf = NULL;
1387             err = source->read(&mbuf, &options);
1388             if (err == OK && mbuf != NULL) {
1389                 mediaBuffers.push_back(mbuf);
1390             }
1391         }
1392         mLock.lock();
1393 
1394         options.clearNonPersistent();
1395 
1396         size_t id = 0;
1397         size_t count = mediaBuffers.size();
1398 
1399         // in case track has been changed since we don't have lock for some time.
1400         if (generation != getDataGeneration(trackType)) {
1401             for (; id < count; ++id) {
1402                 mediaBuffers[id]->release();
1403             }
1404             break;
1405         }
1406 
1407         for (; id < count; ++id) {
1408             int64_t timeUs;
1409             MediaBufferBase *mbuf = mediaBuffers[id];
1410             if (!mbuf->meta_data().findInt64(kKeyTime, &timeUs)) {
1411                 mbuf->meta_data().dumpToLog();
1412                 track->mPackets->signalEOS(ERROR_MALFORMED);
1413                 break;
1414             }
1415             if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1416                 mAudioTimeUs = timeUs;
1417             } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1418                 mVideoTimeUs = timeUs;
1419             }
1420 
1421             queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
1422 
1423             sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType);
1424             if (numBuffers == 0 && actualTimeUs != nullptr) {
1425                 *actualTimeUs = timeUs;
1426             }
1427             if (seeking && buffer != nullptr) {
1428                 sp<AMessage> meta = buffer->meta();
1429                 if (meta != nullptr && mode == MediaPlayerSeekMode::SEEK_CLOSEST
1430                         && seekTimeUs > timeUs) {
1431                     sp<AMessage> extra = new AMessage;
1432                     extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1433                     meta->setMessage("extra", extra);
1434                 }
1435             }
1436 
1437             track->mPackets->queueAccessUnit(buffer);
1438             formatChange = false;
1439             seeking = false;
1440             ++numBuffers;
1441         }
1442         if (id < count) {
1443             // Error, some mediaBuffer doesn't have kKeyTime.
1444             for (; id < count; ++id) {
1445                 mediaBuffers[id]->release();
1446             }
1447             break;
1448         }
1449 
1450         if (err == WOULD_BLOCK) {
1451             break;
1452         } else if (err == INFO_FORMAT_CHANGED) {
1453 #if 0
1454             track->mPackets->queueDiscontinuity(
1455                     ATSParser::DISCONTINUITY_FORMATCHANGE,
1456                     NULL,
1457                     false /* discard */);
1458 #endif
1459         } else if (err != OK) {
1460             queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
1461             track->mPackets->signalEOS(err);
1462             break;
1463         }
1464     }
1465 
1466     if (mIsStreaming
1467         && (trackType == MEDIA_TRACK_TYPE_VIDEO || trackType == MEDIA_TRACK_TYPE_AUDIO)) {
1468         status_t finalResult;
1469         int64_t durationUs = track->mPackets->getBufferedDurationUs(&finalResult);
1470 
1471         // TODO: maxRebufferingMarkMs could be larger than
1472         // mBufferingSettings.mResumePlaybackMarkMs
1473         int64_t markUs = (mPreparing ? mBufferingSettings.mInitialMarkMs
1474             : mBufferingSettings.mResumePlaybackMarkMs) * 1000LL;
1475         if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) {
1476             if (mPreparing || mSentPauseOnBuffering) {
1477                 Track *counterTrack =
1478                     (trackType == MEDIA_TRACK_TYPE_VIDEO ? &mAudioTrack : &mVideoTrack);
1479                 if (counterTrack->mSource != NULL) {
1480                     durationUs = counterTrack->mPackets->getBufferedDurationUs(&finalResult);
1481                 }
1482                 if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) {
1483                     if (mPreparing) {
1484                         notifyPrepared();
1485                         mPreparing = false;
1486                     } else {
1487                         sendCacheStats();
1488                         mSentPauseOnBuffering = false;
1489                         sp<AMessage> notify = dupNotify();
1490                         notify->setInt32("what", kWhatResumeOnBufferingEnd);
1491                         notify->post();
1492                     }
1493                 }
1494             }
1495             return;
1496         }
1497 
1498         postReadBuffer(trackType);
1499     }
1500 }
1501 
queueDiscontinuityIfNeeded(bool seeking,bool formatChange,media_track_type trackType,Track * track)1502 void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1503         bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1504     // formatChange && seeking: track whose source is changed during selection
1505     // formatChange && !seeking: track whose source is not changed during selection
1506     // !formatChange: normal seek
1507     if ((seeking || formatChange)
1508             && (trackType == MEDIA_TRACK_TYPE_AUDIO
1509             || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1510         ATSParser::DiscontinuityType type = (formatChange && seeking)
1511                 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1512                 : ATSParser::DISCONTINUITY_NONE;
1513         track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1514     }
1515 }
1516 
notifyBufferingUpdate(int32_t percentage)1517 void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
1518     // Buffering percent could go backward as it's estimated from remaining
1519     // data and last access time. This could cause the buffering position
1520     // drawn on media control to jitter slightly. Remember previously reported
1521     // percentage and don't allow it to go backward.
1522     if (percentage < mPrevBufferPercentage) {
1523         percentage = mPrevBufferPercentage;
1524     } else if (percentage > 100) {
1525         percentage = 100;
1526     }
1527 
1528     mPrevBufferPercentage = percentage;
1529 
1530     ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
1531 
1532     sp<AMessage> notify = dupNotify();
1533     notify->setInt32("what", kWhatBufferingUpdate);
1534     notify->setInt32("percentage", percentage);
1535     notify->post();
1536 }
1537 
schedulePollBuffering()1538 void NuPlayer::GenericSource::schedulePollBuffering() {
1539     sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
1540     msg->setInt32("generation", mPollBufferingGeneration);
1541     // Enquires buffering status every second.
1542     msg->post(1000000LL);
1543 }
1544 
onPollBuffering()1545 void NuPlayer::GenericSource::onPollBuffering() {
1546     status_t finalStatus = UNKNOWN_ERROR;
1547     int64_t cachedDurationUs = -1LL;
1548     ssize_t cachedDataRemaining = -1;
1549 
1550     if (mCachedSource != NULL) {
1551         cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus);
1552 
1553         if (finalStatus == OK) {
1554             off64_t size;
1555             int64_t bitrate = 0LL;
1556             if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
1557                 // |bitrate| uses bits/second unit, while size is number of bytes.
1558                 bitrate = size * 8000000LL / mDurationUs;
1559             } else if (mBitrate > 0) {
1560                 bitrate = mBitrate;
1561             }
1562             if (bitrate > 0) {
1563                 cachedDurationUs = cachedDataRemaining * 8000000LL / bitrate;
1564             }
1565         }
1566     }
1567 
1568     if (finalStatus != OK) {
1569         ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
1570 
1571         if (finalStatus == ERROR_END_OF_STREAM) {
1572             notifyBufferingUpdate(100);
1573         }
1574 
1575         if (mPreparing) {
1576             notifyPreparedAndCleanup(finalStatus == ERROR_END_OF_STREAM ? OK : finalStatus);
1577             mPreparing = false;
1578         } else if (mSentPauseOnBuffering) {
1579             sendCacheStats();
1580             mSentPauseOnBuffering = false;
1581             sp<AMessage> notify = dupNotify();
1582             notify->setInt32("what", kWhatResumeOnBufferingEnd);
1583             notify->post();
1584         }
1585         return;
1586     }
1587 
1588     if (cachedDurationUs >= 0LL) {
1589         if (mDurationUs > 0LL) {
1590             int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
1591             int percentage = 100.0 * cachedPosUs / mDurationUs;
1592             if (percentage > 100) {
1593                 percentage = 100;
1594             }
1595 
1596             notifyBufferingUpdate(percentage);
1597         }
1598 
1599         ALOGV("onPollBuffering: cachedDurationUs %.1f sec", cachedDurationUs / 1000000.0f);
1600     }
1601 
1602     schedulePollBuffering();
1603 }
1604 
1605 // Modular DRM
prepareDrm(const uint8_t uuid[16],const Vector<uint8_t> & drmSessionId,sp<ICrypto> * outCrypto)1606 status_t NuPlayer::GenericSource::prepareDrm(
1607         const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId, sp<ICrypto> *outCrypto) {
1608     Mutex::Autolock _l(mLock);
1609     ALOGV("prepareDrm");
1610 
1611     mIsDrmProtected = false;
1612     mIsDrmReleased = false;
1613     mIsSecure = false;
1614 
1615     status_t status = OK;
1616     sp<ICrypto> crypto = NuPlayerDrm::createCryptoAndPlugin(uuid, drmSessionId, status);
1617     if (crypto == NULL) {
1618         ALOGE("prepareDrm: createCrypto failed. status: %d", status);
1619         return status;
1620     }
1621     ALOGV("prepareDrm: createCryptoAndPlugin succeeded for uuid: %s",
1622             DrmUUID::toHexString(uuid).c_str());
1623 
1624     *outCrypto = crypto;
1625     // as long a there is an active crypto
1626     mIsDrmProtected = true;
1627 
1628     if (mMimes.size() == 0) {
1629         status = UNKNOWN_ERROR;
1630         ALOGE("prepareDrm: Unexpected. Must have at least one track. status: %d", status);
1631         return status;
1632     }
1633 
1634     // first mime in this list is either the video track, or the first audio track
1635     const char *mime = mMimes[0].c_str();
1636     mIsSecure = crypto->requiresSecureDecoderComponent(mime);
1637     ALOGV("prepareDrm: requiresSecureDecoderComponent mime: %s  isSecure: %d",
1638             mime, mIsSecure);
1639 
1640     // Checking the member flags while in the looper to send out the notification.
1641     // The legacy mDecryptHandle!=NULL check (for FLAG_PROTECTED) is equivalent to mIsDrmProtected.
1642     notifyFlagsChanged(
1643             (mIsSecure ? FLAG_SECURE : 0) |
1644             // Setting "protected screen" only for L1: b/38390836
1645             (mIsSecure ? FLAG_PROTECTED : 0) |
1646             FLAG_CAN_PAUSE |
1647             FLAG_CAN_SEEK_BACKWARD |
1648             FLAG_CAN_SEEK_FORWARD |
1649             FLAG_CAN_SEEK);
1650 
1651     if (status == OK) {
1652         ALOGV("prepareDrm: mCrypto: %p (%d)", outCrypto->get(),
1653                 (*outCrypto != NULL ? (*outCrypto)->getStrongCount() : 0));
1654         ALOGD("prepareDrm ret: %d ", status);
1655     } else {
1656         ALOGE("prepareDrm err: %d", status);
1657     }
1658     return status;
1659 }
1660 
releaseDrm()1661 status_t NuPlayer::GenericSource::releaseDrm() {
1662     Mutex::Autolock _l(mLock);
1663     ALOGV("releaseDrm");
1664 
1665     if (mIsDrmProtected) {
1666         mIsDrmProtected = false;
1667         // to prevent returning any more buffer after stop/releaseDrm (b/37960096)
1668         mIsDrmReleased = true;
1669         ALOGV("releaseDrm: mIsDrmProtected is reset.");
1670     } else {
1671         ALOGE("releaseDrm: mIsDrmProtected is already false.");
1672     }
1673 
1674     return OK;
1675 }
1676 
checkDrmInfo()1677 status_t NuPlayer::GenericSource::checkDrmInfo()
1678 {
1679     // clearing the flag at prepare in case the player is reused after stop/releaseDrm with the
1680     // same source without being reset (called by prepareAsync/initFromDataSource)
1681     mIsDrmReleased = false;
1682 
1683     if (mFileMeta == NULL) {
1684         ALOGI("checkDrmInfo: No metadata");
1685         return OK; // letting the caller responds accordingly
1686     }
1687 
1688     uint32_t type;
1689     const void *pssh;
1690     size_t psshsize;
1691 
1692     if (!mFileMeta->findData(kKeyPssh, &type, &pssh, &psshsize)) {
1693         ALOGV("checkDrmInfo: No PSSH");
1694         return OK; // source without DRM info
1695     }
1696 
1697     Parcel parcel;
1698     NuPlayerDrm::retrieveDrmInfo(pssh, psshsize, &parcel);
1699     ALOGV("checkDrmInfo: MEDIA_DRM_INFO PSSH size: %d  Parcel size: %d  objects#: %d",
1700           (int)psshsize, (int)parcel.dataSize(), (int)parcel.objectsCount());
1701 
1702     if (parcel.dataSize() == 0) {
1703         ALOGE("checkDrmInfo: Unexpected parcel size: 0");
1704         return UNKNOWN_ERROR;
1705     }
1706 
1707     // Can't pass parcel as a message to the player. Converting Parcel->ABuffer to pass it
1708     // to the Player's onSourceNotify then back to Parcel for calling driver's notifyListener.
1709     sp<ABuffer> drmInfoBuffer = ABuffer::CreateAsCopy(parcel.data(), parcel.dataSize());
1710     notifyDrmInfo(drmInfoBuffer);
1711 
1712     return OK;
1713 }
1714 
signalBufferReturned(MediaBufferBase * buffer)1715 void NuPlayer::GenericSource::signalBufferReturned(MediaBufferBase *buffer)
1716 {
1717     //ALOGV("signalBufferReturned %p  refCount: %d", buffer, buffer->localRefcount());
1718 
1719     buffer->setObserver(NULL);
1720     buffer->release(); // this leads to delete since that there is no observor
1721 }
1722 
1723 }  // namespace android
1724