blob: 7a4dc47381760b10d0d618004c5c8f932f7ad0e2 [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 Jia0386c912015-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 Jia0386c912015-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 Jia0386c912015-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 Jia0386c912015-08-28 10:35:35 -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) {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700476 mDataSource.clear();
477 mCachedSource.clear();
Robert Shih360d6d02014-09-29 14:42:35 -0700478 mHttpSource.clear();
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700479 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700480
481 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700482 }
483 notifyPrepared(err);
484}
485
Andreas Huberafed0e12011-09-20 15:39:58 -0700486void NuPlayer::GenericSource::start() {
487 ALOGI("start");
488
Andy Hung2abde2c2014-09-30 14:40:32 -0700489 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700490 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700491 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700492 }
493
494 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700495 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700496 }
Ronghua Wu80276872014-08-28 15:50:29 -0700497
498 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
499 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800500
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800501 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700502}
503
504void NuPlayer::GenericSource::stop() {
505 // nothing to do, just account for DRM playback status
506 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
507 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800508 if (mIsWidevine || mIsSecure) {
509 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800510 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700511 sp<AMessage> response;
512 (void) msg->postAndAwaitResponse(&response);
513 }
Ronghua Wu80276872014-08-28 15:50:29 -0700514}
515
516void NuPlayer::GenericSource::pause() {
517 // nothing to do, just account for DRM playback status
518 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
519 mStarted = false;
520}
521
522void NuPlayer::GenericSource::resume() {
523 // nothing to do, just account for DRM playback status
524 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
525 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800526
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800527 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700528}
529
Chong Zhang48296b72014-09-14 14:28:45 -0700530void NuPlayer::GenericSource::disconnect() {
531 if (mDataSource != NULL) {
532 // disconnect data source
533 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
534 static_cast<NuCachedSource2 *>(mDataSource.get())->disconnect();
535 }
Robert Shih360d6d02014-09-29 14:42:35 -0700536 } else if (mHttpSource != NULL) {
537 static_cast<HTTPBase *>(mHttpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700538 }
539}
540
Ronghua Wu80276872014-08-28 15:50:29 -0700541void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
542 if (mDecryptHandle != NULL) {
543 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
544 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700545 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
546 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700547}
548
549status_t NuPlayer::GenericSource::feedMoreTSData() {
550 return OK;
551}
552
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700553void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800554 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700555 msg->setInt32("generation", mPollBufferingGeneration);
556 msg->post(1000000ll);
557}
558
559void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800560 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700561 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800562 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700563}
564
Chong Zhangefbb6192015-01-30 17:13:27 -0800565void NuPlayer::GenericSource::restartPollBuffering() {
566 if (mIsStreaming) {
567 cancelPollBuffering();
568 onPollBuffering();
569 }
570}
571
Chong Zhangc287cad2015-02-19 18:30:30 -0800572void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
573 // Buffering percent could go backward as it's estimated from remaining
574 // data and last access time. This could cause the buffering position
575 // drawn on media control to jitter slightly. Remember previously reported
576 // percentage and don't allow it to go backward.
577 if (percentage < mPrevBufferPercentage) {
578 percentage = mPrevBufferPercentage;
579 } else if (percentage > 100) {
580 percentage = 100;
581 }
582
583 mPrevBufferPercentage = percentage;
584
Chong Zhangefbb6192015-01-30 17:13:27 -0800585 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
586
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700587 sp<AMessage> msg = dupNotify();
588 msg->setInt32("what", kWhatBufferingUpdate);
589 msg->setInt32("percentage", percentage);
590 msg->post();
591}
592
Chong Zhangefbb6192015-01-30 17:13:27 -0800593void NuPlayer::GenericSource::startBufferingIfNecessary() {
594 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
595 mPrepareBuffering, mBuffering);
596
597 if (mPrepareBuffering) {
598 return;
599 }
600
601 if (!mBuffering) {
602 mBuffering = true;
603
604 ensureCacheIsFetching();
605 sendCacheStats();
606
607 sp<AMessage> notify = dupNotify();
608 notify->setInt32("what", kWhatPauseOnBufferingStart);
609 notify->post();
610 }
611}
612
613void NuPlayer::GenericSource::stopBufferingIfNecessary() {
614 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
615 mPrepareBuffering, mBuffering);
616
617 if (mPrepareBuffering) {
618 mPrepareBuffering = false;
619 notifyPrepared();
620 return;
621 }
622
623 if (mBuffering) {
624 mBuffering = false;
625
626 sendCacheStats();
627
628 sp<AMessage> notify = dupNotify();
629 notify->setInt32("what", kWhatResumeOnBufferingEnd);
630 notify->post();
631 }
632}
633
634void NuPlayer::GenericSource::sendCacheStats() {
635 int32_t kbps = 0;
636 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700637
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800638 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800639 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800640 } else if (mCachedSource != NULL) {
641 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800642 }
643
644 if (err == OK) {
645 sp<AMessage> notify = dupNotify();
646 notify->setInt32("what", kWhatCacheStats);
647 notify->setInt32("bandwidth", kbps);
648 notify->post();
649 }
650}
651
652void NuPlayer::GenericSource::ensureCacheIsFetching() {
653 if (mCachedSource != NULL) {
654 mCachedSource->resumeFetchingIfNecessary();
655 }
656}
657
658void NuPlayer::GenericSource::onPollBuffering() {
659 status_t finalStatus = UNKNOWN_ERROR;
660 int64_t cachedDurationUs = -1ll;
661 ssize_t cachedDataRemaining = -1;
662
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800663 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
664 "WVMExtractor and NuCachedSource both present");
665
666 if (mWVMExtractor != NULL) {
667 cachedDurationUs =
668 mWVMExtractor->getCachedDurationUs(&finalStatus);
669 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800670 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700671 mCachedSource->approxDataRemaining(&finalStatus);
672
673 if (finalStatus == OK) {
674 off64_t size;
675 int64_t bitrate = 0ll;
676 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
677 bitrate = size * 8000000ll / mDurationUs;
678 } else if (mBitrate > 0) {
679 bitrate = mBitrate;
680 }
681 if (bitrate > 0) {
682 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
683 }
684 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700685 }
686
Chong Zhangefbb6192015-01-30 17:13:27 -0800687 if (finalStatus != OK) {
688 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
689
690 if (finalStatus == ERROR_END_OF_STREAM) {
691 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700692 }
693
Chong Zhangefbb6192015-01-30 17:13:27 -0800694 stopBufferingIfNecessary();
695 return;
696 } else if (cachedDurationUs >= 0ll) {
697 if (mDurationUs > 0ll) {
698 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
699 int percentage = 100.0 * cachedPosUs / mDurationUs;
700 if (percentage > 100) {
701 percentage = 100;
702 }
703
704 notifyBufferingUpdate(percentage);
705 }
706
707 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
708 cachedDurationUs / 1000000.0f);
709
710 if (cachedDurationUs < kLowWaterMarkUs) {
711 startBufferingIfNecessary();
712 } else if (cachedDurationUs > kHighWaterMarkUs) {
713 stopBufferingIfNecessary();
714 }
715 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700716 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800717 cachedDataRemaining);
718
719 if (cachedDataRemaining < kLowWaterMarkBytes) {
720 startBufferingIfNecessary();
721 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
722 stopBufferingIfNecessary();
723 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700724 }
725
726 schedulePollBuffering();
727}
728
Robert Shih3423bbd2014-07-16 15:47:09 -0700729void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
730 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700731 case kWhatPrepareAsync:
732 {
733 onPrepareAsync();
734 break;
735 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700736 case kWhatFetchSubtitleData:
737 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700738 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
739 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
740 break;
741 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700742
Lajos Molnare26940f2014-07-31 10:31:26 -0700743 case kWhatFetchTimedTextData:
744 {
745 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
746 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700747 break;
748 }
749
750 case kWhatSendSubtitleData:
751 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700752 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
753 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
754 break;
755 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700756
Lajos Molnare26940f2014-07-31 10:31:26 -0700757 case kWhatSendTimedTextData:
758 {
759 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
760 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700761 break;
762 }
763
764 case kWhatChangeAVSource:
765 {
766 int32_t trackIndex;
767 CHECK(msg->findInt32("trackIndex", &trackIndex));
768 const sp<MediaSource> source = mSources.itemAt(trackIndex);
769
770 Track* track;
771 const char *mime;
772 media_track_type trackType, counterpartType;
773 sp<MetaData> meta = source->getFormat();
774 meta->findCString(kKeyMIMEType, &mime);
775 if (!strncasecmp(mime, "audio/", 6)) {
776 track = &mAudioTrack;
777 trackType = MEDIA_TRACK_TYPE_AUDIO;
778 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
779 } else {
780 CHECK(!strncasecmp(mime, "video/", 6));
781 track = &mVideoTrack;
782 trackType = MEDIA_TRACK_TYPE_VIDEO;
783 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
784 }
785
786
787 if (track->mSource != NULL) {
788 track->mSource->stop();
789 }
790 track->mSource = source;
791 track->mSource->start();
792 track->mIndex = trackIndex;
793
Robert Shih3423bbd2014-07-16 15:47:09 -0700794 int64_t timeUs, actualTimeUs;
795 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800796 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
797 timeUs = mAudioLastDequeueTimeUs;
798 } else {
799 timeUs = mVideoLastDequeueTimeUs;
800 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700801 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
802 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700803 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700804
805 break;
806 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800807
808 case kWhatStart:
809 case kWhatResume:
810 {
811 restartPollBuffering();
812 break;
813 }
814
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700815 case kWhatPollBuffering:
816 {
817 int32_t generation;
818 CHECK(msg->findInt32("generation", &generation));
819 if (generation == mPollBufferingGeneration) {
820 onPollBuffering();
821 }
822 break;
823 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700824
825 case kWhatGetFormat:
826 {
827 onGetFormatMeta(msg);
828 break;
829 }
830
831 case kWhatGetSelectedTrack:
832 {
833 onGetSelectedTrack(msg);
834 break;
835 }
836
837 case kWhatSelectTrack:
838 {
839 onSelectTrack(msg);
840 break;
841 }
842
843 case kWhatSeek:
844 {
845 onSeek(msg);
846 break;
847 }
848
849 case kWhatReadBuffer:
850 {
851 onReadBuffer(msg);
852 break;
853 }
854
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700855 case kWhatSecureDecodersInstantiated:
856 {
857 int32_t err;
858 CHECK(msg->findInt32("err", &err));
859 onSecureDecodersInstantiated(err);
860 break;
861 }
862
Andy Hung2abde2c2014-09-30 14:40:32 -0700863 case kWhatStopWidevine:
864 {
865 // mStopRead is only used for Widevine to prevent the video source
866 // from being read while the associated video decoder is shutting down.
867 mStopRead = true;
868 if (mVideoTrack.mSource != NULL) {
869 mVideoTrack.mPackets->clear();
870 }
871 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800872 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700873 CHECK(msg->senderAwaitsResponse(&replyID));
874 response->postReply(replyID);
875 break;
876 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700877 default:
878 Source::onMessageReceived(msg);
879 break;
880 }
881}
882
Lajos Molnare26940f2014-07-31 10:31:26 -0700883void NuPlayer::GenericSource::fetchTextData(
884 uint32_t sendWhat,
885 media_track_type type,
886 int32_t curGen,
887 sp<AnotherPacketSource> packets,
888 sp<AMessage> msg) {
889 int32_t msgGeneration;
890 CHECK(msg->findInt32("generation", &msgGeneration));
891 if (msgGeneration != curGen) {
892 // stale
893 return;
894 }
895
896 int32_t avail;
897 if (packets->hasBufferAvailable(&avail)) {
898 return;
899 }
900
901 int64_t timeUs;
902 CHECK(msg->findInt64("timeUs", &timeUs));
903
904 int64_t subTimeUs;
905 readBuffer(type, timeUs, &subTimeUs);
906
907 int64_t delayUs = subTimeUs - timeUs;
908 if (msg->what() == kWhatFetchSubtitleData) {
909 const int64_t oneSecUs = 1000000ll;
910 delayUs -= oneSecUs;
911 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800912 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700913 msg2->setInt32("generation", msgGeneration);
914 msg2->post(delayUs < 0 ? 0 : delayUs);
915}
916
917void NuPlayer::GenericSource::sendTextData(
918 uint32_t what,
919 media_track_type type,
920 int32_t curGen,
921 sp<AnotherPacketSource> packets,
922 sp<AMessage> msg) {
923 int32_t msgGeneration;
924 CHECK(msg->findInt32("generation", &msgGeneration));
925 if (msgGeneration != curGen) {
926 // stale
927 return;
928 }
929
930 int64_t subTimeUs;
931 if (packets->nextBufferTime(&subTimeUs) != OK) {
932 return;
933 }
934
935 int64_t nextSubTimeUs;
936 readBuffer(type, -1, &nextSubTimeUs);
937
938 sp<ABuffer> buffer;
939 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
940 if (dequeueStatus == OK) {
941 sp<AMessage> notify = dupNotify();
942 notify->setInt32("what", what);
943 notify->setBuffer("buffer", buffer);
944 notify->post();
945
946 const int64_t delayUs = nextSubTimeUs - subTimeUs;
947 msg->post(delayUs < 0 ? 0 : delayUs);
948 }
949}
950
Andreas Huber84066782011-08-16 09:34:26 -0700951sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800952 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -0700953 msg->setInt32("audio", audio);
954
955 sp<AMessage> response;
956 void *format;
957 status_t err = msg->postAndAwaitResponse(&response);
958 if (err == OK && response != NULL) {
959 CHECK(response->findPointer("format", &format));
960 return (MetaData *)format;
961 } else {
962 return NULL;
963 }
964}
965
966void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
967 int32_t audio;
968 CHECK(msg->findInt32("audio", &audio));
969
970 sp<AMessage> response = new AMessage;
971 sp<MetaData> format = doGetFormatMeta(audio);
972 response->setPointer("format", format.get());
973
Lajos Molnar3f274362015-03-05 14:35:41 -0800974 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -0700975 CHECK(msg->senderAwaitsResponse(&replyID));
976 response->postReply(replyID);
977}
978
979sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -0700980 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
981
982 if (source == NULL) {
983 return NULL;
984 }
985
986 return source->getFormat();
987}
988
989status_t NuPlayer::GenericSource::dequeueAccessUnit(
990 bool audio, sp<ABuffer> *accessUnit) {
991 Track *track = audio ? &mAudioTrack : &mVideoTrack;
992
993 if (track->mSource == NULL) {
994 return -EWOULDBLOCK;
995 }
996
Lajos Molnarcc227032014-07-17 15:33:06 -0700997 if (mIsWidevine && !audio) {
998 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -0700999 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001000 }
1001
Andreas Huberafed0e12011-09-20 15:39:58 -07001002 status_t finalResult;
1003 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001004 if (finalResult == OK) {
1005 postReadBuffer(
1006 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1007 return -EWOULDBLOCK;
1008 }
1009 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001010 }
1011
1012 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1013
Chong Zhangfcf044a2015-07-14 15:58:51 -07001014 // start pulling in more buffers if we only have one (or no) buffer left
1015 // so that decoder has less chance of being starved
1016 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001017 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001018 }
1019
Robert Shih3423bbd2014-07-16 15:47:09 -07001020 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001021 if (mSubtitleTrack.mSource != NULL) {
1022 mSubtitleTrack.mPackets->clear();
1023 mFetchSubtitleDataGeneration++;
1024 }
1025 if (mTimedTextTrack.mSource != NULL) {
1026 mTimedTextTrack.mPackets->clear();
1027 mFetchTimedTextDataGeneration++;
1028 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001029 return result;
1030 }
1031
1032 int64_t timeUs;
1033 status_t eosResult; // ignored
1034 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001035 if (audio) {
1036 mAudioLastDequeueTimeUs = timeUs;
1037 } else {
1038 mVideoLastDequeueTimeUs = timeUs;
1039 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001040
1041 if (mSubtitleTrack.mSource != NULL
1042 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001043 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001044 msg->setInt64("timeUs", timeUs);
1045 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1046 msg->post();
1047 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001048
Lajos Molnare26940f2014-07-31 10:31:26 -07001049 if (mTimedTextTrack.mSource != NULL
1050 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001051 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001052 msg->setInt64("timeUs", timeUs);
1053 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1054 msg->post();
1055 }
1056
Andreas Huberafed0e12011-09-20 15:39:58 -07001057 return result;
1058}
1059
1060status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1061 *durationUs = mDurationUs;
1062 return OK;
1063}
1064
Robert Shihdd235722014-06-12 14:49:23 -07001065size_t NuPlayer::GenericSource::getTrackCount() const {
1066 return mSources.size();
1067}
1068
1069sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1070 size_t trackCount = mSources.size();
1071 if (trackIndex >= trackCount) {
1072 return NULL;
1073 }
1074
1075 sp<AMessage> format = new AMessage();
1076 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1077
1078 const char *mime;
1079 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001080 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001081
1082 int32_t trackType;
1083 if (!strncasecmp(mime, "video/", 6)) {
1084 trackType = MEDIA_TRACK_TYPE_VIDEO;
1085 } else if (!strncasecmp(mime, "audio/", 6)) {
1086 trackType = MEDIA_TRACK_TYPE_AUDIO;
1087 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1088 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1089 } else {
1090 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1091 }
1092 format->setInt32("type", trackType);
1093
1094 const char *lang;
1095 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1096 lang = "und";
1097 }
1098 format->setString("language", lang);
1099
1100 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001101 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1102 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1103 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1104 meta->findInt32(kKeyTrackIsForced, &isForced);
1105
1106 format->setInt32("auto", !!isAutoselect);
1107 format->setInt32("default", !!isDefault);
1108 format->setInt32("forced", !!isForced);
1109 }
1110
1111 return format;
1112}
1113
Lajos Molnare26940f2014-07-31 10:31:26 -07001114ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001115 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001116 msg->setInt32("type", type);
1117
1118 sp<AMessage> response;
1119 int32_t index;
1120 status_t err = msg->postAndAwaitResponse(&response);
1121 if (err == OK && response != NULL) {
1122 CHECK(response->findInt32("index", &index));
1123 return index;
1124 } else {
1125 return -1;
1126 }
1127}
1128
1129void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1130 int32_t tmpType;
1131 CHECK(msg->findInt32("type", &tmpType));
1132 media_track_type type = (media_track_type)tmpType;
1133
1134 sp<AMessage> response = new AMessage;
1135 ssize_t index = doGetSelectedTrack(type);
1136 response->setInt32("index", index);
1137
Lajos Molnar3f274362015-03-05 14:35:41 -08001138 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001139 CHECK(msg->senderAwaitsResponse(&replyID));
1140 response->postReply(replyID);
1141}
1142
1143ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001144 const Track *track = NULL;
1145 switch (type) {
1146 case MEDIA_TRACK_TYPE_VIDEO:
1147 track = &mVideoTrack;
1148 break;
1149 case MEDIA_TRACK_TYPE_AUDIO:
1150 track = &mAudioTrack;
1151 break;
1152 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1153 track = &mTimedTextTrack;
1154 break;
1155 case MEDIA_TRACK_TYPE_SUBTITLE:
1156 track = &mSubtitleTrack;
1157 break;
1158 default:
1159 break;
1160 }
1161
1162 if (track != NULL && track->mSource != NULL) {
1163 return track->mIndex;
1164 }
1165
1166 return -1;
1167}
1168
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001169status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001170 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001171 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001172 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001173 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001174 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001175
1176 sp<AMessage> response;
1177 status_t err = msg->postAndAwaitResponse(&response);
1178 if (err == OK && response != NULL) {
1179 CHECK(response->findInt32("err", &err));
1180 }
1181
1182 return err;
1183}
1184
1185void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1186 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001187 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001188 CHECK(msg->findInt32("trackIndex", &trackIndex));
1189 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001190 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001191
1192 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001193 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001194 response->setInt32("err", err);
1195
Lajos Molnar3f274362015-03-05 14:35:41 -08001196 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001197 CHECK(msg->senderAwaitsResponse(&replyID));
1198 response->postReply(replyID);
1199}
1200
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001201status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001202 if (trackIndex >= mSources.size()) {
1203 return BAD_INDEX;
1204 }
1205
1206 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001207 Track* track = NULL;
1208 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1209 track = &mSubtitleTrack;
1210 mFetchSubtitleDataGeneration++;
1211 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1212 track = &mTimedTextTrack;
1213 mFetchTimedTextDataGeneration++;
1214 }
1215 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001216 return INVALID_OPERATION;
1217 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001218 track->mSource->stop();
1219 track->mSource = NULL;
1220 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001221 return OK;
1222 }
1223
1224 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1225 sp<MetaData> meta = source->getFormat();
1226 const char *mime;
1227 CHECK(meta->findCString(kKeyMIMEType, &mime));
1228 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001229 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1230 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1231 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001232 return OK;
1233 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001234 track->mIndex = trackIndex;
1235 if (track->mSource != NULL) {
1236 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001237 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001238 track->mSource = mSources.itemAt(trackIndex);
1239 track->mSource->start();
1240 if (track->mPackets == NULL) {
1241 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001242 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001243 track->mPackets->clear();
1244 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001245
1246 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001247
1248 if (isSubtitle) {
1249 mFetchSubtitleDataGeneration++;
1250 } else {
1251 mFetchTimedTextDataGeneration++;
1252 }
1253
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001254 status_t eosResult; // ignored
1255 if (mSubtitleTrack.mSource != NULL
1256 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001257 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001258 msg->setInt64("timeUs", timeUs);
1259 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1260 msg->post();
1261 }
1262
1263 if (mTimedTextTrack.mSource != NULL
1264 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001265 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001266 msg->setInt64("timeUs", timeUs);
1267 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1268 msg->post();
1269 }
1270
Robert Shih3423bbd2014-07-16 15:47:09 -07001271 return OK;
1272 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1273 bool audio = !strncasecmp(mime, "audio/", 6);
1274 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1275 if (track->mSource != NULL && track->mIndex == trackIndex) {
1276 return OK;
1277 }
1278
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001279 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 msg->setInt32("trackIndex", trackIndex);
1281 msg->post();
1282 return OK;
1283 }
1284
1285 return INVALID_OPERATION;
1286}
1287
Andreas Huberafed0e12011-09-20 15:39:58 -07001288status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001289 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001290 msg->setInt64("seekTimeUs", seekTimeUs);
1291
1292 sp<AMessage> response;
1293 status_t err = msg->postAndAwaitResponse(&response);
1294 if (err == OK && response != NULL) {
1295 CHECK(response->findInt32("err", &err));
1296 }
1297
1298 return err;
1299}
1300
1301void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1302 int64_t seekTimeUs;
1303 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1304
1305 sp<AMessage> response = new AMessage;
1306 status_t err = doSeek(seekTimeUs);
1307 response->setInt32("err", err);
1308
Lajos Molnar3f274362015-03-05 14:35:41 -08001309 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001310 CHECK(msg->senderAwaitsResponse(&replyID));
1311 response->postReply(replyID);
1312}
1313
1314status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001315 // If the Widevine source is stopped, do not attempt to read any
1316 // more buffers.
1317 if (mStopRead) {
1318 return INVALID_OPERATION;
1319 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001320 if (mVideoTrack.mSource != NULL) {
1321 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001322 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001323
1324 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001325 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001326 }
1327
1328 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001329 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001330 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001331 }
1332
Ronghua Wu80276872014-08-28 15:50:29 -07001333 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1334 if (!mStarted) {
1335 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1336 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001337
1338 // If currently buffering, post kWhatBufferingEnd first, so that
1339 // NuPlayer resumes. Otherwise, if cache hits high watermark
1340 // before new polling happens, no one will resume the playback.
1341 stopBufferingIfNecessary();
1342 restartPollBuffering();
1343
Andreas Huberafed0e12011-09-20 15:39:58 -07001344 return OK;
1345}
1346
Robert Shih3423bbd2014-07-16 15:47:09 -07001347sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1348 MediaBuffer* mb,
1349 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001350 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001351 int64_t *actualTimeUs) {
1352 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1353 size_t outLength = mb->range_length();
1354
1355 if (audio && mAudioIsVorbis) {
1356 outLength += sizeof(int32_t);
1357 }
1358
1359 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001360 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001361 // data is already provided in the buffer
1362 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001363 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001364 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001365 } else {
1366 ab = new ABuffer(outLength);
1367 memcpy(ab->data(),
1368 (const uint8_t *)mb->data() + mb->range_offset(),
1369 mb->range_length());
1370 }
1371
1372 if (audio && mAudioIsVorbis) {
1373 int32_t numPageSamples;
1374 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1375 numPageSamples = -1;
1376 }
1377
1378 uint8_t* abEnd = ab->data() + mb->range_length();
1379 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1380 }
1381
Lajos Molnare26940f2014-07-31 10:31:26 -07001382 sp<AMessage> meta = ab->meta();
1383
Robert Shih3423bbd2014-07-16 15:47:09 -07001384 int64_t timeUs;
1385 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001386 meta->setInt64("timeUs", timeUs);
1387
Wei Jia474d7c72014-12-04 15:12:13 -08001388#if 0
1389 // Temporarily disable pre-roll till we have a full solution to handle
1390 // both single seek and continous seek gracefully.
1391 if (seekTimeUs > timeUs) {
1392 sp<AMessage> extra = new AMessage;
1393 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1394 meta->setMessage("extra", extra);
1395 }
1396#endif
1397
Lajos Molnare26940f2014-07-31 10:31:26 -07001398 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1399 const char *mime;
1400 CHECK(mTimedTextTrack.mSource != NULL
1401 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1402 meta->setString("mime", mime);
1403 }
1404
Robert Shih3423bbd2014-07-16 15:47:09 -07001405 int64_t durationUs;
1406 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1407 meta->setInt64("durationUs", durationUs);
1408 }
1409
1410 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1411 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1412 }
1413
Robert Shihf8bd8512015-04-23 16:39:18 -07001414 uint32_t dataType; // unused
1415 const void *seiData;
1416 size_t seiLength;
1417 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1418 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1419 meta->setBuffer("sei", sei);
1420 }
1421
Robert Shih3423bbd2014-07-16 15:47:09 -07001422 if (actualTimeUs) {
1423 *actualTimeUs = timeUs;
1424 }
1425
1426 mb->release();
1427 mb = NULL;
1428
1429 return ab;
1430}
1431
Robert Shih17f6dd62014-08-20 17:00:21 -07001432void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001433 Mutex::Autolock _l(mReadBufferLock);
1434
1435 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1436 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001437 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001438 msg->setInt32("trackType", trackType);
1439 msg->post();
1440 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001441}
1442
1443void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1444 int32_t tmpType;
1445 CHECK(msg->findInt32("trackType", &tmpType));
1446 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001447 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001448 {
1449 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001450 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001451 Mutex::Autolock _l(mReadBufferLock);
1452 mPendingReadBufferTypes &= ~(1 << trackType);
1453 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001454}
1455
Andreas Huberafed0e12011-09-20 15:39:58 -07001456void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001457 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001458 // Do not read data if Widevine source is stopped
1459 if (mStopRead) {
1460 return;
1461 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001462 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001463 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001464 switch (trackType) {
1465 case MEDIA_TRACK_TYPE_VIDEO:
1466 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001467 if (mIsWidevine) {
1468 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001469 } else {
1470 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001471 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001472 break;
1473 case MEDIA_TRACK_TYPE_AUDIO:
1474 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001475 if (mIsWidevine) {
1476 maxBuffers = 8;
1477 } else {
1478 maxBuffers = 64;
1479 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001480 break;
1481 case MEDIA_TRACK_TYPE_SUBTITLE:
1482 track = &mSubtitleTrack;
1483 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001484 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1485 track = &mTimedTextTrack;
1486 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001487 default:
1488 TRESPASS();
1489 }
1490
1491 if (track->mSource == NULL) {
1492 return;
1493 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001494
1495 if (actualTimeUs) {
1496 *actualTimeUs = seekTimeUs;
1497 }
1498
1499 MediaSource::ReadOptions options;
1500
1501 bool seeking = false;
1502
1503 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001504 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001505 seeking = true;
1506 }
1507
Chong Zhang42e81532014-12-01 13:44:26 -08001508 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001509 options.setNonBlocking();
1510 }
1511
Phil Burkc5cc2e22014-09-09 20:08:39 -07001512 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001513 MediaBuffer *mbuf;
1514 status_t err = track->mSource->read(&mbuf, &options);
1515
1516 options.clearSeekTo();
1517
1518 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001519 int64_t timeUs;
1520 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1521 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1522 mAudioTimeUs = timeUs;
1523 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1524 mVideoTimeUs = timeUs;
1525 }
1526
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001527 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001528
Wei Jia474d7c72014-12-04 15:12:13 -08001529 sp<ABuffer> buffer = mediaBufferToABuffer(
Shivaprasad Hongal98e7ece2015-07-23 19:57:14 -07001530 mbuf, trackType, seekTimeUs,
1531 numBuffers == 0 ? actualTimeUs : NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -07001532 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001533 formatChange = false;
1534 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001535 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001536 } else if (err == WOULD_BLOCK) {
1537 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001538 } else if (err == INFO_FORMAT_CHANGED) {
1539#if 0
1540 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001541 ATSParser::DISCONTINUITY_FORMATCHANGE,
1542 NULL,
1543 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001544#endif
1545 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001546 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001547 track->mPackets->signalEOS(err);
1548 break;
1549 }
1550 }
1551}
1552
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001553void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1554 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1555 // formatChange && seeking: track whose source is changed during selection
1556 // formatChange && !seeking: track whose source is not changed during selection
1557 // !formatChange: normal seek
1558 if ((seeking || formatChange)
1559 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1560 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1561 ATSParser::DiscontinuityType type = (formatChange && seeking)
1562 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1563 : ATSParser::DISCONTINUITY_NONE;
1564 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1565 }
1566}
1567
Andreas Huberafed0e12011-09-20 15:39:58 -07001568} // namespace android