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