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