blob: 7c3b4092f53b0f2bd1b7e90e33813f6866fdda7e [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);
Marco Nelissenc367ca12015-09-15 09:51:59 -0700219 if (meta == NULL) {
220 ALOGE("no metadata for track %zu", i);
221 return UNKNOWN_ERROR;
222 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700223
224 const char *mime;
225 CHECK(meta->findCString(kKeyMIMEType, &mime));
226
Chong Zhangafc0a872014-08-26 09:56:52 -0700227 // Do the string compare immediately with "mime",
228 // we can't assume "mime" would stay valid after another
229 // extractor operation, some extractors might modify meta
230 // during getTrack() and make it invalid.
Andreas Huberafed0e12011-09-20 15:39:58 -0700231 if (!strncasecmp(mime, "audio/", 6)) {
232 if (mAudioTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700233 mAudioTrack.mIndex = i;
234 mAudioTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700235 mAudioTrack.mPackets =
236 new AnotherPacketSource(mAudioTrack.mSource->getFormat());
Andreas Huberafed0e12011-09-20 15:39:58 -0700237
238 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) {
239 mAudioIsVorbis = true;
240 } else {
241 mAudioIsVorbis = false;
242 }
243 }
244 } else if (!strncasecmp(mime, "video/", 6)) {
245 if (mVideoTrack.mSource == NULL) {
Robert Shihdd235722014-06-12 14:49:23 -0700246 mVideoTrack.mIndex = i;
247 mVideoTrack.mSource = track;
Robert Shihaf52c1a2014-09-11 15:38:54 -0700248 mVideoTrack.mPackets =
249 new AnotherPacketSource(mVideoTrack.mSource->getFormat());
Chong Zhang7e892182014-08-05 11:58:21 -0700250
251 // check if the source requires secure buffers
252 int32_t secure;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700253 if (meta->findInt32(kKeyRequiresSecureBuffers, &secure)
254 && secure) {
Chong Zhang42e81532014-12-01 13:44:26 -0800255 mIsSecure = true;
Chong Zhang3de157d2014-08-05 20:54:44 -0700256 if (mUIDValid) {
257 extractor->setUID(mUID);
258 }
Chong Zhang7e892182014-08-05 11:58:21 -0700259 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700260 }
261 }
262
Wei Jia0386c912015-08-28 10:35:35 -0700263 mSources.push(track);
264 int64_t durationUs;
265 if (meta->findInt64(kKeyDuration, &durationUs)) {
266 if (durationUs > mDurationUs) {
267 mDurationUs = durationUs;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700268 }
Andreas Huberafed0e12011-09-20 15:39:58 -0700269 }
Wei Jia0386c912015-08-28 10:35:35 -0700270
271 int32_t bitrate;
272 if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) {
273 totalBitrate += bitrate;
274 } else {
275 totalBitrate = -1;
276 }
277 }
278
279 if (mSources.size() == 0) {
280 ALOGE("b/23705695");
281 return UNKNOWN_ERROR;
Andreas Huberafed0e12011-09-20 15:39:58 -0700282 }
Chong Zhang3de157d2014-08-05 20:54:44 -0700283
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700284 mBitrate = totalBitrate;
285
286 return OK;
287}
288
289status_t NuPlayer::GenericSource::startSources() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800290 // Start the selected A/V tracks now before we start buffering.
291 // Widevine sources might re-initialize crypto when starting, if we delay
292 // this to start(), all data buffered during prepare would be wasted.
293 // (We don't actually start reading until start().)
294 if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) {
295 ALOGE("failed to start audio track!");
296 return UNKNOWN_ERROR;
297 }
298
299 if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) {
300 ALOGE("failed to start video track!");
301 return UNKNOWN_ERROR;
302 }
303
Chong Zhang3de157d2014-08-05 20:54:44 -0700304 return OK;
Andreas Huberafed0e12011-09-20 15:39:58 -0700305}
306
Ronghua Wu80276872014-08-28 15:50:29 -0700307void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) {
308 dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient);
309 if (mDecryptHandle != NULL) {
310 CHECK(mDrmManagerClient);
311 if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) {
312 sp<AMessage> msg = dupNotify();
313 msg->setInt32("what", kWhatDrmNoLicense);
314 msg->post();
315 }
316 }
317}
318
319int64_t NuPlayer::GenericSource::getLastReadPosition() {
320 if (mAudioTrack.mSource != NULL) {
321 return mAudioTimeUs;
322 } else if (mVideoTrack.mSource != NULL) {
323 return mVideoTimeUs;
324 } else {
325 return 0;
326 }
327}
328
Chong Zhanga19f33e2014-08-07 15:35:07 -0700329status_t NuPlayer::GenericSource::setBuffers(
330 bool audio, Vector<MediaBuffer *> &buffers) {
Wei Jia0386c912015-08-28 10:35:35 -0700331 if (mIsSecure && !audio && mVideoTrack.mSource != NULL) {
Lajos Molnarcc227032014-07-17 15:33:06 -0700332 return mVideoTrack.mSource->setBuffers(buffers);
333 }
334 return INVALID_OPERATION;
335}
336
Ronghua Wu02cb98d2015-05-27 11:02:54 -0700337bool NuPlayer::GenericSource::isStreaming() const {
338 return mIsStreaming;
339}
340
Andreas Huberafed0e12011-09-20 15:39:58 -0700341NuPlayer::GenericSource::~GenericSource() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700342 if (mLooper != NULL) {
343 mLooper->unregisterHandler(id());
344 mLooper->stop();
345 }
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800346 resetDataSource();
Andreas Huberafed0e12011-09-20 15:39:58 -0700347}
348
Andreas Huber9575c962013-02-05 13:59:56 -0800349void NuPlayer::GenericSource::prepareAsync() {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700350 if (mLooper == NULL) {
351 mLooper = new ALooper;
352 mLooper->setName("generic");
353 mLooper->start();
354
355 mLooper->registerHandler(this);
356 }
357
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800358 sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this);
Chong Zhang1228d6b2014-08-12 21:25:48 -0700359 msg->post();
360}
361
362void NuPlayer::GenericSource::onPrepareAsync() {
Chong Zhanga19f33e2014-08-07 15:35:07 -0700363 // delayed data source creation
Chong Zhangd354d8d2014-08-20 13:09:58 -0700364 if (mDataSource == NULL) {
Chong Zhang42e81532014-12-01 13:44:26 -0800365 // set to false first, if the extractor
366 // comes back as secure, set it to true then.
367 mIsSecure = false;
368
Chong Zhangd354d8d2014-08-20 13:09:58 -0700369 if (!mUri.empty()) {
Robert Shih360d6d02014-09-29 14:42:35 -0700370 const char* uri = mUri.c_str();
Chong Zhangc287cad2015-02-19 18:30:30 -0800371 String8 contentType;
Robert Shih360d6d02014-09-29 14:42:35 -0700372 mIsWidevine = !strncasecmp(uri, "widevine://", 11);
373
374 if (!strncasecmp("http://", uri, 7)
375 || !strncasecmp("https://", uri, 8)
376 || mIsWidevine) {
377 mHttpSource = DataSource::CreateMediaHTTP(mHTTPService);
378 if (mHttpSource == NULL) {
379 ALOGE("Failed to create http source!");
380 notifyPreparedAndCleanup(UNKNOWN_ERROR);
381 return;
382 }
383 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700384
Chong Zhangd354d8d2014-08-20 13:09:58 -0700385 mDataSource = DataSource::CreateFromURI(
Chong Zhangc287cad2015-02-19 18:30:30 -0800386 mHTTPService, uri, &mUriHeaders, &contentType,
Robert Shih360d6d02014-09-29 14:42:35 -0700387 static_cast<HTTPBase *>(mHttpSource.get()));
Chong Zhangd354d8d2014-08-20 13:09:58 -0700388 } else {
Chong Zhangd354d8d2014-08-20 13:09:58 -0700389 mIsWidevine = false;
Chong Zhanga19f33e2014-08-07 15:35:07 -0700390
Chong Zhangd354d8d2014-08-20 13:09:58 -0700391 mDataSource = new FileSource(mFd, mOffset, mLength);
Chong Zhanga6bf21f2014-11-19 20:26:34 -0800392 mFd = -1;
Chong Zhangd354d8d2014-08-20 13:09:58 -0700393 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700394
Chong Zhangd354d8d2014-08-20 13:09:58 -0700395 if (mDataSource == NULL) {
396 ALOGE("Failed to create data source!");
397 notifyPreparedAndCleanup(UNKNOWN_ERROR);
398 return;
399 }
Chong Zhanga19f33e2014-08-07 15:35:07 -0700400 }
401
Chris Watkins99f31602015-03-20 13:06:33 -0700402 if (mDataSource->flags() & DataSource::kIsCachingDataSource) {
403 mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get());
404 }
405
406 // For widevine or other cached streaming cases, we need to wait for
407 // enough buffering before reporting prepared.
408 // Note that even when URL doesn't start with widevine://, mIsWidevine
409 // could still be set to true later, if the streaming or file source
410 // is sniffed to be widevine. We don't want to buffer for file source
411 // in that case, so must check the flag now.
412 mIsStreaming = (mIsWidevine || mCachedSource != NULL);
413
Chong Zhangc287cad2015-02-19 18:30:30 -0800414 // init extractor from data source
415 status_t err = initFromDataSource();
Chong Zhanga19f33e2014-08-07 15:35:07 -0700416
417 if (err != OK) {
418 ALOGE("Failed to init from data source!");
Chong Zhangd354d8d2014-08-20 13:09:58 -0700419 notifyPreparedAndCleanup(err);
Chong Zhanga19f33e2014-08-07 15:35:07 -0700420 return;
421 }
422
Andreas Huber9575c962013-02-05 13:59:56 -0800423 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700424 sp<MetaData> meta = doGetFormatMeta(false /* audio */);
425 sp<AMessage> msg = new AMessage;
426 err = convertMetaDataToMessage(meta, &msg);
427 if(err != OK) {
428 notifyPreparedAndCleanup(err);
429 return;
430 }
431 notifyVideoSizeChanged(msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800432 }
433
434 notifyFlagsChanged(
Chong Zhang42e81532014-12-01 13:44:26 -0800435 (mIsSecure ? FLAG_SECURE : 0)
Chong Zhang17134602015-01-07 16:14:34 -0800436 | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0)
Lajos Molnarcc227032014-07-17 15:33:06 -0700437 | FLAG_CAN_PAUSE
Andreas Huber9575c962013-02-05 13:59:56 -0800438 | FLAG_CAN_SEEK_BACKWARD
439 | FLAG_CAN_SEEK_FORWARD
440 | FLAG_CAN_SEEK);
441
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700442 if (mIsSecure) {
443 // secure decoders must be instantiated before starting widevine source
444 sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, this);
445 notifyInstantiateSecureDecoders(reply);
446 } else {
447 finishPrepareAsync();
448 }
449}
450
451void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) {
452 if (err != OK) {
453 ALOGE("Failed to instantiate secure decoders!");
454 notifyPreparedAndCleanup(err);
455 return;
456 }
457 finishPrepareAsync();
458}
459
460void NuPlayer::GenericSource::finishPrepareAsync() {
461 status_t err = startSources();
462 if (err != OK) {
463 ALOGE("Failed to init start data source!");
464 notifyPreparedAndCleanup(err);
465 return;
466 }
467
Chong Zhangefbb6192015-01-30 17:13:27 -0800468 if (mIsStreaming) {
469 mPrepareBuffering = true;
470
471 ensureCacheIsFetching();
472 restartPollBuffering();
473 } else {
474 notifyPrepared();
475 }
Andreas Huber9575c962013-02-05 13:59:56 -0800476}
477
Chong Zhangd354d8d2014-08-20 13:09:58 -0700478void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) {
479 if (err != OK) {
Robert Shihebc27122015-09-02 14:02:47 -0700480 {
481 sp<DataSource> dataSource = mDataSource;
482 sp<NuCachedSource2> cachedSource = mCachedSource;
483 sp<DataSource> httpSource = mHttpSource;
484 {
485 Mutex::Autolock _l(mDisconnectLock);
486 mDataSource.clear();
487 mCachedSource.clear();
488 mHttpSource.clear();
489 }
490 }
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700491 mBitrate = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700492
493 cancelPollBuffering();
Chong Zhangd354d8d2014-08-20 13:09:58 -0700494 }
495 notifyPrepared(err);
496}
497
Andreas Huberafed0e12011-09-20 15:39:58 -0700498void NuPlayer::GenericSource::start() {
499 ALOGI("start");
500
Andy Hung2abde2c2014-09-30 14:40:32 -0700501 mStopRead = false;
Andreas Huberafed0e12011-09-20 15:39:58 -0700502 if (mAudioTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700503 postReadBuffer(MEDIA_TRACK_TYPE_AUDIO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700504 }
505
506 if (mVideoTrack.mSource != NULL) {
Robert Shih17f6dd62014-08-20 17:00:21 -0700507 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Andreas Huberafed0e12011-09-20 15:39:58 -0700508 }
Ronghua Wu80276872014-08-28 15:50:29 -0700509
510 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
511 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800512
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800513 (new AMessage(kWhatStart, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700514}
515
516void NuPlayer::GenericSource::stop() {
517 // nothing to do, just account for DRM playback status
518 setDrmPlaybackStatusIfNeeded(Playback::STOP, 0);
519 mStarted = false;
Chong Zhang42e81532014-12-01 13:44:26 -0800520 if (mIsWidevine || mIsSecure) {
521 // For widevine or secure sources we need to prevent any further reads.
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800522 sp<AMessage> msg = new AMessage(kWhatStopWidevine, this);
Andy Hung2abde2c2014-09-30 14:40:32 -0700523 sp<AMessage> response;
524 (void) msg->postAndAwaitResponse(&response);
525 }
Ronghua Wu80276872014-08-28 15:50:29 -0700526}
527
528void NuPlayer::GenericSource::pause() {
529 // nothing to do, just account for DRM playback status
530 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
531 mStarted = false;
532}
533
534void NuPlayer::GenericSource::resume() {
535 // nothing to do, just account for DRM playback status
536 setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000);
537 mStarted = true;
Chong Zhangefbb6192015-01-30 17:13:27 -0800538
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800539 (new AMessage(kWhatResume, this))->post();
Ronghua Wu80276872014-08-28 15:50:29 -0700540}
541
Chong Zhang48296b72014-09-14 14:28:45 -0700542void NuPlayer::GenericSource::disconnect() {
Robert Shihebc27122015-09-02 14:02:47 -0700543 sp<DataSource> dataSource, httpSource;
544 {
545 Mutex::Autolock _l(mDisconnectLock);
546 dataSource = mDataSource;
547 httpSource = mHttpSource;
548 }
549
550 if (dataSource != NULL) {
Chong Zhang48296b72014-09-14 14:28:45 -0700551 // disconnect data source
Robert Shihebc27122015-09-02 14:02:47 -0700552 if (dataSource->flags() & DataSource::kIsCachingDataSource) {
553 static_cast<NuCachedSource2 *>(dataSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700554 }
Robert Shihebc27122015-09-02 14:02:47 -0700555 } else if (httpSource != NULL) {
556 static_cast<HTTPBase *>(httpSource.get())->disconnect();
Chong Zhang48296b72014-09-14 14:28:45 -0700557 }
558}
559
Ronghua Wu80276872014-08-28 15:50:29 -0700560void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) {
561 if (mDecryptHandle != NULL) {
562 mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position);
563 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700564 mSubtitleTrack.mPackets = new AnotherPacketSource(NULL);
565 mTimedTextTrack.mPackets = new AnotherPacketSource(NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -0700566}
567
568status_t NuPlayer::GenericSource::feedMoreTSData() {
569 return OK;
570}
571
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700572void NuPlayer::GenericSource::schedulePollBuffering() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800573 sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700574 msg->setInt32("generation", mPollBufferingGeneration);
575 msg->post(1000000ll);
576}
577
578void NuPlayer::GenericSource::cancelPollBuffering() {
Chong Zhangefbb6192015-01-30 17:13:27 -0800579 mBuffering = false;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700580 ++mPollBufferingGeneration;
Chong Zhangc287cad2015-02-19 18:30:30 -0800581 mPrevBufferPercentage = -1;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700582}
583
Chong Zhangefbb6192015-01-30 17:13:27 -0800584void NuPlayer::GenericSource::restartPollBuffering() {
585 if (mIsStreaming) {
586 cancelPollBuffering();
587 onPollBuffering();
588 }
589}
590
Chong Zhangc287cad2015-02-19 18:30:30 -0800591void NuPlayer::GenericSource::notifyBufferingUpdate(int32_t percentage) {
592 // Buffering percent could go backward as it's estimated from remaining
593 // data and last access time. This could cause the buffering position
594 // drawn on media control to jitter slightly. Remember previously reported
595 // percentage and don't allow it to go backward.
596 if (percentage < mPrevBufferPercentage) {
597 percentage = mPrevBufferPercentage;
598 } else if (percentage > 100) {
599 percentage = 100;
600 }
601
602 mPrevBufferPercentage = percentage;
603
Chong Zhangefbb6192015-01-30 17:13:27 -0800604 ALOGV("notifyBufferingUpdate: buffering %d%%", percentage);
605
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700606 sp<AMessage> msg = dupNotify();
607 msg->setInt32("what", kWhatBufferingUpdate);
608 msg->setInt32("percentage", percentage);
609 msg->post();
610}
611
Chong Zhangefbb6192015-01-30 17:13:27 -0800612void NuPlayer::GenericSource::startBufferingIfNecessary() {
613 ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
614 mPrepareBuffering, mBuffering);
615
616 if (mPrepareBuffering) {
617 return;
618 }
619
620 if (!mBuffering) {
621 mBuffering = true;
622
623 ensureCacheIsFetching();
624 sendCacheStats();
625
626 sp<AMessage> notify = dupNotify();
627 notify->setInt32("what", kWhatPauseOnBufferingStart);
628 notify->post();
629 }
630}
631
632void NuPlayer::GenericSource::stopBufferingIfNecessary() {
633 ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d",
634 mPrepareBuffering, mBuffering);
635
636 if (mPrepareBuffering) {
637 mPrepareBuffering = false;
638 notifyPrepared();
639 return;
640 }
641
642 if (mBuffering) {
643 mBuffering = false;
644
645 sendCacheStats();
646
647 sp<AMessage> notify = dupNotify();
648 notify->setInt32("what", kWhatResumeOnBufferingEnd);
649 notify->post();
650 }
651}
652
653void NuPlayer::GenericSource::sendCacheStats() {
654 int32_t kbps = 0;
655 status_t err = UNKNOWN_ERROR;
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700656
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800657 if (mWVMExtractor != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800658 err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps);
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800659 } else if (mCachedSource != NULL) {
660 err = mCachedSource->getEstimatedBandwidthKbps(&kbps);
Chong Zhangefbb6192015-01-30 17:13:27 -0800661 }
662
663 if (err == OK) {
664 sp<AMessage> notify = dupNotify();
665 notify->setInt32("what", kWhatCacheStats);
666 notify->setInt32("bandwidth", kbps);
667 notify->post();
668 }
669}
670
671void NuPlayer::GenericSource::ensureCacheIsFetching() {
672 if (mCachedSource != NULL) {
673 mCachedSource->resumeFetchingIfNecessary();
674 }
675}
676
677void NuPlayer::GenericSource::onPollBuffering() {
678 status_t finalStatus = UNKNOWN_ERROR;
679 int64_t cachedDurationUs = -1ll;
680 ssize_t cachedDataRemaining = -1;
681
Chong Zhangfc6cfd82015-02-19 16:39:59 -0800682 ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL,
683 "WVMExtractor and NuCachedSource both present");
684
685 if (mWVMExtractor != NULL) {
686 cachedDurationUs =
687 mWVMExtractor->getCachedDurationUs(&finalStatus);
688 } else if (mCachedSource != NULL) {
Chong Zhangefbb6192015-01-30 17:13:27 -0800689 cachedDataRemaining =
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700690 mCachedSource->approxDataRemaining(&finalStatus);
691
692 if (finalStatus == OK) {
693 off64_t size;
694 int64_t bitrate = 0ll;
695 if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) {
696 bitrate = size * 8000000ll / mDurationUs;
697 } else if (mBitrate > 0) {
698 bitrate = mBitrate;
699 }
700 if (bitrate > 0) {
701 cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate;
702 }
703 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700704 }
705
Chong Zhangefbb6192015-01-30 17:13:27 -0800706 if (finalStatus != OK) {
707 ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus);
708
709 if (finalStatus == ERROR_END_OF_STREAM) {
710 notifyBufferingUpdate(100);
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700711 }
712
Chong Zhangefbb6192015-01-30 17:13:27 -0800713 stopBufferingIfNecessary();
714 return;
715 } else if (cachedDurationUs >= 0ll) {
716 if (mDurationUs > 0ll) {
717 int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs;
718 int percentage = 100.0 * cachedPosUs / mDurationUs;
719 if (percentage > 100) {
720 percentage = 100;
721 }
722
723 notifyBufferingUpdate(percentage);
724 }
725
726 ALOGV("onPollBuffering: cachedDurationUs %.1f sec",
727 cachedDurationUs / 1000000.0f);
728
729 if (cachedDurationUs < kLowWaterMarkUs) {
730 startBufferingIfNecessary();
731 } else if (cachedDurationUs > kHighWaterMarkUs) {
732 stopBufferingIfNecessary();
733 }
734 } else if (cachedDataRemaining >= 0) {
Lajos Molnar6d339f12015-04-17 16:15:53 -0700735 ALOGV("onPollBuffering: cachedDataRemaining %zd bytes",
Chong Zhangefbb6192015-01-30 17:13:27 -0800736 cachedDataRemaining);
737
738 if (cachedDataRemaining < kLowWaterMarkBytes) {
739 startBufferingIfNecessary();
740 } else if (cachedDataRemaining > kHighWaterMarkBytes) {
741 stopBufferingIfNecessary();
742 }
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700743 }
744
745 schedulePollBuffering();
746}
747
Robert Shih3423bbd2014-07-16 15:47:09 -0700748void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) {
749 switch (msg->what()) {
Chong Zhang1228d6b2014-08-12 21:25:48 -0700750 case kWhatPrepareAsync:
751 {
752 onPrepareAsync();
753 break;
754 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700755 case kWhatFetchSubtitleData:
756 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700757 fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
758 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
759 break;
760 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700761
Lajos Molnare26940f2014-07-31 10:31:26 -0700762 case kWhatFetchTimedTextData:
763 {
764 fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
765 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700766 break;
767 }
768
769 case kWhatSendSubtitleData:
770 {
Lajos Molnare26940f2014-07-31 10:31:26 -0700771 sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE,
772 mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg);
773 break;
774 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700775
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700776 case kWhatSendGlobalTimedTextData:
777 {
778 sendGlobalTextData(kWhatTimedTextData, mFetchTimedTextDataGeneration, msg);
779 break;
780 }
Lajos Molnare26940f2014-07-31 10:31:26 -0700781 case kWhatSendTimedTextData:
782 {
783 sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT,
784 mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg);
Robert Shih3423bbd2014-07-16 15:47:09 -0700785 break;
786 }
787
788 case kWhatChangeAVSource:
789 {
790 int32_t trackIndex;
791 CHECK(msg->findInt32("trackIndex", &trackIndex));
792 const sp<MediaSource> source = mSources.itemAt(trackIndex);
793
794 Track* track;
795 const char *mime;
796 media_track_type trackType, counterpartType;
797 sp<MetaData> meta = source->getFormat();
798 meta->findCString(kKeyMIMEType, &mime);
799 if (!strncasecmp(mime, "audio/", 6)) {
800 track = &mAudioTrack;
801 trackType = MEDIA_TRACK_TYPE_AUDIO;
802 counterpartType = MEDIA_TRACK_TYPE_VIDEO;;
803 } else {
804 CHECK(!strncasecmp(mime, "video/", 6));
805 track = &mVideoTrack;
806 trackType = MEDIA_TRACK_TYPE_VIDEO;
807 counterpartType = MEDIA_TRACK_TYPE_AUDIO;;
808 }
809
810
811 if (track->mSource != NULL) {
812 track->mSource->stop();
813 }
814 track->mSource = source;
815 track->mSource->start();
816 track->mIndex = trackIndex;
817
Robert Shih3423bbd2014-07-16 15:47:09 -0700818 int64_t timeUs, actualTimeUs;
819 const bool formatChange = true;
Robert Shih5c67ddc2014-11-04 17:46:05 -0800820 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
821 timeUs = mAudioLastDequeueTimeUs;
822 } else {
823 timeUs = mVideoLastDequeueTimeUs;
824 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700825 readBuffer(trackType, timeUs, &actualTimeUs, formatChange);
826 readBuffer(counterpartType, -1, NULL, formatChange);
Lajos Molnar6d339f12015-04-17 16:15:53 -0700827 ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs);
Robert Shih3423bbd2014-07-16 15:47:09 -0700828
829 break;
830 }
Chong Zhangefbb6192015-01-30 17:13:27 -0800831
832 case kWhatStart:
833 case kWhatResume:
834 {
835 restartPollBuffering();
836 break;
837 }
838
Chong Zhang2a3cc9a2014-08-21 17:48:26 -0700839 case kWhatPollBuffering:
840 {
841 int32_t generation;
842 CHECK(msg->findInt32("generation", &generation));
843 if (generation == mPollBufferingGeneration) {
844 onPollBuffering();
845 }
846 break;
847 }
Robert Shih17f6dd62014-08-20 17:00:21 -0700848
849 case kWhatGetFormat:
850 {
851 onGetFormatMeta(msg);
852 break;
853 }
854
855 case kWhatGetSelectedTrack:
856 {
857 onGetSelectedTrack(msg);
858 break;
859 }
860
861 case kWhatSelectTrack:
862 {
863 onSelectTrack(msg);
864 break;
865 }
866
867 case kWhatSeek:
868 {
869 onSeek(msg);
870 break;
871 }
872
873 case kWhatReadBuffer:
874 {
875 onReadBuffer(msg);
876 break;
877 }
878
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700879 case kWhatSecureDecodersInstantiated:
880 {
881 int32_t err;
882 CHECK(msg->findInt32("err", &err));
883 onSecureDecodersInstantiated(err);
884 break;
885 }
886
Andy Hung2abde2c2014-09-30 14:40:32 -0700887 case kWhatStopWidevine:
888 {
889 // mStopRead is only used for Widevine to prevent the video source
890 // from being read while the associated video decoder is shutting down.
891 mStopRead = true;
892 if (mVideoTrack.mSource != NULL) {
893 mVideoTrack.mPackets->clear();
894 }
895 sp<AMessage> response = new AMessage;
Lajos Molnar3f274362015-03-05 14:35:41 -0800896 sp<AReplyToken> replyID;
Andy Hung2abde2c2014-09-30 14:40:32 -0700897 CHECK(msg->senderAwaitsResponse(&replyID));
898 response->postReply(replyID);
899 break;
900 }
Robert Shih3423bbd2014-07-16 15:47:09 -0700901 default:
902 Source::onMessageReceived(msg);
903 break;
904 }
905}
906
Lajos Molnare26940f2014-07-31 10:31:26 -0700907void NuPlayer::GenericSource::fetchTextData(
908 uint32_t sendWhat,
909 media_track_type type,
910 int32_t curGen,
911 sp<AnotherPacketSource> packets,
912 sp<AMessage> msg) {
913 int32_t msgGeneration;
914 CHECK(msg->findInt32("generation", &msgGeneration));
915 if (msgGeneration != curGen) {
916 // stale
917 return;
918 }
919
920 int32_t avail;
921 if (packets->hasBufferAvailable(&avail)) {
922 return;
923 }
924
925 int64_t timeUs;
926 CHECK(msg->findInt64("timeUs", &timeUs));
927
928 int64_t subTimeUs;
929 readBuffer(type, timeUs, &subTimeUs);
930
931 int64_t delayUs = subTimeUs - timeUs;
932 if (msg->what() == kWhatFetchSubtitleData) {
933 const int64_t oneSecUs = 1000000ll;
934 delayUs -= oneSecUs;
935 }
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800936 sp<AMessage> msg2 = new AMessage(sendWhat, this);
Lajos Molnare26940f2014-07-31 10:31:26 -0700937 msg2->setInt32("generation", msgGeneration);
938 msg2->post(delayUs < 0 ? 0 : delayUs);
939}
940
941void NuPlayer::GenericSource::sendTextData(
942 uint32_t what,
943 media_track_type type,
944 int32_t curGen,
945 sp<AnotherPacketSource> packets,
946 sp<AMessage> msg) {
947 int32_t msgGeneration;
948 CHECK(msg->findInt32("generation", &msgGeneration));
949 if (msgGeneration != curGen) {
950 // stale
951 return;
952 }
953
954 int64_t subTimeUs;
955 if (packets->nextBufferTime(&subTimeUs) != OK) {
956 return;
957 }
958
959 int64_t nextSubTimeUs;
960 readBuffer(type, -1, &nextSubTimeUs);
961
962 sp<ABuffer> buffer;
963 status_t dequeueStatus = packets->dequeueAccessUnit(&buffer);
964 if (dequeueStatus == OK) {
965 sp<AMessage> notify = dupNotify();
966 notify->setInt32("what", what);
967 notify->setBuffer("buffer", buffer);
968 notify->post();
969
970 const int64_t delayUs = nextSubTimeUs - subTimeUs;
971 msg->post(delayUs < 0 ? 0 : delayUs);
972 }
973}
974
Marco Nelissen55e2f4c2015-09-04 15:57:15 -0700975void NuPlayer::GenericSource::sendGlobalTextData(
976 uint32_t what,
977 int32_t curGen,
978 sp<AMessage> msg) {
979 int32_t msgGeneration;
980 CHECK(msg->findInt32("generation", &msgGeneration));
981 if (msgGeneration != curGen) {
982 // stale
983 return;
984 }
985
986 uint32_t textType;
987 const void *data;
988 size_t size = 0;
989 if (mTimedTextTrack.mSource->getFormat()->findData(
990 kKeyTextFormatData, &textType, &data, &size)) {
991 mGlobalTimedText = new ABuffer(size);
992 if (mGlobalTimedText->data()) {
993 memcpy(mGlobalTimedText->data(), data, size);
994 sp<AMessage> globalMeta = mGlobalTimedText->meta();
995 globalMeta->setInt64("timeUs", 0);
996 globalMeta->setString("mime", MEDIA_MIMETYPE_TEXT_3GPP);
997 globalMeta->setInt32("global", 1);
998 sp<AMessage> notify = dupNotify();
999 notify->setInt32("what", what);
1000 notify->setBuffer("buffer", mGlobalTimedText);
1001 notify->post();
1002 }
1003 }
1004}
1005
Andreas Huber84066782011-08-16 09:34:26 -07001006sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001007 sp<AMessage> msg = new AMessage(kWhatGetFormat, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001008 msg->setInt32("audio", audio);
1009
1010 sp<AMessage> response;
1011 void *format;
1012 status_t err = msg->postAndAwaitResponse(&response);
1013 if (err == OK && response != NULL) {
1014 CHECK(response->findPointer("format", &format));
1015 return (MetaData *)format;
1016 } else {
1017 return NULL;
1018 }
1019}
1020
1021void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const {
1022 int32_t audio;
1023 CHECK(msg->findInt32("audio", &audio));
1024
1025 sp<AMessage> response = new AMessage;
1026 sp<MetaData> format = doGetFormatMeta(audio);
1027 response->setPointer("format", format.get());
1028
Lajos Molnar3f274362015-03-05 14:35:41 -08001029 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001030 CHECK(msg->senderAwaitsResponse(&replyID));
1031 response->postReply(replyID);
1032}
1033
1034sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const {
Andreas Huberafed0e12011-09-20 15:39:58 -07001035 sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource;
1036
1037 if (source == NULL) {
1038 return NULL;
1039 }
1040
1041 return source->getFormat();
1042}
1043
1044status_t NuPlayer::GenericSource::dequeueAccessUnit(
1045 bool audio, sp<ABuffer> *accessUnit) {
1046 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1047
1048 if (track->mSource == NULL) {
1049 return -EWOULDBLOCK;
1050 }
1051
Lajos Molnarcc227032014-07-17 15:33:06 -07001052 if (mIsWidevine && !audio) {
1053 // try to read a buffer as we may not have been able to the last time
Robert Shih17f6dd62014-08-20 17:00:21 -07001054 postReadBuffer(MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnarcc227032014-07-17 15:33:06 -07001055 }
1056
Andreas Huberafed0e12011-09-20 15:39:58 -07001057 status_t finalResult;
1058 if (!track->mPackets->hasBufferAvailable(&finalResult)) {
Chong Zhang42e81532014-12-01 13:44:26 -08001059 if (finalResult == OK) {
1060 postReadBuffer(
1061 audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
1062 return -EWOULDBLOCK;
1063 }
1064 return finalResult;
Andreas Huberafed0e12011-09-20 15:39:58 -07001065 }
1066
1067 status_t result = track->mPackets->dequeueAccessUnit(accessUnit);
1068
Chong Zhangfcf044a2015-07-14 15:58:51 -07001069 // start pulling in more buffers if we only have one (or no) buffer left
1070 // so that decoder has less chance of being starved
1071 if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) {
Robert Shih17f6dd62014-08-20 17:00:21 -07001072 postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO);
Lajos Molnare26940f2014-07-31 10:31:26 -07001073 }
1074
Robert Shih3423bbd2014-07-16 15:47:09 -07001075 if (result != OK) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001076 if (mSubtitleTrack.mSource != NULL) {
1077 mSubtitleTrack.mPackets->clear();
1078 mFetchSubtitleDataGeneration++;
1079 }
1080 if (mTimedTextTrack.mSource != NULL) {
1081 mTimedTextTrack.mPackets->clear();
1082 mFetchTimedTextDataGeneration++;
1083 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001084 return result;
1085 }
1086
1087 int64_t timeUs;
1088 status_t eosResult; // ignored
1089 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
Robert Shih5c67ddc2014-11-04 17:46:05 -08001090 if (audio) {
1091 mAudioLastDequeueTimeUs = timeUs;
1092 } else {
1093 mVideoLastDequeueTimeUs = timeUs;
1094 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001095
1096 if (mSubtitleTrack.mSource != NULL
1097 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001098 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001099 msg->setInt64("timeUs", timeUs);
1100 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1101 msg->post();
1102 }
Robert Shiheb1735e2014-07-23 15:53:14 -07001103
Lajos Molnare26940f2014-07-31 10:31:26 -07001104 if (mTimedTextTrack.mSource != NULL
1105 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001106 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Lajos Molnare26940f2014-07-31 10:31:26 -07001107 msg->setInt64("timeUs", timeUs);
1108 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1109 msg->post();
1110 }
1111
Andreas Huberafed0e12011-09-20 15:39:58 -07001112 return result;
1113}
1114
1115status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) {
1116 *durationUs = mDurationUs;
1117 return OK;
1118}
1119
Robert Shihdd235722014-06-12 14:49:23 -07001120size_t NuPlayer::GenericSource::getTrackCount() const {
1121 return mSources.size();
1122}
1123
1124sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const {
1125 size_t trackCount = mSources.size();
1126 if (trackIndex >= trackCount) {
1127 return NULL;
1128 }
1129
1130 sp<AMessage> format = new AMessage();
1131 sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat();
Marco Nelissenc367ca12015-09-15 09:51:59 -07001132 if (meta == NULL) {
1133 ALOGE("no metadata for track %zu", trackIndex);
1134 return NULL;
1135 }
Robert Shihdd235722014-06-12 14:49:23 -07001136
1137 const char *mime;
1138 CHECK(meta->findCString(kKeyMIMEType, &mime));
Robert Shih755106e2015-04-30 14:36:45 -07001139 format->setString("mime", mime);
Robert Shihdd235722014-06-12 14:49:23 -07001140
1141 int32_t trackType;
1142 if (!strncasecmp(mime, "video/", 6)) {
1143 trackType = MEDIA_TRACK_TYPE_VIDEO;
1144 } else if (!strncasecmp(mime, "audio/", 6)) {
1145 trackType = MEDIA_TRACK_TYPE_AUDIO;
1146 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
1147 trackType = MEDIA_TRACK_TYPE_TIMEDTEXT;
1148 } else {
1149 trackType = MEDIA_TRACK_TYPE_UNKNOWN;
1150 }
1151 format->setInt32("type", trackType);
1152
1153 const char *lang;
1154 if (!meta->findCString(kKeyMediaLanguage, &lang)) {
1155 lang = "und";
1156 }
1157 format->setString("language", lang);
1158
1159 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
Robert Shihdd235722014-06-12 14:49:23 -07001160 int32_t isAutoselect = 1, isDefault = 0, isForced = 0;
1161 meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect);
1162 meta->findInt32(kKeyTrackIsDefault, &isDefault);
1163 meta->findInt32(kKeyTrackIsForced, &isForced);
1164
1165 format->setInt32("auto", !!isAutoselect);
1166 format->setInt32("default", !!isDefault);
1167 format->setInt32("forced", !!isForced);
1168 }
1169
1170 return format;
1171}
1172
Lajos Molnare26940f2014-07-31 10:31:26 -07001173ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001174 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001175 msg->setInt32("type", type);
1176
1177 sp<AMessage> response;
1178 int32_t index;
1179 status_t err = msg->postAndAwaitResponse(&response);
1180 if (err == OK && response != NULL) {
1181 CHECK(response->findInt32("index", &index));
1182 return index;
1183 } else {
1184 return -1;
1185 }
1186}
1187
1188void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const {
1189 int32_t tmpType;
1190 CHECK(msg->findInt32("type", &tmpType));
1191 media_track_type type = (media_track_type)tmpType;
1192
1193 sp<AMessage> response = new AMessage;
1194 ssize_t index = doGetSelectedTrack(type);
1195 response->setInt32("index", index);
1196
Lajos Molnar3f274362015-03-05 14:35:41 -08001197 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001198 CHECK(msg->senderAwaitsResponse(&replyID));
1199 response->postReply(replyID);
1200}
1201
1202ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const {
Lajos Molnare26940f2014-07-31 10:31:26 -07001203 const Track *track = NULL;
1204 switch (type) {
1205 case MEDIA_TRACK_TYPE_VIDEO:
1206 track = &mVideoTrack;
1207 break;
1208 case MEDIA_TRACK_TYPE_AUDIO:
1209 track = &mAudioTrack;
1210 break;
1211 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1212 track = &mTimedTextTrack;
1213 break;
1214 case MEDIA_TRACK_TYPE_SUBTITLE:
1215 track = &mSubtitleTrack;
1216 break;
1217 default:
1218 break;
1219 }
1220
1221 if (track != NULL && track->mSource != NULL) {
1222 return track->mIndex;
1223 }
1224
1225 return -1;
1226}
1227
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001228status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001229 ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001230 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001231 msg->setInt32("trackIndex", trackIndex);
Robert Shihda23ab92014-09-16 11:34:08 -07001232 msg->setInt32("select", select);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001233 msg->setInt64("timeUs", timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001234
1235 sp<AMessage> response;
1236 status_t err = msg->postAndAwaitResponse(&response);
1237 if (err == OK && response != NULL) {
1238 CHECK(response->findInt32("err", &err));
1239 }
1240
1241 return err;
1242}
1243
1244void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) {
1245 int32_t trackIndex, select;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001246 int64_t timeUs;
Robert Shih17f6dd62014-08-20 17:00:21 -07001247 CHECK(msg->findInt32("trackIndex", &trackIndex));
1248 CHECK(msg->findInt32("select", &select));
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001249 CHECK(msg->findInt64("timeUs", &timeUs));
Robert Shih17f6dd62014-08-20 17:00:21 -07001250
1251 sp<AMessage> response = new AMessage;
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001252 status_t err = doSelectTrack(trackIndex, select, timeUs);
Robert Shih17f6dd62014-08-20 17:00:21 -07001253 response->setInt32("err", err);
1254
Lajos Molnar3f274362015-03-05 14:35:41 -08001255 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001256 CHECK(msg->senderAwaitsResponse(&replyID));
1257 response->postReply(replyID);
1258}
1259
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001260status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001261 if (trackIndex >= mSources.size()) {
1262 return BAD_INDEX;
1263 }
1264
1265 if (!select) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001266 Track* track = NULL;
1267 if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) {
1268 track = &mSubtitleTrack;
1269 mFetchSubtitleDataGeneration++;
1270 } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) {
1271 track = &mTimedTextTrack;
1272 mFetchTimedTextDataGeneration++;
1273 }
1274 if (track == NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001275 return INVALID_OPERATION;
1276 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001277 track->mSource->stop();
1278 track->mSource = NULL;
1279 track->mPackets->clear();
Robert Shih3423bbd2014-07-16 15:47:09 -07001280 return OK;
1281 }
1282
1283 const sp<MediaSource> source = mSources.itemAt(trackIndex);
1284 sp<MetaData> meta = source->getFormat();
1285 const char *mime;
1286 CHECK(meta->findCString(kKeyMIMEType, &mime));
1287 if (!strncasecmp(mime, "text/", 5)) {
Lajos Molnare26940f2014-07-31 10:31:26 -07001288 bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP);
1289 Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack;
1290 if (track->mSource != NULL && track->mIndex == trackIndex) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001291 return OK;
1292 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001293 track->mIndex = trackIndex;
1294 if (track->mSource != NULL) {
1295 track->mSource->stop();
Robert Shih3423bbd2014-07-16 15:47:09 -07001296 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001297 track->mSource = mSources.itemAt(trackIndex);
1298 track->mSource->start();
1299 if (track->mPackets == NULL) {
1300 track->mPackets = new AnotherPacketSource(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001301 } else {
Lajos Molnare26940f2014-07-31 10:31:26 -07001302 track->mPackets->clear();
1303 track->mPackets->setFormat(track->mSource->getFormat());
Robert Shih3423bbd2014-07-16 15:47:09 -07001304
1305 }
Lajos Molnare26940f2014-07-31 10:31:26 -07001306
1307 if (isSubtitle) {
1308 mFetchSubtitleDataGeneration++;
1309 } else {
1310 mFetchTimedTextDataGeneration++;
1311 }
1312
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001313 status_t eosResult; // ignored
1314 if (mSubtitleTrack.mSource != NULL
1315 && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001316 sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001317 msg->setInt64("timeUs", timeUs);
1318 msg->setInt32("generation", mFetchSubtitleDataGeneration);
1319 msg->post();
1320 }
1321
Marco Nelissen55e2f4c2015-09-04 15:57:15 -07001322 sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this);
1323 msg2->setInt32("generation", mFetchTimedTextDataGeneration);
1324 msg2->post();
1325
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001326 if (mTimedTextTrack.mSource != NULL
1327 && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001328 sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this);
Robert Shih6ffb1fd2014-10-29 16:24:32 -07001329 msg->setInt64("timeUs", timeUs);
1330 msg->setInt32("generation", mFetchTimedTextDataGeneration);
1331 msg->post();
1332 }
1333
Robert Shih3423bbd2014-07-16 15:47:09 -07001334 return OK;
1335 } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) {
1336 bool audio = !strncasecmp(mime, "audio/", 6);
1337 Track *track = audio ? &mAudioTrack : &mVideoTrack;
1338 if (track->mSource != NULL && track->mIndex == trackIndex) {
1339 return OK;
1340 }
1341
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001342 sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this);
Robert Shih3423bbd2014-07-16 15:47:09 -07001343 msg->setInt32("trackIndex", trackIndex);
1344 msg->post();
1345 return OK;
1346 }
1347
1348 return INVALID_OPERATION;
1349}
1350
Andreas Huberafed0e12011-09-20 15:39:58 -07001351status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001352 sp<AMessage> msg = new AMessage(kWhatSeek, this);
Robert Shih17f6dd62014-08-20 17:00:21 -07001353 msg->setInt64("seekTimeUs", seekTimeUs);
1354
1355 sp<AMessage> response;
1356 status_t err = msg->postAndAwaitResponse(&response);
1357 if (err == OK && response != NULL) {
1358 CHECK(response->findInt32("err", &err));
1359 }
1360
1361 return err;
1362}
1363
1364void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) {
1365 int64_t seekTimeUs;
1366 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
1367
1368 sp<AMessage> response = new AMessage;
1369 status_t err = doSeek(seekTimeUs);
1370 response->setInt32("err", err);
1371
Lajos Molnar3f274362015-03-05 14:35:41 -08001372 sp<AReplyToken> replyID;
Robert Shih17f6dd62014-08-20 17:00:21 -07001373 CHECK(msg->senderAwaitsResponse(&replyID));
1374 response->postReply(replyID);
1375}
1376
1377status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001378 // If the Widevine source is stopped, do not attempt to read any
1379 // more buffers.
1380 if (mStopRead) {
1381 return INVALID_OPERATION;
1382 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001383 if (mVideoTrack.mSource != NULL) {
1384 int64_t actualTimeUs;
Robert Shih3423bbd2014-07-16 15:47:09 -07001385 readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs);
Andreas Huberafed0e12011-09-20 15:39:58 -07001386
1387 seekTimeUs = actualTimeUs;
Robert Shih5c67ddc2014-11-04 17:46:05 -08001388 mVideoLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001389 }
1390
1391 if (mAudioTrack.mSource != NULL) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001392 readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs);
Robert Shih5c67ddc2014-11-04 17:46:05 -08001393 mAudioLastDequeueTimeUs = seekTimeUs;
Andreas Huberafed0e12011-09-20 15:39:58 -07001394 }
1395
Ronghua Wu80276872014-08-28 15:50:29 -07001396 setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000);
1397 if (!mStarted) {
1398 setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0);
1399 }
Chong Zhangefbb6192015-01-30 17:13:27 -08001400
1401 // If currently buffering, post kWhatBufferingEnd first, so that
1402 // NuPlayer resumes. Otherwise, if cache hits high watermark
1403 // before new polling happens, no one will resume the playback.
1404 stopBufferingIfNecessary();
1405 restartPollBuffering();
1406
Andreas Huberafed0e12011-09-20 15:39:58 -07001407 return OK;
1408}
1409
Robert Shih3423bbd2014-07-16 15:47:09 -07001410sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer(
1411 MediaBuffer* mb,
1412 media_track_type trackType,
Wei Jia474d7c72014-12-04 15:12:13 -08001413 int64_t /* seekTimeUs */,
Robert Shih3423bbd2014-07-16 15:47:09 -07001414 int64_t *actualTimeUs) {
1415 bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO;
1416 size_t outLength = mb->range_length();
1417
1418 if (audio && mAudioIsVorbis) {
1419 outLength += sizeof(int32_t);
1420 }
1421
1422 sp<ABuffer> ab;
Chong Zhang42e81532014-12-01 13:44:26 -08001423 if (mIsSecure && !audio) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001424 // data is already provided in the buffer
1425 ab = new ABuffer(NULL, mb->range_length());
Robert Shih3423bbd2014-07-16 15:47:09 -07001426 mb->add_ref();
Wei Jia96e92b52014-09-18 17:36:20 -07001427 ab->setMediaBufferBase(mb);
Robert Shih3423bbd2014-07-16 15:47:09 -07001428 } else {
1429 ab = new ABuffer(outLength);
1430 memcpy(ab->data(),
1431 (const uint8_t *)mb->data() + mb->range_offset(),
1432 mb->range_length());
1433 }
1434
1435 if (audio && mAudioIsVorbis) {
1436 int32_t numPageSamples;
1437 if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) {
1438 numPageSamples = -1;
1439 }
1440
1441 uint8_t* abEnd = ab->data() + mb->range_length();
1442 memcpy(abEnd, &numPageSamples, sizeof(numPageSamples));
1443 }
1444
Lajos Molnare26940f2014-07-31 10:31:26 -07001445 sp<AMessage> meta = ab->meta();
1446
Robert Shih3423bbd2014-07-16 15:47:09 -07001447 int64_t timeUs;
1448 CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs));
Robert Shih3423bbd2014-07-16 15:47:09 -07001449 meta->setInt64("timeUs", timeUs);
1450
Wei Jia474d7c72014-12-04 15:12:13 -08001451#if 0
1452 // Temporarily disable pre-roll till we have a full solution to handle
1453 // both single seek and continous seek gracefully.
1454 if (seekTimeUs > timeUs) {
1455 sp<AMessage> extra = new AMessage;
1456 extra->setInt64("resume-at-mediaTimeUs", seekTimeUs);
1457 meta->setMessage("extra", extra);
1458 }
1459#endif
1460
Lajos Molnare26940f2014-07-31 10:31:26 -07001461 if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) {
1462 const char *mime;
1463 CHECK(mTimedTextTrack.mSource != NULL
1464 && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime));
1465 meta->setString("mime", mime);
1466 }
1467
Robert Shih3423bbd2014-07-16 15:47:09 -07001468 int64_t durationUs;
1469 if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) {
1470 meta->setInt64("durationUs", durationUs);
1471 }
1472
1473 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) {
1474 meta->setInt32("trackIndex", mSubtitleTrack.mIndex);
1475 }
1476
Robert Shihf8bd8512015-04-23 16:39:18 -07001477 uint32_t dataType; // unused
1478 const void *seiData;
1479 size_t seiLength;
1480 if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
1481 sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
1482 meta->setBuffer("sei", sei);
1483 }
1484
Jaesung Chung3694d7c2015-10-21 11:41:38 +09001485 const void *mpegUserDataPointer;
1486 size_t mpegUserDataLength;
1487 if (mb->meta_data()->findData(
1488 kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
1489 sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
1490 meta->setBuffer("mpegUserData", mpegUserData);
1491 }
1492
Robert Shih3423bbd2014-07-16 15:47:09 -07001493 if (actualTimeUs) {
1494 *actualTimeUs = timeUs;
1495 }
1496
1497 mb->release();
1498 mb = NULL;
1499
1500 return ab;
1501}
1502
Robert Shih17f6dd62014-08-20 17:00:21 -07001503void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) {
Lajos Molnar84f52782014-09-11 10:01:55 -07001504 Mutex::Autolock _l(mReadBufferLock);
1505
1506 if ((mPendingReadBufferTypes & (1 << trackType)) == 0) {
1507 mPendingReadBufferTypes |= (1 << trackType);
Lajos Molnar1d15ab52015-03-04 16:46:34 -08001508 sp<AMessage> msg = new AMessage(kWhatReadBuffer, this);
Lajos Molnar84f52782014-09-11 10:01:55 -07001509 msg->setInt32("trackType", trackType);
1510 msg->post();
1511 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001512}
1513
1514void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) {
1515 int32_t tmpType;
1516 CHECK(msg->findInt32("trackType", &tmpType));
1517 media_track_type trackType = (media_track_type)tmpType;
Chong Zhang42e81532014-12-01 13:44:26 -08001518 readBuffer(trackType);
Lajos Molnar84f52782014-09-11 10:01:55 -07001519 {
1520 // only protect the variable change, as readBuffer may
Chong Zhang42e81532014-12-01 13:44:26 -08001521 // take considerable time.
Lajos Molnar84f52782014-09-11 10:01:55 -07001522 Mutex::Autolock _l(mReadBufferLock);
1523 mPendingReadBufferTypes &= ~(1 << trackType);
1524 }
Robert Shih17f6dd62014-08-20 17:00:21 -07001525}
1526
Andreas Huberafed0e12011-09-20 15:39:58 -07001527void NuPlayer::GenericSource::readBuffer(
Robert Shih3423bbd2014-07-16 15:47:09 -07001528 media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) {
Andy Hung2abde2c2014-09-30 14:40:32 -07001529 // Do not read data if Widevine source is stopped
1530 if (mStopRead) {
1531 return;
1532 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001533 Track *track;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001534 size_t maxBuffers = 1;
Robert Shih3423bbd2014-07-16 15:47:09 -07001535 switch (trackType) {
1536 case MEDIA_TRACK_TYPE_VIDEO:
1537 track = &mVideoTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001538 if (mIsWidevine) {
1539 maxBuffers = 2;
Chong Zhangfcf044a2015-07-14 15:58:51 -07001540 } else {
1541 maxBuffers = 4;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001542 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001543 break;
1544 case MEDIA_TRACK_TYPE_AUDIO:
1545 track = &mAudioTrack;
Jeff Tinkera28785a2014-09-23 22:24:26 -07001546 if (mIsWidevine) {
1547 maxBuffers = 8;
1548 } else {
1549 maxBuffers = 64;
1550 }
Robert Shih3423bbd2014-07-16 15:47:09 -07001551 break;
1552 case MEDIA_TRACK_TYPE_SUBTITLE:
1553 track = &mSubtitleTrack;
1554 break;
Lajos Molnare26940f2014-07-31 10:31:26 -07001555 case MEDIA_TRACK_TYPE_TIMEDTEXT:
1556 track = &mTimedTextTrack;
1557 break;
Robert Shih3423bbd2014-07-16 15:47:09 -07001558 default:
1559 TRESPASS();
1560 }
1561
1562 if (track->mSource == NULL) {
1563 return;
1564 }
Andreas Huberafed0e12011-09-20 15:39:58 -07001565
1566 if (actualTimeUs) {
1567 *actualTimeUs = seekTimeUs;
1568 }
1569
1570 MediaSource::ReadOptions options;
1571
1572 bool seeking = false;
1573
1574 if (seekTimeUs >= 0) {
Robert Shih3423bbd2014-07-16 15:47:09 -07001575 options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
Andreas Huberafed0e12011-09-20 15:39:58 -07001576 seeking = true;
1577 }
1578
Chong Zhang42e81532014-12-01 13:44:26 -08001579 if (mIsWidevine) {
Lajos Molnarcc227032014-07-17 15:33:06 -07001580 options.setNonBlocking();
1581 }
1582
Phil Burkc5cc2e22014-09-09 20:08:39 -07001583 for (size_t numBuffers = 0; numBuffers < maxBuffers; ) {
Andreas Huberafed0e12011-09-20 15:39:58 -07001584 MediaBuffer *mbuf;
1585 status_t err = track->mSource->read(&mbuf, &options);
1586
1587 options.clearSeekTo();
1588
1589 if (err == OK) {
Ronghua Wu80276872014-08-28 15:50:29 -07001590 int64_t timeUs;
1591 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs));
1592 if (trackType == MEDIA_TRACK_TYPE_AUDIO) {
1593 mAudioTimeUs = timeUs;
1594 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) {
1595 mVideoTimeUs = timeUs;
1596 }
1597
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001598 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001599
Wei Jia474d7c72014-12-04 15:12:13 -08001600 sp<ABuffer> buffer = mediaBufferToABuffer(
Shivaprasad Hongal98e7ece2015-07-23 19:57:14 -07001601 mbuf, trackType, seekTimeUs,
1602 numBuffers == 0 ? actualTimeUs : NULL);
Andreas Huberafed0e12011-09-20 15:39:58 -07001603 track->mPackets->queueAccessUnit(buffer);
Marco Nelissen317a49a2014-09-16 21:32:33 -07001604 formatChange = false;
1605 seeking = false;
Phil Burkc5cc2e22014-09-09 20:08:39 -07001606 ++numBuffers;
Lajos Molnarcc227032014-07-17 15:33:06 -07001607 } else if (err == WOULD_BLOCK) {
1608 break;
Andreas Huberafed0e12011-09-20 15:39:58 -07001609 } else if (err == INFO_FORMAT_CHANGED) {
1610#if 0
1611 track->mPackets->queueDiscontinuity(
Chong Zhang632740c2014-06-26 13:03:47 -07001612 ATSParser::DISCONTINUITY_FORMATCHANGE,
1613 NULL,
1614 false /* discard */);
Andreas Huberafed0e12011-09-20 15:39:58 -07001615#endif
1616 } else {
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001617 queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track);
Andreas Huberafed0e12011-09-20 15:39:58 -07001618 track->mPackets->signalEOS(err);
1619 break;
1620 }
1621 }
1622}
1623
Ronghua Wu8f291bc2015-05-19 10:11:53 -07001624void NuPlayer::GenericSource::queueDiscontinuityIfNeeded(
1625 bool seeking, bool formatChange, media_track_type trackType, Track *track) {
1626 // formatChange && seeking: track whose source is changed during selection
1627 // formatChange && !seeking: track whose source is not changed during selection
1628 // !formatChange: normal seek
1629 if ((seeking || formatChange)
1630 && (trackType == MEDIA_TRACK_TYPE_AUDIO
1631 || trackType == MEDIA_TRACK_TYPE_VIDEO)) {
1632 ATSParser::DiscontinuityType type = (formatChange && seeking)
1633 ? ATSParser::DISCONTINUITY_FORMATCHANGE
1634 : ATSParser::DISCONTINUITY_NONE;
1635 track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */);
1636 }
1637}
1638
Andreas Huberafed0e12011-09-20 15:39:58 -07001639} // namespace android