Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 17 | //#define LOG_NDEBUG 0 |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 18 | #define LOG_TAG "GenericSource2" |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 19 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 20 | #include "GenericSource2.h" |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 21 | #include "NuPlayer2Drm.h" |
| 22 | |
| 23 | #include "AnotherPacketSource.h" |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <cutils/properties.h> |
| 26 | #include <media/DataSource.h> |
Dongwon Kang | bc8f53b | 2018-01-25 17:01:44 -0800 | [diff] [blame] | 27 | #include <media/MediaBufferHolder.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 28 | #include <media/IMediaExtractorService.h> |
Dongwon Kang | 49ce671 | 2018-01-24 10:16:25 -0800 | [diff] [blame] | 29 | #include <media/IMediaSource.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 30 | #include <media/MediaHTTPService.h> |
| 31 | #include <media/MediaExtractor.h> |
| 32 | #include <media/MediaSource.h> |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 33 | #include <media/NdkWrapper.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 34 | #include <media/stagefright/foundation/ABuffer.h> |
| 35 | #include <media/stagefright/foundation/ADebug.h> |
| 36 | #include <media/stagefright/foundation/AMessage.h> |
| 37 | #include <media/stagefright/DataSourceFactory.h> |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 38 | #include <media/stagefright/FileSource.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 39 | #include <media/stagefright/InterfaceUtils.h> |
| 40 | #include <media/stagefright/MediaBuffer.h> |
| 41 | #include <media/stagefright/MediaClock.h> |
| 42 | #include <media/stagefright/MediaDefs.h> |
| 43 | #include <media/stagefright/MediaExtractorFactory.h> |
| 44 | #include <media/stagefright/MetaData.h> |
| 45 | #include <media/stagefright/Utils.h> |
| 46 | #include "../../libstagefright/include/NuCachedSource2.h" |
| 47 | #include "../../libstagefright/include/HTTPBase.h" |
| 48 | |
| 49 | namespace android { |
| 50 | |
| 51 | static const int kInitialMarkMs = 5000; // 5secs |
| 52 | |
| 53 | //static const int kPausePlaybackMarkMs = 2000; // 2secs |
| 54 | static const int kResumePlaybackMarkMs = 15000; // 15secs |
| 55 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 56 | NuPlayer2::GenericSource2::GenericSource2( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 57 | const sp<AMessage> ¬ify, |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 58 | uid_t uid, |
| 59 | const sp<MediaClock> &mediaClock) |
| 60 | : Source(notify), |
| 61 | mAudioTimeUs(0), |
| 62 | mAudioLastDequeueTimeUs(0), |
| 63 | mVideoTimeUs(0), |
| 64 | mVideoLastDequeueTimeUs(0), |
| 65 | mPrevBufferPercentage(-1), |
| 66 | mPollBufferingGeneration(0), |
| 67 | mSentPauseOnBuffering(false), |
| 68 | mAudioDataGeneration(0), |
| 69 | mVideoDataGeneration(0), |
| 70 | mFetchSubtitleDataGeneration(0), |
| 71 | mFetchTimedTextDataGeneration(0), |
| 72 | mDurationUs(-1ll), |
| 73 | mAudioIsVorbis(false), |
| 74 | mIsSecure(false), |
| 75 | mIsStreaming(false), |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 76 | mUID(uid), |
| 77 | mMediaClock(mediaClock), |
| 78 | mFd(-1), |
| 79 | mBitrate(-1ll), |
| 80 | mPendingReadBufferTypes(0) { |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 81 | ALOGV("GenericSource2"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 82 | CHECK(mediaClock != NULL); |
| 83 | |
| 84 | mBufferingSettings.mInitialMarkMs = kInitialMarkMs; |
| 85 | mBufferingSettings.mResumePlaybackMarkMs = kResumePlaybackMarkMs; |
| 86 | resetDataSource(); |
| 87 | } |
| 88 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 89 | void NuPlayer2::GenericSource2::resetDataSource() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 90 | ALOGV("resetDataSource"); |
| 91 | |
| 92 | mHTTPService.clear(); |
| 93 | mHttpSource.clear(); |
| 94 | mDisconnected = false; |
| 95 | mUri.clear(); |
| 96 | mUriHeaders.clear(); |
| 97 | if (mFd >= 0) { |
| 98 | close(mFd); |
| 99 | mFd = -1; |
| 100 | } |
| 101 | mOffset = 0; |
| 102 | mLength = 0; |
| 103 | mStarted = false; |
| 104 | mPreparing = false; |
| 105 | |
| 106 | mIsDrmProtected = false; |
| 107 | mIsDrmReleased = false; |
| 108 | mIsSecure = false; |
| 109 | mMimes.clear(); |
| 110 | } |
| 111 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 112 | status_t NuPlayer2::GenericSource2::setDataSource( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 113 | const sp<MediaHTTPService> &httpService, |
| 114 | const char *url, |
| 115 | const KeyedVector<String8, String8> *headers) { |
| 116 | Mutex::Autolock _l(mLock); |
| 117 | ALOGV("setDataSource url: %s", url); |
| 118 | |
| 119 | resetDataSource(); |
| 120 | |
| 121 | mHTTPService = httpService; |
| 122 | mUri = url; |
| 123 | |
| 124 | if (headers) { |
| 125 | mUriHeaders = *headers; |
| 126 | } |
| 127 | |
| 128 | // delay data source creation to prepareAsync() to avoid blocking |
| 129 | // the calling thread in setDataSource for any significant time. |
| 130 | return OK; |
| 131 | } |
| 132 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 133 | status_t NuPlayer2::GenericSource2::setDataSource( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 134 | int fd, int64_t offset, int64_t length) { |
| 135 | Mutex::Autolock _l(mLock); |
| 136 | ALOGV("setDataSource %d/%lld/%lld", fd, (long long)offset, (long long)length); |
| 137 | |
| 138 | resetDataSource(); |
| 139 | |
| 140 | mFd = dup(fd); |
| 141 | mOffset = offset; |
| 142 | mLength = length; |
| 143 | |
| 144 | // delay data source creation to prepareAsync() to avoid blocking |
| 145 | // the calling thread in setDataSource for any significant time. |
| 146 | return OK; |
| 147 | } |
| 148 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 149 | status_t NuPlayer2::GenericSource2::setDataSource(const sp<DataSource>& source) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 150 | Mutex::Autolock _l(mLock); |
| 151 | ALOGV("setDataSource (source: %p)", source.get()); |
| 152 | |
| 153 | resetDataSource(); |
| 154 | mDataSource = source; |
| 155 | return OK; |
| 156 | } |
| 157 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 158 | sp<MetaData> NuPlayer2::GenericSource2::getFileFormatMeta() const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 159 | Mutex::Autolock _l(mLock); |
| 160 | return mFileMeta; |
| 161 | } |
| 162 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 163 | status_t NuPlayer2::GenericSource2::initFromDataSource() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 164 | sp<IMediaExtractor> extractor; |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 165 | CHECK(mDataSource != NULL); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 166 | sp<DataSource> dataSource = mDataSource; |
| 167 | |
| 168 | mLock.unlock(); |
| 169 | // This might take long time if data source is not reliable. |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 170 | extractor = MediaExtractorFactory::Create(dataSource, NULL); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 171 | |
| 172 | if (extractor == NULL) { |
| 173 | ALOGE("initFromDataSource, cannot create extractor!"); |
| 174 | return UNKNOWN_ERROR; |
| 175 | } |
| 176 | |
| 177 | sp<MetaData> fileMeta = extractor->getMetaData(); |
| 178 | |
| 179 | size_t numtracks = extractor->countTracks(); |
| 180 | if (numtracks == 0) { |
| 181 | ALOGE("initFromDataSource, source has no track!"); |
| 182 | return UNKNOWN_ERROR; |
| 183 | } |
| 184 | |
| 185 | mLock.lock(); |
| 186 | mFileMeta = fileMeta; |
| 187 | if (mFileMeta != NULL) { |
| 188 | int64_t duration; |
| 189 | if (mFileMeta->findInt64(kKeyDuration, &duration)) { |
| 190 | mDurationUs = duration; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int32_t totalBitrate = 0; |
| 195 | |
| 196 | mMimes.clear(); |
| 197 | |
| 198 | for (size_t i = 0; i < numtracks; ++i) { |
| 199 | sp<IMediaSource> track = extractor->getTrack(i); |
| 200 | if (track == NULL) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | sp<MetaData> meta = extractor->getTrackMetaData(i); |
| 205 | if (meta == NULL) { |
| 206 | ALOGE("no metadata for track %zu", i); |
| 207 | return UNKNOWN_ERROR; |
| 208 | } |
| 209 | |
| 210 | const char *mime; |
| 211 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 212 | |
| 213 | ALOGV("initFromDataSource track[%zu]: %s", i, mime); |
| 214 | |
| 215 | // Do the string compare immediately with "mime", |
| 216 | // we can't assume "mime" would stay valid after another |
| 217 | // extractor operation, some extractors might modify meta |
| 218 | // during getTrack() and make it invalid. |
| 219 | if (!strncasecmp(mime, "audio/", 6)) { |
| 220 | if (mAudioTrack.mSource == NULL) { |
| 221 | mAudioTrack.mIndex = i; |
| 222 | mAudioTrack.mSource = track; |
| 223 | mAudioTrack.mPackets = |
| 224 | new AnotherPacketSource(mAudioTrack.mSource->getFormat()); |
| 225 | |
| 226 | if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { |
| 227 | mAudioIsVorbis = true; |
| 228 | } else { |
| 229 | mAudioIsVorbis = false; |
| 230 | } |
| 231 | |
| 232 | mMimes.add(String8(mime)); |
| 233 | } |
| 234 | } else if (!strncasecmp(mime, "video/", 6)) { |
| 235 | if (mVideoTrack.mSource == NULL) { |
| 236 | mVideoTrack.mIndex = i; |
| 237 | mVideoTrack.mSource = track; |
| 238 | mVideoTrack.mPackets = |
| 239 | new AnotherPacketSource(mVideoTrack.mSource->getFormat()); |
| 240 | |
| 241 | // video always at the beginning |
| 242 | mMimes.insertAt(String8(mime), 0); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | mSources.push(track); |
| 247 | int64_t durationUs; |
| 248 | if (meta->findInt64(kKeyDuration, &durationUs)) { |
| 249 | if (durationUs > mDurationUs) { |
| 250 | mDurationUs = durationUs; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | int32_t bitrate; |
| 255 | if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) { |
| 256 | totalBitrate += bitrate; |
| 257 | } else { |
| 258 | totalBitrate = -1; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | ALOGV("initFromDataSource mSources.size(): %zu mIsSecure: %d mime[0]: %s", mSources.size(), |
| 263 | mIsSecure, (mMimes.isEmpty() ? "NONE" : mMimes[0].string())); |
| 264 | |
| 265 | if (mSources.size() == 0) { |
| 266 | ALOGE("b/23705695"); |
| 267 | return UNKNOWN_ERROR; |
| 268 | } |
| 269 | |
| 270 | // Modular DRM: The return value doesn't affect source initialization. |
| 271 | (void)checkDrmInfo(); |
| 272 | |
| 273 | mBitrate = totalBitrate; |
| 274 | |
| 275 | return OK; |
| 276 | } |
| 277 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 278 | status_t NuPlayer2::GenericSource2::getBufferingSettings( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 279 | BufferingSettings* buffering /* nonnull */) { |
| 280 | { |
| 281 | Mutex::Autolock _l(mLock); |
| 282 | *buffering = mBufferingSettings; |
| 283 | } |
| 284 | |
| 285 | ALOGV("getBufferingSettings{%s}", buffering->toString().string()); |
| 286 | return OK; |
| 287 | } |
| 288 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 289 | status_t NuPlayer2::GenericSource2::setBufferingSettings(const BufferingSettings& buffering) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 290 | ALOGV("setBufferingSettings{%s}", buffering.toString().string()); |
| 291 | |
| 292 | Mutex::Autolock _l(mLock); |
| 293 | mBufferingSettings = buffering; |
| 294 | return OK; |
| 295 | } |
| 296 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 297 | status_t NuPlayer2::GenericSource2::startSources() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 298 | // Start the selected A/V tracks now before we start buffering. |
| 299 | // Widevine sources might re-initialize crypto when starting, if we delay |
| 300 | // this to start(), all data buffered during prepare would be wasted. |
| 301 | // (We don't actually start reading until start().) |
| 302 | // |
| 303 | // TODO: this logic may no longer be relevant after the removal of widevine |
| 304 | // support |
| 305 | if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) { |
| 306 | ALOGE("failed to start audio track!"); |
| 307 | return UNKNOWN_ERROR; |
| 308 | } |
| 309 | |
| 310 | if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) { |
| 311 | ALOGE("failed to start video track!"); |
| 312 | return UNKNOWN_ERROR; |
| 313 | } |
| 314 | |
| 315 | return OK; |
| 316 | } |
| 317 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 318 | int64_t NuPlayer2::GenericSource2::getLastReadPosition() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 319 | if (mAudioTrack.mSource != NULL) { |
| 320 | return mAudioTimeUs; |
| 321 | } else if (mVideoTrack.mSource != NULL) { |
| 322 | return mVideoTimeUs; |
| 323 | } else { |
| 324 | return 0; |
| 325 | } |
| 326 | } |
| 327 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 328 | bool NuPlayer2::GenericSource2::isStreaming() const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 329 | Mutex::Autolock _l(mLock); |
| 330 | return mIsStreaming; |
| 331 | } |
| 332 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 333 | NuPlayer2::GenericSource2::~GenericSource2() { |
| 334 | ALOGV("~GenericSource2"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 335 | if (mLooper != NULL) { |
| 336 | mLooper->unregisterHandler(id()); |
| 337 | mLooper->stop(); |
| 338 | } |
Wei Jia | 1745933 | 2018-01-09 14:21:23 -0800 | [diff] [blame] | 339 | if (mDataSource != NULL) { |
| 340 | mDataSource->close(); |
| 341 | } |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 342 | resetDataSource(); |
| 343 | } |
| 344 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 345 | void NuPlayer2::GenericSource2::prepareAsync() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 346 | Mutex::Autolock _l(mLock); |
| 347 | ALOGV("prepareAsync: (looper: %d)", (mLooper != NULL)); |
| 348 | |
| 349 | if (mLooper == NULL) { |
| 350 | mLooper = new ALooper; |
| 351 | mLooper->setName("generic"); |
Wei Jia | c5c79da | 2017-12-21 18:03:05 -0800 | [diff] [blame] | 352 | mLooper->start(false, /* runOnCallingThread */ |
| 353 | true, /* canCallJava */ |
| 354 | PRIORITY_DEFAULT); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 355 | |
| 356 | mLooper->registerHandler(this); |
| 357 | } |
| 358 | |
| 359 | sp<AMessage> msg = new AMessage(kWhatPrepareAsync, this); |
| 360 | msg->post(); |
| 361 | } |
| 362 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 363 | void NuPlayer2::GenericSource2::onPrepareAsync() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 364 | ALOGV("onPrepareAsync: mDataSource: %d", (mDataSource != NULL)); |
| 365 | |
| 366 | // delayed data source creation |
| 367 | if (mDataSource == NULL) { |
| 368 | // set to false first, if the extractor |
| 369 | // comes back as secure, set it to true then. |
| 370 | mIsSecure = false; |
| 371 | |
| 372 | if (!mUri.empty()) { |
| 373 | const char* uri = mUri.c_str(); |
| 374 | String8 contentType; |
| 375 | |
| 376 | if (!strncasecmp("http://", uri, 7) || !strncasecmp("https://", uri, 8)) { |
| 377 | mHttpSource = DataSourceFactory::CreateMediaHTTP(mHTTPService); |
| 378 | if (mHttpSource == NULL) { |
| 379 | ALOGE("Failed to create http source!"); |
| 380 | notifyPreparedAndCleanup(UNKNOWN_ERROR); |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | mLock.unlock(); |
| 386 | // This might take long time if connection has some issue. |
| 387 | sp<DataSource> dataSource = DataSourceFactory::CreateFromURI( |
| 388 | mHTTPService, uri, &mUriHeaders, &contentType, |
| 389 | static_cast<HTTPBase *>(mHttpSource.get())); |
| 390 | mLock.lock(); |
| 391 | if (!mDisconnected) { |
| 392 | mDataSource = dataSource; |
| 393 | } |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 394 | } else { |
| 395 | if (property_get_bool("media.stagefright.extractremote", true) && |
| 396 | !FileSource::requiresDrm(mFd, mOffset, mLength, nullptr /* mime */)) { |
| 397 | sp<IBinder> binder = |
| 398 | defaultServiceManager()->getService(String16("media.extractor")); |
| 399 | if (binder != nullptr) { |
| 400 | ALOGD("FileSource remote"); |
| 401 | sp<IMediaExtractorService> mediaExService( |
| 402 | interface_cast<IMediaExtractorService>(binder)); |
| 403 | sp<IDataSource> source = |
| 404 | mediaExService->makeIDataSource(mFd, mOffset, mLength); |
| 405 | ALOGV("IDataSource(FileSource): %p %d %lld %lld", |
| 406 | source.get(), mFd, (long long)mOffset, (long long)mLength); |
| 407 | if (source.get() != nullptr) { |
| 408 | mDataSource = CreateDataSourceFromIDataSource(source); |
| 409 | if (mDataSource != nullptr) { |
| 410 | // Close the local file descriptor as it is not needed anymore. |
| 411 | close(mFd); |
| 412 | mFd = -1; |
| 413 | } |
| 414 | } else { |
| 415 | ALOGW("extractor service cannot make data source"); |
| 416 | } |
| 417 | } else { |
| 418 | ALOGW("extractor service not running"); |
| 419 | } |
| 420 | } |
| 421 | if (mDataSource == nullptr) { |
| 422 | ALOGD("FileSource local"); |
| 423 | mDataSource = new FileSource(mFd, mOffset, mLength); |
| 424 | } |
| 425 | // TODO: close should always be done on mFd, see the lines following |
| 426 | // CreateDataSourceFromIDataSource above, |
| 427 | // and the FileSource constructor should dup the mFd argument as needed. |
| 428 | mFd = -1; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 431 | if (mDataSource == NULL) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 432 | ALOGE("Failed to create data source!"); |
| 433 | notifyPreparedAndCleanup(UNKNOWN_ERROR); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
Dongwon Kang | 6969da6 | 2018-02-12 20:55:14 -0800 | [diff] [blame^] | 438 | if (mDataSource->flags() & DataSource::kIsCachingDataSource) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 439 | mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get()); |
| 440 | } |
| 441 | |
| 442 | // For cached streaming cases, we need to wait for enough |
| 443 | // buffering before reporting prepared. |
| 444 | mIsStreaming = (mCachedSource != NULL); |
| 445 | |
| 446 | // init extractor from data source |
| 447 | status_t err = initFromDataSource(); |
| 448 | |
| 449 | if (err != OK) { |
| 450 | ALOGE("Failed to init from data source!"); |
| 451 | notifyPreparedAndCleanup(err); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | if (mVideoTrack.mSource != NULL) { |
| 456 | sp<MetaData> meta = getFormatMeta_l(false /* audio */); |
| 457 | sp<AMessage> msg = new AMessage; |
| 458 | err = convertMetaDataToMessage(meta, &msg); |
| 459 | if(err != OK) { |
| 460 | notifyPreparedAndCleanup(err); |
| 461 | return; |
| 462 | } |
| 463 | notifyVideoSizeChanged(msg); |
| 464 | } |
| 465 | |
| 466 | notifyFlagsChanged( |
| 467 | // FLAG_SECURE will be known if/when prepareDrm is called by the app |
| 468 | // FLAG_PROTECTED will be known if/when prepareDrm is called by the app |
| 469 | FLAG_CAN_PAUSE | |
| 470 | FLAG_CAN_SEEK_BACKWARD | |
| 471 | FLAG_CAN_SEEK_FORWARD | |
| 472 | FLAG_CAN_SEEK); |
| 473 | |
| 474 | finishPrepareAsync(); |
| 475 | |
| 476 | ALOGV("onPrepareAsync: Done"); |
| 477 | } |
| 478 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 479 | void NuPlayer2::GenericSource2::finishPrepareAsync() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 480 | ALOGV("finishPrepareAsync"); |
| 481 | |
| 482 | status_t err = startSources(); |
| 483 | if (err != OK) { |
| 484 | ALOGE("Failed to init start data source!"); |
| 485 | notifyPreparedAndCleanup(err); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | if (mIsStreaming) { |
| 490 | mCachedSource->resumeFetchingIfNecessary(); |
| 491 | mPreparing = true; |
| 492 | schedulePollBuffering(); |
| 493 | } else { |
| 494 | notifyPrepared(); |
| 495 | } |
| 496 | |
| 497 | if (mAudioTrack.mSource != NULL) { |
| 498 | postReadBuffer(MEDIA_TRACK_TYPE_AUDIO); |
| 499 | } |
| 500 | |
| 501 | if (mVideoTrack.mSource != NULL) { |
| 502 | postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); |
| 503 | } |
| 504 | } |
| 505 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 506 | void NuPlayer2::GenericSource2::notifyPreparedAndCleanup(status_t err) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 507 | if (err != OK) { |
| 508 | mDataSource.clear(); |
| 509 | mCachedSource.clear(); |
| 510 | mHttpSource.clear(); |
| 511 | |
| 512 | mBitrate = -1; |
| 513 | mPrevBufferPercentage = -1; |
| 514 | ++mPollBufferingGeneration; |
| 515 | } |
| 516 | notifyPrepared(err); |
| 517 | } |
| 518 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 519 | void NuPlayer2::GenericSource2::start() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 520 | Mutex::Autolock _l(mLock); |
| 521 | ALOGI("start"); |
| 522 | |
| 523 | if (mAudioTrack.mSource != NULL) { |
| 524 | postReadBuffer(MEDIA_TRACK_TYPE_AUDIO); |
| 525 | } |
| 526 | |
| 527 | if (mVideoTrack.mSource != NULL) { |
| 528 | postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); |
| 529 | } |
| 530 | |
| 531 | mStarted = true; |
| 532 | } |
| 533 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 534 | void NuPlayer2::GenericSource2::stop() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 535 | Mutex::Autolock _l(mLock); |
| 536 | mStarted = false; |
| 537 | } |
| 538 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 539 | void NuPlayer2::GenericSource2::pause() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 540 | Mutex::Autolock _l(mLock); |
| 541 | mStarted = false; |
| 542 | } |
| 543 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 544 | void NuPlayer2::GenericSource2::resume() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 545 | Mutex::Autolock _l(mLock); |
| 546 | mStarted = true; |
| 547 | } |
| 548 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 549 | void NuPlayer2::GenericSource2::disconnect() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 550 | sp<DataSource> dataSource, httpSource; |
| 551 | { |
| 552 | Mutex::Autolock _l(mLock); |
| 553 | dataSource = mDataSource; |
| 554 | httpSource = mHttpSource; |
| 555 | mDisconnected = true; |
| 556 | } |
| 557 | |
| 558 | if (dataSource != NULL) { |
| 559 | // disconnect data source |
| 560 | if (dataSource->flags() & DataSource::kIsCachingDataSource) { |
| 561 | static_cast<NuCachedSource2 *>(dataSource.get())->disconnect(); |
| 562 | } |
| 563 | } else if (httpSource != NULL) { |
| 564 | static_cast<HTTPBase *>(httpSource.get())->disconnect(); |
| 565 | } |
| 566 | } |
| 567 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 568 | status_t NuPlayer2::GenericSource2::feedMoreTSData() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 569 | return OK; |
| 570 | } |
| 571 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 572 | void NuPlayer2::GenericSource2::sendCacheStats() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 573 | int32_t kbps = 0; |
| 574 | status_t err = UNKNOWN_ERROR; |
| 575 | |
| 576 | if (mCachedSource != NULL) { |
| 577 | err = mCachedSource->getEstimatedBandwidthKbps(&kbps); |
| 578 | } |
| 579 | |
| 580 | if (err == OK) { |
| 581 | sp<AMessage> notify = dupNotify(); |
| 582 | notify->setInt32("what", kWhatCacheStats); |
| 583 | notify->setInt32("bandwidth", kbps); |
| 584 | notify->post(); |
| 585 | } |
| 586 | } |
| 587 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 588 | void NuPlayer2::GenericSource2::onMessageReceived(const sp<AMessage> &msg) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 589 | Mutex::Autolock _l(mLock); |
| 590 | switch (msg->what()) { |
| 591 | case kWhatPrepareAsync: |
| 592 | { |
| 593 | onPrepareAsync(); |
| 594 | break; |
| 595 | } |
| 596 | case kWhatFetchSubtitleData: |
| 597 | { |
| 598 | fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, |
| 599 | mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | case kWhatFetchTimedTextData: |
| 604 | { |
| 605 | fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, |
| 606 | mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | case kWhatSendSubtitleData: |
| 611 | { |
| 612 | sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, |
| 613 | mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | case kWhatSendGlobalTimedTextData: |
| 618 | { |
| 619 | sendGlobalTextData(kWhatTimedTextData, mFetchTimedTextDataGeneration, msg); |
| 620 | break; |
| 621 | } |
| 622 | case kWhatSendTimedTextData: |
| 623 | { |
| 624 | sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, |
| 625 | mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | case kWhatChangeAVSource: |
| 630 | { |
| 631 | int32_t trackIndex; |
| 632 | CHECK(msg->findInt32("trackIndex", &trackIndex)); |
| 633 | const sp<IMediaSource> source = mSources.itemAt(trackIndex); |
| 634 | |
| 635 | Track* track; |
| 636 | const char *mime; |
| 637 | media_track_type trackType, counterpartType; |
| 638 | sp<MetaData> meta = source->getFormat(); |
| 639 | meta->findCString(kKeyMIMEType, &mime); |
| 640 | if (!strncasecmp(mime, "audio/", 6)) { |
| 641 | track = &mAudioTrack; |
| 642 | trackType = MEDIA_TRACK_TYPE_AUDIO; |
| 643 | counterpartType = MEDIA_TRACK_TYPE_VIDEO;; |
| 644 | } else { |
| 645 | CHECK(!strncasecmp(mime, "video/", 6)); |
| 646 | track = &mVideoTrack; |
| 647 | trackType = MEDIA_TRACK_TYPE_VIDEO; |
| 648 | counterpartType = MEDIA_TRACK_TYPE_AUDIO;; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | if (track->mSource != NULL) { |
| 653 | track->mSource->stop(); |
| 654 | } |
| 655 | track->mSource = source; |
| 656 | track->mSource->start(); |
| 657 | track->mIndex = trackIndex; |
| 658 | ++mAudioDataGeneration; |
| 659 | ++mVideoDataGeneration; |
| 660 | |
| 661 | int64_t timeUs, actualTimeUs; |
| 662 | const bool formatChange = true; |
| 663 | if (trackType == MEDIA_TRACK_TYPE_AUDIO) { |
| 664 | timeUs = mAudioLastDequeueTimeUs; |
| 665 | } else { |
| 666 | timeUs = mVideoLastDequeueTimeUs; |
| 667 | } |
| 668 | readBuffer(trackType, timeUs, MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */, |
| 669 | &actualTimeUs, formatChange); |
| 670 | readBuffer(counterpartType, -1, MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */, |
| 671 | NULL, !formatChange); |
| 672 | ALOGV("timeUs %lld actualTimeUs %lld", (long long)timeUs, (long long)actualTimeUs); |
| 673 | |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | case kWhatSeek: |
| 678 | { |
| 679 | onSeek(msg); |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | case kWhatReadBuffer: |
| 684 | { |
| 685 | onReadBuffer(msg); |
| 686 | break; |
| 687 | } |
| 688 | |
| 689 | case kWhatPollBuffering: |
| 690 | { |
| 691 | int32_t generation; |
| 692 | CHECK(msg->findInt32("generation", &generation)); |
| 693 | if (generation == mPollBufferingGeneration) { |
| 694 | onPollBuffering(); |
| 695 | } |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | default: |
| 700 | Source::onMessageReceived(msg); |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 705 | void NuPlayer2::GenericSource2::fetchTextData( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 706 | uint32_t sendWhat, |
| 707 | media_track_type type, |
| 708 | int32_t curGen, |
| 709 | const sp<AnotherPacketSource>& packets, |
| 710 | const sp<AMessage>& msg) { |
| 711 | int32_t msgGeneration; |
| 712 | CHECK(msg->findInt32("generation", &msgGeneration)); |
| 713 | if (msgGeneration != curGen) { |
| 714 | // stale |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | int32_t avail; |
| 719 | if (packets->hasBufferAvailable(&avail)) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | int64_t timeUs; |
| 724 | CHECK(msg->findInt64("timeUs", &timeUs)); |
| 725 | |
| 726 | int64_t subTimeUs = 0; |
| 727 | readBuffer(type, timeUs, MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */, &subTimeUs); |
| 728 | |
| 729 | status_t eosResult; |
| 730 | if (!packets->hasBufferAvailable(&eosResult)) { |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | if (msg->what() == kWhatFetchSubtitleData) { |
| 735 | subTimeUs -= 1000000ll; // send subtile data one second earlier |
| 736 | } |
| 737 | sp<AMessage> msg2 = new AMessage(sendWhat, this); |
| 738 | msg2->setInt32("generation", msgGeneration); |
| 739 | mMediaClock->addTimer(msg2, subTimeUs); |
| 740 | } |
| 741 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 742 | void NuPlayer2::GenericSource2::sendTextData( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 743 | uint32_t what, |
| 744 | media_track_type type, |
| 745 | int32_t curGen, |
| 746 | const sp<AnotherPacketSource>& packets, |
| 747 | const sp<AMessage>& msg) { |
| 748 | int32_t msgGeneration; |
| 749 | CHECK(msg->findInt32("generation", &msgGeneration)); |
| 750 | if (msgGeneration != curGen) { |
| 751 | // stale |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | int64_t subTimeUs; |
| 756 | if (packets->nextBufferTime(&subTimeUs) != OK) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | int64_t nextSubTimeUs; |
| 761 | readBuffer(type, -1, MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */, &nextSubTimeUs); |
| 762 | |
| 763 | sp<ABuffer> buffer; |
| 764 | status_t dequeueStatus = packets->dequeueAccessUnit(&buffer); |
| 765 | if (dequeueStatus == OK) { |
| 766 | sp<AMessage> notify = dupNotify(); |
| 767 | notify->setInt32("what", what); |
| 768 | notify->setBuffer("buffer", buffer); |
| 769 | notify->post(); |
| 770 | |
| 771 | if (msg->what() == kWhatSendSubtitleData) { |
| 772 | nextSubTimeUs -= 1000000ll; // send subtile data one second earlier |
| 773 | } |
| 774 | mMediaClock->addTimer(msg, nextSubTimeUs); |
| 775 | } |
| 776 | } |
| 777 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 778 | void NuPlayer2::GenericSource2::sendGlobalTextData( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 779 | uint32_t what, |
| 780 | int32_t curGen, |
| 781 | sp<AMessage> msg) { |
| 782 | int32_t msgGeneration; |
| 783 | CHECK(msg->findInt32("generation", &msgGeneration)); |
| 784 | if (msgGeneration != curGen) { |
| 785 | // stale |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | uint32_t textType; |
| 790 | const void *data; |
| 791 | size_t size = 0; |
| 792 | if (mTimedTextTrack.mSource->getFormat()->findData( |
| 793 | kKeyTextFormatData, &textType, &data, &size)) { |
| 794 | mGlobalTimedText = new ABuffer(size); |
| 795 | if (mGlobalTimedText->data()) { |
| 796 | memcpy(mGlobalTimedText->data(), data, size); |
| 797 | sp<AMessage> globalMeta = mGlobalTimedText->meta(); |
| 798 | globalMeta->setInt64("timeUs", 0); |
| 799 | globalMeta->setString("mime", MEDIA_MIMETYPE_TEXT_3GPP); |
| 800 | globalMeta->setInt32("global", 1); |
| 801 | sp<AMessage> notify = dupNotify(); |
| 802 | notify->setInt32("what", what); |
| 803 | notify->setBuffer("buffer", mGlobalTimedText); |
| 804 | notify->post(); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 809 | sp<MetaData> NuPlayer2::GenericSource2::getFormatMeta(bool audio) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 810 | Mutex::Autolock _l(mLock); |
| 811 | return getFormatMeta_l(audio); |
| 812 | } |
| 813 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 814 | sp<MetaData> NuPlayer2::GenericSource2::getFormatMeta_l(bool audio) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 815 | sp<IMediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource; |
| 816 | |
| 817 | if (source == NULL) { |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | return source->getFormat(); |
| 822 | } |
| 823 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 824 | status_t NuPlayer2::GenericSource2::dequeueAccessUnit( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 825 | bool audio, sp<ABuffer> *accessUnit) { |
| 826 | Mutex::Autolock _l(mLock); |
| 827 | // If has gone through stop/releaseDrm sequence, we no longer send down any buffer b/c |
| 828 | // the codec's crypto object has gone away (b/37960096). |
| 829 | // Note: This will be unnecessary when stop() changes behavior and releases codec (b/35248283). |
| 830 | if (!mStarted && mIsDrmReleased) { |
| 831 | return -EWOULDBLOCK; |
| 832 | } |
| 833 | |
| 834 | Track *track = audio ? &mAudioTrack : &mVideoTrack; |
| 835 | |
| 836 | if (track->mSource == NULL) { |
| 837 | return -EWOULDBLOCK; |
| 838 | } |
| 839 | |
| 840 | status_t finalResult; |
| 841 | if (!track->mPackets->hasBufferAvailable(&finalResult)) { |
| 842 | if (finalResult == OK) { |
| 843 | postReadBuffer( |
| 844 | audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); |
| 845 | return -EWOULDBLOCK; |
| 846 | } |
| 847 | return finalResult; |
| 848 | } |
| 849 | |
| 850 | status_t result = track->mPackets->dequeueAccessUnit(accessUnit); |
| 851 | |
| 852 | // start pulling in more buffers if cache is running low |
| 853 | // so that decoder has less chance of being starved |
| 854 | if (!mIsStreaming) { |
| 855 | if (track->mPackets->getAvailableBufferCount(&finalResult) < 2) { |
| 856 | postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); |
| 857 | } |
| 858 | } else { |
| 859 | int64_t durationUs = track->mPackets->getBufferedDurationUs(&finalResult); |
| 860 | // TODO: maxRebufferingMarkMs could be larger than |
| 861 | // mBufferingSettings.mResumePlaybackMarkMs |
| 862 | int64_t restartBufferingMarkUs = |
| 863 | mBufferingSettings.mResumePlaybackMarkMs * 1000ll / 2; |
| 864 | if (finalResult == OK) { |
| 865 | if (durationUs < restartBufferingMarkUs) { |
| 866 | postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); |
| 867 | } |
| 868 | if (track->mPackets->getAvailableBufferCount(&finalResult) < 2 |
| 869 | && !mSentPauseOnBuffering && !mPreparing) { |
| 870 | mCachedSource->resumeFetchingIfNecessary(); |
| 871 | sendCacheStats(); |
| 872 | mSentPauseOnBuffering = true; |
| 873 | sp<AMessage> notify = dupNotify(); |
| 874 | notify->setInt32("what", kWhatPauseOnBufferingStart); |
| 875 | notify->post(); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | if (result != OK) { |
| 881 | if (mSubtitleTrack.mSource != NULL) { |
| 882 | mSubtitleTrack.mPackets->clear(); |
| 883 | mFetchSubtitleDataGeneration++; |
| 884 | } |
| 885 | if (mTimedTextTrack.mSource != NULL) { |
| 886 | mTimedTextTrack.mPackets->clear(); |
| 887 | mFetchTimedTextDataGeneration++; |
| 888 | } |
| 889 | return result; |
| 890 | } |
| 891 | |
| 892 | int64_t timeUs; |
| 893 | status_t eosResult; // ignored |
| 894 | CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); |
| 895 | if (audio) { |
| 896 | mAudioLastDequeueTimeUs = timeUs; |
| 897 | } else { |
| 898 | mVideoLastDequeueTimeUs = timeUs; |
| 899 | } |
| 900 | |
| 901 | if (mSubtitleTrack.mSource != NULL |
| 902 | && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 903 | sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this); |
| 904 | msg->setInt64("timeUs", timeUs); |
| 905 | msg->setInt32("generation", mFetchSubtitleDataGeneration); |
| 906 | msg->post(); |
| 907 | } |
| 908 | |
| 909 | if (mTimedTextTrack.mSource != NULL |
| 910 | && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 911 | sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this); |
| 912 | msg->setInt64("timeUs", timeUs); |
| 913 | msg->setInt32("generation", mFetchTimedTextDataGeneration); |
| 914 | msg->post(); |
| 915 | } |
| 916 | |
| 917 | return result; |
| 918 | } |
| 919 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 920 | status_t NuPlayer2::GenericSource2::getDuration(int64_t *durationUs) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 921 | Mutex::Autolock _l(mLock); |
| 922 | *durationUs = mDurationUs; |
| 923 | return OK; |
| 924 | } |
| 925 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 926 | size_t NuPlayer2::GenericSource2::getTrackCount() const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 927 | Mutex::Autolock _l(mLock); |
| 928 | return mSources.size(); |
| 929 | } |
| 930 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 931 | sp<AMessage> NuPlayer2::GenericSource2::getTrackInfo(size_t trackIndex) const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 932 | Mutex::Autolock _l(mLock); |
| 933 | size_t trackCount = mSources.size(); |
| 934 | if (trackIndex >= trackCount) { |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | sp<AMessage> format = new AMessage(); |
| 939 | sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat(); |
| 940 | if (meta == NULL) { |
| 941 | ALOGE("no metadata for track %zu", trackIndex); |
| 942 | return NULL; |
| 943 | } |
| 944 | |
| 945 | const char *mime; |
| 946 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 947 | format->setString("mime", mime); |
| 948 | |
| 949 | int32_t trackType; |
| 950 | if (!strncasecmp(mime, "video/", 6)) { |
| 951 | trackType = MEDIA_TRACK_TYPE_VIDEO; |
| 952 | } else if (!strncasecmp(mime, "audio/", 6)) { |
| 953 | trackType = MEDIA_TRACK_TYPE_AUDIO; |
| 954 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) { |
| 955 | trackType = MEDIA_TRACK_TYPE_TIMEDTEXT; |
| 956 | } else { |
| 957 | trackType = MEDIA_TRACK_TYPE_UNKNOWN; |
| 958 | } |
| 959 | format->setInt32("type", trackType); |
| 960 | |
| 961 | const char *lang; |
| 962 | if (!meta->findCString(kKeyMediaLanguage, &lang)) { |
| 963 | lang = "und"; |
| 964 | } |
| 965 | format->setString("language", lang); |
| 966 | |
| 967 | if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
| 968 | int32_t isAutoselect = 1, isDefault = 0, isForced = 0; |
| 969 | meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect); |
| 970 | meta->findInt32(kKeyTrackIsDefault, &isDefault); |
| 971 | meta->findInt32(kKeyTrackIsForced, &isForced); |
| 972 | |
| 973 | format->setInt32("auto", !!isAutoselect); |
| 974 | format->setInt32("default", !!isDefault); |
| 975 | format->setInt32("forced", !!isForced); |
| 976 | } |
| 977 | |
| 978 | return format; |
| 979 | } |
| 980 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 981 | ssize_t NuPlayer2::GenericSource2::getSelectedTrack(media_track_type type) const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 982 | Mutex::Autolock _l(mLock); |
| 983 | const Track *track = NULL; |
| 984 | switch (type) { |
| 985 | case MEDIA_TRACK_TYPE_VIDEO: |
| 986 | track = &mVideoTrack; |
| 987 | break; |
| 988 | case MEDIA_TRACK_TYPE_AUDIO: |
| 989 | track = &mAudioTrack; |
| 990 | break; |
| 991 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 992 | track = &mTimedTextTrack; |
| 993 | break; |
| 994 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 995 | track = &mSubtitleTrack; |
| 996 | break; |
| 997 | default: |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | if (track != NULL && track->mSource != NULL) { |
| 1002 | return track->mIndex; |
| 1003 | } |
| 1004 | |
| 1005 | return -1; |
| 1006 | } |
| 1007 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1008 | status_t NuPlayer2::GenericSource2::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1009 | Mutex::Autolock _l(mLock); |
| 1010 | ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex); |
| 1011 | |
| 1012 | if (trackIndex >= mSources.size()) { |
| 1013 | return BAD_INDEX; |
| 1014 | } |
| 1015 | |
| 1016 | if (!select) { |
| 1017 | Track* track = NULL; |
| 1018 | if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) { |
| 1019 | track = &mSubtitleTrack; |
| 1020 | mFetchSubtitleDataGeneration++; |
| 1021 | } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) { |
| 1022 | track = &mTimedTextTrack; |
| 1023 | mFetchTimedTextDataGeneration++; |
| 1024 | } |
| 1025 | if (track == NULL) { |
| 1026 | return INVALID_OPERATION; |
| 1027 | } |
| 1028 | track->mSource->stop(); |
| 1029 | track->mSource = NULL; |
| 1030 | track->mPackets->clear(); |
| 1031 | return OK; |
| 1032 | } |
| 1033 | |
| 1034 | const sp<IMediaSource> source = mSources.itemAt(trackIndex); |
| 1035 | sp<MetaData> meta = source->getFormat(); |
| 1036 | const char *mime; |
| 1037 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 1038 | if (!strncasecmp(mime, "text/", 5)) { |
| 1039 | bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP); |
| 1040 | Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack; |
| 1041 | if (track->mSource != NULL && track->mIndex == trackIndex) { |
| 1042 | return OK; |
| 1043 | } |
| 1044 | track->mIndex = trackIndex; |
| 1045 | if (track->mSource != NULL) { |
| 1046 | track->mSource->stop(); |
| 1047 | } |
| 1048 | track->mSource = mSources.itemAt(trackIndex); |
| 1049 | track->mSource->start(); |
| 1050 | if (track->mPackets == NULL) { |
| 1051 | track->mPackets = new AnotherPacketSource(track->mSource->getFormat()); |
| 1052 | } else { |
| 1053 | track->mPackets->clear(); |
| 1054 | track->mPackets->setFormat(track->mSource->getFormat()); |
| 1055 | |
| 1056 | } |
| 1057 | |
| 1058 | if (isSubtitle) { |
| 1059 | mFetchSubtitleDataGeneration++; |
| 1060 | } else { |
| 1061 | mFetchTimedTextDataGeneration++; |
| 1062 | } |
| 1063 | |
| 1064 | status_t eosResult; // ignored |
| 1065 | if (mSubtitleTrack.mSource != NULL |
| 1066 | && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 1067 | sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, this); |
| 1068 | msg->setInt64("timeUs", timeUs); |
| 1069 | msg->setInt32("generation", mFetchSubtitleDataGeneration); |
| 1070 | msg->post(); |
| 1071 | } |
| 1072 | |
| 1073 | sp<AMessage> msg2 = new AMessage(kWhatSendGlobalTimedTextData, this); |
| 1074 | msg2->setInt32("generation", mFetchTimedTextDataGeneration); |
| 1075 | msg2->post(); |
| 1076 | |
| 1077 | if (mTimedTextTrack.mSource != NULL |
| 1078 | && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 1079 | sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, this); |
| 1080 | msg->setInt64("timeUs", timeUs); |
| 1081 | msg->setInt32("generation", mFetchTimedTextDataGeneration); |
| 1082 | msg->post(); |
| 1083 | } |
| 1084 | |
| 1085 | return OK; |
| 1086 | } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) { |
| 1087 | bool audio = !strncasecmp(mime, "audio/", 6); |
| 1088 | Track *track = audio ? &mAudioTrack : &mVideoTrack; |
| 1089 | if (track->mSource != NULL && track->mIndex == trackIndex) { |
| 1090 | return OK; |
| 1091 | } |
| 1092 | |
| 1093 | sp<AMessage> msg = new AMessage(kWhatChangeAVSource, this); |
| 1094 | msg->setInt32("trackIndex", trackIndex); |
| 1095 | msg->post(); |
| 1096 | return OK; |
| 1097 | } |
| 1098 | |
| 1099 | return INVALID_OPERATION; |
| 1100 | } |
| 1101 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1102 | status_t NuPlayer2::GenericSource2::seekTo(int64_t seekTimeUs, MediaPlayer2SeekMode mode) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1103 | ALOGV("seekTo: %lld, %d", (long long)seekTimeUs, mode); |
| 1104 | sp<AMessage> msg = new AMessage(kWhatSeek, this); |
| 1105 | msg->setInt64("seekTimeUs", seekTimeUs); |
| 1106 | msg->setInt32("mode", mode); |
| 1107 | |
| 1108 | // Need to call readBuffer on |mLooper| to ensure the calls to |
| 1109 | // IMediaSource::read* are serialized. Note that IMediaSource::read* |
| 1110 | // is called without |mLock| acquired and MediaSource is not thread safe. |
| 1111 | sp<AMessage> response; |
| 1112 | status_t err = msg->postAndAwaitResponse(&response); |
| 1113 | if (err == OK && response != NULL) { |
| 1114 | CHECK(response->findInt32("err", &err)); |
| 1115 | } |
| 1116 | |
| 1117 | return err; |
| 1118 | } |
| 1119 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1120 | void NuPlayer2::GenericSource2::onSeek(const sp<AMessage>& msg) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1121 | int64_t seekTimeUs; |
| 1122 | int32_t mode; |
| 1123 | CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); |
| 1124 | CHECK(msg->findInt32("mode", &mode)); |
| 1125 | |
| 1126 | sp<AMessage> response = new AMessage; |
| 1127 | status_t err = doSeek(seekTimeUs, (MediaPlayer2SeekMode)mode); |
| 1128 | response->setInt32("err", err); |
| 1129 | |
| 1130 | sp<AReplyToken> replyID; |
| 1131 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 1132 | response->postReply(replyID); |
| 1133 | } |
| 1134 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1135 | status_t NuPlayer2::GenericSource2::doSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1136 | if (mVideoTrack.mSource != NULL) { |
| 1137 | ++mVideoDataGeneration; |
| 1138 | |
| 1139 | int64_t actualTimeUs; |
| 1140 | readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, mode, &actualTimeUs); |
| 1141 | |
| 1142 | if (mode != MediaPlayer2SeekMode::SEEK_CLOSEST) { |
| 1143 | seekTimeUs = actualTimeUs; |
| 1144 | } |
| 1145 | mVideoLastDequeueTimeUs = actualTimeUs; |
| 1146 | } |
| 1147 | |
| 1148 | if (mAudioTrack.mSource != NULL) { |
| 1149 | ++mAudioDataGeneration; |
| 1150 | readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs, MediaPlayer2SeekMode::SEEK_CLOSEST); |
| 1151 | mAudioLastDequeueTimeUs = seekTimeUs; |
| 1152 | } |
| 1153 | |
| 1154 | if (mSubtitleTrack.mSource != NULL) { |
| 1155 | mSubtitleTrack.mPackets->clear(); |
| 1156 | mFetchSubtitleDataGeneration++; |
| 1157 | } |
| 1158 | |
| 1159 | if (mTimedTextTrack.mSource != NULL) { |
| 1160 | mTimedTextTrack.mPackets->clear(); |
| 1161 | mFetchTimedTextDataGeneration++; |
| 1162 | } |
| 1163 | |
| 1164 | ++mPollBufferingGeneration; |
| 1165 | schedulePollBuffering(); |
| 1166 | return OK; |
| 1167 | } |
| 1168 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1169 | sp<ABuffer> NuPlayer2::GenericSource2::mediaBufferToABuffer( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1170 | MediaBuffer* mb, |
| 1171 | media_track_type trackType) { |
| 1172 | bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO; |
| 1173 | size_t outLength = mb->range_length(); |
| 1174 | |
| 1175 | if (audio && mAudioIsVorbis) { |
| 1176 | outLength += sizeof(int32_t); |
| 1177 | } |
| 1178 | |
| 1179 | sp<ABuffer> ab; |
| 1180 | |
| 1181 | if (mIsDrmProtected) { |
| 1182 | // Modular DRM |
| 1183 | // Enabled for both video/audio so 1) media buffer is reused without extra copying |
| 1184 | // 2) meta data can be retrieved in onInputBufferFetched for calling queueSecureInputBuffer. |
| 1185 | |
| 1186 | // data is already provided in the buffer |
| 1187 | ab = new ABuffer(NULL, mb->range_length()); |
Dongwon Kang | bc8f53b | 2018-01-25 17:01:44 -0800 | [diff] [blame] | 1188 | ab->meta()->setObject("mediaBufferHolder", new MediaBufferHolder(mb)); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1189 | |
| 1190 | // Modular DRM: Required b/c of the above add_ref. |
| 1191 | // If ref>0, there must be an observer, or it'll crash at release(). |
| 1192 | // TODO: MediaBuffer might need to be revised to ease such need. |
| 1193 | mb->setObserver(this); |
| 1194 | // setMediaBufferBase() interestingly doesn't increment the ref count on its own. |
| 1195 | // Extra increment (since we want to keep mb alive and attached to ab beyond this function |
| 1196 | // call. This is to counter the effect of mb->release() towards the end. |
| 1197 | mb->add_ref(); |
| 1198 | |
| 1199 | } else { |
| 1200 | ab = new ABuffer(outLength); |
| 1201 | memcpy(ab->data(), |
| 1202 | (const uint8_t *)mb->data() + mb->range_offset(), |
| 1203 | mb->range_length()); |
| 1204 | } |
| 1205 | |
| 1206 | if (audio && mAudioIsVorbis) { |
| 1207 | int32_t numPageSamples; |
| 1208 | if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) { |
| 1209 | numPageSamples = -1; |
| 1210 | } |
| 1211 | |
| 1212 | uint8_t* abEnd = ab->data() + mb->range_length(); |
| 1213 | memcpy(abEnd, &numPageSamples, sizeof(numPageSamples)); |
| 1214 | } |
| 1215 | |
| 1216 | sp<AMessage> meta = ab->meta(); |
| 1217 | |
| 1218 | int64_t timeUs; |
| 1219 | CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 1220 | meta->setInt64("timeUs", timeUs); |
| 1221 | |
| 1222 | if (trackType == MEDIA_TRACK_TYPE_VIDEO) { |
| 1223 | int32_t layerId; |
| 1224 | if (mb->meta_data()->findInt32(kKeyTemporalLayerId, &layerId)) { |
| 1225 | meta->setInt32("temporal-layer-id", layerId); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) { |
| 1230 | const char *mime; |
| 1231 | CHECK(mTimedTextTrack.mSource != NULL |
| 1232 | && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime)); |
| 1233 | meta->setString("mime", mime); |
| 1234 | } |
| 1235 | |
| 1236 | int64_t durationUs; |
| 1237 | if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) { |
| 1238 | meta->setInt64("durationUs", durationUs); |
| 1239 | } |
| 1240 | |
| 1241 | if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
| 1242 | meta->setInt32("trackIndex", mSubtitleTrack.mIndex); |
| 1243 | } |
| 1244 | |
| 1245 | uint32_t dataType; // unused |
| 1246 | const void *seiData; |
| 1247 | size_t seiLength; |
| 1248 | if (mb->meta_data()->findData(kKeySEI, &dataType, &seiData, &seiLength)) { |
| 1249 | sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);; |
| 1250 | meta->setBuffer("sei", sei); |
| 1251 | } |
| 1252 | |
| 1253 | const void *mpegUserDataPointer; |
| 1254 | size_t mpegUserDataLength; |
| 1255 | if (mb->meta_data()->findData( |
| 1256 | kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) { |
| 1257 | sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength); |
| 1258 | meta->setBuffer("mpegUserData", mpegUserData); |
| 1259 | } |
| 1260 | |
| 1261 | mb->release(); |
| 1262 | mb = NULL; |
| 1263 | |
| 1264 | return ab; |
| 1265 | } |
| 1266 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1267 | int32_t NuPlayer2::GenericSource2::getDataGeneration(media_track_type type) const { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1268 | int32_t generation = -1; |
| 1269 | switch (type) { |
| 1270 | case MEDIA_TRACK_TYPE_VIDEO: |
| 1271 | generation = mVideoDataGeneration; |
| 1272 | break; |
| 1273 | case MEDIA_TRACK_TYPE_AUDIO: |
| 1274 | generation = mAudioDataGeneration; |
| 1275 | break; |
| 1276 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 1277 | generation = mFetchTimedTextDataGeneration; |
| 1278 | break; |
| 1279 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 1280 | generation = mFetchSubtitleDataGeneration; |
| 1281 | break; |
| 1282 | default: |
| 1283 | break; |
| 1284 | } |
| 1285 | |
| 1286 | return generation; |
| 1287 | } |
| 1288 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1289 | void NuPlayer2::GenericSource2::postReadBuffer(media_track_type trackType) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1290 | if ((mPendingReadBufferTypes & (1 << trackType)) == 0) { |
| 1291 | mPendingReadBufferTypes |= (1 << trackType); |
| 1292 | sp<AMessage> msg = new AMessage(kWhatReadBuffer, this); |
| 1293 | msg->setInt32("trackType", trackType); |
| 1294 | msg->post(); |
| 1295 | } |
| 1296 | } |
| 1297 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1298 | void NuPlayer2::GenericSource2::onReadBuffer(const sp<AMessage>& msg) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1299 | int32_t tmpType; |
| 1300 | CHECK(msg->findInt32("trackType", &tmpType)); |
| 1301 | media_track_type trackType = (media_track_type)tmpType; |
| 1302 | mPendingReadBufferTypes &= ~(1 << trackType); |
| 1303 | readBuffer(trackType); |
| 1304 | } |
| 1305 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1306 | void NuPlayer2::GenericSource2::readBuffer( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1307 | media_track_type trackType, int64_t seekTimeUs, MediaPlayer2SeekMode mode, |
| 1308 | int64_t *actualTimeUs, bool formatChange) { |
| 1309 | Track *track; |
| 1310 | size_t maxBuffers = 1; |
| 1311 | switch (trackType) { |
| 1312 | case MEDIA_TRACK_TYPE_VIDEO: |
| 1313 | track = &mVideoTrack; |
| 1314 | maxBuffers = 8; // too large of a number may influence seeks |
| 1315 | break; |
| 1316 | case MEDIA_TRACK_TYPE_AUDIO: |
| 1317 | track = &mAudioTrack; |
| 1318 | maxBuffers = 64; |
| 1319 | break; |
| 1320 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 1321 | track = &mSubtitleTrack; |
| 1322 | break; |
| 1323 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 1324 | track = &mTimedTextTrack; |
| 1325 | break; |
| 1326 | default: |
| 1327 | TRESPASS(); |
| 1328 | } |
| 1329 | |
| 1330 | if (track->mSource == NULL) { |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | if (actualTimeUs) { |
| 1335 | *actualTimeUs = seekTimeUs; |
| 1336 | } |
| 1337 | |
| 1338 | MediaSource::ReadOptions options; |
| 1339 | |
| 1340 | bool seeking = false; |
| 1341 | if (seekTimeUs >= 0) { |
| 1342 | options.setSeekTo(seekTimeUs, mode); |
| 1343 | seeking = true; |
| 1344 | } |
| 1345 | |
| 1346 | const bool couldReadMultiple = (track->mSource->supportReadMultiple()); |
| 1347 | |
| 1348 | if (couldReadMultiple) { |
| 1349 | options.setNonBlocking(); |
| 1350 | } |
| 1351 | |
| 1352 | int32_t generation = getDataGeneration(trackType); |
| 1353 | for (size_t numBuffers = 0; numBuffers < maxBuffers; ) { |
| 1354 | Vector<MediaBuffer *> mediaBuffers; |
| 1355 | status_t err = NO_ERROR; |
| 1356 | |
| 1357 | sp<IMediaSource> source = track->mSource; |
| 1358 | mLock.unlock(); |
| 1359 | if (couldReadMultiple) { |
| 1360 | err = source->readMultiple( |
| 1361 | &mediaBuffers, maxBuffers - numBuffers, &options); |
| 1362 | } else { |
| 1363 | MediaBuffer *mbuf = NULL; |
| 1364 | err = source->read(&mbuf, &options); |
| 1365 | if (err == OK && mbuf != NULL) { |
| 1366 | mediaBuffers.push_back(mbuf); |
| 1367 | } |
| 1368 | } |
| 1369 | mLock.lock(); |
| 1370 | |
| 1371 | options.clearNonPersistent(); |
| 1372 | |
| 1373 | size_t id = 0; |
| 1374 | size_t count = mediaBuffers.size(); |
| 1375 | |
| 1376 | // in case track has been changed since we don't have lock for some time. |
| 1377 | if (generation != getDataGeneration(trackType)) { |
| 1378 | for (; id < count; ++id) { |
| 1379 | mediaBuffers[id]->release(); |
| 1380 | } |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | for (; id < count; ++id) { |
| 1385 | int64_t timeUs; |
| 1386 | MediaBuffer *mbuf = mediaBuffers[id]; |
| 1387 | if (!mbuf->meta_data()->findInt64(kKeyTime, &timeUs)) { |
| 1388 | mbuf->meta_data()->dumpToLog(); |
| 1389 | track->mPackets->signalEOS(ERROR_MALFORMED); |
| 1390 | break; |
| 1391 | } |
| 1392 | if (trackType == MEDIA_TRACK_TYPE_AUDIO) { |
| 1393 | mAudioTimeUs = timeUs; |
| 1394 | } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { |
| 1395 | mVideoTimeUs = timeUs; |
| 1396 | } |
| 1397 | |
| 1398 | queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track); |
| 1399 | |
| 1400 | sp<ABuffer> buffer = mediaBufferToABuffer(mbuf, trackType); |
| 1401 | if (numBuffers == 0 && actualTimeUs != nullptr) { |
| 1402 | *actualTimeUs = timeUs; |
| 1403 | } |
| 1404 | if (seeking && buffer != nullptr) { |
| 1405 | sp<AMessage> meta = buffer->meta(); |
| 1406 | if (meta != nullptr && mode == MediaPlayer2SeekMode::SEEK_CLOSEST |
| 1407 | && seekTimeUs > timeUs) { |
| 1408 | sp<AMessage> extra = new AMessage; |
| 1409 | extra->setInt64("resume-at-mediaTimeUs", seekTimeUs); |
| 1410 | meta->setMessage("extra", extra); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | track->mPackets->queueAccessUnit(buffer); |
| 1415 | formatChange = false; |
| 1416 | seeking = false; |
| 1417 | ++numBuffers; |
| 1418 | } |
| 1419 | if (id < count) { |
| 1420 | // Error, some mediaBuffer doesn't have kKeyTime. |
| 1421 | for (; id < count; ++id) { |
| 1422 | mediaBuffers[id]->release(); |
| 1423 | } |
| 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | if (err == WOULD_BLOCK) { |
| 1428 | break; |
| 1429 | } else if (err == INFO_FORMAT_CHANGED) { |
| 1430 | #if 0 |
| 1431 | track->mPackets->queueDiscontinuity( |
| 1432 | ATSParser::DISCONTINUITY_FORMATCHANGE, |
| 1433 | NULL, |
| 1434 | false /* discard */); |
| 1435 | #endif |
| 1436 | } else if (err != OK) { |
| 1437 | queueDiscontinuityIfNeeded(seeking, formatChange, trackType, track); |
| 1438 | track->mPackets->signalEOS(err); |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | if (mIsStreaming |
| 1444 | && (trackType == MEDIA_TRACK_TYPE_VIDEO || trackType == MEDIA_TRACK_TYPE_AUDIO)) { |
| 1445 | status_t finalResult; |
| 1446 | int64_t durationUs = track->mPackets->getBufferedDurationUs(&finalResult); |
| 1447 | |
| 1448 | // TODO: maxRebufferingMarkMs could be larger than |
| 1449 | // mBufferingSettings.mResumePlaybackMarkMs |
| 1450 | int64_t markUs = (mPreparing ? mBufferingSettings.mInitialMarkMs |
| 1451 | : mBufferingSettings.mResumePlaybackMarkMs) * 1000ll; |
| 1452 | if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) { |
| 1453 | if (mPreparing || mSentPauseOnBuffering) { |
| 1454 | Track *counterTrack = |
| 1455 | (trackType == MEDIA_TRACK_TYPE_VIDEO ? &mAudioTrack : &mVideoTrack); |
| 1456 | if (counterTrack->mSource != NULL) { |
| 1457 | durationUs = counterTrack->mPackets->getBufferedDurationUs(&finalResult); |
| 1458 | } |
| 1459 | if (finalResult == ERROR_END_OF_STREAM || durationUs >= markUs) { |
| 1460 | if (mPreparing) { |
| 1461 | notifyPrepared(); |
| 1462 | mPreparing = false; |
| 1463 | } else { |
| 1464 | sendCacheStats(); |
| 1465 | mSentPauseOnBuffering = false; |
| 1466 | sp<AMessage> notify = dupNotify(); |
| 1467 | notify->setInt32("what", kWhatResumeOnBufferingEnd); |
| 1468 | notify->post(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | return; |
| 1473 | } |
| 1474 | |
| 1475 | postReadBuffer(trackType); |
| 1476 | } |
| 1477 | } |
| 1478 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1479 | void NuPlayer2::GenericSource2::queueDiscontinuityIfNeeded( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1480 | bool seeking, bool formatChange, media_track_type trackType, Track *track) { |
| 1481 | // formatChange && seeking: track whose source is changed during selection |
| 1482 | // formatChange && !seeking: track whose source is not changed during selection |
| 1483 | // !formatChange: normal seek |
| 1484 | if ((seeking || formatChange) |
| 1485 | && (trackType == MEDIA_TRACK_TYPE_AUDIO |
| 1486 | || trackType == MEDIA_TRACK_TYPE_VIDEO)) { |
| 1487 | ATSParser::DiscontinuityType type = (formatChange && seeking) |
| 1488 | ? ATSParser::DISCONTINUITY_FORMATCHANGE |
| 1489 | : ATSParser::DISCONTINUITY_NONE; |
| 1490 | track->mPackets->queueDiscontinuity(type, NULL /* extra */, true /* discard */); |
| 1491 | } |
| 1492 | } |
| 1493 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1494 | void NuPlayer2::GenericSource2::notifyBufferingUpdate(int32_t percentage) { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1495 | // Buffering percent could go backward as it's estimated from remaining |
| 1496 | // data and last access time. This could cause the buffering position |
| 1497 | // drawn on media control to jitter slightly. Remember previously reported |
| 1498 | // percentage and don't allow it to go backward. |
| 1499 | if (percentage < mPrevBufferPercentage) { |
| 1500 | percentage = mPrevBufferPercentage; |
| 1501 | } else if (percentage > 100) { |
| 1502 | percentage = 100; |
| 1503 | } |
| 1504 | |
| 1505 | mPrevBufferPercentage = percentage; |
| 1506 | |
| 1507 | ALOGV("notifyBufferingUpdate: buffering %d%%", percentage); |
| 1508 | |
| 1509 | sp<AMessage> notify = dupNotify(); |
| 1510 | notify->setInt32("what", kWhatBufferingUpdate); |
| 1511 | notify->setInt32("percentage", percentage); |
| 1512 | notify->post(); |
| 1513 | } |
| 1514 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1515 | void NuPlayer2::GenericSource2::schedulePollBuffering() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1516 | sp<AMessage> msg = new AMessage(kWhatPollBuffering, this); |
| 1517 | msg->setInt32("generation", mPollBufferingGeneration); |
| 1518 | // Enquires buffering status every second. |
| 1519 | msg->post(1000000ll); |
| 1520 | } |
| 1521 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1522 | void NuPlayer2::GenericSource2::onPollBuffering() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1523 | status_t finalStatus = UNKNOWN_ERROR; |
| 1524 | int64_t cachedDurationUs = -1ll; |
| 1525 | ssize_t cachedDataRemaining = -1; |
| 1526 | |
| 1527 | if (mCachedSource != NULL) { |
| 1528 | cachedDataRemaining = mCachedSource->approxDataRemaining(&finalStatus); |
| 1529 | |
| 1530 | if (finalStatus == OK) { |
| 1531 | off64_t size; |
| 1532 | int64_t bitrate = 0ll; |
| 1533 | if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) { |
| 1534 | // |bitrate| uses bits/second unit, while size is number of bytes. |
| 1535 | bitrate = size * 8000000ll / mDurationUs; |
| 1536 | } else if (mBitrate > 0) { |
| 1537 | bitrate = mBitrate; |
| 1538 | } |
| 1539 | if (bitrate > 0) { |
| 1540 | cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate; |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | if (finalStatus != OK) { |
| 1546 | ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus); |
| 1547 | |
| 1548 | if (finalStatus == ERROR_END_OF_STREAM) { |
| 1549 | notifyBufferingUpdate(100); |
| 1550 | } |
| 1551 | |
| 1552 | return; |
| 1553 | } |
| 1554 | |
| 1555 | if (cachedDurationUs >= 0ll) { |
| 1556 | if (mDurationUs > 0ll) { |
| 1557 | int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs; |
| 1558 | int percentage = 100.0 * cachedPosUs / mDurationUs; |
| 1559 | if (percentage > 100) { |
| 1560 | percentage = 100; |
| 1561 | } |
| 1562 | |
| 1563 | notifyBufferingUpdate(percentage); |
| 1564 | } |
| 1565 | |
| 1566 | ALOGV("onPollBuffering: cachedDurationUs %.1f sec", cachedDurationUs / 1000000.0f); |
| 1567 | } |
| 1568 | |
| 1569 | schedulePollBuffering(); |
| 1570 | } |
| 1571 | |
| 1572 | // Modular DRM |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1573 | status_t NuPlayer2::GenericSource2::prepareDrm( |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1574 | const uint8_t uuid[16], |
| 1575 | const Vector<uint8_t> &drmSessionId, |
| 1576 | sp<AMediaCryptoWrapper> *outCrypto) { |
| 1577 | Mutex::Autolock _l(mLock); |
| 1578 | ALOGV("prepareDrm"); |
| 1579 | |
| 1580 | mIsDrmProtected = false; |
| 1581 | mIsDrmReleased = false; |
| 1582 | mIsSecure = false; |
| 1583 | |
| 1584 | status_t status = OK; |
| 1585 | sp<AMediaCryptoWrapper> crypto = |
| 1586 | new AMediaCryptoWrapper(uuid, drmSessionId.array(), drmSessionId.size()); |
| 1587 | if (crypto == NULL) { |
| 1588 | ALOGE("prepareDrm: failed to create crypto."); |
| 1589 | return UNKNOWN_ERROR; |
| 1590 | } |
| 1591 | ALOGV("prepareDrm: crypto created for uuid: %s", |
| 1592 | DrmUUID::toHexString(uuid).string()); |
| 1593 | |
| 1594 | *outCrypto = crypto; |
| 1595 | // as long a there is an active crypto |
| 1596 | mIsDrmProtected = true; |
| 1597 | |
| 1598 | if (mMimes.size() == 0) { |
| 1599 | status = UNKNOWN_ERROR; |
| 1600 | ALOGE("prepareDrm: Unexpected. Must have at least one track. status: %d", status); |
| 1601 | return status; |
| 1602 | } |
| 1603 | |
| 1604 | // first mime in this list is either the video track, or the first audio track |
| 1605 | const char *mime = mMimes[0].string(); |
| 1606 | mIsSecure = crypto->requiresSecureDecoderComponent(mime); |
| 1607 | ALOGV("prepareDrm: requiresSecureDecoderComponent mime: %s isSecure: %d", |
| 1608 | mime, mIsSecure); |
| 1609 | |
| 1610 | // Checking the member flags while in the looper to send out the notification. |
| 1611 | // The legacy mDecryptHandle!=NULL check (for FLAG_PROTECTED) is equivalent to mIsDrmProtected. |
| 1612 | notifyFlagsChanged( |
| 1613 | (mIsSecure ? FLAG_SECURE : 0) | |
| 1614 | // Setting "protected screen" only for L1: b/38390836 |
| 1615 | (mIsSecure ? FLAG_PROTECTED : 0) | |
| 1616 | FLAG_CAN_PAUSE | |
| 1617 | FLAG_CAN_SEEK_BACKWARD | |
| 1618 | FLAG_CAN_SEEK_FORWARD | |
| 1619 | FLAG_CAN_SEEK); |
| 1620 | |
| 1621 | if (status == OK) { |
| 1622 | ALOGV("prepareDrm: mCrypto: %p", outCrypto->get()); |
| 1623 | ALOGD("prepareDrm ret: %d ", status); |
| 1624 | } else { |
| 1625 | ALOGE("prepareDrm err: %d", status); |
| 1626 | } |
| 1627 | return status; |
| 1628 | } |
| 1629 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1630 | status_t NuPlayer2::GenericSource2::releaseDrm() { |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1631 | Mutex::Autolock _l(mLock); |
| 1632 | ALOGV("releaseDrm"); |
| 1633 | |
| 1634 | if (mIsDrmProtected) { |
| 1635 | mIsDrmProtected = false; |
| 1636 | // to prevent returning any more buffer after stop/releaseDrm (b/37960096) |
| 1637 | mIsDrmReleased = true; |
| 1638 | ALOGV("releaseDrm: mIsDrmProtected is reset."); |
| 1639 | } else { |
| 1640 | ALOGE("releaseDrm: mIsDrmProtected is already false."); |
| 1641 | } |
| 1642 | |
| 1643 | return OK; |
| 1644 | } |
| 1645 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1646 | status_t NuPlayer2::GenericSource2::checkDrmInfo() |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1647 | { |
| 1648 | // clearing the flag at prepare in case the player is reused after stop/releaseDrm with the |
| 1649 | // same source without being reset (called by prepareAsync/initFromDataSource) |
| 1650 | mIsDrmReleased = false; |
| 1651 | |
| 1652 | if (mFileMeta == NULL) { |
| 1653 | ALOGI("checkDrmInfo: No metadata"); |
| 1654 | return OK; // letting the caller responds accordingly |
| 1655 | } |
| 1656 | |
| 1657 | uint32_t type; |
| 1658 | const void *pssh; |
| 1659 | size_t psshsize; |
| 1660 | |
| 1661 | if (!mFileMeta->findData(kKeyPssh, &type, &pssh, &psshsize)) { |
| 1662 | ALOGV("checkDrmInfo: No PSSH"); |
| 1663 | return OK; // source without DRM info |
| 1664 | } |
| 1665 | |
Robert Shih | 4884325 | 2018-01-09 15:24:07 -0800 | [diff] [blame] | 1666 | sp<ABuffer> drmInfoBuffer = NuPlayer2Drm::retrieveDrmInfo(pssh, psshsize); |
| 1667 | ALOGV("checkDrmInfo: MEDIA2_DRM_INFO PSSH size: %d drmInfoBuffer size: %d", |
| 1668 | (int)psshsize, (int)drmInfoBuffer->size()); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1669 | |
Robert Shih | 4884325 | 2018-01-09 15:24:07 -0800 | [diff] [blame] | 1670 | if (drmInfoBuffer->size() == 0) { |
| 1671 | ALOGE("checkDrmInfo: Unexpected drmInfoBuffer size: 0"); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1672 | return UNKNOWN_ERROR; |
| 1673 | } |
| 1674 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1675 | notifyDrmInfo(drmInfoBuffer); |
| 1676 | |
| 1677 | return OK; |
| 1678 | } |
| 1679 | |
Wei Jia | 2409c87 | 2018-02-02 10:34:33 -0800 | [diff] [blame] | 1680 | void NuPlayer2::GenericSource2::signalBufferReturned(MediaBuffer *buffer) |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 1681 | { |
| 1682 | //ALOGV("signalBufferReturned %p refCount: %d", buffer, buffer->localRefcount()); |
| 1683 | |
| 1684 | buffer->setObserver(NULL); |
| 1685 | buffer->release(); // this leads to delete since that there is no observor |
| 1686 | } |
| 1687 | |
| 1688 | } // namespace android |