blob: b3eb5fd657da6530c887aaa048fbbde02d8c1f52 [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) {
Robert Shihb9466482015-09-02 14:02:47 -0700470 {
471 sp<DataSource> dataSource = mDataSource;
472 sp<NuCachedSource2> cachedSource = mCachedSource;
473 sp<DataSource> httpSource = mHttpSource;
474 {
475 Mutex::Autolock _l(mDisconnectLock);
476 mDataSource.clear();
477 mCachedSource.clear();
478 mHttpSource.clear();
479 }
480 }
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700481 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700482
483 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700484 }
485 notifyPrepared(err);
486}
487
Andreas Huberafed0e12011-09-20 15:39:58 -0700488void NuPlayer::GenericSource::start() {
489 ALOGI("start");
490
Andy Hung2abde2c2014-09-30 14:40:32 -0700491 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700492 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700493 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700494 }
495
496 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700497 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700498 }
Ronghua Wu80276872014-08-28 15:50:29 -0700499
500 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
501 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800502
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800503 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700504}
505
506void NuPlayer::GenericSource::stop() {
507 // nothing to do, just account for DRM playback status
508 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
509 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800510 if (mIsWidevine || mIsSecure) {
511 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800512 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700513 sp<AMessage> response;
514 (void) msg->postAndAwaitResponse(&response);
515 }
Ronghua Wu80276872014-08-28 15:50:29 -0700516}
517
518void NuPlayer::GenericSource::pause() {
519 // nothing to do, just account for DRM playback status
520 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
521 mStarted = false;
522}
523
524void NuPlayer::GenericSource::resume() {
525 // nothing to do, just account for DRM playback status
526 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
527 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800528
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800529 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700530}
531
Chong Zhang48296b72014-09-14 14:28:45 -0700532void NuPlayer::GenericSource::disconnect() {
Robert Shihb9466482015-09-02 14:02:47 -0700533 sp<DataSource> dataSource, httpSource;
534 {
535 Mutex::Autolock _l(mDisconnectLock);
536 dataSource = mDataSource;
537 httpSource = mHttpSource;
538 }
539
540 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700541 // disconnect data source
Robert Shihb9466482015-09-02 14:02:47 -0700542 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
543 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700544 }
Robert Shihb9466482015-09-02 14:02:47 -0700545 } else if (httpSource != NULL) {
546 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700547 }
548}
549
Ronghua Wu80276872014-08-28 15:50:29 -0700550void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
551 if (mDecryptHandle != NULL) {
552 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
553 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700554 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
555 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700556}
557
558status_t NuPlayer::GenericSource::feedMoreTSData() {
559 return OK;
560}
561
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700562void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800563 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700564 msg->setInt32("generation", mPollBufferingGeneration);
565 msg->post(1000000ll);
566}
567
568void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800569 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700570 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800571 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700572}
573
Chong Zhangefbb6192015-01-30 17:13:27 -0800574void NuPlayer::GenericSource::restartPollBuffering() {
575 if (mIsStreaming) {
576 cancelPollBuffering();
577 onPollBuffering();
578 }
579}
580
Chong Zhangc287cad2015-02-19 18:30:30 -0800581void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
582 // Buffering percent could go backward as it's estimated from remaining
583 // data and last access time. This could cause the buffering position
584 // drawn on media control to jitter slightly. Remember previously reported
585 // percentage and don't allow it to go backward.
586 if (percentage < mPrevBufferPercentage) {
587 percentage = mPrevBufferPercentage;
588 } else if (percentage > 100) {
589 percentage = 100;
590 }
591
592 mPrevBufferPercentage = percentage;
593
Chong Zhangefbb6192015-01-30 17:13:27 -0800594 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
595
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700596 sp<AMessage> msg = dupNotify();
597 msg->setInt32("what", kWhatBufferingUpdate);
598 msg->setInt32("percentage", percentage);
599 msg->post();
600}
601
Chong Zhangefbb6192015-01-30 17:13:27 -0800602void NuPlayer::GenericSource::startBufferingIfNecessary() {
603 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
604 mPrepareBuffering, mBuffering);
605
606 if (mPrepareBuffering) {
607 return;
608 }
609
610 if (!mBuffering) {
611 mBuffering = true;
612
613 ensureCacheIsFetching();
614 sendCacheStats();
615
616 sp<AMessage> notify = dupNotify();
617 notify->setInt32("what", kWhatPauseOnBufferingStart);
618 notify->post();
619 }
620}
621
622void NuPlayer::GenericSource::stopBufferingIfNecessary() {
623 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
624 mPrepareBuffering, mBuffering);
625
626 if (mPrepareBuffering) {
627 mPrepareBuffering = false;
628 notifyPrepared();
629 return;
630 }
631
632 if (mBuffering) {
633 mBuffering = false;
634
635 sendCacheStats();
636
637 sp<AMessage> notify = dupNotify();
638 notify->setInt32("what", kWhatResumeOnBufferingEnd);
639 notify->post();
640 }
641}
642
643void NuPlayer::GenericSource::sendCacheStats() {
644 int32_t kbps = 0;
645 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700646
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800647 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800648 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800649 } else if (mCachedSource != NULL) {
650 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800651 }
652
653 if (err == OK) {
654 sp<AMessage> notify = dupNotify();
655 notify->setInt32("what", kWhatCacheStats);
656 notify->setInt32("bandwidth", kbps);
657 notify->post();
658 }
659}
660
661void NuPlayer::GenericSource::ensureCacheIsFetching() {
662 if (mCachedSource != NULL) {
663 mCachedSource->resumeFetchingIfNecessary();
664 }
665}
666
667void NuPlayer::GenericSource::onPollBuffering() {
668 status_t finalStatus = UNKNOWN_ERROR;
669 int64_t cachedDurationUs = -1ll;
670 ssize_t cachedDataRemaining = -1;
671
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800672 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
673 "WVMExtractor and NuCachedSource both present");
674
675 if (mWVMExtractor != NULL) {
676 cachedDurationUs =
677 mWVMExtractor->getCachedDurationUs(&finalStatus);
678 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800679 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700680 mCachedSource->approxDataRemaining(&finalStatus);
681
682 if (finalStatus == OK) {
683 off64_t size;
684 int64_t bitrate = 0ll;
685 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
686 bitrate = size * 8000000ll / mDurationUs;
687 } else if (mBitrate > 0) {
688 bitrate = mBitrate;
689 }
690 if (bitrate > 0) {
691 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
692 }
693 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700694 }
695
Chong Zhangefbb6192015-01-30 17:13:27 -0800696 if (finalStatus != OK) {
697 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
698
699 if (finalStatus == ERROR_END_OF_STREAM) {
700 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700701 }
702
Chong Zhangefbb6192015-01-30 17:13:27 -0800703 stopBufferingIfNecessary();
704 return;
705 } else if (cachedDurationUs >= 0ll) {
706 if (mDurationUs > 0ll) {
707 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
708 int percentage = 100.0 * cachedPosUs / mDurationUs;
709 if (percentage > 100) {
710 percentage = 100;
711 }
712
713 notifyBufferingUpdate(percentage);
714 }
715
716 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
717 cachedDurationUs / 1000000.0f);
718
719 if (cachedDurationUs < kLowWaterMarkUs) {
720 startBufferingIfNecessary();
721 } else if (cachedDurationUs > kHighWaterMarkUs) {
722 stopBufferingIfNecessary();
723 }
724 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700725 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800726 cachedDataRemaining);
727
728 if (cachedDataRemaining < kLowWaterMarkBytes) {
729 startBufferingIfNecessary();
730 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
731 stopBufferingIfNecessary();
732 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700733 }
734
735 schedulePollBuffering();
736}
737
Robert Shih3423bbd2014-07-16 15:47:09 -0700738void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
739 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700740 case kWhatPrepareAsync:
741 {
742 onPrepareAsync();
743 break;
744 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700745 case kWhatFetchSubtitleData:
746 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700747 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
748 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
749 break;
750 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700751
Lajos Molnare26940f2014-07-31 10:31:26 -0700752 case kWhatFetchTimedTextData:
753 {
754 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
755 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700756 break;
757 }
758
759 case kWhatSendSubtitleData:
760 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700761 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
762 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
763 break;
764 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700765
Lajos Molnare26940f2014-07-31 10:31:26 -0700766 case kWhatSendTimedTextData:
767 {
768 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
769 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700770 break;
771 }
772
773 case kWhatChangeAVSource:
774 {
775 int32_t trackIndex;
776 CHECK(msg->findInt32("trackIndex", &trackIndex));
777 const sp<MediaSource> source = mSources.itemAt(trackIndex);
778
779 Track* track;
780 const char *mime;
781 media_track_type trackType, counterpartType;
782 sp<MetaData> meta = source->getFormat();
783 meta->findCString(kKeyMIMEType, &mime);
784 if (!strncasecmp(mime, "audio/", 6)) {
785 track = &mAudioTrack;
786 trackType = MEDIA_TRACK_TYPE_AUDIO;
787 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
788 } else {
789 CHECK(!strncasecmp(mime, "video/", 6));
790 track = &mVideoTrack;
791 trackType = MEDIA_TRACK_TYPE_VIDEO;
792 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
793 }
794
795
796 if (track->mSource != NULL) {
797 track->mSource->stop();
798 }
799 track->mSource = source;
800 track->mSource->start();
801 track->mIndex = trackIndex;
802
Robert Shih3423bbd2014-07-16 15:47:09 -0700803 int64_t timeUs, actualTimeUs;
804 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800805 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
806 timeUs = mAudioLastDequeueTimeUs;
807 } else {
808 timeUs = mVideoLastDequeueTimeUs;
809 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700810 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
811 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700812 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700813
814 break;
815 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800816
817 case kWhatStart:
818 case kWhatResume:
819 {
820 restartPollBuffering();
821 break;
822 }
823
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700824 case kWhatPollBuffering:
825 {
826 int32_t generation;
827 CHECK(msg->findInt32("generation", &generation));
828 if (generation == mPollBufferingGeneration) {
829 onPollBuffering();
830 }
831 break;
832 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700833
834 case kWhatGetFormat:
835 {
836 onGetFormatMeta(msg);
837 break;
838 }
839
840 case kWhatGetSelectedTrack:
841 {
842 onGetSelectedTrack(msg);
843 break;
844 }
845
846 case kWhatSelectTrack:
847 {
848 onSelectTrack(msg);
849 break;
850 }
851
852 case kWhatSeek:
853 {
854 onSeek(msg);
855 break;
856 }
857
858 case kWhatReadBuffer:
859 {
860 onReadBuffer(msg);
861 break;
862 }
863
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700864 case kWhatSecureDecodersInstantiated:
865 {
866 int32_t err;
867 CHECK(msg->findInt32("err", &err));
868 onSecureDecodersInstantiated(err);
869 break;
870 }
871
Andy Hung2abde2c2014-09-30 14:40:32 -0700872 case kWhatStopWidevine:
873 {
874 // mStopRead is only used for Widevine to prevent the video source
875 // from being read while the associated video decoder is shutting down.
876 mStopRead = true;
877 if (mVideoTrack.mSource != NULL) {
878 mVideoTrack.mPackets->clear();
879 }
880 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800881 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700882 CHECK(msg->senderAwaitsResponse(&replyID));
883 response->postReply(replyID);
884 break;
885 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700886 default:
887 Source::onMessageReceived(msg);
888 break;
889 }
890}
891
Lajos Molnare26940f2014-07-31 10:31:26 -0700892void NuPlayer::GenericSource::fetchTextData(
893 uint32_t sendWhat,
894 media_track_type type,
895 int32_t curGen,
896 sp<AnotherPacketSource> packets,
897 sp<AMessage> msg) {
898 int32_t msgGeneration;
899 CHECK(msg->findInt32("generation", &msgGeneration));
900 if (msgGeneration != curGen) {
901 // stale
902 return;
903 }
904
905 int32_t avail;
906 if (packets->hasBufferAvailable(&avail)) {
907 return;
908 }
909
910 int64_t timeUs;
911 CHECK(msg->findInt64("timeUs", &timeUs));
912
913 int64_t subTimeUs;
914 readBuffer(type, timeUs, &subTimeUs);
915
916 int64_t delayUs = subTimeUs - timeUs;
917 if (msg->what() == kWhatFetchSubtitleData) {
918 const int64_t oneSecUs = 1000000ll;
919 delayUs -= oneSecUs;
920 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800921 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700922 msg2->setInt32("generation", msgGeneration);
923 msg2->post(delayUs < 0 ? 0 : delayUs);
924}
925
926void NuPlayer::GenericSource::sendTextData(
927 uint32_t what,
928 media_track_type type,
929 int32_t curGen,
930 sp<AnotherPacketSource> packets,
931 sp<AMessage> msg) {
932 int32_t msgGeneration;
933 CHECK(msg->findInt32("generation", &msgGeneration));
934 if (msgGeneration != curGen) {
935 // stale
936 return;
937 }
938
939 int64_t subTimeUs;
940 if (packets->nextBufferTime(&subTimeUs) != OK) {
941 return;
942 }
943
944 int64_t nextSubTimeUs;
945 readBuffer(type, -1, &nextSubTimeUs);
946
947 sp<ABuffer> buffer;
948 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
949 if (dequeueStatus == OK) {
950 sp<AMessage> notify = dupNotify();
951 notify->setInt32("what", what);
952 notify->setBuffer("buffer", buffer);
953 notify->post();
954
955 const int64_t delayUs = nextSubTimeUs - subTimeUs;
956 msg->post(delayUs < 0 ? 0 : delayUs);
957 }
958}
959
Andreas Huber84066782011-08-16 09:34:26 -0700960sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800961 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700962 msg->setInt32("audio", audio);
963
964 sp<AMessage> response;
965 void *format;
966 status_t err = msg->postAndAwaitResponse(&response);
967 if (err == OK && response != NULL) {
968 CHECK(response->findPointer("format", &format));
969 return (MetaData *)format;
970 } else {
971 return NULL;
972 }
973}
974
975void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
976 int32_t audio;
977 CHECK(msg->findInt32("audio", &audio));
978
979 sp<AMessage> response = new AMessage;
980 sp<MetaData> format = doGetFormatMeta(audio);
981 response->setPointer("format", format.get());
982
Lajos Molnar3f274362015-03-05 14:35:41 -0800983 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -0700984 CHECK(msg->senderAwaitsResponse(&replyID));
985 response->postReply(replyID);
986}
987
988sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700989 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
990
991 if (source == NULL) {
992 return NULL;
993 }
994
995 return source->getFormat();
996}
997
998status_t NuPlayer::GenericSource::dequeueAccessUnit(
999 bool audio, sp<ABuffer> *accessUnit) {
1000 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1001
1002 if (track->mSource == NULL) {
1003 return -EWOULDBLOCK;
1004 }
1005
Lajos Molnarcc227032014-07-17 15:33:06 -07001006 if (mIsWidevine && !audio) {
1007 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001008 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001009 }
1010
Andreas Huberafed0e12011-09-20 15:39:58 -07001011 status_t finalResult;
1012 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001013 if (finalResult == OK) {
1014 postReadBuffer(
1015 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1016 return -EWOULDBLOCK;
1017 }
1018 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001019 }
1020
1021 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1022
Chong Zhangfcf044a2015-07-14 15:58:51 -07001023 // start pulling in more buffers if we only have one (or no) buffer left
1024 // so that decoder has less chance of being starved
1025 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001026 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001027 }
1028
Robert Shih3423bbd2014-07-16 15:47:09 -07001029 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001030 if (mSubtitleTrack.mSource != NULL) {
1031 mSubtitleTrack.mPackets->clear();
1032 mFetchSubtitleDataGeneration++;
1033 }
1034 if (mTimedTextTrack.mSource != NULL) {
1035 mTimedTextTrack.mPackets->clear();
1036 mFetchTimedTextDataGeneration++;
1037 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001038 return result;
1039 }
1040
1041 int64_t timeUs;
1042 status_t eosResult; // ignored
1043 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001044 if (audio) {
1045 mAudioLastDequeueTimeUs = timeUs;
1046 } else {
1047 mVideoLastDequeueTimeUs = timeUs;
1048 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001049
1050 if (mSubtitleTrack.mSource != NULL
1051 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001052 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001053 msg->setInt64("timeUs", timeUs);
1054 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1055 msg->post();
1056 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001057
Lajos Molnare26940f2014-07-31 10:31:26 -07001058 if (mTimedTextTrack.mSource != NULL
1059 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001060 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001061 msg->setInt64("timeUs", timeUs);
1062 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1063 msg->post();
1064 }
1065
Andreas Huberafed0e12011-09-20 15:39:58 -07001066 return result;
1067}
1068
1069status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1070 *durationUs = mDurationUs;
1071 return OK;
1072}
1073
Robert Shihdd235722014-06-12 14:49:23 -07001074size_t NuPlayer::GenericSource::getTrackCount() const {
1075 return mSources.size();
1076}
1077
1078sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1079 size_t trackCount = mSources.size();
1080 if (trackIndex >= trackCount) {
1081 return NULL;
1082 }
1083
1084 sp<AMessage> format = new AMessage();
1085 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1086
1087 const char *mime;
1088 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001089 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001090
1091 int32_t trackType;
1092 if (!strncasecmp(mime, "video/", 6)) {
1093 trackType = MEDIA_TRACK_TYPE_VIDEO;
1094 } else if (!strncasecmp(mime, "audio/", 6)) {
1095 trackType = MEDIA_TRACK_TYPE_AUDIO;
1096 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1097 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1098 } else {
1099 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1100 }
1101 format->setInt32("type", trackType);
1102
1103 const char *lang;
1104 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1105 lang = "und";
1106 }
1107 format->setString("language", lang);
1108
1109 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001110 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1111 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1112 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1113 meta->findInt32(kKeyTrackIsForced, &isForced);
1114
1115 format->setInt32("auto", !!isAutoselect);
1116 format->setInt32("default", !!isDefault);
1117 format->setInt32("forced", !!isForced);
1118 }
1119
1120 return format;
1121}
1122
Lajos Molnare26940f2014-07-31 10:31:26 -07001123ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001124 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001125 msg->setInt32("type", type);
1126
1127 sp<AMessage> response;
1128 int32_t index;
1129 status_t err = msg->postAndAwaitResponse(&response);
1130 if (err == OK && response != NULL) {
1131 CHECK(response->findInt32("index", &index));
1132 return index;
1133 } else {
1134 return -1;
1135 }
1136}
1137
1138void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1139 int32_t tmpType;
1140 CHECK(msg->findInt32("type", &tmpType));
1141 media_track_type type = (media_track_type)tmpType;
1142
1143 sp<AMessage> response = new AMessage;
1144 ssize_t index = doGetSelectedTrack(type);
1145 response->setInt32("index", index);
1146
Lajos Molnar3f274362015-03-05 14:35:41 -08001147 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001148 CHECK(msg->senderAwaitsResponse(&replyID));
1149 response->postReply(replyID);
1150}
1151
1152ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001153 const Track *track = NULL;
1154 switch (type) {
1155 case MEDIA_TRACK_TYPE_VIDEO:
1156 track = &mVideoTrack;
1157 break;
1158 case MEDIA_TRACK_TYPE_AUDIO:
1159 track = &mAudioTrack;
1160 break;
1161 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1162 track = &mTimedTextTrack;
1163 break;
1164 case MEDIA_TRACK_TYPE_SUBTITLE:
1165 track = &mSubtitleTrack;
1166 break;
1167 default:
1168 break;
1169 }
1170
1171 if (track != NULL && track->mSource != NULL) {
1172 return track->mIndex;
1173 }
1174
1175 return -1;
1176}
1177
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001178status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001179 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001180 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001181 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001182 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001183 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001184
1185 sp<AMessage> response;
1186 status_t err = msg->postAndAwaitResponse(&response);
1187 if (err == OK && response != NULL) {
1188 CHECK(response->findInt32("err", &err));
1189 }
1190
1191 return err;
1192}
1193
1194void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1195 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001196 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001197 CHECK(msg->findInt32("trackIndex", &trackIndex));
1198 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001199 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001200
1201 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001202 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001203 response->setInt32("err", err);
1204
Lajos Molnar3f274362015-03-05 14:35:41 -08001205 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001206 CHECK(msg->senderAwaitsResponse(&replyID));
1207 response->postReply(replyID);
1208}
1209
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001210status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001211 if (trackIndex >= mSources.size()) {
1212 return BAD_INDEX;
1213 }
1214
1215 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001216 Track* track = NULL;
1217 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1218 track = &mSubtitleTrack;
1219 mFetchSubtitleDataGeneration++;
1220 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1221 track = &mTimedTextTrack;
1222 mFetchTimedTextDataGeneration++;
1223 }
1224 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001225 return INVALID_OPERATION;
1226 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001227 track->mSource->stop();
1228 track->mSource = NULL;
1229 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001230 return OK;
1231 }
1232
1233 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1234 sp<MetaData> meta = source->getFormat();
1235 const char *mime;
1236 CHECK(meta->findCString(kKeyMIMEType, &mime));
1237 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001238 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1239 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1240 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001241 return OK;
1242 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001243 track->mIndex = trackIndex;
1244 if (track->mSource != NULL) {
1245 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001246 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001247 track->mSource = mSources.itemAt(trackIndex);
1248 track->mSource->start();
1249 if (track->mPackets == NULL) {
1250 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001251 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001252 track->mPackets->clear();
1253 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001254
1255 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001256
1257 if (isSubtitle) {
1258 mFetchSubtitleDataGeneration++;
1259 } else {
1260 mFetchTimedTextDataGeneration++;
1261 }
1262
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001263 status_t eosResult; // ignored
1264 if (mSubtitleTrack.mSource != NULL
1265 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001266 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001267 msg->setInt64("timeUs", timeUs);
1268 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1269 msg->post();
1270 }
1271
1272 if (mTimedTextTrack.mSource != NULL
1273 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001274 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001275 msg->setInt64("timeUs", timeUs);
1276 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1277 msg->post();
1278 }
1279
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 return OK;
1281 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1282 bool audio = !strncasecmp(mime, "audio/", 6);
1283 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1284 if (track->mSource != NULL && track->mIndex == trackIndex) {
1285 return OK;
1286 }
1287
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001288 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001289 msg->setInt32("trackIndex", trackIndex);
1290 msg->post();
1291 return OK;
1292 }
1293
1294 return INVALID_OPERATION;
1295}
1296
Andreas Huberafed0e12011-09-20 15:39:58 -07001297status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001298 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001299 msg->setInt64("seekTimeUs", seekTimeUs);
1300
1301 sp<AMessage> response;
1302 status_t err = msg->postAndAwaitResponse(&response);
1303 if (err == OK && response != NULL) {
1304 CHECK(response->findInt32("err", &err));
1305 }
1306
1307 return err;
1308}
1309
1310void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1311 int64_t seekTimeUs;
1312 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1313
1314 sp<AMessage> response = new AMessage;
1315 status_t err = doSeek(seekTimeUs);
1316 response->setInt32("err", err);
1317
Lajos Molnar3f274362015-03-05 14:35:41 -08001318 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001319 CHECK(msg->senderAwaitsResponse(&replyID));
1320 response->postReply(replyID);
1321}
1322
1323status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001324 // If the Widevine source is stopped, do not attempt to read any
1325 // more buffers.
1326 if (mStopRead) {
1327 return INVALID_OPERATION;
1328 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001329 if (mVideoTrack.mSource != NULL) {
1330 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001331 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001332
1333 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001334 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001335 }
1336
1337 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001338 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001339 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001340 }
1341
Ronghua Wu80276872014-08-28 15:50:29 -07001342 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1343 if (!mStarted) {
1344 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1345 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001346
1347 // If currently buffering, post kWhatBufferingEnd first, so that
1348 // NuPlayer resumes. Otherwise, if cache hits high watermark
1349 // before new polling happens, no one will resume the playback.
1350 stopBufferingIfNecessary();
1351 restartPollBuffering();
1352
Andreas Huberafed0e12011-09-20 15:39:58 -07001353 return OK;
1354}
1355
Robert Shih3423bbd2014-07-16 15:47:09 -07001356sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1357 MediaBuffer* mb,
1358 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001359 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001360 int64_t *actualTimeUs) {
1361 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1362 size_t outLength = mb->range_length();
1363
1364 if (audio && mAudioIsVorbis) {
1365 outLength += sizeof(int32_t);
1366 }
1367
1368 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001369 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001370 // data is already provided in the buffer
1371 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001372 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001373 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001374 } else {
1375 ab = new ABuffer(outLength);
1376 memcpy(ab->data(),
1377 (const uint8_t *)mb->data() + mb->range_offset(),
1378 mb->range_length());
1379 }
1380
1381 if (audio && mAudioIsVorbis) {
1382 int32_t numPageSamples;
1383 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1384 numPageSamples = -1;
1385 }
1386
1387 uint8_t* abEnd = ab->data() + mb->range_length();
1388 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1389 }
1390
Lajos Molnare26940f2014-07-31 10:31:26 -07001391 sp<AMessage> meta = ab->meta();
1392
Robert Shih3423bbd2014-07-16 15:47:09 -07001393 int64_t timeUs;
1394 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001395 meta->setInt64("timeUs", timeUs);
1396
Wei Jia474d7c72014-12-04 15:12:13 -08001397#if 0
1398 // Temporarily disable pre-roll till we have a full solution to handle
1399 // both single seek and continous seek gracefully.
1400 if (seekTimeUs > timeUs) {
1401 sp<AMessage> extra = new AMessage;
1402 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1403 meta->setMessage("extra", extra);
1404 }
1405#endif
1406
Lajos Molnare26940f2014-07-31 10:31:26 -07001407 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1408 const char *mime;
1409 CHECK(mTimedTextTrack.mSource != NULL
1410 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1411 meta->setString("mime", mime);
1412 }
1413
Robert Shih3423bbd2014-07-16 15:47:09 -07001414 int64_t durationUs;
1415 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1416 meta->setInt64("durationUs", durationUs);
1417 }
1418
1419 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1420 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1421 }
1422
Robert Shihf8bd8512015-04-23 16:39:18 -07001423 uint32_t dataType; // unused
1424 const void *seiData;
1425 size_t seiLength;
1426 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1427 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1428 meta->setBuffer("sei", sei);
1429 }
1430
Robert Shih3423bbd2014-07-16 15:47:09 -07001431 if (actualTimeUs) {
1432 *actualTimeUs = timeUs;
1433 }
1434
1435 mb->release();
1436 mb = NULL;
1437
1438 return ab;
1439}
1440
Robert Shih17f6dd62014-08-20 17:00:21 -07001441void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001442 Mutex::Autolock _l(mReadBufferLock);
1443
1444 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1445 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001446 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001447 msg->setInt32("trackType", trackType);
1448 msg->post();
1449 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001450}
1451
1452void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1453 int32_t tmpType;
1454 CHECK(msg->findInt32("trackType", &tmpType));
1455 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001456 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001457 {
1458 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001459 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001460 Mutex::Autolock _l(mReadBufferLock);
1461 mPendingReadBufferTypes &= ~(1 << trackType);
1462 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001463}
1464
Andreas Huberafed0e12011-09-20 15:39:58 -07001465void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001466 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001467 // Do not read data if Widevine source is stopped
1468 if (mStopRead) {
1469 return;
1470 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001471 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001472 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001473 switch (trackType) {
1474 case MEDIA_TRACK_TYPE_VIDEO:
1475 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001476 if (mIsWidevine) {
1477 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001478 } else {
1479 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001480 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001481 break;
1482 case MEDIA_TRACK_TYPE_AUDIO:
1483 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001484 if (mIsWidevine) {
1485 maxBuffers = 8;
1486 } else {
1487 maxBuffers = 64;
1488 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001489 break;
1490 case MEDIA_TRACK_TYPE_SUBTITLE:
1491 track = &mSubtitleTrack;
1492 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001493 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1494 track = &mTimedTextTrack;
1495 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001496 default:
1497 TRESPASS();
1498 }
1499
1500 if (track->mSource == NULL) {
1501 return;
1502 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001503
1504 if (actualTimeUs) {
1505 *actualTimeUs = seekTimeUs;
1506 }
1507
1508 MediaSource::ReadOptions options;
1509
1510 bool seeking = false;
1511
1512 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001513 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001514 seeking = true;
1515 }
1516
Chong Zhang42e81532014-12-01 13:44:26 -08001517 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001518 options.setNonBlocking();
1519 }
1520
Phil Burkc5cc2e22014-09-09 20:08:39 -07001521 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001522 MediaBuffer *mbuf;
1523 status_t err = track->mSource->read(&mbuf, &options);
1524
1525 options.clearSeekTo();
1526
1527 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001528 int64_t timeUs;
1529 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1530 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1531 mAudioTimeUs = timeUs;
1532 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1533 mVideoTimeUs = timeUs;
1534 }
1535
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001536 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001537
Wei Jia474d7c72014-12-04 15:12:13 -08001538 sp<ABuffer> buffer = mediaBufferToABuffer(
1539 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001540 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001541 formatChange = false;
1542 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001543 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001544 } else if (err == WOULD_BLOCK) {
1545 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001546 } else if (err == INFO_FORMAT_CHANGED) {
1547#if 0
1548 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001549 ATSParser::DISCONTINUITY_FORMATCHANGE,
1550 NULL,
1551 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001552#endif
1553 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001554 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001555 track->mPackets->signalEOS(err);
1556 break;
1557 }
1558 }
1559}
1560
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001561void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1562 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1563 // formatChange && seeking: track whose source is changed during selection
1564 // formatChange && !seeking: track whose source is not changed during selection
1565 // !formatChange: normal seek
1566 if ((seeking || formatChange)
1567 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1568 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1569 ATSParser::DiscontinuityType type = (formatChange && seeking)
1570 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1571 : ATSParser::DISCONTINUITY_NONE;
1572 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1573 }
1574}
1575
Andreas Huberafed0e12011-09-20 15:39:58 -07001576} // namespace android