blob: 5a51ba6210851cc984b6291ca1d2e3862477d48a [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
Chong Zhangefbb6192015-01-30 17:13:27 -080043static int64_t kLowWaterMarkUs = 2000000ll; // 2secs
44static int64_t kHighWaterMarkUs = 5000000ll; // 5secs
45static const ssize_t kLowWaterMarkBytes = 40000;
46static const ssize_t kHighWaterMarkBytes = 200000;
47
Andreas Huberafed0e12011-09-20 15:39:58 -070048NuPlayer::GenericSource::GenericSource(
Andreas Huberb5f25f02013-02-05 10:14:26 -080049 const sp<AMessage> &notify,
Lajos Molnarcc227032014-07-17 15:33:06 -070050 bool uidValid,
51 uid_t uid)
Andreas Huberb5f25f02013-02-05 10:14:26 -080052 : Source(notify),
Robert Shih5c67ddc2014-11-04 17:46:05 -080053 mAudioTimeUs(0),
54 mAudioLastDequeueTimeUs(0),
55 mVideoTimeUs(0),
56 mVideoLastDequeueTimeUs(0),
Robert Shih3423bbd2014-07-16 15:47:09 -070057 mFetchSubtitleDataGeneration(0),
Lajos Molnare26940f2014-07-31 10:31:26 -070058 mFetchTimedTextDataGeneration(0),
Andreas Huberb5f25f02013-02-05 10:14:26 -080059 mDurationUs(0ll),
Lajos Molnarcc227032014-07-17 15:33:06 -070060 mAudioIsVorbis(false),
Chong Zhang3de157d2014-08-05 20:54:44 -070061 mIsWidevine(false),
Chong Zhang42e81532014-12-01 13:44:26 -080062 mIsSecure(false),
Chong Zhangefbb6192015-01-30 17:13:27 -080063 mIsStreaming(false),
Lajos Molnarcc227032014-07-17 15:33:06 -070064 mUIDValid(uidValid),
Chong Zhangd354d8d2014-08-20 13:09:58 -070065 mUID(uid),
Chong Zhanga6bf21f2014-11-19 20:26:34 -080066 mFd(-1),
Ronghua Wu80276872014-08-28 15:50:29 -070067 mDrmManagerClient(NULL),
Chong Zhang2a3cc9a2014-08-21 17:48:26 -070068 mMetaDataSize(-1ll),
69 mBitrate(-1ll),
Lajos Molnar84f52782014-09-11 10:01:55 -070070 mPollBufferingGeneration(0),
Chong Zhangefbb6192015-01-30 17:13:27 -080071 mPendingReadBufferTypes(0),
72 mBuffering(false),
73 mPrepareBuffering(false) {
Chong Zhanga19f33e2014-08-07 15:35:07 -070074 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -070075 DataSource::RegisterDefaultSniffers();
Chong Zhang3de157d2014-08-05 20:54:44 -070076}
77
Chong Zhanga19f33e2014-08-07 15:35:07 -070078void NuPlayer::GenericSource::resetDataSource() {
79 mHTTPService.clear();
Robert Shih360d6d02014-09-29 14:42:35 -070080 mHttpSource.clear();
Chong Zhanga19f33e2014-08-07 15:35:07 -070081 mUri.clear();
82 mUriHeaders.clear();
Chong Zhanga6bf21f2014-11-19 20:26:34 -080083 if (mFd >= 0) {
84 close(mFd);
85 mFd = -1;
86 }
Chong Zhanga19f33e2014-08-07 15:35:07 -070087 mOffset = 0;
88 mLength = 0;
Ronghua Wu80276872014-08-28 15:50:29 -070089 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
90 mDecryptHandle = NULL;
91 mDrmManagerClient = NULL;
92 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -070093 mStopRead = true;
Chong Zhanga19f33e2014-08-07 15:35:07 -070094}
95
96status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -070097 const sp<IMediaHTTPService> &httpService,
98 const char *url,
99 const KeyedVector<String8, String8> *headers) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700100 resetDataSource();
Chong Zhang3de157d2014-08-05 20:54:44 -0700101
Chong Zhanga19f33e2014-08-07 15:35:07 -0700102 mHTTPService = httpService;
103 mUri = url;
Andreas Huberafed0e12011-09-20 15:39:58 -0700104
Chong Zhanga19f33e2014-08-07 15:35:07 -0700105 if (headers) {
106 mUriHeaders = *headers;
Chong Zhang3de157d2014-08-05 20:54:44 -0700107 }
108
Chong Zhanga19f33e2014-08-07 15:35:07 -0700109 // delay data source creation to prepareAsync() to avoid blocking
110 // the calling thread in setDataSource for any significant time.
111 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700112}
113
Chong Zhanga19f33e2014-08-07 15:35:07 -0700114status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -0700115 int fd, int64_t offset, int64_t length) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700116 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700117
Chong Zhanga19f33e2014-08-07 15:35:07 -0700118 mFd = dup(fd);
119 mOffset = offset;
120 mLength = length;
121
122 // delay data source creation to prepareAsync() to avoid blocking
123 // the calling thread in setDataSource for any significant time.
124 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700125}
126
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700127sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const {
128 return mFileMeta;
129}
130
Chong Zhangd354d8d2014-08-20 13:09:58 -0700131status_t NuPlayer::GenericSource::initFromDataSource() {
Lajos Molnarcc227032014-07-17 15:33:06 -0700132 sp<MediaExtractor> extractor;
Chong Zhang91a23ed2015-02-19 16:39:59 -0800133 String8 mimeType;
134 float confidence;
135 sp<AMessage> dummy;
136 bool isWidevineStreaming = false;
Lajos Molnarcc227032014-07-17 15:33:06 -0700137
Chong Zhangd354d8d2014-08-20 13:09:58 -0700138 CHECK(mDataSource != NULL);
139
Lajos Molnarcc227032014-07-17 15:33:06 -0700140 if (mIsWidevine) {
Chong Zhang91a23ed2015-02-19 16:39:59 -0800141 isWidevineStreaming = SniffWVM(
142 mDataSource, &mimeType, &confidence, &dummy);
143 if (!isWidevineStreaming ||
144 strcasecmp(
Lajos Molnarcc227032014-07-17 15:33:06 -0700145 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
146 ALOGE("unsupported widevine mime: %s", mimeType.string());
Chong Zhang3de157d2014-08-05 20:54:44 -0700147 return UNKNOWN_ERROR;
Lajos Molnarcc227032014-07-17 15:33:06 -0700148 }
Chong Zhang91a23ed2015-02-19 16:39:59 -0800149 } else if (mIsStreaming) {
150 if (mSniffedMIME.empty()) {
151 if (!mDataSource->sniff(&mimeType, &confidence, &dummy)) {
152 return UNKNOWN_ERROR;
153 }
154 mSniffedMIME = mimeType.string();
155 }
156 isWidevineStreaming = !strcasecmp(
157 mSniffedMIME.c_str(), MEDIA_MIMETYPE_CONTAINER_WVM);
158 }
Lajos Molnarcc227032014-07-17 15:33:06 -0700159
Chong Zhang91a23ed2015-02-19 16:39:59 -0800160 if (isWidevineStreaming) {
161 // we don't want cached source for widevine streaming.
162 mCachedSource.clear();
163 mDataSource = mHttpSource;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700164 mWVMExtractor = new WVMExtractor(mDataSource);
165 mWVMExtractor->setAdaptiveStreamingMode(true);
Lajos Molnarcc227032014-07-17 15:33:06 -0700166 if (mUIDValid) {
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700167 mWVMExtractor->setUID(mUID);
Lajos Molnarcc227032014-07-17 15:33:06 -0700168 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700169 extractor = mWVMExtractor;
Lajos Molnarcc227032014-07-17 15:33:06 -0700170 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700171 extractor = MediaExtractor::Create(mDataSource,
172 mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str());
Lajos Molnarcc227032014-07-17 15:33:06 -0700173 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700174
Chong Zhang3de157d2014-08-05 20:54:44 -0700175 if (extractor == NULL) {
176 return UNKNOWN_ERROR;
177 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700178
Ronghua Wu80276872014-08-28 15:50:29 -0700179 if (extractor->getDrmFlag()) {
180 checkDrmStatus(mDataSource);
181 }
182
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700183 mFileMeta = extractor->getMetaData();
184 if (mFileMeta != NULL) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700185 int64_t duration;
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700186 if (mFileMeta->findInt64(kKeyDuration, &duration)) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700187 mDurationUs = duration;
188 }
Chong Zhang42e81532014-12-01 13:44:26 -0800189
190 if (!mIsWidevine) {
191 // Check mime to see if we actually have a widevine source.
192 // If the data source is not URL-type (eg. file source), we
193 // won't be able to tell until now.
194 const char *fileMime;
195 if (mFileMeta->findCString(kKeyMIMEType, &fileMime)
196 && !strncasecmp(fileMime, "video/wvm", 9)) {
197 mIsWidevine = true;
198 }
199 }
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700200 }
201
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700202 int32_t totalBitrate = 0;
203
Marco Nelissen705d3292014-09-19 15:14:37 -0700204 size_t numtracks = extractor->countTracks();
205 if (numtracks == 0) {
206 return UNKNOWN_ERROR;
207 }
208
209 for (size_t i = 0; i < numtracks; ++i) {
Chong Zhangafc0a872014-08-26 09:56:52 -0700210 sp<MediaSource> track = extractor->getTrack(i);
Wei Jiae31eeb42015-08-28 10:35:35 -0700211 if (track == NULL) {
212 continue;
213 }
Chong Zhangafc0a872014-08-26 09:56:52 -0700214
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 sp<MetaData> meta = extractor->getTrackMetaData(i);
216
217 const char *mime;
218 CHECK(meta->findCString(kKeyMIMEType, &mime));
219
Chong Zhangafc0a872014-08-26 09:56:52 -0700220 // Do the string compare immediately with "mime",
221 // we can't assume "mime" would stay valid after another
222 // extractor operation, some extractors might modify meta
223 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 if (!strncasecmp(mime, "audio/", 6)) {
225 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700226 mAudioTrack.mIndex = i;
227 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700228 mAudioTrack.mPackets =
229 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700230
231 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
232 mAudioIsVorbis = true;
233 } else {
234 mAudioIsVorbis = false;
235 }
236 }
237 } else if (!strncasecmp(mime, "video/", 6)) {
238 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700239 mVideoTrack.mIndex = i;
240 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700241 mVideoTrack.mPackets =
242 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700243
244 // check if the source requires secure buffers
245 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700246 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
247 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800248 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700249 if (mUIDValid) {
250 extractor->setUID(mUID);
251 }
Chong Zhang7e892182014-08-05 11:58:21 -0700252 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700253 }
254 }
255
Wei Jiae31eeb42015-08-28 10:35:35 -0700256 mSources.push(track);
257 int64_t durationUs;
258 if (meta->findInt64(kKeyDuration, &durationUs)) {
259 if (durationUs > mDurationUs) {
260 mDurationUs = durationUs;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700261 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700262 }
Wei Jiae31eeb42015-08-28 10:35:35 -0700263
264 int32_t bitrate;
265 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
266 totalBitrate += bitrate;
267 } else {
268 totalBitrate = -1;
269 }
270 }
271
272 if (mSources.size() == 0) {
273 ALOGE("b/23705695");
274 return UNKNOWN_ERROR;
Andreas Huberafed0e12011-09-20 15:39:58 -0700275 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700276
Lajos Molnar68fca632015-03-31 10:06:48 -0700277 mBitrate = totalBitrate;
278
279 return OK;
280}
281
282status_t NuPlayer::GenericSource::startSources() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800283 // Start the selected A/V tracks now before we start buffering.
284 // Widevine sources might re-initialize crypto when starting, if we delay
285 // this to start(), all data buffered during prepare would be wasted.
286 // (We don't actually start reading until start().)
287 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
288 ALOGE("failed to start audio track!");
289 return UNKNOWN_ERROR;
290 }
291
292 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
293 ALOGE("failed to start video track!");
294 return UNKNOWN_ERROR;
295 }
296
Chong Zhang3de157d2014-08-05 20:54:44 -0700297 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700298}
299
Ronghua Wu80276872014-08-28 15:50:29 -0700300void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
301 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
302 if (mDecryptHandle != NULL) {
303 CHECK(mDrmManagerClient);
304 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
305 sp<AMessage> msg = dupNotify();
306 msg->setInt32("what", kWhatDrmNoLicense);
307 msg->post();
308 }
309 }
310}
311
312int64_t NuPlayer::GenericSource::getLastReadPosition() {
313 if (mAudioTrack.mSource != NULL) {
314 return mAudioTimeUs;
315 } else if (mVideoTrack.mSource != NULL) {
316 return mVideoTimeUs;
317 } else {
318 return 0;
319 }
320}
321
Chong Zhanga19f33e2014-08-07 15:35:07 -0700322status_t NuPlayer::GenericSource::setBuffers(
323 bool audio, Vector<MediaBuffer *> &buffers) {
Wei Jia67ca3ca2016-05-25 14:10:26 -0700324 if (mIsSecure && !audio && mVideoTrack.mSource != NULL) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700325 return mVideoTrack.mSource->setBuffers(buffers);
326 }
327 return INVALID_OPERATION;
328}
329
Andreas Huberafed0e12011-09-20 15:39:58 -0700330NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700331 if (mLooper != NULL) {
332 mLooper->unregisterHandler(id());
333 mLooper->stop();
334 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800335 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700336}
337
Andreas Huber9575c962013-02-05 13:59:56 -0800338void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700339 if (mLooper == NULL) {
340 mLooper = new ALooper;
341 mLooper->setName("generic");
342 mLooper->start();
343
344 mLooper->registerHandler(this);
345 }
346
347 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());
348 msg->post();
349}
350
351void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700352 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700353 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800354 // set to false first, if the extractor
355 // comes back as secure, set it to true then.
356 mIsSecure = false;
357
Chong Zhangd354d8d2014-08-20 13:09:58 -0700358 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700359 const char* uri = mUri.c_str();
360 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
361
362 if (!strncasecmp("http://", uri, 7)
363 || !strncasecmp("https://", uri, 8)
364 || mIsWidevine) {
365 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
366 if (mHttpSource == NULL) {
367 ALOGE("Failed to create http source!");
368 notifyPreparedAndCleanup(UNKNOWN_ERROR);
369 return;
370 }
371 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700372
Chong Zhangd354d8d2014-08-20 13:09:58 -0700373 mDataSource = DataSource::CreateFromURI(
Robert Shih360d6d02014-09-29 14:42:35 -0700374 mHTTPService, uri, &mUriHeaders, &mContentType,
375 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700376 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700377 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700378
Chong Zhangd354d8d2014-08-20 13:09:58 -0700379 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800380 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700381 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700382
Chong Zhangd354d8d2014-08-20 13:09:58 -0700383 if (mDataSource == NULL) {
384 ALOGE("Failed to create data source!");
385 notifyPreparedAndCleanup(UNKNOWN_ERROR);
386 return;
387 }
388
389 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
390 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
391 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700392
Chong Zhangefbb6192015-01-30 17:13:27 -0800393 // For widevine or other cached streaming cases, we need to wait for
394 // enough buffering before reporting prepared.
395 // Note that even when URL doesn't start with widevine://, mIsWidevine
396 // could still be set to true later, if the streaming or file source
397 // is sniffed to be widevine. We don't want to buffer for file source
398 // in that case, so must check the flag now.
399 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700400 }
401
Chong Zhangd354d8d2014-08-20 13:09:58 -0700402 // check initial caching status
403 status_t err = prefillCacheIfNecessary();
404 if (err != OK) {
405 if (err == -EAGAIN) {
406 (new AMessage(kWhatPrepareAsync, id()))->post(200000);
407 } else {
408 ALOGE("Failed to prefill data cache!");
409 notifyPreparedAndCleanup(UNKNOWN_ERROR);
410 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700411 return;
412 }
413
Chong Zhangd354d8d2014-08-20 13:09:58 -0700414 // init extrator from data source
415 err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700416
417 if (err != OK) {
418 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700419 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700420 return;
421 }
422
Andreas Huber9575c962013-02-05 13:59:56 -0800423 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700424 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
425 sp<AMessage> msg = new AMessage;
426 err = convertMetaDataToMessage(meta, &msg);
427 if(err != OK) {
428 notifyPreparedAndCleanup(err);
429 return;
430 }
431 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800432 }
433
434 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800435 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800436 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700437 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800438 | FLAG_CAN_SEEK_BACKWARD
439 | FLAG_CAN_SEEK_FORWARD
440 | FLAG_CAN_SEEK);
441
Lajos Molnar68fca632015-03-31 10:06:48 -0700442 if (mIsSecure) {
443 // secure decoders must be instantiated before starting widevine source
444 sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, id());
445 notifyInstantiateSecureDecoders(reply);
446 } else {
447 finishPrepareAsync();
448 }
449}
450
451void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) {
452 if (err != OK) {
453 ALOGE("Failed to instantiate secure decoders!");
454 notifyPreparedAndCleanup(err);
455 return;
456 }
457 finishPrepareAsync();
458}
459
460void NuPlayer::GenericSource::finishPrepareAsync() {
461 status_t err = startSources();
462 if (err != OK) {
463 ALOGE("Failed to init start data source!");
464 notifyPreparedAndCleanup(err);
465 return;
466 }
467
Chong Zhangefbb6192015-01-30 17:13:27 -0800468 if (mIsStreaming) {
469 mPrepareBuffering = true;
470
471 ensureCacheIsFetching();
472 restartPollBuffering();
473 } else {
474 notifyPrepared();
475 }
Andreas Huber9575c962013-02-05 13:59:56 -0800476}
477
Chong Zhangd354d8d2014-08-20 13:09:58 -0700478void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
479 if (err != OK) {
480 mMetaDataSize = -1ll;
481 mContentType = "";
482 mSniffedMIME = "";
Robert Shihf3eb8262015-09-02 14:02:47 -0700483 {
484 sp<DataSource> dataSource = mDataSource;
485 sp<NuCachedSource2> cachedSource = mCachedSource;
486 sp<DataSource> httpSource = mHttpSource;
487 {
488 Mutex::Autolock _l(mDisconnectLock);
489 mDataSource.clear();
Wei Jia09c291c2015-10-22 11:35:04 -0700490 mDecryptHandle = NULL;
Wei Jia224858e2015-10-19 16:14:14 -0700491 mDrmManagerClient = NULL;
Robert Shihf3eb8262015-09-02 14:02:47 -0700492 mCachedSource.clear();
493 mHttpSource.clear();
494 }
495 }
Lajos Molnar68fca632015-03-31 10:06:48 -0700496 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700497
498 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700499 }
500 notifyPrepared(err);
501}
502
503status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
504 CHECK(mDataSource != NULL);
505
506 if (mCachedSource == NULL) {
507 // no prefill if the data source is not cached
508 return OK;
509 }
510
511 // We're not doing this for streams that appear to be audio-only
512 // streams to ensure that even low bandwidth streams start
513 // playing back fairly instantly.
514 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
515 return OK;
516 }
517
518 // We're going to prefill the cache before trying to instantiate
519 // the extractor below, as the latter is an operation that otherwise
520 // could block on the datasource for a significant amount of time.
521 // During that time we'd be unable to abort the preparation phase
522 // without this prefill.
523
524 // Initially make sure we have at least 192 KB for the sniff
525 // to complete without blocking.
526 static const size_t kMinBytesForSniffing = 192 * 1024;
527 static const size_t kDefaultMetaSize = 200000;
528
529 status_t finalStatus;
530
531 size_t cachedDataRemaining =
532 mCachedSource->approxDataRemaining(&finalStatus);
533
534 if (finalStatus != OK || (mMetaDataSize >= 0
535 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
536 ALOGV("stop caching, status %d, "
537 "metaDataSize %lld, cachedDataRemaining %zu",
538 finalStatus, mMetaDataSize, cachedDataRemaining);
539 return OK;
540 }
541
542 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
543
544 if (mMetaDataSize < 0
545 && cachedDataRemaining >= kMinBytesForSniffing) {
546 String8 tmp;
547 float confidence;
548 sp<AMessage> meta;
549 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
550 return UNKNOWN_ERROR;
551 }
552
553 // We successfully identified the file's extractor to
554 // be, remember this mime type so we don't have to
555 // sniff it again when we call MediaExtractor::Create()
556 mSniffedMIME = tmp.string();
557
558 if (meta == NULL
559 || !meta->findInt64("meta-data-size",
560 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
561 mMetaDataSize = kDefaultMetaSize;
562 }
563
564 if (mMetaDataSize < 0ll) {
565 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
566 return UNKNOWN_ERROR;
567 }
568 }
569
570 return -EAGAIN;
571}
572
Andreas Huberafed0e12011-09-20 15:39:58 -0700573void NuPlayer::GenericSource::start() {
574 ALOGI("start");
575
Andy Hung2abde2c2014-09-30 14:40:32 -0700576 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700577 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700578 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700579 }
580
581 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700582 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700583 }
Ronghua Wu80276872014-08-28 15:50:29 -0700584
585 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
586 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800587
588 (new AMessage(kWhatStart, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700589}
590
591void NuPlayer::GenericSource::stop() {
592 // nothing to do, just account for DRM playback status
593 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
594 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800595 if (mIsWidevine || mIsSecure) {
596 // For widevine or secure sources we need to prevent any further reads.
Andy Hung2abde2c2014-09-30 14:40:32 -0700597 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
598 sp<AMessage> response;
599 (void) msg->postAndAwaitResponse(&response);
600 }
Ronghua Wu80276872014-08-28 15:50:29 -0700601}
602
603void NuPlayer::GenericSource::pause() {
604 // nothing to do, just account for DRM playback status
605 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
606 mStarted = false;
607}
608
609void NuPlayer::GenericSource::resume() {
610 // nothing to do, just account for DRM playback status
611 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
612 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800613
614 (new AMessage(kWhatResume, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700615}
616
Chong Zhang48296b72014-09-14 14:28:45 -0700617void NuPlayer::GenericSource::disconnect() {
Robert Shihf3eb8262015-09-02 14:02:47 -0700618 sp<DataSource> dataSource, httpSource;
619 {
620 Mutex::Autolock _l(mDisconnectLock);
621 dataSource = mDataSource;
622 httpSource = mHttpSource;
623 }
624
625 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700626 // disconnect data source
Robert Shihf3eb8262015-09-02 14:02:47 -0700627 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
628 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700629 }
Robert Shihf3eb8262015-09-02 14:02:47 -0700630 } else if (httpSource != NULL) {
631 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700632 }
633}
634
Ronghua Wu80276872014-08-28 15:50:29 -0700635void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
636 if (mDecryptHandle != NULL) {
637 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
638 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700639 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
640 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700641}
642
643status_t NuPlayer::GenericSource::feedMoreTSData() {
644 return OK;
645}
646
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700647void NuPlayer::GenericSource::schedulePollBuffering() {
648 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
649 msg->setInt32("generation", mPollBufferingGeneration);
650 msg->post(1000000ll);
651}
652
653void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800654 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700655 ++mPollBufferingGeneration;
656}
657
Chong Zhangefbb6192015-01-30 17:13:27 -0800658void NuPlayer::GenericSource::restartPollBuffering() {
659 if (mIsStreaming) {
660 cancelPollBuffering();
661 onPollBuffering();
662 }
663}
664
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700665void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800666 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
667
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700668 sp<AMessage> msg = dupNotify();
669 msg->setInt32("what", kWhatBufferingUpdate);
670 msg->setInt32("percentage", percentage);
671 msg->post();
672}
673
Chong Zhangefbb6192015-01-30 17:13:27 -0800674void NuPlayer::GenericSource::startBufferingIfNecessary() {
675 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
676 mPrepareBuffering, mBuffering);
677
678 if (mPrepareBuffering) {
679 return;
680 }
681
682 if (!mBuffering) {
683 mBuffering = true;
684
685 ensureCacheIsFetching();
686 sendCacheStats();
687
688 sp<AMessage> notify = dupNotify();
689 notify->setInt32("what", kWhatPauseOnBufferingStart);
690 notify->post();
691 }
692}
693
694void NuPlayer::GenericSource::stopBufferingIfNecessary() {
695 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
696 mPrepareBuffering, mBuffering);
697
698 if (mPrepareBuffering) {
699 mPrepareBuffering = false;
700 notifyPrepared();
701 return;
702 }
703
704 if (mBuffering) {
705 mBuffering = false;
706
707 sendCacheStats();
708
709 sp<AMessage> notify = dupNotify();
710 notify->setInt32("what", kWhatResumeOnBufferingEnd);
711 notify->post();
712 }
713}
714
715void NuPlayer::GenericSource::sendCacheStats() {
716 int32_t kbps = 0;
717 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700718
Chong Zhang91a23ed2015-02-19 16:39:59 -0800719 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800720 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhang91a23ed2015-02-19 16:39:59 -0800721 } else if (mCachedSource != NULL) {
722 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800723 }
724
725 if (err == OK) {
726 sp<AMessage> notify = dupNotify();
727 notify->setInt32("what", kWhatCacheStats);
728 notify->setInt32("bandwidth", kbps);
729 notify->post();
730 }
731}
732
733void NuPlayer::GenericSource::ensureCacheIsFetching() {
734 if (mCachedSource != NULL) {
735 mCachedSource->resumeFetchingIfNecessary();
736 }
737}
738
739void NuPlayer::GenericSource::onPollBuffering() {
740 status_t finalStatus = UNKNOWN_ERROR;
741 int64_t cachedDurationUs = -1ll;
742 ssize_t cachedDataRemaining = -1;
743
Chong Zhang91a23ed2015-02-19 16:39:59 -0800744 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
745 "WVMExtractor and NuCachedSource both present");
746
747 if (mWVMExtractor != NULL) {
748 cachedDurationUs =
749 mWVMExtractor->getCachedDurationUs(&finalStatus);
750 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800751 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700752 mCachedSource->approxDataRemaining(&finalStatus);
753
754 if (finalStatus == OK) {
755 off64_t size;
756 int64_t bitrate = 0ll;
757 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
758 bitrate = size * 8000000ll / mDurationUs;
759 } else if (mBitrate > 0) {
760 bitrate = mBitrate;
761 }
762 if (bitrate > 0) {
763 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
764 }
765 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700766 }
767
Chong Zhangefbb6192015-01-30 17:13:27 -0800768 if (finalStatus != OK) {
769 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
770
771 if (finalStatus == ERROR_END_OF_STREAM) {
772 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700773 }
774
Chong Zhangefbb6192015-01-30 17:13:27 -0800775 stopBufferingIfNecessary();
776 return;
777 } else if (cachedDurationUs >= 0ll) {
778 if (mDurationUs > 0ll) {
779 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
780 int percentage = 100.0 * cachedPosUs / mDurationUs;
781 if (percentage > 100) {
782 percentage = 100;
783 }
784
785 notifyBufferingUpdate(percentage);
786 }
787
788 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
789 cachedDurationUs / 1000000.0f);
790
791 if (cachedDurationUs < kLowWaterMarkUs) {
792 startBufferingIfNecessary();
793 } else if (cachedDurationUs > kHighWaterMarkUs) {
794 stopBufferingIfNecessary();
795 }
796 } else if (cachedDataRemaining >= 0) {
797 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
798 cachedDataRemaining);
799
800 if (cachedDataRemaining < kLowWaterMarkBytes) {
801 startBufferingIfNecessary();
802 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
803 stopBufferingIfNecessary();
804 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700805 }
806
807 schedulePollBuffering();
808}
809
Robert Shih3423bbd2014-07-16 15:47:09 -0700810void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
811 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700812 case kWhatPrepareAsync:
813 {
814 onPrepareAsync();
815 break;
816 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700817 case kWhatFetchSubtitleData:
818 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700819 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
820 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
821 break;
822 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700823
Lajos Molnare26940f2014-07-31 10:31:26 -0700824 case kWhatFetchTimedTextData:
825 {
826 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
827 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700828 break;
829 }
830
831 case kWhatSendSubtitleData:
832 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700833 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
834 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
835 break;
836 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700837
Lajos Molnare26940f2014-07-31 10:31:26 -0700838 case kWhatSendTimedTextData:
839 {
840 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
841 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700842 break;
843 }
844
845 case kWhatChangeAVSource:
846 {
847 int32_t trackIndex;
848 CHECK(msg->findInt32("trackIndex", &trackIndex));
849 const sp<MediaSource> source = mSources.itemAt(trackIndex);
850
851 Track* track;
852 const char *mime;
853 media_track_type trackType, counterpartType;
854 sp<MetaData> meta = source->getFormat();
855 meta->findCString(kKeyMIMEType, &mime);
856 if (!strncasecmp(mime, "audio/", 6)) {
857 track = &mAudioTrack;
858 trackType = MEDIA_TRACK_TYPE_AUDIO;
859 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
860 } else {
861 CHECK(!strncasecmp(mime, "video/", 6));
862 track = &mVideoTrack;
863 trackType = MEDIA_TRACK_TYPE_VIDEO;
864 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
865 }
866
867
868 if (track->mSource != NULL) {
869 track->mSource->stop();
870 }
871 track->mSource = source;
872 track->mSource->start();
873 track->mIndex = trackIndex;
874
Robert Shih3423bbd2014-07-16 15:47:09 -0700875 int64_t timeUs, actualTimeUs;
876 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800877 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
878 timeUs = mAudioLastDequeueTimeUs;
879 } else {
880 timeUs = mVideoLastDequeueTimeUs;
881 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700882 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
883 readBuffer(counterpartType, -1, NULL, formatChange);
884 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
885
886 break;
887 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800888
889 case kWhatStart:
890 case kWhatResume:
891 {
892 restartPollBuffering();
893 break;
894 }
895
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700896 case kWhatPollBuffering:
897 {
898 int32_t generation;
899 CHECK(msg->findInt32("generation", &generation));
900 if (generation == mPollBufferingGeneration) {
901 onPollBuffering();
902 }
903 break;
904 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700905
906 case kWhatGetFormat:
907 {
908 onGetFormatMeta(msg);
909 break;
910 }
911
912 case kWhatGetSelectedTrack:
913 {
914 onGetSelectedTrack(msg);
915 break;
916 }
917
918 case kWhatSelectTrack:
919 {
920 onSelectTrack(msg);
921 break;
922 }
923
924 case kWhatSeek:
925 {
926 onSeek(msg);
927 break;
928 }
929
930 case kWhatReadBuffer:
931 {
932 onReadBuffer(msg);
933 break;
934 }
935
Lajos Molnar68fca632015-03-31 10:06:48 -0700936 case kWhatSecureDecodersInstantiated:
937 {
938 int32_t err;
939 CHECK(msg->findInt32("err", &err));
940 onSecureDecodersInstantiated(err);
941 break;
942 }
943
Andy Hung2abde2c2014-09-30 14:40:32 -0700944 case kWhatStopWidevine:
945 {
946 // mStopRead is only used for Widevine to prevent the video source
947 // from being read while the associated video decoder is shutting down.
948 mStopRead = true;
949 if (mVideoTrack.mSource != NULL) {
950 mVideoTrack.mPackets->clear();
951 }
952 sp<AMessage> response = new AMessage;
953 uint32_t replyID;
954 CHECK(msg->senderAwaitsResponse(&replyID));
955 response->postReply(replyID);
956 break;
957 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700958 default:
959 Source::onMessageReceived(msg);
960 break;
961 }
962}
963
Lajos Molnare26940f2014-07-31 10:31:26 -0700964void NuPlayer::GenericSource::fetchTextData(
965 uint32_t sendWhat,
966 media_track_type type,
967 int32_t curGen,
968 sp<AnotherPacketSource> packets,
969 sp<AMessage> msg) {
970 int32_t msgGeneration;
971 CHECK(msg->findInt32("generation", &msgGeneration));
972 if (msgGeneration != curGen) {
973 // stale
974 return;
975 }
976
977 int32_t avail;
978 if (packets->hasBufferAvailable(&avail)) {
979 return;
980 }
981
982 int64_t timeUs;
983 CHECK(msg->findInt64("timeUs", &timeUs));
984
985 int64_t subTimeUs;
986 readBuffer(type, timeUs, &subTimeUs);
987
988 int64_t delayUs = subTimeUs - timeUs;
989 if (msg->what() == kWhatFetchSubtitleData) {
990 const int64_t oneSecUs = 1000000ll;
991 delayUs -= oneSecUs;
992 }
993 sp<AMessage> msg2 = new AMessage(sendWhat, id());
994 msg2->setInt32("generation", msgGeneration);
995 msg2->post(delayUs < 0 ? 0 : delayUs);
996}
997
998void NuPlayer::GenericSource::sendTextData(
999 uint32_t what,
1000 media_track_type type,
1001 int32_t curGen,
1002 sp<AnotherPacketSource> packets,
1003 sp<AMessage> msg) {
1004 int32_t msgGeneration;
1005 CHECK(msg->findInt32("generation", &msgGeneration));
1006 if (msgGeneration != curGen) {
1007 // stale
1008 return;
1009 }
1010
1011 int64_t subTimeUs;
1012 if (packets->nextBufferTime(&subTimeUs) != OK) {
1013 return;
1014 }
1015
1016 int64_t nextSubTimeUs;
1017 readBuffer(type, -1, &nextSubTimeUs);
1018
1019 sp<ABuffer> buffer;
1020 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
1021 if (dequeueStatus == OK) {
1022 sp<AMessage> notify = dupNotify();
1023 notify->setInt32("what", what);
1024 notify->setBuffer("buffer", buffer);
1025 notify->post();
1026
1027 const int64_t delayUs = nextSubTimeUs - subTimeUs;
1028 msg->post(delayUs < 0 ? 0 : delayUs);
1029 }
1030}
1031
Andreas Huber84066782011-08-16 09:34:26 -07001032sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001033 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
1034 msg->setInt32("audio", audio);
1035
1036 sp<AMessage> response;
1037 void *format;
1038 status_t err = msg->postAndAwaitResponse(&response);
1039 if (err == OK && response != NULL) {
1040 CHECK(response->findPointer("format", &format));
1041 return (MetaData *)format;
1042 } else {
1043 return NULL;
1044 }
1045}
1046
1047void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1048 int32_t audio;
1049 CHECK(msg->findInt32("audio", &audio));
1050
1051 sp<AMessage> response = new AMessage;
1052 sp<MetaData> format = doGetFormatMeta(audio);
1053 response->setPointer("format", format.get());
1054
1055 uint32_t replyID;
1056 CHECK(msg->senderAwaitsResponse(&replyID));
1057 response->postReply(replyID);
1058}
1059
1060sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001061 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1062
1063 if (source == NULL) {
1064 return NULL;
1065 }
1066
1067 return source->getFormat();
1068}
1069
1070status_t NuPlayer::GenericSource::dequeueAccessUnit(
1071 bool audio, sp<ABuffer> *accessUnit) {
1072 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1073
1074 if (track->mSource == NULL) {
1075 return -EWOULDBLOCK;
1076 }
1077
Lajos Molnarcc227032014-07-17 15:33:06 -07001078 if (mIsWidevine && !audio) {
1079 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001080 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001081 }
1082
Andreas Huberafed0e12011-09-20 15:39:58 -07001083 status_t finalResult;
1084 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001085 if (finalResult == OK) {
1086 postReadBuffer(
1087 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1088 return -EWOULDBLOCK;
1089 }
1090 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001091 }
1092
1093 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1094
Robert Shih3423bbd2014-07-16 15:47:09 -07001095 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001096 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001097 }
1098
Robert Shih3423bbd2014-07-16 15:47:09 -07001099 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001100 if (mSubtitleTrack.mSource != NULL) {
1101 mSubtitleTrack.mPackets->clear();
1102 mFetchSubtitleDataGeneration++;
1103 }
1104 if (mTimedTextTrack.mSource != NULL) {
1105 mTimedTextTrack.mPackets->clear();
1106 mFetchTimedTextDataGeneration++;
1107 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001108 return result;
1109 }
1110
1111 int64_t timeUs;
1112 status_t eosResult; // ignored
1113 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001114 if (audio) {
1115 mAudioLastDequeueTimeUs = timeUs;
1116 } else {
1117 mVideoLastDequeueTimeUs = timeUs;
1118 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001119
1120 if (mSubtitleTrack.mSource != NULL
1121 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001122 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1123 msg->setInt64("timeUs", timeUs);
1124 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1125 msg->post();
1126 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001127
Lajos Molnare26940f2014-07-31 10:31:26 -07001128 if (mTimedTextTrack.mSource != NULL
1129 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1130 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1131 msg->setInt64("timeUs", timeUs);
1132 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1133 msg->post();
1134 }
1135
Andreas Huberafed0e12011-09-20 15:39:58 -07001136 return result;
1137}
1138
1139status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1140 *durationUs = mDurationUs;
1141 return OK;
1142}
1143
Robert Shihdd235722014-06-12 14:49:23 -07001144size_t NuPlayer::GenericSource::getTrackCount() const {
1145 return mSources.size();
1146}
1147
1148sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1149 size_t trackCount = mSources.size();
1150 if (trackIndex >= trackCount) {
1151 return NULL;
1152 }
1153
1154 sp<AMessage> format = new AMessage();
1155 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1156
1157 const char *mime;
1158 CHECK(meta->findCString(kKeyMIMEType, &mime));
1159
1160 int32_t trackType;
1161 if (!strncasecmp(mime, "video/", 6)) {
1162 trackType = MEDIA_TRACK_TYPE_VIDEO;
1163 } else if (!strncasecmp(mime, "audio/", 6)) {
1164 trackType = MEDIA_TRACK_TYPE_AUDIO;
1165 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1166 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1167 } else {
1168 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1169 }
1170 format->setInt32("type", trackType);
1171
1172 const char *lang;
1173 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1174 lang = "und";
1175 }
1176 format->setString("language", lang);
1177
1178 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1179 format->setString("mime", mime);
1180
1181 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1182 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1183 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1184 meta->findInt32(kKeyTrackIsForced, &isForced);
1185
1186 format->setInt32("auto", !!isAutoselect);
1187 format->setInt32("default", !!isDefault);
1188 format->setInt32("forced", !!isForced);
1189 }
1190
1191 return format;
1192}
1193
Lajos Molnare26940f2014-07-31 10:31:26 -07001194ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -07001195 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1196 msg->setInt32("type", type);
1197
1198 sp<AMessage> response;
1199 int32_t index;
1200 status_t err = msg->postAndAwaitResponse(&response);
1201 if (err == OK && response != NULL) {
1202 CHECK(response->findInt32("index", &index));
1203 return index;
1204 } else {
1205 return -1;
1206 }
1207}
1208
1209void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1210 int32_t tmpType;
1211 CHECK(msg->findInt32("type", &tmpType));
1212 media_track_type type = (media_track_type)tmpType;
1213
1214 sp<AMessage> response = new AMessage;
1215 ssize_t index = doGetSelectedTrack(type);
1216 response->setInt32("index", index);
1217
1218 uint32_t replyID;
1219 CHECK(msg->senderAwaitsResponse(&replyID));
1220 response->postReply(replyID);
1221}
1222
1223ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001224 const Track *track = NULL;
1225 switch (type) {
1226 case MEDIA_TRACK_TYPE_VIDEO:
1227 track = &mVideoTrack;
1228 break;
1229 case MEDIA_TRACK_TYPE_AUDIO:
1230 track = &mAudioTrack;
1231 break;
1232 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1233 track = &mTimedTextTrack;
1234 break;
1235 case MEDIA_TRACK_TYPE_SUBTITLE:
1236 track = &mSubtitleTrack;
1237 break;
1238 default:
1239 break;
1240 }
1241
1242 if (track != NULL && track->mSource != NULL) {
1243 return track->mIndex;
1244 }
1245
1246 return -1;
1247}
1248
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001249status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001250 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001251 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1252 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001253 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001254 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001255
1256 sp<AMessage> response;
1257 status_t err = msg->postAndAwaitResponse(&response);
1258 if (err == OK && response != NULL) {
1259 CHECK(response->findInt32("err", &err));
1260 }
1261
1262 return err;
1263}
1264
1265void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1266 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001267 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001268 CHECK(msg->findInt32("trackIndex", &trackIndex));
1269 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001270 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001271
1272 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001273 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001274 response->setInt32("err", err);
1275
1276 uint32_t replyID;
1277 CHECK(msg->senderAwaitsResponse(&replyID));
1278 response->postReply(replyID);
1279}
1280
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001281status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001282 if (trackIndex >= mSources.size()) {
1283 return BAD_INDEX;
1284 }
1285
1286 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001287 Track* track = NULL;
1288 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1289 track = &mSubtitleTrack;
1290 mFetchSubtitleDataGeneration++;
1291 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1292 track = &mTimedTextTrack;
1293 mFetchTimedTextDataGeneration++;
1294 }
1295 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001296 return INVALID_OPERATION;
1297 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001298 track->mSource->stop();
1299 track->mSource = NULL;
1300 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001301 return OK;
1302 }
1303
1304 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1305 sp<MetaData> meta = source->getFormat();
1306 const char *mime;
1307 CHECK(meta->findCString(kKeyMIMEType, &mime));
1308 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001309 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1310 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1311 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001312 return OK;
1313 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001314 track->mIndex = trackIndex;
1315 if (track->mSource != NULL) {
1316 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001317 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001318 track->mSource = mSources.itemAt(trackIndex);
1319 track->mSource->start();
1320 if (track->mPackets == NULL) {
1321 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001322 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001323 track->mPackets->clear();
1324 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001325
1326 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001327
1328 if (isSubtitle) {
1329 mFetchSubtitleDataGeneration++;
1330 } else {
1331 mFetchTimedTextDataGeneration++;
1332 }
1333
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001334 status_t eosResult; // ignored
1335 if (mSubtitleTrack.mSource != NULL
1336 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1337 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1338 msg->setInt64("timeUs", timeUs);
1339 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1340 msg->post();
1341 }
1342
1343 if (mTimedTextTrack.mSource != NULL
1344 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1345 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1346 msg->setInt64("timeUs", timeUs);
1347 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1348 msg->post();
1349 }
1350
Robert Shih3423bbd2014-07-16 15:47:09 -07001351 return OK;
1352 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1353 bool audio = !strncasecmp(mime, "audio/", 6);
1354 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1355 if (track->mSource != NULL && track->mIndex == trackIndex) {
1356 return OK;
1357 }
1358
1359 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1360 msg->setInt32("trackIndex", trackIndex);
1361 msg->post();
1362 return OK;
1363 }
1364
1365 return INVALID_OPERATION;
1366}
1367
Andreas Huberafed0e12011-09-20 15:39:58 -07001368status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001369 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1370 msg->setInt64("seekTimeUs", seekTimeUs);
1371
1372 sp<AMessage> response;
1373 status_t err = msg->postAndAwaitResponse(&response);
1374 if (err == OK && response != NULL) {
1375 CHECK(response->findInt32("err", &err));
1376 }
1377
1378 return err;
1379}
1380
1381void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1382 int64_t seekTimeUs;
1383 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1384
1385 sp<AMessage> response = new AMessage;
1386 status_t err = doSeek(seekTimeUs);
1387 response->setInt32("err", err);
1388
1389 uint32_t replyID;
1390 CHECK(msg->senderAwaitsResponse(&replyID));
1391 response->postReply(replyID);
1392}
1393
1394status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001395 // If the Widevine source is stopped, do not attempt to read any
1396 // more buffers.
1397 if (mStopRead) {
1398 return INVALID_OPERATION;
1399 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001400 if (mVideoTrack.mSource != NULL) {
1401 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001402 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001403
1404 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001405 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001406 }
1407
1408 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001409 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001410 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001411 }
1412
Ronghua Wu80276872014-08-28 15:50:29 -07001413 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1414 if (!mStarted) {
1415 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1416 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001417
1418 // If currently buffering, post kWhatBufferingEnd first, so that
1419 // NuPlayer resumes. Otherwise, if cache hits high watermark
1420 // before new polling happens, no one will resume the playback.
1421 stopBufferingIfNecessary();
1422 restartPollBuffering();
1423
Andreas Huberafed0e12011-09-20 15:39:58 -07001424 return OK;
1425}
1426
Robert Shih3423bbd2014-07-16 15:47:09 -07001427sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1428 MediaBuffer* mb,
1429 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001430 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001431 int64_t *actualTimeUs) {
1432 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1433 size_t outLength = mb->range_length();
1434
1435 if (audio && mAudioIsVorbis) {
1436 outLength += sizeof(int32_t);
1437 }
1438
1439 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001440 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001441 // data is already provided in the buffer
1442 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001443 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001444 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001445 } else {
1446 ab = new ABuffer(outLength);
1447 memcpy(ab->data(),
1448 (const uint8_t *)mb->data() + mb->range_offset(),
1449 mb->range_length());
1450 }
1451
1452 if (audio && mAudioIsVorbis) {
1453 int32_t numPageSamples;
1454 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1455 numPageSamples = -1;
1456 }
1457
1458 uint8_t* abEnd = ab->data() + mb->range_length();
1459 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1460 }
1461
Lajos Molnare26940f2014-07-31 10:31:26 -07001462 sp<AMessage> meta = ab->meta();
1463
Robert Shih3423bbd2014-07-16 15:47:09 -07001464 int64_t timeUs;
1465 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001466 meta->setInt64("timeUs", timeUs);
1467
Wei Jia474d7c72014-12-04 15:12:13 -08001468#if 0
1469 // Temporarily disable pre-roll till we have a full solution to handle
1470 // both single seek and continous seek gracefully.
1471 if (seekTimeUs > timeUs) {
1472 sp<AMessage> extra = new AMessage;
1473 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1474 meta->setMessage("extra", extra);
1475 }
1476#endif
1477
Lajos Molnare26940f2014-07-31 10:31:26 -07001478 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1479 const char *mime;
1480 CHECK(mTimedTextTrack.mSource != NULL
1481 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1482 meta->setString("mime", mime);
1483 }
1484
Robert Shih3423bbd2014-07-16 15:47:09 -07001485 int64_t durationUs;
1486 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1487 meta->setInt64("durationUs", durationUs);
1488 }
1489
1490 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1491 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1492 }
1493
1494 if (actualTimeUs) {
1495 *actualTimeUs = timeUs;
1496 }
1497
1498 mb->release();
1499 mb = NULL;
1500
1501 return ab;
1502}
1503
Robert Shih17f6dd62014-08-20 17:00:21 -07001504void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001505 Mutex::Autolock _l(mReadBufferLock);
1506
1507 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1508 mPendingReadBufferTypes |= (1 << trackType);
1509 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1510 msg->setInt32("trackType", trackType);
1511 msg->post();
1512 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001513}
1514
1515void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1516 int32_t tmpType;
1517 CHECK(msg->findInt32("trackType", &tmpType));
1518 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001519 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001520 {
1521 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001522 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001523 Mutex::Autolock _l(mReadBufferLock);
1524 mPendingReadBufferTypes &= ~(1 << trackType);
1525 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001526}
1527
Andreas Huberafed0e12011-09-20 15:39:58 -07001528void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001529 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001530 // Do not read data if Widevine source is stopped
1531 if (mStopRead) {
1532 return;
1533 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001534 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001535 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001536 switch (trackType) {
1537 case MEDIA_TRACK_TYPE_VIDEO:
1538 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001539 if (mIsWidevine) {
1540 maxBuffers = 2;
1541 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001542 break;
1543 case MEDIA_TRACK_TYPE_AUDIO:
1544 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001545 if (mIsWidevine) {
1546 maxBuffers = 8;
1547 } else {
1548 maxBuffers = 64;
1549 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001550 break;
1551 case MEDIA_TRACK_TYPE_SUBTITLE:
1552 track = &mSubtitleTrack;
1553 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001554 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1555 track = &mTimedTextTrack;
1556 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001557 default:
1558 TRESPASS();
1559 }
1560
1561 if (track->mSource == NULL) {
1562 return;
1563 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001564
1565 if (actualTimeUs) {
1566 *actualTimeUs = seekTimeUs;
1567 }
1568
1569 MediaSource::ReadOptions options;
1570
1571 bool seeking = false;
1572
1573 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001574 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001575 seeking = true;
1576 }
1577
Chong Zhang42e81532014-12-01 13:44:26 -08001578 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001579 options.setNonBlocking();
1580 }
1581
Phil Burkc5cc2e22014-09-09 20:08:39 -07001582 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001583 MediaBuffer *mbuf;
1584 status_t err = track->mSource->read(&mbuf, &options);
1585
1586 options.clearSeekTo();
1587
1588 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001589 int64_t timeUs;
1590 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1591 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1592 mAudioTimeUs = timeUs;
1593 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1594 mVideoTimeUs = timeUs;
1595 }
1596
Robert Shih3423bbd2014-07-16 15:47:09 -07001597 // formatChange && seeking: track whose source is changed during selection
1598 // formatChange && !seeking: track whose source is not changed during selection
1599 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001600 if ((seeking || formatChange)
1601 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1602 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001603 ATSParser::DiscontinuityType type = (formatChange && seeking)
1604 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1605 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001606 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001607 }
1608
Wei Jia474d7c72014-12-04 15:12:13 -08001609 sp<ABuffer> buffer = mediaBufferToABuffer(
1610 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001611 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001612 formatChange = false;
1613 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001614 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001615 } else if (err == WOULD_BLOCK) {
1616 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001617 } else if (err == INFO_FORMAT_CHANGED) {
1618#if 0
1619 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001620 ATSParser::DISCONTINUITY_FORMATCHANGE,
1621 NULL,
1622 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001623#endif
1624 } else {
1625 track->mPackets->signalEOS(err);
1626 break;
1627 }
1628 }
1629}
1630
Andreas Huberafed0e12011-09-20 15:39:58 -07001631} // namespace android