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