blob: e8c28d592626b32b4e4d4149ed9cce824720a132 [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);
Wei Jiae31eeb42015-08-28 10:35:35 -0700214 if (track == NULL) {
215 continue;
216 }
Chong Zhangafc0a872014-08-26 09:56:52 -0700217
Andreas Huberafed0e12011-09-20 15:39:58 -0700218 sp<MetaData> meta = extractor->getTrackMetaData(i);
219
220 const char *mime;
221 CHECK(meta->findCString(kKeyMIMEType, &mime));
222
Chong Zhangafc0a872014-08-26 09:56:52 -0700223 // Do the string compare immediately with "mime",
224 // we can't assume "mime" would stay valid after another
225 // extractor operation, some extractors might modify meta
226 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700227 if (!strncasecmp(mime, "audio/", 6)) {
228 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700229 mAudioTrack.mIndex = i;
230 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700231 mAudioTrack.mPackets =
232 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700233
234 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
235 mAudioIsVorbis = true;
236 } else {
237 mAudioIsVorbis = false;
238 }
239 }
240 } else if (!strncasecmp(mime, "video/", 6)) {
241 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700242 mVideoTrack.mIndex = i;
243 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700244 mVideoTrack.mPackets =
245 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700246
247 // check if the source requires secure buffers
248 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700249 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
250 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800251 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700252 if (mUIDValid) {
253 extractor->setUID(mUID);
254 }
Chong Zhang7e892182014-08-05 11:58:21 -0700255 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700256 }
257 }
258
Wei Jiae31eeb42015-08-28 10:35:35 -0700259 mSources.push(track);
260 int64_t durationUs;
261 if (meta->findInt64(kKeyDuration, &durationUs)) {
262 if (durationUs > mDurationUs) {
263 mDurationUs = durationUs;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700264 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700265 }
Wei Jiae31eeb42015-08-28 10:35:35 -0700266
267 int32_t bitrate;
268 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
269 totalBitrate += bitrate;
270 } else {
271 totalBitrate = -1;
272 }
273 }
274
275 if (mSources.size() == 0) {
276 ALOGE("b/23705695");
277 return UNKNOWN_ERROR;
Andreas Huberafed0e12011-09-20 15:39:58 -0700278 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700279
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700280 mBitrate = totalBitrate;
281
282 return OK;
283}
284
285status_t NuPlayer::GenericSource::startSources() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800286 // Start the selected A/V tracks now before we start buffering.
287 // Widevine sources might re-initialize crypto when starting, if we delay
288 // this to start(), all data buffered during prepare would be wasted.
289 // (We don't actually start reading until start().)
290 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
291 ALOGE("failed to start audio track!");
292 return UNKNOWN_ERROR;
293 }
294
295 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
296 ALOGE("failed to start video track!");
297 return UNKNOWN_ERROR;
298 }
299
Chong Zhang3de157d2014-08-05 20:54:44 -0700300 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700301}
302
Ronghua Wu80276872014-08-28 15:50:29 -0700303void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
304 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
305 if (mDecryptHandle != NULL) {
306 CHECK(mDrmManagerClient);
307 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
308 sp<AMessage> msg = dupNotify();
309 msg->setInt32("what", kWhatDrmNoLicense);
310 msg->post();
311 }
312 }
313}
314
315int64_t NuPlayer::GenericSource::getLastReadPosition() {
316 if (mAudioTrack.mSource != NULL) {
317 return mAudioTimeUs;
318 } else if (mVideoTrack.mSource != NULL) {
319 return mVideoTimeUs;
320 } else {
321 return 0;
322 }
323}
324
Chong Zhanga19f33e2014-08-07 15:35:07 -0700325status_t NuPlayer::GenericSource::setBuffers(
326 bool audio, Vector<MediaBuffer *> &buffers) {
Wei Jia67ca3ca2016-05-25 14:10:26 -0700327 if (mIsSecure && !audio && mVideoTrack.mSource != NULL) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700328 return mVideoTrack.mSource->setBuffers(buffers);
329 }
330 return INVALID_OPERATION;
331}
332
Ronghua Wu02cb98d2015-05-27 11:02:54 -0700333bool NuPlayer::GenericSource::isStreaming() const {
334 return mIsStreaming;
335}
336
Andreas Huberafed0e12011-09-20 15:39:58 -0700337NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700338 if (mLooper != NULL) {
339 mLooper->unregisterHandler(id());
340 mLooper->stop();
341 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800342 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700343}
344
Andreas Huber9575c962013-02-05 13:59:56 -0800345void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700346 if (mLooper == NULL) {
347 mLooper = new ALooper;
348 mLooper->setName("generic");
349 mLooper->start();
350
351 mLooper->registerHandler(this);
352 }
353
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800354 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
Chong Zhang1228d6b2014-08-12 21:25:48 -0700355 msg->post();
356}
357
358void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700359 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700360 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800361 // set to false first, if the extractor
362 // comes back as secure, set it to true then.
363 mIsSecure = false;
364
Chong Zhangd354d8d2014-08-20 13:09:58 -0700365 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700366 const char* uri = mUri.c_str();
Chong Zhangc287cad2015-02-19 18:30:30 -0800367 String8 contentType;
Robert Shih360d6d02014-09-29 14:42:35 -0700368 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
369
370 if (!strncasecmp("http://", uri, 7)
371 || !strncasecmp("https://", uri, 8)
372 || mIsWidevine) {
373 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
374 if (mHttpSource == NULL) {
375 ALOGE("Failed to create http source!");
376 notifyPreparedAndCleanup(UNKNOWN_ERROR);
377 return;
378 }
379 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700380
Chong Zhangd354d8d2014-08-20 13:09:58 -0700381 mDataSource = DataSource::CreateFromURI(
Chong Zhangc287cad2015-02-19 18:30:30 -0800382 mHTTPService, uri, &mUriHeaders, &contentType,
Robert Shih360d6d02014-09-29 14:42:35 -0700383 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700384 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700385 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700386
Chong Zhangd354d8d2014-08-20 13:09:58 -0700387 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800388 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700389 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700390
Chong Zhangd354d8d2014-08-20 13:09:58 -0700391 if (mDataSource == NULL) {
392 ALOGE("Failed to create data source!");
393 notifyPreparedAndCleanup(UNKNOWN_ERROR);
394 return;
395 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700396 }
397
Chris Watkins99f31602015-03-20 13:06:33 -0700398 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
399 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
400 }
401
402 // For widevine or other cached streaming cases, we need to wait for
403 // enough buffering before reporting prepared.
404 // Note that even when URL doesn't start with widevine://, mIsWidevine
405 // could still be set to true later, if the streaming or file source
406 // is sniffed to be widevine. We don't want to buffer for file source
407 // in that case, so must check the flag now.
408 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
409
Chong Zhangc287cad2015-02-19 18:30:30 -0800410 // init extractor from data source
411 status_t err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700412
413 if (err != OK) {
414 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700415 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700416 return;
417 }
418
Andreas Huber9575c962013-02-05 13:59:56 -0800419 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700420 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
421 sp<AMessage> msg = new AMessage;
422 err = convertMetaDataToMessage(meta, &msg);
423 if(err != OK) {
424 notifyPreparedAndCleanup(err);
425 return;
426 }
427 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800428 }
429
430 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800431 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800432 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700433 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800434 | FLAG_CAN_SEEK_BACKWARD
435 | FLAG_CAN_SEEK_FORWARD
436 | FLAG_CAN_SEEK);
437
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700438 if (mIsSecure) {
439 // secure decoders must be instantiated before starting widevine source
440 sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, this);
441 notifyInstantiateSecureDecoders(reply);
442 } else {
443 finishPrepareAsync();
444 }
445}
446
447void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) {
448 if (err != OK) {
449 ALOGE("Failed to instantiate secure decoders!");
450 notifyPreparedAndCleanup(err);
451 return;
452 }
453 finishPrepareAsync();
454}
455
456void NuPlayer::GenericSource::finishPrepareAsync() {
457 status_t err = startSources();
458 if (err != OK) {
459 ALOGE("Failed to init start data source!");
460 notifyPreparedAndCleanup(err);
461 return;
462 }
463
Chong Zhangefbb6192015-01-30 17:13:27 -0800464 if (mIsStreaming) {
465 mPrepareBuffering = true;
466
467 ensureCacheIsFetching();
468 restartPollBuffering();
469 } else {
470 notifyPrepared();
471 }
Andreas Huber9575c962013-02-05 13:59:56 -0800472}
473
Chong Zhangd354d8d2014-08-20 13:09:58 -0700474void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
475 if (err != OK) {
Robert Shihb9466482015-09-02 14:02:47 -0700476 {
477 sp<DataSource> dataSource = mDataSource;
478 sp<NuCachedSource2> cachedSource = mCachedSource;
479 sp<DataSource> httpSource = mHttpSource;
480 {
481 Mutex::Autolock _l(mDisconnectLock);
482 mDataSource.clear();
Wei Jia09c291c2015-10-22 11:35:04 -0700483 mDecryptHandle = NULL;
Wei Jia224858e2015-10-19 16:14:14 -0700484 mDrmManagerClient = NULL;
Robert Shihb9466482015-09-02 14:02:47 -0700485 mCachedSource.clear();
486 mHttpSource.clear();
487 }
488 }
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700489 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700490
491 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700492 }
493 notifyPrepared(err);
494}
495
Andreas Huberafed0e12011-09-20 15:39:58 -0700496void NuPlayer::GenericSource::start() {
497 ALOGI("start");
498
Andy Hung2abde2c2014-09-30 14:40:32 -0700499 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700500 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700501 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700502 }
503
504 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700505 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700506 }
Ronghua Wu80276872014-08-28 15:50:29 -0700507
508 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
509 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800510
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800511 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700512}
513
514void NuPlayer::GenericSource::stop() {
515 // nothing to do, just account for DRM playback status
516 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
517 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800518 if (mIsWidevine || mIsSecure) {
519 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800520 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700521 sp<AMessage> response;
522 (void) msg->postAndAwaitResponse(&response);
523 }
Ronghua Wu80276872014-08-28 15:50:29 -0700524}
525
526void NuPlayer::GenericSource::pause() {
527 // nothing to do, just account for DRM playback status
528 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
529 mStarted = false;
530}
531
532void NuPlayer::GenericSource::resume() {
533 // nothing to do, just account for DRM playback status
534 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
535 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800536
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800537 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700538}
539
Chong Zhang48296b72014-09-14 14:28:45 -0700540void NuPlayer::GenericSource::disconnect() {
Robert Shihb9466482015-09-02 14:02:47 -0700541 sp<DataSource> dataSource, httpSource;
542 {
543 Mutex::Autolock _l(mDisconnectLock);
544 dataSource = mDataSource;
545 httpSource = mHttpSource;
546 }
547
548 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700549 // disconnect data source
Robert Shihb9466482015-09-02 14:02:47 -0700550 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
551 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700552 }
Robert Shihb9466482015-09-02 14:02:47 -0700553 } else if (httpSource != NULL) {
554 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700555 }
556}
557
Ronghua Wu80276872014-08-28 15:50:29 -0700558void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
559 if (mDecryptHandle != NULL) {
560 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
561 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700562 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
563 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700564}
565
566status_t NuPlayer::GenericSource::feedMoreTSData() {
567 return OK;
568}
569
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700570void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800571 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700572 msg->setInt32("generation", mPollBufferingGeneration);
573 msg->post(1000000ll);
574}
575
576void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800577 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700578 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800579 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700580}
581
Chong Zhangefbb6192015-01-30 17:13:27 -0800582void NuPlayer::GenericSource::restartPollBuffering() {
583 if (mIsStreaming) {
584 cancelPollBuffering();
585 onPollBuffering();
586 }
587}
588
Chong Zhangc287cad2015-02-19 18:30:30 -0800589void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
590 // Buffering percent could go backward as it's estimated from remaining
591 // data and last access time. This could cause the buffering position
592 // drawn on media control to jitter slightly. Remember previously reported
593 // percentage and don't allow it to go backward.
594 if (percentage < mPrevBufferPercentage) {
595 percentage = mPrevBufferPercentage;
596 } else if (percentage > 100) {
597 percentage = 100;
598 }
599
600 mPrevBufferPercentage = percentage;
601
Chong Zhangefbb6192015-01-30 17:13:27 -0800602 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
603
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700604 sp<AMessage> msg = dupNotify();
605 msg->setInt32("what", kWhatBufferingUpdate);
606 msg->setInt32("percentage", percentage);
607 msg->post();
608}
609
Chong Zhangefbb6192015-01-30 17:13:27 -0800610void NuPlayer::GenericSource::startBufferingIfNecessary() {
611 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
612 mPrepareBuffering, mBuffering);
613
614 if (mPrepareBuffering) {
615 return;
616 }
617
618 if (!mBuffering) {
619 mBuffering = true;
620
621 ensureCacheIsFetching();
622 sendCacheStats();
623
624 sp<AMessage> notify = dupNotify();
625 notify->setInt32("what", kWhatPauseOnBufferingStart);
626 notify->post();
627 }
628}
629
630void NuPlayer::GenericSource::stopBufferingIfNecessary() {
631 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
632 mPrepareBuffering, mBuffering);
633
634 if (mPrepareBuffering) {
635 mPrepareBuffering = false;
636 notifyPrepared();
637 return;
638 }
639
640 if (mBuffering) {
641 mBuffering = false;
642
643 sendCacheStats();
644
645 sp<AMessage> notify = dupNotify();
646 notify->setInt32("what", kWhatResumeOnBufferingEnd);
647 notify->post();
648 }
649}
650
651void NuPlayer::GenericSource::sendCacheStats() {
652 int32_t kbps = 0;
653 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700654
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800655 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800656 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800657 } else if (mCachedSource != NULL) {
658 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800659 }
660
661 if (err == OK) {
662 sp<AMessage> notify = dupNotify();
663 notify->setInt32("what", kWhatCacheStats);
664 notify->setInt32("bandwidth", kbps);
665 notify->post();
666 }
667}
668
669void NuPlayer::GenericSource::ensureCacheIsFetching() {
670 if (mCachedSource != NULL) {
671 mCachedSource->resumeFetchingIfNecessary();
672 }
673}
674
675void NuPlayer::GenericSource::onPollBuffering() {
676 status_t finalStatus = UNKNOWN_ERROR;
677 int64_t cachedDurationUs = -1ll;
678 ssize_t cachedDataRemaining = -1;
679
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800680 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
681 "WVMExtractor and NuCachedSource both present");
682
683 if (mWVMExtractor != NULL) {
684 cachedDurationUs =
685 mWVMExtractor->getCachedDurationUs(&finalStatus);
686 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800687 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700688 mCachedSource->approxDataRemaining(&finalStatus);
689
690 if (finalStatus == OK) {
691 off64_t size;
692 int64_t bitrate = 0ll;
693 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
694 bitrate = size * 8000000ll / mDurationUs;
695 } else if (mBitrate > 0) {
696 bitrate = mBitrate;
697 }
698 if (bitrate > 0) {
699 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
700 }
701 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700702 }
703
Chong Zhangefbb6192015-01-30 17:13:27 -0800704 if (finalStatus != OK) {
705 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
706
707 if (finalStatus == ERROR_END_OF_STREAM) {
708 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700709 }
710
Chong Zhangefbb6192015-01-30 17:13:27 -0800711 stopBufferingIfNecessary();
712 return;
713 } else if (cachedDurationUs >= 0ll) {
714 if (mDurationUs > 0ll) {
715 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
716 int percentage = 100.0 * cachedPosUs / mDurationUs;
717 if (percentage > 100) {
718 percentage = 100;
719 }
720
721 notifyBufferingUpdate(percentage);
722 }
723
724 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
725 cachedDurationUs / 1000000.0f);
726
727 if (cachedDurationUs < kLowWaterMarkUs) {
728 startBufferingIfNecessary();
729 } else if (cachedDurationUs > kHighWaterMarkUs) {
730 stopBufferingIfNecessary();
731 }
732 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700733 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800734 cachedDataRemaining);
735
736 if (cachedDataRemaining < kLowWaterMarkBytes) {
737 startBufferingIfNecessary();
738 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
739 stopBufferingIfNecessary();
740 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700741 }
742
743 schedulePollBuffering();
744}
745
Robert Shih3423bbd2014-07-16 15:47:09 -0700746void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
747 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700748 case kWhatPrepareAsync:
749 {
750 onPrepareAsync();
751 break;
752 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700753 case kWhatFetchSubtitleData:
754 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700755 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
756 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
757 break;
758 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700759
Lajos Molnare26940f2014-07-31 10:31:26 -0700760 case kWhatFetchTimedTextData:
761 {
762 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
763 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700764 break;
765 }
766
767 case kWhatSendSubtitleData:
768 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700769 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
770 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
771 break;
772 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700773
Lajos Molnare26940f2014-07-31 10:31:26 -0700774 case kWhatSendTimedTextData:
775 {
776 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
777 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700778 break;
779 }
780
781 case kWhatChangeAVSource:
782 {
783 int32_t trackIndex;
784 CHECK(msg->findInt32("trackIndex", &trackIndex));
785 const sp<MediaSource> source = mSources.itemAt(trackIndex);
786
787 Track* track;
788 const char *mime;
789 media_track_type trackType, counterpartType;
790 sp<MetaData> meta = source->getFormat();
791 meta->findCString(kKeyMIMEType, &mime);
792 if (!strncasecmp(mime, "audio/", 6)) {
793 track = &mAudioTrack;
794 trackType = MEDIA_TRACK_TYPE_AUDIO;
795 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
796 } else {
797 CHECK(!strncasecmp(mime, "video/", 6));
798 track = &mVideoTrack;
799 trackType = MEDIA_TRACK_TYPE_VIDEO;
800 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
801 }
802
803
804 if (track->mSource != NULL) {
805 track->mSource->stop();
806 }
807 track->mSource = source;
808 track->mSource->start();
809 track->mIndex = trackIndex;
810
Robert Shih3423bbd2014-07-16 15:47:09 -0700811 int64_t timeUs, actualTimeUs;
812 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800813 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
814 timeUs = mAudioLastDequeueTimeUs;
815 } else {
816 timeUs = mVideoLastDequeueTimeUs;
817 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700818 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
819 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700820 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700821
822 break;
823 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800824
825 case kWhatStart:
826 case kWhatResume:
827 {
828 restartPollBuffering();
829 break;
830 }
831
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700832 case kWhatPollBuffering:
833 {
834 int32_t generation;
835 CHECK(msg->findInt32("generation", &generation));
836 if (generation == mPollBufferingGeneration) {
837 onPollBuffering();
838 }
839 break;
840 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700841
842 case kWhatGetFormat:
843 {
844 onGetFormatMeta(msg);
845 break;
846 }
847
848 case kWhatGetSelectedTrack:
849 {
850 onGetSelectedTrack(msg);
851 break;
852 }
853
854 case kWhatSelectTrack:
855 {
856 onSelectTrack(msg);
857 break;
858 }
859
860 case kWhatSeek:
861 {
862 onSeek(msg);
863 break;
864 }
865
866 case kWhatReadBuffer:
867 {
868 onReadBuffer(msg);
869 break;
870 }
871
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700872 case kWhatSecureDecodersInstantiated:
873 {
874 int32_t err;
875 CHECK(msg->findInt32("err", &err));
876 onSecureDecodersInstantiated(err);
877 break;
878 }
879
Andy Hung2abde2c2014-09-30 14:40:32 -0700880 case kWhatStopWidevine:
881 {
882 // mStopRead is only used for Widevine to prevent the video source
883 // from being read while the associated video decoder is shutting down.
884 mStopRead = true;
885 if (mVideoTrack.mSource != NULL) {
886 mVideoTrack.mPackets->clear();
887 }
888 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800889 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700890 CHECK(msg->senderAwaitsResponse(&replyID));
891 response->postReply(replyID);
892 break;
893 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700894 default:
895 Source::onMessageReceived(msg);
896 break;
897 }
898}
899
Lajos Molnare26940f2014-07-31 10:31:26 -0700900void NuPlayer::GenericSource::fetchTextData(
901 uint32_t sendWhat,
902 media_track_type type,
903 int32_t curGen,
904 sp<AnotherPacketSource> packets,
905 sp<AMessage> msg) {
906 int32_t msgGeneration;
907 CHECK(msg->findInt32("generation", &msgGeneration));
908 if (msgGeneration != curGen) {
909 // stale
910 return;
911 }
912
913 int32_t avail;
914 if (packets->hasBufferAvailable(&avail)) {
915 return;
916 }
917
918 int64_t timeUs;
919 CHECK(msg->findInt64("timeUs", &timeUs));
920
921 int64_t subTimeUs;
922 readBuffer(type, timeUs, &subTimeUs);
923
924 int64_t delayUs = subTimeUs - timeUs;
925 if (msg->what() == kWhatFetchSubtitleData) {
926 const int64_t oneSecUs = 1000000ll;
927 delayUs -= oneSecUs;
928 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800929 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700930 msg2->setInt32("generation", msgGeneration);
931 msg2->post(delayUs < 0 ? 0 : delayUs);
932}
933
934void NuPlayer::GenericSource::sendTextData(
935 uint32_t what,
936 media_track_type type,
937 int32_t curGen,
938 sp<AnotherPacketSource> packets,
939 sp<AMessage> msg) {
940 int32_t msgGeneration;
941 CHECK(msg->findInt32("generation", &msgGeneration));
942 if (msgGeneration != curGen) {
943 // stale
944 return;
945 }
946
947 int64_t subTimeUs;
948 if (packets->nextBufferTime(&subTimeUs) != OK) {
949 return;
950 }
951
952 int64_t nextSubTimeUs;
953 readBuffer(type, -1, &nextSubTimeUs);
954
955 sp<ABuffer> buffer;
956 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
957 if (dequeueStatus == OK) {
958 sp<AMessage> notify = dupNotify();
959 notify->setInt32("what", what);
960 notify->setBuffer("buffer", buffer);
961 notify->post();
962
963 const int64_t delayUs = nextSubTimeUs - subTimeUs;
964 msg->post(delayUs < 0 ? 0 : delayUs);
965 }
966}
967
Andreas Huber84066782011-08-16 09:34:26 -0700968sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800969 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700970 msg->setInt32("audio", audio);
971
972 sp<AMessage> response;
973 void *format;
974 status_t err = msg->postAndAwaitResponse(&response);
975 if (err == OK && response != NULL) {
976 CHECK(response->findPointer("format", &format));
977 return (MetaData *)format;
978 } else {
979 return NULL;
980 }
981}
982
983void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
984 int32_t audio;
985 CHECK(msg->findInt32("audio", &audio));
986
987 sp<AMessage> response = new AMessage;
988 sp<MetaData> format = doGetFormatMeta(audio);
989 response->setPointer("format", format.get());
990
Lajos Molnar3f274362015-03-05 14:35:41 -0800991 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -0700992 CHECK(msg->senderAwaitsResponse(&replyID));
993 response->postReply(replyID);
994}
995
996sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700997 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
998
999 if (source == NULL) {
1000 return NULL;
1001 }
1002
1003 return source->getFormat();
1004}
1005
1006status_t NuPlayer::GenericSource::dequeueAccessUnit(
1007 bool audio, sp<ABuffer> *accessUnit) {
1008 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1009
1010 if (track->mSource == NULL) {
1011 return -EWOULDBLOCK;
1012 }
1013
Lajos Molnarcc227032014-07-17 15:33:06 -07001014 if (mIsWidevine && !audio) {
1015 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001016 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001017 }
1018
Andreas Huberafed0e12011-09-20 15:39:58 -07001019 status_t finalResult;
1020 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001021 if (finalResult == OK) {
1022 postReadBuffer(
1023 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1024 return -EWOULDBLOCK;
1025 }
1026 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001027 }
1028
1029 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1030
Chong Zhangfcf044a2015-07-14 15:58:51 -07001031 // start pulling in more buffers if we only have one (or no) buffer left
1032 // so that decoder has less chance of being starved
1033 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001034 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001035 }
1036
Robert Shih3423bbd2014-07-16 15:47:09 -07001037 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001038 if (mSubtitleTrack.mSource != NULL) {
1039 mSubtitleTrack.mPackets->clear();
1040 mFetchSubtitleDataGeneration++;
1041 }
1042 if (mTimedTextTrack.mSource != NULL) {
1043 mTimedTextTrack.mPackets->clear();
1044 mFetchTimedTextDataGeneration++;
1045 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001046 return result;
1047 }
1048
1049 int64_t timeUs;
1050 status_t eosResult; // ignored
1051 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001052 if (audio) {
1053 mAudioLastDequeueTimeUs = timeUs;
1054 } else {
1055 mVideoLastDequeueTimeUs = timeUs;
1056 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001057
1058 if (mSubtitleTrack.mSource != NULL
1059 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001060 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001061 msg->setInt64("timeUs", timeUs);
1062 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1063 msg->post();
1064 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001065
Lajos Molnare26940f2014-07-31 10:31:26 -07001066 if (mTimedTextTrack.mSource != NULL
1067 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001068 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001069 msg->setInt64("timeUs", timeUs);
1070 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1071 msg->post();
1072 }
1073
Andreas Huberafed0e12011-09-20 15:39:58 -07001074 return result;
1075}
1076
1077status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1078 *durationUs = mDurationUs;
1079 return OK;
1080}
1081
Robert Shihdd235722014-06-12 14:49:23 -07001082size_t NuPlayer::GenericSource::getTrackCount() const {
1083 return mSources.size();
1084}
1085
1086sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1087 size_t trackCount = mSources.size();
1088 if (trackIndex >= trackCount) {
1089 return NULL;
1090 }
1091
1092 sp<AMessage> format = new AMessage();
1093 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1094
1095 const char *mime;
1096 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001097 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001098
1099 int32_t trackType;
1100 if (!strncasecmp(mime, "video/", 6)) {
1101 trackType = MEDIA_TRACK_TYPE_VIDEO;
1102 } else if (!strncasecmp(mime, "audio/", 6)) {
1103 trackType = MEDIA_TRACK_TYPE_AUDIO;
1104 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1105 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1106 } else {
1107 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1108 }
1109 format->setInt32("type", trackType);
1110
1111 const char *lang;
1112 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1113 lang = "und";
1114 }
1115 format->setString("language", lang);
1116
1117 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001118 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1119 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1120 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1121 meta->findInt32(kKeyTrackIsForced, &isForced);
1122
1123 format->setInt32("auto", !!isAutoselect);
1124 format->setInt32("default", !!isDefault);
1125 format->setInt32("forced", !!isForced);
1126 }
1127
1128 return format;
1129}
1130
Lajos Molnare26940f2014-07-31 10:31:26 -07001131ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001132 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001133 msg->setInt32("type", type);
1134
1135 sp<AMessage> response;
1136 int32_t index;
1137 status_t err = msg->postAndAwaitResponse(&response);
1138 if (err == OK && response != NULL) {
1139 CHECK(response->findInt32("index", &index));
1140 return index;
1141 } else {
1142 return -1;
1143 }
1144}
1145
1146void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1147 int32_t tmpType;
1148 CHECK(msg->findInt32("type", &tmpType));
1149 media_track_type type = (media_track_type)tmpType;
1150
1151 sp<AMessage> response = new AMessage;
1152 ssize_t index = doGetSelectedTrack(type);
1153 response->setInt32("index", index);
1154
Lajos Molnar3f274362015-03-05 14:35:41 -08001155 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001156 CHECK(msg->senderAwaitsResponse(&replyID));
1157 response->postReply(replyID);
1158}
1159
1160ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001161 const Track *track = NULL;
1162 switch (type) {
1163 case MEDIA_TRACK_TYPE_VIDEO:
1164 track = &mVideoTrack;
1165 break;
1166 case MEDIA_TRACK_TYPE_AUDIO:
1167 track = &mAudioTrack;
1168 break;
1169 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1170 track = &mTimedTextTrack;
1171 break;
1172 case MEDIA_TRACK_TYPE_SUBTITLE:
1173 track = &mSubtitleTrack;
1174 break;
1175 default:
1176 break;
1177 }
1178
1179 if (track != NULL && track->mSource != NULL) {
1180 return track->mIndex;
1181 }
1182
1183 return -1;
1184}
1185
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001186status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001187 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001188 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001189 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001190 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001191 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001192
1193 sp<AMessage> response;
1194 status_t err = msg->postAndAwaitResponse(&response);
1195 if (err == OK && response != NULL) {
1196 CHECK(response->findInt32("err", &err));
1197 }
1198
1199 return err;
1200}
1201
1202void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1203 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001204 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001205 CHECK(msg->findInt32("trackIndex", &trackIndex));
1206 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001207 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001208
1209 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001210 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001211 response->setInt32("err", err);
1212
Lajos Molnar3f274362015-03-05 14:35:41 -08001213 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001214 CHECK(msg->senderAwaitsResponse(&replyID));
1215 response->postReply(replyID);
1216}
1217
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001218status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001219 if (trackIndex >= mSources.size()) {
1220 return BAD_INDEX;
1221 }
1222
1223 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001224 Track* track = NULL;
1225 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1226 track = &mSubtitleTrack;
1227 mFetchSubtitleDataGeneration++;
1228 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1229 track = &mTimedTextTrack;
1230 mFetchTimedTextDataGeneration++;
1231 }
1232 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001233 return INVALID_OPERATION;
1234 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001235 track->mSource->stop();
1236 track->mSource = NULL;
1237 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001238 return OK;
1239 }
1240
1241 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1242 sp<MetaData> meta = source->getFormat();
1243 const char *mime;
1244 CHECK(meta->findCString(kKeyMIMEType, &mime));
1245 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001246 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1247 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1248 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001249 return OK;
1250 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001251 track->mIndex = trackIndex;
1252 if (track->mSource != NULL) {
1253 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001254 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001255 track->mSource = mSources.itemAt(trackIndex);
1256 track->mSource->start();
1257 if (track->mPackets == NULL) {
1258 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001259 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001260 track->mPackets->clear();
1261 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001262
1263 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001264
1265 if (isSubtitle) {
1266 mFetchSubtitleDataGeneration++;
1267 } else {
1268 mFetchTimedTextDataGeneration++;
1269 }
1270
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001271 status_t eosResult; // ignored
1272 if (mSubtitleTrack.mSource != NULL
1273 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001274 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001275 msg->setInt64("timeUs", timeUs);
1276 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1277 msg->post();
1278 }
1279
1280 if (mTimedTextTrack.mSource != NULL
1281 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001282 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001283 msg->setInt64("timeUs", timeUs);
1284 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1285 msg->post();
1286 }
1287
Robert Shih3423bbd2014-07-16 15:47:09 -07001288 return OK;
1289 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1290 bool audio = !strncasecmp(mime, "audio/", 6);
1291 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1292 if (track->mSource != NULL && track->mIndex == trackIndex) {
1293 return OK;
1294 }
1295
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001296 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001297 msg->setInt32("trackIndex", trackIndex);
1298 msg->post();
1299 return OK;
1300 }
1301
1302 return INVALID_OPERATION;
1303}
1304
Andreas Huberafed0e12011-09-20 15:39:58 -07001305status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001306 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001307 msg->setInt64("seekTimeUs", seekTimeUs);
1308
1309 sp<AMessage> response;
1310 status_t err = msg->postAndAwaitResponse(&response);
1311 if (err == OK && response != NULL) {
1312 CHECK(response->findInt32("err", &err));
1313 }
1314
1315 return err;
1316}
1317
1318void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1319 int64_t seekTimeUs;
1320 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1321
1322 sp<AMessage> response = new AMessage;
1323 status_t err = doSeek(seekTimeUs);
1324 response->setInt32("err", err);
1325
Lajos Molnar3f274362015-03-05 14:35:41 -08001326 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001327 CHECK(msg->senderAwaitsResponse(&replyID));
1328 response->postReply(replyID);
1329}
1330
1331status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001332 // If the Widevine source is stopped, do not attempt to read any
1333 // more buffers.
1334 if (mStopRead) {
1335 return INVALID_OPERATION;
1336 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001337 if (mVideoTrack.mSource != NULL) {
1338 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001339 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001340
1341 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001342 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001343 }
1344
1345 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001346 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001347 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001348 }
1349
Ronghua Wu80276872014-08-28 15:50:29 -07001350 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1351 if (!mStarted) {
1352 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1353 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001354
1355 // If currently buffering, post kWhatBufferingEnd first, so that
1356 // NuPlayer resumes. Otherwise, if cache hits high watermark
1357 // before new polling happens, no one will resume the playback.
1358 stopBufferingIfNecessary();
1359 restartPollBuffering();
1360
Andreas Huberafed0e12011-09-20 15:39:58 -07001361 return OK;
1362}
1363
Robert Shih3423bbd2014-07-16 15:47:09 -07001364sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1365 MediaBuffer* mb,
1366 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001367 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001368 int64_t *actualTimeUs) {
1369 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1370 size_t outLength = mb->range_length();
1371
1372 if (audio && mAudioIsVorbis) {
1373 outLength += sizeof(int32_t);
1374 }
1375
1376 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001377 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001378 // data is already provided in the buffer
1379 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001380 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001381 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001382 } else {
1383 ab = new ABuffer(outLength);
1384 memcpy(ab->data(),
1385 (const uint8_t *)mb->data() + mb->range_offset(),
1386 mb->range_length());
1387 }
1388
1389 if (audio && mAudioIsVorbis) {
1390 int32_t numPageSamples;
1391 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1392 numPageSamples = -1;
1393 }
1394
1395 uint8_t* abEnd = ab->data() + mb->range_length();
1396 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1397 }
1398
Lajos Molnare26940f2014-07-31 10:31:26 -07001399 sp<AMessage> meta = ab->meta();
1400
Robert Shih3423bbd2014-07-16 15:47:09 -07001401 int64_t timeUs;
1402 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001403 meta->setInt64("timeUs", timeUs);
1404
Wei Jia474d7c72014-12-04 15:12:13 -08001405#if 0
1406 // Temporarily disable pre-roll till we have a full solution to handle
1407 // both single seek and continous seek gracefully.
1408 if (seekTimeUs > timeUs) {
1409 sp<AMessage> extra = new AMessage;
1410 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1411 meta->setMessage("extra", extra);
1412 }
1413#endif
1414
Lajos Molnare26940f2014-07-31 10:31:26 -07001415 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1416 const char *mime;
1417 CHECK(mTimedTextTrack.mSource != NULL
1418 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1419 meta->setString("mime", mime);
1420 }
1421
Robert Shih3423bbd2014-07-16 15:47:09 -07001422 int64_t durationUs;
1423 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1424 meta->setInt64("durationUs", durationUs);
1425 }
1426
1427 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1428 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1429 }
1430
Robert Shihf8bd8512015-04-23 16:39:18 -07001431 uint32_t dataType; // unused
1432 const void *seiData;
1433 size_t seiLength;
1434 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1435 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1436 meta->setBuffer("sei", sei);
1437 }
1438
Robert Shih3423bbd2014-07-16 15:47:09 -07001439 if (actualTimeUs) {
1440 *actualTimeUs = timeUs;
1441 }
1442
1443 mb->release();
1444 mb = NULL;
1445
1446 return ab;
1447}
1448
Robert Shih17f6dd62014-08-20 17:00:21 -07001449void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001450 Mutex::Autolock _l(mReadBufferLock);
1451
1452 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1453 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001454 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001455 msg->setInt32("trackType", trackType);
1456 msg->post();
1457 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001458}
1459
1460void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1461 int32_t tmpType;
1462 CHECK(msg->findInt32("trackType", &tmpType));
1463 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001464 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001465 {
1466 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001467 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001468 Mutex::Autolock _l(mReadBufferLock);
1469 mPendingReadBufferTypes &= ~(1 << trackType);
1470 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001471}
1472
Andreas Huberafed0e12011-09-20 15:39:58 -07001473void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001474 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001475 // Do not read data if Widevine source is stopped
1476 if (mStopRead) {
1477 return;
1478 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001479 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001480 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001481 switch (trackType) {
1482 case MEDIA_TRACK_TYPE_VIDEO:
1483 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001484 if (mIsWidevine) {
1485 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001486 } else {
1487 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001488 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001489 break;
1490 case MEDIA_TRACK_TYPE_AUDIO:
1491 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001492 if (mIsWidevine) {
1493 maxBuffers = 8;
1494 } else {
1495 maxBuffers = 64;
1496 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001497 break;
1498 case MEDIA_TRACK_TYPE_SUBTITLE:
1499 track = &mSubtitleTrack;
1500 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001501 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1502 track = &mTimedTextTrack;
1503 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001504 default:
1505 TRESPASS();
1506 }
1507
1508 if (track->mSource == NULL) {
1509 return;
1510 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001511
1512 if (actualTimeUs) {
1513 *actualTimeUs = seekTimeUs;
1514 }
1515
1516 MediaSource::ReadOptions options;
1517
1518 bool seeking = false;
1519
1520 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001521 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001522 seeking = true;
1523 }
1524
Chong Zhang42e81532014-12-01 13:44:26 -08001525 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001526 options.setNonBlocking();
1527 }
1528
Phil Burkc5cc2e22014-09-09 20:08:39 -07001529 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001530 MediaBuffer *mbuf;
1531 status_t err = track->mSource->read(&mbuf, &options);
1532
1533 options.clearSeekTo();
1534
1535 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001536 int64_t timeUs;
1537 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1538 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1539 mAudioTimeUs = timeUs;
1540 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1541 mVideoTimeUs = timeUs;
1542 }
1543
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001544 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001545
Wei Jia474d7c72014-12-04 15:12:13 -08001546 sp<ABuffer> buffer = mediaBufferToABuffer(
1547 mbuf, trackType, seekTimeUs, actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001548 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001549 formatChange = false;
1550 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001551 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001552 } else if (err == WOULD_BLOCK) {
1553 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001554 } else if (err == INFO_FORMAT_CHANGED) {
1555#if 0
1556 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001557 ATSParser::DISCONTINUITY_FORMATCHANGE,
1558 NULL,
1559 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001560#endif
1561 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001562 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001563 track->mPackets->signalEOS(err);
1564 break;
1565 }
1566 }
1567}
1568
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001569void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1570 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1571 // formatChange && seeking: track whose source is changed during selection
1572 // formatChange && !seeking: track whose source is not changed during selection
1573 // !formatChange: normal seek
1574 if ((seeking || formatChange)
1575 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1576 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1577 ATSParser::DiscontinuityType type = (formatChange && seeking)
1578 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1579 : ATSParser::DISCONTINUITY_NONE;
1580 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1581 }
1582}
1583
Andreas Huberafed0e12011-09-20 15:39:58 -07001584} // namespace android