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