blob: b3efd74c1f0e0001239f72b590b724e63924ee84 [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();
Wei Jia09c291c2015-10-22 11:35:04 -0700384 mDecryptHandle = NULL;
Wei Jia224858e2015-10-19 16:14:14 -0700385 mDrmManagerClient = NULL;
Robert Shihf3eb8262015-09-02 14:02:47 -0700386 mCachedSource.clear();
387 mHttpSource.clear();
388 }
389 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700390
391 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700392 }
393 notifyPrepared(err);
394}
395
396status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
397 CHECK(mDataSource != NULL);
398
399 if (mCachedSource == NULL) {
400 // no prefill if the data source is not cached
401 return OK;
402 }
403
404 // We're not doing this for streams that appear to be audio-only
405 // streams to ensure that even low bandwidth streams start
406 // playing back fairly instantly.
407 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
408 return OK;
409 }
410
411 // We're going to prefill the cache before trying to instantiate
412 // the extractor below, as the latter is an operation that otherwise
413 // could block on the datasource for a significant amount of time.
414 // During that time we'd be unable to abort the preparation phase
415 // without this prefill.
416
417 // Initially make sure we have at least 192 KB for the sniff
418 // to complete without blocking.
419 static const size_t kMinBytesForSniffing = 192 * 1024;
420 static const size_t kDefaultMetaSize = 200000;
421
422 status_t finalStatus;
423
424 size_t cachedDataRemaining =
425 mCachedSource->approxDataRemaining(&finalStatus);
426
427 if (finalStatus != OK || (mMetaDataSize >= 0
428 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
429 ALOGV("stop caching, status %d, "
430 "metaDataSize %lld, cachedDataRemaining %zu",
431 finalStatus, mMetaDataSize, cachedDataRemaining);
432 return OK;
433 }
434
435 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
436
437 if (mMetaDataSize < 0
438 && cachedDataRemaining >= kMinBytesForSniffing) {
439 String8 tmp;
440 float confidence;
441 sp<AMessage> meta;
442 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
443 return UNKNOWN_ERROR;
444 }
445
446 // We successfully identified the file's extractor to
447 // be, remember this mime type so we don't have to
448 // sniff it again when we call MediaExtractor::Create()
449 mSniffedMIME = tmp.string();
450
451 if (meta == NULL
452 || !meta->findInt64("meta-data-size",
453 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
454 mMetaDataSize = kDefaultMetaSize;
455 }
456
457 if (mMetaDataSize < 0ll) {
458 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
459 return UNKNOWN_ERROR;
460 }
461 }
462
463 return -EAGAIN;
464}
465
Andreas Huberafed0e12011-09-20 15:39:58 -0700466void NuPlayer::GenericSource::start() {
467 ALOGI("start");
468
Andy Hung2abde2c2014-09-30 14:40:32 -0700469 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700470 if (mAudioTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700471 CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700472
Robert Shih17f6dd62014-08-20 17:00:21 -0700473 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700474 }
475
476 if (mVideoTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700477 CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700478
Robert Shih17f6dd62014-08-20 17:00:21 -0700479 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700480 }
Ronghua Wu80276872014-08-28 15:50:29 -0700481
482 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
483 mStarted = true;
484}
485
486void NuPlayer::GenericSource::stop() {
487 // nothing to do, just account for DRM playback status
488 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
489 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -0700490 if (mIsWidevine) {
491 // For a widevine source we need to prevent any further reads.
492 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
493 sp<AMessage> response;
494 (void) msg->postAndAwaitResponse(&response);
495 }
Ronghua Wu80276872014-08-28 15:50:29 -0700496}
497
498void NuPlayer::GenericSource::pause() {
499 // nothing to do, just account for DRM playback status
500 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
501 mStarted = false;
502}
503
504void NuPlayer::GenericSource::resume() {
505 // nothing to do, just account for DRM playback status
506 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
507 mStarted = true;
508}
509
Chong Zhang48296b72014-09-14 14:28:45 -0700510void NuPlayer::GenericSource::disconnect() {
Robert Shihf3eb8262015-09-02 14:02:47 -0700511 sp<DataSource> dataSource, httpSource;
512 {
513 Mutex::Autolock _l(mDisconnectLock);
514 dataSource = mDataSource;
515 httpSource = mHttpSource;
516 }
517
518 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700519 // disconnect data source
Robert Shihf3eb8262015-09-02 14:02:47 -0700520 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
521 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700522 }
Robert Shihf3eb8262015-09-02 14:02:47 -0700523 } else if (httpSource != NULL) {
524 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700525 }
526}
527
Ronghua Wu80276872014-08-28 15:50:29 -0700528void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
529 if (mDecryptHandle != NULL) {
530 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
531 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700532 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
533 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700534}
535
536status_t NuPlayer::GenericSource::feedMoreTSData() {
537 return OK;
538}
539
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700540void NuPlayer::GenericSource::schedulePollBuffering() {
541 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
542 msg->setInt32("generation", mPollBufferingGeneration);
543 msg->post(1000000ll);
544}
545
546void NuPlayer::GenericSource::cancelPollBuffering() {
547 ++mPollBufferingGeneration;
548}
549
550void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
551 sp<AMessage> msg = dupNotify();
552 msg->setInt32("what", kWhatBufferingUpdate);
553 msg->setInt32("percentage", percentage);
554 msg->post();
555}
556
557void NuPlayer::GenericSource::onPollBuffering() {
558 status_t finalStatus = UNKNOWN_ERROR;
559 int64_t cachedDurationUs = 0ll;
560
561 if (mCachedSource != NULL) {
562 size_t cachedDataRemaining =
563 mCachedSource->approxDataRemaining(&finalStatus);
564
565 if (finalStatus == OK) {
566 off64_t size;
567 int64_t bitrate = 0ll;
568 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
569 bitrate = size * 8000000ll / mDurationUs;
570 } else if (mBitrate > 0) {
571 bitrate = mBitrate;
572 }
573 if (bitrate > 0) {
574 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
575 }
576 }
577 } else if (mWVMExtractor != NULL) {
578 cachedDurationUs
579 = mWVMExtractor->getCachedDurationUs(&finalStatus);
580 }
581
582 if (finalStatus == ERROR_END_OF_STREAM) {
583 notifyBufferingUpdate(100);
584 cancelPollBuffering();
585 return;
586 } else if (cachedDurationUs > 0ll && mDurationUs > 0ll) {
587 int percentage = 100.0 * cachedDurationUs / mDurationUs;
588 if (percentage > 100) {
589 percentage = 100;
590 }
591
592 notifyBufferingUpdate(percentage);
593 }
594
595 schedulePollBuffering();
596}
597
598
Robert Shih3423bbd2014-07-16 15:47:09 -0700599void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
600 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700601 case kWhatPrepareAsync:
602 {
603 onPrepareAsync();
604 break;
605 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700606 case kWhatFetchSubtitleData:
607 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700608 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
609 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
610 break;
611 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700612
Lajos Molnare26940f2014-07-31 10:31:26 -0700613 case kWhatFetchTimedTextData:
614 {
615 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
616 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700617 break;
618 }
619
620 case kWhatSendSubtitleData:
621 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700622 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
623 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
624 break;
625 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700626
Lajos Molnare26940f2014-07-31 10:31:26 -0700627 case kWhatSendTimedTextData:
628 {
629 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
630 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700631 break;
632 }
633
634 case kWhatChangeAVSource:
635 {
636 int32_t trackIndex;
637 CHECK(msg->findInt32("trackIndex", &trackIndex));
638 const sp<MediaSource> source = mSources.itemAt(trackIndex);
639
640 Track* track;
641 const char *mime;
642 media_track_type trackType, counterpartType;
643 sp<MetaData> meta = source->getFormat();
644 meta->findCString(kKeyMIMEType, &mime);
645 if (!strncasecmp(mime, "audio/", 6)) {
646 track = &mAudioTrack;
647 trackType = MEDIA_TRACK_TYPE_AUDIO;
648 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
649 } else {
650 CHECK(!strncasecmp(mime, "video/", 6));
651 track = &mVideoTrack;
652 trackType = MEDIA_TRACK_TYPE_VIDEO;
653 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
654 }
655
656
657 if (track->mSource != NULL) {
658 track->mSource->stop();
659 }
660 track->mSource = source;
661 track->mSource->start();
662 track->mIndex = trackIndex;
663
664 status_t avail;
665 if (!track->mPackets->hasBufferAvailable(&avail)) {
666 // sync from other source
667 TRESPASS();
668 break;
669 }
670
671 int64_t timeUs, actualTimeUs;
672 const bool formatChange = true;
Robert Shih309aa8b2014-07-29 18:34:36 -0700673 sp<AMessage> latestMeta = track->mPackets->getLatestEnqueuedMeta();
Robert Shih3423bbd2014-07-16 15:47:09 -0700674 CHECK(latestMeta != NULL && latestMeta->findInt64("timeUs", &timeUs));
675 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
676 readBuffer(counterpartType, -1, NULL, formatChange);
677 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
678
679 break;
680 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700681 case kWhatPollBuffering:
682 {
683 int32_t generation;
684 CHECK(msg->findInt32("generation", &generation));
685 if (generation == mPollBufferingGeneration) {
686 onPollBuffering();
687 }
688 break;
689 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700690
691 case kWhatGetFormat:
692 {
693 onGetFormatMeta(msg);
694 break;
695 }
696
697 case kWhatGetSelectedTrack:
698 {
699 onGetSelectedTrack(msg);
700 break;
701 }
702
703 case kWhatSelectTrack:
704 {
705 onSelectTrack(msg);
706 break;
707 }
708
709 case kWhatSeek:
710 {
711 onSeek(msg);
712 break;
713 }
714
715 case kWhatReadBuffer:
716 {
717 onReadBuffer(msg);
718 break;
719 }
720
Andy Hung2abde2c2014-09-30 14:40:32 -0700721 case kWhatStopWidevine:
722 {
723 // mStopRead is only used for Widevine to prevent the video source
724 // from being read while the associated video decoder is shutting down.
725 mStopRead = true;
726 if (mVideoTrack.mSource != NULL) {
727 mVideoTrack.mPackets->clear();
728 }
729 sp<AMessage> response = new AMessage;
730 uint32_t replyID;
731 CHECK(msg->senderAwaitsResponse(&replyID));
732 response->postReply(replyID);
733 break;
734 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700735 default:
736 Source::onMessageReceived(msg);
737 break;
738 }
739}
740
Lajos Molnare26940f2014-07-31 10:31:26 -0700741void NuPlayer::GenericSource::fetchTextData(
742 uint32_t sendWhat,
743 media_track_type type,
744 int32_t curGen,
745 sp<AnotherPacketSource> packets,
746 sp<AMessage> msg) {
747 int32_t msgGeneration;
748 CHECK(msg->findInt32("generation", &msgGeneration));
749 if (msgGeneration != curGen) {
750 // stale
751 return;
752 }
753
754 int32_t avail;
755 if (packets->hasBufferAvailable(&avail)) {
756 return;
757 }
758
759 int64_t timeUs;
760 CHECK(msg->findInt64("timeUs", &timeUs));
761
762 int64_t subTimeUs;
763 readBuffer(type, timeUs, &subTimeUs);
764
765 int64_t delayUs = subTimeUs - timeUs;
766 if (msg->what() == kWhatFetchSubtitleData) {
767 const int64_t oneSecUs = 1000000ll;
768 delayUs -= oneSecUs;
769 }
770 sp<AMessage> msg2 = new AMessage(sendWhat, id());
771 msg2->setInt32("generation", msgGeneration);
772 msg2->post(delayUs < 0 ? 0 : delayUs);
773}
774
775void NuPlayer::GenericSource::sendTextData(
776 uint32_t what,
777 media_track_type type,
778 int32_t curGen,
779 sp<AnotherPacketSource> packets,
780 sp<AMessage> msg) {
781 int32_t msgGeneration;
782 CHECK(msg->findInt32("generation", &msgGeneration));
783 if (msgGeneration != curGen) {
784 // stale
785 return;
786 }
787
788 int64_t subTimeUs;
789 if (packets->nextBufferTime(&subTimeUs) != OK) {
790 return;
791 }
792
793 int64_t nextSubTimeUs;
794 readBuffer(type, -1, &nextSubTimeUs);
795
796 sp<ABuffer> buffer;
797 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
798 if (dequeueStatus == OK) {
799 sp<AMessage> notify = dupNotify();
800 notify->setInt32("what", what);
801 notify->setBuffer("buffer", buffer);
802 notify->post();
803
804 const int64_t delayUs = nextSubTimeUs - subTimeUs;
805 msg->post(delayUs < 0 ? 0 : delayUs);
806 }
807}
808
Andreas Huber84066782011-08-16 09:34:26 -0700809sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700810 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
811 msg->setInt32("audio", audio);
812
813 sp<AMessage> response;
814 void *format;
815 status_t err = msg->postAndAwaitResponse(&response);
816 if (err == OK && response != NULL) {
817 CHECK(response->findPointer("format", &format));
818 return (MetaData *)format;
819 } else {
820 return NULL;
821 }
822}
823
824void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
825 int32_t audio;
826 CHECK(msg->findInt32("audio", &audio));
827
828 sp<AMessage> response = new AMessage;
829 sp<MetaData> format = doGetFormatMeta(audio);
830 response->setPointer("format", format.get());
831
832 uint32_t replyID;
833 CHECK(msg->senderAwaitsResponse(&replyID));
834 response->postReply(replyID);
835}
836
837sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700838 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
839
840 if (source == NULL) {
841 return NULL;
842 }
843
844 return source->getFormat();
845}
846
847status_t NuPlayer::GenericSource::dequeueAccessUnit(
848 bool audio, sp<ABuffer> *accessUnit) {
849 Track *track = audio ? &mAudioTrack : &mVideoTrack;
850
851 if (track->mSource == NULL) {
852 return -EWOULDBLOCK;
853 }
854
Lajos Molnarcc227032014-07-17 15:33:06 -0700855 if (mIsWidevine && !audio) {
856 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700857 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -0700858 }
859
Andreas Huberafed0e12011-09-20 15:39:58 -0700860 status_t finalResult;
861 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700862 return (finalResult == OK ? -EWOULDBLOCK : finalResult);
Andreas Huberafed0e12011-09-20 15:39:58 -0700863 }
864
865 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
866
Robert Shih3423bbd2014-07-16 15:47:09 -0700867 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700868 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -0700869 }
870
Robert Shih3423bbd2014-07-16 15:47:09 -0700871 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -0700872 if (mSubtitleTrack.mSource != NULL) {
873 mSubtitleTrack.mPackets->clear();
874 mFetchSubtitleDataGeneration++;
875 }
876 if (mTimedTextTrack.mSource != NULL) {
877 mTimedTextTrack.mPackets->clear();
878 mFetchTimedTextDataGeneration++;
879 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700880 return result;
881 }
882
883 int64_t timeUs;
884 status_t eosResult; // ignored
885 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Lajos Molnare26940f2014-07-31 10:31:26 -0700886
887 if (mSubtitleTrack.mSource != NULL
888 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -0700889 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
890 msg->setInt64("timeUs", timeUs);
891 msg->setInt32("generation", mFetchSubtitleDataGeneration);
892 msg->post();
893 }
Robert Shiheb1735e2014-07-23 15:53:14 -0700894
Lajos Molnare26940f2014-07-31 10:31:26 -0700895 if (mTimedTextTrack.mSource != NULL
896 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
897 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
898 msg->setInt64("timeUs", timeUs);
899 msg->setInt32("generation", mFetchTimedTextDataGeneration);
900 msg->post();
901 }
902
Andreas Huberafed0e12011-09-20 15:39:58 -0700903 return result;
904}
905
906status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
907 *durationUs = mDurationUs;
908 return OK;
909}
910
Robert Shihdd235722014-06-12 14:49:23 -0700911size_t NuPlayer::GenericSource::getTrackCount() const {
912 return mSources.size();
913}
914
915sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
916 size_t trackCount = mSources.size();
917 if (trackIndex >= trackCount) {
918 return NULL;
919 }
920
921 sp<AMessage> format = new AMessage();
922 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
923
924 const char *mime;
925 CHECK(meta->findCString(kKeyMIMEType, &mime));
926
927 int32_t trackType;
928 if (!strncasecmp(mime, "video/", 6)) {
929 trackType = MEDIA_TRACK_TYPE_VIDEO;
930 } else if (!strncasecmp(mime, "audio/", 6)) {
931 trackType = MEDIA_TRACK_TYPE_AUDIO;
932 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
933 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
934 } else {
935 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
936 }
937 format->setInt32("type", trackType);
938
939 const char *lang;
940 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
941 lang = "und";
942 }
943 format->setString("language", lang);
944
945 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
946 format->setString("mime", mime);
947
948 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
949 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
950 meta->findInt32(kKeyTrackIsDefault, &isDefault);
951 meta->findInt32(kKeyTrackIsForced, &isForced);
952
953 format->setInt32("auto", !!isAutoselect);
954 format->setInt32("default", !!isDefault);
955 format->setInt32("forced", !!isForced);
956 }
957
958 return format;
959}
960
Lajos Molnare26940f2014-07-31 10:31:26 -0700961ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -0700962 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
963 msg->setInt32("type", type);
964
965 sp<AMessage> response;
966 int32_t index;
967 status_t err = msg->postAndAwaitResponse(&response);
968 if (err == OK && response != NULL) {
969 CHECK(response->findInt32("index", &index));
970 return index;
971 } else {
972 return -1;
973 }
974}
975
976void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
977 int32_t tmpType;
978 CHECK(msg->findInt32("type", &tmpType));
979 media_track_type type = (media_track_type)tmpType;
980
981 sp<AMessage> response = new AMessage;
982 ssize_t index = doGetSelectedTrack(type);
983 response->setInt32("index", index);
984
985 uint32_t replyID;
986 CHECK(msg->senderAwaitsResponse(&replyID));
987 response->postReply(replyID);
988}
989
990ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -0700991 const Track *track = NULL;
992 switch (type) {
993 case MEDIA_TRACK_TYPE_VIDEO:
994 track = &mVideoTrack;
995 break;
996 case MEDIA_TRACK_TYPE_AUDIO:
997 track = &mAudioTrack;
998 break;
999 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1000 track = &mTimedTextTrack;
1001 break;
1002 case MEDIA_TRACK_TYPE_SUBTITLE:
1003 track = &mSubtitleTrack;
1004 break;
1005 default:
1006 break;
1007 }
1008
1009 if (track != NULL && track->mSource != NULL) {
1010 return track->mIndex;
1011 }
1012
1013 return -1;
1014}
1015
Robert Shih3423bbd2014-07-16 15:47:09 -07001016status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001017 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001018 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1019 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001020 msg->setInt32("select", select);
Robert Shih17f6dd62014-08-20 17:00:21 -07001021
1022 sp<AMessage> response;
1023 status_t err = msg->postAndAwaitResponse(&response);
1024 if (err == OK && response != NULL) {
1025 CHECK(response->findInt32("err", &err));
1026 }
1027
1028 return err;
1029}
1030
1031void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1032 int32_t trackIndex, select;
1033 CHECK(msg->findInt32("trackIndex", &trackIndex));
1034 CHECK(msg->findInt32("select", &select));
1035
1036 sp<AMessage> response = new AMessage;
1037 status_t err = doSelectTrack(trackIndex, select);
1038 response->setInt32("err", err);
1039
1040 uint32_t replyID;
1041 CHECK(msg->senderAwaitsResponse(&replyID));
1042 response->postReply(replyID);
1043}
1044
1045status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001046 if (trackIndex >= mSources.size()) {
1047 return BAD_INDEX;
1048 }
1049
1050 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001051 Track* track = NULL;
1052 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1053 track = &mSubtitleTrack;
1054 mFetchSubtitleDataGeneration++;
1055 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1056 track = &mTimedTextTrack;
1057 mFetchTimedTextDataGeneration++;
1058 }
1059 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001060 return INVALID_OPERATION;
1061 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001062 track->mSource->stop();
1063 track->mSource = NULL;
1064 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001065 return OK;
1066 }
1067
1068 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1069 sp<MetaData> meta = source->getFormat();
1070 const char *mime;
1071 CHECK(meta->findCString(kKeyMIMEType, &mime));
1072 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001073 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1074 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1075 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001076 return OK;
1077 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001078 track->mIndex = trackIndex;
1079 if (track->mSource != NULL) {
1080 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001081 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001082 track->mSource = mSources.itemAt(trackIndex);
1083 track->mSource->start();
1084 if (track->mPackets == NULL) {
1085 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001086 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001087 track->mPackets->clear();
1088 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001089
1090 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001091
1092 if (isSubtitle) {
1093 mFetchSubtitleDataGeneration++;
1094 } else {
1095 mFetchTimedTextDataGeneration++;
1096 }
1097
Robert Shih3423bbd2014-07-16 15:47:09 -07001098 return OK;
1099 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1100 bool audio = !strncasecmp(mime, "audio/", 6);
1101 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1102 if (track->mSource != NULL && track->mIndex == trackIndex) {
1103 return OK;
1104 }
1105
1106 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1107 msg->setInt32("trackIndex", trackIndex);
1108 msg->post();
1109 return OK;
1110 }
1111
1112 return INVALID_OPERATION;
1113}
1114
Andreas Huberafed0e12011-09-20 15:39:58 -07001115status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001116 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1117 msg->setInt64("seekTimeUs", seekTimeUs);
1118
1119 sp<AMessage> response;
1120 status_t err = msg->postAndAwaitResponse(&response);
1121 if (err == OK && response != NULL) {
1122 CHECK(response->findInt32("err", &err));
1123 }
1124
1125 return err;
1126}
1127
1128void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1129 int64_t seekTimeUs;
1130 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1131
1132 sp<AMessage> response = new AMessage;
1133 status_t err = doSeek(seekTimeUs);
1134 response->setInt32("err", err);
1135
1136 uint32_t replyID;
1137 CHECK(msg->senderAwaitsResponse(&replyID));
1138 response->postReply(replyID);
1139}
1140
1141status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001142 // If the Widevine source is stopped, do not attempt to read any
1143 // more buffers.
1144 if (mStopRead) {
1145 return INVALID_OPERATION;
1146 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001147 if (mVideoTrack.mSource != NULL) {
1148 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001149 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001150
1151 seekTimeUs = actualTimeUs;
1152 }
1153
1154 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001155 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001156 }
1157
Ronghua Wu80276872014-08-28 15:50:29 -07001158 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1159 if (!mStarted) {
1160 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1161 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001162 return OK;
1163}
1164
Robert Shih3423bbd2014-07-16 15:47:09 -07001165sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1166 MediaBuffer* mb,
1167 media_track_type trackType,
1168 int64_t *actualTimeUs) {
1169 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1170 size_t outLength = mb->range_length();
1171
1172 if (audio && mAudioIsVorbis) {
1173 outLength += sizeof(int32_t);
1174 }
1175
1176 sp<ABuffer> ab;
1177 if (mIsWidevine && !audio) {
1178 // data is already provided in the buffer
1179 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001180 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001181 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001182 } else {
1183 ab = new ABuffer(outLength);
1184 memcpy(ab->data(),
1185 (const uint8_t *)mb->data() + mb->range_offset(),
1186 mb->range_length());
1187 }
1188
1189 if (audio && mAudioIsVorbis) {
1190 int32_t numPageSamples;
1191 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1192 numPageSamples = -1;
1193 }
1194
1195 uint8_t* abEnd = ab->data() + mb->range_length();
1196 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1197 }
1198
Lajos Molnare26940f2014-07-31 10:31:26 -07001199 sp<AMessage> meta = ab->meta();
1200
Robert Shih3423bbd2014-07-16 15:47:09 -07001201 int64_t timeUs;
1202 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001203 meta->setInt64("timeUs", timeUs);
1204
Lajos Molnare26940f2014-07-31 10:31:26 -07001205 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1206 const char *mime;
1207 CHECK(mTimedTextTrack.mSource != NULL
1208 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1209 meta->setString("mime", mime);
1210 }
1211
Robert Shih3423bbd2014-07-16 15:47:09 -07001212 int64_t durationUs;
1213 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1214 meta->setInt64("durationUs", durationUs);
1215 }
1216
1217 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1218 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1219 }
1220
1221 if (actualTimeUs) {
1222 *actualTimeUs = timeUs;
1223 }
1224
1225 mb->release();
1226 mb = NULL;
1227
1228 return ab;
1229}
1230
Robert Shih17f6dd62014-08-20 17:00:21 -07001231void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001232 Mutex::Autolock _l(mReadBufferLock);
1233
1234 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1235 mPendingReadBufferTypes |= (1 << trackType);
1236 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1237 msg->setInt32("trackType", trackType);
1238 msg->post();
1239 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001240}
1241
1242void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1243 int32_t tmpType;
1244 CHECK(msg->findInt32("trackType", &tmpType));
1245 media_track_type trackType = (media_track_type)tmpType;
Lajos Molnar84f52782014-09-11 10:01:55 -07001246 {
1247 // only protect the variable change, as readBuffer may
1248 // take considerable time. This may result in one extra
1249 // read being processed, but that is benign.
1250 Mutex::Autolock _l(mReadBufferLock);
1251 mPendingReadBufferTypes &= ~(1 << trackType);
1252 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001253 readBuffer(trackType);
1254}
1255
Andreas Huberafed0e12011-09-20 15:39:58 -07001256void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001257 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001258 // Do not read data if Widevine source is stopped
1259 if (mStopRead) {
1260 return;
1261 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001262 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001263 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001264 switch (trackType) {
1265 case MEDIA_TRACK_TYPE_VIDEO:
1266 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001267 if (mIsWidevine) {
1268 maxBuffers = 2;
1269 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001270 break;
1271 case MEDIA_TRACK_TYPE_AUDIO:
1272 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001273 if (mIsWidevine) {
1274 maxBuffers = 8;
1275 } else {
1276 maxBuffers = 64;
1277 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001278 break;
1279 case MEDIA_TRACK_TYPE_SUBTITLE:
1280 track = &mSubtitleTrack;
1281 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001282 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1283 track = &mTimedTextTrack;
1284 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001285 default:
1286 TRESPASS();
1287 }
1288
1289 if (track->mSource == NULL) {
1290 return;
1291 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001292
1293 if (actualTimeUs) {
1294 *actualTimeUs = seekTimeUs;
1295 }
1296
1297 MediaSource::ReadOptions options;
1298
1299 bool seeking = false;
1300
1301 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001302 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001303 seeking = true;
1304 }
1305
Robert Shih3423bbd2014-07-16 15:47:09 -07001306 if (mIsWidevine && trackType != MEDIA_TRACK_TYPE_AUDIO) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001307 options.setNonBlocking();
1308 }
1309
Phil Burkc5cc2e22014-09-09 20:08:39 -07001310 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001311 MediaBuffer *mbuf;
1312 status_t err = track->mSource->read(&mbuf, &options);
1313
1314 options.clearSeekTo();
1315
1316 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001317 int64_t timeUs;
1318 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1319 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1320 mAudioTimeUs = timeUs;
1321 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1322 mVideoTimeUs = timeUs;
1323 }
1324
Robert Shih3423bbd2014-07-16 15:47:09 -07001325 // formatChange && seeking: track whose source is changed during selection
1326 // formatChange && !seeking: track whose source is not changed during selection
1327 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001328 if ((seeking || formatChange)
1329 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1330 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001331 ATSParser::DiscontinuityType type = formatChange
1332 ? (seeking
1333 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1334 : ATSParser::DISCONTINUITY_NONE)
1335 : ATSParser::DISCONTINUITY_SEEK;
1336 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001337 }
1338
Robert Shih3423bbd2014-07-16 15:47:09 -07001339 sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001340 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001341 formatChange = false;
1342 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001343 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001344 } else if (err == WOULD_BLOCK) {
1345 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001346 } else if (err == INFO_FORMAT_CHANGED) {
1347#if 0
1348 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001349 ATSParser::DISCONTINUITY_FORMATCHANGE,
1350 NULL,
1351 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001352#endif
1353 } else {
1354 track->mPackets->signalEOS(err);
1355 break;
1356 }
1357 }
1358}
1359
Andreas Huberafed0e12011-09-20 15:39:58 -07001360} // namespace android