• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "AnotherPacketSource.h"
24 #include <binder/IServiceManager.h>
25 #include <cutils/properties.h>
26 #include <datasource/PlayerServiceDataSourceFactory.h>
27 #include <datasource/PlayerServiceFileSource.h>
28 #include <datasource/HTTPBase.h>
29 #include <datasource/NuCachedSource2.h>
30 #include <media/DataSource.h>
31 #include <media/MediaBufferHolder.h>
32 #include <media/stagefright/MediaSource.h>
33 #include <android/IMediaExtractorService.h>
34 #include <media/IMediaHTTPService.h>
35 #include <media/stagefright/foundation/ABuffer.h>
36 #include <media/stagefright/foundation/ADebug.h>
37 #include <media/stagefright/foundation/AMessage.h>
38 #include <media/stagefright/InterfaceUtils.h>
39 #include <media/stagefright/FoundationUtils.h>
40 #include <media/stagefright/MediaBuffer.h>
41 #include <media/stagefright/MediaClock.h>
42 #include <media/stagefright/MediaDefs.h>
43 #include <media/stagefright/MediaExtractor.h>
44 #include <media/stagefright/MediaExtractorFactory.h>
45 #include <media/stagefright/MetaData.h>
46 #include <media/stagefright/Utils.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].string()));
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().string());
296     return OK;
297 }
298 
setBufferingSettings(const BufferingSettings & buffering)299 status_t NuPlayer::GenericSource::setBufferingSettings(const BufferingSettings& buffering) {
300     ALOGV("setBufferingSettings{%s}", buffering.toString().string());
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     }
996 
997     return format;
998 }
999 
getSelectedTrack(media_track_type type) const1000 ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
1001     Mutex::Autolock _l(mLock);
1002     const Track *track = NULL;
1003     switch (type) {
1004     case MEDIA_TRACK_TYPE_VIDEO:
1005         track = &mVideoTrack;
1006         break;
1007     case MEDIA_TRACK_TYPE_AUDIO:
1008         track = &mAudioTrack;
1009         break;
1010     case MEDIA_TRACK_TYPE_TIMEDTEXT:
1011         track = &mTimedTextTrack;
1012         break;
1013     case MEDIA_TRACK_TYPE_SUBTITLE:
1014         track = &mSubtitleTrack;
1015         break;
1016     default:
1017         break;
1018     }
1019 
1020     if (track != NULL && track->mSource != NULL) {
1021         return track->mIndex;
1022     }
1023 
1024     return -1;
1025 }
1026 
selectTrack(size_t trackIndex,bool select,int64_t timeUs)1027 status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
1028     Mutex::Autolock _l(mLock);
1029     ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
1030 
1031     if (trackIndex >= mSources.size()) {
1032         return BAD_INDEX;
1033     }
1034 
1035     if (!select) {
1036         Track* track = NULL;
1037         if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1038             track = &mSubtitleTrack;
1039             mFetchSubtitleDataGeneration++;
1040         } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1041             track = &mTimedTextTrack;
1042             mFetchTimedTextDataGeneration++;
1043         }
1044         if (track == NULL) {
1045             return INVALID_OPERATION;
1046         }
1047         track->mSource->stop();
1048         track->mSource = NULL;
1049         track->mPackets->clear();
1050         return OK;
1051     }
1052 
1053     const sp<IMediaSource> source = mSources.itemAt(trackIndex);
1054     sp<MetaData> meta = source->getFormat();
1055     const char *mime;
1056     CHECK(meta->findCString(kKeyMIMEType, &mime));
1057     if (!strncasecmp(mime, "text/", 5)) {
1058         bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1059         Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1060         if (track->mSource != NULL && track->mIndex == trackIndex) {
1061             return OK;
1062         }
1063         track->mIndex = trackIndex;
1064         if (track->mSource != NULL) {
1065             track->mSource->stop();
1066         }
1067         track->mSource = mSources.itemAt(trackIndex);
1068         track->mSource->start();
1069         if (track->mPackets == NULL) {
1070             track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
1071         } else {
1072             track->mPackets->clear();
1073             track->mPackets->setFormat(track->mSource->getFormat());
1074 
1075         }
1076 
1077         if (isSubtitle) {
1078             mFetchSubtitleDataGeneration++;
1079         } else {
1080             mFetchTimedTextDataGeneration++;
1081         }
1082 
1083         status_t eosResult; // ignored
1084         if (mSubtitleTrack.mSource != NULL
1085                 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1086             sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
1087             msg->setInt64("timeUs", timeUs);
1088             msg->setInt32("generation", mFetchSubtitleDataGeneration);
1089             msg->post();
1090         }
1091 
1092         sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this);
1093         msg2->setInt32("generation", mFetchTimedTextDataGeneration);
1094         msg2->post();
1095 
1096         if (mTimedTextTrack.mSource != NULL
1097                 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1098             sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
1099             msg->setInt64("timeUs", timeUs);
1100             msg->setInt32("generation", mFetchTimedTextDataGeneration);
1101             msg->post();
1102         }
1103 
1104         return OK;
1105     } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1106         bool audio = !strncasecmp(mime, "audio/", 6);
1107         Track *track = audio ? &mAudioTrack : &mVideoTrack;
1108         if (track->mSource != NULL && track->mIndex == trackIndex) {
1109             return OK;
1110         }
1111 
1112         sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
1113         msg->setInt32("trackIndex", trackIndex);
1114         msg->post();
1115         return OK;
1116     }
1117 
1118     return INVALID_OPERATION;
1119 }
1120 
seekTo(int64_t seekTimeUs,MediaPlayerSeekMode mode)1121 status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1122     ALOGV("seekTo: %lld, %d", (long long)seekTimeUs, mode);
1123     sp<AMessage> msg = new AMessage(kWhatSeek, this);
1124     msg->setInt64("seekTimeUs", seekTimeUs);
1125     msg->setInt32("mode", mode);
1126 
1127     // Need to call readBuffer on |mLooper| to ensure the calls to
1128     // IMediaSource::read* are serialized. Note that IMediaSource::read*
1129     // is called without |mLock| acquired and MediaSource is not thread safe.
1130     sp<AMessage> response;
1131     status_t err = msg->postAndAwaitResponse(&response);
1132     if (err == OK && response != NULL) {
1133         CHECK(response->findInt32("err", &err));
1134     }
1135 
1136     return err;
1137 }
1138 
onSeek(const sp<AMessage> & msg)1139 void NuPlayer::GenericSource::onSeek(const sp<AMessage>& msg) {
1140     int64_t seekTimeUs;
1141     int32_t mode;
1142     CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1143     CHECK(msg->findInt32("mode", &mode));
1144 
1145     sp<AMessage> response = new AMessage;
1146     status_t err = doSeek(seekTimeUs, (MediaPlayerSeekMode)mode);
1147     response->setInt32("err", err);
1148 
1149     sp<AReplyToken> replyID;
1150     CHECK(msg->senderAwaitsResponse(&replyID));
1151     response->postReply(replyID);
1152 }
1153 
doSeek(int64_t seekTimeUs,MediaPlayerSeekMode mode)1154 status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode) {
1155     if (mVideoTrack.mSource != NULL) {
1156         ++mVideoDataGeneration;
1157 
1158         int64_t actualTimeUs;
1159         readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, mode, &actualTimeUs);
1160 
1161         if (mode != MediaPlayerSeekMode::SEEK_CLOSEST) {
1162             seekTimeUs = std::max<int64_t>(0, actualTimeUs);
1163         }
1164         mVideoLastDequeueTimeUs = actualTimeUs;
1165     }
1166 
1167     if (mAudioTrack.mSource != NULL) {
1168         ++mAudioDataGeneration;
1169         readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs, MediaPlayerSeekMode::SEEK_CLOSEST);
1170         mAudioLastDequeueTimeUs = seekTimeUs;
1171     }
1172 
1173     if (mSubtitleTrack.mSource != NULL) {
1174         mSubtitleTrack.mPackets->clear();
1175         mFetchSubtitleDataGeneration++;
1176     }
1177 
1178     if (mTimedTextTrack.mSource != NULL) {
1179         mTimedTextTrack.mPackets->clear();
1180         mFetchTimedTextDataGeneration++;
1181     }
1182 
1183     ++mPollBufferingGeneration;
1184     schedulePollBuffering();
1185     return OK;
1186 }
1187 
mediaBufferToABuffer(MediaBufferBase * mb,media_track_type trackType)1188 sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1189         MediaBufferBase* mb,
1190         media_track_type trackType) {
1191     bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1192     size_t outLength = mb->range_length();
1193 
1194     if (audio && mAudioIsVorbis) {
1195         outLength += sizeof(int32_t);
1196     }
1197 
1198     sp<ABuffer> ab;
1199 
1200     if (mIsDrmProtected)   {
1201         // Modular DRM
1202         // Enabled for both video/audio so 1) media buffer is reused without extra copying
1203         // 2) meta data can be retrieved in onInputBufferFetched for calling queueSecureInputBuffer.
1204 
1205         // data is already provided in the buffer
1206         ab = new ABuffer(NULL, mb->range_length());
1207         ab->meta()->setObject("mediaBufferHolder", new MediaBufferHolder(mb));
1208 
1209         // Modular DRM: Required b/c of the above add_ref.
1210         // If ref>0, there must be an observer, or it'll crash at release().
1211         // TODO: MediaBuffer might need to be revised to ease such need.
1212         mb->setObserver(this);
1213         // Extra increment (since we want to keep mb alive and attached to ab beyond this function
1214         // call. This is to counter the effect of mb->release() towards the end.
1215         mb->add_ref();
1216 
1217     } else {
1218         ab = new ABuffer(outLength);
1219         memcpy(ab->data(),
1220                (const uint8_t *)mb->data() + mb->range_offset(),
1221                mb->range_length());
1222     }
1223 
1224     if (audio && mAudioIsVorbis) {
1225         int32_t numPageSamples;
1226         if (!mb->meta_data().findInt32(kKeyValidSamples, &numPageSamples)) {
1227             numPageSamples = -1;
1228         }
1229 
1230         uint8_t* abEnd = ab->data() + mb->range_length();
1231         memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1232     }
1233 
1234     sp<AMessage> meta = ab->meta();
1235 
1236     int64_t timeUs;
1237     CHECK(mb->meta_data().findInt64(kKeyTime, &timeUs));
1238     meta->setInt64("timeUs", timeUs);
1239 
1240     if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1241         int32_t layerId;
1242         if (mb->meta_data().findInt32(kKeyTemporalLayerId, &layerId)) {
1243             meta->setInt32("temporal-layer-id", layerId);
1244         }
1245     }
1246 
1247     if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1248         const char *mime;
1249         CHECK(mTimedTextTrack.mSource != NULL
1250                 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1251         meta->setString("mime", mime);
1252     }
1253 
1254     int64_t durationUs;
1255     if (mb->meta_data().findInt64(kKeyDuration, &durationUs)) {
1256         meta->setInt64("durationUs", durationUs);
1257     }
1258 
1259     if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1260         meta->setInt32("track-index", mSubtitleTrack.mIndex);
1261     }
1262 
1263     uint32_t dataType; // unused
1264     const void *seiData;
1265     size_t seiLength;
1266     if (mb->meta_data().findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1267         sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1268         meta->setBuffer("sei", sei);
1269     }
1270 
1271     const void *mpegUserDataPointer;
1272     size_t mpegUserDataLength;
1273     if (mb->meta_data().findData(
1274             kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
1275         sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
1276         meta->setBuffer("mpeg-user-data", mpegUserData);
1277     }
1278 
1279     mb->release();
1280     mb = NULL;
1281 
1282     return ab;
1283 }
1284 
getDataGeneration(media_track_type type) const1285 int32_t NuPlayer::GenericSource::getDataGeneration(media_track_type type) const {
1286     int32_t generation = -1;
1287     switch (type) {
1288     case MEDIA_TRACK_TYPE_VIDEO:
1289         generation = mVideoDataGeneration;
1290         break;
1291     case MEDIA_TRACK_TYPE_AUDIO:
1292         generation = mAudioDataGeneration;
1293         break;
1294     case MEDIA_TRACK_TYPE_TIMEDTEXT:
1295         generation = mFetchTimedTextDataGeneration;
1296         break;
1297     case MEDIA_TRACK_TYPE_SUBTITLE:
1298         generation = mFetchSubtitleDataGeneration;
1299         break;
1300     default:
1301         break;
1302     }
1303 
1304     return generation;
1305 }
1306 
postReadBuffer(media_track_type trackType)1307 void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
1308     if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1309         mPendingReadBufferTypes |= (1 << trackType);
1310         sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
1311         msg->setInt32("trackType", trackType);
1312         msg->post();
1313     }
1314 }
1315 
onReadBuffer(const sp<AMessage> & msg)1316 void NuPlayer::GenericSource::onReadBuffer(const sp<AMessage>& msg) {
1317     int32_t tmpType;
1318     CHECK(msg->findInt32("trackType", &tmpType));
1319     media_track_type trackType = (media_track_type)tmpType;
1320     mPendingReadBufferTypes &= ~(1 << trackType);
1321     readBuffer(trackType);
1322 }
1323 
readBuffer(media_track_type trackType,int64_t seekTimeUs,MediaPlayerSeekMode mode,int64_t * actualTimeUs,bool formatChange)1324 void NuPlayer::GenericSource::readBuffer(
1325         media_track_type trackType, int64_t seekTimeUs, MediaPlayerSeekMode mode,
1326         int64_t *actualTimeUs, bool formatChange) {
1327     Track *track;
1328     size_t maxBuffers = 1;
1329     switch (trackType) {
1330         case MEDIA_TRACK_TYPE_VIDEO:
1331             track = &mVideoTrack;
1332             maxBuffers = 8;  // too large of a number may influence seeks
1333             break;
1334         case MEDIA_TRACK_TYPE_AUDIO:
1335             track = &mAudioTrack;
1336             maxBuffers = 64;
1337             break;
1338         case MEDIA_TRACK_TYPE_SUBTITLE:
1339             track = &mSubtitleTrack;
1340             break;
1341         case MEDIA_TRACK_TYPE_TIMEDTEXT:
1342             track = &mTimedTextTrack;
1343             break;
1344         default:
1345             TRESPASS();
1346     }
1347 
1348     if (track->mSource == NULL) {
1349         return;
1350     }
1351 
1352     if (actualTimeUs) {
1353         *actualTimeUs = seekTimeUs;
1354     }
1355 
1356     MediaSource::ReadOptions options;
1357 
1358     bool seeking = false;
1359     if (seekTimeUs >= 0) {
1360         options.setSeekTo(seekTimeUs, mode);
1361         seeking = true;
1362     }
1363 
1364     const bool couldReadMultiple = (track->mSource->supportReadMultiple());
1365 
1366     if (couldReadMultiple) {
1367         options.setNonBlocking();
1368     }
1369 
1370     int32_t generation = getDataGeneration(trackType);
1371     for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
1372         Vector<MediaBufferBase *> mediaBuffers;
1373         status_t err = NO_ERROR;
1374 
1375         sp<IMediaSource> source = track->mSource;
1376         mLock.unlock();
1377         if (couldReadMultiple) {
1378             err = source->readMultiple(
1379                     &mediaBuffers, maxBuffers - numBuffers, &options);
1380         } else {
1381             MediaBufferBase *mbuf = NULL;
1382             err = source->read(&mbuf, &options);
1383             if (err == OK && mbuf != NULL) {
1384                 mediaBuffers.push_back(mbuf);
1385             }
1386         }
1387         mLock.lock();
1388 
1389         options.clearNonPersistent();
1390 
1391         size_t id = 0;
1392         size_t count = mediaBuffers.size();
1393 
1394         // in case track has been changed since we don't have lock for some time.
1395         if (generation != getDataGeneration(trackType)) {
1396             for (; id < count; ++id) {
1397                 mediaBuffers[id]->release();
1398             }
1399             break;
1400         }
1401 
1402         for (; id < count; ++id) {
1403             int64_t timeUs;
1404             MediaBufferBase *mbuf = mediaBuffers[id];
1405             if (!mbuf->meta_data().findInt64(kKeyTime, &timeUs)) {
1406                 mbuf->meta_data().dumpToLog();
1407                 track->mPackets->signalEOS(ERROR_MALFORMED);
1408                 break;
1409             }
1410             if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1411                 mAudioTimeUs = timeUs;
1412             } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1413                 mVideoTimeUs = timeUs;
1414             }
1415 
1416             queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
1417 
1418             sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType);
1419             if (numBuffers == 0 && actualTimeUs != nullptr) {
1420                 *actualTimeUs = timeUs;
1421             }
1422             if (seeking && buffer != nullptr) {
1423                 sp<AMessage> meta = buffer->meta();
1424                 if (meta != nullptr && mode == MediaPlayerSeekMode::SEEK_CLOSEST
1425                         && seekTimeUs > timeUs) {
1426                     sp<AMessage> extra = new AMessage;
1427                     extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1428                     meta->setMessage("extra", extra);
1429                 }
1430             }
1431 
1432             track->mPackets->queueAccessUnit(buffer);
1433             formatChange = false;
1434             seeking = false;
1435             ++numBuffers;
1436         }
1437         if (id < count) {
1438             // Error, some mediaBuffer doesn't have kKeyTime.
1439             for (; id < count; ++id) {
1440                 mediaBuffers[id]->release();
1441             }
1442             break;
1443         }
1444 
1445         if (err == WOULD_BLOCK) {
1446             break;
1447         } else if (err == INFO_FORMAT_CHANGED) {
1448 #if 0
1449             track->mPackets->queueDiscontinuity(
1450                     ATSParser::DISCONTINUITY_FORMATCHANGE,
1451                     NULL,
1452                     false /* discard */);
1453 #endif
1454         } else if (err != OK) {
1455             queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
1456             track->mPackets->signalEOS(err);
1457             break;
1458         }
1459     }
1460 
1461     if (mIsStreaming
1462         && (trackType == MEDIA_TRACK_TYPE_VIDEO || trackType == MEDIA_TRACK_TYPE_AUDIO)) {
1463         status_t finalResult;
1464         int64_t durationUs = track->mPackets->getBufferedDurationUs(&finalResult);
1465 
1466         // TODO: maxRebufferingMarkMs could be larger than
1467         // mBufferingSettings.mResumePlaybackMarkMs
1468         int64_t markUs = (mPreparing ? mBufferingSettings.mInitialMarkMs
1469             : mBufferingSettings.mResumePlaybackMarkMs) * 1000LL;
1470         if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) {
1471             if (mPreparing || mSentPauseOnBuffering) {
1472                 Track *counterTrack =
1473                     (trackType == MEDIA_TRACK_TYPE_VIDEO ? &mAudioTrack : &mVideoTrack);
1474                 if (counterTrack->mSource != NULL) {
1475                     durationUs = counterTrack->mPackets->getBufferedDurationUs(&finalResult);
1476                 }
1477                 if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) {
1478                     if (mPreparing) {
1479                         notifyPrepared();
1480                         mPreparing = false;
1481                     } else {
1482                         sendCacheStats();
1483                         mSentPauseOnBuffering = false;
1484                         sp<AMessage> notify = dupNotify();
1485                         notify->setInt32("what", kWhatResumeOnBufferingEnd);
1486                         notify->post();
1487                     }
1488                 }
1489             }
1490             return;
1491         }
1492 
1493         postReadBuffer(trackType);
1494     }
1495 }
1496 
queueDiscontinuityIfNeeded(bool seeking,bool formatChange,media_track_type trackType,Track * track)1497 void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1498         bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1499     // formatChange && seeking: track whose source is changed during selection
1500     // formatChange && !seeking: track whose source is not changed during selection
1501     // !formatChange: normal seek
1502     if ((seeking || formatChange)
1503             && (trackType == MEDIA_TRACK_TYPE_AUDIO
1504             || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1505         ATSParser::DiscontinuityType type = (formatChange && seeking)
1506                 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1507                 : ATSParser::DISCONTINUITY_NONE;
1508         track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1509     }
1510 }
1511 
notifyBufferingUpdate(int32_t percentage)1512 void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
1513     // Buffering percent could go backward as it's estimated from remaining
1514     // data and last access time. This could cause the buffering position
1515     // drawn on media control to jitter slightly. Remember previously reported
1516     // percentage and don't allow it to go backward.
1517     if (percentage < mPrevBufferPercentage) {
1518         percentage = mPrevBufferPercentage;
1519     } else if (percentage > 100) {
1520         percentage = 100;
1521     }
1522 
1523     mPrevBufferPercentage = percentage;
1524 
1525     ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
1526 
1527     sp<AMessage> notify = dupNotify();
1528     notify->setInt32("what", kWhatBufferingUpdate);
1529     notify->setInt32("percentage", percentage);
1530     notify->post();
1531 }
1532 
schedulePollBuffering()1533 void NuPlayer::GenericSource::schedulePollBuffering() {
1534     sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
1535     msg->setInt32("generation", mPollBufferingGeneration);
1536     // Enquires buffering status every second.
1537     msg->post(1000000LL);
1538 }
1539 
onPollBuffering()1540 void NuPlayer::GenericSource::onPollBuffering() {
1541     status_t finalStatus = UNKNOWN_ERROR;
1542     int64_t cachedDurationUs = -1LL;
1543     ssize_t cachedDataRemaining = -1;
1544 
1545     if (mCachedSource != NULL) {
1546         cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus);
1547 
1548         if (finalStatus == OK) {
1549             off64_t size;
1550             int64_t bitrate = 0LL;
1551             if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
1552                 // |bitrate| uses bits/second unit, while size is number of bytes.
1553                 bitrate = size * 8000000LL / mDurationUs;
1554             } else if (mBitrate > 0) {
1555                 bitrate = mBitrate;
1556             }
1557             if (bitrate > 0) {
1558                 cachedDurationUs = cachedDataRemaining * 8000000LL / bitrate;
1559             }
1560         }
1561     }
1562 
1563     if (finalStatus != OK) {
1564         ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
1565 
1566         if (finalStatus == ERROR_END_OF_STREAM) {
1567             notifyBufferingUpdate(100);
1568         }
1569 
1570         if (mPreparing) {
1571             notifyPreparedAndCleanup(finalStatus == ERROR_END_OF_STREAM ? OK : finalStatus);
1572             mPreparing = false;
1573         } else if (mSentPauseOnBuffering) {
1574             sendCacheStats();
1575             mSentPauseOnBuffering = false;
1576             sp<AMessage> notify = dupNotify();
1577             notify->setInt32("what", kWhatResumeOnBufferingEnd);
1578             notify->post();
1579         }
1580         return;
1581     }
1582 
1583     if (cachedDurationUs >= 0LL) {
1584         if (mDurationUs > 0LL) {
1585             int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
1586             int percentage = 100.0 * cachedPosUs / mDurationUs;
1587             if (percentage > 100) {
1588                 percentage = 100;
1589             }
1590 
1591             notifyBufferingUpdate(percentage);
1592         }
1593 
1594         ALOGV("onPollBuffering: cachedDurationUs %.1f sec", cachedDurationUs / 1000000.0f);
1595     }
1596 
1597     schedulePollBuffering();
1598 }
1599 
1600 // Modular DRM
prepareDrm(const uint8_t uuid[16],const Vector<uint8_t> & drmSessionId,sp<ICrypto> * outCrypto)1601 status_t NuPlayer::GenericSource::prepareDrm(
1602         const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId, sp<ICrypto> *outCrypto) {
1603     Mutex::Autolock _l(mLock);
1604     ALOGV("prepareDrm");
1605 
1606     mIsDrmProtected = false;
1607     mIsDrmReleased = false;
1608     mIsSecure = false;
1609 
1610     status_t status = OK;
1611     sp<ICrypto> crypto = NuPlayerDrm::createCryptoAndPlugin(uuid, drmSessionId, status);
1612     if (crypto == NULL) {
1613         ALOGE("prepareDrm: createCrypto failed. status: %d", status);
1614         return status;
1615     }
1616     ALOGV("prepareDrm: createCryptoAndPlugin succeeded for uuid: %s",
1617             DrmUUID::toHexString(uuid).string());
1618 
1619     *outCrypto = crypto;
1620     // as long a there is an active crypto
1621     mIsDrmProtected = true;
1622 
1623     if (mMimes.size() == 0) {
1624         status = UNKNOWN_ERROR;
1625         ALOGE("prepareDrm: Unexpected. Must have at least one track. status: %d", status);
1626         return status;
1627     }
1628 
1629     // first mime in this list is either the video track, or the first audio track
1630     const char *mime = mMimes[0].string();
1631     mIsSecure = crypto->requiresSecureDecoderComponent(mime);
1632     ALOGV("prepareDrm: requiresSecureDecoderComponent mime: %s  isSecure: %d",
1633             mime, mIsSecure);
1634 
1635     // Checking the member flags while in the looper to send out the notification.
1636     // The legacy mDecryptHandle!=NULL check (for FLAG_PROTECTED) is equivalent to mIsDrmProtected.
1637     notifyFlagsChanged(
1638             (mIsSecure ? FLAG_SECURE : 0) |
1639             // Setting "protected screen" only for L1: b/38390836
1640             (mIsSecure ? FLAG_PROTECTED : 0) |
1641             FLAG_CAN_PAUSE |
1642             FLAG_CAN_SEEK_BACKWARD |
1643             FLAG_CAN_SEEK_FORWARD |
1644             FLAG_CAN_SEEK);
1645 
1646     if (status == OK) {
1647         ALOGV("prepareDrm: mCrypto: %p (%d)", outCrypto->get(),
1648                 (*outCrypto != NULL ? (*outCrypto)->getStrongCount() : 0));
1649         ALOGD("prepareDrm ret: %d ", status);
1650     } else {
1651         ALOGE("prepareDrm err: %d", status);
1652     }
1653     return status;
1654 }
1655 
releaseDrm()1656 status_t NuPlayer::GenericSource::releaseDrm() {
1657     Mutex::Autolock _l(mLock);
1658     ALOGV("releaseDrm");
1659 
1660     if (mIsDrmProtected) {
1661         mIsDrmProtected = false;
1662         // to prevent returning any more buffer after stop/releaseDrm (b/37960096)
1663         mIsDrmReleased = true;
1664         ALOGV("releaseDrm: mIsDrmProtected is reset.");
1665     } else {
1666         ALOGE("releaseDrm: mIsDrmProtected is already false.");
1667     }
1668 
1669     return OK;
1670 }
1671 
checkDrmInfo()1672 status_t NuPlayer::GenericSource::checkDrmInfo()
1673 {
1674     // clearing the flag at prepare in case the player is reused after stop/releaseDrm with the
1675     // same source without being reset (called by prepareAsync/initFromDataSource)
1676     mIsDrmReleased = false;
1677 
1678     if (mFileMeta == NULL) {
1679         ALOGI("checkDrmInfo: No metadata");
1680         return OK; // letting the caller responds accordingly
1681     }
1682 
1683     uint32_t type;
1684     const void *pssh;
1685     size_t psshsize;
1686 
1687     if (!mFileMeta->findData(kKeyPssh, &type, &pssh, &psshsize)) {
1688         ALOGV("checkDrmInfo: No PSSH");
1689         return OK; // source without DRM info
1690     }
1691 
1692     Parcel parcel;
1693     NuPlayerDrm::retrieveDrmInfo(pssh, psshsize, &parcel);
1694     ALOGV("checkDrmInfo: MEDIA_DRM_INFO PSSH size: %d  Parcel size: %d  objects#: %d",
1695           (int)psshsize, (int)parcel.dataSize(), (int)parcel.objectsCount());
1696 
1697     if (parcel.dataSize() == 0) {
1698         ALOGE("checkDrmInfo: Unexpected parcel size: 0");
1699         return UNKNOWN_ERROR;
1700     }
1701 
1702     // Can't pass parcel as a message to the player. Converting Parcel->ABuffer to pass it
1703     // to the Player's onSourceNotify then back to Parcel for calling driver's notifyListener.
1704     sp<ABuffer> drmInfoBuffer = ABuffer::CreateAsCopy(parcel.data(), parcel.dataSize());
1705     notifyDrmInfo(drmInfoBuffer);
1706 
1707     return OK;
1708 }
1709 
signalBufferReturned(MediaBufferBase * buffer)1710 void NuPlayer::GenericSource::signalBufferReturned(MediaBufferBase *buffer)
1711 {
1712     //ALOGV("signalBufferReturned %p  refCount: %d", buffer, buffer->localRefcount());
1713 
1714     buffer->setObserver(NULL);
1715     buffer->release(); // this leads to delete since that there is no observor
1716 }
1717 
1718 }  // namespace android
1719