blob: cdd267071cb600e848f8b457c95742de4fda9e3d [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 Jia09c291c2015-10-22 11:35:04 -0700478 mDecryptHandle = NULL;
Wei Jia224858e2015-10-19 16:14:14 -0700479 mDrmManagerClient = NULL;
Robert Shihf3eb8262015-09-02 14:02:47 -0700480 mCachedSource.clear();
481 mHttpSource.clear();
482 }
483 }
Lajos Molnar68fca632015-03-31 10:06:48 -0700484 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700485
486 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700487 }
488 notifyPrepared(err);
489}
490
491status_t NuPlayer::GenericSource::prefillCacheIfNecessary() {
492 CHECK(mDataSource != NULL);
493
494 if (mCachedSource == NULL) {
495 // no prefill if the data source is not cached
496 return OK;
497 }
498
499 // We're not doing this for streams that appear to be audio-only
500 // streams to ensure that even low bandwidth streams start
501 // playing back fairly instantly.
502 if (!strncasecmp(mContentType.string(), "audio/", 6)) {
503 return OK;
504 }
505
506 // We're going to prefill the cache before trying to instantiate
507 // the extractor below, as the latter is an operation that otherwise
508 // could block on the datasource for a significant amount of time.
509 // During that time we'd be unable to abort the preparation phase
510 // without this prefill.
511
512 // Initially make sure we have at least 192 KB for the sniff
513 // to complete without blocking.
514 static const size_t kMinBytesForSniffing = 192 * 1024;
515 static const size_t kDefaultMetaSize = 200000;
516
517 status_t finalStatus;
518
519 size_t cachedDataRemaining =
520 mCachedSource->approxDataRemaining(&finalStatus);
521
522 if (finalStatus != OK || (mMetaDataSize >= 0
523 && (off64_t)cachedDataRemaining >= mMetaDataSize)) {
524 ALOGV("stop caching, status %d, "
525 "metaDataSize %lld, cachedDataRemaining %zu",
526 finalStatus, mMetaDataSize, cachedDataRemaining);
527 return OK;
528 }
529
530 ALOGV("now cached %zu bytes of data", cachedDataRemaining);
531
532 if (mMetaDataSize < 0
533 && cachedDataRemaining >= kMinBytesForSniffing) {
534 String8 tmp;
535 float confidence;
536 sp<AMessage> meta;
537 if (!mCachedSource->sniff(&tmp, &confidence, &meta)) {
538 return UNKNOWN_ERROR;
539 }
540
541 // We successfully identified the file's extractor to
542 // be, remember this mime type so we don't have to
543 // sniff it again when we call MediaExtractor::Create()
544 mSniffedMIME = tmp.string();
545
546 if (meta == NULL
547 || !meta->findInt64("meta-data-size",
548 reinterpret_cast<int64_t*>(&mMetaDataSize))) {
549 mMetaDataSize = kDefaultMetaSize;
550 }
551
552 if (mMetaDataSize < 0ll) {
553 ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize);
554 return UNKNOWN_ERROR;
555 }
556 }
557
558 return -EAGAIN;
559}
560
Andreas Huberafed0e12011-09-20 15:39:58 -0700561void NuPlayer::GenericSource::start() {
562 ALOGI("start");
563
Andy Hung2abde2c2014-09-30 14:40:32 -0700564 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700565 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700566 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700567 }
568
569 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700570 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700571 }
Ronghua Wu80276872014-08-28 15:50:29 -0700572
573 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
574 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800575
576 (new AMessage(kWhatStart, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700577}
578
579void NuPlayer::GenericSource::stop() {
580 // nothing to do, just account for DRM playback status
581 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
582 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800583 if (mIsWidevine || mIsSecure) {
584 // For widevine or secure sources we need to prevent any further reads.
Andy Hung2abde2c2014-09-30 14:40:32 -0700585 sp<AMessage> msg = new AMessage(kWhatStopWidevine, id());
586 sp<AMessage> response;
587 (void) msg->postAndAwaitResponse(&response);
588 }
Ronghua Wu80276872014-08-28 15:50:29 -0700589}
590
591void NuPlayer::GenericSource::pause() {
592 // nothing to do, just account for DRM playback status
593 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
594 mStarted = false;
595}
596
597void NuPlayer::GenericSource::resume() {
598 // nothing to do, just account for DRM playback status
599 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
600 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800601
602 (new AMessage(kWhatResume, id()))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700603}
604
Chong Zhang48296b72014-09-14 14:28:45 -0700605void NuPlayer::GenericSource::disconnect() {
Robert Shihf3eb8262015-09-02 14:02:47 -0700606 sp<DataSource> dataSource, httpSource;
607 {
608 Mutex::Autolock _l(mDisconnectLock);
609 dataSource = mDataSource;
610 httpSource = mHttpSource;
611 }
612
613 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700614 // disconnect data source
Robert Shihf3eb8262015-09-02 14:02:47 -0700615 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
616 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700617 }
Robert Shihf3eb8262015-09-02 14:02:47 -0700618 } else if (httpSource != NULL) {
619 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700620 }
621}
622
Ronghua Wu80276872014-08-28 15:50:29 -0700623void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
624 if (mDecryptHandle != NULL) {
625 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
626 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700627 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
628 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700629}
630
631status_t NuPlayer::GenericSource::feedMoreTSData() {
632 return OK;
633}
634
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700635void NuPlayer::GenericSource::schedulePollBuffering() {
636 sp<AMessage> msg = new AMessage(kWhatPollBuffering, id());
637 msg->setInt32("generation", mPollBufferingGeneration);
638 msg->post(1000000ll);
639}
640
641void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800642 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700643 ++mPollBufferingGeneration;
644}
645
Chong Zhangefbb6192015-01-30 17:13:27 -0800646void NuPlayer::GenericSource::restartPollBuffering() {
647 if (mIsStreaming) {
648 cancelPollBuffering();
649 onPollBuffering();
650 }
651}
652
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700653void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800654 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
655
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700656 sp<AMessage> msg = dupNotify();
657 msg->setInt32("what", kWhatBufferingUpdate);
658 msg->setInt32("percentage", percentage);
659 msg->post();
660}
661
Chong Zhangefbb6192015-01-30 17:13:27 -0800662void NuPlayer::GenericSource::startBufferingIfNecessary() {
663 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
664 mPrepareBuffering, mBuffering);
665
666 if (mPrepareBuffering) {
667 return;
668 }
669
670 if (!mBuffering) {
671 mBuffering = true;
672
673 ensureCacheIsFetching();
674 sendCacheStats();
675
676 sp<AMessage> notify = dupNotify();
677 notify->setInt32("what", kWhatPauseOnBufferingStart);
678 notify->post();
679 }
680}
681
682void NuPlayer::GenericSource::stopBufferingIfNecessary() {
683 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
684 mPrepareBuffering, mBuffering);
685
686 if (mPrepareBuffering) {
687 mPrepareBuffering = false;
688 notifyPrepared();
689 return;
690 }
691
692 if (mBuffering) {
693 mBuffering = false;
694
695 sendCacheStats();
696
697 sp<AMessage> notify = dupNotify();
698 notify->setInt32("what", kWhatResumeOnBufferingEnd);
699 notify->post();
700 }
701}
702
703void NuPlayer::GenericSource::sendCacheStats() {
704 int32_t kbps = 0;
705 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700706
707 if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800708 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
709 } else if (mWVMExtractor != NULL) {
710 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
711 }
712
713 if (err == OK) {
714 sp<AMessage> notify = dupNotify();
715 notify->setInt32("what", kWhatCacheStats);
716 notify->setInt32("bandwidth", kbps);
717 notify->post();
718 }
719}
720
721void NuPlayer::GenericSource::ensureCacheIsFetching() {
722 if (mCachedSource != NULL) {
723 mCachedSource->resumeFetchingIfNecessary();
724 }
725}
726
727void NuPlayer::GenericSource::onPollBuffering() {
728 status_t finalStatus = UNKNOWN_ERROR;
729 int64_t cachedDurationUs = -1ll;
730 ssize_t cachedDataRemaining = -1;
731
732 if (mCachedSource != NULL) {
733 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700734 mCachedSource->approxDataRemaining(&finalStatus);
735
736 if (finalStatus == OK) {
737 off64_t size;
738 int64_t bitrate = 0ll;
739 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
740 bitrate = size * 8000000ll / mDurationUs;
741 } else if (mBitrate > 0) {
742 bitrate = mBitrate;
743 }
744 if (bitrate > 0) {
745 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
746 }
747 }
748 } else if (mWVMExtractor != NULL) {
749 cachedDurationUs
750 = mWVMExtractor->getCachedDurationUs(&finalStatus);
751 }
752
Chong Zhangefbb6192015-01-30 17:13:27 -0800753 if (finalStatus != OK) {
754 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
755
756 if (finalStatus == ERROR_END_OF_STREAM) {
757 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700758 }
759
Chong Zhangefbb6192015-01-30 17:13:27 -0800760 stopBufferingIfNecessary();
761 return;
762 } else if (cachedDurationUs >= 0ll) {
763 if (mDurationUs > 0ll) {
764 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
765 int percentage = 100.0 * cachedPosUs / mDurationUs;
766 if (percentage > 100) {
767 percentage = 100;
768 }
769
770 notifyBufferingUpdate(percentage);
771 }
772
773 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
774 cachedDurationUs / 1000000.0f);
775
776 if (cachedDurationUs < kLowWaterMarkUs) {
777 startBufferingIfNecessary();
778 } else if (cachedDurationUs > kHighWaterMarkUs) {
779 stopBufferingIfNecessary();
780 }
781 } else if (cachedDataRemaining >= 0) {
782 ALOGV("onPollBuffering: cachedDataRemaining %d bytes",
783 cachedDataRemaining);
784
785 if (cachedDataRemaining < kLowWaterMarkBytes) {
786 startBufferingIfNecessary();
787 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
788 stopBufferingIfNecessary();
789 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700790 }
791
792 schedulePollBuffering();
793}
794
Robert Shih3423bbd2014-07-16 15:47:09 -0700795void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
796 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700797 case kWhatPrepareAsync:
798 {
799 onPrepareAsync();
800 break;
801 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700802 case kWhatFetchSubtitleData:
803 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700804 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
805 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
806 break;
807 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700808
Lajos Molnare26940f2014-07-31 10:31:26 -0700809 case kWhatFetchTimedTextData:
810 {
811 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
812 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700813 break;
814 }
815
816 case kWhatSendSubtitleData:
817 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700818 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
819 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
820 break;
821 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700822
Lajos Molnare26940f2014-07-31 10:31:26 -0700823 case kWhatSendTimedTextData:
824 {
825 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
826 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700827 break;
828 }
829
830 case kWhatChangeAVSource:
831 {
832 int32_t trackIndex;
833 CHECK(msg->findInt32("trackIndex", &trackIndex));
834 const sp<MediaSource> source = mSources.itemAt(trackIndex);
835
836 Track* track;
837 const char *mime;
838 media_track_type trackType, counterpartType;
839 sp<MetaData> meta = source->getFormat();
840 meta->findCString(kKeyMIMEType, &mime);
841 if (!strncasecmp(mime, "audio/", 6)) {
842 track = &mAudioTrack;
843 trackType = MEDIA_TRACK_TYPE_AUDIO;
844 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
845 } else {
846 CHECK(!strncasecmp(mime, "video/", 6));
847 track = &mVideoTrack;
848 trackType = MEDIA_TRACK_TYPE_VIDEO;
849 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
850 }
851
852
853 if (track->mSource != NULL) {
854 track->mSource->stop();
855 }
856 track->mSource = source;
857 track->mSource->start();
858 track->mIndex = trackIndex;
859
Robert Shih3423bbd2014-07-16 15:47:09 -0700860 int64_t timeUs, actualTimeUs;
861 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800862 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
863 timeUs = mAudioLastDequeueTimeUs;
864 } else {
865 timeUs = mVideoLastDequeueTimeUs;
866 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700867 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
868 readBuffer(counterpartType, -1, NULL, formatChange);
869 ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs);
870
871 break;
872 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800873
874 case kWhatStart:
875 case kWhatResume:
876 {
877 restartPollBuffering();
878 break;
879 }
880
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700881 case kWhatPollBuffering:
882 {
883 int32_t generation;
884 CHECK(msg->findInt32("generation", &generation));
885 if (generation == mPollBufferingGeneration) {
886 onPollBuffering();
887 }
888 break;
889 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700890
891 case kWhatGetFormat:
892 {
893 onGetFormatMeta(msg);
894 break;
895 }
896
897 case kWhatGetSelectedTrack:
898 {
899 onGetSelectedTrack(msg);
900 break;
901 }
902
903 case kWhatSelectTrack:
904 {
905 onSelectTrack(msg);
906 break;
907 }
908
909 case kWhatSeek:
910 {
911 onSeek(msg);
912 break;
913 }
914
915 case kWhatReadBuffer:
916 {
917 onReadBuffer(msg);
918 break;
919 }
920
Lajos Molnar68fca632015-03-31 10:06:48 -0700921 case kWhatSecureDecodersInstantiated:
922 {
923 int32_t err;
924 CHECK(msg->findInt32("err", &err));
925 onSecureDecodersInstantiated(err);
926 break;
927 }
928
Andy Hung2abde2c2014-09-30 14:40:32 -0700929 case kWhatStopWidevine:
930 {
931 // mStopRead is only used for Widevine to prevent the video source
932 // from being read while the associated video decoder is shutting down.
933 mStopRead = true;
934 if (mVideoTrack.mSource != NULL) {
935 mVideoTrack.mPackets->clear();
936 }
937 sp<AMessage> response = new AMessage;
938 uint32_t replyID;
939 CHECK(msg->senderAwaitsResponse(&replyID));
940 response->postReply(replyID);
941 break;
942 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700943 default:
944 Source::onMessageReceived(msg);
945 break;
946 }
947}
948
Lajos Molnare26940f2014-07-31 10:31:26 -0700949void NuPlayer::GenericSource::fetchTextData(
950 uint32_t sendWhat,
951 media_track_type type,
952 int32_t curGen,
953 sp<AnotherPacketSource> packets,
954 sp<AMessage> msg) {
955 int32_t msgGeneration;
956 CHECK(msg->findInt32("generation", &msgGeneration));
957 if (msgGeneration != curGen) {
958 // stale
959 return;
960 }
961
962 int32_t avail;
963 if (packets->hasBufferAvailable(&avail)) {
964 return;
965 }
966
967 int64_t timeUs;
968 CHECK(msg->findInt64("timeUs", &timeUs));
969
970 int64_t subTimeUs;
971 readBuffer(type, timeUs, &subTimeUs);
972
973 int64_t delayUs = subTimeUs - timeUs;
974 if (msg->what() == kWhatFetchSubtitleData) {
975 const int64_t oneSecUs = 1000000ll;
976 delayUs -= oneSecUs;
977 }
978 sp<AMessage> msg2 = new AMessage(sendWhat, id());
979 msg2->setInt32("generation", msgGeneration);
980 msg2->post(delayUs < 0 ? 0 : delayUs);
981}
982
983void NuPlayer::GenericSource::sendTextData(
984 uint32_t what,
985 media_track_type type,
986 int32_t curGen,
987 sp<AnotherPacketSource> packets,
988 sp<AMessage> msg) {
989 int32_t msgGeneration;
990 CHECK(msg->findInt32("generation", &msgGeneration));
991 if (msgGeneration != curGen) {
992 // stale
993 return;
994 }
995
996 int64_t subTimeUs;
997 if (packets->nextBufferTime(&subTimeUs) != OK) {
998 return;
999 }
1000
1001 int64_t nextSubTimeUs;
1002 readBuffer(type, -1, &nextSubTimeUs);
1003
1004 sp<ABuffer> buffer;
1005 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
1006 if (dequeueStatus == OK) {
1007 sp<AMessage> notify = dupNotify();
1008 notify->setInt32("what", what);
1009 notify->setBuffer("buffer", buffer);
1010 notify->post();
1011
1012 const int64_t delayUs = nextSubTimeUs - subTimeUs;
1013 msg->post(delayUs < 0 ? 0 : delayUs);
1014 }
1015}
1016
Andreas Huber84066782011-08-16 09:34:26 -07001017sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001018 sp<AMessage> msg = new AMessage(kWhatGetFormat, id());
1019 msg->setInt32("audio", audio);
1020
1021 sp<AMessage> response;
1022 void *format;
1023 status_t err = msg->postAndAwaitResponse(&response);
1024 if (err == OK && response != NULL) {
1025 CHECK(response->findPointer("format", &format));
1026 return (MetaData *)format;
1027 } else {
1028 return NULL;
1029 }
1030}
1031
1032void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1033 int32_t audio;
1034 CHECK(msg->findInt32("audio", &audio));
1035
1036 sp<AMessage> response = new AMessage;
1037 sp<MetaData> format = doGetFormatMeta(audio);
1038 response->setPointer("format", format.get());
1039
1040 uint32_t replyID;
1041 CHECK(msg->senderAwaitsResponse(&replyID));
1042 response->postReply(replyID);
1043}
1044
1045sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001046 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1047
1048 if (source == NULL) {
1049 return NULL;
1050 }
1051
1052 return source->getFormat();
1053}
1054
1055status_t NuPlayer::GenericSource::dequeueAccessUnit(
1056 bool audio, sp<ABuffer> *accessUnit) {
1057 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1058
1059 if (track->mSource == NULL) {
1060 return -EWOULDBLOCK;
1061 }
1062
Lajos Molnarcc227032014-07-17 15:33:06 -07001063 if (mIsWidevine && !audio) {
1064 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001065 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001066 }
1067
Andreas Huberafed0e12011-09-20 15:39:58 -07001068 status_t finalResult;
1069 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001070 if (finalResult == OK) {
1071 postReadBuffer(
1072 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1073 return -EWOULDBLOCK;
1074 }
1075 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001076 }
1077
1078 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1079
Robert Shih3423bbd2014-07-16 15:47:09 -07001080 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001081 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001082 }
1083
Robert Shih3423bbd2014-07-16 15:47:09 -07001084 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001085 if (mSubtitleTrack.mSource != NULL) {
1086 mSubtitleTrack.mPackets->clear();
1087 mFetchSubtitleDataGeneration++;
1088 }
1089 if (mTimedTextTrack.mSource != NULL) {
1090 mTimedTextTrack.mPackets->clear();
1091 mFetchTimedTextDataGeneration++;
1092 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001093 return result;
1094 }
1095
1096 int64_t timeUs;
1097 status_t eosResult; // ignored
1098 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001099 if (audio) {
1100 mAudioLastDequeueTimeUs = timeUs;
1101 } else {
1102 mVideoLastDequeueTimeUs = timeUs;
1103 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001104
1105 if (mSubtitleTrack.mSource != NULL
1106 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001107 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1108 msg->setInt64("timeUs", timeUs);
1109 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1110 msg->post();
1111 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001112
Lajos Molnare26940f2014-07-31 10:31:26 -07001113 if (mTimedTextTrack.mSource != NULL
1114 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1115 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1116 msg->setInt64("timeUs", timeUs);
1117 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1118 msg->post();
1119 }
1120
Andreas Huberafed0e12011-09-20 15:39:58 -07001121 return result;
1122}
1123
1124status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1125 *durationUs = mDurationUs;
1126 return OK;
1127}
1128
Robert Shihdd235722014-06-12 14:49:23 -07001129size_t NuPlayer::GenericSource::getTrackCount() const {
1130 return mSources.size();
1131}
1132
1133sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1134 size_t trackCount = mSources.size();
1135 if (trackIndex >= trackCount) {
1136 return NULL;
1137 }
1138
1139 sp<AMessage> format = new AMessage();
1140 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1141
1142 const char *mime;
1143 CHECK(meta->findCString(kKeyMIMEType, &mime));
1144
1145 int32_t trackType;
1146 if (!strncasecmp(mime, "video/", 6)) {
1147 trackType = MEDIA_TRACK_TYPE_VIDEO;
1148 } else if (!strncasecmp(mime, "audio/", 6)) {
1149 trackType = MEDIA_TRACK_TYPE_AUDIO;
1150 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1151 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1152 } else {
1153 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1154 }
1155 format->setInt32("type", trackType);
1156
1157 const char *lang;
1158 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1159 lang = "und";
1160 }
1161 format->setString("language", lang);
1162
1163 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1164 format->setString("mime", mime);
1165
1166 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1167 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1168 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1169 meta->findInt32(kKeyTrackIsForced, &isForced);
1170
1171 format->setInt32("auto", !!isAutoselect);
1172 format->setInt32("default", !!isDefault);
1173 format->setInt32("forced", !!isForced);
1174 }
1175
1176 return format;
1177}
1178
Lajos Molnare26940f2014-07-31 10:31:26 -07001179ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Robert Shih17f6dd62014-08-20 17:00:21 -07001180 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id());
1181 msg->setInt32("type", type);
1182
1183 sp<AMessage> response;
1184 int32_t index;
1185 status_t err = msg->postAndAwaitResponse(&response);
1186 if (err == OK && response != NULL) {
1187 CHECK(response->findInt32("index", &index));
1188 return index;
1189 } else {
1190 return -1;
1191 }
1192}
1193
1194void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1195 int32_t tmpType;
1196 CHECK(msg->findInt32("type", &tmpType));
1197 media_track_type type = (media_track_type)tmpType;
1198
1199 sp<AMessage> response = new AMessage;
1200 ssize_t index = doGetSelectedTrack(type);
1201 response->setInt32("index", index);
1202
1203 uint32_t replyID;
1204 CHECK(msg->senderAwaitsResponse(&replyID));
1205 response->postReply(replyID);
1206}
1207
1208ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001209 const Track *track = NULL;
1210 switch (type) {
1211 case MEDIA_TRACK_TYPE_VIDEO:
1212 track = &mVideoTrack;
1213 break;
1214 case MEDIA_TRACK_TYPE_AUDIO:
1215 track = &mAudioTrack;
1216 break;
1217 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1218 track = &mTimedTextTrack;
1219 break;
1220 case MEDIA_TRACK_TYPE_SUBTITLE:
1221 track = &mSubtitleTrack;
1222 break;
1223 default:
1224 break;
1225 }
1226
1227 if (track != NULL && track->mSource != NULL) {
1228 return track->mIndex;
1229 }
1230
1231 return -1;
1232}
1233
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001234status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001235 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Robert Shih17f6dd62014-08-20 17:00:21 -07001236 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1237 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001238 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001239 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001240
1241 sp<AMessage> response;
1242 status_t err = msg->postAndAwaitResponse(&response);
1243 if (err == OK && response != NULL) {
1244 CHECK(response->findInt32("err", &err));
1245 }
1246
1247 return err;
1248}
1249
1250void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1251 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001252 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001253 CHECK(msg->findInt32("trackIndex", &trackIndex));
1254 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001255 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001256
1257 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001258 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001259 response->setInt32("err", err);
1260
1261 uint32_t replyID;
1262 CHECK(msg->senderAwaitsResponse(&replyID));
1263 response->postReply(replyID);
1264}
1265
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001266status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001267 if (trackIndex >= mSources.size()) {
1268 return BAD_INDEX;
1269 }
1270
1271 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001272 Track* track = NULL;
1273 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1274 track = &mSubtitleTrack;
1275 mFetchSubtitleDataGeneration++;
1276 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1277 track = &mTimedTextTrack;
1278 mFetchTimedTextDataGeneration++;
1279 }
1280 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001281 return INVALID_OPERATION;
1282 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001283 track->mSource->stop();
1284 track->mSource = NULL;
1285 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001286 return OK;
1287 }
1288
1289 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1290 sp<MetaData> meta = source->getFormat();
1291 const char *mime;
1292 CHECK(meta->findCString(kKeyMIMEType, &mime));
1293 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001294 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1295 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1296 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001297 return OK;
1298 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001299 track->mIndex = trackIndex;
1300 if (track->mSource != NULL) {
1301 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001302 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001303 track->mSource = mSources.itemAt(trackIndex);
1304 track->mSource->start();
1305 if (track->mPackets == NULL) {
1306 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001307 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001308 track->mPackets->clear();
1309 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001310
1311 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001312
1313 if (isSubtitle) {
1314 mFetchSubtitleDataGeneration++;
1315 } else {
1316 mFetchTimedTextDataGeneration++;
1317 }
1318
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001319 status_t eosResult; // ignored
1320 if (mSubtitleTrack.mSource != NULL
1321 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
1322 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
1323 msg->setInt64("timeUs", timeUs);
1324 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1325 msg->post();
1326 }
1327
1328 if (mTimedTextTrack.mSource != NULL
1329 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
1330 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id());
1331 msg->setInt64("timeUs", timeUs);
1332 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1333 msg->post();
1334 }
1335
Robert Shih3423bbd2014-07-16 15:47:09 -07001336 return OK;
1337 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1338 bool audio = !strncasecmp(mime, "audio/", 6);
1339 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1340 if (track->mSource != NULL && track->mIndex == trackIndex) {
1341 return OK;
1342 }
1343
1344 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id());
1345 msg->setInt32("trackIndex", trackIndex);
1346 msg->post();
1347 return OK;
1348 }
1349
1350 return INVALID_OPERATION;
1351}
1352
Andreas Huberafed0e12011-09-20 15:39:58 -07001353status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001354 sp<AMessage> msg = new AMessage(kWhatSeek, id());
1355 msg->setInt64("seekTimeUs", seekTimeUs);
1356
1357 sp<AMessage> response;
1358 status_t err = msg->postAndAwaitResponse(&response);
1359 if (err == OK && response != NULL) {
1360 CHECK(response->findInt32("err", &err));
1361 }
1362
1363 return err;
1364}
1365
1366void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1367 int64_t seekTimeUs;
1368 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1369
1370 sp<AMessage> response = new AMessage;
1371 status_t err = doSeek(seekTimeUs);
1372 response->setInt32("err", err);
1373
1374 uint32_t replyID;
1375 CHECK(msg->senderAwaitsResponse(&replyID));
1376 response->postReply(replyID);
1377}
1378
1379status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001380 // If the Widevine source is stopped, do not attempt to read any
1381 // more buffers.
1382 if (mStopRead) {
1383 return INVALID_OPERATION;
1384 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001385 if (mVideoTrack.mSource != NULL) {
1386 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001387 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001388
1389 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001390 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001391 }
1392
1393 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001394 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001395 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001396 }
1397
Ronghua Wu80276872014-08-28 15:50:29 -07001398 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1399 if (!mStarted) {
1400 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1401 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001402
1403 // If currently buffering, post kWhatBufferingEnd first, so that
1404 // NuPlayer resumes. Otherwise, if cache hits high watermark
1405 // before new polling happens, no one will resume the playback.
1406 stopBufferingIfNecessary();
1407 restartPollBuffering();
1408
Andreas Huberafed0e12011-09-20 15:39:58 -07001409 return OK;
1410}
1411
Robert Shih3423bbd2014-07-16 15:47:09 -07001412sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1413 MediaBuffer* mb,
1414 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001415 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001416 int64_t *actualTimeUs) {
1417 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1418 size_t outLength = mb->range_length();
1419
1420 if (audio && mAudioIsVorbis) {
1421 outLength += sizeof(int32_t);
1422 }
1423
1424 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001425 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001426 // data is already provided in the buffer
1427 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001428 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001429 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001430 } else {
1431 ab = new ABuffer(outLength);
1432 memcpy(ab->data(),
1433 (const uint8_t *)mb->data() + mb->range_offset(),
1434 mb->range_length());
1435 }
1436
1437 if (audio && mAudioIsVorbis) {
1438 int32_t numPageSamples;
1439 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1440 numPageSamples = -1;
1441 }
1442
1443 uint8_t* abEnd = ab->data() + mb->range_length();
1444 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1445 }
1446
Lajos Molnare26940f2014-07-31 10:31:26 -07001447 sp<AMessage> meta = ab->meta();
1448
Robert Shih3423bbd2014-07-16 15:47:09 -07001449 int64_t timeUs;
1450 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001451 meta->setInt64("timeUs", timeUs);
1452
Wei Jia474d7c72014-12-04 15:12:13 -08001453#if 0
1454 // Temporarily disable pre-roll till we have a full solution to handle
1455 // both single seek and continous seek gracefully.
1456 if (seekTimeUs > timeUs) {
1457 sp<AMessage> extra = new AMessage;
1458 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1459 meta->setMessage("extra", extra);
1460 }
1461#endif
1462
Lajos Molnare26940f2014-07-31 10:31:26 -07001463 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1464 const char *mime;
1465 CHECK(mTimedTextTrack.mSource != NULL
1466 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1467 meta->setString("mime", mime);
1468 }
1469
Robert Shih3423bbd2014-07-16 15:47:09 -07001470 int64_t durationUs;
1471 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1472 meta->setInt64("durationUs", durationUs);
1473 }
1474
1475 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1476 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1477 }
1478
1479 if (actualTimeUs) {
1480 *actualTimeUs = timeUs;
1481 }
1482
1483 mb->release();
1484 mb = NULL;
1485
1486 return ab;
1487}
1488
Robert Shih17f6dd62014-08-20 17:00:21 -07001489void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001490 Mutex::Autolock _l(mReadBufferLock);
1491
1492 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1493 mPendingReadBufferTypes |= (1 << trackType);
1494 sp<AMessage> msg = new AMessage(kWhatReadBuffer, id());
1495 msg->setInt32("trackType", trackType);
1496 msg->post();
1497 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001498}
1499
1500void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1501 int32_t tmpType;
1502 CHECK(msg->findInt32("trackType", &tmpType));
1503 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001504 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001505 {
1506 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001507 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001508 Mutex::Autolock _l(mReadBufferLock);
1509 mPendingReadBufferTypes &= ~(1 << trackType);
1510 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001511}
1512
Andreas Huberafed0e12011-09-20 15:39:58 -07001513void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001514 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001515 // Do not read data if Widevine source is stopped
1516 if (mStopRead) {
1517 return;
1518 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001519 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001520 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001521 switch (trackType) {
1522 case MEDIA_TRACK_TYPE_VIDEO:
1523 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001524 if (mIsWidevine) {
1525 maxBuffers = 2;
1526 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001527 break;
1528 case MEDIA_TRACK_TYPE_AUDIO:
1529 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001530 if (mIsWidevine) {
1531 maxBuffers = 8;
1532 } else {
1533 maxBuffers = 64;
1534 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001535 break;
1536 case MEDIA_TRACK_TYPE_SUBTITLE:
1537 track = &mSubtitleTrack;
1538 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001539 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1540 track = &mTimedTextTrack;
1541 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001542 default:
1543 TRESPASS();
1544 }
1545
1546 if (track->mSource == NULL) {
1547 return;
1548 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001549
1550 if (actualTimeUs) {
1551 *actualTimeUs = seekTimeUs;
1552 }
1553
1554 MediaSource::ReadOptions options;
1555
1556 bool seeking = false;
1557
1558 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001559 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001560 seeking = true;
1561 }
1562
Chong Zhang42e81532014-12-01 13:44:26 -08001563 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001564 options.setNonBlocking();
1565 }
1566
Phil Burkc5cc2e22014-09-09 20:08:39 -07001567 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001568 MediaBuffer *mbuf;
1569 status_t err = track->mSource->read(&mbuf, &options);
1570
1571 options.clearSeekTo();
1572
1573 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001574 int64_t timeUs;
1575 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1576 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1577 mAudioTimeUs = timeUs;
1578 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1579 mVideoTimeUs = timeUs;
1580 }
1581
Robert Shih3423bbd2014-07-16 15:47:09 -07001582 // formatChange && seeking: track whose source is changed during selection
1583 // formatChange && !seeking: track whose source is not changed during selection
1584 // !formatChange: normal seek
Lajos Molnare26940f2014-07-31 10:31:26 -07001585 if ((seeking || formatChange)
1586 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1587 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
Wei Jiafef808d2014-10-31 17:57:05 -07001588 ATSParser::DiscontinuityType type = (formatChange && seeking)
1589 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1590 : ATSParser::DISCONTINUITY_NONE;
Robert Shih3423bbd2014-07-16 15:47:09 -07001591 track->mPackets->queueDiscontinuity( type, NULL, true /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001592 }
1593
Wei Jia474d7c72014-12-04 15:12:13 -08001594 sp<ABuffer> buffer = mediaBufferToABuffer(
1595 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001596 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001597 formatChange = false;
1598 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001599 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001600 } else if (err == WOULD_BLOCK) {
1601 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001602 } else if (err == INFO_FORMAT_CHANGED) {
1603#if 0
1604 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001605 ATSParser::DISCONTINUITY_FORMATCHANGE,
1606 NULL,
1607 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001608#endif
1609 } else {
1610 track->mPackets->signalEOS(err);
1611 break;
1612 }
1613 }
1614}
1615
Andreas Huberafed0e12011-09-20 15:39:58 -07001616} // namespace android