blob: 7dc9be7b21febd775b409c4bdefd328f4bcaf639 [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),
Marco Nelissen02fc5e32015-05-27 11:20:41 -070059 mDurationUs(-1ll),
Lajos Molnarcc227032014-07-17 15:33:06 -070060 mAudioIsVorbis(false),
Chong Zhang3de157d2014-08-05 20:54:44 -070061 mIsWidevine(false),
Chong Zhang42e81532014-12-01 13:44:26 -080062 mIsSecure(false),
Chong Zhangefbb6192015-01-30 17:13:27 -080063 mIsStreaming(false),
Lajos Molnarcc227032014-07-17 15:33:06 -070064 mUIDValid(uidValid),
Chong Zhangd354d8d2014-08-20 13:09:58 -070065 mUID(uid),
Chong Zhanga6bf21f2014-11-19 20:26:34 -080066 mFd(-1),
Ronghua Wu80276872014-08-28 15:50:29 -070067 mDrmManagerClient(NULL),
Chong Zhang2a3cc9a2014-08-21 17:48:26 -070068 mBitrate(-1ll),
Lajos Molnar84f52782014-09-11 10:01:55 -070069 mPollBufferingGeneration(0),
Chong Zhangefbb6192015-01-30 17:13:27 -080070 mPendingReadBufferTypes(0),
71 mBuffering(false),
Chong Zhangc287cad2015-02-19 18:30:30 -080072 mPrepareBuffering(false),
73 mPrevBufferPercentage(-1) {
Chong Zhanga19f33e2014-08-07 15:35:07 -070074 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -070075 DataSource::RegisterDefaultSniffers();
Chong Zhang3de157d2014-08-05 20:54:44 -070076}
77
Chong Zhanga19f33e2014-08-07 15:35:07 -070078void NuPlayer::GenericSource::resetDataSource() {
79 mHTTPService.clear();
Robert Shih360d6d02014-09-29 14:42:35 -070080 mHttpSource.clear();
Chong Zhanga19f33e2014-08-07 15:35:07 -070081 mUri.clear();
82 mUriHeaders.clear();
Chong Zhanga6bf21f2014-11-19 20:26:34 -080083 if (mFd >= 0) {
84 close(mFd);
85 mFd = -1;
86 }
Chong Zhanga19f33e2014-08-07 15:35:07 -070087 mOffset = 0;
88 mLength = 0;
Ronghua Wu80276872014-08-28 15:50:29 -070089 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
90 mDecryptHandle = NULL;
91 mDrmManagerClient = NULL;
92 mStarted = false;
Andy Hung2abde2c2014-09-30 14:40:32 -070093 mStopRead = true;
Chong Zhanga19f33e2014-08-07 15:35:07 -070094}
95
96status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -070097 const sp<IMediaHTTPService> &httpService,
98 const char *url,
99 const KeyedVector<String8, String8> *headers) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700100 resetDataSource();
Chong Zhang3de157d2014-08-05 20:54:44 -0700101
Chong Zhanga19f33e2014-08-07 15:35:07 -0700102 mHTTPService = httpService;
103 mUri = url;
Andreas Huberafed0e12011-09-20 15:39:58 -0700104
Chong Zhanga19f33e2014-08-07 15:35:07 -0700105 if (headers) {
106 mUriHeaders = *headers;
Chong Zhang3de157d2014-08-05 20:54:44 -0700107 }
108
Chong Zhanga19f33e2014-08-07 15:35:07 -0700109 // delay data source creation to prepareAsync() to avoid blocking
110 // the calling thread in setDataSource for any significant time.
111 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700112}
113
Chong Zhanga19f33e2014-08-07 15:35:07 -0700114status_t NuPlayer::GenericSource::setDataSource(
Chong Zhang3de157d2014-08-05 20:54:44 -0700115 int fd, int64_t offset, int64_t length) {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700116 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700117
Chong Zhanga19f33e2014-08-07 15:35:07 -0700118 mFd = dup(fd);
119 mOffset = offset;
120 mLength = length;
121
122 // delay data source creation to prepareAsync() to avoid blocking
123 // the calling thread in setDataSource for any significant time.
124 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700125}
126
Chris Watkins99f31602015-03-20 13:06:33 -0700127status_t NuPlayer::GenericSource::setDataSource(const sp<DataSource>& source) {
128 resetDataSource();
129 mDataSource = source;
130 return OK;
131}
132
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700133sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const {
134 return mFileMeta;
135}
136
Chong Zhangd354d8d2014-08-20 13:09:58 -0700137status_t NuPlayer::GenericSource::initFromDataSource() {
Lajos Molnarcc227032014-07-17 15:33:06 -0700138 sp<MediaExtractor> extractor;
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800139 String8 mimeType;
140 float confidence;
141 sp<AMessage> dummy;
142 bool isWidevineStreaming = false;
Lajos Molnarcc227032014-07-17 15:33:06 -0700143
Chong Zhangd354d8d2014-08-20 13:09:58 -0700144 CHECK(mDataSource != NULL);
145
Lajos Molnarcc227032014-07-17 15:33:06 -0700146 if (mIsWidevine) {
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800147 isWidevineStreaming = SniffWVM(
148 mDataSource, &mimeType, &confidence, &dummy);
149 if (!isWidevineStreaming ||
150 strcasecmp(
Lajos Molnarcc227032014-07-17 15:33:06 -0700151 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) {
152 ALOGE("unsupported widevine mime: %s", mimeType.string());
Chong Zhang3de157d2014-08-05 20:54:44 -0700153 return UNKNOWN_ERROR;
Lajos Molnarcc227032014-07-17 15:33:06 -0700154 }
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800155 } else if (mIsStreaming) {
Chong Zhangc287cad2015-02-19 18:30:30 -0800156 if (!mDataSource->sniff(&mimeType, &confidence, &dummy)) {
157 return UNKNOWN_ERROR;
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800158 }
159 isWidevineStreaming = !strcasecmp(
Chong Zhangc287cad2015-02-19 18:30:30 -0800160 mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800161 }
Lajos Molnarcc227032014-07-17 15:33:06 -0700162
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800163 if (isWidevineStreaming) {
164 // we don't want cached source for widevine streaming.
165 mCachedSource.clear();
166 mDataSource = mHttpSource;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700167 mWVMExtractor = new WVMExtractor(mDataSource);
168 mWVMExtractor->setAdaptiveStreamingMode(true);
Lajos Molnarcc227032014-07-17 15:33:06 -0700169 if (mUIDValid) {
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700170 mWVMExtractor->setUID(mUID);
Lajos Molnarcc227032014-07-17 15:33:06 -0700171 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700172 extractor = mWVMExtractor;
Lajos Molnarcc227032014-07-17 15:33:06 -0700173 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700174 extractor = MediaExtractor::Create(mDataSource,
Chong Zhangc287cad2015-02-19 18:30:30 -0800175 mimeType.isEmpty() ? NULL : mimeType.string());
Lajos Molnarcc227032014-07-17 15:33:06 -0700176 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700177
Chong Zhang3de157d2014-08-05 20:54:44 -0700178 if (extractor == NULL) {
179 return UNKNOWN_ERROR;
180 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700181
Ronghua Wu80276872014-08-28 15:50:29 -0700182 if (extractor->getDrmFlag()) {
183 checkDrmStatus(mDataSource);
184 }
185
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700186 mFileMeta = extractor->getMetaData();
187 if (mFileMeta != NULL) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700188 int64_t duration;
Marco Nelissenf0b72b52014-09-16 15:43:44 -0700189 if (mFileMeta->findInt64(kKeyDuration, &duration)) {
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700190 mDurationUs = duration;
191 }
Chong Zhang42e81532014-12-01 13:44:26 -0800192
193 if (!mIsWidevine) {
194 // Check mime to see if we actually have a widevine source.
195 // If the data source is not URL-type (eg. file source), we
196 // won't be able to tell until now.
197 const char *fileMime;
198 if (mFileMeta->findCString(kKeyMIMEType, &fileMime)
199 && !strncasecmp(fileMime, "video/wvm", 9)) {
200 mIsWidevine = true;
201 }
202 }
Marco Nelissenc1f4b2b2014-06-17 14:48:32 -0700203 }
204
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700205 int32_t totalBitrate = 0;
206
Marco Nelissen705d3292014-09-19 15:14:37 -0700207 size_t numtracks = extractor->countTracks();
208 if (numtracks == 0) {
209 return UNKNOWN_ERROR;
210 }
211
212 for (size_t i = 0; i < numtracks; ++i) {
Chong Zhangafc0a872014-08-26 09:56:52 -0700213 sp<MediaSource> track = extractor->getTrack(i);
214
Andreas Huberafed0e12011-09-20 15:39:58 -0700215 sp<MetaData> meta = extractor->getTrackMetaData(i);
216
217 const char *mime;
218 CHECK(meta->findCString(kKeyMIMEType, &mime));
219
Chong Zhangafc0a872014-08-26 09:56:52 -0700220 // Do the string compare immediately with "mime",
221 // we can't assume "mime" would stay valid after another
222 // extractor operation, some extractors might modify meta
223 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700224 if (!strncasecmp(mime, "audio/", 6)) {
225 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700226 mAudioTrack.mIndex = i;
227 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700228 mAudioTrack.mPackets =
229 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700230
231 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
232 mAudioIsVorbis = true;
233 } else {
234 mAudioIsVorbis = false;
235 }
236 }
237 } else if (!strncasecmp(mime, "video/", 6)) {
238 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700239 mVideoTrack.mIndex = i;
240 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700241 mVideoTrack.mPackets =
242 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700243
244 // check if the source requires secure buffers
245 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700246 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
247 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800248 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700249 if (mUIDValid) {
250 extractor->setUID(mUID);
251 }
Chong Zhang7e892182014-08-05 11:58:21 -0700252 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700253 }
254 }
255
256 if (track != NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700257 mSources.push(track);
Andreas Huberafed0e12011-09-20 15:39:58 -0700258 int64_t durationUs;
259 if (meta->findInt64(kKeyDuration, &durationUs)) {
260 if (durationUs > mDurationUs) {
261 mDurationUs = durationUs;
262 }
263 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700264
265 int32_t bitrate;
266 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
267 totalBitrate += bitrate;
268 } else {
269 totalBitrate = -1;
270 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700271 }
272 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700273
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700274 mBitrate = totalBitrate;
275
276 return OK;
277}
278
279status_t NuPlayer::GenericSource::startSources() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800280 // Start the selected A/V tracks now before we start buffering.
281 // Widevine sources might re-initialize crypto when starting, if we delay
282 // this to start(), all data buffered during prepare would be wasted.
283 // (We don't actually start reading until start().)
284 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
285 ALOGE("failed to start audio track!");
286 return UNKNOWN_ERROR;
287 }
288
289 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
290 ALOGE("failed to start video track!");
291 return UNKNOWN_ERROR;
292 }
293
Chong Zhang3de157d2014-08-05 20:54:44 -0700294 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700295}
296
Ronghua Wu80276872014-08-28 15:50:29 -0700297void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
298 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
299 if (mDecryptHandle != NULL) {
300 CHECK(mDrmManagerClient);
301 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
302 sp<AMessage> msg = dupNotify();
303 msg->setInt32("what", kWhatDrmNoLicense);
304 msg->post();
305 }
306 }
307}
308
309int64_t NuPlayer::GenericSource::getLastReadPosition() {
310 if (mAudioTrack.mSource != NULL) {
311 return mAudioTimeUs;
312 } else if (mVideoTrack.mSource != NULL) {
313 return mVideoTimeUs;
314 } else {
315 return 0;
316 }
317}
318
Chong Zhanga19f33e2014-08-07 15:35:07 -0700319status_t NuPlayer::GenericSource::setBuffers(
320 bool audio, Vector<MediaBuffer *> &buffers) {
Chong Zhang42e81532014-12-01 13:44:26 -0800321 if (mIsSecure && !audio) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700322 return mVideoTrack.mSource->setBuffers(buffers);
323 }
324 return INVALID_OPERATION;
325}
326
Ronghua Wu02cb98d2015-05-27 11:02:54 -0700327bool NuPlayer::GenericSource::isStreaming() const {
328 return mIsStreaming;
329}
330
Andreas Huberafed0e12011-09-20 15:39:58 -0700331NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700332 if (mLooper != NULL) {
333 mLooper->unregisterHandler(id());
334 mLooper->stop();
335 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800336 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700337}
338
Andreas Huber9575c962013-02-05 13:59:56 -0800339void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700340 if (mLooper == NULL) {
341 mLooper = new ALooper;
342 mLooper->setName("generic");
343 mLooper->start();
344
345 mLooper->registerHandler(this);
346 }
347
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800348 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
Chong Zhang1228d6b2014-08-12 21:25:48 -0700349 msg->post();
350}
351
352void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700353 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700354 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800355 // set to false first, if the extractor
356 // comes back as secure, set it to true then.
357 mIsSecure = false;
358
Chong Zhangd354d8d2014-08-20 13:09:58 -0700359 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700360 const char* uri = mUri.c_str();
Chong Zhangc287cad2015-02-19 18:30:30 -0800361 String8 contentType;
Robert Shih360d6d02014-09-29 14:42:35 -0700362 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
363
364 if (!strncasecmp("http://", uri, 7)
365 || !strncasecmp("https://", uri, 8)
366 || mIsWidevine) {
367 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
368 if (mHttpSource == NULL) {
369 ALOGE("Failed to create http source!");
370 notifyPreparedAndCleanup(UNKNOWN_ERROR);
371 return;
372 }
373 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700374
Chong Zhangd354d8d2014-08-20 13:09:58 -0700375 mDataSource = DataSource::CreateFromURI(
Chong Zhangc287cad2015-02-19 18:30:30 -0800376 mHTTPService, uri, &mUriHeaders, &contentType,
Robert Shih360d6d02014-09-29 14:42:35 -0700377 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700378 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700379 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700380
Chong Zhangd354d8d2014-08-20 13:09:58 -0700381 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800382 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700383 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700384
Chong Zhangd354d8d2014-08-20 13:09:58 -0700385 if (mDataSource == NULL) {
386 ALOGE("Failed to create data source!");
387 notifyPreparedAndCleanup(UNKNOWN_ERROR);
388 return;
389 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700390 }
391
Chris Watkins99f31602015-03-20 13:06:33 -0700392 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
393 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
394 }
395
396 // For widevine or other cached streaming cases, we need to wait for
397 // enough buffering before reporting prepared.
398 // Note that even when URL doesn't start with widevine://, mIsWidevine
399 // could still be set to true later, if the streaming or file source
400 // is sniffed to be widevine. We don't want to buffer for file source
401 // in that case, so must check the flag now.
402 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
403
Chong Zhangc287cad2015-02-19 18:30:30 -0800404 // init extractor from data source
405 status_t err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700406
407 if (err != OK) {
408 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700409 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700410 return;
411 }
412
Andreas Huber9575c962013-02-05 13:59:56 -0800413 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700414 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
415 sp<AMessage> msg = new AMessage;
416 err = convertMetaDataToMessage(meta, &msg);
417 if(err != OK) {
418 notifyPreparedAndCleanup(err);
419 return;
420 }
421 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800422 }
423
424 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800425 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800426 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700427 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800428 | FLAG_CAN_SEEK_BACKWARD
429 | FLAG_CAN_SEEK_FORWARD
430 | FLAG_CAN_SEEK);
431
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700432 if (mIsSecure) {
433 // secure decoders must be instantiated before starting widevine source
434 sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, this);
435 notifyInstantiateSecureDecoders(reply);
436 } else {
437 finishPrepareAsync();
438 }
439}
440
441void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) {
442 if (err != OK) {
443 ALOGE("Failed to instantiate secure decoders!");
444 notifyPreparedAndCleanup(err);
445 return;
446 }
447 finishPrepareAsync();
448}
449
450void NuPlayer::GenericSource::finishPrepareAsync() {
451 status_t err = startSources();
452 if (err != OK) {
453 ALOGE("Failed to init start data source!");
454 notifyPreparedAndCleanup(err);
455 return;
456 }
457
Chong Zhangefbb6192015-01-30 17:13:27 -0800458 if (mIsStreaming) {
459 mPrepareBuffering = true;
460
461 ensureCacheIsFetching();
462 restartPollBuffering();
463 } else {
464 notifyPrepared();
465 }
Andreas Huber9575c962013-02-05 13:59:56 -0800466}
467
Chong Zhangd354d8d2014-08-20 13:09:58 -0700468void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
469 if (err != OK) {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700470 mDataSource.clear();
471 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700472 mHttpSource.clear();
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700473 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700474
475 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700476 }
477 notifyPrepared(err);
478}
479
Andreas Huberafed0e12011-09-20 15:39:58 -0700480void NuPlayer::GenericSource::start() {
481 ALOGI("start");
482
Andy Hung2abde2c2014-09-30 14:40:32 -0700483 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700484 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700485 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700486 }
487
488 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700489 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700490 }
Ronghua Wu80276872014-08-28 15:50:29 -0700491
492 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
493 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800494
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800495 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700496}
497
498void NuPlayer::GenericSource::stop() {
499 // nothing to do, just account for DRM playback status
500 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
501 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800502 if (mIsWidevine || mIsSecure) {
503 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800504 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700505 sp<AMessage> response;
506 (void) msg->postAndAwaitResponse(&response);
507 }
Ronghua Wu80276872014-08-28 15:50:29 -0700508}
509
510void NuPlayer::GenericSource::pause() {
511 // nothing to do, just account for DRM playback status
512 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
513 mStarted = false;
514}
515
516void NuPlayer::GenericSource::resume() {
517 // nothing to do, just account for DRM playback status
518 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
519 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800520
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800521 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700522}
523
Chong Zhang48296b72014-09-14 14:28:45 -0700524void NuPlayer::GenericSource::disconnect() {
525 if (mDataSource != NULL) {
526 // disconnect data source
527 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
528 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
529 }
Robert Shih360d6d02014-09-29 14:42:35 -0700530 } else if (mHttpSource != NULL) {
531 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700532 }
533}
534
Ronghua Wu80276872014-08-28 15:50:29 -0700535void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
536 if (mDecryptHandle != NULL) {
537 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
538 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700539 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
540 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700541}
542
543status_t NuPlayer::GenericSource::feedMoreTSData() {
544 return OK;
545}
546
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700547void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800548 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700549 msg->setInt32("generation", mPollBufferingGeneration);
550 msg->post(1000000ll);
551}
552
553void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800554 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700555 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800556 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700557}
558
Chong Zhangefbb6192015-01-30 17:13:27 -0800559void NuPlayer::GenericSource::restartPollBuffering() {
560 if (mIsStreaming) {
561 cancelPollBuffering();
562 onPollBuffering();
563 }
564}
565
Chong Zhangc287cad2015-02-19 18:30:30 -0800566void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
567 // Buffering percent could go backward as it's estimated from remaining
568 // data and last access time. This could cause the buffering position
569 // drawn on media control to jitter slightly. Remember previously reported
570 // percentage and don't allow it to go backward.
571 if (percentage < mPrevBufferPercentage) {
572 percentage = mPrevBufferPercentage;
573 } else if (percentage > 100) {
574 percentage = 100;
575 }
576
577 mPrevBufferPercentage = percentage;
578
Chong Zhangefbb6192015-01-30 17:13:27 -0800579 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
580
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700581 sp<AMessage> msg = dupNotify();
582 msg->setInt32("what", kWhatBufferingUpdate);
583 msg->setInt32("percentage", percentage);
584 msg->post();
585}
586
Chong Zhangefbb6192015-01-30 17:13:27 -0800587void NuPlayer::GenericSource::startBufferingIfNecessary() {
588 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
589 mPrepareBuffering, mBuffering);
590
591 if (mPrepareBuffering) {
592 return;
593 }
594
595 if (!mBuffering) {
596 mBuffering = true;
597
598 ensureCacheIsFetching();
599 sendCacheStats();
600
601 sp<AMessage> notify = dupNotify();
602 notify->setInt32("what", kWhatPauseOnBufferingStart);
603 notify->post();
604 }
605}
606
607void NuPlayer::GenericSource::stopBufferingIfNecessary() {
608 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
609 mPrepareBuffering, mBuffering);
610
611 if (mPrepareBuffering) {
612 mPrepareBuffering = false;
613 notifyPrepared();
614 return;
615 }
616
617 if (mBuffering) {
618 mBuffering = false;
619
620 sendCacheStats();
621
622 sp<AMessage> notify = dupNotify();
623 notify->setInt32("what", kWhatResumeOnBufferingEnd);
624 notify->post();
625 }
626}
627
628void NuPlayer::GenericSource::sendCacheStats() {
629 int32_t kbps = 0;
630 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700631
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800632 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800633 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800634 } else if (mCachedSource != NULL) {
635 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800636 }
637
638 if (err == OK) {
639 sp<AMessage> notify = dupNotify();
640 notify->setInt32("what", kWhatCacheStats);
641 notify->setInt32("bandwidth", kbps);
642 notify->post();
643 }
644}
645
646void NuPlayer::GenericSource::ensureCacheIsFetching() {
647 if (mCachedSource != NULL) {
648 mCachedSource->resumeFetchingIfNecessary();
649 }
650}
651
652void NuPlayer::GenericSource::onPollBuffering() {
653 status_t finalStatus = UNKNOWN_ERROR;
654 int64_t cachedDurationUs = -1ll;
655 ssize_t cachedDataRemaining = -1;
656
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800657 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
658 "WVMExtractor and NuCachedSource both present");
659
660 if (mWVMExtractor != NULL) {
661 cachedDurationUs =
662 mWVMExtractor->getCachedDurationUs(&finalStatus);
663 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800664 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700665 mCachedSource->approxDataRemaining(&finalStatus);
666
667 if (finalStatus == OK) {
668 off64_t size;
669 int64_t bitrate = 0ll;
670 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
671 bitrate = size * 8000000ll / mDurationUs;
672 } else if (mBitrate > 0) {
673 bitrate = mBitrate;
674 }
675 if (bitrate > 0) {
676 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
677 }
678 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700679 }
680
Chong Zhangefbb6192015-01-30 17:13:27 -0800681 if (finalStatus != OK) {
682 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
683
684 if (finalStatus == ERROR_END_OF_STREAM) {
685 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700686 }
687
Chong Zhangefbb6192015-01-30 17:13:27 -0800688 stopBufferingIfNecessary();
689 return;
690 } else if (cachedDurationUs >= 0ll) {
691 if (mDurationUs > 0ll) {
692 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
693 int percentage = 100.0 * cachedPosUs / mDurationUs;
694 if (percentage > 100) {
695 percentage = 100;
696 }
697
698 notifyBufferingUpdate(percentage);
699 }
700
701 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
702 cachedDurationUs / 1000000.0f);
703
704 if (cachedDurationUs < kLowWaterMarkUs) {
705 startBufferingIfNecessary();
706 } else if (cachedDurationUs > kHighWaterMarkUs) {
707 stopBufferingIfNecessary();
708 }
709 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700710 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800711 cachedDataRemaining);
712
713 if (cachedDataRemaining < kLowWaterMarkBytes) {
714 startBufferingIfNecessary();
715 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
716 stopBufferingIfNecessary();
717 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700718 }
719
720 schedulePollBuffering();
721}
722
Robert Shih3423bbd2014-07-16 15:47:09 -0700723void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
724 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700725 case kWhatPrepareAsync:
726 {
727 onPrepareAsync();
728 break;
729 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700730 case kWhatFetchSubtitleData:
731 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700732 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
733 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
734 break;
735 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700736
Lajos Molnare26940f2014-07-31 10:31:26 -0700737 case kWhatFetchTimedTextData:
738 {
739 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
740 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700741 break;
742 }
743
744 case kWhatSendSubtitleData:
745 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700746 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
747 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
748 break;
749 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700750
Lajos Molnare26940f2014-07-31 10:31:26 -0700751 case kWhatSendTimedTextData:
752 {
753 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
754 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700755 break;
756 }
757
758 case kWhatChangeAVSource:
759 {
760 int32_t trackIndex;
761 CHECK(msg->findInt32("trackIndex", &trackIndex));
762 const sp<MediaSource> source = mSources.itemAt(trackIndex);
763
764 Track* track;
765 const char *mime;
766 media_track_type trackType, counterpartType;
767 sp<MetaData> meta = source->getFormat();
768 meta->findCString(kKeyMIMEType, &mime);
769 if (!strncasecmp(mime, "audio/", 6)) {
770 track = &mAudioTrack;
771 trackType = MEDIA_TRACK_TYPE_AUDIO;
772 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
773 } else {
774 CHECK(!strncasecmp(mime, "video/", 6));
775 track = &mVideoTrack;
776 trackType = MEDIA_TRACK_TYPE_VIDEO;
777 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
778 }
779
780
781 if (track->mSource != NULL) {
782 track->mSource->stop();
783 }
784 track->mSource = source;
785 track->mSource->start();
786 track->mIndex = trackIndex;
787
Robert Shih3423bbd2014-07-16 15:47:09 -0700788 int64_t timeUs, actualTimeUs;
789 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800790 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
791 timeUs = mAudioLastDequeueTimeUs;
792 } else {
793 timeUs = mVideoLastDequeueTimeUs;
794 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700795 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
796 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700797 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700798
799 break;
800 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800801
802 case kWhatStart:
803 case kWhatResume:
804 {
805 restartPollBuffering();
806 break;
807 }
808
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700809 case kWhatPollBuffering:
810 {
811 int32_t generation;
812 CHECK(msg->findInt32("generation", &generation));
813 if (generation == mPollBufferingGeneration) {
814 onPollBuffering();
815 }
816 break;
817 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700818
819 case kWhatGetFormat:
820 {
821 onGetFormatMeta(msg);
822 break;
823 }
824
825 case kWhatGetSelectedTrack:
826 {
827 onGetSelectedTrack(msg);
828 break;
829 }
830
831 case kWhatSelectTrack:
832 {
833 onSelectTrack(msg);
834 break;
835 }
836
837 case kWhatSeek:
838 {
839 onSeek(msg);
840 break;
841 }
842
843 case kWhatReadBuffer:
844 {
845 onReadBuffer(msg);
846 break;
847 }
848
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700849 case kWhatSecureDecodersInstantiated:
850 {
851 int32_t err;
852 CHECK(msg->findInt32("err", &err));
853 onSecureDecodersInstantiated(err);
854 break;
855 }
856
Andy Hung2abde2c2014-09-30 14:40:32 -0700857 case kWhatStopWidevine:
858 {
859 // mStopRead is only used for Widevine to prevent the video source
860 // from being read while the associated video decoder is shutting down.
861 mStopRead = true;
862 if (mVideoTrack.mSource != NULL) {
863 mVideoTrack.mPackets->clear();
864 }
865 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800866 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700867 CHECK(msg->senderAwaitsResponse(&replyID));
868 response->postReply(replyID);
869 break;
870 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700871 default:
872 Source::onMessageReceived(msg);
873 break;
874 }
875}
876
Lajos Molnare26940f2014-07-31 10:31:26 -0700877void NuPlayer::GenericSource::fetchTextData(
878 uint32_t sendWhat,
879 media_track_type type,
880 int32_t curGen,
881 sp<AnotherPacketSource> packets,
882 sp<AMessage> msg) {
883 int32_t msgGeneration;
884 CHECK(msg->findInt32("generation", &msgGeneration));
885 if (msgGeneration != curGen) {
886 // stale
887 return;
888 }
889
890 int32_t avail;
891 if (packets->hasBufferAvailable(&avail)) {
892 return;
893 }
894
895 int64_t timeUs;
896 CHECK(msg->findInt64("timeUs", &timeUs));
897
898 int64_t subTimeUs;
899 readBuffer(type, timeUs, &subTimeUs);
900
901 int64_t delayUs = subTimeUs - timeUs;
902 if (msg->what() == kWhatFetchSubtitleData) {
903 const int64_t oneSecUs = 1000000ll;
904 delayUs -= oneSecUs;
905 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800906 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700907 msg2->setInt32("generation", msgGeneration);
908 msg2->post(delayUs < 0 ? 0 : delayUs);
909}
910
911void NuPlayer::GenericSource::sendTextData(
912 uint32_t what,
913 media_track_type type,
914 int32_t curGen,
915 sp<AnotherPacketSource> packets,
916 sp<AMessage> msg) {
917 int32_t msgGeneration;
918 CHECK(msg->findInt32("generation", &msgGeneration));
919 if (msgGeneration != curGen) {
920 // stale
921 return;
922 }
923
924 int64_t subTimeUs;
925 if (packets->nextBufferTime(&subTimeUs) != OK) {
926 return;
927 }
928
929 int64_t nextSubTimeUs;
930 readBuffer(type, -1, &nextSubTimeUs);
931
932 sp<ABuffer> buffer;
933 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
934 if (dequeueStatus == OK) {
935 sp<AMessage> notify = dupNotify();
936 notify->setInt32("what", what);
937 notify->setBuffer("buffer", buffer);
938 notify->post();
939
940 const int64_t delayUs = nextSubTimeUs - subTimeUs;
941 msg->post(delayUs < 0 ? 0 : delayUs);
942 }
943}
944
Andreas Huber84066782011-08-16 09:34:26 -0700945sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800946 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700947 msg->setInt32("audio", audio);
948
949 sp<AMessage> response;
950 void *format;
951 status_t err = msg->postAndAwaitResponse(&response);
952 if (err == OK && response != NULL) {
953 CHECK(response->findPointer("format", &format));
954 return (MetaData *)format;
955 } else {
956 return NULL;
957 }
958}
959
960void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
961 int32_t audio;
962 CHECK(msg->findInt32("audio", &audio));
963
964 sp<AMessage> response = new AMessage;
965 sp<MetaData> format = doGetFormatMeta(audio);
966 response->setPointer("format", format.get());
967
Lajos Molnar3f274362015-03-05 14:35:41 -0800968 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -0700969 CHECK(msg->senderAwaitsResponse(&replyID));
970 response->postReply(replyID);
971}
972
973sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700974 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
975
976 if (source == NULL) {
977 return NULL;
978 }
979
980 return source->getFormat();
981}
982
983status_t NuPlayer::GenericSource::dequeueAccessUnit(
984 bool audio, sp<ABuffer> *accessUnit) {
985 Track *track = audio ? &mAudioTrack : &mVideoTrack;
986
987 if (track->mSource == NULL) {
988 return -EWOULDBLOCK;
989 }
990
Lajos Molnarcc227032014-07-17 15:33:06 -0700991 if (mIsWidevine && !audio) {
992 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700993 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -0700994 }
995
Andreas Huberafed0e12011-09-20 15:39:58 -0700996 status_t finalResult;
997 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -0800998 if (finalResult == OK) {
999 postReadBuffer(
1000 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1001 return -EWOULDBLOCK;
1002 }
1003 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001004 }
1005
1006 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1007
Chong Zhangfcf044a2015-07-14 15:58:51 -07001008 // start pulling in more buffers if we only have one (or no) buffer left
1009 // so that decoder has less chance of being starved
1010 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001011 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001012 }
1013
Robert Shih3423bbd2014-07-16 15:47:09 -07001014 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001015 if (mSubtitleTrack.mSource != NULL) {
1016 mSubtitleTrack.mPackets->clear();
1017 mFetchSubtitleDataGeneration++;
1018 }
1019 if (mTimedTextTrack.mSource != NULL) {
1020 mTimedTextTrack.mPackets->clear();
1021 mFetchTimedTextDataGeneration++;
1022 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001023 return result;
1024 }
1025
1026 int64_t timeUs;
1027 status_t eosResult; // ignored
1028 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001029 if (audio) {
1030 mAudioLastDequeueTimeUs = timeUs;
1031 } else {
1032 mVideoLastDequeueTimeUs = timeUs;
1033 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001034
1035 if (mSubtitleTrack.mSource != NULL
1036 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001037 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001038 msg->setInt64("timeUs", timeUs);
1039 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1040 msg->post();
1041 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001042
Lajos Molnare26940f2014-07-31 10:31:26 -07001043 if (mTimedTextTrack.mSource != NULL
1044 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001045 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001046 msg->setInt64("timeUs", timeUs);
1047 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1048 msg->post();
1049 }
1050
Andreas Huberafed0e12011-09-20 15:39:58 -07001051 return result;
1052}
1053
1054status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1055 *durationUs = mDurationUs;
1056 return OK;
1057}
1058
Robert Shihdd235722014-06-12 14:49:23 -07001059size_t NuPlayer::GenericSource::getTrackCount() const {
1060 return mSources.size();
1061}
1062
1063sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1064 size_t trackCount = mSources.size();
1065 if (trackIndex >= trackCount) {
1066 return NULL;
1067 }
1068
1069 sp<AMessage> format = new AMessage();
1070 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1071
1072 const char *mime;
1073 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001074 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001075
1076 int32_t trackType;
1077 if (!strncasecmp(mime, "video/", 6)) {
1078 trackType = MEDIA_TRACK_TYPE_VIDEO;
1079 } else if (!strncasecmp(mime, "audio/", 6)) {
1080 trackType = MEDIA_TRACK_TYPE_AUDIO;
1081 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1082 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1083 } else {
1084 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1085 }
1086 format->setInt32("type", trackType);
1087
1088 const char *lang;
1089 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1090 lang = "und";
1091 }
1092 format->setString("language", lang);
1093
1094 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001095 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1096 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1097 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1098 meta->findInt32(kKeyTrackIsForced, &isForced);
1099
1100 format->setInt32("auto", !!isAutoselect);
1101 format->setInt32("default", !!isDefault);
1102 format->setInt32("forced", !!isForced);
1103 }
1104
1105 return format;
1106}
1107
Lajos Molnare26940f2014-07-31 10:31:26 -07001108ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001109 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001110 msg->setInt32("type", type);
1111
1112 sp<AMessage> response;
1113 int32_t index;
1114 status_t err = msg->postAndAwaitResponse(&response);
1115 if (err == OK && response != NULL) {
1116 CHECK(response->findInt32("index", &index));
1117 return index;
1118 } else {
1119 return -1;
1120 }
1121}
1122
1123void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1124 int32_t tmpType;
1125 CHECK(msg->findInt32("type", &tmpType));
1126 media_track_type type = (media_track_type)tmpType;
1127
1128 sp<AMessage> response = new AMessage;
1129 ssize_t index = doGetSelectedTrack(type);
1130 response->setInt32("index", index);
1131
Lajos Molnar3f274362015-03-05 14:35:41 -08001132 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001133 CHECK(msg->senderAwaitsResponse(&replyID));
1134 response->postReply(replyID);
1135}
1136
1137ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001138 const Track *track = NULL;
1139 switch (type) {
1140 case MEDIA_TRACK_TYPE_VIDEO:
1141 track = &mVideoTrack;
1142 break;
1143 case MEDIA_TRACK_TYPE_AUDIO:
1144 track = &mAudioTrack;
1145 break;
1146 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1147 track = &mTimedTextTrack;
1148 break;
1149 case MEDIA_TRACK_TYPE_SUBTITLE:
1150 track = &mSubtitleTrack;
1151 break;
1152 default:
1153 break;
1154 }
1155
1156 if (track != NULL && track->mSource != NULL) {
1157 return track->mIndex;
1158 }
1159
1160 return -1;
1161}
1162
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001163status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001164 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001165 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001166 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001167 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001168 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001169
1170 sp<AMessage> response;
1171 status_t err = msg->postAndAwaitResponse(&response);
1172 if (err == OK && response != NULL) {
1173 CHECK(response->findInt32("err", &err));
1174 }
1175
1176 return err;
1177}
1178
1179void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1180 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001181 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001182 CHECK(msg->findInt32("trackIndex", &trackIndex));
1183 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001184 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001185
1186 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001187 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001188 response->setInt32("err", err);
1189
Lajos Molnar3f274362015-03-05 14:35:41 -08001190 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001191 CHECK(msg->senderAwaitsResponse(&replyID));
1192 response->postReply(replyID);
1193}
1194
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001195status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001196 if (trackIndex >= mSources.size()) {
1197 return BAD_INDEX;
1198 }
1199
1200 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001201 Track* track = NULL;
1202 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1203 track = &mSubtitleTrack;
1204 mFetchSubtitleDataGeneration++;
1205 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1206 track = &mTimedTextTrack;
1207 mFetchTimedTextDataGeneration++;
1208 }
1209 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001210 return INVALID_OPERATION;
1211 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001212 track->mSource->stop();
1213 track->mSource = NULL;
1214 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001215 return OK;
1216 }
1217
1218 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1219 sp<MetaData> meta = source->getFormat();
1220 const char *mime;
1221 CHECK(meta->findCString(kKeyMIMEType, &mime));
1222 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001223 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1224 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1225 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001226 return OK;
1227 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001228 track->mIndex = trackIndex;
1229 if (track->mSource != NULL) {
1230 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001231 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001232 track->mSource = mSources.itemAt(trackIndex);
1233 track->mSource->start();
1234 if (track->mPackets == NULL) {
1235 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001236 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001237 track->mPackets->clear();
1238 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001239
1240 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001241
1242 if (isSubtitle) {
1243 mFetchSubtitleDataGeneration++;
1244 } else {
1245 mFetchTimedTextDataGeneration++;
1246 }
1247
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001248 status_t eosResult; // ignored
1249 if (mSubtitleTrack.mSource != NULL
1250 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001251 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001252 msg->setInt64("timeUs", timeUs);
1253 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1254 msg->post();
1255 }
1256
1257 if (mTimedTextTrack.mSource != NULL
1258 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001259 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001260 msg->setInt64("timeUs", timeUs);
1261 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1262 msg->post();
1263 }
1264
Robert Shih3423bbd2014-07-16 15:47:09 -07001265 return OK;
1266 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1267 bool audio = !strncasecmp(mime, "audio/", 6);
1268 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1269 if (track->mSource != NULL && track->mIndex == trackIndex) {
1270 return OK;
1271 }
1272
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001273 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001274 msg->setInt32("trackIndex", trackIndex);
1275 msg->post();
1276 return OK;
1277 }
1278
1279 return INVALID_OPERATION;
1280}
1281
Andreas Huberafed0e12011-09-20 15:39:58 -07001282status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001283 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001284 msg->setInt64("seekTimeUs", seekTimeUs);
1285
1286 sp<AMessage> response;
1287 status_t err = msg->postAndAwaitResponse(&response);
1288 if (err == OK && response != NULL) {
1289 CHECK(response->findInt32("err", &err));
1290 }
1291
1292 return err;
1293}
1294
1295void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1296 int64_t seekTimeUs;
1297 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1298
1299 sp<AMessage> response = new AMessage;
1300 status_t err = doSeek(seekTimeUs);
1301 response->setInt32("err", err);
1302
Lajos Molnar3f274362015-03-05 14:35:41 -08001303 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001304 CHECK(msg->senderAwaitsResponse(&replyID));
1305 response->postReply(replyID);
1306}
1307
1308status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001309 // If the Widevine source is stopped, do not attempt to read any
1310 // more buffers.
1311 if (mStopRead) {
1312 return INVALID_OPERATION;
1313 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001314 if (mVideoTrack.mSource != NULL) {
1315 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001316 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001317
1318 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001319 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001320 }
1321
1322 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001323 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001324 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001325 }
1326
Ronghua Wu80276872014-08-28 15:50:29 -07001327 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1328 if (!mStarted) {
1329 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1330 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001331
1332 // If currently buffering, post kWhatBufferingEnd first, so that
1333 // NuPlayer resumes. Otherwise, if cache hits high watermark
1334 // before new polling happens, no one will resume the playback.
1335 stopBufferingIfNecessary();
1336 restartPollBuffering();
1337
Andreas Huberafed0e12011-09-20 15:39:58 -07001338 return OK;
1339}
1340
Robert Shih3423bbd2014-07-16 15:47:09 -07001341sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1342 MediaBuffer* mb,
1343 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001344 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001345 int64_t *actualTimeUs) {
1346 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1347 size_t outLength = mb->range_length();
1348
1349 if (audio && mAudioIsVorbis) {
1350 outLength += sizeof(int32_t);
1351 }
1352
1353 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001354 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001355 // data is already provided in the buffer
1356 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001357 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001358 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001359 } else {
1360 ab = new ABuffer(outLength);
1361 memcpy(ab->data(),
1362 (const uint8_t *)mb->data() + mb->range_offset(),
1363 mb->range_length());
1364 }
1365
1366 if (audio && mAudioIsVorbis) {
1367 int32_t numPageSamples;
1368 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1369 numPageSamples = -1;
1370 }
1371
1372 uint8_t* abEnd = ab->data() + mb->range_length();
1373 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1374 }
1375
Lajos Molnare26940f2014-07-31 10:31:26 -07001376 sp<AMessage> meta = ab->meta();
1377
Robert Shih3423bbd2014-07-16 15:47:09 -07001378 int64_t timeUs;
1379 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001380 meta->setInt64("timeUs", timeUs);
1381
Wei Jia474d7c72014-12-04 15:12:13 -08001382#if 0
1383 // Temporarily disable pre-roll till we have a full solution to handle
1384 // both single seek and continous seek gracefully.
1385 if (seekTimeUs > timeUs) {
1386 sp<AMessage> extra = new AMessage;
1387 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1388 meta->setMessage("extra", extra);
1389 }
1390#endif
1391
Lajos Molnare26940f2014-07-31 10:31:26 -07001392 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1393 const char *mime;
1394 CHECK(mTimedTextTrack.mSource != NULL
1395 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1396 meta->setString("mime", mime);
1397 }
1398
Robert Shih3423bbd2014-07-16 15:47:09 -07001399 int64_t durationUs;
1400 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1401 meta->setInt64("durationUs", durationUs);
1402 }
1403
1404 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1405 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1406 }
1407
Robert Shihf8bd8512015-04-23 16:39:18 -07001408 uint32_t dataType; // unused
1409 const void *seiData;
1410 size_t seiLength;
1411 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1412 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1413 meta->setBuffer("sei", sei);
1414 }
1415
Robert Shih3423bbd2014-07-16 15:47:09 -07001416 if (actualTimeUs) {
1417 *actualTimeUs = timeUs;
1418 }
1419
1420 mb->release();
1421 mb = NULL;
1422
1423 return ab;
1424}
1425
Robert Shih17f6dd62014-08-20 17:00:21 -07001426void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001427 Mutex::Autolock _l(mReadBufferLock);
1428
1429 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1430 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001431 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001432 msg->setInt32("trackType", trackType);
1433 msg->post();
1434 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001435}
1436
1437void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1438 int32_t tmpType;
1439 CHECK(msg->findInt32("trackType", &tmpType));
1440 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001441 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001442 {
1443 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001444 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001445 Mutex::Autolock _l(mReadBufferLock);
1446 mPendingReadBufferTypes &= ~(1 << trackType);
1447 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001448}
1449
Andreas Huberafed0e12011-09-20 15:39:58 -07001450void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001451 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001452 // Do not read data if Widevine source is stopped
1453 if (mStopRead) {
1454 return;
1455 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001456 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001457 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001458 switch (trackType) {
1459 case MEDIA_TRACK_TYPE_VIDEO:
1460 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001461 if (mIsWidevine) {
1462 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001463 } else {
1464 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001465 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001466 break;
1467 case MEDIA_TRACK_TYPE_AUDIO:
1468 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001469 if (mIsWidevine) {
1470 maxBuffers = 8;
1471 } else {
1472 maxBuffers = 64;
1473 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001474 break;
1475 case MEDIA_TRACK_TYPE_SUBTITLE:
1476 track = &mSubtitleTrack;
1477 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001478 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1479 track = &mTimedTextTrack;
1480 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001481 default:
1482 TRESPASS();
1483 }
1484
1485 if (track->mSource == NULL) {
1486 return;
1487 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001488
1489 if (actualTimeUs) {
1490 *actualTimeUs = seekTimeUs;
1491 }
1492
1493 MediaSource::ReadOptions options;
1494
1495 bool seeking = false;
1496
1497 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001498 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001499 seeking = true;
1500 }
1501
Chong Zhang42e81532014-12-01 13:44:26 -08001502 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001503 options.setNonBlocking();
1504 }
1505
Phil Burkc5cc2e22014-09-09 20:08:39 -07001506 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001507 MediaBuffer *mbuf;
1508 status_t err = track->mSource->read(&mbuf, &options);
1509
1510 options.clearSeekTo();
1511
1512 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001513 int64_t timeUs;
1514 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1515 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1516 mAudioTimeUs = timeUs;
1517 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1518 mVideoTimeUs = timeUs;
1519 }
1520
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001521 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001522
Wei Jia474d7c72014-12-04 15:12:13 -08001523 sp<ABuffer> buffer = mediaBufferToABuffer(
1524 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001525 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001526 formatChange = false;
1527 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001528 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001529 } else if (err == WOULD_BLOCK) {
1530 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001531 } else if (err == INFO_FORMAT_CHANGED) {
1532#if 0
1533 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001534 ATSParser::DISCONTINUITY_FORMATCHANGE,
1535 NULL,
1536 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001537#endif
1538 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001539 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001540 track->mPackets->signalEOS(err);
1541 break;
1542 }
1543 }
1544}
1545
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001546void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1547 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1548 // formatChange && seeking: track whose source is changed during selection
1549 // formatChange && !seeking: track whose source is not changed during selection
1550 // !formatChange: normal seek
1551 if ((seeking || formatChange)
1552 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1553 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1554 ATSParser::DiscontinuityType type = (formatChange && seeking)
1555 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1556 : ATSParser::DISCONTINUITY_NONE;
1557 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1558 }
1559}
1560
Andreas Huberafed0e12011-09-20 15:39:58 -07001561} // namespace android