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