blob: ccc11dfd7fb610ac4b9f1a59d10ea491f00bd677 [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 = "";
Robert Shihf3eb8262015-09-02 14:02:47 -0700471 {
472 sp<DataSource> dataSource = mDataSource;
473 sp<NuCachedSource2> cachedSource = mCachedSource;
474 sp<DataSource> httpSource = mHttpSource;
475 {
476 Mutex::Autolock _l(mDisconnectLock);
477 mDataSource.clear();
Wei Jia224858e2015-10-19 16:14:14 -0700478 mDrmManagerClient = NULL;
Robert Shihf3eb8262015-09-02 14:02:47 -0700479 mCachedSource.clear();
480 mHttpSource.clear();
481 }
482 }
Lajos Molnar68fca632015-03-31 10:06:48 -0700483 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700484
485 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700486 }
487 notifyPrepared(err);
488}
489
490status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
491 CHECK(mDataSource != NULL);
492
493 if (mCachedSource == NULL) {
494 // no prefill if the data source is not cached
495 return OK;
496 }
497
498 // We're not doing this for streams that appear to be audio-only
499 // streams to ensure that even low bandwidth streams start
500 // playing back fairly instantly.
501 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
502 return OK;
503 }
504
505 // We're going to prefill the cache before trying to instantiate
506 // the extractor below, as the latter is an operation that otherwise
507 // could block on the datasource for a significant amount of time.
508 // During that time we'd be unable to abort the preparation phase
509 // without this prefill.
510
511 // Initially make sure we have at least 192 KB for the sniff
512 // to complete without blocking.
513 static const size_t kMinBytesForSniffing = 192 * 1024;
514 static const size_t kDefaultMetaSize = 200000;
515
516 status_t finalStatus;
517
518 size_t cachedDataRemaining =
519 mCachedSource->approxDataRemaining(&finalStatus);
520
521 if (finalStatus != OK || (mMetaDataSize >= 0
522 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
523 ALOGV("stop caching, status %d, "
524 "metaDataSize %lld, cachedDataRemaining %zu",
525 finalStatus, mMetaDataSize, cachedDataRemaining);
526 return OK;
527 }
528
529 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
530
531 if (mMetaDataSize < 0
532 && cachedDataRemaining >= kMinBytesForSniffing) {
533 String8 tmp;
534 float confidence;
535 sp<AMessage> meta;
536 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
537 return UNKNOWN_ERROR;
538 }
539
540 // We successfully identified the file's extractor to
541 // be, remember this mime type so we don't have to
542 // sniff it again when we call MediaExtractor::Create()
543 mSniffedMIME = tmp.string();
544
545 if (meta == NULL
546 || !meta->findInt64("meta-data-size",
547 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
548 mMetaDataSize = kDefaultMetaSize;
549 }
550
551 if (mMetaDataSize < 0ll) {
552 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
553 return UNKNOWN_ERROR;
554 }
555 }
556
557 return -EAGAIN;
558}
559
Andreas Huberafed0e12011-09-20 15:39:58 -0700560void NuPlayer::GenericSource::start() {
561 ALOGI("start");
562
Andy Hung2abde2c2014-09-30 14:40:32 -0700563 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700564 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700565 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700566 }
567
568 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700569 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700570 }
Ronghua Wu80276872014-08-28 15:50:29 -0700571
572 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
573 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800574
575 (new AMessage(kWhatStart, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700576}
577
578void NuPlayer::GenericSource::stop() {
579 // nothing to do, just account for DRM playback status
580 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
581 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800582 if (mIsWidevine || mIsSecure) {
583 // For widevine or secure sources we need to prevent any further reads.
Andy Hung2abde2c2014-09-30 14:40:32 -0700584 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
585 sp<AMessage> response;
586 (void) msg->postAndAwaitResponse(&response);
587 }
Ronghua Wu80276872014-08-28 15:50:29 -0700588}
589
590void NuPlayer::GenericSource::pause() {
591 // nothing to do, just account for DRM playback status
592 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
593 mStarted = false;
594}
595
596void NuPlayer::GenericSource::resume() {
597 // nothing to do, just account for DRM playback status
598 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
599 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800600
601 (new AMessage(kWhatResume, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700602}
603
Chong Zhang48296b72014-09-14 14:28:45 -0700604void NuPlayer::GenericSource::disconnect() {
Robert Shihf3eb8262015-09-02 14:02:47 -0700605 sp<DataSource> dataSource, httpSource;
606 {
607 Mutex::Autolock _l(mDisconnectLock);
608 dataSource = mDataSource;
609 httpSource = mHttpSource;
610 }
611
612 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700613 // disconnect data source
Robert Shihf3eb8262015-09-02 14:02:47 -0700614 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
615 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700616 }
Robert Shihf3eb8262015-09-02 14:02:47 -0700617 } else if (httpSource != NULL) {
618 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700619 }
620}
621
Ronghua Wu80276872014-08-28 15:50:29 -0700622void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
623 if (mDecryptHandle != NULL) {
624 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
625 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700626 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
627 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700628}
629
630status_t NuPlayer::GenericSource::feedMoreTSData() {
631 return OK;
632}
633
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700634void NuPlayer::GenericSource::schedulePollBuffering() {
635 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
636 msg->setInt32("generation", mPollBufferingGeneration);
637 msg->post(1000000ll);
638}
639
640void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800641 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700642 ++mPollBufferingGeneration;
643}
644
Chong Zhangefbb6192015-01-30 17:13:27 -0800645void NuPlayer::GenericSource::restartPollBuffering() {
646 if (mIsStreaming) {
647 cancelPollBuffering();
648 onPollBuffering();
649 }
650}
651
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700652void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800653 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
654
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700655 sp<AMessage> msg = dupNotify();
656 msg->setInt32("what", kWhatBufferingUpdate);
657 msg->setInt32("percentage", percentage);
658 msg->post();
659}
660
Chong Zhangefbb6192015-01-30 17:13:27 -0800661void NuPlayer::GenericSource::startBufferingIfNecessary() {
662 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
663 mPrepareBuffering, mBuffering);
664
665 if (mPrepareBuffering) {
666 return;
667 }
668
669 if (!mBuffering) {
670 mBuffering = true;
671
672 ensureCacheIsFetching();
673 sendCacheStats();
674
675 sp<AMessage> notify = dupNotify();
676 notify->setInt32("what", kWhatPauseOnBufferingStart);
677 notify->post();
678 }
679}
680
681void NuPlayer::GenericSource::stopBufferingIfNecessary() {
682 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
683 mPrepareBuffering, mBuffering);
684
685 if (mPrepareBuffering) {
686 mPrepareBuffering = false;
687 notifyPrepared();
688 return;
689 }
690
691 if (mBuffering) {
692 mBuffering = false;
693
694 sendCacheStats();
695
696 sp<AMessage> notify = dupNotify();
697 notify->setInt32("what", kWhatResumeOnBufferingEnd);
698 notify->post();
699 }
700}
701
702void NuPlayer::GenericSource::sendCacheStats() {
703 int32_t kbps = 0;
704 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700705
706 if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800707 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
708 } else if (mWVMExtractor != NULL) {
709 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
710 }
711
712 if (err == OK) {
713 sp<AMessage> notify = dupNotify();
714 notify->setInt32("what", kWhatCacheStats);
715 notify->setInt32("bandwidth", kbps);
716 notify->post();
717 }
718}
719
720void NuPlayer::GenericSource::ensureCacheIsFetching() {
721 if (mCachedSource != NULL) {
722 mCachedSource->resumeFetchingIfNecessary();
723 }
724}
725
726void NuPlayer::GenericSource::onPollBuffering() {
727 status_t finalStatus = UNKNOWN_ERROR;
728 int64_t cachedDurationUs = -1ll;
729 ssize_t cachedDataRemaining = -1;
730
731 if (mCachedSource != NULL) {
732 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700733 mCachedSource->approxDataRemaining(&finalStatus);
734
735 if (finalStatus == OK) {
736 off64_t size;
737 int64_t bitrate = 0ll;
738 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
739 bitrate = size * 8000000ll / mDurationUs;
740 } else if (mBitrate > 0) {
741 bitrate = mBitrate;
742 }
743 if (bitrate > 0) {
744 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
745 }
746 }
747 } else if (mWVMExtractor != NULL) {
748 cachedDurationUs
749 = mWVMExtractor->getCachedDurationUs(&finalStatus);
750 }
751
Chong Zhangefbb6192015-01-30 17:13:27 -0800752 if (finalStatus != OK) {
753 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
754
755 if (finalStatus == ERROR_END_OF_STREAM) {
756 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700757 }
758
Chong Zhangefbb6192015-01-30 17:13:27 -0800759 stopBufferingIfNecessary();
760 return;
761 } else if (cachedDurationUs >= 0ll) {
762 if (mDurationUs > 0ll) {
763 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
764 int percentage = 100.0 * cachedPosUs / mDurationUs;
765 if (percentage > 100) {
766 percentage = 100;
767 }
768
769 notifyBufferingUpdate(percentage);
770 }
771
772 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
773 cachedDurationUs / 1000000.0f);
774
775 if (cachedDurationUs < kLowWaterMarkUs) {
776 startBufferingIfNecessary();
777 } else if (cachedDurationUs > kHighWaterMarkUs) {
778 stopBufferingIfNecessary();
779 }
780 } else if (cachedDataRemaining >= 0) {
781 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
782 cachedDataRemaining);
783
784 if (cachedDataRemaining < kLowWaterMarkBytes) {
785 startBufferingIfNecessary();
786 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
787 stopBufferingIfNecessary();
788 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700789 }
790
791 schedulePollBuffering();
792}
793
Robert Shih3423bbd2014-07-16 15:47:09 -0700794void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
795 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700796 case kWhatPrepareAsync:
797 {
798 onPrepareAsync();
799 break;
800 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700801 case kWhatFetchSubtitleData:
802 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700803 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
804 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
805 break;
806 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700807
Lajos Molnare26940f2014-07-31 10:31:26 -0700808 case kWhatFetchTimedTextData:
809 {
810 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
811 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700812 break;
813 }
814
815 case kWhatSendSubtitleData:
816 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700817 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
818 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
819 break;
820 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700821
Lajos Molnare26940f2014-07-31 10:31:26 -0700822 case kWhatSendTimedTextData:
823 {
824 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
825 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700826 break;
827 }
828
829 case kWhatChangeAVSource:
830 {
831 int32_t trackIndex;
832 CHECK(msg->findInt32("trackIndex", &trackIndex));
833 const sp<MediaSource> source = mSources.itemAt(trackIndex);
834
835 Track* track;
836 const char *mime;
837 media_track_type trackType, counterpartType;
838 sp<MetaData> meta = source->getFormat();
839 meta->findCString(kKeyMIMEType, &mime);
840 if (!strncasecmp(mime, "audio/", 6)) {
841 track = &mAudioTrack;
842 trackType = MEDIA_TRACK_TYPE_AUDIO;
843 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
844 } else {
845 CHECK(!strncasecmp(mime, "video/", 6));
846 track = &mVideoTrack;
847 trackType = MEDIA_TRACK_TYPE_VIDEO;
848 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
849 }
850
851
852 if (track->mSource != NULL) {
853 track->mSource->stop();
854 }
855 track->mSource = source;
856 track->mSource->start();
857 track->mIndex = trackIndex;
858
Robert Shih3423bbd2014-07-16 15:47:09 -0700859 int64_t timeUs, actualTimeUs;
860 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800861 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
862 timeUs = mAudioLastDequeueTimeUs;
863 } else {
864 timeUs = mVideoLastDequeueTimeUs;
865 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700866 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
867 readBuffer(counterpartType, -1, NULL, formatChange);
868 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
869
870 break;
871 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800872
873 case kWhatStart:
874 case kWhatResume:
875 {
876 restartPollBuffering();
877 break;
878 }
879
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700880 case kWhatPollBuffering:
881 {
882 int32_t generation;
883 CHECK(msg->findInt32("generation", &generation));
884 if (generation == mPollBufferingGeneration) {
885 onPollBuffering();
886 }
887 break;
888 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700889
890 case kWhatGetFormat:
891 {
892 onGetFormatMeta(msg);
893 break;
894 }
895
896 case kWhatGetSelectedTrack:
897 {
898 onGetSelectedTrack(msg);
899 break;
900 }
901
902 case kWhatSelectTrack:
903 {
904 onSelectTrack(msg);
905 break;
906 }
907
908 case kWhatSeek:
909 {
910 onSeek(msg);
911 break;
912 }
913
914 case kWhatReadBuffer:
915 {
916 onReadBuffer(msg);
917 break;
918 }
919
Lajos Molnar68fca632015-03-31 10:06:48 -0700920 case kWhatSecureDecodersInstantiated:
921 {
922 int32_t err;
923 CHECK(msg->findInt32("err", &err));
924 onSecureDecodersInstantiated(err);
925 break;
926 }
927
Andy Hung2abde2c2014-09-30 14:40:32 -0700928 case kWhatStopWidevine:
929 {
930 // mStopRead is only used for Widevine to prevent the video source
931 // from being read while the associated video decoder is shutting down.
932 mStopRead = true;
933 if (mVideoTrack.mSource != NULL) {
934 mVideoTrack.mPackets->clear();
935 }
936 sp<AMessage> response = new AMessage;
937 uint32_t replyID;
938 CHECK(msg->senderAwaitsResponse(&replyID));
939 response->postReply(replyID);
940 break;
941 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700942 default:
943 Source::onMessageReceived(msg);
944 break;
945 }
946}
947
Lajos Molnare26940f2014-07-31 10:31:26 -0700948void NuPlayer::GenericSource::fetchTextData(
949 uint32_t sendWhat,
950 media_track_type type,
951 int32_t curGen,
952 sp<AnotherPacketSource> packets,
953 sp<AMessage> msg) {
954 int32_t msgGeneration;
955 CHECK(msg->findInt32("generation", &msgGeneration));
956 if (msgGeneration != curGen) {
957 // stale
958 return;
959 }
960
961 int32_t avail;
962 if (packets->hasBufferAvailable(&avail)) {
963 return;
964 }
965
966 int64_t timeUs;
967 CHECK(msg->findInt64("timeUs", &timeUs));
968
969 int64_t subTimeUs;
970 readBuffer(type, timeUs, &subTimeUs);
971
972 int64_t delayUs = subTimeUs - timeUs;
973 if (msg->what() == kWhatFetchSubtitleData) {
974 const int64_t oneSecUs = 1000000ll;
975 delayUs -= oneSecUs;
976 }
977 sp<AMessage> msg2 = new AMessage(sendWhat, id());
978 msg2->setInt32("generation", msgGeneration);
979 msg2->post(delayUs < 0 ? 0 : delayUs);
980}
981
982void NuPlayer::GenericSource::sendTextData(
983 uint32_t what,
984 media_track_type type,
985 int32_t curGen,
986 sp<AnotherPacketSource> packets,
987 sp<AMessage> msg) {
988 int32_t msgGeneration;
989 CHECK(msg->findInt32("generation", &msgGeneration));
990 if (msgGeneration != curGen) {
991 // stale
992 return;
993 }
994
995 int64_t subTimeUs;
996 if (packets->nextBufferTime(&subTimeUs) != OK) {
997 return;
998 }
999
1000 int64_t nextSubTimeUs;
1001 readBuffer(type, -1, &nextSubTimeUs);
1002
1003 sp<ABuffer> buffer;
1004 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
1005 if (dequeueStatus == OK) {
1006 sp<AMessage> notify = dupNotify();
1007 notify->setInt32("what", what);
1008 notify->setBuffer("buffer", buffer);
1009 notify->post();
1010
1011 const int64_t delayUs = nextSubTimeUs - subTimeUs;
1012 msg->post(delayUs < 0 ? 0 : delayUs);
1013 }
1014}
1015
Andreas Huber84066782011-08-16 09:34:26 -07001016sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001017 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
1018 msg->setInt32("audio", audio);
1019
1020 sp<AMessage> response;
1021 void *format;
1022 status_t err = msg->postAndAwaitResponse(&response);
1023 if (err == OK && response != NULL) {
1024 CHECK(response->findPointer("format", &format));
1025 return (MetaData *)format;
1026 } else {
1027 return NULL;
1028 }
1029}
1030
1031void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1032 int32_t audio;
1033 CHECK(msg->findInt32("audio", &audio));
1034
1035 sp<AMessage> response = new AMessage;
1036 sp<MetaData> format = doGetFormatMeta(audio);
1037 response->setPointer("format", format.get());
1038
1039 uint32_t replyID;
1040 CHECK(msg->senderAwaitsResponse(&replyID));
1041 response->postReply(replyID);
1042}
1043
1044sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001045 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1046
1047 if (source == NULL) {
1048 return NULL;
1049 }
1050
1051 return source->getFormat();
1052}
1053
1054status_t NuPlayer::GenericSource::dequeueAccessUnit(
1055 bool audio, sp<ABuffer> *accessUnit) {
1056 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1057
1058 if (track->mSource == NULL) {
1059 return -EWOULDBLOCK;
1060 }
1061
Lajos Molnarcc227032014-07-17 15:33:06 -07001062 if (mIsWidevine && !audio) {
1063 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001064 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001065 }
1066
Andreas Huberafed0e12011-09-20 15:39:58 -07001067 status_t finalResult;
1068 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001069 if (finalResult == OK) {
1070 postReadBuffer(
1071 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1072 return -EWOULDBLOCK;
1073 }
1074 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001075 }
1076
1077 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1078
Robert Shih3423bbd2014-07-16 15:47:09 -07001079 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001080 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001081 }
1082
Robert Shih3423bbd2014-07-16 15:47:09 -07001083 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001084 if (mSubtitleTrack.mSource != NULL) {
1085 mSubtitleTrack.mPackets->clear();
1086 mFetchSubtitleDataGeneration++;
1087 }
1088 if (mTimedTextTrack.mSource != NULL) {
1089 mTimedTextTrack.mPackets->clear();
1090 mFetchTimedTextDataGeneration++;
1091 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001092 return result;
1093 }
1094
1095 int64_t timeUs;
1096 status_t eosResult; // ignored
1097 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001098 if (audio) {
1099 mAudioLastDequeueTimeUs = timeUs;
1100 } else {
1101 mVideoLastDequeueTimeUs = timeUs;
1102 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001103
1104 if (mSubtitleTrack.mSource != NULL
1105 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001106 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1107 msg->setInt64("timeUs", timeUs);
1108 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1109 msg->post();
1110 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001111
Lajos Molnare26940f2014-07-31 10:31:26 -07001112 if (mTimedTextTrack.mSource != NULL
1113 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1114 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1115 msg->setInt64("timeUs", timeUs);
1116 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1117 msg->post();
1118 }
1119
Andreas Huberafed0e12011-09-20 15:39:58 -07001120 return result;
1121}
1122
1123status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1124 *durationUs = mDurationUs;
1125 return OK;
1126}
1127
Robert Shihdd235722014-06-12 14:49:23 -07001128size_t NuPlayer::GenericSource::getTrackCount() const {
1129 return mSources.size();
1130}
1131
1132sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1133 size_t trackCount = mSources.size();
1134 if (trackIndex >= trackCount) {
1135 return NULL;
1136 }
1137
1138 sp<AMessage> format = new AMessage();
1139 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1140
1141 const char *mime;
1142 CHECK(meta->findCString(kKeyMIMEType, &mime));
1143
1144 int32_t trackType;
1145 if (!strncasecmp(mime, "video/", 6)) {
1146 trackType = MEDIA_TRACK_TYPE_VIDEO;
1147 } else if (!strncasecmp(mime, "audio/", 6)) {
1148 trackType = MEDIA_TRACK_TYPE_AUDIO;
1149 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1150 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1151 } else {
1152 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1153 }
1154 format->setInt32("type", trackType);
1155
1156 const char *lang;
1157 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1158 lang = "und";
1159 }
1160 format->setString("language", lang);
1161
1162 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1163 format->setString("mime", mime);
1164
1165 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1166 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1167 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1168 meta->findInt32(kKeyTrackIsForced, &isForced);
1169
1170 format->setInt32("auto", !!isAutoselect);
1171 format->setInt32("default", !!isDefault);
1172 format->setInt32("forced", !!isForced);
1173 }
1174
1175 return format;
1176}
1177
Lajos Molnare26940f2014-07-31 10:31:26 -07001178ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -07001179 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1180 msg->setInt32("type", type);
1181
1182 sp<AMessage> response;
1183 int32_t index;
1184 status_t err = msg->postAndAwaitResponse(&response);
1185 if (err == OK && response != NULL) {
1186 CHECK(response->findInt32("index", &index));
1187 return index;
1188 } else {
1189 return -1;
1190 }
1191}
1192
1193void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1194 int32_t tmpType;
1195 CHECK(msg->findInt32("type", &tmpType));
1196 media_track_type type = (media_track_type)tmpType;
1197
1198 sp<AMessage> response = new AMessage;
1199 ssize_t index = doGetSelectedTrack(type);
1200 response->setInt32("index", index);
1201
1202 uint32_t replyID;
1203 CHECK(msg->senderAwaitsResponse(&replyID));
1204 response->postReply(replyID);
1205}
1206
1207ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001208 const Track *track = NULL;
1209 switch (type) {
1210 case MEDIA_TRACK_TYPE_VIDEO:
1211 track = &mVideoTrack;
1212 break;
1213 case MEDIA_TRACK_TYPE_AUDIO:
1214 track = &mAudioTrack;
1215 break;
1216 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1217 track = &mTimedTextTrack;
1218 break;
1219 case MEDIA_TRACK_TYPE_SUBTITLE:
1220 track = &mSubtitleTrack;
1221 break;
1222 default:
1223 break;
1224 }
1225
1226 if (track != NULL && track->mSource != NULL) {
1227 return track->mIndex;
1228 }
1229
1230 return -1;
1231}
1232
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001233status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001234 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001235 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1236 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001237 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001238 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001239
1240 sp<AMessage> response;
1241 status_t err = msg->postAndAwaitResponse(&response);
1242 if (err == OK && response != NULL) {
1243 CHECK(response->findInt32("err", &err));
1244 }
1245
1246 return err;
1247}
1248
1249void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1250 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001251 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001252 CHECK(msg->findInt32("trackIndex", &trackIndex));
1253 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001254 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001255
1256 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001257 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001258 response->setInt32("err", err);
1259
1260 uint32_t replyID;
1261 CHECK(msg->senderAwaitsResponse(&replyID));
1262 response->postReply(replyID);
1263}
1264
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001265status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001266 if (trackIndex >= mSources.size()) {
1267 return BAD_INDEX;
1268 }
1269
1270 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001271 Track* track = NULL;
1272 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1273 track = &mSubtitleTrack;
1274 mFetchSubtitleDataGeneration++;
1275 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1276 track = &mTimedTextTrack;
1277 mFetchTimedTextDataGeneration++;
1278 }
1279 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 return INVALID_OPERATION;
1281 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001282 track->mSource->stop();
1283 track->mSource = NULL;
1284 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001285 return OK;
1286 }
1287
1288 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1289 sp<MetaData> meta = source->getFormat();
1290 const char *mime;
1291 CHECK(meta->findCString(kKeyMIMEType, &mime));
1292 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001293 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1294 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1295 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001296 return OK;
1297 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001298 track->mIndex = trackIndex;
1299 if (track->mSource != NULL) {
1300 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001301 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001302 track->mSource = mSources.itemAt(trackIndex);
1303 track->mSource->start();
1304 if (track->mPackets == NULL) {
1305 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001306 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001307 track->mPackets->clear();
1308 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001309
1310 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001311
1312 if (isSubtitle) {
1313 mFetchSubtitleDataGeneration++;
1314 } else {
1315 mFetchTimedTextDataGeneration++;
1316 }
1317
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001318 status_t eosResult; // ignored
1319 if (mSubtitleTrack.mSource != NULL
1320 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1321 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1322 msg->setInt64("timeUs", timeUs);
1323 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1324 msg->post();
1325 }
1326
1327 if (mTimedTextTrack.mSource != NULL
1328 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1329 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1330 msg->setInt64("timeUs", timeUs);
1331 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1332 msg->post();
1333 }
1334
Robert Shih3423bbd2014-07-16 15:47:09 -07001335 return OK;
1336 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1337 bool audio = !strncasecmp(mime, "audio/", 6);
1338 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1339 if (track->mSource != NULL && track->mIndex == trackIndex) {
1340 return OK;
1341 }
1342
1343 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1344 msg->setInt32("trackIndex", trackIndex);
1345 msg->post();
1346 return OK;
1347 }
1348
1349 return INVALID_OPERATION;
1350}
1351
Andreas Huberafed0e12011-09-20 15:39:58 -07001352status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001353 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1354 msg->setInt64("seekTimeUs", seekTimeUs);
1355
1356 sp<AMessage> response;
1357 status_t err = msg->postAndAwaitResponse(&response);
1358 if (err == OK && response != NULL) {
1359 CHECK(response->findInt32("err", &err));
1360 }
1361
1362 return err;
1363}
1364
1365void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1366 int64_t seekTimeUs;
1367 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1368
1369 sp<AMessage> response = new AMessage;
1370 status_t err = doSeek(seekTimeUs);
1371 response->setInt32("err", err);
1372
1373 uint32_t replyID;
1374 CHECK(msg->senderAwaitsResponse(&replyID));
1375 response->postReply(replyID);
1376}
1377
1378status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001379 // If the Widevine source is stopped, do not attempt to read any
1380 // more buffers.
1381 if (mStopRead) {
1382 return INVALID_OPERATION;
1383 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001384 if (mVideoTrack.mSource != NULL) {
1385 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001386 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001387
1388 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001389 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001390 }
1391
1392 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001393 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001394 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001395 }
1396
Ronghua Wu80276872014-08-28 15:50:29 -07001397 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1398 if (!mStarted) {
1399 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1400 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001401
1402 // If currently buffering, post kWhatBufferingEnd first, so that
1403 // NuPlayer resumes. Otherwise, if cache hits high watermark
1404 // before new polling happens, no one will resume the playback.
1405 stopBufferingIfNecessary();
1406 restartPollBuffering();
1407
Andreas Huberafed0e12011-09-20 15:39:58 -07001408 return OK;
1409}
1410
Robert Shih3423bbd2014-07-16 15:47:09 -07001411sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1412 MediaBuffer* mb,
1413 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001414 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001415 int64_t *actualTimeUs) {
1416 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1417 size_t outLength = mb->range_length();
1418
1419 if (audio && mAudioIsVorbis) {
1420 outLength += sizeof(int32_t);
1421 }
1422
1423 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001424 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001425 // data is already provided in the buffer
1426 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001427 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001428 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001429 } else {
1430 ab = new ABuffer(outLength);
1431 memcpy(ab->data(),
1432 (const uint8_t *)mb->data() + mb->range_offset(),
1433 mb->range_length());
1434 }
1435
1436 if (audio && mAudioIsVorbis) {
1437 int32_t numPageSamples;
1438 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1439 numPageSamples = -1;
1440 }
1441
1442 uint8_t* abEnd = ab->data() + mb->range_length();
1443 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1444 }
1445
Lajos Molnare26940f2014-07-31 10:31:26 -07001446 sp<AMessage> meta = ab->meta();
1447
Robert Shih3423bbd2014-07-16 15:47:09 -07001448 int64_t timeUs;
1449 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001450 meta->setInt64("timeUs", timeUs);
1451
Wei Jia474d7c72014-12-04 15:12:13 -08001452#if 0
1453 // Temporarily disable pre-roll till we have a full solution to handle
1454 // both single seek and continous seek gracefully.
1455 if (seekTimeUs > timeUs) {
1456 sp<AMessage> extra = new AMessage;
1457 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1458 meta->setMessage("extra", extra);
1459 }
1460#endif
1461
Lajos Molnare26940f2014-07-31 10:31:26 -07001462 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1463 const char *mime;
1464 CHECK(mTimedTextTrack.mSource != NULL
1465 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1466 meta->setString("mime", mime);
1467 }
1468
Robert Shih3423bbd2014-07-16 15:47:09 -07001469 int64_t durationUs;
1470 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1471 meta->setInt64("durationUs", durationUs);
1472 }
1473
1474 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1475 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1476 }
1477
1478 if (actualTimeUs) {
1479 *actualTimeUs = timeUs;
1480 }
1481
1482 mb->release();
1483 mb = NULL;
1484
1485 return ab;
1486}
1487
Robert Shih17f6dd62014-08-20 17:00:21 -07001488void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001489 Mutex::Autolock _l(mReadBufferLock);
1490
1491 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1492 mPendingReadBufferTypes |= (1 << trackType);
1493 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1494 msg->setInt32("trackType", trackType);
1495 msg->post();
1496 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001497}
1498
1499void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1500 int32_t tmpType;
1501 CHECK(msg->findInt32("trackType", &tmpType));
1502 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001503 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001504 {
1505 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001506 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001507 Mutex::Autolock _l(mReadBufferLock);
1508 mPendingReadBufferTypes &= ~(1 << trackType);
1509 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001510}
1511
Andreas Huberafed0e12011-09-20 15:39:58 -07001512void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001513 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001514 // Do not read data if Widevine source is stopped
1515 if (mStopRead) {
1516 return;
1517 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001518 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001519 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001520 switch (trackType) {
1521 case MEDIA_TRACK_TYPE_VIDEO:
1522 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001523 if (mIsWidevine) {
1524 maxBuffers = 2;
1525 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001526 break;
1527 case MEDIA_TRACK_TYPE_AUDIO:
1528 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001529 if (mIsWidevine) {
1530 maxBuffers = 8;
1531 } else {
1532 maxBuffers = 64;
1533 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001534 break;
1535 case MEDIA_TRACK_TYPE_SUBTITLE:
1536 track = &mSubtitleTrack;
1537 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001538 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1539 track = &mTimedTextTrack;
1540 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001541 default:
1542 TRESPASS();
1543 }
1544
1545 if (track->mSource == NULL) {
1546 return;
1547 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001548
1549 if (actualTimeUs) {
1550 *actualTimeUs = seekTimeUs;
1551 }
1552
1553 MediaSource::ReadOptions options;
1554
1555 bool seeking = false;
1556
1557 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001558 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001559 seeking = true;
1560 }
1561
Chong Zhang42e81532014-12-01 13:44:26 -08001562 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001563 options.setNonBlocking();
1564 }
1565
Phil Burkc5cc2e22014-09-09 20:08:39 -07001566 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001567 MediaBuffer *mbuf;
1568 status_t err = track->mSource->read(&mbuf, &options);
1569
1570 options.clearSeekTo();
1571
1572 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001573 int64_t timeUs;
1574 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1575 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1576 mAudioTimeUs = timeUs;
1577 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1578 mVideoTimeUs = timeUs;
1579 }
1580
Robert Shih3423bbd2014-07-16 15:47:09 -07001581 // formatChange && seeking: track whose source is changed during selection
1582 // formatChange && !seeking: track whose source is not changed during selection
1583 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001584 if ((seeking || formatChange)
1585 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1586 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001587 ATSParser::DiscontinuityType type = (formatChange && seeking)
1588 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1589 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001590 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001591 }
1592
Wei Jia474d7c72014-12-04 15:12:13 -08001593 sp<ABuffer> buffer = mediaBufferToABuffer(
1594 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001595 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001596 formatChange = false;
1597 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001598 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001599 } else if (err == WOULD_BLOCK) {
1600 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001601 } else if (err == INFO_FORMAT_CHANGED) {
1602#if 0
1603 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001604 ATSParser::DISCONTINUITY_FORMATCHANGE,
1605 NULL,
1606 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001607#endif
1608 } else {
1609 track->mPackets->signalEOS(err);
1610 break;
1611 }
1612 }
1613}
1614
Andreas Huberafed0e12011-09-20 15:39:58 -07001615} // namespace android