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