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