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