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