blob: b6f3a9fa7340ad99fb552808d8e50fb63e999a2b [file] [log] [blame]
Andreas Huberafed0e12011-09-20 15:39:58 -07001/*
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
Chong Zhang7e892182014-08-05 11:58:21 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "GenericSource"
19
Andreas Huberafed0e12011-09-20 15:39:58 -070020#include "GenericSource.h"
21
22#include "AnotherPacketSource.h"
23
Chong Zhanga19f33e2014-08-07 15:35:07 -070024#include <media/IMediaHTTPService.h>
Andreas Huberafed0e12011-09-20 15:39:58 -070025#include <media/stagefright/foundation/ABuffer.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AMessage.h>
28#include <media/stagefright/DataSource.h>
29#include <media/stagefright/FileSource.h>
30#include <media/stagefright/MediaBuffer.h>
31#include <media/stagefright/MediaDefs.h>
32#include <media/stagefright/MediaExtractor.h>
33#include <media/stagefright/MediaSource.h>
34#include <media/stagefright/MetaData.h>
Robert Shih17f6dd62014-08-20 17:00:21 -070035#include <media/stagefright/Utils.h>
Ronghua Wu80276872014-08-28 15:50:29 -070036#include "../../libstagefright/include/DRMExtractor.h"
Chong Zhangd354d8d2014-08-20 13:09:58 -070037#include "../../libstagefright/include/NuCachedSource2.h"
Lajos Molnarcc227032014-07-17 15:33:06 -070038#include "../../libstagefright/include/WVMExtractor.h"
Robert Shih360d6d02014-09-29 14:42:35 -070039#include "../../libstagefright/include/HTTPBase.h"
Andreas Huberafed0e12011-09-20 15:39:58 -070040
41namespace android {
42
43NuPlayer::GenericSource::GenericSource(
Andreas Huberb5f25f02013-02-05 10:14:26 -080044 const sp<AMessage> &notify,
Lajos Molnarcc227032014-07-17 15:33:06 -070045 bool uidValid,
46 uid_t uid)
Andreas Huberb5f25f02013-02-05 10:14:26 -080047 : Source(notify),
Robert Shih3423bbd2014-07-16 15:47:09 -070048 mFetchSubtitleDataGeneration(0),
Lajos Molnare26940f2014-07-31 10:31:26 -070049 mFetchTimedTextDataGeneration(0),
Andreas Huberb5f25f02013-02-05 10:14:26 -080050 mDurationUs(0ll),
Lajos Molnarcc227032014-07-17 15:33:06 -070051 mAudioIsVorbis(false),
Chong Zhang3de157d2014-08-05 20:54:44 -070052 mIsWidevine(false),
Lajos Molnarcc227032014-07-17 15:33:06 -070053 mUIDValid(uidValid),
Chong Zhangd354d8d2014-08-20 13:09:58 -070054 mUID(uid),
Ronghua Wu80276872014-08-28 15:50:29 -070055 mDrmManagerClient(NULL),
Chong Zhang2a3cc9a2014-08-21 17:48:26 -070056 mMetaDataSize(-1ll),
57 mBitrate(-1ll),
Lajos Molnar84f52782014-09-11 10:01:55 -070058 mPollBufferingGeneration(0),
59 mPendingReadBufferTypes(0) {
Chong Zhanga19f33e2014-08-07 15:35:07 -070060 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -070061 DataSource::RegisterDefaultSniffers();
Chong Zhang3de157d2014-08-05 20:54:44 -070062}
63
Chong Zhanga19f33e2014-08-07 15:35:07 -070064void NuPlayer::GenericSource::resetDataSource() {
Ronghua Wu80276872014-08-28 15:50:29 -070065 mAudioTimeUs = 0;
66 mVideoTimeUs = 0;
Chong Zhanga19f33e2014-08-07 15:35:07 -070067 mHTTPService.clear();
Robert Shih360d6d02014-09-29 14:42:35 -070068 mHttpSource.clear();
Chong Zhanga19f33e2014-08-07 15:35:07 -070069 mUri.clear();
70 mUriHeaders.clear();
71 mFd = -1;
72 mOffset = 0;
73 mLength = 0;
Ronghua Wu80276872014-08-28 15:50:29 -070074 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
75 mDecryptHandle = NULL;
76 mDrmManagerClient = NULL;
77 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -070078 mStopRead = true;
Chong Zhanga19f33e2014-08-07 15:35:07 -070079}
80
81status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -070082 const sp<IMediaHTTPService> &httpService,
83 const char *url,
84 const KeyedVector<String8, String8> *headers) {
Chong Zhanga19f33e2014-08-07 15:35:07 -070085 resetDataSource();
Chong Zhang3de157d2014-08-05 20:54:44 -070086
Chong Zhanga19f33e2014-08-07 15:35:07 -070087 mHTTPService = httpService;
88 mUri = url;
Andreas Huberafed0e12011-09-20 15:39:58 -070089
Chong Zhanga19f33e2014-08-07 15:35:07 -070090 if (headers) {
91 mUriHeaders = *headers;
Chong Zhang3de157d2014-08-05 20:54:44 -070092 }
93
Chong Zhanga19f33e2014-08-07 15:35:07 -070094 // delay data source creation to prepareAsync() to avoid blocking
95 // the calling thread in setDataSource for any significant time.
96 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -070097}
98
Chong Zhanga19f33e2014-08-07 15:35:07 -070099status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -0700100 int fd, int64_t offset, int64_t length) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700101 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700102
Chong Zhanga19f33e2014-08-07 15:35:07 -0700103 mFd = dup(fd);
104 mOffset = offset;
105 mLength = length;
106
107 // delay data source creation to prepareAsync() to avoid blocking
108 // the calling thread in setDataSource for any significant time.
109 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700110}
111
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700112sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const {
113 return mFileMeta;
114}
115
Chong Zhangd354d8d2014-08-20 13:09:58 -0700116status_t NuPlayer::GenericSource::initFromDataSource() {
Lajos Molnarcc227032014-07-17 15:33:06 -0700117 sp<MediaExtractor> extractor;
118
Chong Zhangd354d8d2014-08-20 13:09:58 -0700119 CHECK(mDataSource != NULL);
120
Lajos Molnarcc227032014-07-17 15:33:06 -0700121 if (mIsWidevine) {
122 String8 mimeType;
123 float confidence;
124 sp<AMessage> dummy;
125 bool success;
126
Chong Zhangd354d8d2014-08-20 13:09:58 -0700127 success = SniffWVM(mDataSource, &mimeType, &confidence, &dummy);
Lajos Molnarcc227032014-07-17 15:33:06 -0700128 if (!success
129 || strcasecmp(
130 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
131 ALOGE("unsupported widevine mime: %s", mimeType.string());
Chong Zhang3de157d2014-08-05 20:54:44 -0700132 return UNKNOWN_ERROR;
Lajos Molnarcc227032014-07-17 15:33:06 -0700133 }
134
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700135 mWVMExtractor = new WVMExtractor(mDataSource);
136 mWVMExtractor->setAdaptiveStreamingMode(true);
Lajos Molnarcc227032014-07-17 15:33:06 -0700137 if (mUIDValid) {
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700138 mWVMExtractor->setUID(mUID);
Lajos Molnarcc227032014-07-17 15:33:06 -0700139 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700140 extractor = mWVMExtractor;
Lajos Molnarcc227032014-07-17 15:33:06 -0700141 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700142 extractor = MediaExtractor::Create(mDataSource,
143 mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str());
Lajos Molnarcc227032014-07-17 15:33:06 -0700144 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700145
Chong Zhang3de157d2014-08-05 20:54:44 -0700146 if (extractor == NULL) {
147 return UNKNOWN_ERROR;
148 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700149
Ronghua Wu80276872014-08-28 15:50:29 -0700150 if (extractor->getDrmFlag()) {
151 checkDrmStatus(mDataSource);
152 }
153
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700154 mFileMeta = extractor->getMetaData();
155 if (mFileMeta != NULL) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700156 int64_t duration;
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700157 if (mFileMeta->findInt64(kKeyDuration, &duration)) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700158 mDurationUs = duration;
159 }
160 }
161
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700162 int32_t totalBitrate = 0;
163
Marco Nelissen705d3292014-09-19 15:14:37 -0700164 size_t numtracks = extractor->countTracks();
165 if (numtracks == 0) {
166 return UNKNOWN_ERROR;
167 }
168
169 for (size_t i = 0; i < numtracks; ++i) {
Chong Zhangafc0a872014-08-26 09:56:52 -0700170 sp<MediaSource> track = extractor->getTrack(i);
171
Andreas Huberafed0e12011-09-20 15:39:58 -0700172 sp<MetaData> meta = extractor->getTrackMetaData(i);
173
174 const char *mime;
175 CHECK(meta->findCString(kKeyMIMEType, &mime));
176
Chong Zhangafc0a872014-08-26 09:56:52 -0700177 // Do the string compare immediately with "mime",
178 // we can't assume "mime" would stay valid after another
179 // extractor operation, some extractors might modify meta
180 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700181 if (!strncasecmp(mime, "audio/", 6)) {
182 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700183 mAudioTrack.mIndex = i;
184 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700185 mAudioTrack.mPackets =
186 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700187
188 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
189 mAudioIsVorbis = true;
190 } else {
191 mAudioIsVorbis = false;
192 }
193 }
194 } else if (!strncasecmp(mime, "video/", 6)) {
195 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700196 mVideoTrack.mIndex = i;
197 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700198 mVideoTrack.mPackets =
199 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700200
201 // check if the source requires secure buffers
202 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700203 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
204 && secure) {
Chong Zhang7e892182014-08-05 11:58:21 -0700205 mIsWidevine = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700206 if (mUIDValid) {
207 extractor->setUID(mUID);
208 }
Chong Zhang7e892182014-08-05 11:58:21 -0700209 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700210 }
211 }
212
213 if (track != NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700214 mSources.push(track);
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 int64_t durationUs;
216 if (meta->findInt64(kKeyDuration, &durationUs)) {
217 if (durationUs > mDurationUs) {
218 mDurationUs = durationUs;
219 }
220 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700221
222 int32_t bitrate;
223 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
224 totalBitrate += bitrate;
225 } else {
226 totalBitrate = -1;
227 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700228 }
229 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700230
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700231 mBitrate = totalBitrate;
232
Chong Zhang3de157d2014-08-05 20:54:44 -0700233 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700234}
235
Ronghua Wu80276872014-08-28 15:50:29 -0700236void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
237 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
238 if (mDecryptHandle != NULL) {
239 CHECK(mDrmManagerClient);
240 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
241 sp<AMessage> msg = dupNotify();
242 msg->setInt32("what", kWhatDrmNoLicense);
243 msg->post();
244 }
245 }
246}
247
248int64_t NuPlayer::GenericSource::getLastReadPosition() {
249 if (mAudioTrack.mSource != NULL) {
250 return mAudioTimeUs;
251 } else if (mVideoTrack.mSource != NULL) {
252 return mVideoTimeUs;
253 } else {
254 return 0;
255 }
256}
257
Chong Zhanga19f33e2014-08-07 15:35:07 -0700258status_t NuPlayer::GenericSource::setBuffers(
259 bool audio, Vector<MediaBuffer *> &buffers) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700260 if (mIsWidevine && !audio) {
261 return mVideoTrack.mSource->setBuffers(buffers);
262 }
263 return INVALID_OPERATION;
264}
265
Andreas Huberafed0e12011-09-20 15:39:58 -0700266NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700267 if (mLooper != NULL) {
268 mLooper->unregisterHandler(id());
269 mLooper->stop();
270 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700271}
272
Andreas Huber9575c962013-02-05 13:59:56 -0800273void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700274 if (mLooper == NULL) {
275 mLooper = new ALooper;
276 mLooper->setName("generic");
277 mLooper->start();
278
279 mLooper->registerHandler(this);
280 }
281
282 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());
283 msg->post();
284}
285
286void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700287 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700288 if (mDataSource == NULL) {
289 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700290 const char* uri = mUri.c_str();
291 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
292
293 if (!strncasecmp("http://", uri, 7)
294 || !strncasecmp("https://", uri, 8)
295 || mIsWidevine) {
296 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
297 if (mHttpSource == NULL) {
298 ALOGE("Failed to create http source!");
299 notifyPreparedAndCleanup(UNKNOWN_ERROR);
300 return;
301 }
302 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700303
Chong Zhangd354d8d2014-08-20 13:09:58 -0700304 mDataSource = DataSource::CreateFromURI(
Robert Shih360d6d02014-09-29 14:42:35 -0700305 mHTTPService, uri, &mUriHeaders, &mContentType,
306 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700307 } else {
308 // set to false first, if the extractor
309 // comes back as secure, set it to true then.
310 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700311
Chong Zhangd354d8d2014-08-20 13:09:58 -0700312 mDataSource = new FileSource(mFd, mOffset, mLength);
313 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700314
Chong Zhangd354d8d2014-08-20 13:09:58 -0700315 if (mDataSource == NULL) {
316 ALOGE("Failed to create data source!");
317 notifyPreparedAndCleanup(UNKNOWN_ERROR);
318 return;
319 }
320
321 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
322 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
323 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700324
325 if (mIsWidevine || mCachedSource != NULL) {
326 schedulePollBuffering();
327 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700328 }
329
Chong Zhangd354d8d2014-08-20 13:09:58 -0700330 // check initial caching status
331 status_t err = prefillCacheIfNecessary();
332 if (err != OK) {
333 if (err == -EAGAIN) {
334 (new AMessage(kWhatPrepareAsync, id()))->post(200000);
335 } else {
336 ALOGE("Failed to prefill data cache!");
337 notifyPreparedAndCleanup(UNKNOWN_ERROR);
338 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700339 return;
340 }
341
Chong Zhangd354d8d2014-08-20 13:09:58 -0700342 // init extrator from data source
343 err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700344
345 if (err != OK) {
346 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700347 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700348 return;
349 }
350
Andreas Huber9575c962013-02-05 13:59:56 -0800351 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700352 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
353 sp<AMessage> msg = new AMessage;
354 err = convertMetaDataToMessage(meta, &msg);
355 if(err != OK) {
356 notifyPreparedAndCleanup(err);
357 return;
358 }
359 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800360 }
361
362 notifyFlagsChanged(
Lajos Molnarcc227032014-07-17 15:33:06 -0700363 (mIsWidevine ? FLAG_SECURE : 0)
364 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800365 | FLAG_CAN_SEEK_BACKWARD
366 | FLAG_CAN_SEEK_FORWARD
367 | FLAG_CAN_SEEK);
368
369 notifyPrepared();
370}
371
Chong Zhangd354d8d2014-08-20 13:09:58 -0700372void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
373 if (err != OK) {
374 mMetaDataSize = -1ll;
375 mContentType = "";
376 mSniffedMIME = "";
Robert Shihf3eb8262015-09-02 14:02:47 -0700377 {
378 sp<DataSource> dataSource = mDataSource;
379 sp<NuCachedSource2> cachedSource = mCachedSource;
380 sp<DataSource> httpSource = mHttpSource;
381 {
382 Mutex::Autolock _l(mDisconnectLock);
383 mDataSource.clear();
384 mCachedSource.clear();
385 mHttpSource.clear();
386 }
387 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700388
389 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700390 }
391 notifyPrepared(err);
392}
393
394status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
395 CHECK(mDataSource != NULL);
396
397 if (mCachedSource == NULL) {
398 // no prefill if the data source is not cached
399 return OK;
400 }
401
402 // We're not doing this for streams that appear to be audio-only
403 // streams to ensure that even low bandwidth streams start
404 // playing back fairly instantly.
405 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
406 return OK;
407 }
408
409 // We're going to prefill the cache before trying to instantiate
410 // the extractor below, as the latter is an operation that otherwise
411 // could block on the datasource for a significant amount of time.
412 // During that time we'd be unable to abort the preparation phase
413 // without this prefill.
414
415 // Initially make sure we have at least 192 KB for the sniff
416 // to complete without blocking.
417 static const size_t kMinBytesForSniffing = 192 * 1024;
418 static const size_t kDefaultMetaSize = 200000;
419
420 status_t finalStatus;
421
422 size_t cachedDataRemaining =
423 mCachedSource->approxDataRemaining(&finalStatus);
424
425 if (finalStatus != OK || (mMetaDataSize >= 0
426 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
427 ALOGV("stop caching, status %d, "
428 "metaDataSize %lld, cachedDataRemaining %zu",
429 finalStatus, mMetaDataSize, cachedDataRemaining);
430 return OK;
431 }
432
433 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
434
435 if (mMetaDataSize < 0
436 && cachedDataRemaining >= kMinBytesForSniffing) {
437 String8 tmp;
438 float confidence;
439 sp<AMessage> meta;
440 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
441 return UNKNOWN_ERROR;
442 }
443
444 // We successfully identified the file's extractor to
445 // be, remember this mime type so we don't have to
446 // sniff it again when we call MediaExtractor::Create()
447 mSniffedMIME = tmp.string();
448
449 if (meta == NULL
450 || !meta->findInt64("meta-data-size",
451 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
452 mMetaDataSize = kDefaultMetaSize;
453 }
454
455 if (mMetaDataSize < 0ll) {
456 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
457 return UNKNOWN_ERROR;
458 }
459 }
460
461 return -EAGAIN;
462}
463
Andreas Huberafed0e12011-09-20 15:39:58 -0700464void NuPlayer::GenericSource::start() {
465 ALOGI("start");
466
Andy Hung2abde2c2014-09-30 14:40:32 -0700467 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700468 if (mAudioTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700469 CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700470
Robert Shih17f6dd62014-08-20 17:00:21 -0700471 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700472 }
473
474 if (mVideoTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700475 CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700476
Robert Shih17f6dd62014-08-20 17:00:21 -0700477 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700478 }
Ronghua Wu80276872014-08-28 15:50:29 -0700479
480 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
481 mStarted = true;
482}
483
484void NuPlayer::GenericSource::stop() {
485 // nothing to do, just account for DRM playback status
486 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
487 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -0700488 if (mIsWidevine) {
489 // For a widevine source we need to prevent any further reads.
490 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
491 sp<AMessage> response;
492 (void) msg->postAndAwaitResponse(&response);
493 }
Ronghua Wu80276872014-08-28 15:50:29 -0700494}
495
496void NuPlayer::GenericSource::pause() {
497 // nothing to do, just account for DRM playback status
498 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
499 mStarted = false;
500}
501
502void NuPlayer::GenericSource::resume() {
503 // nothing to do, just account for DRM playback status
504 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
505 mStarted = true;
506}
507
Chong Zhang48296b72014-09-14 14:28:45 -0700508void NuPlayer::GenericSource::disconnect() {
Robert Shihf3eb8262015-09-02 14:02:47 -0700509 sp<DataSource> dataSource, httpSource;
510 {
511 Mutex::Autolock _l(mDisconnectLock);
512 dataSource = mDataSource;
513 httpSource = mHttpSource;
514 }
515
516 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700517 // disconnect data source
Robert Shihf3eb8262015-09-02 14:02:47 -0700518 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
519 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700520 }
Robert Shihf3eb8262015-09-02 14:02:47 -0700521 } else if (httpSource != NULL) {
522 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700523 }
524}
525
Ronghua Wu80276872014-08-28 15:50:29 -0700526void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
527 if (mDecryptHandle != NULL) {
528 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
529 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700530 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
531 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700532}
533
534status_t NuPlayer::GenericSource::feedMoreTSData() {
535 return OK;
536}
537
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700538void NuPlayer::GenericSource::schedulePollBuffering() {
539 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
540 msg->setInt32("generation", mPollBufferingGeneration);
541 msg->post(1000000ll);
542}
543
544void NuPlayer::GenericSource::cancelPollBuffering() {
545 ++mPollBufferingGeneration;
546}
547
548void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
549 sp<AMessage> msg = dupNotify();
550 msg->setInt32("what", kWhatBufferingUpdate);
551 msg->setInt32("percentage", percentage);
552 msg->post();
553}
554
555void NuPlayer::GenericSource::onPollBuffering() {
556 status_t finalStatus = UNKNOWN_ERROR;
557 int64_t cachedDurationUs = 0ll;
558
559 if (mCachedSource != NULL) {
560 size_t cachedDataRemaining =
561 mCachedSource->approxDataRemaining(&finalStatus);
562
563 if (finalStatus == OK) {
564 off64_t size;
565 int64_t bitrate = 0ll;
566 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
567 bitrate = size * 8000000ll / mDurationUs;
568 } else if (mBitrate > 0) {
569 bitrate = mBitrate;
570 }
571 if (bitrate > 0) {
572 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
573 }
574 }
575 } else if (mWVMExtractor != NULL) {
576 cachedDurationUs
577 = mWVMExtractor->getCachedDurationUs(&finalStatus);
578 }
579
580 if (finalStatus == ERROR_END_OF_STREAM) {
581 notifyBufferingUpdate(100);
582 cancelPollBuffering();
583 return;
584 } else if (cachedDurationUs > 0ll && mDurationUs > 0ll) {
585 int percentage = 100.0 * cachedDurationUs / mDurationUs;
586 if (percentage > 100) {
587 percentage = 100;
588 }
589
590 notifyBufferingUpdate(percentage);
591 }
592
593 schedulePollBuffering();
594}
595
596
Robert Shih3423bbd2014-07-16 15:47:09 -0700597void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
598 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700599 case kWhatPrepareAsync:
600 {
601 onPrepareAsync();
602 break;
603 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700604 case kWhatFetchSubtitleData:
605 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700606 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
607 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
608 break;
609 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700610
Lajos Molnare26940f2014-07-31 10:31:26 -0700611 case kWhatFetchTimedTextData:
612 {
613 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
614 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700615 break;
616 }
617
618 case kWhatSendSubtitleData:
619 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700620 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
621 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
622 break;
623 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700624
Lajos Molnare26940f2014-07-31 10:31:26 -0700625 case kWhatSendTimedTextData:
626 {
627 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
628 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700629 break;
630 }
631
632 case kWhatChangeAVSource:
633 {
634 int32_t trackIndex;
635 CHECK(msg->findInt32("trackIndex", &trackIndex));
636 const sp<MediaSource> source = mSources.itemAt(trackIndex);
637
638 Track* track;
639 const char *mime;
640 media_track_type trackType, counterpartType;
641 sp<MetaData> meta = source->getFormat();
642 meta->findCString(kKeyMIMEType, &mime);
643 if (!strncasecmp(mime, "audio/", 6)) {
644 track = &mAudioTrack;
645 trackType = MEDIA_TRACK_TYPE_AUDIO;
646 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
647 } else {
648 CHECK(!strncasecmp(mime, "video/", 6));
649 track = &mVideoTrack;
650 trackType = MEDIA_TRACK_TYPE_VIDEO;
651 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
652 }
653
654
655 if (track->mSource != NULL) {
656 track->mSource->stop();
657 }
658 track->mSource = source;
659 track->mSource->start();
660 track->mIndex = trackIndex;
661
662 status_t avail;
663 if (!track->mPackets->hasBufferAvailable(&avail)) {
664 // sync from other source
665 TRESPASS();
666 break;
667 }
668
669 int64_t timeUs, actualTimeUs;
670 const bool formatChange = true;
Robert Shih309aa8b2014-07-29 18:34:36 -0700671 sp<AMessage> latestMeta = track->mPackets->getLatestEnqueuedMeta();
Robert Shih3423bbd2014-07-16 15:47:09 -0700672 CHECK(latestMeta != NULL && latestMeta->findInt64("timeUs", &timeUs));
673 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
674 readBuffer(counterpartType, -1, NULL, formatChange);
675 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
676
677 break;
678 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700679 case kWhatPollBuffering:
680 {
681 int32_t generation;
682 CHECK(msg->findInt32("generation", &generation));
683 if (generation == mPollBufferingGeneration) {
684 onPollBuffering();
685 }
686 break;
687 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700688
689 case kWhatGetFormat:
690 {
691 onGetFormatMeta(msg);
692 break;
693 }
694
695 case kWhatGetSelectedTrack:
696 {
697 onGetSelectedTrack(msg);
698 break;
699 }
700
701 case kWhatSelectTrack:
702 {
703 onSelectTrack(msg);
704 break;
705 }
706
707 case kWhatSeek:
708 {
709 onSeek(msg);
710 break;
711 }
712
713 case kWhatReadBuffer:
714 {
715 onReadBuffer(msg);
716 break;
717 }
718
Andy Hung2abde2c2014-09-30 14:40:32 -0700719 case kWhatStopWidevine:
720 {
721 // mStopRead is only used for Widevine to prevent the video source
722 // from being read while the associated video decoder is shutting down.
723 mStopRead = true;
724 if (mVideoTrack.mSource != NULL) {
725 mVideoTrack.mPackets->clear();
726 }
727 sp<AMessage> response = new AMessage;
728 uint32_t replyID;
729 CHECK(msg->senderAwaitsResponse(&replyID));
730 response->postReply(replyID);
731 break;
732 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700733 default:
734 Source::onMessageReceived(msg);
735 break;
736 }
737}
738
Lajos Molnare26940f2014-07-31 10:31:26 -0700739void NuPlayer::GenericSource::fetchTextData(
740 uint32_t sendWhat,
741 media_track_type type,
742 int32_t curGen,
743 sp<AnotherPacketSource> packets,
744 sp<AMessage> msg) {
745 int32_t msgGeneration;
746 CHECK(msg->findInt32("generation", &msgGeneration));
747 if (msgGeneration != curGen) {
748 // stale
749 return;
750 }
751
752 int32_t avail;
753 if (packets->hasBufferAvailable(&avail)) {
754 return;
755 }
756
757 int64_t timeUs;
758 CHECK(msg->findInt64("timeUs", &timeUs));
759
760 int64_t subTimeUs;
761 readBuffer(type, timeUs, &subTimeUs);
762
763 int64_t delayUs = subTimeUs - timeUs;
764 if (msg->what() == kWhatFetchSubtitleData) {
765 const int64_t oneSecUs = 1000000ll;
766 delayUs -= oneSecUs;
767 }
768 sp<AMessage> msg2 = new AMessage(sendWhat, id());
769 msg2->setInt32("generation", msgGeneration);
770 msg2->post(delayUs < 0 ? 0 : delayUs);
771}
772
773void NuPlayer::GenericSource::sendTextData(
774 uint32_t what,
775 media_track_type type,
776 int32_t curGen,
777 sp<AnotherPacketSource> packets,
778 sp<AMessage> msg) {
779 int32_t msgGeneration;
780 CHECK(msg->findInt32("generation", &msgGeneration));
781 if (msgGeneration != curGen) {
782 // stale
783 return;
784 }
785
786 int64_t subTimeUs;
787 if (packets->nextBufferTime(&subTimeUs) != OK) {
788 return;
789 }
790
791 int64_t nextSubTimeUs;
792 readBuffer(type, -1, &nextSubTimeUs);
793
794 sp<ABuffer> buffer;
795 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
796 if (dequeueStatus == OK) {
797 sp<AMessage> notify = dupNotify();
798 notify->setInt32("what", what);
799 notify->setBuffer("buffer", buffer);
800 notify->post();
801
802 const int64_t delayUs = nextSubTimeUs - subTimeUs;
803 msg->post(delayUs < 0 ? 0 : delayUs);
804 }
805}
806
Andreas Huber84066782011-08-16 09:34:26 -0700807sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700808 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
809 msg->setInt32("audio", audio);
810
811 sp<AMessage> response;
812 void *format;
813 status_t err = msg->postAndAwaitResponse(&response);
814 if (err == OK && response != NULL) {
815 CHECK(response->findPointer("format", &format));
816 return (MetaData *)format;
817 } else {
818 return NULL;
819 }
820}
821
822void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
823 int32_t audio;
824 CHECK(msg->findInt32("audio", &audio));
825
826 sp<AMessage> response = new AMessage;
827 sp<MetaData> format = doGetFormatMeta(audio);
828 response->setPointer("format", format.get());
829
830 uint32_t replyID;
831 CHECK(msg->senderAwaitsResponse(&replyID));
832 response->postReply(replyID);
833}
834
835sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700836 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
837
838 if (source == NULL) {
839 return NULL;
840 }
841
842 return source->getFormat();
843}
844
845status_t NuPlayer::GenericSource::dequeueAccessUnit(
846 bool audio, sp<ABuffer> *accessUnit) {
847 Track *track = audio ? &mAudioTrack : &mVideoTrack;
848
849 if (track->mSource == NULL) {
850 return -EWOULDBLOCK;
851 }
852
Lajos Molnarcc227032014-07-17 15:33:06 -0700853 if (mIsWidevine && !audio) {
854 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700855 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -0700856 }
857
Andreas Huberafed0e12011-09-20 15:39:58 -0700858 status_t finalResult;
859 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700860 return (finalResult == OK ? -EWOULDBLOCK : finalResult);
Andreas Huberafed0e12011-09-20 15:39:58 -0700861 }
862
863 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
864
Robert Shih3423bbd2014-07-16 15:47:09 -0700865 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700866 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -0700867 }
868
Robert Shih3423bbd2014-07-16 15:47:09 -0700869 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -0700870 if (mSubtitleTrack.mSource != NULL) {
871 mSubtitleTrack.mPackets->clear();
872 mFetchSubtitleDataGeneration++;
873 }
874 if (mTimedTextTrack.mSource != NULL) {
875 mTimedTextTrack.mPackets->clear();
876 mFetchTimedTextDataGeneration++;
877 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700878 return result;
879 }
880
881 int64_t timeUs;
882 status_t eosResult; // ignored
883 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Lajos Molnare26940f2014-07-31 10:31:26 -0700884
885 if (mSubtitleTrack.mSource != NULL
886 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -0700887 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
888 msg->setInt64("timeUs", timeUs);
889 msg->setInt32("generation", mFetchSubtitleDataGeneration);
890 msg->post();
891 }
Robert Shiheb1735e2014-07-23 15:53:14 -0700892
Lajos Molnare26940f2014-07-31 10:31:26 -0700893 if (mTimedTextTrack.mSource != NULL
894 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
895 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
896 msg->setInt64("timeUs", timeUs);
897 msg->setInt32("generation", mFetchTimedTextDataGeneration);
898 msg->post();
899 }
900
Andreas Huberafed0e12011-09-20 15:39:58 -0700901 return result;
902}
903
904status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
905 *durationUs = mDurationUs;
906 return OK;
907}
908
Robert Shihdd235722014-06-12 14:49:23 -0700909size_t NuPlayer::GenericSource::getTrackCount() const {
910 return mSources.size();
911}
912
913sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
914 size_t trackCount = mSources.size();
915 if (trackIndex >= trackCount) {
916 return NULL;
917 }
918
919 sp<AMessage> format = new AMessage();
920 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
921
922 const char *mime;
923 CHECK(meta->findCString(kKeyMIMEType, &mime));
924
925 int32_t trackType;
926 if (!strncasecmp(mime, "video/", 6)) {
927 trackType = MEDIA_TRACK_TYPE_VIDEO;
928 } else if (!strncasecmp(mime, "audio/", 6)) {
929 trackType = MEDIA_TRACK_TYPE_AUDIO;
930 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
931 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
932 } else {
933 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
934 }
935 format->setInt32("type", trackType);
936
937 const char *lang;
938 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
939 lang = "und";
940 }
941 format->setString("language", lang);
942
943 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
944 format->setString("mime", mime);
945
946 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
947 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
948 meta->findInt32(kKeyTrackIsDefault, &isDefault);
949 meta->findInt32(kKeyTrackIsForced, &isForced);
950
951 format->setInt32("auto", !!isAutoselect);
952 format->setInt32("default", !!isDefault);
953 format->setInt32("forced", !!isForced);
954 }
955
956 return format;
957}
958
Lajos Molnare26940f2014-07-31 10:31:26 -0700959ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -0700960 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
961 msg->setInt32("type", type);
962
963 sp<AMessage> response;
964 int32_t index;
965 status_t err = msg->postAndAwaitResponse(&response);
966 if (err == OK && response != NULL) {
967 CHECK(response->findInt32("index", &index));
968 return index;
969 } else {
970 return -1;
971 }
972}
973
974void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
975 int32_t tmpType;
976 CHECK(msg->findInt32("type", &tmpType));
977 media_track_type type = (media_track_type)tmpType;
978
979 sp<AMessage> response = new AMessage;
980 ssize_t index = doGetSelectedTrack(type);
981 response->setInt32("index", index);
982
983 uint32_t replyID;
984 CHECK(msg->senderAwaitsResponse(&replyID));
985 response->postReply(replyID);
986}
987
988ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -0700989 const Track *track = NULL;
990 switch (type) {
991 case MEDIA_TRACK_TYPE_VIDEO:
992 track = &mVideoTrack;
993 break;
994 case MEDIA_TRACK_TYPE_AUDIO:
995 track = &mAudioTrack;
996 break;
997 case MEDIA_TRACK_TYPE_TIMEDTEXT:
998 track = &mTimedTextTrack;
999 break;
1000 case MEDIA_TRACK_TYPE_SUBTITLE:
1001 track = &mSubtitleTrack;
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 if (track != NULL && track->mSource != NULL) {
1008 return track->mIndex;
1009 }
1010
1011 return -1;
1012}
1013
Robert Shih3423bbd2014-07-16 15:47:09 -07001014status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001015 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001016 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1017 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001018 msg->setInt32("select", select);
Robert Shih17f6dd62014-08-20 17:00:21 -07001019
1020 sp<AMessage> response;
1021 status_t err = msg->postAndAwaitResponse(&response);
1022 if (err == OK && response != NULL) {
1023 CHECK(response->findInt32("err", &err));
1024 }
1025
1026 return err;
1027}
1028
1029void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1030 int32_t trackIndex, select;
1031 CHECK(msg->findInt32("trackIndex", &trackIndex));
1032 CHECK(msg->findInt32("select", &select));
1033
1034 sp<AMessage> response = new AMessage;
1035 status_t err = doSelectTrack(trackIndex, select);
1036 response->setInt32("err", err);
1037
1038 uint32_t replyID;
1039 CHECK(msg->senderAwaitsResponse(&replyID));
1040 response->postReply(replyID);
1041}
1042
1043status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001044 if (trackIndex >= mSources.size()) {
1045 return BAD_INDEX;
1046 }
1047
1048 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001049 Track* track = NULL;
1050 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1051 track = &mSubtitleTrack;
1052 mFetchSubtitleDataGeneration++;
1053 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1054 track = &mTimedTextTrack;
1055 mFetchTimedTextDataGeneration++;
1056 }
1057 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001058 return INVALID_OPERATION;
1059 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001060 track->mSource->stop();
1061 track->mSource = NULL;
1062 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001063 return OK;
1064 }
1065
1066 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1067 sp<MetaData> meta = source->getFormat();
1068 const char *mime;
1069 CHECK(meta->findCString(kKeyMIMEType, &mime));
1070 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001071 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1072 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1073 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001074 return OK;
1075 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001076 track->mIndex = trackIndex;
1077 if (track->mSource != NULL) {
1078 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001079 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001080 track->mSource = mSources.itemAt(trackIndex);
1081 track->mSource->start();
1082 if (track->mPackets == NULL) {
1083 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001084 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001085 track->mPackets->clear();
1086 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001087
1088 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001089
1090 if (isSubtitle) {
1091 mFetchSubtitleDataGeneration++;
1092 } else {
1093 mFetchTimedTextDataGeneration++;
1094 }
1095
Robert Shih3423bbd2014-07-16 15:47:09 -07001096 return OK;
1097 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1098 bool audio = !strncasecmp(mime, "audio/", 6);
1099 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1100 if (track->mSource != NULL && track->mIndex == trackIndex) {
1101 return OK;
1102 }
1103
1104 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1105 msg->setInt32("trackIndex", trackIndex);
1106 msg->post();
1107 return OK;
1108 }
1109
1110 return INVALID_OPERATION;
1111}
1112
Andreas Huberafed0e12011-09-20 15:39:58 -07001113status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001114 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1115 msg->setInt64("seekTimeUs", seekTimeUs);
1116
1117 sp<AMessage> response;
1118 status_t err = msg->postAndAwaitResponse(&response);
1119 if (err == OK && response != NULL) {
1120 CHECK(response->findInt32("err", &err));
1121 }
1122
1123 return err;
1124}
1125
1126void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1127 int64_t seekTimeUs;
1128 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1129
1130 sp<AMessage> response = new AMessage;
1131 status_t err = doSeek(seekTimeUs);
1132 response->setInt32("err", err);
1133
1134 uint32_t replyID;
1135 CHECK(msg->senderAwaitsResponse(&replyID));
1136 response->postReply(replyID);
1137}
1138
1139status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001140 // If the Widevine source is stopped, do not attempt to read any
1141 // more buffers.
1142 if (mStopRead) {
1143 return INVALID_OPERATION;
1144 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001145 if (mVideoTrack.mSource != NULL) {
1146 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001147 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001148
1149 seekTimeUs = actualTimeUs;
1150 }
1151
1152 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001153 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001154 }
1155
Ronghua Wu80276872014-08-28 15:50:29 -07001156 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1157 if (!mStarted) {
1158 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1159 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001160 return OK;
1161}
1162
Robert Shih3423bbd2014-07-16 15:47:09 -07001163sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1164 MediaBuffer* mb,
1165 media_track_type trackType,
1166 int64_t *actualTimeUs) {
1167 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1168 size_t outLength = mb->range_length();
1169
1170 if (audio && mAudioIsVorbis) {
1171 outLength += sizeof(int32_t);
1172 }
1173
1174 sp<ABuffer> ab;
1175 if (mIsWidevine && !audio) {
1176 // data is already provided in the buffer
1177 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001178 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001179 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001180 } else {
1181 ab = new ABuffer(outLength);
1182 memcpy(ab->data(),
1183 (const uint8_t *)mb->data() + mb->range_offset(),
1184 mb->range_length());
1185 }
1186
1187 if (audio && mAudioIsVorbis) {
1188 int32_t numPageSamples;
1189 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1190 numPageSamples = -1;
1191 }
1192
1193 uint8_t* abEnd = ab->data() + mb->range_length();
1194 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1195 }
1196
Lajos Molnare26940f2014-07-31 10:31:26 -07001197 sp<AMessage> meta = ab->meta();
1198
Robert Shih3423bbd2014-07-16 15:47:09 -07001199 int64_t timeUs;
1200 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001201 meta->setInt64("timeUs", timeUs);
1202
Lajos Molnare26940f2014-07-31 10:31:26 -07001203 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1204 const char *mime;
1205 CHECK(mTimedTextTrack.mSource != NULL
1206 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1207 meta->setString("mime", mime);
1208 }
1209
Robert Shih3423bbd2014-07-16 15:47:09 -07001210 int64_t durationUs;
1211 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1212 meta->setInt64("durationUs", durationUs);
1213 }
1214
1215 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1216 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1217 }
1218
1219 if (actualTimeUs) {
1220 *actualTimeUs = timeUs;
1221 }
1222
1223 mb->release();
1224 mb = NULL;
1225
1226 return ab;
1227}
1228
Robert Shih17f6dd62014-08-20 17:00:21 -07001229void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001230 Mutex::Autolock _l(mReadBufferLock);
1231
1232 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1233 mPendingReadBufferTypes |= (1 << trackType);
1234 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1235 msg->setInt32("trackType", trackType);
1236 msg->post();
1237 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001238}
1239
1240void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1241 int32_t tmpType;
1242 CHECK(msg->findInt32("trackType", &tmpType));
1243 media_track_type trackType = (media_track_type)tmpType;
Lajos Molnar84f52782014-09-11 10:01:55 -07001244 {
1245 // only protect the variable change, as readBuffer may
1246 // take considerable time. This may result in one extra
1247 // read being processed, but that is benign.
1248 Mutex::Autolock _l(mReadBufferLock);
1249 mPendingReadBufferTypes &= ~(1 << trackType);
1250 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001251 readBuffer(trackType);
1252}
1253
Andreas Huberafed0e12011-09-20 15:39:58 -07001254void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001255 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001256 // Do not read data if Widevine source is stopped
1257 if (mStopRead) {
1258 return;
1259 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001260 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001261 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001262 switch (trackType) {
1263 case MEDIA_TRACK_TYPE_VIDEO:
1264 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001265 if (mIsWidevine) {
1266 maxBuffers = 2;
1267 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001268 break;
1269 case MEDIA_TRACK_TYPE_AUDIO:
1270 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001271 if (mIsWidevine) {
1272 maxBuffers = 8;
1273 } else {
1274 maxBuffers = 64;
1275 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001276 break;
1277 case MEDIA_TRACK_TYPE_SUBTITLE:
1278 track = &mSubtitleTrack;
1279 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001280 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1281 track = &mTimedTextTrack;
1282 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001283 default:
1284 TRESPASS();
1285 }
1286
1287 if (track->mSource == NULL) {
1288 return;
1289 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001290
1291 if (actualTimeUs) {
1292 *actualTimeUs = seekTimeUs;
1293 }
1294
1295 MediaSource::ReadOptions options;
1296
1297 bool seeking = false;
1298
1299 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001300 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001301 seeking = true;
1302 }
1303
Robert Shih3423bbd2014-07-16 15:47:09 -07001304 if (mIsWidevine && trackType != MEDIA_TRACK_TYPE_AUDIO) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001305 options.setNonBlocking();
1306 }
1307
Phil Burkc5cc2e22014-09-09 20:08:39 -07001308 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001309 MediaBuffer *mbuf;
1310 status_t err = track->mSource->read(&mbuf, &options);
1311
1312 options.clearSeekTo();
1313
1314 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001315 int64_t timeUs;
1316 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1317 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1318 mAudioTimeUs = timeUs;
1319 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1320 mVideoTimeUs = timeUs;
1321 }
1322
Robert Shih3423bbd2014-07-16 15:47:09 -07001323 // formatChange && seeking: track whose source is changed during selection
1324 // formatChange && !seeking: track whose source is not changed during selection
1325 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001326 if ((seeking || formatChange)
1327 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1328 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001329 ATSParser::DiscontinuityType type = formatChange
1330 ? (seeking
1331 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1332 : ATSParser::DISCONTINUITY_NONE)
1333 : ATSParser::DISCONTINUITY_SEEK;
1334 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001335 }
1336
Robert Shih3423bbd2014-07-16 15:47:09 -07001337 sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001338 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001339 formatChange = false;
1340 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001341 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001342 } else if (err == WOULD_BLOCK) {
1343 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001344 } else if (err == INFO_FORMAT_CHANGED) {
1345#if 0
1346 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001347 ATSParser::DISCONTINUITY_FORMATCHANGE,
1348 NULL,
1349 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001350#endif
1351 } else {
1352 track->mPackets->signalEOS(err);
1353 break;
1354 }
1355 }
1356}
1357
Andreas Huberafed0e12011-09-20 15:39:58 -07001358} // namespace android