blob: a040343e21ca23662b0b7dc3d95910517efc57a5 [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 mBitrate(-1ll),
Lajos Molnar84f52782014-09-11 10:01:55 -070069 mPollBufferingGeneration(0),
Chong Zhangefbb6192015-01-30 17:13:27 -080070 mPendingReadBufferTypes(0),
71 mBuffering(false),
Chong Zhangc287cad2015-02-19 18:30:30 -080072 mPrepareBuffering(false),
73 mPrevBufferPercentage(-1) {
Chong Zhanga19f33e2014-08-07 15:35:07 -070074 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -070075 DataSource::RegisterDefaultSniffers();
Chong Zhang3de157d2014-08-05 20:54:44 -070076}
77
Chong Zhanga19f33e2014-08-07 15:35:07 -070078void NuPlayer::GenericSource::resetDataSource() {
79 mHTTPService.clear();
Robert Shih360d6d02014-09-29 14:42:35 -070080 mHttpSource.clear();
Chong Zhanga19f33e2014-08-07 15:35:07 -070081 mUri.clear();
82 mUriHeaders.clear();
Chong Zhanga6bf21f2014-11-19 20:26:34 -080083 if (mFd >= 0) {
84 close(mFd);
85 mFd = -1;
86 }
Chong Zhanga19f33e2014-08-07 15:35:07 -070087 mOffset = 0;
88 mLength = 0;
Ronghua Wu80276872014-08-28 15:50:29 -070089 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
90 mDecryptHandle = NULL;
91 mDrmManagerClient = NULL;
92 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -070093 mStopRead = true;
Chong Zhanga19f33e2014-08-07 15:35:07 -070094}
95
96status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -070097 const sp<IMediaHTTPService> &httpService,
98 const char *url,
99 const KeyedVector<String8, String8> *headers) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700100 resetDataSource();
Chong Zhang3de157d2014-08-05 20:54:44 -0700101
Chong Zhanga19f33e2014-08-07 15:35:07 -0700102 mHTTPService = httpService;
103 mUri = url;
Andreas Huberafed0e12011-09-20 15:39:58 -0700104
Chong Zhanga19f33e2014-08-07 15:35:07 -0700105 if (headers) {
106 mUriHeaders = *headers;
Chong Zhang3de157d2014-08-05 20:54:44 -0700107 }
108
Chong Zhanga19f33e2014-08-07 15:35:07 -0700109 // delay data source creation to prepareAsync() to avoid blocking
110 // the calling thread in setDataSource for any significant time.
111 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700112}
113
Chong Zhanga19f33e2014-08-07 15:35:07 -0700114status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -0700115 int fd, int64_t offset, int64_t length) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700116 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700117
Chong Zhanga19f33e2014-08-07 15:35:07 -0700118 mFd = dup(fd);
119 mOffset = offset;
120 mLength = length;
121
122 // delay data source creation to prepareAsync() to avoid blocking
123 // the calling thread in setDataSource for any significant time.
124 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700125}
126
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700127sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const {
128 return mFileMeta;
129}
130
Chong Zhangd354d8d2014-08-20 13:09:58 -0700131status_t NuPlayer::GenericSource::initFromDataSource() {
Lajos Molnarcc227032014-07-17 15:33:06 -0700132 sp<MediaExtractor> extractor;
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800133 String8 mimeType;
134 float confidence;
135 sp<AMessage> dummy;
136 bool isWidevineStreaming = false;
Lajos Molnarcc227032014-07-17 15:33:06 -0700137
Chong Zhangd354d8d2014-08-20 13:09:58 -0700138 CHECK(mDataSource != NULL);
139
Lajos Molnarcc227032014-07-17 15:33:06 -0700140 if (mIsWidevine) {
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800141 isWidevineStreaming = SniffWVM(
142 mDataSource, &mimeType, &confidence, &dummy);
143 if (!isWidevineStreaming ||
144 strcasecmp(
Lajos Molnarcc227032014-07-17 15:33:06 -0700145 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
146 ALOGE("unsupported widevine mime: %s", mimeType.string());
Chong Zhang3de157d2014-08-05 20:54:44 -0700147 return UNKNOWN_ERROR;
Lajos Molnarcc227032014-07-17 15:33:06 -0700148 }
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800149 } else if (mIsStreaming) {
Chong Zhangc287cad2015-02-19 18:30:30 -0800150 if (!mDataSource->sniff(&mimeType, &confidence, &dummy)) {
151 return UNKNOWN_ERROR;
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800152 }
153 isWidevineStreaming = !strcasecmp(
Chong Zhangc287cad2015-02-19 18:30:30 -0800154 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800155 }
Lajos Molnarcc227032014-07-17 15:33:06 -0700156
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800157 if (isWidevineStreaming) {
158 // we don't want cached source for widevine streaming.
159 mCachedSource.clear();
160 mDataSource = mHttpSource;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700161 mWVMExtractor = new WVMExtractor(mDataSource);
162 mWVMExtractor->setAdaptiveStreamingMode(true);
Lajos Molnarcc227032014-07-17 15:33:06 -0700163 if (mUIDValid) {
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700164 mWVMExtractor->setUID(mUID);
Lajos Molnarcc227032014-07-17 15:33:06 -0700165 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700166 extractor = mWVMExtractor;
Lajos Molnarcc227032014-07-17 15:33:06 -0700167 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700168 extractor = MediaExtractor::Create(mDataSource,
Chong Zhangc287cad2015-02-19 18:30:30 -0800169 mimeType.isEmpty() ? NULL : mimeType.string());
Lajos Molnarcc227032014-07-17 15:33:06 -0700170 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700171
Chong Zhang3de157d2014-08-05 20:54:44 -0700172 if (extractor == NULL) {
173 return UNKNOWN_ERROR;
174 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700175
Ronghua Wu80276872014-08-28 15:50:29 -0700176 if (extractor->getDrmFlag()) {
177 checkDrmStatus(mDataSource);
178 }
179
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700180 mFileMeta = extractor->getMetaData();
181 if (mFileMeta != NULL) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700182 int64_t duration;
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700183 if (mFileMeta->findInt64(kKeyDuration, &duration)) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700184 mDurationUs = duration;
185 }
Chong Zhang42e81532014-12-01 13:44:26 -0800186
187 if (!mIsWidevine) {
188 // Check mime to see if we actually have a widevine source.
189 // If the data source is not URL-type (eg. file source), we
190 // won't be able to tell until now.
191 const char *fileMime;
192 if (mFileMeta->findCString(kKeyMIMEType, &fileMime)
193 && !strncasecmp(fileMime, "video/wvm", 9)) {
194 mIsWidevine = true;
195 }
196 }
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700197 }
198
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700199 int32_t totalBitrate = 0;
200
Marco Nelissen705d3292014-09-19 15:14:37 -0700201 size_t numtracks = extractor->countTracks();
202 if (numtracks == 0) {
203 return UNKNOWN_ERROR;
204 }
205
206 for (size_t i = 0; i < numtracks; ++i) {
Chong Zhangafc0a872014-08-26 09:56:52 -0700207 sp<MediaSource> track = extractor->getTrack(i);
208
Andreas Huberafed0e12011-09-20 15:39:58 -0700209 sp<MetaData> meta = extractor->getTrackMetaData(i);
210
211 const char *mime;
212 CHECK(meta->findCString(kKeyMIMEType, &mime));
213
Chong Zhangafc0a872014-08-26 09:56:52 -0700214 // Do the string compare immediately with "mime",
215 // we can't assume "mime" would stay valid after another
216 // extractor operation, some extractors might modify meta
217 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700218 if (!strncasecmp(mime, "audio/", 6)) {
219 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700220 mAudioTrack.mIndex = i;
221 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700222 mAudioTrack.mPackets =
223 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700224
225 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
226 mAudioIsVorbis = true;
227 } else {
228 mAudioIsVorbis = false;
229 }
230 }
231 } else if (!strncasecmp(mime, "video/", 6)) {
232 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700233 mVideoTrack.mIndex = i;
234 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700235 mVideoTrack.mPackets =
236 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700237
238 // check if the source requires secure buffers
239 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700240 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
241 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800242 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700243 if (mUIDValid) {
244 extractor->setUID(mUID);
245 }
Chong Zhang7e892182014-08-05 11:58:21 -0700246 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700247 }
248 }
249
250 if (track != NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700251 mSources.push(track);
Andreas Huberafed0e12011-09-20 15:39:58 -0700252 int64_t durationUs;
253 if (meta->findInt64(kKeyDuration, &durationUs)) {
254 if (durationUs > mDurationUs) {
255 mDurationUs = durationUs;
256 }
257 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700258
259 int32_t bitrate;
260 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
261 totalBitrate += bitrate;
262 } else {
263 totalBitrate = -1;
264 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700265 }
266 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700267
Chong Zhangefbb6192015-01-30 17:13:27 -0800268 // Start the selected A/V tracks now before we start buffering.
269 // Widevine sources might re-initialize crypto when starting, if we delay
270 // this to start(), all data buffered during prepare would be wasted.
271 // (We don't actually start reading until start().)
272 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
273 ALOGE("failed to start audio track!");
274 return UNKNOWN_ERROR;
275 }
276
277 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
278 ALOGE("failed to start video track!");
279 return UNKNOWN_ERROR;
280 }
281
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700282 mBitrate = totalBitrate;
283
Chong Zhang3de157d2014-08-05 20:54:44 -0700284 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700285}
286
Ronghua Wu80276872014-08-28 15:50:29 -0700287void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
288 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
289 if (mDecryptHandle != NULL) {
290 CHECK(mDrmManagerClient);
291 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
292 sp<AMessage> msg = dupNotify();
293 msg->setInt32("what", kWhatDrmNoLicense);
294 msg->post();
295 }
296 }
297}
298
299int64_t NuPlayer::GenericSource::getLastReadPosition() {
300 if (mAudioTrack.mSource != NULL) {
301 return mAudioTimeUs;
302 } else if (mVideoTrack.mSource != NULL) {
303 return mVideoTimeUs;
304 } else {
305 return 0;
306 }
307}
308
Chong Zhanga19f33e2014-08-07 15:35:07 -0700309status_t NuPlayer::GenericSource::setBuffers(
310 bool audio, Vector<MediaBuffer *> &buffers) {
Chong Zhang42e81532014-12-01 13:44:26 -0800311 if (mIsSecure && !audio) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700312 return mVideoTrack.mSource->setBuffers(buffers);
313 }
314 return INVALID_OPERATION;
315}
316
Andreas Huberafed0e12011-09-20 15:39:58 -0700317NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700318 if (mLooper != NULL) {
319 mLooper->unregisterHandler(id());
320 mLooper->stop();
321 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800322 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700323}
324
Andreas Huber9575c962013-02-05 13:59:56 -0800325void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700326 if (mLooper == NULL) {
327 mLooper = new ALooper;
328 mLooper->setName("generic");
329 mLooper->start();
330
331 mLooper->registerHandler(this);
332 }
333
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800334 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
Chong Zhang1228d6b2014-08-12 21:25:48 -0700335 msg->post();
336}
337
338void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700339 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700340 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800341 // set to false first, if the extractor
342 // comes back as secure, set it to true then.
343 mIsSecure = false;
344
Chong Zhangd354d8d2014-08-20 13:09:58 -0700345 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700346 const char* uri = mUri.c_str();
Chong Zhangc287cad2015-02-19 18:30:30 -0800347 String8 contentType;
Robert Shih360d6d02014-09-29 14:42:35 -0700348 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(
Chong Zhangc287cad2015-02-19 18:30:30 -0800362 mHTTPService, uri, &mUriHeaders, &contentType,
Robert Shih360d6d02014-09-29 14:42:35 -0700363 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 Zhangc287cad2015-02-19 18:30:30 -0800390 // init extractor from data source
391 status_t err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700392
393 if (err != OK) {
394 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700395 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700396 return;
397 }
398
Andreas Huber9575c962013-02-05 13:59:56 -0800399 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700400 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
401 sp<AMessage> msg = new AMessage;
402 err = convertMetaDataToMessage(meta, &msg);
403 if(err != OK) {
404 notifyPreparedAndCleanup(err);
405 return;
406 }
407 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800408 }
409
410 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800411 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800412 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700413 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800414 | FLAG_CAN_SEEK_BACKWARD
415 | FLAG_CAN_SEEK_FORWARD
416 | FLAG_CAN_SEEK);
417
Chong Zhangefbb6192015-01-30 17:13:27 -0800418 if (mIsStreaming) {
419 mPrepareBuffering = true;
420
421 ensureCacheIsFetching();
422 restartPollBuffering();
423 } else {
424 notifyPrepared();
425 }
Andreas Huber9575c962013-02-05 13:59:56 -0800426}
427
Chong Zhangd354d8d2014-08-20 13:09:58 -0700428void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
429 if (err != OK) {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700430 mDataSource.clear();
431 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700432 mHttpSource.clear();
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700433
434 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700435 }
436 notifyPrepared(err);
437}
438
Andreas Huberafed0e12011-09-20 15:39:58 -0700439void NuPlayer::GenericSource::start() {
440 ALOGI("start");
441
Andy Hung2abde2c2014-09-30 14:40:32 -0700442 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700443 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700444 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700445 }
446
447 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700448 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700449 }
Ronghua Wu80276872014-08-28 15:50:29 -0700450
451 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
452 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800453
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800454 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700455}
456
457void NuPlayer::GenericSource::stop() {
458 // nothing to do, just account for DRM playback status
459 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
460 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800461 if (mIsWidevine || mIsSecure) {
462 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800463 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700464 sp<AMessage> response;
465 (void) msg->postAndAwaitResponse(&response);
466 }
Ronghua Wu80276872014-08-28 15:50:29 -0700467}
468
469void NuPlayer::GenericSource::pause() {
470 // nothing to do, just account for DRM playback status
471 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
472 mStarted = false;
473}
474
475void NuPlayer::GenericSource::resume() {
476 // nothing to do, just account for DRM playback status
477 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
478 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800479
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800480 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700481}
482
Chong Zhang48296b72014-09-14 14:28:45 -0700483void NuPlayer::GenericSource::disconnect() {
484 if (mDataSource != NULL) {
485 // disconnect data source
486 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
487 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
488 }
Robert Shih360d6d02014-09-29 14:42:35 -0700489 } else if (mHttpSource != NULL) {
490 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700491 }
492}
493
Ronghua Wu80276872014-08-28 15:50:29 -0700494void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
495 if (mDecryptHandle != NULL) {
496 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
497 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700498 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
499 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700500}
501
502status_t NuPlayer::GenericSource::feedMoreTSData() {
503 return OK;
504}
505
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700506void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800507 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700508 msg->setInt32("generation", mPollBufferingGeneration);
509 msg->post(1000000ll);
510}
511
512void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800513 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700514 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800515 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700516}
517
Chong Zhangefbb6192015-01-30 17:13:27 -0800518void NuPlayer::GenericSource::restartPollBuffering() {
519 if (mIsStreaming) {
520 cancelPollBuffering();
521 onPollBuffering();
522 }
523}
524
Chong Zhangc287cad2015-02-19 18:30:30 -0800525void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
526 // Buffering percent could go backward as it's estimated from remaining
527 // data and last access time. This could cause the buffering position
528 // drawn on media control to jitter slightly. Remember previously reported
529 // percentage and don't allow it to go backward.
530 if (percentage < mPrevBufferPercentage) {
531 percentage = mPrevBufferPercentage;
532 } else if (percentage > 100) {
533 percentage = 100;
534 }
535
536 mPrevBufferPercentage = percentage;
537
Chong Zhangefbb6192015-01-30 17:13:27 -0800538 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
539
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700540 sp<AMessage> msg = dupNotify();
541 msg->setInt32("what", kWhatBufferingUpdate);
542 msg->setInt32("percentage", percentage);
543 msg->post();
544}
545
Chong Zhangefbb6192015-01-30 17:13:27 -0800546void NuPlayer::GenericSource::startBufferingIfNecessary() {
547 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
548 mPrepareBuffering, mBuffering);
549
550 if (mPrepareBuffering) {
551 return;
552 }
553
554 if (!mBuffering) {
555 mBuffering = true;
556
557 ensureCacheIsFetching();
558 sendCacheStats();
559
560 sp<AMessage> notify = dupNotify();
561 notify->setInt32("what", kWhatPauseOnBufferingStart);
562 notify->post();
563 }
564}
565
566void NuPlayer::GenericSource::stopBufferingIfNecessary() {
567 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
568 mPrepareBuffering, mBuffering);
569
570 if (mPrepareBuffering) {
571 mPrepareBuffering = false;
572 notifyPrepared();
573 return;
574 }
575
576 if (mBuffering) {
577 mBuffering = false;
578
579 sendCacheStats();
580
581 sp<AMessage> notify = dupNotify();
582 notify->setInt32("what", kWhatResumeOnBufferingEnd);
583 notify->post();
584 }
585}
586
587void NuPlayer::GenericSource::sendCacheStats() {
588 int32_t kbps = 0;
589 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700590
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800591 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800592 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800593 } else if (mCachedSource != NULL) {
594 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800595 }
596
597 if (err == OK) {
598 sp<AMessage> notify = dupNotify();
599 notify->setInt32("what", kWhatCacheStats);
600 notify->setInt32("bandwidth", kbps);
601 notify->post();
602 }
603}
604
605void NuPlayer::GenericSource::ensureCacheIsFetching() {
606 if (mCachedSource != NULL) {
607 mCachedSource->resumeFetchingIfNecessary();
608 }
609}
610
611void NuPlayer::GenericSource::onPollBuffering() {
612 status_t finalStatus = UNKNOWN_ERROR;
613 int64_t cachedDurationUs = -1ll;
614 ssize_t cachedDataRemaining = -1;
615
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800616 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
617 "WVMExtractor and NuCachedSource both present");
618
619 if (mWVMExtractor != NULL) {
620 cachedDurationUs =
621 mWVMExtractor->getCachedDurationUs(&finalStatus);
622 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800623 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700624 mCachedSource->approxDataRemaining(&finalStatus);
625
626 if (finalStatus == OK) {
627 off64_t size;
628 int64_t bitrate = 0ll;
629 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
630 bitrate = size * 8000000ll / mDurationUs;
631 } else if (mBitrate > 0) {
632 bitrate = mBitrate;
633 }
634 if (bitrate > 0) {
635 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
636 }
637 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700638 }
639
Chong Zhangefbb6192015-01-30 17:13:27 -0800640 if (finalStatus != OK) {
641 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
642
643 if (finalStatus == ERROR_END_OF_STREAM) {
644 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700645 }
646
Chong Zhangefbb6192015-01-30 17:13:27 -0800647 stopBufferingIfNecessary();
648 return;
649 } else if (cachedDurationUs >= 0ll) {
650 if (mDurationUs > 0ll) {
651 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
652 int percentage = 100.0 * cachedPosUs / mDurationUs;
653 if (percentage > 100) {
654 percentage = 100;
655 }
656
657 notifyBufferingUpdate(percentage);
658 }
659
660 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
661 cachedDurationUs / 1000000.0f);
662
663 if (cachedDurationUs < kLowWaterMarkUs) {
664 startBufferingIfNecessary();
665 } else if (cachedDurationUs > kHighWaterMarkUs) {
666 stopBufferingIfNecessary();
667 }
668 } else if (cachedDataRemaining >= 0) {
669 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
670 cachedDataRemaining);
671
672 if (cachedDataRemaining < kLowWaterMarkBytes) {
673 startBufferingIfNecessary();
674 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
675 stopBufferingIfNecessary();
676 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700677 }
678
679 schedulePollBuffering();
680}
681
Robert Shih3423bbd2014-07-16 15:47:09 -0700682void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
683 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700684 case kWhatPrepareAsync:
685 {
686 onPrepareAsync();
687 break;
688 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700689 case kWhatFetchSubtitleData:
690 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700691 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
692 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
693 break;
694 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700695
Lajos Molnare26940f2014-07-31 10:31:26 -0700696 case kWhatFetchTimedTextData:
697 {
698 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
699 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700700 break;
701 }
702
703 case kWhatSendSubtitleData:
704 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700705 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
706 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
707 break;
708 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700709
Lajos Molnare26940f2014-07-31 10:31:26 -0700710 case kWhatSendTimedTextData:
711 {
712 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
713 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700714 break;
715 }
716
717 case kWhatChangeAVSource:
718 {
719 int32_t trackIndex;
720 CHECK(msg->findInt32("trackIndex", &trackIndex));
721 const sp<MediaSource> source = mSources.itemAt(trackIndex);
722
723 Track* track;
724 const char *mime;
725 media_track_type trackType, counterpartType;
726 sp<MetaData> meta = source->getFormat();
727 meta->findCString(kKeyMIMEType, &mime);
728 if (!strncasecmp(mime, "audio/", 6)) {
729 track = &mAudioTrack;
730 trackType = MEDIA_TRACK_TYPE_AUDIO;
731 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
732 } else {
733 CHECK(!strncasecmp(mime, "video/", 6));
734 track = &mVideoTrack;
735 trackType = MEDIA_TRACK_TYPE_VIDEO;
736 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
737 }
738
739
740 if (track->mSource != NULL) {
741 track->mSource->stop();
742 }
743 track->mSource = source;
744 track->mSource->start();
745 track->mIndex = trackIndex;
746
Robert Shih3423bbd2014-07-16 15:47:09 -0700747 int64_t timeUs, actualTimeUs;
748 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800749 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
750 timeUs = mAudioLastDequeueTimeUs;
751 } else {
752 timeUs = mVideoLastDequeueTimeUs;
753 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700754 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
755 readBuffer(counterpartType, -1, NULL, formatChange);
756 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
757
758 break;
759 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800760
761 case kWhatStart:
762 case kWhatResume:
763 {
764 restartPollBuffering();
765 break;
766 }
767
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700768 case kWhatPollBuffering:
769 {
770 int32_t generation;
771 CHECK(msg->findInt32("generation", &generation));
772 if (generation == mPollBufferingGeneration) {
773 onPollBuffering();
774 }
775 break;
776 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700777
778 case kWhatGetFormat:
779 {
780 onGetFormatMeta(msg);
781 break;
782 }
783
784 case kWhatGetSelectedTrack:
785 {
786 onGetSelectedTrack(msg);
787 break;
788 }
789
790 case kWhatSelectTrack:
791 {
792 onSelectTrack(msg);
793 break;
794 }
795
796 case kWhatSeek:
797 {
798 onSeek(msg);
799 break;
800 }
801
802 case kWhatReadBuffer:
803 {
804 onReadBuffer(msg);
805 break;
806 }
807
Andy Hung2abde2c2014-09-30 14:40:32 -0700808 case kWhatStopWidevine:
809 {
810 // mStopRead is only used for Widevine to prevent the video source
811 // from being read while the associated video decoder is shutting down.
812 mStopRead = true;
813 if (mVideoTrack.mSource != NULL) {
814 mVideoTrack.mPackets->clear();
815 }
816 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800817 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700818 CHECK(msg->senderAwaitsResponse(&replyID));
819 response->postReply(replyID);
820 break;
821 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700822 default:
823 Source::onMessageReceived(msg);
824 break;
825 }
826}
827
Lajos Molnare26940f2014-07-31 10:31:26 -0700828void NuPlayer::GenericSource::fetchTextData(
829 uint32_t sendWhat,
830 media_track_type type,
831 int32_t curGen,
832 sp<AnotherPacketSource> packets,
833 sp<AMessage> msg) {
834 int32_t msgGeneration;
835 CHECK(msg->findInt32("generation", &msgGeneration));
836 if (msgGeneration != curGen) {
837 // stale
838 return;
839 }
840
841 int32_t avail;
842 if (packets->hasBufferAvailable(&avail)) {
843 return;
844 }
845
846 int64_t timeUs;
847 CHECK(msg->findInt64("timeUs", &timeUs));
848
849 int64_t subTimeUs;
850 readBuffer(type, timeUs, &subTimeUs);
851
852 int64_t delayUs = subTimeUs - timeUs;
853 if (msg->what() == kWhatFetchSubtitleData) {
854 const int64_t oneSecUs = 1000000ll;
855 delayUs -= oneSecUs;
856 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800857 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700858 msg2->setInt32("generation", msgGeneration);
859 msg2->post(delayUs < 0 ? 0 : delayUs);
860}
861
862void NuPlayer::GenericSource::sendTextData(
863 uint32_t what,
864 media_track_type type,
865 int32_t curGen,
866 sp<AnotherPacketSource> packets,
867 sp<AMessage> msg) {
868 int32_t msgGeneration;
869 CHECK(msg->findInt32("generation", &msgGeneration));
870 if (msgGeneration != curGen) {
871 // stale
872 return;
873 }
874
875 int64_t subTimeUs;
876 if (packets->nextBufferTime(&subTimeUs) != OK) {
877 return;
878 }
879
880 int64_t nextSubTimeUs;
881 readBuffer(type, -1, &nextSubTimeUs);
882
883 sp<ABuffer> buffer;
884 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
885 if (dequeueStatus == OK) {
886 sp<AMessage> notify = dupNotify();
887 notify->setInt32("what", what);
888 notify->setBuffer("buffer", buffer);
889 notify->post();
890
891 const int64_t delayUs = nextSubTimeUs - subTimeUs;
892 msg->post(delayUs < 0 ? 0 : delayUs);
893 }
894}
895
Andreas Huber84066782011-08-16 09:34:26 -0700896sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800897 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700898 msg->setInt32("audio", audio);
899
900 sp<AMessage> response;
901 void *format;
902 status_t err = msg->postAndAwaitResponse(&response);
903 if (err == OK && response != NULL) {
904 CHECK(response->findPointer("format", &format));
905 return (MetaData *)format;
906 } else {
907 return NULL;
908 }
909}
910
911void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
912 int32_t audio;
913 CHECK(msg->findInt32("audio", &audio));
914
915 sp<AMessage> response = new AMessage;
916 sp<MetaData> format = doGetFormatMeta(audio);
917 response->setPointer("format", format.get());
918
Lajos Molnar3f274362015-03-05 14:35:41 -0800919 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -0700920 CHECK(msg->senderAwaitsResponse(&replyID));
921 response->postReply(replyID);
922}
923
924sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700925 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
926
927 if (source == NULL) {
928 return NULL;
929 }
930
931 return source->getFormat();
932}
933
934status_t NuPlayer::GenericSource::dequeueAccessUnit(
935 bool audio, sp<ABuffer> *accessUnit) {
936 Track *track = audio ? &mAudioTrack : &mVideoTrack;
937
938 if (track->mSource == NULL) {
939 return -EWOULDBLOCK;
940 }
941
Lajos Molnarcc227032014-07-17 15:33:06 -0700942 if (mIsWidevine && !audio) {
943 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700944 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -0700945 }
946
Andreas Huberafed0e12011-09-20 15:39:58 -0700947 status_t finalResult;
948 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -0800949 if (finalResult == OK) {
950 postReadBuffer(
951 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
952 return -EWOULDBLOCK;
953 }
954 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -0700955 }
956
957 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
958
Robert Shih3423bbd2014-07-16 15:47:09 -0700959 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700960 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -0700961 }
962
Robert Shih3423bbd2014-07-16 15:47:09 -0700963 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -0700964 if (mSubtitleTrack.mSource != NULL) {
965 mSubtitleTrack.mPackets->clear();
966 mFetchSubtitleDataGeneration++;
967 }
968 if (mTimedTextTrack.mSource != NULL) {
969 mTimedTextTrack.mPackets->clear();
970 mFetchTimedTextDataGeneration++;
971 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700972 return result;
973 }
974
975 int64_t timeUs;
976 status_t eosResult; // ignored
977 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -0800978 if (audio) {
979 mAudioLastDequeueTimeUs = timeUs;
980 } else {
981 mVideoLastDequeueTimeUs = timeUs;
982 }
Lajos Molnare26940f2014-07-31 10:31:26 -0700983
984 if (mSubtitleTrack.mSource != NULL
985 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800986 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -0700987 msg->setInt64("timeUs", timeUs);
988 msg->setInt32("generation", mFetchSubtitleDataGeneration);
989 msg->post();
990 }
Robert Shiheb1735e2014-07-23 15:53:14 -0700991
Lajos Molnare26940f2014-07-31 10:31:26 -0700992 if (mTimedTextTrack.mSource != NULL
993 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800994 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700995 msg->setInt64("timeUs", timeUs);
996 msg->setInt32("generation", mFetchTimedTextDataGeneration);
997 msg->post();
998 }
999
Andreas Huberafed0e12011-09-20 15:39:58 -07001000 return result;
1001}
1002
1003status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1004 *durationUs = mDurationUs;
1005 return OK;
1006}
1007
Robert Shihdd235722014-06-12 14:49:23 -07001008size_t NuPlayer::GenericSource::getTrackCount() const {
1009 return mSources.size();
1010}
1011
1012sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1013 size_t trackCount = mSources.size();
1014 if (trackIndex >= trackCount) {
1015 return NULL;
1016 }
1017
1018 sp<AMessage> format = new AMessage();
1019 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1020
1021 const char *mime;
1022 CHECK(meta->findCString(kKeyMIMEType, &mime));
1023
1024 int32_t trackType;
1025 if (!strncasecmp(mime, "video/", 6)) {
1026 trackType = MEDIA_TRACK_TYPE_VIDEO;
1027 } else if (!strncasecmp(mime, "audio/", 6)) {
1028 trackType = MEDIA_TRACK_TYPE_AUDIO;
1029 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1030 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1031 } else {
1032 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1033 }
1034 format->setInt32("type", trackType);
1035
1036 const char *lang;
1037 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1038 lang = "und";
1039 }
1040 format->setString("language", lang);
1041
1042 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1043 format->setString("mime", mime);
1044
1045 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1046 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1047 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1048 meta->findInt32(kKeyTrackIsForced, &isForced);
1049
1050 format->setInt32("auto", !!isAutoselect);
1051 format->setInt32("default", !!isDefault);
1052 format->setInt32("forced", !!isForced);
1053 }
1054
1055 return format;
1056}
1057
Lajos Molnare26940f2014-07-31 10:31:26 -07001058ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001059 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001060 msg->setInt32("type", type);
1061
1062 sp<AMessage> response;
1063 int32_t index;
1064 status_t err = msg->postAndAwaitResponse(&response);
1065 if (err == OK && response != NULL) {
1066 CHECK(response->findInt32("index", &index));
1067 return index;
1068 } else {
1069 return -1;
1070 }
1071}
1072
1073void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1074 int32_t tmpType;
1075 CHECK(msg->findInt32("type", &tmpType));
1076 media_track_type type = (media_track_type)tmpType;
1077
1078 sp<AMessage> response = new AMessage;
1079 ssize_t index = doGetSelectedTrack(type);
1080 response->setInt32("index", index);
1081
Lajos Molnar3f274362015-03-05 14:35:41 -08001082 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001083 CHECK(msg->senderAwaitsResponse(&replyID));
1084 response->postReply(replyID);
1085}
1086
1087ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001088 const Track *track = NULL;
1089 switch (type) {
1090 case MEDIA_TRACK_TYPE_VIDEO:
1091 track = &mVideoTrack;
1092 break;
1093 case MEDIA_TRACK_TYPE_AUDIO:
1094 track = &mAudioTrack;
1095 break;
1096 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1097 track = &mTimedTextTrack;
1098 break;
1099 case MEDIA_TRACK_TYPE_SUBTITLE:
1100 track = &mSubtitleTrack;
1101 break;
1102 default:
1103 break;
1104 }
1105
1106 if (track != NULL && track->mSource != NULL) {
1107 return track->mIndex;
1108 }
1109
1110 return -1;
1111}
1112
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001113status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001114 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001115 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001116 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001117 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001118 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001119
1120 sp<AMessage> response;
1121 status_t err = msg->postAndAwaitResponse(&response);
1122 if (err == OK && response != NULL) {
1123 CHECK(response->findInt32("err", &err));
1124 }
1125
1126 return err;
1127}
1128
1129void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1130 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001131 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001132 CHECK(msg->findInt32("trackIndex", &trackIndex));
1133 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001134 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001135
1136 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001137 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001138 response->setInt32("err", err);
1139
Lajos Molnar3f274362015-03-05 14:35:41 -08001140 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001141 CHECK(msg->senderAwaitsResponse(&replyID));
1142 response->postReply(replyID);
1143}
1144
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001145status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001146 if (trackIndex >= mSources.size()) {
1147 return BAD_INDEX;
1148 }
1149
1150 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001151 Track* track = NULL;
1152 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1153 track = &mSubtitleTrack;
1154 mFetchSubtitleDataGeneration++;
1155 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1156 track = &mTimedTextTrack;
1157 mFetchTimedTextDataGeneration++;
1158 }
1159 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001160 return INVALID_OPERATION;
1161 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001162 track->mSource->stop();
1163 track->mSource = NULL;
1164 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001165 return OK;
1166 }
1167
1168 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1169 sp<MetaData> meta = source->getFormat();
1170 const char *mime;
1171 CHECK(meta->findCString(kKeyMIMEType, &mime));
1172 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001173 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1174 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1175 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001176 return OK;
1177 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001178 track->mIndex = trackIndex;
1179 if (track->mSource != NULL) {
1180 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001181 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001182 track->mSource = mSources.itemAt(trackIndex);
1183 track->mSource->start();
1184 if (track->mPackets == NULL) {
1185 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001186 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001187 track->mPackets->clear();
1188 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001189
1190 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001191
1192 if (isSubtitle) {
1193 mFetchSubtitleDataGeneration++;
1194 } else {
1195 mFetchTimedTextDataGeneration++;
1196 }
1197
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001198 status_t eosResult; // ignored
1199 if (mSubtitleTrack.mSource != NULL
1200 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001201 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001202 msg->setInt64("timeUs", timeUs);
1203 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1204 msg->post();
1205 }
1206
1207 if (mTimedTextTrack.mSource != NULL
1208 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001209 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001210 msg->setInt64("timeUs", timeUs);
1211 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1212 msg->post();
1213 }
1214
Robert Shih3423bbd2014-07-16 15:47:09 -07001215 return OK;
1216 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1217 bool audio = !strncasecmp(mime, "audio/", 6);
1218 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1219 if (track->mSource != NULL && track->mIndex == trackIndex) {
1220 return OK;
1221 }
1222
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001223 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001224 msg->setInt32("trackIndex", trackIndex);
1225 msg->post();
1226 return OK;
1227 }
1228
1229 return INVALID_OPERATION;
1230}
1231
Andreas Huberafed0e12011-09-20 15:39:58 -07001232status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001233 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001234 msg->setInt64("seekTimeUs", seekTimeUs);
1235
1236 sp<AMessage> response;
1237 status_t err = msg->postAndAwaitResponse(&response);
1238 if (err == OK && response != NULL) {
1239 CHECK(response->findInt32("err", &err));
1240 }
1241
1242 return err;
1243}
1244
1245void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1246 int64_t seekTimeUs;
1247 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1248
1249 sp<AMessage> response = new AMessage;
1250 status_t err = doSeek(seekTimeUs);
1251 response->setInt32("err", err);
1252
Lajos Molnar3f274362015-03-05 14:35:41 -08001253 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001254 CHECK(msg->senderAwaitsResponse(&replyID));
1255 response->postReply(replyID);
1256}
1257
1258status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001259 // If the Widevine source is stopped, do not attempt to read any
1260 // more buffers.
1261 if (mStopRead) {
1262 return INVALID_OPERATION;
1263 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001264 if (mVideoTrack.mSource != NULL) {
1265 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001266 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001267
1268 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001269 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001270 }
1271
1272 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001273 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001274 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001275 }
1276
Ronghua Wu80276872014-08-28 15:50:29 -07001277 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1278 if (!mStarted) {
1279 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1280 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001281
1282 // If currently buffering, post kWhatBufferingEnd first, so that
1283 // NuPlayer resumes. Otherwise, if cache hits high watermark
1284 // before new polling happens, no one will resume the playback.
1285 stopBufferingIfNecessary();
1286 restartPollBuffering();
1287
Andreas Huberafed0e12011-09-20 15:39:58 -07001288 return OK;
1289}
1290
Robert Shih3423bbd2014-07-16 15:47:09 -07001291sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1292 MediaBuffer* mb,
1293 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001294 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001295 int64_t *actualTimeUs) {
1296 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1297 size_t outLength = mb->range_length();
1298
1299 if (audio && mAudioIsVorbis) {
1300 outLength += sizeof(int32_t);
1301 }
1302
1303 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001304 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001305 // data is already provided in the buffer
1306 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001307 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001308 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001309 } else {
1310 ab = new ABuffer(outLength);
1311 memcpy(ab->data(),
1312 (const uint8_t *)mb->data() + mb->range_offset(),
1313 mb->range_length());
1314 }
1315
1316 if (audio && mAudioIsVorbis) {
1317 int32_t numPageSamples;
1318 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1319 numPageSamples = -1;
1320 }
1321
1322 uint8_t* abEnd = ab->data() + mb->range_length();
1323 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1324 }
1325
Lajos Molnare26940f2014-07-31 10:31:26 -07001326 sp<AMessage> meta = ab->meta();
1327
Robert Shih3423bbd2014-07-16 15:47:09 -07001328 int64_t timeUs;
1329 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001330 meta->setInt64("timeUs", timeUs);
1331
Wei Jia474d7c72014-12-04 15:12:13 -08001332#if 0
1333 // Temporarily disable pre-roll till we have a full solution to handle
1334 // both single seek and continous seek gracefully.
1335 if (seekTimeUs > timeUs) {
1336 sp<AMessage> extra = new AMessage;
1337 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1338 meta->setMessage("extra", extra);
1339 }
1340#endif
1341
Lajos Molnare26940f2014-07-31 10:31:26 -07001342 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1343 const char *mime;
1344 CHECK(mTimedTextTrack.mSource != NULL
1345 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1346 meta->setString("mime", mime);
1347 }
1348
Robert Shih3423bbd2014-07-16 15:47:09 -07001349 int64_t durationUs;
1350 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1351 meta->setInt64("durationUs", durationUs);
1352 }
1353
1354 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1355 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1356 }
1357
1358 if (actualTimeUs) {
1359 *actualTimeUs = timeUs;
1360 }
1361
1362 mb->release();
1363 mb = NULL;
1364
1365 return ab;
1366}
1367
Robert Shih17f6dd62014-08-20 17:00:21 -07001368void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001369 Mutex::Autolock _l(mReadBufferLock);
1370
1371 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1372 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001373 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001374 msg->setInt32("trackType", trackType);
1375 msg->post();
1376 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001377}
1378
1379void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1380 int32_t tmpType;
1381 CHECK(msg->findInt32("trackType", &tmpType));
1382 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001383 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001384 {
1385 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001386 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001387 Mutex::Autolock _l(mReadBufferLock);
1388 mPendingReadBufferTypes &= ~(1 << trackType);
1389 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001390}
1391
Andreas Huberafed0e12011-09-20 15:39:58 -07001392void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001393 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001394 // Do not read data if Widevine source is stopped
1395 if (mStopRead) {
1396 return;
1397 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001398 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001399 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001400 switch (trackType) {
1401 case MEDIA_TRACK_TYPE_VIDEO:
1402 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001403 if (mIsWidevine) {
1404 maxBuffers = 2;
1405 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001406 break;
1407 case MEDIA_TRACK_TYPE_AUDIO:
1408 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001409 if (mIsWidevine) {
1410 maxBuffers = 8;
1411 } else {
1412 maxBuffers = 64;
1413 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001414 break;
1415 case MEDIA_TRACK_TYPE_SUBTITLE:
1416 track = &mSubtitleTrack;
1417 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001418 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1419 track = &mTimedTextTrack;
1420 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001421 default:
1422 TRESPASS();
1423 }
1424
1425 if (track->mSource == NULL) {
1426 return;
1427 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001428
1429 if (actualTimeUs) {
1430 *actualTimeUs = seekTimeUs;
1431 }
1432
1433 MediaSource::ReadOptions options;
1434
1435 bool seeking = false;
1436
1437 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001438 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001439 seeking = true;
1440 }
1441
Chong Zhang42e81532014-12-01 13:44:26 -08001442 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001443 options.setNonBlocking();
1444 }
1445
Phil Burkc5cc2e22014-09-09 20:08:39 -07001446 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001447 MediaBuffer *mbuf;
1448 status_t err = track->mSource->read(&mbuf, &options);
1449
1450 options.clearSeekTo();
1451
1452 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001453 int64_t timeUs;
1454 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1455 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1456 mAudioTimeUs = timeUs;
1457 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1458 mVideoTimeUs = timeUs;
1459 }
1460
Robert Shih3423bbd2014-07-16 15:47:09 -07001461 // formatChange && seeking: track whose source is changed during selection
1462 // formatChange && !seeking: track whose source is not changed during selection
1463 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001464 if ((seeking || formatChange)
1465 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1466 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001467 ATSParser::DiscontinuityType type = (formatChange && seeking)
1468 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1469 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001470 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001471 }
1472
Wei Jia474d7c72014-12-04 15:12:13 -08001473 sp<ABuffer> buffer = mediaBufferToABuffer(
1474 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001475 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001476 formatChange = false;
1477 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001478 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001479 } else if (err == WOULD_BLOCK) {
1480 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001481 } else if (err == INFO_FORMAT_CHANGED) {
1482#if 0
1483 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001484 ATSParser::DISCONTINUITY_FORMATCHANGE,
1485 NULL,
1486 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001487#endif
1488 } else {
1489 track->mPackets->signalEOS(err);
1490 break;
1491 }
1492 }
1493}
1494
Andreas Huberafed0e12011-09-20 15:39:58 -07001495} // namespace android