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