blob: 0e520a8cca276e0275742cb0e8b11a044de474d3 [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 = "";
377 mDataSource.clear();
378 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700379 mHttpSource.clear();
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700380
381 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700382 }
383 notifyPrepared(err);
384}
385
386status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
387 CHECK(mDataSource != NULL);
388
389 if (mCachedSource == NULL) {
390 // no prefill if the data source is not cached
391 return OK;
392 }
393
394 // We're not doing this for streams that appear to be audio-only
395 // streams to ensure that even low bandwidth streams start
396 // playing back fairly instantly.
397 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
398 return OK;
399 }
400
401 // We're going to prefill the cache before trying to instantiate
402 // the extractor below, as the latter is an operation that otherwise
403 // could block on the datasource for a significant amount of time.
404 // During that time we'd be unable to abort the preparation phase
405 // without this prefill.
406
407 // Initially make sure we have at least 192 KB for the sniff
408 // to complete without blocking.
409 static const size_t kMinBytesForSniffing = 192 * 1024;
410 static const size_t kDefaultMetaSize = 200000;
411
412 status_t finalStatus;
413
414 size_t cachedDataRemaining =
415 mCachedSource->approxDataRemaining(&finalStatus);
416
417 if (finalStatus != OK || (mMetaDataSize >= 0
418 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
419 ALOGV("stop caching, status %d, "
420 "metaDataSize %lld, cachedDataRemaining %zu",
421 finalStatus, mMetaDataSize, cachedDataRemaining);
422 return OK;
423 }
424
425 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
426
427 if (mMetaDataSize < 0
428 && cachedDataRemaining >= kMinBytesForSniffing) {
429 String8 tmp;
430 float confidence;
431 sp<AMessage> meta;
432 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
433 return UNKNOWN_ERROR;
434 }
435
436 // We successfully identified the file's extractor to
437 // be, remember this mime type so we don't have to
438 // sniff it again when we call MediaExtractor::Create()
439 mSniffedMIME = tmp.string();
440
441 if (meta == NULL
442 || !meta->findInt64("meta-data-size",
443 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
444 mMetaDataSize = kDefaultMetaSize;
445 }
446
447 if (mMetaDataSize < 0ll) {
448 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
449 return UNKNOWN_ERROR;
450 }
451 }
452
453 return -EAGAIN;
454}
455
Andreas Huberafed0e12011-09-20 15:39:58 -0700456void NuPlayer::GenericSource::start() {
457 ALOGI("start");
458
Andy Hung2abde2c2014-09-30 14:40:32 -0700459 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700460 if (mAudioTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700461 CHECK_EQ(mAudioTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700462
Robert Shih17f6dd62014-08-20 17:00:21 -0700463 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700464 }
465
466 if (mVideoTrack.mSource != NULL) {
Robert Shihbace25b2014-07-25 14:14:34 -0700467 CHECK_EQ(mVideoTrack.mSource->start(), (status_t)OK);
Andreas Huberafed0e12011-09-20 15:39:58 -0700468
Robert Shih17f6dd62014-08-20 17:00:21 -0700469 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700470 }
Ronghua Wu80276872014-08-28 15:50:29 -0700471
472 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
473 mStarted = true;
474}
475
476void NuPlayer::GenericSource::stop() {
477 // nothing to do, just account for DRM playback status
478 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
479 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -0700480 if (mIsWidevine) {
481 // For a widevine source we need to prevent any further reads.
482 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
483 sp<AMessage> response;
484 (void) msg->postAndAwaitResponse(&response);
485 }
Ronghua Wu80276872014-08-28 15:50:29 -0700486}
487
488void NuPlayer::GenericSource::pause() {
489 // nothing to do, just account for DRM playback status
490 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
491 mStarted = false;
492}
493
494void NuPlayer::GenericSource::resume() {
495 // nothing to do, just account for DRM playback status
496 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
497 mStarted = true;
498}
499
Chong Zhang48296b72014-09-14 14:28:45 -0700500void NuPlayer::GenericSource::disconnect() {
501 if (mDataSource != NULL) {
502 // disconnect data source
503 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
504 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
505 }
Robert Shih360d6d02014-09-29 14:42:35 -0700506 } else if (mHttpSource != NULL) {
507 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700508 }
509}
510
Ronghua Wu80276872014-08-28 15:50:29 -0700511void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
512 if (mDecryptHandle != NULL) {
513 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
514 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700515 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
516 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700517}
518
519status_t NuPlayer::GenericSource::feedMoreTSData() {
520 return OK;
521}
522
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700523void NuPlayer::GenericSource::schedulePollBuffering() {
524 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
525 msg->setInt32("generation", mPollBufferingGeneration);
526 msg->post(1000000ll);
527}
528
529void NuPlayer::GenericSource::cancelPollBuffering() {
530 ++mPollBufferingGeneration;
531}
532
533void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
534 sp<AMessage> msg = dupNotify();
535 msg->setInt32("what", kWhatBufferingUpdate);
536 msg->setInt32("percentage", percentage);
537 msg->post();
538}
539
540void NuPlayer::GenericSource::onPollBuffering() {
541 status_t finalStatus = UNKNOWN_ERROR;
542 int64_t cachedDurationUs = 0ll;
543
544 if (mCachedSource != NULL) {
545 size_t cachedDataRemaining =
546 mCachedSource->approxDataRemaining(&finalStatus);
547
548 if (finalStatus == OK) {
549 off64_t size;
550 int64_t bitrate = 0ll;
551 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
552 bitrate = size * 8000000ll / mDurationUs;
553 } else if (mBitrate > 0) {
554 bitrate = mBitrate;
555 }
556 if (bitrate > 0) {
557 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
558 }
559 }
560 } else if (mWVMExtractor != NULL) {
561 cachedDurationUs
562 = mWVMExtractor->getCachedDurationUs(&finalStatus);
563 }
564
565 if (finalStatus == ERROR_END_OF_STREAM) {
566 notifyBufferingUpdate(100);
567 cancelPollBuffering();
568 return;
569 } else if (cachedDurationUs > 0ll && mDurationUs > 0ll) {
570 int percentage = 100.0 * cachedDurationUs / mDurationUs;
571 if (percentage > 100) {
572 percentage = 100;
573 }
574
575 notifyBufferingUpdate(percentage);
576 }
577
578 schedulePollBuffering();
579}
580
581
Robert Shih3423bbd2014-07-16 15:47:09 -0700582void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
583 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700584 case kWhatPrepareAsync:
585 {
586 onPrepareAsync();
587 break;
588 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700589 case kWhatFetchSubtitleData:
590 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700591 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
592 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
593 break;
594 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700595
Lajos Molnare26940f2014-07-31 10:31:26 -0700596 case kWhatFetchTimedTextData:
597 {
598 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
599 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700600 break;
601 }
602
603 case kWhatSendSubtitleData:
604 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700605 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
606 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
607 break;
608 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700609
Lajos Molnare26940f2014-07-31 10:31:26 -0700610 case kWhatSendTimedTextData:
611 {
612 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
613 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700614 break;
615 }
616
617 case kWhatChangeAVSource:
618 {
619 int32_t trackIndex;
620 CHECK(msg->findInt32("trackIndex", &trackIndex));
621 const sp<MediaSource> source = mSources.itemAt(trackIndex);
622
623 Track* track;
624 const char *mime;
625 media_track_type trackType, counterpartType;
626 sp<MetaData> meta = source->getFormat();
627 meta->findCString(kKeyMIMEType, &mime);
628 if (!strncasecmp(mime, "audio/", 6)) {
629 track = &mAudioTrack;
630 trackType = MEDIA_TRACK_TYPE_AUDIO;
631 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
632 } else {
633 CHECK(!strncasecmp(mime, "video/", 6));
634 track = &mVideoTrack;
635 trackType = MEDIA_TRACK_TYPE_VIDEO;
636 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
637 }
638
639
640 if (track->mSource != NULL) {
641 track->mSource->stop();
642 }
643 track->mSource = source;
644 track->mSource->start();
645 track->mIndex = trackIndex;
646
647 status_t avail;
648 if (!track->mPackets->hasBufferAvailable(&avail)) {
649 // sync from other source
650 TRESPASS();
651 break;
652 }
653
654 int64_t timeUs, actualTimeUs;
655 const bool formatChange = true;
Robert Shih309aa8b2014-07-29 18:34:36 -0700656 sp<AMessage> latestMeta = track->mPackets->getLatestEnqueuedMeta();
Robert Shih3423bbd2014-07-16 15:47:09 -0700657 CHECK(latestMeta != NULL && latestMeta->findInt64("timeUs", &timeUs));
658 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
659 readBuffer(counterpartType, -1, NULL, formatChange);
660 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
661
662 break;
663 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700664 case kWhatPollBuffering:
665 {
666 int32_t generation;
667 CHECK(msg->findInt32("generation", &generation));
668 if (generation == mPollBufferingGeneration) {
669 onPollBuffering();
670 }
671 break;
672 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700673
674 case kWhatGetFormat:
675 {
676 onGetFormatMeta(msg);
677 break;
678 }
679
680 case kWhatGetSelectedTrack:
681 {
682 onGetSelectedTrack(msg);
683 break;
684 }
685
686 case kWhatSelectTrack:
687 {
688 onSelectTrack(msg);
689 break;
690 }
691
692 case kWhatSeek:
693 {
694 onSeek(msg);
695 break;
696 }
697
698 case kWhatReadBuffer:
699 {
700 onReadBuffer(msg);
701 break;
702 }
703
Andy Hung2abde2c2014-09-30 14:40:32 -0700704 case kWhatStopWidevine:
705 {
706 // mStopRead is only used for Widevine to prevent the video source
707 // from being read while the associated video decoder is shutting down.
708 mStopRead = true;
709 if (mVideoTrack.mSource != NULL) {
710 mVideoTrack.mPackets->clear();
711 }
712 sp<AMessage> response = new AMessage;
713 uint32_t replyID;
714 CHECK(msg->senderAwaitsResponse(&replyID));
715 response->postReply(replyID);
716 break;
717 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700718 default:
719 Source::onMessageReceived(msg);
720 break;
721 }
722}
723
Lajos Molnare26940f2014-07-31 10:31:26 -0700724void NuPlayer::GenericSource::fetchTextData(
725 uint32_t sendWhat,
726 media_track_type type,
727 int32_t curGen,
728 sp<AnotherPacketSource> packets,
729 sp<AMessage> msg) {
730 int32_t msgGeneration;
731 CHECK(msg->findInt32("generation", &msgGeneration));
732 if (msgGeneration != curGen) {
733 // stale
734 return;
735 }
736
737 int32_t avail;
738 if (packets->hasBufferAvailable(&avail)) {
739 return;
740 }
741
742 int64_t timeUs;
743 CHECK(msg->findInt64("timeUs", &timeUs));
744
745 int64_t subTimeUs;
746 readBuffer(type, timeUs, &subTimeUs);
747
748 int64_t delayUs = subTimeUs - timeUs;
749 if (msg->what() == kWhatFetchSubtitleData) {
750 const int64_t oneSecUs = 1000000ll;
751 delayUs -= oneSecUs;
752 }
753 sp<AMessage> msg2 = new AMessage(sendWhat, id());
754 msg2->setInt32("generation", msgGeneration);
755 msg2->post(delayUs < 0 ? 0 : delayUs);
756}
757
758void NuPlayer::GenericSource::sendTextData(
759 uint32_t what,
760 media_track_type type,
761 int32_t curGen,
762 sp<AnotherPacketSource> packets,
763 sp<AMessage> msg) {
764 int32_t msgGeneration;
765 CHECK(msg->findInt32("generation", &msgGeneration));
766 if (msgGeneration != curGen) {
767 // stale
768 return;
769 }
770
771 int64_t subTimeUs;
772 if (packets->nextBufferTime(&subTimeUs) != OK) {
773 return;
774 }
775
776 int64_t nextSubTimeUs;
777 readBuffer(type, -1, &nextSubTimeUs);
778
779 sp<ABuffer> buffer;
780 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
781 if (dequeueStatus == OK) {
782 sp<AMessage> notify = dupNotify();
783 notify->setInt32("what", what);
784 notify->setBuffer("buffer", buffer);
785 notify->post();
786
787 const int64_t delayUs = nextSubTimeUs - subTimeUs;
788 msg->post(delayUs < 0 ? 0 : delayUs);
789 }
790}
791
Andreas Huber84066782011-08-16 09:34:26 -0700792sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700793 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
794 msg->setInt32("audio", audio);
795
796 sp<AMessage> response;
797 void *format;
798 status_t err = msg->postAndAwaitResponse(&response);
799 if (err == OK && response != NULL) {
800 CHECK(response->findPointer("format", &format));
801 return (MetaData *)format;
802 } else {
803 return NULL;
804 }
805}
806
807void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
808 int32_t audio;
809 CHECK(msg->findInt32("audio", &audio));
810
811 sp<AMessage> response = new AMessage;
812 sp<MetaData> format = doGetFormatMeta(audio);
813 response->setPointer("format", format.get());
814
815 uint32_t replyID;
816 CHECK(msg->senderAwaitsResponse(&replyID));
817 response->postReply(replyID);
818}
819
820sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700821 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
822
823 if (source == NULL) {
824 return NULL;
825 }
826
827 return source->getFormat();
828}
829
830status_t NuPlayer::GenericSource::dequeueAccessUnit(
831 bool audio, sp<ABuffer> *accessUnit) {
832 Track *track = audio ? &mAudioTrack : &mVideoTrack;
833
834 if (track->mSource == NULL) {
835 return -EWOULDBLOCK;
836 }
837
Lajos Molnarcc227032014-07-17 15:33:06 -0700838 if (mIsWidevine && !audio) {
839 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700840 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -0700841 }
842
Andreas Huberafed0e12011-09-20 15:39:58 -0700843 status_t finalResult;
844 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700845 return (finalResult == OK ? -EWOULDBLOCK : finalResult);
Andreas Huberafed0e12011-09-20 15:39:58 -0700846 }
847
848 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
849
Robert Shih3423bbd2014-07-16 15:47:09 -0700850 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700851 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -0700852 }
853
Robert Shih3423bbd2014-07-16 15:47:09 -0700854 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -0700855 if (mSubtitleTrack.mSource != NULL) {
856 mSubtitleTrack.mPackets->clear();
857 mFetchSubtitleDataGeneration++;
858 }
859 if (mTimedTextTrack.mSource != NULL) {
860 mTimedTextTrack.mPackets->clear();
861 mFetchTimedTextDataGeneration++;
862 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700863 return result;
864 }
865
866 int64_t timeUs;
867 status_t eosResult; // ignored
868 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Lajos Molnare26940f2014-07-31 10:31:26 -0700869
870 if (mSubtitleTrack.mSource != NULL
871 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -0700872 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
873 msg->setInt64("timeUs", timeUs);
874 msg->setInt32("generation", mFetchSubtitleDataGeneration);
875 msg->post();
876 }
Robert Shiheb1735e2014-07-23 15:53:14 -0700877
Lajos Molnare26940f2014-07-31 10:31:26 -0700878 if (mTimedTextTrack.mSource != NULL
879 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
880 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
881 msg->setInt64("timeUs", timeUs);
882 msg->setInt32("generation", mFetchTimedTextDataGeneration);
883 msg->post();
884 }
885
Andreas Huberafed0e12011-09-20 15:39:58 -0700886 return result;
887}
888
889status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
890 *durationUs = mDurationUs;
891 return OK;
892}
893
Robert Shihdd235722014-06-12 14:49:23 -0700894size_t NuPlayer::GenericSource::getTrackCount() const {
895 return mSources.size();
896}
897
898sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
899 size_t trackCount = mSources.size();
900 if (trackIndex >= trackCount) {
901 return NULL;
902 }
903
904 sp<AMessage> format = new AMessage();
905 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
906
907 const char *mime;
908 CHECK(meta->findCString(kKeyMIMEType, &mime));
909
910 int32_t trackType;
911 if (!strncasecmp(mime, "video/", 6)) {
912 trackType = MEDIA_TRACK_TYPE_VIDEO;
913 } else if (!strncasecmp(mime, "audio/", 6)) {
914 trackType = MEDIA_TRACK_TYPE_AUDIO;
915 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
916 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
917 } else {
918 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
919 }
920 format->setInt32("type", trackType);
921
922 const char *lang;
923 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
924 lang = "und";
925 }
926 format->setString("language", lang);
927
928 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
929 format->setString("mime", mime);
930
931 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
932 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
933 meta->findInt32(kKeyTrackIsDefault, &isDefault);
934 meta->findInt32(kKeyTrackIsForced, &isForced);
935
936 format->setInt32("auto", !!isAutoselect);
937 format->setInt32("default", !!isDefault);
938 format->setInt32("forced", !!isForced);
939 }
940
941 return format;
942}
943
Lajos Molnare26940f2014-07-31 10:31:26 -0700944ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -0700945 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
946 msg->setInt32("type", type);
947
948 sp<AMessage> response;
949 int32_t index;
950 status_t err = msg->postAndAwaitResponse(&response);
951 if (err == OK && response != NULL) {
952 CHECK(response->findInt32("index", &index));
953 return index;
954 } else {
955 return -1;
956 }
957}
958
959void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
960 int32_t tmpType;
961 CHECK(msg->findInt32("type", &tmpType));
962 media_track_type type = (media_track_type)tmpType;
963
964 sp<AMessage> response = new AMessage;
965 ssize_t index = doGetSelectedTrack(type);
966 response->setInt32("index", index);
967
968 uint32_t replyID;
969 CHECK(msg->senderAwaitsResponse(&replyID));
970 response->postReply(replyID);
971}
972
973ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -0700974 const Track *track = NULL;
975 switch (type) {
976 case MEDIA_TRACK_TYPE_VIDEO:
977 track = &mVideoTrack;
978 break;
979 case MEDIA_TRACK_TYPE_AUDIO:
980 track = &mAudioTrack;
981 break;
982 case MEDIA_TRACK_TYPE_TIMEDTEXT:
983 track = &mTimedTextTrack;
984 break;
985 case MEDIA_TRACK_TYPE_SUBTITLE:
986 track = &mSubtitleTrack;
987 break;
988 default:
989 break;
990 }
991
992 if (track != NULL && track->mSource != NULL) {
993 return track->mIndex;
994 }
995
996 return -1;
997}
998
Robert Shih3423bbd2014-07-16 15:47:09 -0700999status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001000 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001001 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1002 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001003 msg->setInt32("select", select);
Robert Shih17f6dd62014-08-20 17:00:21 -07001004
1005 sp<AMessage> response;
1006 status_t err = msg->postAndAwaitResponse(&response);
1007 if (err == OK && response != NULL) {
1008 CHECK(response->findInt32("err", &err));
1009 }
1010
1011 return err;
1012}
1013
1014void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1015 int32_t trackIndex, select;
1016 CHECK(msg->findInt32("trackIndex", &trackIndex));
1017 CHECK(msg->findInt32("select", &select));
1018
1019 sp<AMessage> response = new AMessage;
1020 status_t err = doSelectTrack(trackIndex, select);
1021 response->setInt32("err", err);
1022
1023 uint32_t replyID;
1024 CHECK(msg->senderAwaitsResponse(&replyID));
1025 response->postReply(replyID);
1026}
1027
1028status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001029 if (trackIndex >= mSources.size()) {
1030 return BAD_INDEX;
1031 }
1032
1033 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001034 Track* track = NULL;
1035 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1036 track = &mSubtitleTrack;
1037 mFetchSubtitleDataGeneration++;
1038 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1039 track = &mTimedTextTrack;
1040 mFetchTimedTextDataGeneration++;
1041 }
1042 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001043 return INVALID_OPERATION;
1044 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001045 track->mSource->stop();
1046 track->mSource = NULL;
1047 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001048 return OK;
1049 }
1050
1051 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1052 sp<MetaData> meta = source->getFormat();
1053 const char *mime;
1054 CHECK(meta->findCString(kKeyMIMEType, &mime));
1055 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001056 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1057 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1058 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001059 return OK;
1060 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001061 track->mIndex = trackIndex;
1062 if (track->mSource != NULL) {
1063 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001064 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001065 track->mSource = mSources.itemAt(trackIndex);
1066 track->mSource->start();
1067 if (track->mPackets == NULL) {
1068 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001069 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001070 track->mPackets->clear();
1071 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001072
1073 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001074
1075 if (isSubtitle) {
1076 mFetchSubtitleDataGeneration++;
1077 } else {
1078 mFetchTimedTextDataGeneration++;
1079 }
1080
Robert Shih3423bbd2014-07-16 15:47:09 -07001081 return OK;
1082 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1083 bool audio = !strncasecmp(mime, "audio/", 6);
1084 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1085 if (track->mSource != NULL && track->mIndex == trackIndex) {
1086 return OK;
1087 }
1088
1089 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1090 msg->setInt32("trackIndex", trackIndex);
1091 msg->post();
1092 return OK;
1093 }
1094
1095 return INVALID_OPERATION;
1096}
1097
Andreas Huberafed0e12011-09-20 15:39:58 -07001098status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001099 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1100 msg->setInt64("seekTimeUs", seekTimeUs);
1101
1102 sp<AMessage> response;
1103 status_t err = msg->postAndAwaitResponse(&response);
1104 if (err == OK && response != NULL) {
1105 CHECK(response->findInt32("err", &err));
1106 }
1107
1108 return err;
1109}
1110
1111void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1112 int64_t seekTimeUs;
1113 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1114
1115 sp<AMessage> response = new AMessage;
1116 status_t err = doSeek(seekTimeUs);
1117 response->setInt32("err", err);
1118
1119 uint32_t replyID;
1120 CHECK(msg->senderAwaitsResponse(&replyID));
1121 response->postReply(replyID);
1122}
1123
1124status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001125 // If the Widevine source is stopped, do not attempt to read any
1126 // more buffers.
1127 if (mStopRead) {
1128 return INVALID_OPERATION;
1129 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001130 if (mVideoTrack.mSource != NULL) {
1131 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001132 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001133
1134 seekTimeUs = actualTimeUs;
1135 }
1136
1137 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001138 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001139 }
1140
Ronghua Wu80276872014-08-28 15:50:29 -07001141 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1142 if (!mStarted) {
1143 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1144 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001145 return OK;
1146}
1147
Robert Shih3423bbd2014-07-16 15:47:09 -07001148sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1149 MediaBuffer* mb,
1150 media_track_type trackType,
1151 int64_t *actualTimeUs) {
1152 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1153 size_t outLength = mb->range_length();
1154
1155 if (audio && mAudioIsVorbis) {
1156 outLength += sizeof(int32_t);
1157 }
1158
1159 sp<ABuffer> ab;
1160 if (mIsWidevine && !audio) {
1161 // data is already provided in the buffer
1162 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001163 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001164 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001165 } else {
1166 ab = new ABuffer(outLength);
1167 memcpy(ab->data(),
1168 (const uint8_t *)mb->data() + mb->range_offset(),
1169 mb->range_length());
1170 }
1171
1172 if (audio && mAudioIsVorbis) {
1173 int32_t numPageSamples;
1174 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1175 numPageSamples = -1;
1176 }
1177
1178 uint8_t* abEnd = ab->data() + mb->range_length();
1179 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1180 }
1181
Lajos Molnare26940f2014-07-31 10:31:26 -07001182 sp<AMessage> meta = ab->meta();
1183
Robert Shih3423bbd2014-07-16 15:47:09 -07001184 int64_t timeUs;
1185 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001186 meta->setInt64("timeUs", timeUs);
1187
Lajos Molnare26940f2014-07-31 10:31:26 -07001188 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1189 const char *mime;
1190 CHECK(mTimedTextTrack.mSource != NULL
1191 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1192 meta->setString("mime", mime);
1193 }
1194
Robert Shih3423bbd2014-07-16 15:47:09 -07001195 int64_t durationUs;
1196 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1197 meta->setInt64("durationUs", durationUs);
1198 }
1199
1200 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1201 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1202 }
1203
1204 if (actualTimeUs) {
1205 *actualTimeUs = timeUs;
1206 }
1207
1208 mb->release();
1209 mb = NULL;
1210
1211 return ab;
1212}
1213
Robert Shih17f6dd62014-08-20 17:00:21 -07001214void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001215 Mutex::Autolock _l(mReadBufferLock);
1216
1217 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1218 mPendingReadBufferTypes |= (1 << trackType);
1219 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1220 msg->setInt32("trackType", trackType);
1221 msg->post();
1222 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001223}
1224
1225void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1226 int32_t tmpType;
1227 CHECK(msg->findInt32("trackType", &tmpType));
1228 media_track_type trackType = (media_track_type)tmpType;
Lajos Molnar84f52782014-09-11 10:01:55 -07001229 {
1230 // only protect the variable change, as readBuffer may
1231 // take considerable time. This may result in one extra
1232 // read being processed, but that is benign.
1233 Mutex::Autolock _l(mReadBufferLock);
1234 mPendingReadBufferTypes &= ~(1 << trackType);
1235 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001236 readBuffer(trackType);
1237}
1238
Andreas Huberafed0e12011-09-20 15:39:58 -07001239void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001240 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001241 // Do not read data if Widevine source is stopped
1242 if (mStopRead) {
1243 return;
1244 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001245 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001246 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001247 switch (trackType) {
1248 case MEDIA_TRACK_TYPE_VIDEO:
1249 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001250 if (mIsWidevine) {
1251 maxBuffers = 2;
1252 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001253 break;
1254 case MEDIA_TRACK_TYPE_AUDIO:
1255 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001256 if (mIsWidevine) {
1257 maxBuffers = 8;
1258 } else {
1259 maxBuffers = 64;
1260 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001261 break;
1262 case MEDIA_TRACK_TYPE_SUBTITLE:
1263 track = &mSubtitleTrack;
1264 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001265 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1266 track = &mTimedTextTrack;
1267 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001268 default:
1269 TRESPASS();
1270 }
1271
1272 if (track->mSource == NULL) {
1273 return;
1274 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001275
1276 if (actualTimeUs) {
1277 *actualTimeUs = seekTimeUs;
1278 }
1279
1280 MediaSource::ReadOptions options;
1281
1282 bool seeking = false;
1283
1284 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001285 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001286 seeking = true;
1287 }
1288
Robert Shih3423bbd2014-07-16 15:47:09 -07001289 if (mIsWidevine && trackType != MEDIA_TRACK_TYPE_AUDIO) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001290 options.setNonBlocking();
1291 }
1292
Phil Burkc5cc2e22014-09-09 20:08:39 -07001293 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001294 MediaBuffer *mbuf;
1295 status_t err = track->mSource->read(&mbuf, &options);
1296
1297 options.clearSeekTo();
1298
1299 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001300 int64_t timeUs;
1301 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1302 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1303 mAudioTimeUs = timeUs;
1304 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1305 mVideoTimeUs = timeUs;
1306 }
1307
Robert Shih3423bbd2014-07-16 15:47:09 -07001308 // formatChange && seeking: track whose source is changed during selection
1309 // formatChange && !seeking: track whose source is not changed during selection
1310 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001311 if ((seeking || formatChange)
1312 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1313 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001314 ATSParser::DiscontinuityType type = (formatChange && seeking)
1315 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1316 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001317 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001318 }
1319
Robert Shih3423bbd2014-07-16 15:47:09 -07001320 sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001321 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001322 formatChange = false;
1323 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001324 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001325 } else if (err == WOULD_BLOCK) {
1326 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001327 } else if (err == INFO_FORMAT_CHANGED) {
1328#if 0
1329 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001330 ATSParser::DISCONTINUITY_FORMATCHANGE,
1331 NULL,
1332 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001333#endif
1334 } else {
1335 track->mPackets->signalEOS(err);
1336 break;
1337 }
1338 }
1339}
1340
Andreas Huberafed0e12011-09-20 15:39:58 -07001341} // namespace android