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