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