blob: 9b446b831780f8e37a6afc331dc730d1eb2488c7 [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;
133
Chong Zhangd354d8d2014-08-20 13:09:58 -0700134 CHECK(mDataSource != NULL);
135
Lajos Molnarcc227032014-07-17 15:33:06 -0700136 if (mIsWidevine) {
137 String8 mimeType;
138 float confidence;
139 sp<AMessage> dummy;
140 bool success;
141
Chong Zhangd354d8d2014-08-20 13:09:58 -0700142 success = SniffWVM(mDataSource, &mimeType, &confidence, &dummy);
Lajos Molnarcc227032014-07-17 15:33:06 -0700143 if (!success
144 || strcasecmp(
145 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 }
149
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700150 mWVMExtractor = new WVMExtractor(mDataSource);
151 mWVMExtractor->setAdaptiveStreamingMode(true);
Lajos Molnarcc227032014-07-17 15:33:06 -0700152 if (mUIDValid) {
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700153 mWVMExtractor->setUID(mUID);
Lajos Molnarcc227032014-07-17 15:33:06 -0700154 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700155 extractor = mWVMExtractor;
Lajos Molnarcc227032014-07-17 15:33:06 -0700156 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700157 extractor = MediaExtractor::Create(mDataSource,
158 mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str());
Lajos Molnarcc227032014-07-17 15:33:06 -0700159 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700160
Chong Zhang3de157d2014-08-05 20:54:44 -0700161 if (extractor == NULL) {
162 return UNKNOWN_ERROR;
163 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700164
Ronghua Wu80276872014-08-28 15:50:29 -0700165 if (extractor->getDrmFlag()) {
166 checkDrmStatus(mDataSource);
167 }
168
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700169 mFileMeta = extractor->getMetaData();
170 if (mFileMeta != NULL) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700171 int64_t duration;
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700172 if (mFileMeta->findInt64(kKeyDuration, &duration)) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700173 mDurationUs = duration;
174 }
Chong Zhang42e81532014-12-01 13:44:26 -0800175
176 if (!mIsWidevine) {
177 // Check mime to see if we actually have a widevine source.
178 // If the data source is not URL-type (eg. file source), we
179 // won't be able to tell until now.
180 const char *fileMime;
181 if (mFileMeta->findCString(kKeyMIMEType, &fileMime)
182 && !strncasecmp(fileMime, "video/wvm", 9)) {
183 mIsWidevine = true;
Jeff Tinker661a5272014-12-12 15:24:46 -0800184 if (!mUri.empty()) {
185 // streaming, but the app forgot to specify widevine:// url
186 mWVMExtractor = static_cast<WVMExtractor *>(extractor.get());
187 mWVMExtractor->setAdaptiveStreamingMode(true);
188 if (mUIDValid) {
189 mWVMExtractor->setUID(mUID);
190 }
191 }
Chong Zhang42e81532014-12-01 13:44:26 -0800192 }
193 }
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700194 }
195
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700196 int32_t totalBitrate = 0;
197
Marco Nelissen705d3292014-09-19 15:14:37 -0700198 size_t numtracks = extractor->countTracks();
199 if (numtracks == 0) {
200 return UNKNOWN_ERROR;
201 }
202
203 for (size_t i = 0; i < numtracks; ++i) {
Chong Zhangafc0a872014-08-26 09:56:52 -0700204 sp<MediaSource> track = extractor->getTrack(i);
205
Andreas Huberafed0e12011-09-20 15:39:58 -0700206 sp<MetaData> meta = extractor->getTrackMetaData(i);
207
208 const char *mime;
209 CHECK(meta->findCString(kKeyMIMEType, &mime));
210
Chong Zhangafc0a872014-08-26 09:56:52 -0700211 // Do the string compare immediately with "mime",
212 // we can't assume "mime" would stay valid after another
213 // extractor operation, some extractors might modify meta
214 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 if (!strncasecmp(mime, "audio/", 6)) {
216 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700217 mAudioTrack.mIndex = i;
218 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700219 mAudioTrack.mPackets =
220 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700221
222 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
223 mAudioIsVorbis = true;
224 } else {
225 mAudioIsVorbis = false;
226 }
227 }
228 } else if (!strncasecmp(mime, "video/", 6)) {
229 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700230 mVideoTrack.mIndex = i;
231 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700232 mVideoTrack.mPackets =
233 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700234
235 // check if the source requires secure buffers
236 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700237 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
238 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800239 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700240 if (mUIDValid) {
241 extractor->setUID(mUID);
242 }
Chong Zhang7e892182014-08-05 11:58:21 -0700243 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700244 }
245 }
246
247 if (track != NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700248 mSources.push(track);
Andreas Huberafed0e12011-09-20 15:39:58 -0700249 int64_t durationUs;
250 if (meta->findInt64(kKeyDuration, &durationUs)) {
251 if (durationUs > mDurationUs) {
252 mDurationUs = durationUs;
253 }
254 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700255
256 int32_t bitrate;
257 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
258 totalBitrate += bitrate;
259 } else {
260 totalBitrate = -1;
261 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700262 }
263 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700264
Chong Zhangefbb6192015-01-30 17:13:27 -0800265 // Start the selected A/V tracks now before we start buffering.
266 // Widevine sources might re-initialize crypto when starting, if we delay
267 // this to start(), all data buffered during prepare would be wasted.
268 // (We don't actually start reading until start().)
269 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
270 ALOGE("failed to start audio track!");
271 return UNKNOWN_ERROR;
272 }
273
274 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
275 ALOGE("failed to start video track!");
276 return UNKNOWN_ERROR;
277 }
278
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700279 mBitrate = totalBitrate;
280
Chong Zhang3de157d2014-08-05 20:54:44 -0700281 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700282}
283
Ronghua Wu80276872014-08-28 15:50:29 -0700284void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
285 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
286 if (mDecryptHandle != NULL) {
287 CHECK(mDrmManagerClient);
288 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
289 sp<AMessage> msg = dupNotify();
290 msg->setInt32("what", kWhatDrmNoLicense);
291 msg->post();
292 }
293 }
294}
295
296int64_t NuPlayer::GenericSource::getLastReadPosition() {
297 if (mAudioTrack.mSource != NULL) {
298 return mAudioTimeUs;
299 } else if (mVideoTrack.mSource != NULL) {
300 return mVideoTimeUs;
301 } else {
302 return 0;
303 }
304}
305
Chong Zhanga19f33e2014-08-07 15:35:07 -0700306status_t NuPlayer::GenericSource::setBuffers(
307 bool audio, Vector<MediaBuffer *> &buffers) {
Chong Zhang42e81532014-12-01 13:44:26 -0800308 if (mIsSecure && !audio) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700309 return mVideoTrack.mSource->setBuffers(buffers);
310 }
311 return INVALID_OPERATION;
312}
313
Andreas Huberafed0e12011-09-20 15:39:58 -0700314NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700315 if (mLooper != NULL) {
316 mLooper->unregisterHandler(id());
317 mLooper->stop();
318 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800319 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700320}
321
Andreas Huber9575c962013-02-05 13:59:56 -0800322void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700323 if (mLooper == NULL) {
324 mLooper = new ALooper;
325 mLooper->setName("generic");
326 mLooper->start();
327
328 mLooper->registerHandler(this);
329 }
330
331 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());
332 msg->post();
333}
334
335void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700336 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700337 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800338 // set to false first, if the extractor
339 // comes back as secure, set it to true then.
340 mIsSecure = false;
341
Chong Zhangd354d8d2014-08-20 13:09:58 -0700342 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700343 const char* uri = mUri.c_str();
344 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
345
346 if (!strncasecmp("http://", uri, 7)
347 || !strncasecmp("https://", uri, 8)
348 || mIsWidevine) {
349 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
350 if (mHttpSource == NULL) {
351 ALOGE("Failed to create http source!");
352 notifyPreparedAndCleanup(UNKNOWN_ERROR);
353 return;
354 }
355 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700356
Chong Zhangd354d8d2014-08-20 13:09:58 -0700357 mDataSource = DataSource::CreateFromURI(
Robert Shih360d6d02014-09-29 14:42:35 -0700358 mHTTPService, uri, &mUriHeaders, &mContentType,
359 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700360 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700361 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700362
Chong Zhangd354d8d2014-08-20 13:09:58 -0700363 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800364 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700365 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700366
Chong Zhangd354d8d2014-08-20 13:09:58 -0700367 if (mDataSource == NULL) {
368 ALOGE("Failed to create data source!");
369 notifyPreparedAndCleanup(UNKNOWN_ERROR);
370 return;
371 }
372
373 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
374 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
375 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700376
Chong Zhangefbb6192015-01-30 17:13:27 -0800377 // For widevine or other cached streaming cases, we need to wait for
378 // enough buffering before reporting prepared.
379 // Note that even when URL doesn't start with widevine://, mIsWidevine
380 // could still be set to true later, if the streaming or file source
381 // is sniffed to be widevine. We don't want to buffer for file source
382 // in that case, so must check the flag now.
383 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700384 }
385
Chong Zhangd354d8d2014-08-20 13:09:58 -0700386 // check initial caching status
387 status_t err = prefillCacheIfNecessary();
388 if (err != OK) {
389 if (err == -EAGAIN) {
390 (new AMessage(kWhatPrepareAsync, id()))->post(200000);
391 } else {
392 ALOGE("Failed to prefill data cache!");
393 notifyPreparedAndCleanup(UNKNOWN_ERROR);
394 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700395 return;
396 }
397
Chong Zhangd354d8d2014-08-20 13:09:58 -0700398 // init extrator from data source
399 err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700400
401 if (err != OK) {
402 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700403 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700404 return;
405 }
406
Andreas Huber9575c962013-02-05 13:59:56 -0800407 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700408 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
409 sp<AMessage> msg = new AMessage;
410 err = convertMetaDataToMessage(meta, &msg);
411 if(err != OK) {
412 notifyPreparedAndCleanup(err);
413 return;
414 }
415 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800416 }
417
418 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800419 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800420 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700421 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800422 | FLAG_CAN_SEEK_BACKWARD
423 | FLAG_CAN_SEEK_FORWARD
424 | FLAG_CAN_SEEK);
425
Chong Zhangefbb6192015-01-30 17:13:27 -0800426 if (mIsStreaming) {
427 mPrepareBuffering = true;
428
429 ensureCacheIsFetching();
430 restartPollBuffering();
431 } else {
432 notifyPrepared();
433 }
Andreas Huber9575c962013-02-05 13:59:56 -0800434}
435
Chong Zhangd354d8d2014-08-20 13:09:58 -0700436void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
437 if (err != OK) {
438 mMetaDataSize = -1ll;
439 mContentType = "";
440 mSniffedMIME = "";
441 mDataSource.clear();
442 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700443 mHttpSource.clear();
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700444
445 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700446 }
447 notifyPrepared(err);
448}
449
450status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
451 CHECK(mDataSource != NULL);
452
453 if (mCachedSource == NULL) {
454 // no prefill if the data source is not cached
455 return OK;
456 }
457
458 // We're not doing this for streams that appear to be audio-only
459 // streams to ensure that even low bandwidth streams start
460 // playing back fairly instantly.
461 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
462 return OK;
463 }
464
465 // We're going to prefill the cache before trying to instantiate
466 // the extractor below, as the latter is an operation that otherwise
467 // could block on the datasource for a significant amount of time.
468 // During that time we'd be unable to abort the preparation phase
469 // without this prefill.
470
471 // Initially make sure we have at least 192 KB for the sniff
472 // to complete without blocking.
473 static const size_t kMinBytesForSniffing = 192 * 1024;
474 static const size_t kDefaultMetaSize = 200000;
475
476 status_t finalStatus;
477
478 size_t cachedDataRemaining =
479 mCachedSource->approxDataRemaining(&finalStatus);
480
481 if (finalStatus != OK || (mMetaDataSize >= 0
482 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
483 ALOGV("stop caching, status %d, "
484 "metaDataSize %lld, cachedDataRemaining %zu",
485 finalStatus, mMetaDataSize, cachedDataRemaining);
486 return OK;
487 }
488
489 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
490
491 if (mMetaDataSize < 0
492 && cachedDataRemaining >= kMinBytesForSniffing) {
493 String8 tmp;
494 float confidence;
495 sp<AMessage> meta;
496 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
497 return UNKNOWN_ERROR;
498 }
499
500 // We successfully identified the file's extractor to
501 // be, remember this mime type so we don't have to
502 // sniff it again when we call MediaExtractor::Create()
503 mSniffedMIME = tmp.string();
504
505 if (meta == NULL
506 || !meta->findInt64("meta-data-size",
507 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
508 mMetaDataSize = kDefaultMetaSize;
509 }
510
511 if (mMetaDataSize < 0ll) {
512 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
513 return UNKNOWN_ERROR;
514 }
515 }
516
517 return -EAGAIN;
518}
519
Andreas Huberafed0e12011-09-20 15:39:58 -0700520void NuPlayer::GenericSource::start() {
521 ALOGI("start");
522
Andy Hung2abde2c2014-09-30 14:40:32 -0700523 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700524 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700525 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700526 }
527
528 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700529 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700530 }
Ronghua Wu80276872014-08-28 15:50:29 -0700531
532 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
533 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800534
535 (new AMessage(kWhatStart, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700536}
537
538void NuPlayer::GenericSource::stop() {
539 // nothing to do, just account for DRM playback status
540 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
541 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800542 if (mIsWidevine || mIsSecure) {
543 // For widevine or secure sources we need to prevent any further reads.
Andy Hung2abde2c2014-09-30 14:40:32 -0700544 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
545 sp<AMessage> response;
546 (void) msg->postAndAwaitResponse(&response);
547 }
Ronghua Wu80276872014-08-28 15:50:29 -0700548}
549
550void NuPlayer::GenericSource::pause() {
551 // nothing to do, just account for DRM playback status
552 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
553 mStarted = false;
554}
555
556void NuPlayer::GenericSource::resume() {
557 // nothing to do, just account for DRM playback status
558 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
559 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800560
561 (new AMessage(kWhatResume, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700562}
563
Chong Zhang48296b72014-09-14 14:28:45 -0700564void NuPlayer::GenericSource::disconnect() {
565 if (mDataSource != NULL) {
566 // disconnect data source
567 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
568 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
569 }
Robert Shih360d6d02014-09-29 14:42:35 -0700570 } else if (mHttpSource != NULL) {
571 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700572 }
573}
574
Ronghua Wu80276872014-08-28 15:50:29 -0700575void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
576 if (mDecryptHandle != NULL) {
577 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
578 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700579 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
580 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700581}
582
583status_t NuPlayer::GenericSource::feedMoreTSData() {
584 return OK;
585}
586
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700587void NuPlayer::GenericSource::schedulePollBuffering() {
588 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
589 msg->setInt32("generation", mPollBufferingGeneration);
590 msg->post(1000000ll);
591}
592
593void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800594 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700595 ++mPollBufferingGeneration;
596}
597
Chong Zhangefbb6192015-01-30 17:13:27 -0800598void NuPlayer::GenericSource::restartPollBuffering() {
599 if (mIsStreaming) {
600 cancelPollBuffering();
601 onPollBuffering();
602 }
603}
604
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700605void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800606 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
607
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700608 sp<AMessage> msg = dupNotify();
609 msg->setInt32("what", kWhatBufferingUpdate);
610 msg->setInt32("percentage", percentage);
611 msg->post();
612}
613
Chong Zhangefbb6192015-01-30 17:13:27 -0800614void NuPlayer::GenericSource::startBufferingIfNecessary() {
615 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
616 mPrepareBuffering, mBuffering);
617
618 if (mPrepareBuffering) {
619 return;
620 }
621
622 if (!mBuffering) {
623 mBuffering = true;
624
625 ensureCacheIsFetching();
626 sendCacheStats();
627
628 sp<AMessage> notify = dupNotify();
629 notify->setInt32("what", kWhatPauseOnBufferingStart);
630 notify->post();
631 }
632}
633
634void NuPlayer::GenericSource::stopBufferingIfNecessary() {
635 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
636 mPrepareBuffering, mBuffering);
637
638 if (mPrepareBuffering) {
639 mPrepareBuffering = false;
640 notifyPrepared();
641 return;
642 }
643
644 if (mBuffering) {
645 mBuffering = false;
646
647 sendCacheStats();
648
649 sp<AMessage> notify = dupNotify();
650 notify->setInt32("what", kWhatResumeOnBufferingEnd);
651 notify->post();
652 }
653}
654
655void NuPlayer::GenericSource::sendCacheStats() {
656 int32_t kbps = 0;
657 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700658
659 if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800660 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
661 } else if (mWVMExtractor != NULL) {
662 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
663 }
664
665 if (err == OK) {
666 sp<AMessage> notify = dupNotify();
667 notify->setInt32("what", kWhatCacheStats);
668 notify->setInt32("bandwidth", kbps);
669 notify->post();
670 }
671}
672
673void NuPlayer::GenericSource::ensureCacheIsFetching() {
674 if (mCachedSource != NULL) {
675 mCachedSource->resumeFetchingIfNecessary();
676 }
677}
678
679void NuPlayer::GenericSource::onPollBuffering() {
680 status_t finalStatus = UNKNOWN_ERROR;
681 int64_t cachedDurationUs = -1ll;
682 ssize_t cachedDataRemaining = -1;
683
684 if (mCachedSource != NULL) {
685 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700686 mCachedSource->approxDataRemaining(&finalStatus);
687
688 if (finalStatus == OK) {
689 off64_t size;
690 int64_t bitrate = 0ll;
691 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
692 bitrate = size * 8000000ll / mDurationUs;
693 } else if (mBitrate > 0) {
694 bitrate = mBitrate;
695 }
696 if (bitrate > 0) {
697 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
698 }
699 }
700 } else if (mWVMExtractor != NULL) {
701 cachedDurationUs
702 = mWVMExtractor->getCachedDurationUs(&finalStatus);
703 }
704
Chong Zhangefbb6192015-01-30 17:13:27 -0800705 if (finalStatus != OK) {
706 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
707
708 if (finalStatus == ERROR_END_OF_STREAM) {
709 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700710 }
711
Chong Zhangefbb6192015-01-30 17:13:27 -0800712 stopBufferingIfNecessary();
713 return;
714 } else if (cachedDurationUs >= 0ll) {
715 if (mDurationUs > 0ll) {
716 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
717 int percentage = 100.0 * cachedPosUs / mDurationUs;
718 if (percentage > 100) {
719 percentage = 100;
720 }
721
722 notifyBufferingUpdate(percentage);
723 }
724
725 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
726 cachedDurationUs / 1000000.0f);
727
728 if (cachedDurationUs < kLowWaterMarkUs) {
729 startBufferingIfNecessary();
730 } else if (cachedDurationUs > kHighWaterMarkUs) {
731 stopBufferingIfNecessary();
732 }
733 } else if (cachedDataRemaining >= 0) {
734 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
735 cachedDataRemaining);
736
737 if (cachedDataRemaining < kLowWaterMarkBytes) {
738 startBufferingIfNecessary();
739 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
740 stopBufferingIfNecessary();
741 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700742 }
743
744 schedulePollBuffering();
745}
746
Robert Shih3423bbd2014-07-16 15:47:09 -0700747void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
748 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700749 case kWhatPrepareAsync:
750 {
751 onPrepareAsync();
752 break;
753 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700754 case kWhatFetchSubtitleData:
755 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700756 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
757 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
758 break;
759 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700760
Lajos Molnare26940f2014-07-31 10:31:26 -0700761 case kWhatFetchTimedTextData:
762 {
763 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
764 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700765 break;
766 }
767
768 case kWhatSendSubtitleData:
769 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700770 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
771 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
772 break;
773 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700774
Lajos Molnare26940f2014-07-31 10:31:26 -0700775 case kWhatSendTimedTextData:
776 {
777 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
778 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700779 break;
780 }
781
782 case kWhatChangeAVSource:
783 {
784 int32_t trackIndex;
785 CHECK(msg->findInt32("trackIndex", &trackIndex));
786 const sp<MediaSource> source = mSources.itemAt(trackIndex);
787
788 Track* track;
789 const char *mime;
790 media_track_type trackType, counterpartType;
791 sp<MetaData> meta = source->getFormat();
792 meta->findCString(kKeyMIMEType, &mime);
793 if (!strncasecmp(mime, "audio/", 6)) {
794 track = &mAudioTrack;
795 trackType = MEDIA_TRACK_TYPE_AUDIO;
796 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
797 } else {
798 CHECK(!strncasecmp(mime, "video/", 6));
799 track = &mVideoTrack;
800 trackType = MEDIA_TRACK_TYPE_VIDEO;
801 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
802 }
803
804
805 if (track->mSource != NULL) {
806 track->mSource->stop();
807 }
808 track->mSource = source;
809 track->mSource->start();
810 track->mIndex = trackIndex;
811
Robert Shih3423bbd2014-07-16 15:47:09 -0700812 int64_t timeUs, actualTimeUs;
813 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800814 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
815 timeUs = mAudioLastDequeueTimeUs;
816 } else {
817 timeUs = mVideoLastDequeueTimeUs;
818 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700819 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
820 readBuffer(counterpartType, -1, NULL, formatChange);
821 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
822
823 break;
824 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800825
826 case kWhatStart:
827 case kWhatResume:
828 {
829 restartPollBuffering();
830 break;
831 }
832
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700833 case kWhatPollBuffering:
834 {
835 int32_t generation;
836 CHECK(msg->findInt32("generation", &generation));
837 if (generation == mPollBufferingGeneration) {
838 onPollBuffering();
839 }
840 break;
841 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700842
843 case kWhatGetFormat:
844 {
845 onGetFormatMeta(msg);
846 break;
847 }
848
849 case kWhatGetSelectedTrack:
850 {
851 onGetSelectedTrack(msg);
852 break;
853 }
854
855 case kWhatSelectTrack:
856 {
857 onSelectTrack(msg);
858 break;
859 }
860
861 case kWhatSeek:
862 {
863 onSeek(msg);
864 break;
865 }
866
867 case kWhatReadBuffer:
868 {
869 onReadBuffer(msg);
870 break;
871 }
872
Andy Hung2abde2c2014-09-30 14:40:32 -0700873 case kWhatStopWidevine:
874 {
875 // mStopRead is only used for Widevine to prevent the video source
876 // from being read while the associated video decoder is shutting down.
877 mStopRead = true;
878 if (mVideoTrack.mSource != NULL) {
879 mVideoTrack.mPackets->clear();
880 }
881 sp<AMessage> response = new AMessage;
882 uint32_t replyID;
883 CHECK(msg->senderAwaitsResponse(&replyID));
884 response->postReply(replyID);
885 break;
886 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700887 default:
888 Source::onMessageReceived(msg);
889 break;
890 }
891}
892
Lajos Molnare26940f2014-07-31 10:31:26 -0700893void NuPlayer::GenericSource::fetchTextData(
894 uint32_t sendWhat,
895 media_track_type type,
896 int32_t curGen,
897 sp<AnotherPacketSource> packets,
898 sp<AMessage> msg) {
899 int32_t msgGeneration;
900 CHECK(msg->findInt32("generation", &msgGeneration));
901 if (msgGeneration != curGen) {
902 // stale
903 return;
904 }
905
906 int32_t avail;
907 if (packets->hasBufferAvailable(&avail)) {
908 return;
909 }
910
911 int64_t timeUs;
912 CHECK(msg->findInt64("timeUs", &timeUs));
913
914 int64_t subTimeUs;
915 readBuffer(type, timeUs, &subTimeUs);
916
917 int64_t delayUs = subTimeUs - timeUs;
918 if (msg->what() == kWhatFetchSubtitleData) {
919 const int64_t oneSecUs = 1000000ll;
920 delayUs -= oneSecUs;
921 }
922 sp<AMessage> msg2 = new AMessage(sendWhat, id());
923 msg2->setInt32("generation", msgGeneration);
924 msg2->post(delayUs < 0 ? 0 : delayUs);
925}
926
927void NuPlayer::GenericSource::sendTextData(
928 uint32_t what,
929 media_track_type type,
930 int32_t curGen,
931 sp<AnotherPacketSource> packets,
932 sp<AMessage> msg) {
933 int32_t msgGeneration;
934 CHECK(msg->findInt32("generation", &msgGeneration));
935 if (msgGeneration != curGen) {
936 // stale
937 return;
938 }
939
940 int64_t subTimeUs;
941 if (packets->nextBufferTime(&subTimeUs) != OK) {
942 return;
943 }
944
945 int64_t nextSubTimeUs;
946 readBuffer(type, -1, &nextSubTimeUs);
947
948 sp<ABuffer> buffer;
949 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
950 if (dequeueStatus == OK) {
951 sp<AMessage> notify = dupNotify();
952 notify->setInt32("what", what);
953 notify->setBuffer("buffer", buffer);
954 notify->post();
955
956 const int64_t delayUs = nextSubTimeUs - subTimeUs;
957 msg->post(delayUs < 0 ? 0 : delayUs);
958 }
959}
960
Andreas Huber84066782011-08-16 09:34:26 -0700961sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700962 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
963 msg->setInt32("audio", audio);
964
965 sp<AMessage> response;
966 void *format;
967 status_t err = msg->postAndAwaitResponse(&response);
968 if (err == OK && response != NULL) {
969 CHECK(response->findPointer("format", &format));
970 return (MetaData *)format;
971 } else {
972 return NULL;
973 }
974}
975
976void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
977 int32_t audio;
978 CHECK(msg->findInt32("audio", &audio));
979
980 sp<AMessage> response = new AMessage;
981 sp<MetaData> format = doGetFormatMeta(audio);
982 response->setPointer("format", format.get());
983
984 uint32_t replyID;
985 CHECK(msg->senderAwaitsResponse(&replyID));
986 response->postReply(replyID);
987}
988
989sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700990 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
991
992 if (source == NULL) {
993 return NULL;
994 }
995
996 return source->getFormat();
997}
998
999status_t NuPlayer::GenericSource::dequeueAccessUnit(
1000 bool audio, sp<ABuffer> *accessUnit) {
1001 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1002
1003 if (track->mSource == NULL) {
1004 return -EWOULDBLOCK;
1005 }
1006
Lajos Molnarcc227032014-07-17 15:33:06 -07001007 if (mIsWidevine && !audio) {
1008 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001009 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001010 }
1011
Andreas Huberafed0e12011-09-20 15:39:58 -07001012 status_t finalResult;
1013 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001014 if (finalResult == OK) {
1015 postReadBuffer(
1016 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1017 return -EWOULDBLOCK;
1018 }
1019 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001020 }
1021
1022 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1023
Robert Shih3423bbd2014-07-16 15:47:09 -07001024 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001025 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001026 }
1027
Robert Shih3423bbd2014-07-16 15:47:09 -07001028 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001029 if (mSubtitleTrack.mSource != NULL) {
1030 mSubtitleTrack.mPackets->clear();
1031 mFetchSubtitleDataGeneration++;
1032 }
1033 if (mTimedTextTrack.mSource != NULL) {
1034 mTimedTextTrack.mPackets->clear();
1035 mFetchTimedTextDataGeneration++;
1036 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001037 return result;
1038 }
1039
1040 int64_t timeUs;
1041 status_t eosResult; // ignored
1042 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001043 if (audio) {
1044 mAudioLastDequeueTimeUs = timeUs;
1045 } else {
1046 mVideoLastDequeueTimeUs = timeUs;
1047 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001048
1049 if (mSubtitleTrack.mSource != NULL
1050 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001051 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1052 msg->setInt64("timeUs", timeUs);
1053 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1054 msg->post();
1055 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001056
Lajos Molnare26940f2014-07-31 10:31:26 -07001057 if (mTimedTextTrack.mSource != NULL
1058 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1059 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1060 msg->setInt64("timeUs", timeUs);
1061 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1062 msg->post();
1063 }
1064
Andreas Huberafed0e12011-09-20 15:39:58 -07001065 return result;
1066}
1067
1068status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1069 *durationUs = mDurationUs;
1070 return OK;
1071}
1072
Robert Shihdd235722014-06-12 14:49:23 -07001073size_t NuPlayer::GenericSource::getTrackCount() const {
1074 return mSources.size();
1075}
1076
1077sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1078 size_t trackCount = mSources.size();
1079 if (trackIndex >= trackCount) {
1080 return NULL;
1081 }
1082
1083 sp<AMessage> format = new AMessage();
1084 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1085
1086 const char *mime;
1087 CHECK(meta->findCString(kKeyMIMEType, &mime));
1088
1089 int32_t trackType;
1090 if (!strncasecmp(mime, "video/", 6)) {
1091 trackType = MEDIA_TRACK_TYPE_VIDEO;
1092 } else if (!strncasecmp(mime, "audio/", 6)) {
1093 trackType = MEDIA_TRACK_TYPE_AUDIO;
1094 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1095 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1096 } else {
1097 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1098 }
1099 format->setInt32("type", trackType);
1100
1101 const char *lang;
1102 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1103 lang = "und";
1104 }
1105 format->setString("language", lang);
1106
1107 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1108 format->setString("mime", mime);
1109
1110 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1111 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1112 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1113 meta->findInt32(kKeyTrackIsForced, &isForced);
1114
1115 format->setInt32("auto", !!isAutoselect);
1116 format->setInt32("default", !!isDefault);
1117 format->setInt32("forced", !!isForced);
1118 }
1119
1120 return format;
1121}
1122
Lajos Molnare26940f2014-07-31 10:31:26 -07001123ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -07001124 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1125 msg->setInt32("type", type);
1126
1127 sp<AMessage> response;
1128 int32_t index;
1129 status_t err = msg->postAndAwaitResponse(&response);
1130 if (err == OK && response != NULL) {
1131 CHECK(response->findInt32("index", &index));
1132 return index;
1133 } else {
1134 return -1;
1135 }
1136}
1137
1138void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1139 int32_t tmpType;
1140 CHECK(msg->findInt32("type", &tmpType));
1141 media_track_type type = (media_track_type)tmpType;
1142
1143 sp<AMessage> response = new AMessage;
1144 ssize_t index = doGetSelectedTrack(type);
1145 response->setInt32("index", index);
1146
1147 uint32_t replyID;
1148 CHECK(msg->senderAwaitsResponse(&replyID));
1149 response->postReply(replyID);
1150}
1151
1152ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001153 const Track *track = NULL;
1154 switch (type) {
1155 case MEDIA_TRACK_TYPE_VIDEO:
1156 track = &mVideoTrack;
1157 break;
1158 case MEDIA_TRACK_TYPE_AUDIO:
1159 track = &mAudioTrack;
1160 break;
1161 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1162 track = &mTimedTextTrack;
1163 break;
1164 case MEDIA_TRACK_TYPE_SUBTITLE:
1165 track = &mSubtitleTrack;
1166 break;
1167 default:
1168 break;
1169 }
1170
1171 if (track != NULL && track->mSource != NULL) {
1172 return track->mIndex;
1173 }
1174
1175 return -1;
1176}
1177
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001178status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001179 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001180 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1181 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001182 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001183 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001184
1185 sp<AMessage> response;
1186 status_t err = msg->postAndAwaitResponse(&response);
1187 if (err == OK && response != NULL) {
1188 CHECK(response->findInt32("err", &err));
1189 }
1190
1191 return err;
1192}
1193
1194void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1195 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001196 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001197 CHECK(msg->findInt32("trackIndex", &trackIndex));
1198 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001199 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001200
1201 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001202 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001203 response->setInt32("err", err);
1204
1205 uint32_t replyID;
1206 CHECK(msg->senderAwaitsResponse(&replyID));
1207 response->postReply(replyID);
1208}
1209
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001210status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001211 if (trackIndex >= mSources.size()) {
1212 return BAD_INDEX;
1213 }
1214
1215 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001216 Track* track = NULL;
1217 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1218 track = &mSubtitleTrack;
1219 mFetchSubtitleDataGeneration++;
1220 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1221 track = &mTimedTextTrack;
1222 mFetchTimedTextDataGeneration++;
1223 }
1224 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001225 return INVALID_OPERATION;
1226 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001227 track->mSource->stop();
1228 track->mSource = NULL;
1229 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001230 return OK;
1231 }
1232
1233 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1234 sp<MetaData> meta = source->getFormat();
1235 const char *mime;
1236 CHECK(meta->findCString(kKeyMIMEType, &mime));
1237 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001238 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1239 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1240 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001241 return OK;
1242 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001243 track->mIndex = trackIndex;
1244 if (track->mSource != NULL) {
1245 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001246 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001247 track->mSource = mSources.itemAt(trackIndex);
1248 track->mSource->start();
1249 if (track->mPackets == NULL) {
1250 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001251 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001252 track->mPackets->clear();
1253 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001254
1255 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001256
1257 if (isSubtitle) {
1258 mFetchSubtitleDataGeneration++;
1259 } else {
1260 mFetchTimedTextDataGeneration++;
1261 }
1262
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001263 status_t eosResult; // ignored
1264 if (mSubtitleTrack.mSource != NULL
1265 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1266 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1267 msg->setInt64("timeUs", timeUs);
1268 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1269 msg->post();
1270 }
1271
1272 if (mTimedTextTrack.mSource != NULL
1273 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1274 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1275 msg->setInt64("timeUs", timeUs);
1276 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1277 msg->post();
1278 }
1279
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 return OK;
1281 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1282 bool audio = !strncasecmp(mime, "audio/", 6);
1283 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1284 if (track->mSource != NULL && track->mIndex == trackIndex) {
1285 return OK;
1286 }
1287
1288 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1289 msg->setInt32("trackIndex", trackIndex);
1290 msg->post();
1291 return OK;
1292 }
1293
1294 return INVALID_OPERATION;
1295}
1296
Andreas Huberafed0e12011-09-20 15:39:58 -07001297status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001298 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1299 msg->setInt64("seekTimeUs", seekTimeUs);
1300
1301 sp<AMessage> response;
1302 status_t err = msg->postAndAwaitResponse(&response);
1303 if (err == OK && response != NULL) {
1304 CHECK(response->findInt32("err", &err));
1305 }
1306
1307 return err;
1308}
1309
1310void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1311 int64_t seekTimeUs;
1312 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1313
1314 sp<AMessage> response = new AMessage;
1315 status_t err = doSeek(seekTimeUs);
1316 response->setInt32("err", err);
1317
1318 uint32_t replyID;
1319 CHECK(msg->senderAwaitsResponse(&replyID));
1320 response->postReply(replyID);
1321}
1322
1323status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001324 // If the Widevine source is stopped, do not attempt to read any
1325 // more buffers.
1326 if (mStopRead) {
1327 return INVALID_OPERATION;
1328 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001329 if (mVideoTrack.mSource != NULL) {
1330 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001331 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001332
1333 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001334 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001335 }
1336
1337 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001338 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001339 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001340 }
1341
Ronghua Wu80276872014-08-28 15:50:29 -07001342 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1343 if (!mStarted) {
1344 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1345 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001346
1347 // If currently buffering, post kWhatBufferingEnd first, so that
1348 // NuPlayer resumes. Otherwise, if cache hits high watermark
1349 // before new polling happens, no one will resume the playback.
1350 stopBufferingIfNecessary();
1351 restartPollBuffering();
1352
Andreas Huberafed0e12011-09-20 15:39:58 -07001353 return OK;
1354}
1355
Robert Shih3423bbd2014-07-16 15:47:09 -07001356sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1357 MediaBuffer* mb,
1358 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001359 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001360 int64_t *actualTimeUs) {
1361 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1362 size_t outLength = mb->range_length();
1363
1364 if (audio && mAudioIsVorbis) {
1365 outLength += sizeof(int32_t);
1366 }
1367
1368 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001369 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001370 // data is already provided in the buffer
1371 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001372 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001373 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001374 } else {
1375 ab = new ABuffer(outLength);
1376 memcpy(ab->data(),
1377 (const uint8_t *)mb->data() + mb->range_offset(),
1378 mb->range_length());
1379 }
1380
1381 if (audio && mAudioIsVorbis) {
1382 int32_t numPageSamples;
1383 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1384 numPageSamples = -1;
1385 }
1386
1387 uint8_t* abEnd = ab->data() + mb->range_length();
1388 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1389 }
1390
Lajos Molnare26940f2014-07-31 10:31:26 -07001391 sp<AMessage> meta = ab->meta();
1392
Robert Shih3423bbd2014-07-16 15:47:09 -07001393 int64_t timeUs;
1394 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001395 meta->setInt64("timeUs", timeUs);
1396
Wei Jia474d7c72014-12-04 15:12:13 -08001397#if 0
1398 // Temporarily disable pre-roll till we have a full solution to handle
1399 // both single seek and continous seek gracefully.
1400 if (seekTimeUs > timeUs) {
1401 sp<AMessage> extra = new AMessage;
1402 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1403 meta->setMessage("extra", extra);
1404 }
1405#endif
1406
Lajos Molnare26940f2014-07-31 10:31:26 -07001407 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1408 const char *mime;
1409 CHECK(mTimedTextTrack.mSource != NULL
1410 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1411 meta->setString("mime", mime);
1412 }
1413
Robert Shih3423bbd2014-07-16 15:47:09 -07001414 int64_t durationUs;
1415 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1416 meta->setInt64("durationUs", durationUs);
1417 }
1418
1419 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1420 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1421 }
1422
1423 if (actualTimeUs) {
1424 *actualTimeUs = timeUs;
1425 }
1426
1427 mb->release();
1428 mb = NULL;
1429
1430 return ab;
1431}
1432
Robert Shih17f6dd62014-08-20 17:00:21 -07001433void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001434 Mutex::Autolock _l(mReadBufferLock);
1435
1436 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1437 mPendingReadBufferTypes |= (1 << trackType);
1438 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1439 msg->setInt32("trackType", trackType);
1440 msg->post();
1441 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001442}
1443
1444void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1445 int32_t tmpType;
1446 CHECK(msg->findInt32("trackType", &tmpType));
1447 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001448 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001449 {
1450 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001451 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001452 Mutex::Autolock _l(mReadBufferLock);
1453 mPendingReadBufferTypes &= ~(1 << trackType);
1454 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001455}
1456
Andreas Huberafed0e12011-09-20 15:39:58 -07001457void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001458 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001459 // Do not read data if Widevine source is stopped
1460 if (mStopRead) {
1461 return;
1462 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001463 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001464 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001465 switch (trackType) {
1466 case MEDIA_TRACK_TYPE_VIDEO:
1467 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001468 if (mIsWidevine) {
1469 maxBuffers = 2;
1470 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001471 break;
1472 case MEDIA_TRACK_TYPE_AUDIO:
1473 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001474 if (mIsWidevine) {
1475 maxBuffers = 8;
1476 } else {
1477 maxBuffers = 64;
1478 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001479 break;
1480 case MEDIA_TRACK_TYPE_SUBTITLE:
1481 track = &mSubtitleTrack;
1482 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001483 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1484 track = &mTimedTextTrack;
1485 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001486 default:
1487 TRESPASS();
1488 }
1489
1490 if (track->mSource == NULL) {
1491 return;
1492 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001493
1494 if (actualTimeUs) {
1495 *actualTimeUs = seekTimeUs;
1496 }
1497
1498 MediaSource::ReadOptions options;
1499
1500 bool seeking = false;
1501
1502 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001503 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001504 seeking = true;
1505 }
1506
Chong Zhang42e81532014-12-01 13:44:26 -08001507 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001508 options.setNonBlocking();
1509 }
1510
Phil Burkc5cc2e22014-09-09 20:08:39 -07001511 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001512 MediaBuffer *mbuf;
1513 status_t err = track->mSource->read(&mbuf, &options);
1514
1515 options.clearSeekTo();
1516
1517 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001518 int64_t timeUs;
1519 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1520 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1521 mAudioTimeUs = timeUs;
1522 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1523 mVideoTimeUs = timeUs;
1524 }
1525
Robert Shih3423bbd2014-07-16 15:47:09 -07001526 // formatChange && seeking: track whose source is changed during selection
1527 // formatChange && !seeking: track whose source is not changed during selection
1528 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001529 if ((seeking || formatChange)
1530 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1531 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001532 ATSParser::DiscontinuityType type = (formatChange && seeking)
1533 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1534 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001535 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001536 }
1537
Wei Jia474d7c72014-12-04 15:12:13 -08001538 sp<ABuffer> buffer = mediaBufferToABuffer(
1539 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001540 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001541 formatChange = false;
1542 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001543 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001544 } else if (err == WOULD_BLOCK) {
1545 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001546 } else if (err == INFO_FORMAT_CHANGED) {
1547#if 0
1548 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001549 ATSParser::DISCONTINUITY_FORMATCHANGE,
1550 NULL,
1551 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001552#endif
1553 } else {
1554 track->mPackets->signalEOS(err);
1555 break;
1556 }
1557 }
1558}
1559
Andreas Huberafed0e12011-09-20 15:39:58 -07001560} // namespace android