blob: 1b2fc5ea84f6fedbb8d8a464f1da75aa678a9eb0 [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
Lajos Molnar68fca632015-03-31 10:06:48 -0700265 mBitrate = totalBitrate;
266
267 return OK;
268}
269
270status_t NuPlayer::GenericSource::startSources() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800271 // Start the selected A/V tracks now before we start buffering.
272 // Widevine sources might re-initialize crypto when starting, if we delay
273 // this to start(), all data buffered during prepare would be wasted.
274 // (We don't actually start reading until start().)
275 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
276 ALOGE("failed to start audio track!");
277 return UNKNOWN_ERROR;
278 }
279
280 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
281 ALOGE("failed to start video track!");
282 return UNKNOWN_ERROR;
283 }
284
Chong Zhang3de157d2014-08-05 20:54:44 -0700285 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700286}
287
Ronghua Wu80276872014-08-28 15:50:29 -0700288void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
289 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
290 if (mDecryptHandle != NULL) {
291 CHECK(mDrmManagerClient);
292 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
293 sp<AMessage> msg = dupNotify();
294 msg->setInt32("what", kWhatDrmNoLicense);
295 msg->post();
296 }
297 }
298}
299
300int64_t NuPlayer::GenericSource::getLastReadPosition() {
301 if (mAudioTrack.mSource != NULL) {
302 return mAudioTimeUs;
303 } else if (mVideoTrack.mSource != NULL) {
304 return mVideoTimeUs;
305 } else {
306 return 0;
307 }
308}
309
Chong Zhanga19f33e2014-08-07 15:35:07 -0700310status_t NuPlayer::GenericSource::setBuffers(
311 bool audio, Vector<MediaBuffer *> &buffers) {
Chong Zhang42e81532014-12-01 13:44:26 -0800312 if (mIsSecure && !audio) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700313 return mVideoTrack.mSource->setBuffers(buffers);
314 }
315 return INVALID_OPERATION;
316}
317
Andreas Huberafed0e12011-09-20 15:39:58 -0700318NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700319 if (mLooper != NULL) {
320 mLooper->unregisterHandler(id());
321 mLooper->stop();
322 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800323 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700324}
325
Andreas Huber9575c962013-02-05 13:59:56 -0800326void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700327 if (mLooper == NULL) {
328 mLooper = new ALooper;
329 mLooper->setName("generic");
330 mLooper->start();
331
332 mLooper->registerHandler(this);
333 }
334
335 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id());
336 msg->post();
337}
338
339void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700340 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700341 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800342 // set to false first, if the extractor
343 // comes back as secure, set it to true then.
344 mIsSecure = false;
345
Chong Zhangd354d8d2014-08-20 13:09:58 -0700346 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700347 const char* uri = mUri.c_str();
348 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
349
350 if (!strncasecmp("http://", uri, 7)
351 || !strncasecmp("https://", uri, 8)
352 || mIsWidevine) {
353 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
354 if (mHttpSource == NULL) {
355 ALOGE("Failed to create http source!");
356 notifyPreparedAndCleanup(UNKNOWN_ERROR);
357 return;
358 }
359 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700360
Chong Zhangd354d8d2014-08-20 13:09:58 -0700361 mDataSource = DataSource::CreateFromURI(
Robert Shih360d6d02014-09-29 14:42:35 -0700362 mHTTPService, uri, &mUriHeaders, &mContentType,
363 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700364 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700365 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700366
Chong Zhangd354d8d2014-08-20 13:09:58 -0700367 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800368 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700369 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700370
Chong Zhangd354d8d2014-08-20 13:09:58 -0700371 if (mDataSource == NULL) {
372 ALOGE("Failed to create data source!");
373 notifyPreparedAndCleanup(UNKNOWN_ERROR);
374 return;
375 }
376
377 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
378 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
379 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700380
Chong Zhangefbb6192015-01-30 17:13:27 -0800381 // For widevine or other cached streaming cases, we need to wait for
382 // enough buffering before reporting prepared.
383 // Note that even when URL doesn't start with widevine://, mIsWidevine
384 // could still be set to true later, if the streaming or file source
385 // is sniffed to be widevine. We don't want to buffer for file source
386 // in that case, so must check the flag now.
387 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700388 }
389
Chong Zhangd354d8d2014-08-20 13:09:58 -0700390 // check initial caching status
391 status_t err = prefillCacheIfNecessary();
392 if (err != OK) {
393 if (err == -EAGAIN) {
394 (new AMessage(kWhatPrepareAsync, id()))->post(200000);
395 } else {
396 ALOGE("Failed to prefill data cache!");
397 notifyPreparedAndCleanup(UNKNOWN_ERROR);
398 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700399 return;
400 }
401
Chong Zhangd354d8d2014-08-20 13:09:58 -0700402 // init extrator from data source
403 err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700404
405 if (err != OK) {
406 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700407 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700408 return;
409 }
410
Andreas Huber9575c962013-02-05 13:59:56 -0800411 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700412 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
413 sp<AMessage> msg = new AMessage;
414 err = convertMetaDataToMessage(meta, &msg);
415 if(err != OK) {
416 notifyPreparedAndCleanup(err);
417 return;
418 }
419 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800420 }
421
422 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800423 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800424 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700425 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800426 | FLAG_CAN_SEEK_BACKWARD
427 | FLAG_CAN_SEEK_FORWARD
428 | FLAG_CAN_SEEK);
429
Lajos Molnar68fca632015-03-31 10:06:48 -0700430 if (mIsSecure) {
431 // secure decoders must be instantiated before starting widevine source
432 sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, id());
433 notifyInstantiateSecureDecoders(reply);
434 } else {
435 finishPrepareAsync();
436 }
437}
438
439void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) {
440 if (err != OK) {
441 ALOGE("Failed to instantiate secure decoders!");
442 notifyPreparedAndCleanup(err);
443 return;
444 }
445 finishPrepareAsync();
446}
447
448void NuPlayer::GenericSource::finishPrepareAsync() {
449 status_t err = startSources();
450 if (err != OK) {
451 ALOGE("Failed to init start data source!");
452 notifyPreparedAndCleanup(err);
453 return;
454 }
455
Chong Zhangefbb6192015-01-30 17:13:27 -0800456 if (mIsStreaming) {
457 mPrepareBuffering = true;
458
459 ensureCacheIsFetching();
460 restartPollBuffering();
461 } else {
462 notifyPrepared();
463 }
Andreas Huber9575c962013-02-05 13:59:56 -0800464}
465
Chong Zhangd354d8d2014-08-20 13:09:58 -0700466void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
467 if (err != OK) {
468 mMetaDataSize = -1ll;
469 mContentType = "";
470 mSniffedMIME = "";
471 mDataSource.clear();
472 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700473 mHttpSource.clear();
Lajos Molnar68fca632015-03-31 10:06:48 -0700474 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700475
476 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700477 }
478 notifyPrepared(err);
479}
480
481status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
482 CHECK(mDataSource != NULL);
483
484 if (mCachedSource == NULL) {
485 // no prefill if the data source is not cached
486 return OK;
487 }
488
489 // We're not doing this for streams that appear to be audio-only
490 // streams to ensure that even low bandwidth streams start
491 // playing back fairly instantly.
492 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
493 return OK;
494 }
495
496 // We're going to prefill the cache before trying to instantiate
497 // the extractor below, as the latter is an operation that otherwise
498 // could block on the datasource for a significant amount of time.
499 // During that time we'd be unable to abort the preparation phase
500 // without this prefill.
501
502 // Initially make sure we have at least 192 KB for the sniff
503 // to complete without blocking.
504 static const size_t kMinBytesForSniffing = 192 * 1024;
505 static const size_t kDefaultMetaSize = 200000;
506
507 status_t finalStatus;
508
509 size_t cachedDataRemaining =
510 mCachedSource->approxDataRemaining(&finalStatus);
511
512 if (finalStatus != OK || (mMetaDataSize >= 0
513 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
514 ALOGV("stop caching, status %d, "
515 "metaDataSize %lld, cachedDataRemaining %zu",
516 finalStatus, mMetaDataSize, cachedDataRemaining);
517 return OK;
518 }
519
520 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
521
522 if (mMetaDataSize < 0
523 && cachedDataRemaining >= kMinBytesForSniffing) {
524 String8 tmp;
525 float confidence;
526 sp<AMessage> meta;
527 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
528 return UNKNOWN_ERROR;
529 }
530
531 // We successfully identified the file's extractor to
532 // be, remember this mime type so we don't have to
533 // sniff it again when we call MediaExtractor::Create()
534 mSniffedMIME = tmp.string();
535
536 if (meta == NULL
537 || !meta->findInt64("meta-data-size",
538 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
539 mMetaDataSize = kDefaultMetaSize;
540 }
541
542 if (mMetaDataSize < 0ll) {
543 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
544 return UNKNOWN_ERROR;
545 }
546 }
547
548 return -EAGAIN;
549}
550
Andreas Huberafed0e12011-09-20 15:39:58 -0700551void NuPlayer::GenericSource::start() {
552 ALOGI("start");
553
Andy Hung2abde2c2014-09-30 14:40:32 -0700554 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700555 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700556 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700557 }
558
559 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700560 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700561 }
Ronghua Wu80276872014-08-28 15:50:29 -0700562
563 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
564 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800565
566 (new AMessage(kWhatStart, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700567}
568
569void NuPlayer::GenericSource::stop() {
570 // nothing to do, just account for DRM playback status
571 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
572 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800573 if (mIsWidevine || mIsSecure) {
574 // For widevine or secure sources we need to prevent any further reads.
Andy Hung2abde2c2014-09-30 14:40:32 -0700575 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
576 sp<AMessage> response;
577 (void) msg->postAndAwaitResponse(&response);
578 }
Ronghua Wu80276872014-08-28 15:50:29 -0700579}
580
581void NuPlayer::GenericSource::pause() {
582 // nothing to do, just account for DRM playback status
583 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
584 mStarted = false;
585}
586
587void NuPlayer::GenericSource::resume() {
588 // nothing to do, just account for DRM playback status
589 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
590 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800591
592 (new AMessage(kWhatResume, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700593}
594
Chong Zhang48296b72014-09-14 14:28:45 -0700595void NuPlayer::GenericSource::disconnect() {
596 if (mDataSource != NULL) {
597 // disconnect data source
598 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
599 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
600 }
Robert Shih360d6d02014-09-29 14:42:35 -0700601 } else if (mHttpSource != NULL) {
602 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700603 }
604}
605
Ronghua Wu80276872014-08-28 15:50:29 -0700606void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
607 if (mDecryptHandle != NULL) {
608 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
609 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700610 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
611 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700612}
613
614status_t NuPlayer::GenericSource::feedMoreTSData() {
615 return OK;
616}
617
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700618void NuPlayer::GenericSource::schedulePollBuffering() {
619 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
620 msg->setInt32("generation", mPollBufferingGeneration);
621 msg->post(1000000ll);
622}
623
624void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800625 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700626 ++mPollBufferingGeneration;
627}
628
Chong Zhangefbb6192015-01-30 17:13:27 -0800629void NuPlayer::GenericSource::restartPollBuffering() {
630 if (mIsStreaming) {
631 cancelPollBuffering();
632 onPollBuffering();
633 }
634}
635
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700636void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800637 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
638
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700639 sp<AMessage> msg = dupNotify();
640 msg->setInt32("what", kWhatBufferingUpdate);
641 msg->setInt32("percentage", percentage);
642 msg->post();
643}
644
Chong Zhangefbb6192015-01-30 17:13:27 -0800645void NuPlayer::GenericSource::startBufferingIfNecessary() {
646 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
647 mPrepareBuffering, mBuffering);
648
649 if (mPrepareBuffering) {
650 return;
651 }
652
653 if (!mBuffering) {
654 mBuffering = true;
655
656 ensureCacheIsFetching();
657 sendCacheStats();
658
659 sp<AMessage> notify = dupNotify();
660 notify->setInt32("what", kWhatPauseOnBufferingStart);
661 notify->post();
662 }
663}
664
665void NuPlayer::GenericSource::stopBufferingIfNecessary() {
666 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
667 mPrepareBuffering, mBuffering);
668
669 if (mPrepareBuffering) {
670 mPrepareBuffering = false;
671 notifyPrepared();
672 return;
673 }
674
675 if (mBuffering) {
676 mBuffering = false;
677
678 sendCacheStats();
679
680 sp<AMessage> notify = dupNotify();
681 notify->setInt32("what", kWhatResumeOnBufferingEnd);
682 notify->post();
683 }
684}
685
686void NuPlayer::GenericSource::sendCacheStats() {
687 int32_t kbps = 0;
688 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700689
690 if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800691 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
692 } else if (mWVMExtractor != NULL) {
693 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
694 }
695
696 if (err == OK) {
697 sp<AMessage> notify = dupNotify();
698 notify->setInt32("what", kWhatCacheStats);
699 notify->setInt32("bandwidth", kbps);
700 notify->post();
701 }
702}
703
704void NuPlayer::GenericSource::ensureCacheIsFetching() {
705 if (mCachedSource != NULL) {
706 mCachedSource->resumeFetchingIfNecessary();
707 }
708}
709
710void NuPlayer::GenericSource::onPollBuffering() {
711 status_t finalStatus = UNKNOWN_ERROR;
712 int64_t cachedDurationUs = -1ll;
713 ssize_t cachedDataRemaining = -1;
714
715 if (mCachedSource != NULL) {
716 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700717 mCachedSource->approxDataRemaining(&finalStatus);
718
719 if (finalStatus == OK) {
720 off64_t size;
721 int64_t bitrate = 0ll;
722 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
723 bitrate = size * 8000000ll / mDurationUs;
724 } else if (mBitrate > 0) {
725 bitrate = mBitrate;
726 }
727 if (bitrate > 0) {
728 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
729 }
730 }
731 } else if (mWVMExtractor != NULL) {
732 cachedDurationUs
733 = mWVMExtractor->getCachedDurationUs(&finalStatus);
734 }
735
Chong Zhangefbb6192015-01-30 17:13:27 -0800736 if (finalStatus != OK) {
737 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
738
739 if (finalStatus == ERROR_END_OF_STREAM) {
740 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700741 }
742
Chong Zhangefbb6192015-01-30 17:13:27 -0800743 stopBufferingIfNecessary();
744 return;
745 } else if (cachedDurationUs >= 0ll) {
746 if (mDurationUs > 0ll) {
747 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
748 int percentage = 100.0 * cachedPosUs / mDurationUs;
749 if (percentage > 100) {
750 percentage = 100;
751 }
752
753 notifyBufferingUpdate(percentage);
754 }
755
756 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
757 cachedDurationUs / 1000000.0f);
758
759 if (cachedDurationUs < kLowWaterMarkUs) {
760 startBufferingIfNecessary();
761 } else if (cachedDurationUs > kHighWaterMarkUs) {
762 stopBufferingIfNecessary();
763 }
764 } else if (cachedDataRemaining >= 0) {
765 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
766 cachedDataRemaining);
767
768 if (cachedDataRemaining < kLowWaterMarkBytes) {
769 startBufferingIfNecessary();
770 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
771 stopBufferingIfNecessary();
772 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700773 }
774
775 schedulePollBuffering();
776}
777
Robert Shih3423bbd2014-07-16 15:47:09 -0700778void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
779 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700780 case kWhatPrepareAsync:
781 {
782 onPrepareAsync();
783 break;
784 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700785 case kWhatFetchSubtitleData:
786 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700787 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
788 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
789 break;
790 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700791
Lajos Molnare26940f2014-07-31 10:31:26 -0700792 case kWhatFetchTimedTextData:
793 {
794 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
795 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700796 break;
797 }
798
799 case kWhatSendSubtitleData:
800 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700801 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
802 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
803 break;
804 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700805
Lajos Molnare26940f2014-07-31 10:31:26 -0700806 case kWhatSendTimedTextData:
807 {
808 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
809 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700810 break;
811 }
812
813 case kWhatChangeAVSource:
814 {
815 int32_t trackIndex;
816 CHECK(msg->findInt32("trackIndex", &trackIndex));
817 const sp<MediaSource> source = mSources.itemAt(trackIndex);
818
819 Track* track;
820 const char *mime;
821 media_track_type trackType, counterpartType;
822 sp<MetaData> meta = source->getFormat();
823 meta->findCString(kKeyMIMEType, &mime);
824 if (!strncasecmp(mime, "audio/", 6)) {
825 track = &mAudioTrack;
826 trackType = MEDIA_TRACK_TYPE_AUDIO;
827 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
828 } else {
829 CHECK(!strncasecmp(mime, "video/", 6));
830 track = &mVideoTrack;
831 trackType = MEDIA_TRACK_TYPE_VIDEO;
832 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
833 }
834
835
836 if (track->mSource != NULL) {
837 track->mSource->stop();
838 }
839 track->mSource = source;
840 track->mSource->start();
841 track->mIndex = trackIndex;
842
Robert Shih3423bbd2014-07-16 15:47:09 -0700843 int64_t timeUs, actualTimeUs;
844 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800845 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
846 timeUs = mAudioLastDequeueTimeUs;
847 } else {
848 timeUs = mVideoLastDequeueTimeUs;
849 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700850 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
851 readBuffer(counterpartType, -1, NULL, formatChange);
852 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
853
854 break;
855 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800856
857 case kWhatStart:
858 case kWhatResume:
859 {
860 restartPollBuffering();
861 break;
862 }
863
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700864 case kWhatPollBuffering:
865 {
866 int32_t generation;
867 CHECK(msg->findInt32("generation", &generation));
868 if (generation == mPollBufferingGeneration) {
869 onPollBuffering();
870 }
871 break;
872 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700873
874 case kWhatGetFormat:
875 {
876 onGetFormatMeta(msg);
877 break;
878 }
879
880 case kWhatGetSelectedTrack:
881 {
882 onGetSelectedTrack(msg);
883 break;
884 }
885
886 case kWhatSelectTrack:
887 {
888 onSelectTrack(msg);
889 break;
890 }
891
892 case kWhatSeek:
893 {
894 onSeek(msg);
895 break;
896 }
897
898 case kWhatReadBuffer:
899 {
900 onReadBuffer(msg);
901 break;
902 }
903
Lajos Molnar68fca632015-03-31 10:06:48 -0700904 case kWhatSecureDecodersInstantiated:
905 {
906 int32_t err;
907 CHECK(msg->findInt32("err", &err));
908 onSecureDecodersInstantiated(err);
909 break;
910 }
911
Andy Hung2abde2c2014-09-30 14:40:32 -0700912 case kWhatStopWidevine:
913 {
914 // mStopRead is only used for Widevine to prevent the video source
915 // from being read while the associated video decoder is shutting down.
916 mStopRead = true;
917 if (mVideoTrack.mSource != NULL) {
918 mVideoTrack.mPackets->clear();
919 }
920 sp<AMessage> response = new AMessage;
921 uint32_t replyID;
922 CHECK(msg->senderAwaitsResponse(&replyID));
923 response->postReply(replyID);
924 break;
925 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700926 default:
927 Source::onMessageReceived(msg);
928 break;
929 }
930}
931
Lajos Molnare26940f2014-07-31 10:31:26 -0700932void NuPlayer::GenericSource::fetchTextData(
933 uint32_t sendWhat,
934 media_track_type type,
935 int32_t curGen,
936 sp<AnotherPacketSource> packets,
937 sp<AMessage> msg) {
938 int32_t msgGeneration;
939 CHECK(msg->findInt32("generation", &msgGeneration));
940 if (msgGeneration != curGen) {
941 // stale
942 return;
943 }
944
945 int32_t avail;
946 if (packets->hasBufferAvailable(&avail)) {
947 return;
948 }
949
950 int64_t timeUs;
951 CHECK(msg->findInt64("timeUs", &timeUs));
952
953 int64_t subTimeUs;
954 readBuffer(type, timeUs, &subTimeUs);
955
956 int64_t delayUs = subTimeUs - timeUs;
957 if (msg->what() == kWhatFetchSubtitleData) {
958 const int64_t oneSecUs = 1000000ll;
959 delayUs -= oneSecUs;
960 }
961 sp<AMessage> msg2 = new AMessage(sendWhat, id());
962 msg2->setInt32("generation", msgGeneration);
963 msg2->post(delayUs < 0 ? 0 : delayUs);
964}
965
966void NuPlayer::GenericSource::sendTextData(
967 uint32_t what,
968 media_track_type type,
969 int32_t curGen,
970 sp<AnotherPacketSource> packets,
971 sp<AMessage> msg) {
972 int32_t msgGeneration;
973 CHECK(msg->findInt32("generation", &msgGeneration));
974 if (msgGeneration != curGen) {
975 // stale
976 return;
977 }
978
979 int64_t subTimeUs;
980 if (packets->nextBufferTime(&subTimeUs) != OK) {
981 return;
982 }
983
984 int64_t nextSubTimeUs;
985 readBuffer(type, -1, &nextSubTimeUs);
986
987 sp<ABuffer> buffer;
988 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
989 if (dequeueStatus == OK) {
990 sp<AMessage> notify = dupNotify();
991 notify->setInt32("what", what);
992 notify->setBuffer("buffer", buffer);
993 notify->post();
994
995 const int64_t delayUs = nextSubTimeUs - subTimeUs;
996 msg->post(delayUs < 0 ? 0 : delayUs);
997 }
998}
999
Andreas Huber84066782011-08-16 09:34:26 -07001000sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001001 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
1002 msg->setInt32("audio", audio);
1003
1004 sp<AMessage> response;
1005 void *format;
1006 status_t err = msg->postAndAwaitResponse(&response);
1007 if (err == OK && response != NULL) {
1008 CHECK(response->findPointer("format", &format));
1009 return (MetaData *)format;
1010 } else {
1011 return NULL;
1012 }
1013}
1014
1015void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1016 int32_t audio;
1017 CHECK(msg->findInt32("audio", &audio));
1018
1019 sp<AMessage> response = new AMessage;
1020 sp<MetaData> format = doGetFormatMeta(audio);
1021 response->setPointer("format", format.get());
1022
1023 uint32_t replyID;
1024 CHECK(msg->senderAwaitsResponse(&replyID));
1025 response->postReply(replyID);
1026}
1027
1028sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001029 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1030
1031 if (source == NULL) {
1032 return NULL;
1033 }
1034
1035 return source->getFormat();
1036}
1037
1038status_t NuPlayer::GenericSource::dequeueAccessUnit(
1039 bool audio, sp<ABuffer> *accessUnit) {
1040 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1041
1042 if (track->mSource == NULL) {
1043 return -EWOULDBLOCK;
1044 }
1045
Lajos Molnarcc227032014-07-17 15:33:06 -07001046 if (mIsWidevine && !audio) {
1047 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001048 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001049 }
1050
Andreas Huberafed0e12011-09-20 15:39:58 -07001051 status_t finalResult;
1052 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001053 if (finalResult == OK) {
1054 postReadBuffer(
1055 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1056 return -EWOULDBLOCK;
1057 }
1058 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001059 }
1060
1061 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1062
Robert Shih3423bbd2014-07-16 15:47:09 -07001063 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001064 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001065 }
1066
Robert Shih3423bbd2014-07-16 15:47:09 -07001067 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001068 if (mSubtitleTrack.mSource != NULL) {
1069 mSubtitleTrack.mPackets->clear();
1070 mFetchSubtitleDataGeneration++;
1071 }
1072 if (mTimedTextTrack.mSource != NULL) {
1073 mTimedTextTrack.mPackets->clear();
1074 mFetchTimedTextDataGeneration++;
1075 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001076 return result;
1077 }
1078
1079 int64_t timeUs;
1080 status_t eosResult; // ignored
1081 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001082 if (audio) {
1083 mAudioLastDequeueTimeUs = timeUs;
1084 } else {
1085 mVideoLastDequeueTimeUs = timeUs;
1086 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001087
1088 if (mSubtitleTrack.mSource != NULL
1089 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001090 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1091 msg->setInt64("timeUs", timeUs);
1092 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1093 msg->post();
1094 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001095
Lajos Molnare26940f2014-07-31 10:31:26 -07001096 if (mTimedTextTrack.mSource != NULL
1097 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1098 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1099 msg->setInt64("timeUs", timeUs);
1100 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1101 msg->post();
1102 }
1103
Andreas Huberafed0e12011-09-20 15:39:58 -07001104 return result;
1105}
1106
1107status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1108 *durationUs = mDurationUs;
1109 return OK;
1110}
1111
Robert Shihdd235722014-06-12 14:49:23 -07001112size_t NuPlayer::GenericSource::getTrackCount() const {
1113 return mSources.size();
1114}
1115
1116sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1117 size_t trackCount = mSources.size();
1118 if (trackIndex >= trackCount) {
1119 return NULL;
1120 }
1121
1122 sp<AMessage> format = new AMessage();
1123 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1124
1125 const char *mime;
1126 CHECK(meta->findCString(kKeyMIMEType, &mime));
1127
1128 int32_t trackType;
1129 if (!strncasecmp(mime, "video/", 6)) {
1130 trackType = MEDIA_TRACK_TYPE_VIDEO;
1131 } else if (!strncasecmp(mime, "audio/", 6)) {
1132 trackType = MEDIA_TRACK_TYPE_AUDIO;
1133 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1134 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1135 } else {
1136 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1137 }
1138 format->setInt32("type", trackType);
1139
1140 const char *lang;
1141 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1142 lang = "und";
1143 }
1144 format->setString("language", lang);
1145
1146 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1147 format->setString("mime", mime);
1148
1149 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1150 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1151 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1152 meta->findInt32(kKeyTrackIsForced, &isForced);
1153
1154 format->setInt32("auto", !!isAutoselect);
1155 format->setInt32("default", !!isDefault);
1156 format->setInt32("forced", !!isForced);
1157 }
1158
1159 return format;
1160}
1161
Lajos Molnare26940f2014-07-31 10:31:26 -07001162ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -07001163 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1164 msg->setInt32("type", type);
1165
1166 sp<AMessage> response;
1167 int32_t index;
1168 status_t err = msg->postAndAwaitResponse(&response);
1169 if (err == OK && response != NULL) {
1170 CHECK(response->findInt32("index", &index));
1171 return index;
1172 } else {
1173 return -1;
1174 }
1175}
1176
1177void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1178 int32_t tmpType;
1179 CHECK(msg->findInt32("type", &tmpType));
1180 media_track_type type = (media_track_type)tmpType;
1181
1182 sp<AMessage> response = new AMessage;
1183 ssize_t index = doGetSelectedTrack(type);
1184 response->setInt32("index", index);
1185
1186 uint32_t replyID;
1187 CHECK(msg->senderAwaitsResponse(&replyID));
1188 response->postReply(replyID);
1189}
1190
1191ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001192 const Track *track = NULL;
1193 switch (type) {
1194 case MEDIA_TRACK_TYPE_VIDEO:
1195 track = &mVideoTrack;
1196 break;
1197 case MEDIA_TRACK_TYPE_AUDIO:
1198 track = &mAudioTrack;
1199 break;
1200 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1201 track = &mTimedTextTrack;
1202 break;
1203 case MEDIA_TRACK_TYPE_SUBTITLE:
1204 track = &mSubtitleTrack;
1205 break;
1206 default:
1207 break;
1208 }
1209
1210 if (track != NULL && track->mSource != NULL) {
1211 return track->mIndex;
1212 }
1213
1214 return -1;
1215}
1216
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001217status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001218 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001219 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1220 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001221 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001222 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001223
1224 sp<AMessage> response;
1225 status_t err = msg->postAndAwaitResponse(&response);
1226 if (err == OK && response != NULL) {
1227 CHECK(response->findInt32("err", &err));
1228 }
1229
1230 return err;
1231}
1232
1233void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1234 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001235 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001236 CHECK(msg->findInt32("trackIndex", &trackIndex));
1237 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001238 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001239
1240 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001241 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001242 response->setInt32("err", err);
1243
1244 uint32_t replyID;
1245 CHECK(msg->senderAwaitsResponse(&replyID));
1246 response->postReply(replyID);
1247}
1248
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001249status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001250 if (trackIndex >= mSources.size()) {
1251 return BAD_INDEX;
1252 }
1253
1254 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001255 Track* track = NULL;
1256 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1257 track = &mSubtitleTrack;
1258 mFetchSubtitleDataGeneration++;
1259 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1260 track = &mTimedTextTrack;
1261 mFetchTimedTextDataGeneration++;
1262 }
1263 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001264 return INVALID_OPERATION;
1265 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001266 track->mSource->stop();
1267 track->mSource = NULL;
1268 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001269 return OK;
1270 }
1271
1272 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1273 sp<MetaData> meta = source->getFormat();
1274 const char *mime;
1275 CHECK(meta->findCString(kKeyMIMEType, &mime));
1276 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001277 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1278 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1279 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 return OK;
1281 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001282 track->mIndex = trackIndex;
1283 if (track->mSource != NULL) {
1284 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001285 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001286 track->mSource = mSources.itemAt(trackIndex);
1287 track->mSource->start();
1288 if (track->mPackets == NULL) {
1289 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001290 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001291 track->mPackets->clear();
1292 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001293
1294 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001295
1296 if (isSubtitle) {
1297 mFetchSubtitleDataGeneration++;
1298 } else {
1299 mFetchTimedTextDataGeneration++;
1300 }
1301
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001302 status_t eosResult; // ignored
1303 if (mSubtitleTrack.mSource != NULL
1304 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1305 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1306 msg->setInt64("timeUs", timeUs);
1307 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1308 msg->post();
1309 }
1310
1311 if (mTimedTextTrack.mSource != NULL
1312 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1313 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1314 msg->setInt64("timeUs", timeUs);
1315 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1316 msg->post();
1317 }
1318
Robert Shih3423bbd2014-07-16 15:47:09 -07001319 return OK;
1320 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1321 bool audio = !strncasecmp(mime, "audio/", 6);
1322 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1323 if (track->mSource != NULL && track->mIndex == trackIndex) {
1324 return OK;
1325 }
1326
1327 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1328 msg->setInt32("trackIndex", trackIndex);
1329 msg->post();
1330 return OK;
1331 }
1332
1333 return INVALID_OPERATION;
1334}
1335
Andreas Huberafed0e12011-09-20 15:39:58 -07001336status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001337 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1338 msg->setInt64("seekTimeUs", seekTimeUs);
1339
1340 sp<AMessage> response;
1341 status_t err = msg->postAndAwaitResponse(&response);
1342 if (err == OK && response != NULL) {
1343 CHECK(response->findInt32("err", &err));
1344 }
1345
1346 return err;
1347}
1348
1349void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1350 int64_t seekTimeUs;
1351 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1352
1353 sp<AMessage> response = new AMessage;
1354 status_t err = doSeek(seekTimeUs);
1355 response->setInt32("err", err);
1356
1357 uint32_t replyID;
1358 CHECK(msg->senderAwaitsResponse(&replyID));
1359 response->postReply(replyID);
1360}
1361
1362status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001363 // If the Widevine source is stopped, do not attempt to read any
1364 // more buffers.
1365 if (mStopRead) {
1366 return INVALID_OPERATION;
1367 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001368 if (mVideoTrack.mSource != NULL) {
1369 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001370 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001371
1372 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001373 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001374 }
1375
1376 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001377 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001378 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001379 }
1380
Ronghua Wu80276872014-08-28 15:50:29 -07001381 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1382 if (!mStarted) {
1383 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1384 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001385
1386 // If currently buffering, post kWhatBufferingEnd first, so that
1387 // NuPlayer resumes. Otherwise, if cache hits high watermark
1388 // before new polling happens, no one will resume the playback.
1389 stopBufferingIfNecessary();
1390 restartPollBuffering();
1391
Andreas Huberafed0e12011-09-20 15:39:58 -07001392 return OK;
1393}
1394
Robert Shih3423bbd2014-07-16 15:47:09 -07001395sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1396 MediaBuffer* mb,
1397 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001398 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001399 int64_t *actualTimeUs) {
1400 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1401 size_t outLength = mb->range_length();
1402
1403 if (audio && mAudioIsVorbis) {
1404 outLength += sizeof(int32_t);
1405 }
1406
1407 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001408 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001409 // data is already provided in the buffer
1410 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001411 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001412 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001413 } else {
1414 ab = new ABuffer(outLength);
1415 memcpy(ab->data(),
1416 (const uint8_t *)mb->data() + mb->range_offset(),
1417 mb->range_length());
1418 }
1419
1420 if (audio && mAudioIsVorbis) {
1421 int32_t numPageSamples;
1422 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1423 numPageSamples = -1;
1424 }
1425
1426 uint8_t* abEnd = ab->data() + mb->range_length();
1427 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1428 }
1429
Lajos Molnare26940f2014-07-31 10:31:26 -07001430 sp<AMessage> meta = ab->meta();
1431
Robert Shih3423bbd2014-07-16 15:47:09 -07001432 int64_t timeUs;
1433 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001434 meta->setInt64("timeUs", timeUs);
1435
Wei Jia474d7c72014-12-04 15:12:13 -08001436#if 0
1437 // Temporarily disable pre-roll till we have a full solution to handle
1438 // both single seek and continous seek gracefully.
1439 if (seekTimeUs > timeUs) {
1440 sp<AMessage> extra = new AMessage;
1441 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1442 meta->setMessage("extra", extra);
1443 }
1444#endif
1445
Lajos Molnare26940f2014-07-31 10:31:26 -07001446 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1447 const char *mime;
1448 CHECK(mTimedTextTrack.mSource != NULL
1449 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1450 meta->setString("mime", mime);
1451 }
1452
Robert Shih3423bbd2014-07-16 15:47:09 -07001453 int64_t durationUs;
1454 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1455 meta->setInt64("durationUs", durationUs);
1456 }
1457
1458 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1459 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1460 }
1461
1462 if (actualTimeUs) {
1463 *actualTimeUs = timeUs;
1464 }
1465
1466 mb->release();
1467 mb = NULL;
1468
1469 return ab;
1470}
1471
Robert Shih17f6dd62014-08-20 17:00:21 -07001472void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001473 Mutex::Autolock _l(mReadBufferLock);
1474
1475 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1476 mPendingReadBufferTypes |= (1 << trackType);
1477 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1478 msg->setInt32("trackType", trackType);
1479 msg->post();
1480 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001481}
1482
1483void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1484 int32_t tmpType;
1485 CHECK(msg->findInt32("trackType", &tmpType));
1486 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001487 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001488 {
1489 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001490 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001491 Mutex::Autolock _l(mReadBufferLock);
1492 mPendingReadBufferTypes &= ~(1 << trackType);
1493 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001494}
1495
Andreas Huberafed0e12011-09-20 15:39:58 -07001496void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001497 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001498 // Do not read data if Widevine source is stopped
1499 if (mStopRead) {
1500 return;
1501 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001502 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001503 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001504 switch (trackType) {
1505 case MEDIA_TRACK_TYPE_VIDEO:
1506 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001507 if (mIsWidevine) {
1508 maxBuffers = 2;
1509 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001510 break;
1511 case MEDIA_TRACK_TYPE_AUDIO:
1512 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001513 if (mIsWidevine) {
1514 maxBuffers = 8;
1515 } else {
1516 maxBuffers = 64;
1517 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001518 break;
1519 case MEDIA_TRACK_TYPE_SUBTITLE:
1520 track = &mSubtitleTrack;
1521 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001522 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1523 track = &mTimedTextTrack;
1524 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001525 default:
1526 TRESPASS();
1527 }
1528
1529 if (track->mSource == NULL) {
1530 return;
1531 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001532
1533 if (actualTimeUs) {
1534 *actualTimeUs = seekTimeUs;
1535 }
1536
1537 MediaSource::ReadOptions options;
1538
1539 bool seeking = false;
1540
1541 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001542 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001543 seeking = true;
1544 }
1545
Chong Zhang42e81532014-12-01 13:44:26 -08001546 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001547 options.setNonBlocking();
1548 }
1549
Phil Burkc5cc2e22014-09-09 20:08:39 -07001550 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001551 MediaBuffer *mbuf;
1552 status_t err = track->mSource->read(&mbuf, &options);
1553
1554 options.clearSeekTo();
1555
1556 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001557 int64_t timeUs;
1558 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1559 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1560 mAudioTimeUs = timeUs;
1561 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1562 mVideoTimeUs = timeUs;
1563 }
1564
Robert Shih3423bbd2014-07-16 15:47:09 -07001565 // formatChange && seeking: track whose source is changed during selection
1566 // formatChange && !seeking: track whose source is not changed during selection
1567 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001568 if ((seeking || formatChange)
1569 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1570 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001571 ATSParser::DiscontinuityType type = (formatChange && seeking)
1572 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1573 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001574 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001575 }
1576
Wei Jia474d7c72014-12-04 15:12:13 -08001577 sp<ABuffer> buffer = mediaBufferToABuffer(
1578 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001579 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001580 formatChange = false;
1581 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001582 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001583 } else if (err == WOULD_BLOCK) {
1584 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001585 } else if (err == INFO_FORMAT_CHANGED) {
1586#if 0
1587 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001588 ATSParser::DISCONTINUITY_FORMATCHANGE,
1589 NULL,
1590 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001591#endif
1592 } else {
1593 track->mPackets->signalEOS(err);
1594 break;
1595 }
1596 }
1597}
1598
Andreas Huberafed0e12011-09-20 15:39:58 -07001599} // namespace android