blob: 51fba1b80eab75b414b65fc3853c1d5bea74f407 [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
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700751 case kWhatSendGlobalTimedTextData:
752 {
753 sendGlobalTextData(kWhatTimedTextData, mFetchTimedTextDataGeneration, msg);
754 break;
755 }
Lajos Molnare26940f2014-07-31 10:31:26 -0700756 case kWhatSendTimedTextData:
757 {
758 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
759 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700760 break;
761 }
762
763 case kWhatChangeAVSource:
764 {
765 int32_t trackIndex;
766 CHECK(msg->findInt32("trackIndex", &trackIndex));
767 const sp<MediaSource> source = mSources.itemAt(trackIndex);
768
769 Track* track;
770 const char *mime;
771 media_track_type trackType, counterpartType;
772 sp<MetaData> meta = source->getFormat();
773 meta->findCString(kKeyMIMEType, &mime);
774 if (!strncasecmp(mime, "audio/", 6)) {
775 track = &mAudioTrack;
776 trackType = MEDIA_TRACK_TYPE_AUDIO;
777 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
778 } else {
779 CHECK(!strncasecmp(mime, "video/", 6));
780 track = &mVideoTrack;
781 trackType = MEDIA_TRACK_TYPE_VIDEO;
782 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
783 }
784
785
786 if (track->mSource != NULL) {
787 track->mSource->stop();
788 }
789 track->mSource = source;
790 track->mSource->start();
791 track->mIndex = trackIndex;
792
Robert Shih3423bbd2014-07-16 15:47:09 -0700793 int64_t timeUs, actualTimeUs;
794 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800795 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
796 timeUs = mAudioLastDequeueTimeUs;
797 } else {
798 timeUs = mVideoLastDequeueTimeUs;
799 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700800 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
801 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700802 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700803
804 break;
805 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800806
807 case kWhatStart:
808 case kWhatResume:
809 {
810 restartPollBuffering();
811 break;
812 }
813
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700814 case kWhatPollBuffering:
815 {
816 int32_t generation;
817 CHECK(msg->findInt32("generation", &generation));
818 if (generation == mPollBufferingGeneration) {
819 onPollBuffering();
820 }
821 break;
822 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700823
824 case kWhatGetFormat:
825 {
826 onGetFormatMeta(msg);
827 break;
828 }
829
830 case kWhatGetSelectedTrack:
831 {
832 onGetSelectedTrack(msg);
833 break;
834 }
835
836 case kWhatSelectTrack:
837 {
838 onSelectTrack(msg);
839 break;
840 }
841
842 case kWhatSeek:
843 {
844 onSeek(msg);
845 break;
846 }
847
848 case kWhatReadBuffer:
849 {
850 onReadBuffer(msg);
851 break;
852 }
853
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700854 case kWhatSecureDecodersInstantiated:
855 {
856 int32_t err;
857 CHECK(msg->findInt32("err", &err));
858 onSecureDecodersInstantiated(err);
859 break;
860 }
861
Andy Hung2abde2c2014-09-30 14:40:32 -0700862 case kWhatStopWidevine:
863 {
864 // mStopRead is only used for Widevine to prevent the video source
865 // from being read while the associated video decoder is shutting down.
866 mStopRead = true;
867 if (mVideoTrack.mSource != NULL) {
868 mVideoTrack.mPackets->clear();
869 }
870 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800871 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700872 CHECK(msg->senderAwaitsResponse(&replyID));
873 response->postReply(replyID);
874 break;
875 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700876 default:
877 Source::onMessageReceived(msg);
878 break;
879 }
880}
881
Lajos Molnare26940f2014-07-31 10:31:26 -0700882void NuPlayer::GenericSource::fetchTextData(
883 uint32_t sendWhat,
884 media_track_type type,
885 int32_t curGen,
886 sp<AnotherPacketSource> packets,
887 sp<AMessage> msg) {
888 int32_t msgGeneration;
889 CHECK(msg->findInt32("generation", &msgGeneration));
890 if (msgGeneration != curGen) {
891 // stale
892 return;
893 }
894
895 int32_t avail;
896 if (packets->hasBufferAvailable(&avail)) {
897 return;
898 }
899
900 int64_t timeUs;
901 CHECK(msg->findInt64("timeUs", &timeUs));
902
903 int64_t subTimeUs;
904 readBuffer(type, timeUs, &subTimeUs);
905
906 int64_t delayUs = subTimeUs - timeUs;
907 if (msg->what() == kWhatFetchSubtitleData) {
908 const int64_t oneSecUs = 1000000ll;
909 delayUs -= oneSecUs;
910 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800911 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700912 msg2->setInt32("generation", msgGeneration);
913 msg2->post(delayUs < 0 ? 0 : delayUs);
914}
915
916void NuPlayer::GenericSource::sendTextData(
917 uint32_t what,
918 media_track_type type,
919 int32_t curGen,
920 sp<AnotherPacketSource> packets,
921 sp<AMessage> msg) {
922 int32_t msgGeneration;
923 CHECK(msg->findInt32("generation", &msgGeneration));
924 if (msgGeneration != curGen) {
925 // stale
926 return;
927 }
928
929 int64_t subTimeUs;
930 if (packets->nextBufferTime(&subTimeUs) != OK) {
931 return;
932 }
933
934 int64_t nextSubTimeUs;
935 readBuffer(type, -1, &nextSubTimeUs);
936
937 sp<ABuffer> buffer;
938 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
939 if (dequeueStatus == OK) {
940 sp<AMessage> notify = dupNotify();
941 notify->setInt32("what", what);
942 notify->setBuffer("buffer", buffer);
943 notify->post();
944
945 const int64_t delayUs = nextSubTimeUs - subTimeUs;
946 msg->post(delayUs < 0 ? 0 : delayUs);
947 }
948}
949
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700950void NuPlayer::GenericSource::sendGlobalTextData(
951 uint32_t what,
952 int32_t curGen,
953 sp<AMessage> msg) {
954 int32_t msgGeneration;
955 CHECK(msg->findInt32("generation", &msgGeneration));
956 if (msgGeneration != curGen) {
957 // stale
958 return;
959 }
960
961 uint32_t textType;
962 const void *data;
963 size_t size = 0;
964 if (mTimedTextTrack.mSource->getFormat()->findData(
965 kKeyTextFormatData, &textType, &data, &size)) {
966 mGlobalTimedText = new ABuffer(size);
967 if (mGlobalTimedText->data()) {
968 memcpy(mGlobalTimedText->data(), data, size);
969 sp<AMessage> globalMeta = mGlobalTimedText->meta();
970 globalMeta->setInt64("timeUs", 0);
971 globalMeta->setString("mime", MEDIA_MIMETYPE_TEXT_3GPP);
972 globalMeta->setInt32("global", 1);
973 sp<AMessage> notify = dupNotify();
974 notify->setInt32("what", what);
975 notify->setBuffer("buffer", mGlobalTimedText);
976 notify->post();
977 }
978 }
979}
980
Andreas Huber84066782011-08-16 09:34:26 -0700981sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800982 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700983 msg->setInt32("audio", audio);
984
985 sp<AMessage> response;
986 void *format;
987 status_t err = msg->postAndAwaitResponse(&response);
988 if (err == OK && response != NULL) {
989 CHECK(response->findPointer("format", &format));
990 return (MetaData *)format;
991 } else {
992 return NULL;
993 }
994}
995
996void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
997 int32_t audio;
998 CHECK(msg->findInt32("audio", &audio));
999
1000 sp<AMessage> response = new AMessage;
1001 sp<MetaData> format = doGetFormatMeta(audio);
1002 response->setPointer("format", format.get());
1003
Lajos Molnar3f274362015-03-05 14:35:41 -08001004 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001005 CHECK(msg->senderAwaitsResponse(&replyID));
1006 response->postReply(replyID);
1007}
1008
1009sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001010 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1011
1012 if (source == NULL) {
1013 return NULL;
1014 }
1015
1016 return source->getFormat();
1017}
1018
1019status_t NuPlayer::GenericSource::dequeueAccessUnit(
1020 bool audio, sp<ABuffer> *accessUnit) {
1021 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1022
1023 if (track->mSource == NULL) {
1024 return -EWOULDBLOCK;
1025 }
1026
Lajos Molnarcc227032014-07-17 15:33:06 -07001027 if (mIsWidevine && !audio) {
1028 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001029 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001030 }
1031
Andreas Huberafed0e12011-09-20 15:39:58 -07001032 status_t finalResult;
1033 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001034 if (finalResult == OK) {
1035 postReadBuffer(
1036 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1037 return -EWOULDBLOCK;
1038 }
1039 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001040 }
1041
1042 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1043
Chong Zhangfcf044a2015-07-14 15:58:51 -07001044 // start pulling in more buffers if we only have one (or no) buffer left
1045 // so that decoder has less chance of being starved
1046 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001047 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001048 }
1049
Robert Shih3423bbd2014-07-16 15:47:09 -07001050 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001051 if (mSubtitleTrack.mSource != NULL) {
1052 mSubtitleTrack.mPackets->clear();
1053 mFetchSubtitleDataGeneration++;
1054 }
1055 if (mTimedTextTrack.mSource != NULL) {
1056 mTimedTextTrack.mPackets->clear();
1057 mFetchTimedTextDataGeneration++;
1058 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001059 return result;
1060 }
1061
1062 int64_t timeUs;
1063 status_t eosResult; // ignored
1064 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001065 if (audio) {
1066 mAudioLastDequeueTimeUs = timeUs;
1067 } else {
1068 mVideoLastDequeueTimeUs = timeUs;
1069 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001070
1071 if (mSubtitleTrack.mSource != NULL
1072 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001073 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001074 msg->setInt64("timeUs", timeUs);
1075 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1076 msg->post();
1077 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001078
Lajos Molnare26940f2014-07-31 10:31:26 -07001079 if (mTimedTextTrack.mSource != NULL
1080 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001081 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001082 msg->setInt64("timeUs", timeUs);
1083 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1084 msg->post();
1085 }
1086
Andreas Huberafed0e12011-09-20 15:39:58 -07001087 return result;
1088}
1089
1090status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1091 *durationUs = mDurationUs;
1092 return OK;
1093}
1094
Robert Shihdd235722014-06-12 14:49:23 -07001095size_t NuPlayer::GenericSource::getTrackCount() const {
1096 return mSources.size();
1097}
1098
1099sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1100 size_t trackCount = mSources.size();
1101 if (trackIndex >= trackCount) {
1102 return NULL;
1103 }
1104
1105 sp<AMessage> format = new AMessage();
1106 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1107
1108 const char *mime;
1109 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001110 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001111
1112 int32_t trackType;
1113 if (!strncasecmp(mime, "video/", 6)) {
1114 trackType = MEDIA_TRACK_TYPE_VIDEO;
1115 } else if (!strncasecmp(mime, "audio/", 6)) {
1116 trackType = MEDIA_TRACK_TYPE_AUDIO;
1117 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1118 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1119 } else {
1120 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1121 }
1122 format->setInt32("type", trackType);
1123
1124 const char *lang;
1125 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1126 lang = "und";
1127 }
1128 format->setString("language", lang);
1129
1130 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001131 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1132 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1133 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1134 meta->findInt32(kKeyTrackIsForced, &isForced);
1135
1136 format->setInt32("auto", !!isAutoselect);
1137 format->setInt32("default", !!isDefault);
1138 format->setInt32("forced", !!isForced);
1139 }
1140
1141 return format;
1142}
1143
Lajos Molnare26940f2014-07-31 10:31:26 -07001144ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001145 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001146 msg->setInt32("type", type);
1147
1148 sp<AMessage> response;
1149 int32_t index;
1150 status_t err = msg->postAndAwaitResponse(&response);
1151 if (err == OK && response != NULL) {
1152 CHECK(response->findInt32("index", &index));
1153 return index;
1154 } else {
1155 return -1;
1156 }
1157}
1158
1159void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1160 int32_t tmpType;
1161 CHECK(msg->findInt32("type", &tmpType));
1162 media_track_type type = (media_track_type)tmpType;
1163
1164 sp<AMessage> response = new AMessage;
1165 ssize_t index = doGetSelectedTrack(type);
1166 response->setInt32("index", index);
1167
Lajos Molnar3f274362015-03-05 14:35:41 -08001168 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001169 CHECK(msg->senderAwaitsResponse(&replyID));
1170 response->postReply(replyID);
1171}
1172
1173ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001174 const Track *track = NULL;
1175 switch (type) {
1176 case MEDIA_TRACK_TYPE_VIDEO:
1177 track = &mVideoTrack;
1178 break;
1179 case MEDIA_TRACK_TYPE_AUDIO:
1180 track = &mAudioTrack;
1181 break;
1182 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1183 track = &mTimedTextTrack;
1184 break;
1185 case MEDIA_TRACK_TYPE_SUBTITLE:
1186 track = &mSubtitleTrack;
1187 break;
1188 default:
1189 break;
1190 }
1191
1192 if (track != NULL && track->mSource != NULL) {
1193 return track->mIndex;
1194 }
1195
1196 return -1;
1197}
1198
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001199status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001200 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001201 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001202 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001203 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001204 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001205
1206 sp<AMessage> response;
1207 status_t err = msg->postAndAwaitResponse(&response);
1208 if (err == OK && response != NULL) {
1209 CHECK(response->findInt32("err", &err));
1210 }
1211
1212 return err;
1213}
1214
1215void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1216 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001217 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001218 CHECK(msg->findInt32("trackIndex", &trackIndex));
1219 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001220 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001221
1222 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001223 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001224 response->setInt32("err", err);
1225
Lajos Molnar3f274362015-03-05 14:35:41 -08001226 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001227 CHECK(msg->senderAwaitsResponse(&replyID));
1228 response->postReply(replyID);
1229}
1230
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001231status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001232 if (trackIndex >= mSources.size()) {
1233 return BAD_INDEX;
1234 }
1235
1236 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001237 Track* track = NULL;
1238 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1239 track = &mSubtitleTrack;
1240 mFetchSubtitleDataGeneration++;
1241 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1242 track = &mTimedTextTrack;
1243 mFetchTimedTextDataGeneration++;
1244 }
1245 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001246 return INVALID_OPERATION;
1247 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001248 track->mSource->stop();
1249 track->mSource = NULL;
1250 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001251 return OK;
1252 }
1253
1254 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1255 sp<MetaData> meta = source->getFormat();
1256 const char *mime;
1257 CHECK(meta->findCString(kKeyMIMEType, &mime));
1258 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001259 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1260 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1261 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001262 return OK;
1263 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001264 track->mIndex = trackIndex;
1265 if (track->mSource != NULL) {
1266 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001267 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001268 track->mSource = mSources.itemAt(trackIndex);
1269 track->mSource->start();
1270 if (track->mPackets == NULL) {
1271 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001272 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001273 track->mPackets->clear();
1274 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001275
1276 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001277
1278 if (isSubtitle) {
1279 mFetchSubtitleDataGeneration++;
1280 } else {
1281 mFetchTimedTextDataGeneration++;
1282 }
1283
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001284 status_t eosResult; // ignored
1285 if (mSubtitleTrack.mSource != NULL
1286 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001287 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001288 msg->setInt64("timeUs", timeUs);
1289 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1290 msg->post();
1291 }
1292
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07001293 sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this);
1294 msg2->setInt32("generation", mFetchTimedTextDataGeneration);
1295 msg2->post();
1296
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001297 if (mTimedTextTrack.mSource != NULL
1298 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001299 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001300 msg->setInt64("timeUs", timeUs);
1301 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1302 msg->post();
1303 }
1304
Robert Shih3423bbd2014-07-16 15:47:09 -07001305 return OK;
1306 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1307 bool audio = !strncasecmp(mime, "audio/", 6);
1308 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1309 if (track->mSource != NULL && track->mIndex == trackIndex) {
1310 return OK;
1311 }
1312
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001313 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001314 msg->setInt32("trackIndex", trackIndex);
1315 msg->post();
1316 return OK;
1317 }
1318
1319 return INVALID_OPERATION;
1320}
1321
Andreas Huberafed0e12011-09-20 15:39:58 -07001322status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001323 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001324 msg->setInt64("seekTimeUs", seekTimeUs);
1325
1326 sp<AMessage> response;
1327 status_t err = msg->postAndAwaitResponse(&response);
1328 if (err == OK && response != NULL) {
1329 CHECK(response->findInt32("err", &err));
1330 }
1331
1332 return err;
1333}
1334
1335void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1336 int64_t seekTimeUs;
1337 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1338
1339 sp<AMessage> response = new AMessage;
1340 status_t err = doSeek(seekTimeUs);
1341 response->setInt32("err", err);
1342
Lajos Molnar3f274362015-03-05 14:35:41 -08001343 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001344 CHECK(msg->senderAwaitsResponse(&replyID));
1345 response->postReply(replyID);
1346}
1347
1348status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001349 // If the Widevine source is stopped, do not attempt to read any
1350 // more buffers.
1351 if (mStopRead) {
1352 return INVALID_OPERATION;
1353 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001354 if (mVideoTrack.mSource != NULL) {
1355 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001356 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001357
1358 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001359 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001360 }
1361
1362 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001363 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001364 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001365 }
1366
Ronghua Wu80276872014-08-28 15:50:29 -07001367 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1368 if (!mStarted) {
1369 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1370 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001371
1372 // If currently buffering, post kWhatBufferingEnd first, so that
1373 // NuPlayer resumes. Otherwise, if cache hits high watermark
1374 // before new polling happens, no one will resume the playback.
1375 stopBufferingIfNecessary();
1376 restartPollBuffering();
1377
Andreas Huberafed0e12011-09-20 15:39:58 -07001378 return OK;
1379}
1380
Robert Shih3423bbd2014-07-16 15:47:09 -07001381sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1382 MediaBuffer* mb,
1383 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001384 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001385 int64_t *actualTimeUs) {
1386 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1387 size_t outLength = mb->range_length();
1388
1389 if (audio && mAudioIsVorbis) {
1390 outLength += sizeof(int32_t);
1391 }
1392
1393 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001394 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001395 // data is already provided in the buffer
1396 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001397 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001398 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001399 } else {
1400 ab = new ABuffer(outLength);
1401 memcpy(ab->data(),
1402 (const uint8_t *)mb->data() + mb->range_offset(),
1403 mb->range_length());
1404 }
1405
1406 if (audio && mAudioIsVorbis) {
1407 int32_t numPageSamples;
1408 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1409 numPageSamples = -1;
1410 }
1411
1412 uint8_t* abEnd = ab->data() + mb->range_length();
1413 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1414 }
1415
Lajos Molnare26940f2014-07-31 10:31:26 -07001416 sp<AMessage> meta = ab->meta();
1417
Robert Shih3423bbd2014-07-16 15:47:09 -07001418 int64_t timeUs;
1419 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001420 meta->setInt64("timeUs", timeUs);
1421
Wei Jia474d7c72014-12-04 15:12:13 -08001422#if 0
1423 // Temporarily disable pre-roll till we have a full solution to handle
1424 // both single seek and continous seek gracefully.
1425 if (seekTimeUs > timeUs) {
1426 sp<AMessage> extra = new AMessage;
1427 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1428 meta->setMessage("extra", extra);
1429 }
1430#endif
1431
Lajos Molnare26940f2014-07-31 10:31:26 -07001432 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1433 const char *mime;
1434 CHECK(mTimedTextTrack.mSource != NULL
1435 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1436 meta->setString("mime", mime);
1437 }
1438
Robert Shih3423bbd2014-07-16 15:47:09 -07001439 int64_t durationUs;
1440 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1441 meta->setInt64("durationUs", durationUs);
1442 }
1443
1444 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1445 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1446 }
1447
Robert Shihf8bd8512015-04-23 16:39:18 -07001448 uint32_t dataType; // unused
1449 const void *seiData;
1450 size_t seiLength;
1451 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1452 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1453 meta->setBuffer("sei", sei);
1454 }
1455
Robert Shih3423bbd2014-07-16 15:47:09 -07001456 if (actualTimeUs) {
1457 *actualTimeUs = timeUs;
1458 }
1459
1460 mb->release();
1461 mb = NULL;
1462
1463 return ab;
1464}
1465
Robert Shih17f6dd62014-08-20 17:00:21 -07001466void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001467 Mutex::Autolock _l(mReadBufferLock);
1468
1469 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1470 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001471 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001472 msg->setInt32("trackType", trackType);
1473 msg->post();
1474 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001475}
1476
1477void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1478 int32_t tmpType;
1479 CHECK(msg->findInt32("trackType", &tmpType));
1480 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001481 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001482 {
1483 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001484 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001485 Mutex::Autolock _l(mReadBufferLock);
1486 mPendingReadBufferTypes &= ~(1 << trackType);
1487 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001488}
1489
Andreas Huberafed0e12011-09-20 15:39:58 -07001490void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001491 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001492 // Do not read data if Widevine source is stopped
1493 if (mStopRead) {
1494 return;
1495 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001496 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001497 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001498 switch (trackType) {
1499 case MEDIA_TRACK_TYPE_VIDEO:
1500 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001501 if (mIsWidevine) {
1502 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001503 } else {
1504 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001505 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001506 break;
1507 case MEDIA_TRACK_TYPE_AUDIO:
1508 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001509 if (mIsWidevine) {
1510 maxBuffers = 8;
1511 } else {
1512 maxBuffers = 64;
1513 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001514 break;
1515 case MEDIA_TRACK_TYPE_SUBTITLE:
1516 track = &mSubtitleTrack;
1517 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001518 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1519 track = &mTimedTextTrack;
1520 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001521 default:
1522 TRESPASS();
1523 }
1524
1525 if (track->mSource == NULL) {
1526 return;
1527 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001528
1529 if (actualTimeUs) {
1530 *actualTimeUs = seekTimeUs;
1531 }
1532
1533 MediaSource::ReadOptions options;
1534
1535 bool seeking = false;
1536
1537 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001538 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001539 seeking = true;
1540 }
1541
Chong Zhang42e81532014-12-01 13:44:26 -08001542 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001543 options.setNonBlocking();
1544 }
1545
Phil Burkc5cc2e22014-09-09 20:08:39 -07001546 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001547 MediaBuffer *mbuf;
1548 status_t err = track->mSource->read(&mbuf, &options);
1549
1550 options.clearSeekTo();
1551
1552 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001553 int64_t timeUs;
1554 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1555 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1556 mAudioTimeUs = timeUs;
1557 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1558 mVideoTimeUs = timeUs;
1559 }
1560
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001561 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001562
Wei Jia474d7c72014-12-04 15:12:13 -08001563 sp<ABuffer> buffer = mediaBufferToABuffer(
Shivaprasad Hongal98e7ece2015-07-23 19:57:14 -07001564 mbuf, trackType, seekTimeUs,
1565 numBuffers == 0 ? actualTimeUs : NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -07001566 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001567 formatChange = false;
1568 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001569 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001570 } else if (err == WOULD_BLOCK) {
1571 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001572 } else if (err == INFO_FORMAT_CHANGED) {
1573#if 0
1574 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001575 ATSParser::DISCONTINUITY_FORMATCHANGE,
1576 NULL,
1577 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001578#endif
1579 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001580 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001581 track->mPackets->signalEOS(err);
1582 break;
1583 }
1584 }
1585}
1586
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001587void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1588 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1589 // formatChange && seeking: track whose source is changed during selection
1590 // formatChange && !seeking: track whose source is not changed during selection
1591 // !formatChange: normal seek
1592 if ((seeking || formatChange)
1593 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1594 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1595 ATSParser::DiscontinuityType type = (formatChange && seeking)
1596 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1597 : ATSParser::DISCONTINUITY_NONE;
1598 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1599 }
1600}
1601
Andreas Huberafed0e12011-09-20 15:39:58 -07001602} // namespace android