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