blob: c5b83018e453cd7fca37a1bd092dc3daf7e16b0b [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) {
Robert Shihebc27122015-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();
483 mCachedSource.clear();
484 mHttpSource.clear();
485 }
486 }
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700487 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700488
489 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700490 }
491 notifyPrepared(err);
492}
493
Andreas Huberafed0e12011-09-20 15:39:58 -0700494void NuPlayer::GenericSource::start() {
495 ALOGI("start");
496
Andy Hung2abde2c2014-09-30 14:40:32 -0700497 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700498 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700499 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700500 }
501
502 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700503 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700504 }
Ronghua Wu80276872014-08-28 15:50:29 -0700505
506 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
507 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800508
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800509 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700510}
511
512void NuPlayer::GenericSource::stop() {
513 // nothing to do, just account for DRM playback status
514 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
515 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800516 if (mIsWidevine || mIsSecure) {
517 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800518 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700519 sp<AMessage> response;
520 (void) msg->postAndAwaitResponse(&response);
521 }
Ronghua Wu80276872014-08-28 15:50:29 -0700522}
523
524void NuPlayer::GenericSource::pause() {
525 // nothing to do, just account for DRM playback status
526 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
527 mStarted = false;
528}
529
530void NuPlayer::GenericSource::resume() {
531 // nothing to do, just account for DRM playback status
532 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
533 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800534
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800535 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700536}
537
Chong Zhang48296b72014-09-14 14:28:45 -0700538void NuPlayer::GenericSource::disconnect() {
Robert Shihebc27122015-09-02 14:02:47 -0700539 sp<DataSource> dataSource, httpSource;
540 {
541 Mutex::Autolock _l(mDisconnectLock);
542 dataSource = mDataSource;
543 httpSource = mHttpSource;
544 }
545
546 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700547 // disconnect data source
Robert Shihebc27122015-09-02 14:02:47 -0700548 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
549 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700550 }
Robert Shihebc27122015-09-02 14:02:47 -0700551 } else if (httpSource != NULL) {
552 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700553 }
554}
555
Ronghua Wu80276872014-08-28 15:50:29 -0700556void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
557 if (mDecryptHandle != NULL) {
558 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
559 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700560 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
561 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700562}
563
564status_t NuPlayer::GenericSource::feedMoreTSData() {
565 return OK;
566}
567
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700568void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800569 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700570 msg->setInt32("generation", mPollBufferingGeneration);
571 msg->post(1000000ll);
572}
573
574void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800575 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700576 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800577 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700578}
579
Chong Zhangefbb6192015-01-30 17:13:27 -0800580void NuPlayer::GenericSource::restartPollBuffering() {
581 if (mIsStreaming) {
582 cancelPollBuffering();
583 onPollBuffering();
584 }
585}
586
Chong Zhangc287cad2015-02-19 18:30:30 -0800587void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
588 // Buffering percent could go backward as it's estimated from remaining
589 // data and last access time. This could cause the buffering position
590 // drawn on media control to jitter slightly. Remember previously reported
591 // percentage and don't allow it to go backward.
592 if (percentage < mPrevBufferPercentage) {
593 percentage = mPrevBufferPercentage;
594 } else if (percentage > 100) {
595 percentage = 100;
596 }
597
598 mPrevBufferPercentage = percentage;
599
Chong Zhangefbb6192015-01-30 17:13:27 -0800600 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
601
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700602 sp<AMessage> msg = dupNotify();
603 msg->setInt32("what", kWhatBufferingUpdate);
604 msg->setInt32("percentage", percentage);
605 msg->post();
606}
607
Chong Zhangefbb6192015-01-30 17:13:27 -0800608void NuPlayer::GenericSource::startBufferingIfNecessary() {
609 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
610 mPrepareBuffering, mBuffering);
611
612 if (mPrepareBuffering) {
613 return;
614 }
615
616 if (!mBuffering) {
617 mBuffering = true;
618
619 ensureCacheIsFetching();
620 sendCacheStats();
621
622 sp<AMessage> notify = dupNotify();
623 notify->setInt32("what", kWhatPauseOnBufferingStart);
624 notify->post();
625 }
626}
627
628void NuPlayer::GenericSource::stopBufferingIfNecessary() {
629 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
630 mPrepareBuffering, mBuffering);
631
632 if (mPrepareBuffering) {
633 mPrepareBuffering = false;
634 notifyPrepared();
635 return;
636 }
637
638 if (mBuffering) {
639 mBuffering = false;
640
641 sendCacheStats();
642
643 sp<AMessage> notify = dupNotify();
644 notify->setInt32("what", kWhatResumeOnBufferingEnd);
645 notify->post();
646 }
647}
648
649void NuPlayer::GenericSource::sendCacheStats() {
650 int32_t kbps = 0;
651 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700652
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800653 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800654 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800655 } else if (mCachedSource != NULL) {
656 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800657 }
658
659 if (err == OK) {
660 sp<AMessage> notify = dupNotify();
661 notify->setInt32("what", kWhatCacheStats);
662 notify->setInt32("bandwidth", kbps);
663 notify->post();
664 }
665}
666
667void NuPlayer::GenericSource::ensureCacheIsFetching() {
668 if (mCachedSource != NULL) {
669 mCachedSource->resumeFetchingIfNecessary();
670 }
671}
672
673void NuPlayer::GenericSource::onPollBuffering() {
674 status_t finalStatus = UNKNOWN_ERROR;
675 int64_t cachedDurationUs = -1ll;
676 ssize_t cachedDataRemaining = -1;
677
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800678 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
679 "WVMExtractor and NuCachedSource both present");
680
681 if (mWVMExtractor != NULL) {
682 cachedDurationUs =
683 mWVMExtractor->getCachedDurationUs(&finalStatus);
684 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800685 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700686 mCachedSource->approxDataRemaining(&finalStatus);
687
688 if (finalStatus == OK) {
689 off64_t size;
690 int64_t bitrate = 0ll;
691 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
692 bitrate = size * 8000000ll / mDurationUs;
693 } else if (mBitrate > 0) {
694 bitrate = mBitrate;
695 }
696 if (bitrate > 0) {
697 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
698 }
699 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700700 }
701
Chong Zhangefbb6192015-01-30 17:13:27 -0800702 if (finalStatus != OK) {
703 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
704
705 if (finalStatus == ERROR_END_OF_STREAM) {
706 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700707 }
708
Chong Zhangefbb6192015-01-30 17:13:27 -0800709 stopBufferingIfNecessary();
710 return;
711 } else if (cachedDurationUs >= 0ll) {
712 if (mDurationUs > 0ll) {
713 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
714 int percentage = 100.0 * cachedPosUs / mDurationUs;
715 if (percentage > 100) {
716 percentage = 100;
717 }
718
719 notifyBufferingUpdate(percentage);
720 }
721
722 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
723 cachedDurationUs / 1000000.0f);
724
725 if (cachedDurationUs < kLowWaterMarkUs) {
726 startBufferingIfNecessary();
727 } else if (cachedDurationUs > kHighWaterMarkUs) {
728 stopBufferingIfNecessary();
729 }
730 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700731 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800732 cachedDataRemaining);
733
734 if (cachedDataRemaining < kLowWaterMarkBytes) {
735 startBufferingIfNecessary();
736 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
737 stopBufferingIfNecessary();
738 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700739 }
740
741 schedulePollBuffering();
742}
743
Robert Shih3423bbd2014-07-16 15:47:09 -0700744void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
745 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700746 case kWhatPrepareAsync:
747 {
748 onPrepareAsync();
749 break;
750 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700751 case kWhatFetchSubtitleData:
752 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700753 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
754 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
755 break;
756 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700757
Lajos Molnare26940f2014-07-31 10:31:26 -0700758 case kWhatFetchTimedTextData:
759 {
760 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
761 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700762 break;
763 }
764
765 case kWhatSendSubtitleData:
766 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700767 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
768 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
769 break;
770 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700771
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700772 case kWhatSendGlobalTimedTextData:
773 {
774 sendGlobalTextData(kWhatTimedTextData, mFetchTimedTextDataGeneration, msg);
775 break;
776 }
Lajos Molnare26940f2014-07-31 10:31:26 -0700777 case kWhatSendTimedTextData:
778 {
779 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
780 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700781 break;
782 }
783
784 case kWhatChangeAVSource:
785 {
786 int32_t trackIndex;
787 CHECK(msg->findInt32("trackIndex", &trackIndex));
788 const sp<MediaSource> source = mSources.itemAt(trackIndex);
789
790 Track* track;
791 const char *mime;
792 media_track_type trackType, counterpartType;
793 sp<MetaData> meta = source->getFormat();
794 meta->findCString(kKeyMIMEType, &mime);
795 if (!strncasecmp(mime, "audio/", 6)) {
796 track = &mAudioTrack;
797 trackType = MEDIA_TRACK_TYPE_AUDIO;
798 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
799 } else {
800 CHECK(!strncasecmp(mime, "video/", 6));
801 track = &mVideoTrack;
802 trackType = MEDIA_TRACK_TYPE_VIDEO;
803 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
804 }
805
806
807 if (track->mSource != NULL) {
808 track->mSource->stop();
809 }
810 track->mSource = source;
811 track->mSource->start();
812 track->mIndex = trackIndex;
813
Robert Shih3423bbd2014-07-16 15:47:09 -0700814 int64_t timeUs, actualTimeUs;
815 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800816 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
817 timeUs = mAudioLastDequeueTimeUs;
818 } else {
819 timeUs = mVideoLastDequeueTimeUs;
820 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700821 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
822 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700823 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700824
825 break;
826 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800827
828 case kWhatStart:
829 case kWhatResume:
830 {
831 restartPollBuffering();
832 break;
833 }
834
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700835 case kWhatPollBuffering:
836 {
837 int32_t generation;
838 CHECK(msg->findInt32("generation", &generation));
839 if (generation == mPollBufferingGeneration) {
840 onPollBuffering();
841 }
842 break;
843 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700844
845 case kWhatGetFormat:
846 {
847 onGetFormatMeta(msg);
848 break;
849 }
850
851 case kWhatGetSelectedTrack:
852 {
853 onGetSelectedTrack(msg);
854 break;
855 }
856
857 case kWhatSelectTrack:
858 {
859 onSelectTrack(msg);
860 break;
861 }
862
863 case kWhatSeek:
864 {
865 onSeek(msg);
866 break;
867 }
868
869 case kWhatReadBuffer:
870 {
871 onReadBuffer(msg);
872 break;
873 }
874
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700875 case kWhatSecureDecodersInstantiated:
876 {
877 int32_t err;
878 CHECK(msg->findInt32("err", &err));
879 onSecureDecodersInstantiated(err);
880 break;
881 }
882
Andy Hung2abde2c2014-09-30 14:40:32 -0700883 case kWhatStopWidevine:
884 {
885 // mStopRead is only used for Widevine to prevent the video source
886 // from being read while the associated video decoder is shutting down.
887 mStopRead = true;
888 if (mVideoTrack.mSource != NULL) {
889 mVideoTrack.mPackets->clear();
890 }
891 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800892 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700893 CHECK(msg->senderAwaitsResponse(&replyID));
894 response->postReply(replyID);
895 break;
896 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700897 default:
898 Source::onMessageReceived(msg);
899 break;
900 }
901}
902
Lajos Molnare26940f2014-07-31 10:31:26 -0700903void NuPlayer::GenericSource::fetchTextData(
904 uint32_t sendWhat,
905 media_track_type type,
906 int32_t curGen,
907 sp<AnotherPacketSource> packets,
908 sp<AMessage> msg) {
909 int32_t msgGeneration;
910 CHECK(msg->findInt32("generation", &msgGeneration));
911 if (msgGeneration != curGen) {
912 // stale
913 return;
914 }
915
916 int32_t avail;
917 if (packets->hasBufferAvailable(&avail)) {
918 return;
919 }
920
921 int64_t timeUs;
922 CHECK(msg->findInt64("timeUs", &timeUs));
923
924 int64_t subTimeUs;
925 readBuffer(type, timeUs, &subTimeUs);
926
927 int64_t delayUs = subTimeUs - timeUs;
928 if (msg->what() == kWhatFetchSubtitleData) {
929 const int64_t oneSecUs = 1000000ll;
930 delayUs -= oneSecUs;
931 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800932 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700933 msg2->setInt32("generation", msgGeneration);
934 msg2->post(delayUs < 0 ? 0 : delayUs);
935}
936
937void NuPlayer::GenericSource::sendTextData(
938 uint32_t what,
939 media_track_type type,
940 int32_t curGen,
941 sp<AnotherPacketSource> packets,
942 sp<AMessage> msg) {
943 int32_t msgGeneration;
944 CHECK(msg->findInt32("generation", &msgGeneration));
945 if (msgGeneration != curGen) {
946 // stale
947 return;
948 }
949
950 int64_t subTimeUs;
951 if (packets->nextBufferTime(&subTimeUs) != OK) {
952 return;
953 }
954
955 int64_t nextSubTimeUs;
956 readBuffer(type, -1, &nextSubTimeUs);
957
958 sp<ABuffer> buffer;
959 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
960 if (dequeueStatus == OK) {
961 sp<AMessage> notify = dupNotify();
962 notify->setInt32("what", what);
963 notify->setBuffer("buffer", buffer);
964 notify->post();
965
966 const int64_t delayUs = nextSubTimeUs - subTimeUs;
967 msg->post(delayUs < 0 ? 0 : delayUs);
968 }
969}
970
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700971void NuPlayer::GenericSource::sendGlobalTextData(
972 uint32_t what,
973 int32_t curGen,
974 sp<AMessage> msg) {
975 int32_t msgGeneration;
976 CHECK(msg->findInt32("generation", &msgGeneration));
977 if (msgGeneration != curGen) {
978 // stale
979 return;
980 }
981
982 uint32_t textType;
983 const void *data;
984 size_t size = 0;
985 if (mTimedTextTrack.mSource->getFormat()->findData(
986 kKeyTextFormatData, &textType, &data, &size)) {
987 mGlobalTimedText = new ABuffer(size);
988 if (mGlobalTimedText->data()) {
989 memcpy(mGlobalTimedText->data(), data, size);
990 sp<AMessage> globalMeta = mGlobalTimedText->meta();
991 globalMeta->setInt64("timeUs", 0);
992 globalMeta->setString("mime", MEDIA_MIMETYPE_TEXT_3GPP);
993 globalMeta->setInt32("global", 1);
994 sp<AMessage> notify = dupNotify();
995 notify->setInt32("what", what);
996 notify->setBuffer("buffer", mGlobalTimedText);
997 notify->post();
998 }
999 }
1000}
1001
Andreas Huber84066782011-08-16 09:34:26 -07001002sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001003 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001004 msg->setInt32("audio", audio);
1005
1006 sp<AMessage> response;
1007 void *format;
1008 status_t err = msg->postAndAwaitResponse(&response);
1009 if (err == OK && response != NULL) {
1010 CHECK(response->findPointer("format", &format));
1011 return (MetaData *)format;
1012 } else {
1013 return NULL;
1014 }
1015}
1016
1017void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1018 int32_t audio;
1019 CHECK(msg->findInt32("audio", &audio));
1020
1021 sp<AMessage> response = new AMessage;
1022 sp<MetaData> format = doGetFormatMeta(audio);
1023 response->setPointer("format", format.get());
1024
Lajos Molnar3f274362015-03-05 14:35:41 -08001025 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001026 CHECK(msg->senderAwaitsResponse(&replyID));
1027 response->postReply(replyID);
1028}
1029
1030sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001031 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1032
1033 if (source == NULL) {
1034 return NULL;
1035 }
1036
1037 return source->getFormat();
1038}
1039
1040status_t NuPlayer::GenericSource::dequeueAccessUnit(
1041 bool audio, sp<ABuffer> *accessUnit) {
1042 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1043
1044 if (track->mSource == NULL) {
1045 return -EWOULDBLOCK;
1046 }
1047
Lajos Molnarcc227032014-07-17 15:33:06 -07001048 if (mIsWidevine && !audio) {
1049 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001050 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001051 }
1052
Andreas Huberafed0e12011-09-20 15:39:58 -07001053 status_t finalResult;
1054 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001055 if (finalResult == OK) {
1056 postReadBuffer(
1057 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1058 return -EWOULDBLOCK;
1059 }
1060 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001061 }
1062
1063 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1064
Chong Zhangfcf044a2015-07-14 15:58:51 -07001065 // start pulling in more buffers if we only have one (or no) buffer left
1066 // so that decoder has less chance of being starved
1067 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001068 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001069 }
1070
Robert Shih3423bbd2014-07-16 15:47:09 -07001071 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001072 if (mSubtitleTrack.mSource != NULL) {
1073 mSubtitleTrack.mPackets->clear();
1074 mFetchSubtitleDataGeneration++;
1075 }
1076 if (mTimedTextTrack.mSource != NULL) {
1077 mTimedTextTrack.mPackets->clear();
1078 mFetchTimedTextDataGeneration++;
1079 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001080 return result;
1081 }
1082
1083 int64_t timeUs;
1084 status_t eosResult; // ignored
1085 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001086 if (audio) {
1087 mAudioLastDequeueTimeUs = timeUs;
1088 } else {
1089 mVideoLastDequeueTimeUs = timeUs;
1090 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001091
1092 if (mSubtitleTrack.mSource != NULL
1093 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001094 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001095 msg->setInt64("timeUs", timeUs);
1096 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1097 msg->post();
1098 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001099
Lajos Molnare26940f2014-07-31 10:31:26 -07001100 if (mTimedTextTrack.mSource != NULL
1101 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001102 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001103 msg->setInt64("timeUs", timeUs);
1104 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1105 msg->post();
1106 }
1107
Andreas Huberafed0e12011-09-20 15:39:58 -07001108 return result;
1109}
1110
1111status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1112 *durationUs = mDurationUs;
1113 return OK;
1114}
1115
Robert Shihdd235722014-06-12 14:49:23 -07001116size_t NuPlayer::GenericSource::getTrackCount() const {
1117 return mSources.size();
1118}
1119
1120sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1121 size_t trackCount = mSources.size();
1122 if (trackIndex >= trackCount) {
1123 return NULL;
1124 }
1125
1126 sp<AMessage> format = new AMessage();
1127 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
1128
1129 const char *mime;
1130 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001131 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001132
1133 int32_t trackType;
1134 if (!strncasecmp(mime, "video/", 6)) {
1135 trackType = MEDIA_TRACK_TYPE_VIDEO;
1136 } else if (!strncasecmp(mime, "audio/", 6)) {
1137 trackType = MEDIA_TRACK_TYPE_AUDIO;
1138 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1139 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1140 } else {
1141 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1142 }
1143 format->setInt32("type", trackType);
1144
1145 const char *lang;
1146 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1147 lang = "und";
1148 }
1149 format->setString("language", lang);
1150
1151 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001152 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1153 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1154 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1155 meta->findInt32(kKeyTrackIsForced, &isForced);
1156
1157 format->setInt32("auto", !!isAutoselect);
1158 format->setInt32("default", !!isDefault);
1159 format->setInt32("forced", !!isForced);
1160 }
1161
1162 return format;
1163}
1164
Lajos Molnare26940f2014-07-31 10:31:26 -07001165ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001166 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001167 msg->setInt32("type", type);
1168
1169 sp<AMessage> response;
1170 int32_t index;
1171 status_t err = msg->postAndAwaitResponse(&response);
1172 if (err == OK && response != NULL) {
1173 CHECK(response->findInt32("index", &index));
1174 return index;
1175 } else {
1176 return -1;
1177 }
1178}
1179
1180void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1181 int32_t tmpType;
1182 CHECK(msg->findInt32("type", &tmpType));
1183 media_track_type type = (media_track_type)tmpType;
1184
1185 sp<AMessage> response = new AMessage;
1186 ssize_t index = doGetSelectedTrack(type);
1187 response->setInt32("index", index);
1188
Lajos Molnar3f274362015-03-05 14:35:41 -08001189 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001190 CHECK(msg->senderAwaitsResponse(&replyID));
1191 response->postReply(replyID);
1192}
1193
1194ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001195 const Track *track = NULL;
1196 switch (type) {
1197 case MEDIA_TRACK_TYPE_VIDEO:
1198 track = &mVideoTrack;
1199 break;
1200 case MEDIA_TRACK_TYPE_AUDIO:
1201 track = &mAudioTrack;
1202 break;
1203 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1204 track = &mTimedTextTrack;
1205 break;
1206 case MEDIA_TRACK_TYPE_SUBTITLE:
1207 track = &mSubtitleTrack;
1208 break;
1209 default:
1210 break;
1211 }
1212
1213 if (track != NULL && track->mSource != NULL) {
1214 return track->mIndex;
1215 }
1216
1217 return -1;
1218}
1219
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001220status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001221 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001222 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001223 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001224 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001225 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001226
1227 sp<AMessage> response;
1228 status_t err = msg->postAndAwaitResponse(&response);
1229 if (err == OK && response != NULL) {
1230 CHECK(response->findInt32("err", &err));
1231 }
1232
1233 return err;
1234}
1235
1236void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1237 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001238 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001239 CHECK(msg->findInt32("trackIndex", &trackIndex));
1240 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001241 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001242
1243 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001244 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001245 response->setInt32("err", err);
1246
Lajos Molnar3f274362015-03-05 14:35:41 -08001247 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001248 CHECK(msg->senderAwaitsResponse(&replyID));
1249 response->postReply(replyID);
1250}
1251
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001252status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001253 if (trackIndex >= mSources.size()) {
1254 return BAD_INDEX;
1255 }
1256
1257 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001258 Track* track = NULL;
1259 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1260 track = &mSubtitleTrack;
1261 mFetchSubtitleDataGeneration++;
1262 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1263 track = &mTimedTextTrack;
1264 mFetchTimedTextDataGeneration++;
1265 }
1266 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001267 return INVALID_OPERATION;
1268 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001269 track->mSource->stop();
1270 track->mSource = NULL;
1271 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001272 return OK;
1273 }
1274
1275 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1276 sp<MetaData> meta = source->getFormat();
1277 const char *mime;
1278 CHECK(meta->findCString(kKeyMIMEType, &mime));
1279 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001280 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1281 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1282 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001283 return OK;
1284 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001285 track->mIndex = trackIndex;
1286 if (track->mSource != NULL) {
1287 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001288 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001289 track->mSource = mSources.itemAt(trackIndex);
1290 track->mSource->start();
1291 if (track->mPackets == NULL) {
1292 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001293 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001294 track->mPackets->clear();
1295 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001296
1297 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001298
1299 if (isSubtitle) {
1300 mFetchSubtitleDataGeneration++;
1301 } else {
1302 mFetchTimedTextDataGeneration++;
1303 }
1304
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001305 status_t eosResult; // ignored
1306 if (mSubtitleTrack.mSource != NULL
1307 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001308 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001309 msg->setInt64("timeUs", timeUs);
1310 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1311 msg->post();
1312 }
1313
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07001314 sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this);
1315 msg2->setInt32("generation", mFetchTimedTextDataGeneration);
1316 msg2->post();
1317
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001318 if (mTimedTextTrack.mSource != NULL
1319 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001320 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001321 msg->setInt64("timeUs", timeUs);
1322 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1323 msg->post();
1324 }
1325
Robert Shih3423bbd2014-07-16 15:47:09 -07001326 return OK;
1327 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1328 bool audio = !strncasecmp(mime, "audio/", 6);
1329 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1330 if (track->mSource != NULL && track->mIndex == trackIndex) {
1331 return OK;
1332 }
1333
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001334 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001335 msg->setInt32("trackIndex", trackIndex);
1336 msg->post();
1337 return OK;
1338 }
1339
1340 return INVALID_OPERATION;
1341}
1342
Andreas Huberafed0e12011-09-20 15:39:58 -07001343status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001344 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001345 msg->setInt64("seekTimeUs", seekTimeUs);
1346
1347 sp<AMessage> response;
1348 status_t err = msg->postAndAwaitResponse(&response);
1349 if (err == OK && response != NULL) {
1350 CHECK(response->findInt32("err", &err));
1351 }
1352
1353 return err;
1354}
1355
1356void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1357 int64_t seekTimeUs;
1358 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1359
1360 sp<AMessage> response = new AMessage;
1361 status_t err = doSeek(seekTimeUs);
1362 response->setInt32("err", err);
1363
Lajos Molnar3f274362015-03-05 14:35:41 -08001364 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001365 CHECK(msg->senderAwaitsResponse(&replyID));
1366 response->postReply(replyID);
1367}
1368
1369status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001370 // If the Widevine source is stopped, do not attempt to read any
1371 // more buffers.
1372 if (mStopRead) {
1373 return INVALID_OPERATION;
1374 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001375 if (mVideoTrack.mSource != NULL) {
1376 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001377 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001378
1379 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001380 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001381 }
1382
1383 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001384 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001385 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001386 }
1387
Ronghua Wu80276872014-08-28 15:50:29 -07001388 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1389 if (!mStarted) {
1390 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1391 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001392
1393 // If currently buffering, post kWhatBufferingEnd first, so that
1394 // NuPlayer resumes. Otherwise, if cache hits high watermark
1395 // before new polling happens, no one will resume the playback.
1396 stopBufferingIfNecessary();
1397 restartPollBuffering();
1398
Andreas Huberafed0e12011-09-20 15:39:58 -07001399 return OK;
1400}
1401
Robert Shih3423bbd2014-07-16 15:47:09 -07001402sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1403 MediaBuffer* mb,
1404 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001405 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001406 int64_t *actualTimeUs) {
1407 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1408 size_t outLength = mb->range_length();
1409
1410 if (audio && mAudioIsVorbis) {
1411 outLength += sizeof(int32_t);
1412 }
1413
1414 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001415 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001416 // data is already provided in the buffer
1417 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001418 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001419 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001420 } else {
1421 ab = new ABuffer(outLength);
1422 memcpy(ab->data(),
1423 (const uint8_t *)mb->data() + mb->range_offset(),
1424 mb->range_length());
1425 }
1426
1427 if (audio && mAudioIsVorbis) {
1428 int32_t numPageSamples;
1429 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1430 numPageSamples = -1;
1431 }
1432
1433 uint8_t* abEnd = ab->data() + mb->range_length();
1434 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1435 }
1436
Lajos Molnare26940f2014-07-31 10:31:26 -07001437 sp<AMessage> meta = ab->meta();
1438
Robert Shih3423bbd2014-07-16 15:47:09 -07001439 int64_t timeUs;
1440 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001441 meta->setInt64("timeUs", timeUs);
1442
Wei Jia474d7c72014-12-04 15:12:13 -08001443#if 0
1444 // Temporarily disable pre-roll till we have a full solution to handle
1445 // both single seek and continous seek gracefully.
1446 if (seekTimeUs > timeUs) {
1447 sp<AMessage> extra = new AMessage;
1448 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1449 meta->setMessage("extra", extra);
1450 }
1451#endif
1452
Lajos Molnare26940f2014-07-31 10:31:26 -07001453 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1454 const char *mime;
1455 CHECK(mTimedTextTrack.mSource != NULL
1456 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1457 meta->setString("mime", mime);
1458 }
1459
Robert Shih3423bbd2014-07-16 15:47:09 -07001460 int64_t durationUs;
1461 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1462 meta->setInt64("durationUs", durationUs);
1463 }
1464
1465 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1466 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1467 }
1468
Robert Shihf8bd8512015-04-23 16:39:18 -07001469 uint32_t dataType; // unused
1470 const void *seiData;
1471 size_t seiLength;
1472 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1473 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1474 meta->setBuffer("sei", sei);
1475 }
1476
Robert Shih3423bbd2014-07-16 15:47:09 -07001477 if (actualTimeUs) {
1478 *actualTimeUs = timeUs;
1479 }
1480
1481 mb->release();
1482 mb = NULL;
1483
1484 return ab;
1485}
1486
Robert Shih17f6dd62014-08-20 17:00:21 -07001487void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001488 Mutex::Autolock _l(mReadBufferLock);
1489
1490 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1491 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001492 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001493 msg->setInt32("trackType", trackType);
1494 msg->post();
1495 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001496}
1497
1498void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1499 int32_t tmpType;
1500 CHECK(msg->findInt32("trackType", &tmpType));
1501 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001502 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001503 {
1504 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001505 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001506 Mutex::Autolock _l(mReadBufferLock);
1507 mPendingReadBufferTypes &= ~(1 << trackType);
1508 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001509}
1510
Andreas Huberafed0e12011-09-20 15:39:58 -07001511void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001512 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001513 // Do not read data if Widevine source is stopped
1514 if (mStopRead) {
1515 return;
1516 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001517 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001518 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001519 switch (trackType) {
1520 case MEDIA_TRACK_TYPE_VIDEO:
1521 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001522 if (mIsWidevine) {
1523 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001524 } else {
1525 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001526 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001527 break;
1528 case MEDIA_TRACK_TYPE_AUDIO:
1529 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001530 if (mIsWidevine) {
1531 maxBuffers = 8;
1532 } else {
1533 maxBuffers = 64;
1534 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001535 break;
1536 case MEDIA_TRACK_TYPE_SUBTITLE:
1537 track = &mSubtitleTrack;
1538 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001539 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1540 track = &mTimedTextTrack;
1541 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001542 default:
1543 TRESPASS();
1544 }
1545
1546 if (track->mSource == NULL) {
1547 return;
1548 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001549
1550 if (actualTimeUs) {
1551 *actualTimeUs = seekTimeUs;
1552 }
1553
1554 MediaSource::ReadOptions options;
1555
1556 bool seeking = false;
1557
1558 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001559 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001560 seeking = true;
1561 }
1562
Chong Zhang42e81532014-12-01 13:44:26 -08001563 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001564 options.setNonBlocking();
1565 }
1566
Phil Burkc5cc2e22014-09-09 20:08:39 -07001567 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001568 MediaBuffer *mbuf;
1569 status_t err = track->mSource->read(&mbuf, &options);
1570
1571 options.clearSeekTo();
1572
1573 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001574 int64_t timeUs;
1575 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1576 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1577 mAudioTimeUs = timeUs;
1578 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1579 mVideoTimeUs = timeUs;
1580 }
1581
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001582 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001583
Wei Jia474d7c72014-12-04 15:12:13 -08001584 sp<ABuffer> buffer = mediaBufferToABuffer(
Shivaprasad Hongal98e7ece2015-07-23 19:57:14 -07001585 mbuf, trackType, seekTimeUs,
1586 numBuffers == 0 ? actualTimeUs : NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -07001587 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001588 formatChange = false;
1589 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001590 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001591 } else if (err == WOULD_BLOCK) {
1592 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001593 } else if (err == INFO_FORMAT_CHANGED) {
1594#if 0
1595 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001596 ATSParser::DISCONTINUITY_FORMATCHANGE,
1597 NULL,
1598 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001599#endif
1600 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001601 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001602 track->mPackets->signalEOS(err);
1603 break;
1604 }
1605 }
1606}
1607
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001608void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1609 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1610 // formatChange && seeking: track whose source is changed during selection
1611 // formatChange && !seeking: track whose source is not changed during selection
1612 // !formatChange: normal seek
1613 if ((seeking || formatChange)
1614 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1615 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1616 ATSParser::DiscontinuityType type = (formatChange && seeking)
1617 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1618 : ATSParser::DISCONTINUITY_NONE;
1619 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1620 }
1621}
1622
Andreas Huberafed0e12011-09-20 15:39:58 -07001623} // namespace android