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