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" |
| 21 | |
| 22 | #include "AnotherPacketSource.h" |
| 23 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 24 | #include <media/IMediaHTTPService.h> |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 25 | #include <media/stagefright/foundation/ABuffer.h> |
| 26 | #include <media/stagefright/foundation/ADebug.h> |
| 27 | #include <media/stagefright/foundation/AMessage.h> |
| 28 | #include <media/stagefright/DataSource.h> |
| 29 | #include <media/stagefright/FileSource.h> |
| 30 | #include <media/stagefright/MediaBuffer.h> |
| 31 | #include <media/stagefright/MediaDefs.h> |
| 32 | #include <media/stagefright/MediaExtractor.h> |
| 33 | #include <media/stagefright/MediaSource.h> |
| 34 | #include <media/stagefright/MetaData.h> |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 35 | #include <media/stagefright/Utils.h> |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 36 | #include "../../libstagefright/include/DRMExtractor.h" |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 37 | #include "../../libstagefright/include/NuCachedSource2.h" |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 38 | #include "../../libstagefright/include/WVMExtractor.h" |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 39 | #include "../../libstagefright/include/HTTPBase.h" |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 43 | static int64_t kLowWaterMarkUs = 2000000ll; // 2secs |
| 44 | static int64_t kHighWaterMarkUs = 5000000ll; // 5secs |
| 45 | static const ssize_t kLowWaterMarkBytes = 40000; |
| 46 | static const ssize_t kHighWaterMarkBytes = 200000; |
| 47 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 48 | NuPlayer::GenericSource::GenericSource( |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 49 | const sp<AMessage> ¬ify, |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 50 | bool uidValid, |
| 51 | uid_t uid) |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 52 | : Source(notify), |
Robert Shih | 5c67ddc | 2014-11-04 17:46:05 -0800 | [diff] [blame] | 53 | mAudioTimeUs(0), |
| 54 | mAudioLastDequeueTimeUs(0), |
| 55 | mVideoTimeUs(0), |
| 56 | mVideoLastDequeueTimeUs(0), |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 57 | mFetchSubtitleDataGeneration(0), |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 58 | mFetchTimedTextDataGeneration(0), |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 59 | mDurationUs(0ll), |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 60 | mAudioIsVorbis(false), |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 61 | mIsWidevine(false), |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 62 | mIsSecure(false), |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 63 | mIsStreaming(false), |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 64 | mUIDValid(uidValid), |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 65 | mUID(uid), |
Chong Zhang | a6bf21f | 2014-11-19 20:26:34 -0800 | [diff] [blame] | 66 | mFd(-1), |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 67 | mDrmManagerClient(NULL), |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 68 | mMetaDataSize(-1ll), |
| 69 | mBitrate(-1ll), |
Lajos Molnar | 84f5278 | 2014-09-11 10:01:55 -0700 | [diff] [blame] | 70 | mPollBufferingGeneration(0), |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 71 | mPendingReadBufferTypes(0), |
| 72 | mBuffering(false), |
| 73 | mPrepareBuffering(false) { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 74 | resetDataSource(); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 75 | DataSource::RegisterDefaultSniffers(); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 78 | void NuPlayer::GenericSource::resetDataSource() { |
| 79 | mHTTPService.clear(); |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 80 | mHttpSource.clear(); |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 81 | mUri.clear(); |
| 82 | mUriHeaders.clear(); |
Chong Zhang | a6bf21f | 2014-11-19 20:26:34 -0800 | [diff] [blame] | 83 | if (mFd >= 0) { |
| 84 | close(mFd); |
| 85 | mFd = -1; |
| 86 | } |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 87 | mOffset = 0; |
| 88 | mLength = 0; |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 89 | setDrmPlaybackStatusIfNeeded(Playback::STOP, 0); |
| 90 | mDecryptHandle = NULL; |
| 91 | mDrmManagerClient = NULL; |
| 92 | mStarted = false; |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 93 | mStopRead = true; |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | status_t NuPlayer::GenericSource::setDataSource( |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 97 | const sp<IMediaHTTPService> &httpService, |
| 98 | const char *url, |
| 99 | const KeyedVector<String8, String8> *headers) { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 100 | resetDataSource(); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 101 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 102 | mHTTPService = httpService; |
| 103 | mUri = url; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 104 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 105 | if (headers) { |
| 106 | mUriHeaders = *headers; |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 109 | // delay data source creation to prepareAsync() to avoid blocking |
| 110 | // the calling thread in setDataSource for any significant time. |
| 111 | return OK; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 114 | status_t NuPlayer::GenericSource::setDataSource( |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 115 | int fd, int64_t offset, int64_t length) { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 116 | resetDataSource(); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 117 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 118 | mFd = dup(fd); |
| 119 | mOffset = offset; |
| 120 | mLength = length; |
| 121 | |
| 122 | // delay data source creation to prepareAsync() to avoid blocking |
| 123 | // the calling thread in setDataSource for any significant time. |
| 124 | return OK; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Marco Nelissen | f0b72b5 | 2014-09-16 15:43:44 -0700 | [diff] [blame] | 127 | sp<MetaData> NuPlayer::GenericSource::getFileFormatMeta() const { |
| 128 | return mFileMeta; |
| 129 | } |
| 130 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 131 | status_t NuPlayer::GenericSource::initFromDataSource() { |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 132 | sp<MediaExtractor> extractor; |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 133 | String8 mimeType; |
| 134 | float confidence; |
| 135 | sp<AMessage> dummy; |
| 136 | bool isWidevineStreaming = false; |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 137 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 138 | CHECK(mDataSource != NULL); |
| 139 | |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 140 | if (mIsWidevine) { |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 141 | isWidevineStreaming = SniffWVM( |
| 142 | mDataSource, &mimeType, &confidence, &dummy); |
| 143 | if (!isWidevineStreaming || |
| 144 | strcasecmp( |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 145 | mimeType.string(), MEDIA_MIMETYPE_CONTAINER_WVM)) { |
| 146 | ALOGE("unsupported widevine mime: %s", mimeType.string()); |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 147 | return UNKNOWN_ERROR; |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 148 | } |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 149 | } else if (mIsStreaming) { |
| 150 | if (mSniffedMIME.empty()) { |
| 151 | if (!mDataSource->sniff(&mimeType, &confidence, &dummy)) { |
| 152 | return UNKNOWN_ERROR; |
| 153 | } |
| 154 | mSniffedMIME = mimeType.string(); |
| 155 | } |
| 156 | isWidevineStreaming = !strcasecmp( |
| 157 | mSniffedMIME.c_str(), MEDIA_MIMETYPE_CONTAINER_WVM); |
| 158 | } |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 159 | |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 160 | if (isWidevineStreaming) { |
| 161 | // we don't want cached source for widevine streaming. |
| 162 | mCachedSource.clear(); |
| 163 | mDataSource = mHttpSource; |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 164 | mWVMExtractor = new WVMExtractor(mDataSource); |
| 165 | mWVMExtractor->setAdaptiveStreamingMode(true); |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 166 | if (mUIDValid) { |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 167 | mWVMExtractor->setUID(mUID); |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 168 | } |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 169 | extractor = mWVMExtractor; |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 170 | } else { |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 171 | extractor = MediaExtractor::Create(mDataSource, |
| 172 | mSniffedMIME.empty() ? NULL: mSniffedMIME.c_str()); |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 173 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 174 | |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 175 | if (extractor == NULL) { |
| 176 | return UNKNOWN_ERROR; |
| 177 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 178 | |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 179 | if (extractor->getDrmFlag()) { |
| 180 | checkDrmStatus(mDataSource); |
| 181 | } |
| 182 | |
Marco Nelissen | f0b72b5 | 2014-09-16 15:43:44 -0700 | [diff] [blame] | 183 | mFileMeta = extractor->getMetaData(); |
| 184 | if (mFileMeta != NULL) { |
Marco Nelissen | c1f4b2b | 2014-06-17 14:48:32 -0700 | [diff] [blame] | 185 | int64_t duration; |
Marco Nelissen | f0b72b5 | 2014-09-16 15:43:44 -0700 | [diff] [blame] | 186 | if (mFileMeta->findInt64(kKeyDuration, &duration)) { |
Marco Nelissen | c1f4b2b | 2014-06-17 14:48:32 -0700 | [diff] [blame] | 187 | mDurationUs = duration; |
| 188 | } |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 189 | |
| 190 | if (!mIsWidevine) { |
| 191 | // Check mime to see if we actually have a widevine source. |
| 192 | // If the data source is not URL-type (eg. file source), we |
| 193 | // won't be able to tell until now. |
| 194 | const char *fileMime; |
| 195 | if (mFileMeta->findCString(kKeyMIMEType, &fileMime) |
| 196 | && !strncasecmp(fileMime, "video/wvm", 9)) { |
| 197 | mIsWidevine = true; |
| 198 | } |
| 199 | } |
Marco Nelissen | c1f4b2b | 2014-06-17 14:48:32 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 202 | int32_t totalBitrate = 0; |
| 203 | |
Marco Nelissen | 705d329 | 2014-09-19 15:14:37 -0700 | [diff] [blame] | 204 | size_t numtracks = extractor->countTracks(); |
| 205 | if (numtracks == 0) { |
| 206 | return UNKNOWN_ERROR; |
| 207 | } |
| 208 | |
| 209 | for (size_t i = 0; i < numtracks; ++i) { |
Chong Zhang | afc0a87 | 2014-08-26 09:56:52 -0700 | [diff] [blame] | 210 | sp<MediaSource> track = extractor->getTrack(i); |
Wei Jia | e31eeb4 | 2015-08-28 10:35:35 -0700 | [diff] [blame] | 211 | if (track == NULL) { |
| 212 | continue; |
| 213 | } |
Chong Zhang | afc0a87 | 2014-08-26 09:56:52 -0700 | [diff] [blame] | 214 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 215 | sp<MetaData> meta = extractor->getTrackMetaData(i); |
| 216 | |
| 217 | const char *mime; |
| 218 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 219 | |
Chong Zhang | afc0a87 | 2014-08-26 09:56:52 -0700 | [diff] [blame] | 220 | // Do the string compare immediately with "mime", |
| 221 | // we can't assume "mime" would stay valid after another |
| 222 | // extractor operation, some extractors might modify meta |
| 223 | // during getTrack() and make it invalid. |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 224 | if (!strncasecmp(mime, "audio/", 6)) { |
| 225 | if (mAudioTrack.mSource == NULL) { |
Robert Shih | dd23572 | 2014-06-12 14:49:23 -0700 | [diff] [blame] | 226 | mAudioTrack.mIndex = i; |
| 227 | mAudioTrack.mSource = track; |
Robert Shih | af52c1a | 2014-09-11 15:38:54 -0700 | [diff] [blame] | 228 | mAudioTrack.mPackets = |
| 229 | new AnotherPacketSource(mAudioTrack.mSource->getFormat()); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 230 | |
| 231 | if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_VORBIS)) { |
| 232 | mAudioIsVorbis = true; |
| 233 | } else { |
| 234 | mAudioIsVorbis = false; |
| 235 | } |
| 236 | } |
| 237 | } else if (!strncasecmp(mime, "video/", 6)) { |
| 238 | if (mVideoTrack.mSource == NULL) { |
Robert Shih | dd23572 | 2014-06-12 14:49:23 -0700 | [diff] [blame] | 239 | mVideoTrack.mIndex = i; |
| 240 | mVideoTrack.mSource = track; |
Robert Shih | af52c1a | 2014-09-11 15:38:54 -0700 | [diff] [blame] | 241 | mVideoTrack.mPackets = |
| 242 | new AnotherPacketSource(mVideoTrack.mSource->getFormat()); |
Chong Zhang | 7e89218 | 2014-08-05 11:58:21 -0700 | [diff] [blame] | 243 | |
| 244 | // check if the source requires secure buffers |
| 245 | int32_t secure; |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 246 | if (meta->findInt32(kKeyRequiresSecureBuffers, &secure) |
| 247 | && secure) { |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 248 | mIsSecure = true; |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 249 | if (mUIDValid) { |
| 250 | extractor->setUID(mUID); |
| 251 | } |
Chong Zhang | 7e89218 | 2014-08-05 11:58:21 -0700 | [diff] [blame] | 252 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Wei Jia | e31eeb4 | 2015-08-28 10:35:35 -0700 | [diff] [blame] | 256 | mSources.push(track); |
| 257 | int64_t durationUs; |
| 258 | if (meta->findInt64(kKeyDuration, &durationUs)) { |
| 259 | if (durationUs > mDurationUs) { |
| 260 | mDurationUs = durationUs; |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 261 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 262 | } |
Wei Jia | e31eeb4 | 2015-08-28 10:35:35 -0700 | [diff] [blame] | 263 | |
| 264 | int32_t bitrate; |
| 265 | if (totalBitrate >= 0 && meta->findInt32(kKeyBitRate, &bitrate)) { |
| 266 | totalBitrate += bitrate; |
| 267 | } else { |
| 268 | totalBitrate = -1; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (mSources.size() == 0) { |
| 273 | ALOGE("b/23705695"); |
| 274 | return UNKNOWN_ERROR; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 275 | } |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 276 | |
Lajos Molnar | 68fca63 | 2015-03-31 10:06:48 -0700 | [diff] [blame] | 277 | mBitrate = totalBitrate; |
| 278 | |
| 279 | return OK; |
| 280 | } |
| 281 | |
| 282 | status_t NuPlayer::GenericSource::startSources() { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 283 | // Start the selected A/V tracks now before we start buffering. |
| 284 | // Widevine sources might re-initialize crypto when starting, if we delay |
| 285 | // this to start(), all data buffered during prepare would be wasted. |
| 286 | // (We don't actually start reading until start().) |
| 287 | if (mAudioTrack.mSource != NULL && mAudioTrack.mSource->start() != OK) { |
| 288 | ALOGE("failed to start audio track!"); |
| 289 | return UNKNOWN_ERROR; |
| 290 | } |
| 291 | |
| 292 | if (mVideoTrack.mSource != NULL && mVideoTrack.mSource->start() != OK) { |
| 293 | ALOGE("failed to start video track!"); |
| 294 | return UNKNOWN_ERROR; |
| 295 | } |
| 296 | |
Chong Zhang | 3de157d | 2014-08-05 20:54:44 -0700 | [diff] [blame] | 297 | return OK; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 300 | void NuPlayer::GenericSource::checkDrmStatus(const sp<DataSource>& dataSource) { |
| 301 | dataSource->getDrmInfo(mDecryptHandle, &mDrmManagerClient); |
| 302 | if (mDecryptHandle != NULL) { |
| 303 | CHECK(mDrmManagerClient); |
| 304 | if (RightsStatus::RIGHTS_VALID != mDecryptHandle->status) { |
| 305 | sp<AMessage> msg = dupNotify(); |
| 306 | msg->setInt32("what", kWhatDrmNoLicense); |
| 307 | msg->post(); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | int64_t NuPlayer::GenericSource::getLastReadPosition() { |
| 313 | if (mAudioTrack.mSource != NULL) { |
| 314 | return mAudioTimeUs; |
| 315 | } else if (mVideoTrack.mSource != NULL) { |
| 316 | return mVideoTimeUs; |
| 317 | } else { |
| 318 | return 0; |
| 319 | } |
| 320 | } |
| 321 | |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 322 | status_t NuPlayer::GenericSource::setBuffers( |
| 323 | bool audio, Vector<MediaBuffer *> &buffers) { |
Wei Jia | 67ca3ca | 2016-05-25 14:10:26 -0700 | [diff] [blame] | 324 | if (mIsSecure && !audio && mVideoTrack.mSource != NULL) { |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 325 | return mVideoTrack.mSource->setBuffers(buffers); |
| 326 | } |
| 327 | return INVALID_OPERATION; |
| 328 | } |
| 329 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 330 | NuPlayer::GenericSource::~GenericSource() { |
Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 331 | if (mLooper != NULL) { |
| 332 | mLooper->unregisterHandler(id()); |
| 333 | mLooper->stop(); |
| 334 | } |
Chong Zhang | a6bf21f | 2014-11-19 20:26:34 -0800 | [diff] [blame] | 335 | resetDataSource(); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 338 | void NuPlayer::GenericSource::prepareAsync() { |
Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 339 | if (mLooper == NULL) { |
| 340 | mLooper = new ALooper; |
| 341 | mLooper->setName("generic"); |
| 342 | mLooper->start(); |
| 343 | |
| 344 | mLooper->registerHandler(this); |
| 345 | } |
| 346 | |
| 347 | sp<AMessage> msg = new AMessage(kWhatPrepareAsync, id()); |
| 348 | msg->post(); |
| 349 | } |
| 350 | |
| 351 | void NuPlayer::GenericSource::onPrepareAsync() { |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 352 | // delayed data source creation |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 353 | if (mDataSource == NULL) { |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 354 | // set to false first, if the extractor |
| 355 | // comes back as secure, set it to true then. |
| 356 | mIsSecure = false; |
| 357 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 358 | if (!mUri.empty()) { |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 359 | const char* uri = mUri.c_str(); |
| 360 | mIsWidevine = !strncasecmp(uri, "widevine://", 11); |
| 361 | |
| 362 | if (!strncasecmp("http://", uri, 7) |
| 363 | || !strncasecmp("https://", uri, 8) |
| 364 | || mIsWidevine) { |
| 365 | mHttpSource = DataSource::CreateMediaHTTP(mHTTPService); |
| 366 | if (mHttpSource == NULL) { |
| 367 | ALOGE("Failed to create http source!"); |
| 368 | notifyPreparedAndCleanup(UNKNOWN_ERROR); |
| 369 | return; |
| 370 | } |
| 371 | } |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 372 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 373 | mDataSource = DataSource::CreateFromURI( |
Robert Shih | 360d6d0 | 2014-09-29 14:42:35 -0700 | [diff] [blame] | 374 | mHTTPService, uri, &mUriHeaders, &mContentType, |
| 375 | static_cast<HTTPBase *>(mHttpSource.get())); |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 376 | } else { |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 377 | mIsWidevine = false; |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 378 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 379 | mDataSource = new FileSource(mFd, mOffset, mLength); |
Chong Zhang | a6bf21f | 2014-11-19 20:26:34 -0800 | [diff] [blame] | 380 | mFd = -1; |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 381 | } |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 382 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 383 | if (mDataSource == NULL) { |
| 384 | ALOGE("Failed to create data source!"); |
| 385 | notifyPreparedAndCleanup(UNKNOWN_ERROR); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (mDataSource->flags() & DataSource::kIsCachingDataSource) { |
| 390 | mCachedSource = static_cast<NuCachedSource2 *>(mDataSource.get()); |
| 391 | } |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 392 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 393 | // For widevine or other cached streaming cases, we need to wait for |
| 394 | // enough buffering before reporting prepared. |
| 395 | // Note that even when URL doesn't start with widevine://, mIsWidevine |
| 396 | // could still be set to true later, if the streaming or file source |
| 397 | // is sniffed to be widevine. We don't want to buffer for file source |
| 398 | // in that case, so must check the flag now. |
| 399 | mIsStreaming = (mIsWidevine || mCachedSource != NULL); |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 402 | // check initial caching status |
| 403 | status_t err = prefillCacheIfNecessary(); |
| 404 | if (err != OK) { |
| 405 | if (err == -EAGAIN) { |
| 406 | (new AMessage(kWhatPrepareAsync, id()))->post(200000); |
| 407 | } else { |
| 408 | ALOGE("Failed to prefill data cache!"); |
| 409 | notifyPreparedAndCleanup(UNKNOWN_ERROR); |
| 410 | } |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 414 | // init extrator from data source |
| 415 | err = initFromDataSource(); |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 416 | |
| 417 | if (err != OK) { |
| 418 | ALOGE("Failed to init from data source!"); |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 419 | notifyPreparedAndCleanup(err); |
Chong Zhang | a19f33e | 2014-08-07 15:35:07 -0700 | [diff] [blame] | 420 | return; |
| 421 | } |
| 422 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 423 | if (mVideoTrack.mSource != NULL) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 424 | sp<MetaData> meta = doGetFormatMeta(false /* audio */); |
| 425 | sp<AMessage> msg = new AMessage; |
| 426 | err = convertMetaDataToMessage(meta, &msg); |
| 427 | if(err != OK) { |
| 428 | notifyPreparedAndCleanup(err); |
| 429 | return; |
| 430 | } |
| 431 | notifyVideoSizeChanged(msg); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | notifyFlagsChanged( |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 435 | (mIsSecure ? FLAG_SECURE : 0) |
Chong Zhang | 1713460 | 2015-01-07 16:14:34 -0800 | [diff] [blame] | 436 | | (mDecryptHandle != NULL ? FLAG_PROTECTED : 0) |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 437 | | FLAG_CAN_PAUSE |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 438 | | FLAG_CAN_SEEK_BACKWARD |
| 439 | | FLAG_CAN_SEEK_FORWARD |
| 440 | | FLAG_CAN_SEEK); |
| 441 | |
Lajos Molnar | 68fca63 | 2015-03-31 10:06:48 -0700 | [diff] [blame] | 442 | if (mIsSecure) { |
| 443 | // secure decoders must be instantiated before starting widevine source |
| 444 | sp<AMessage> reply = new AMessage(kWhatSecureDecodersInstantiated, id()); |
| 445 | notifyInstantiateSecureDecoders(reply); |
| 446 | } else { |
| 447 | finishPrepareAsync(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void NuPlayer::GenericSource::onSecureDecodersInstantiated(status_t err) { |
| 452 | if (err != OK) { |
| 453 | ALOGE("Failed to instantiate secure decoders!"); |
| 454 | notifyPreparedAndCleanup(err); |
| 455 | return; |
| 456 | } |
| 457 | finishPrepareAsync(); |
| 458 | } |
| 459 | |
| 460 | void NuPlayer::GenericSource::finishPrepareAsync() { |
| 461 | status_t err = startSources(); |
| 462 | if (err != OK) { |
| 463 | ALOGE("Failed to init start data source!"); |
| 464 | notifyPreparedAndCleanup(err); |
| 465 | return; |
| 466 | } |
| 467 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 468 | if (mIsStreaming) { |
| 469 | mPrepareBuffering = true; |
| 470 | |
| 471 | ensureCacheIsFetching(); |
| 472 | restartPollBuffering(); |
| 473 | } else { |
| 474 | notifyPrepared(); |
| 475 | } |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 478 | void NuPlayer::GenericSource::notifyPreparedAndCleanup(status_t err) { |
| 479 | if (err != OK) { |
| 480 | mMetaDataSize = -1ll; |
| 481 | mContentType = ""; |
| 482 | mSniffedMIME = ""; |
Robert Shih | f3eb826 | 2015-09-02 14:02:47 -0700 | [diff] [blame] | 483 | { |
| 484 | sp<DataSource> dataSource = mDataSource; |
| 485 | sp<NuCachedSource2> cachedSource = mCachedSource; |
| 486 | sp<DataSource> httpSource = mHttpSource; |
| 487 | { |
| 488 | Mutex::Autolock _l(mDisconnectLock); |
| 489 | mDataSource.clear(); |
Wei Jia | 09c291c | 2015-10-22 11:35:04 -0700 | [diff] [blame] | 490 | mDecryptHandle = NULL; |
Wei Jia | 224858e | 2015-10-19 16:14:14 -0700 | [diff] [blame] | 491 | mDrmManagerClient = NULL; |
Robert Shih | f3eb826 | 2015-09-02 14:02:47 -0700 | [diff] [blame] | 492 | mCachedSource.clear(); |
| 493 | mHttpSource.clear(); |
| 494 | } |
| 495 | } |
Lajos Molnar | 68fca63 | 2015-03-31 10:06:48 -0700 | [diff] [blame] | 496 | mBitrate = -1; |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 497 | |
| 498 | cancelPollBuffering(); |
Chong Zhang | d354d8d | 2014-08-20 13:09:58 -0700 | [diff] [blame] | 499 | } |
| 500 | notifyPrepared(err); |
| 501 | } |
| 502 | |
| 503 | status_t NuPlayer::GenericSource::prefillCacheIfNecessary() { |
| 504 | CHECK(mDataSource != NULL); |
| 505 | |
| 506 | if (mCachedSource == NULL) { |
| 507 | // no prefill if the data source is not cached |
| 508 | return OK; |
| 509 | } |
| 510 | |
| 511 | // We're not doing this for streams that appear to be audio-only |
| 512 | // streams to ensure that even low bandwidth streams start |
| 513 | // playing back fairly instantly. |
| 514 | if (!strncasecmp(mContentType.string(), "audio/", 6)) { |
| 515 | return OK; |
| 516 | } |
| 517 | |
| 518 | // We're going to prefill the cache before trying to instantiate |
| 519 | // the extractor below, as the latter is an operation that otherwise |
| 520 | // could block on the datasource for a significant amount of time. |
| 521 | // During that time we'd be unable to abort the preparation phase |
| 522 | // without this prefill. |
| 523 | |
| 524 | // Initially make sure we have at least 192 KB for the sniff |
| 525 | // to complete without blocking. |
| 526 | static const size_t kMinBytesForSniffing = 192 * 1024; |
| 527 | static const size_t kDefaultMetaSize = 200000; |
| 528 | |
| 529 | status_t finalStatus; |
| 530 | |
| 531 | size_t cachedDataRemaining = |
| 532 | mCachedSource->approxDataRemaining(&finalStatus); |
| 533 | |
| 534 | if (finalStatus != OK || (mMetaDataSize >= 0 |
| 535 | && (off64_t)cachedDataRemaining >= mMetaDataSize)) { |
| 536 | ALOGV("stop caching, status %d, " |
| 537 | "metaDataSize %lld, cachedDataRemaining %zu", |
| 538 | finalStatus, mMetaDataSize, cachedDataRemaining); |
| 539 | return OK; |
| 540 | } |
| 541 | |
| 542 | ALOGV("now cached %zu bytes of data", cachedDataRemaining); |
| 543 | |
| 544 | if (mMetaDataSize < 0 |
| 545 | && cachedDataRemaining >= kMinBytesForSniffing) { |
| 546 | String8 tmp; |
| 547 | float confidence; |
| 548 | sp<AMessage> meta; |
| 549 | if (!mCachedSource->sniff(&tmp, &confidence, &meta)) { |
| 550 | return UNKNOWN_ERROR; |
| 551 | } |
| 552 | |
| 553 | // We successfully identified the file's extractor to |
| 554 | // be, remember this mime type so we don't have to |
| 555 | // sniff it again when we call MediaExtractor::Create() |
| 556 | mSniffedMIME = tmp.string(); |
| 557 | |
| 558 | if (meta == NULL |
| 559 | || !meta->findInt64("meta-data-size", |
| 560 | reinterpret_cast<int64_t*>(&mMetaDataSize))) { |
| 561 | mMetaDataSize = kDefaultMetaSize; |
| 562 | } |
| 563 | |
| 564 | if (mMetaDataSize < 0ll) { |
| 565 | ALOGE("invalid metaDataSize = %lld bytes", mMetaDataSize); |
| 566 | return UNKNOWN_ERROR; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return -EAGAIN; |
| 571 | } |
| 572 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 573 | void NuPlayer::GenericSource::start() { |
| 574 | ALOGI("start"); |
| 575 | |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 576 | mStopRead = false; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 577 | if (mAudioTrack.mSource != NULL) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 578 | postReadBuffer(MEDIA_TRACK_TYPE_AUDIO); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | if (mVideoTrack.mSource != NULL) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 582 | postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 583 | } |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 584 | |
| 585 | setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000); |
| 586 | mStarted = true; |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 587 | |
| 588 | (new AMessage(kWhatStart, id()))->post(); |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void NuPlayer::GenericSource::stop() { |
| 592 | // nothing to do, just account for DRM playback status |
| 593 | setDrmPlaybackStatusIfNeeded(Playback::STOP, 0); |
| 594 | mStarted = false; |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 595 | if (mIsWidevine || mIsSecure) { |
| 596 | // For widevine or secure sources we need to prevent any further reads. |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 597 | sp<AMessage> msg = new AMessage(kWhatStopWidevine, id()); |
| 598 | sp<AMessage> response; |
| 599 | (void) msg->postAndAwaitResponse(&response); |
| 600 | } |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void NuPlayer::GenericSource::pause() { |
| 604 | // nothing to do, just account for DRM playback status |
| 605 | setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0); |
| 606 | mStarted = false; |
| 607 | } |
| 608 | |
| 609 | void NuPlayer::GenericSource::resume() { |
| 610 | // nothing to do, just account for DRM playback status |
| 611 | setDrmPlaybackStatusIfNeeded(Playback::START, getLastReadPosition() / 1000); |
| 612 | mStarted = true; |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 613 | |
| 614 | (new AMessage(kWhatResume, id()))->post(); |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 617 | void NuPlayer::GenericSource::disconnect() { |
Robert Shih | f3eb826 | 2015-09-02 14:02:47 -0700 | [diff] [blame] | 618 | sp<DataSource> dataSource, httpSource; |
| 619 | { |
| 620 | Mutex::Autolock _l(mDisconnectLock); |
| 621 | dataSource = mDataSource; |
| 622 | httpSource = mHttpSource; |
| 623 | } |
| 624 | |
| 625 | if (dataSource != NULL) { |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 626 | // disconnect data source |
Robert Shih | f3eb826 | 2015-09-02 14:02:47 -0700 | [diff] [blame] | 627 | if (dataSource->flags() & DataSource::kIsCachingDataSource) { |
| 628 | static_cast<NuCachedSource2 *>(dataSource.get())->disconnect(); |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 629 | } |
Robert Shih | f3eb826 | 2015-09-02 14:02:47 -0700 | [diff] [blame] | 630 | } else if (httpSource != NULL) { |
| 631 | static_cast<HTTPBase *>(httpSource.get())->disconnect(); |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 635 | void NuPlayer::GenericSource::setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position) { |
| 636 | if (mDecryptHandle != NULL) { |
| 637 | mDrmManagerClient->setPlaybackStatus(mDecryptHandle, playbackStatus, position); |
| 638 | } |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 639 | mSubtitleTrack.mPackets = new AnotherPacketSource(NULL); |
| 640 | mTimedTextTrack.mPackets = new AnotherPacketSource(NULL); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | status_t NuPlayer::GenericSource::feedMoreTSData() { |
| 644 | return OK; |
| 645 | } |
| 646 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 647 | void NuPlayer::GenericSource::schedulePollBuffering() { |
| 648 | sp<AMessage> msg = new AMessage(kWhatPollBuffering, id()); |
| 649 | msg->setInt32("generation", mPollBufferingGeneration); |
| 650 | msg->post(1000000ll); |
| 651 | } |
| 652 | |
| 653 | void NuPlayer::GenericSource::cancelPollBuffering() { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 654 | mBuffering = false; |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 655 | ++mPollBufferingGeneration; |
| 656 | } |
| 657 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 658 | void NuPlayer::GenericSource::restartPollBuffering() { |
| 659 | if (mIsStreaming) { |
| 660 | cancelPollBuffering(); |
| 661 | onPollBuffering(); |
| 662 | } |
| 663 | } |
| 664 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 665 | void NuPlayer::GenericSource::notifyBufferingUpdate(int percentage) { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 666 | ALOGV("notifyBufferingUpdate: buffering %d%%", percentage); |
| 667 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 668 | sp<AMessage> msg = dupNotify(); |
| 669 | msg->setInt32("what", kWhatBufferingUpdate); |
| 670 | msg->setInt32("percentage", percentage); |
| 671 | msg->post(); |
| 672 | } |
| 673 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 674 | void NuPlayer::GenericSource::startBufferingIfNecessary() { |
| 675 | ALOGV("startBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d", |
| 676 | mPrepareBuffering, mBuffering); |
| 677 | |
| 678 | if (mPrepareBuffering) { |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | if (!mBuffering) { |
| 683 | mBuffering = true; |
| 684 | |
| 685 | ensureCacheIsFetching(); |
| 686 | sendCacheStats(); |
| 687 | |
| 688 | sp<AMessage> notify = dupNotify(); |
| 689 | notify->setInt32("what", kWhatPauseOnBufferingStart); |
| 690 | notify->post(); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | void NuPlayer::GenericSource::stopBufferingIfNecessary() { |
| 695 | ALOGV("stopBufferingIfNecessary: mPrepareBuffering=%d, mBuffering=%d", |
| 696 | mPrepareBuffering, mBuffering); |
| 697 | |
| 698 | if (mPrepareBuffering) { |
| 699 | mPrepareBuffering = false; |
| 700 | notifyPrepared(); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | if (mBuffering) { |
| 705 | mBuffering = false; |
| 706 | |
| 707 | sendCacheStats(); |
| 708 | |
| 709 | sp<AMessage> notify = dupNotify(); |
| 710 | notify->setInt32("what", kWhatResumeOnBufferingEnd); |
| 711 | notify->post(); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | void NuPlayer::GenericSource::sendCacheStats() { |
| 716 | int32_t kbps = 0; |
| 717 | status_t err = UNKNOWN_ERROR; |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 718 | |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 719 | if (mWVMExtractor != NULL) { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 720 | err = mWVMExtractor->getEstimatedBandwidthKbps(&kbps); |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 721 | } else if (mCachedSource != NULL) { |
| 722 | err = mCachedSource->getEstimatedBandwidthKbps(&kbps); |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (err == OK) { |
| 726 | sp<AMessage> notify = dupNotify(); |
| 727 | notify->setInt32("what", kWhatCacheStats); |
| 728 | notify->setInt32("bandwidth", kbps); |
| 729 | notify->post(); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | void NuPlayer::GenericSource::ensureCacheIsFetching() { |
| 734 | if (mCachedSource != NULL) { |
| 735 | mCachedSource->resumeFetchingIfNecessary(); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void NuPlayer::GenericSource::onPollBuffering() { |
| 740 | status_t finalStatus = UNKNOWN_ERROR; |
| 741 | int64_t cachedDurationUs = -1ll; |
| 742 | ssize_t cachedDataRemaining = -1; |
| 743 | |
Chong Zhang | 91a23ed | 2015-02-19 16:39:59 -0800 | [diff] [blame] | 744 | ALOGW_IF(mWVMExtractor != NULL && mCachedSource != NULL, |
| 745 | "WVMExtractor and NuCachedSource both present"); |
| 746 | |
| 747 | if (mWVMExtractor != NULL) { |
| 748 | cachedDurationUs = |
| 749 | mWVMExtractor->getCachedDurationUs(&finalStatus); |
| 750 | } else if (mCachedSource != NULL) { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 751 | cachedDataRemaining = |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 752 | mCachedSource->approxDataRemaining(&finalStatus); |
| 753 | |
| 754 | if (finalStatus == OK) { |
| 755 | off64_t size; |
| 756 | int64_t bitrate = 0ll; |
| 757 | if (mDurationUs > 0 && mCachedSource->getSize(&size) == OK) { |
| 758 | bitrate = size * 8000000ll / mDurationUs; |
| 759 | } else if (mBitrate > 0) { |
| 760 | bitrate = mBitrate; |
| 761 | } |
| 762 | if (bitrate > 0) { |
| 763 | cachedDurationUs = cachedDataRemaining * 8000000ll / bitrate; |
| 764 | } |
| 765 | } |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 768 | if (finalStatus != OK) { |
| 769 | ALOGV("onPollBuffering: EOS (finalStatus = %d)", finalStatus); |
| 770 | |
| 771 | if (finalStatus == ERROR_END_OF_STREAM) { |
| 772 | notifyBufferingUpdate(100); |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 775 | stopBufferingIfNecessary(); |
| 776 | return; |
| 777 | } else if (cachedDurationUs >= 0ll) { |
| 778 | if (mDurationUs > 0ll) { |
| 779 | int64_t cachedPosUs = getLastReadPosition() + cachedDurationUs; |
| 780 | int percentage = 100.0 * cachedPosUs / mDurationUs; |
| 781 | if (percentage > 100) { |
| 782 | percentage = 100; |
| 783 | } |
| 784 | |
| 785 | notifyBufferingUpdate(percentage); |
| 786 | } |
| 787 | |
| 788 | ALOGV("onPollBuffering: cachedDurationUs %.1f sec", |
| 789 | cachedDurationUs / 1000000.0f); |
| 790 | |
| 791 | if (cachedDurationUs < kLowWaterMarkUs) { |
| 792 | startBufferingIfNecessary(); |
| 793 | } else if (cachedDurationUs > kHighWaterMarkUs) { |
| 794 | stopBufferingIfNecessary(); |
| 795 | } |
| 796 | } else if (cachedDataRemaining >= 0) { |
| 797 | ALOGV("onPollBuffering: cachedDataRemaining %d bytes", |
| 798 | cachedDataRemaining); |
| 799 | |
| 800 | if (cachedDataRemaining < kLowWaterMarkBytes) { |
| 801 | startBufferingIfNecessary(); |
| 802 | } else if (cachedDataRemaining > kHighWaterMarkBytes) { |
| 803 | stopBufferingIfNecessary(); |
| 804 | } |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | schedulePollBuffering(); |
| 808 | } |
| 809 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 810 | void NuPlayer::GenericSource::onMessageReceived(const sp<AMessage> &msg) { |
| 811 | switch (msg->what()) { |
Chong Zhang | 1228d6b | 2014-08-12 21:25:48 -0700 | [diff] [blame] | 812 | case kWhatPrepareAsync: |
| 813 | { |
| 814 | onPrepareAsync(); |
| 815 | break; |
| 816 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 817 | case kWhatFetchSubtitleData: |
| 818 | { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 819 | fetchTextData(kWhatSendSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, |
| 820 | mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); |
| 821 | break; |
| 822 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 823 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 824 | case kWhatFetchTimedTextData: |
| 825 | { |
| 826 | fetchTextData(kWhatSendTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, |
| 827 | mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | |
| 831 | case kWhatSendSubtitleData: |
| 832 | { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 833 | sendTextData(kWhatSubtitleData, MEDIA_TRACK_TYPE_SUBTITLE, |
| 834 | mFetchSubtitleDataGeneration, mSubtitleTrack.mPackets, msg); |
| 835 | break; |
| 836 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 837 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 838 | case kWhatSendTimedTextData: |
| 839 | { |
| 840 | sendTextData(kWhatTimedTextData, MEDIA_TRACK_TYPE_TIMEDTEXT, |
| 841 | mFetchTimedTextDataGeneration, mTimedTextTrack.mPackets, msg); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | |
| 845 | case kWhatChangeAVSource: |
| 846 | { |
| 847 | int32_t trackIndex; |
| 848 | CHECK(msg->findInt32("trackIndex", &trackIndex)); |
| 849 | const sp<MediaSource> source = mSources.itemAt(trackIndex); |
| 850 | |
| 851 | Track* track; |
| 852 | const char *mime; |
| 853 | media_track_type trackType, counterpartType; |
| 854 | sp<MetaData> meta = source->getFormat(); |
| 855 | meta->findCString(kKeyMIMEType, &mime); |
| 856 | if (!strncasecmp(mime, "audio/", 6)) { |
| 857 | track = &mAudioTrack; |
| 858 | trackType = MEDIA_TRACK_TYPE_AUDIO; |
| 859 | counterpartType = MEDIA_TRACK_TYPE_VIDEO;; |
| 860 | } else { |
| 861 | CHECK(!strncasecmp(mime, "video/", 6)); |
| 862 | track = &mVideoTrack; |
| 863 | trackType = MEDIA_TRACK_TYPE_VIDEO; |
| 864 | counterpartType = MEDIA_TRACK_TYPE_AUDIO;; |
| 865 | } |
| 866 | |
| 867 | |
| 868 | if (track->mSource != NULL) { |
| 869 | track->mSource->stop(); |
| 870 | } |
| 871 | track->mSource = source; |
| 872 | track->mSource->start(); |
| 873 | track->mIndex = trackIndex; |
| 874 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 875 | int64_t timeUs, actualTimeUs; |
| 876 | const bool formatChange = true; |
Robert Shih | 5c67ddc | 2014-11-04 17:46:05 -0800 | [diff] [blame] | 877 | if (trackType == MEDIA_TRACK_TYPE_AUDIO) { |
| 878 | timeUs = mAudioLastDequeueTimeUs; |
| 879 | } else { |
| 880 | timeUs = mVideoLastDequeueTimeUs; |
| 881 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 882 | readBuffer(trackType, timeUs, &actualTimeUs, formatChange); |
| 883 | readBuffer(counterpartType, -1, NULL, formatChange); |
| 884 | ALOGV("timeUs %lld actualTimeUs %lld", timeUs, actualTimeUs); |
| 885 | |
| 886 | break; |
| 887 | } |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 888 | |
| 889 | case kWhatStart: |
| 890 | case kWhatResume: |
| 891 | { |
| 892 | restartPollBuffering(); |
| 893 | break; |
| 894 | } |
| 895 | |
Chong Zhang | 2a3cc9a | 2014-08-21 17:48:26 -0700 | [diff] [blame] | 896 | case kWhatPollBuffering: |
| 897 | { |
| 898 | int32_t generation; |
| 899 | CHECK(msg->findInt32("generation", &generation)); |
| 900 | if (generation == mPollBufferingGeneration) { |
| 901 | onPollBuffering(); |
| 902 | } |
| 903 | break; |
| 904 | } |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 905 | |
| 906 | case kWhatGetFormat: |
| 907 | { |
| 908 | onGetFormatMeta(msg); |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | case kWhatGetSelectedTrack: |
| 913 | { |
| 914 | onGetSelectedTrack(msg); |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | case kWhatSelectTrack: |
| 919 | { |
| 920 | onSelectTrack(msg); |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | case kWhatSeek: |
| 925 | { |
| 926 | onSeek(msg); |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | case kWhatReadBuffer: |
| 931 | { |
| 932 | onReadBuffer(msg); |
| 933 | break; |
| 934 | } |
| 935 | |
Lajos Molnar | 68fca63 | 2015-03-31 10:06:48 -0700 | [diff] [blame] | 936 | case kWhatSecureDecodersInstantiated: |
| 937 | { |
| 938 | int32_t err; |
| 939 | CHECK(msg->findInt32("err", &err)); |
| 940 | onSecureDecodersInstantiated(err); |
| 941 | break; |
| 942 | } |
| 943 | |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 944 | case kWhatStopWidevine: |
| 945 | { |
| 946 | // mStopRead is only used for Widevine to prevent the video source |
| 947 | // from being read while the associated video decoder is shutting down. |
| 948 | mStopRead = true; |
| 949 | if (mVideoTrack.mSource != NULL) { |
| 950 | mVideoTrack.mPackets->clear(); |
| 951 | } |
| 952 | sp<AMessage> response = new AMessage; |
| 953 | uint32_t replyID; |
| 954 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 955 | response->postReply(replyID); |
| 956 | break; |
| 957 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 958 | default: |
| 959 | Source::onMessageReceived(msg); |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 964 | void NuPlayer::GenericSource::fetchTextData( |
| 965 | uint32_t sendWhat, |
| 966 | media_track_type type, |
| 967 | int32_t curGen, |
| 968 | sp<AnotherPacketSource> packets, |
| 969 | sp<AMessage> msg) { |
| 970 | int32_t msgGeneration; |
| 971 | CHECK(msg->findInt32("generation", &msgGeneration)); |
| 972 | if (msgGeneration != curGen) { |
| 973 | // stale |
| 974 | return; |
| 975 | } |
| 976 | |
| 977 | int32_t avail; |
| 978 | if (packets->hasBufferAvailable(&avail)) { |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | int64_t timeUs; |
| 983 | CHECK(msg->findInt64("timeUs", &timeUs)); |
| 984 | |
| 985 | int64_t subTimeUs; |
| 986 | readBuffer(type, timeUs, &subTimeUs); |
| 987 | |
| 988 | int64_t delayUs = subTimeUs - timeUs; |
| 989 | if (msg->what() == kWhatFetchSubtitleData) { |
| 990 | const int64_t oneSecUs = 1000000ll; |
| 991 | delayUs -= oneSecUs; |
| 992 | } |
| 993 | sp<AMessage> msg2 = new AMessage(sendWhat, id()); |
| 994 | msg2->setInt32("generation", msgGeneration); |
| 995 | msg2->post(delayUs < 0 ? 0 : delayUs); |
| 996 | } |
| 997 | |
| 998 | void NuPlayer::GenericSource::sendTextData( |
| 999 | uint32_t what, |
| 1000 | media_track_type type, |
| 1001 | int32_t curGen, |
| 1002 | sp<AnotherPacketSource> packets, |
| 1003 | sp<AMessage> msg) { |
| 1004 | int32_t msgGeneration; |
| 1005 | CHECK(msg->findInt32("generation", &msgGeneration)); |
| 1006 | if (msgGeneration != curGen) { |
| 1007 | // stale |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | int64_t subTimeUs; |
| 1012 | if (packets->nextBufferTime(&subTimeUs) != OK) { |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | int64_t nextSubTimeUs; |
| 1017 | readBuffer(type, -1, &nextSubTimeUs); |
| 1018 | |
| 1019 | sp<ABuffer> buffer; |
| 1020 | status_t dequeueStatus = packets->dequeueAccessUnit(&buffer); |
| 1021 | if (dequeueStatus == OK) { |
| 1022 | sp<AMessage> notify = dupNotify(); |
| 1023 | notify->setInt32("what", what); |
| 1024 | notify->setBuffer("buffer", buffer); |
| 1025 | notify->post(); |
| 1026 | |
| 1027 | const int64_t delayUs = nextSubTimeUs - subTimeUs; |
| 1028 | msg->post(delayUs < 0 ? 0 : delayUs); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 1032 | sp<MetaData> NuPlayer::GenericSource::getFormatMeta(bool audio) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1033 | sp<AMessage> msg = new AMessage(kWhatGetFormat, id()); |
| 1034 | msg->setInt32("audio", audio); |
| 1035 | |
| 1036 | sp<AMessage> response; |
| 1037 | void *format; |
| 1038 | status_t err = msg->postAndAwaitResponse(&response); |
| 1039 | if (err == OK && response != NULL) { |
| 1040 | CHECK(response->findPointer("format", &format)); |
| 1041 | return (MetaData *)format; |
| 1042 | } else { |
| 1043 | return NULL; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void NuPlayer::GenericSource::onGetFormatMeta(sp<AMessage> msg) const { |
| 1048 | int32_t audio; |
| 1049 | CHECK(msg->findInt32("audio", &audio)); |
| 1050 | |
| 1051 | sp<AMessage> response = new AMessage; |
| 1052 | sp<MetaData> format = doGetFormatMeta(audio); |
| 1053 | response->setPointer("format", format.get()); |
| 1054 | |
| 1055 | uint32_t replyID; |
| 1056 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 1057 | response->postReply(replyID); |
| 1058 | } |
| 1059 | |
| 1060 | sp<MetaData> NuPlayer::GenericSource::doGetFormatMeta(bool audio) const { |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1061 | sp<MediaSource> source = audio ? mAudioTrack.mSource : mVideoTrack.mSource; |
| 1062 | |
| 1063 | if (source == NULL) { |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | |
| 1067 | return source->getFormat(); |
| 1068 | } |
| 1069 | |
| 1070 | status_t NuPlayer::GenericSource::dequeueAccessUnit( |
| 1071 | bool audio, sp<ABuffer> *accessUnit) { |
| 1072 | Track *track = audio ? &mAudioTrack : &mVideoTrack; |
| 1073 | |
| 1074 | if (track->mSource == NULL) { |
| 1075 | return -EWOULDBLOCK; |
| 1076 | } |
| 1077 | |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 1078 | if (mIsWidevine && !audio) { |
| 1079 | // try to read a buffer as we may not have been able to the last time |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1080 | postReadBuffer(MEDIA_TRACK_TYPE_VIDEO); |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1083 | status_t finalResult; |
| 1084 | if (!track->mPackets->hasBufferAvailable(&finalResult)) { |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 1085 | if (finalResult == OK) { |
| 1086 | postReadBuffer( |
| 1087 | audio ? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); |
| 1088 | return -EWOULDBLOCK; |
| 1089 | } |
| 1090 | return finalResult; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | status_t result = track->mPackets->dequeueAccessUnit(accessUnit); |
| 1094 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1095 | if (!track->mPackets->hasBufferAvailable(&finalResult)) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1096 | postReadBuffer(audio? MEDIA_TRACK_TYPE_AUDIO : MEDIA_TRACK_TYPE_VIDEO); |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1099 | if (result != OK) { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1100 | if (mSubtitleTrack.mSource != NULL) { |
| 1101 | mSubtitleTrack.mPackets->clear(); |
| 1102 | mFetchSubtitleDataGeneration++; |
| 1103 | } |
| 1104 | if (mTimedTextTrack.mSource != NULL) { |
| 1105 | mTimedTextTrack.mPackets->clear(); |
| 1106 | mFetchTimedTextDataGeneration++; |
| 1107 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1108 | return result; |
| 1109 | } |
| 1110 | |
| 1111 | int64_t timeUs; |
| 1112 | status_t eosResult; // ignored |
| 1113 | CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); |
Robert Shih | 5c67ddc | 2014-11-04 17:46:05 -0800 | [diff] [blame] | 1114 | if (audio) { |
| 1115 | mAudioLastDequeueTimeUs = timeUs; |
| 1116 | } else { |
| 1117 | mVideoLastDequeueTimeUs = timeUs; |
| 1118 | } |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1119 | |
| 1120 | if (mSubtitleTrack.mSource != NULL |
| 1121 | && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1122 | sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id()); |
| 1123 | msg->setInt64("timeUs", timeUs); |
| 1124 | msg->setInt32("generation", mFetchSubtitleDataGeneration); |
| 1125 | msg->post(); |
| 1126 | } |
Robert Shih | eb1735e | 2014-07-23 15:53:14 -0700 | [diff] [blame] | 1127 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1128 | if (mTimedTextTrack.mSource != NULL |
| 1129 | && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 1130 | sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id()); |
| 1131 | msg->setInt64("timeUs", timeUs); |
| 1132 | msg->setInt32("generation", mFetchTimedTextDataGeneration); |
| 1133 | msg->post(); |
| 1134 | } |
| 1135 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1136 | return result; |
| 1137 | } |
| 1138 | |
| 1139 | status_t NuPlayer::GenericSource::getDuration(int64_t *durationUs) { |
| 1140 | *durationUs = mDurationUs; |
| 1141 | return OK; |
| 1142 | } |
| 1143 | |
Robert Shih | dd23572 | 2014-06-12 14:49:23 -0700 | [diff] [blame] | 1144 | size_t NuPlayer::GenericSource::getTrackCount() const { |
| 1145 | return mSources.size(); |
| 1146 | } |
| 1147 | |
| 1148 | sp<AMessage> NuPlayer::GenericSource::getTrackInfo(size_t trackIndex) const { |
| 1149 | size_t trackCount = mSources.size(); |
| 1150 | if (trackIndex >= trackCount) { |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | |
| 1154 | sp<AMessage> format = new AMessage(); |
| 1155 | sp<MetaData> meta = mSources.itemAt(trackIndex)->getFormat(); |
| 1156 | |
| 1157 | const char *mime; |
| 1158 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 1159 | |
| 1160 | int32_t trackType; |
| 1161 | if (!strncasecmp(mime, "video/", 6)) { |
| 1162 | trackType = MEDIA_TRACK_TYPE_VIDEO; |
| 1163 | } else if (!strncasecmp(mime, "audio/", 6)) { |
| 1164 | trackType = MEDIA_TRACK_TYPE_AUDIO; |
| 1165 | } else if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) { |
| 1166 | trackType = MEDIA_TRACK_TYPE_TIMEDTEXT; |
| 1167 | } else { |
| 1168 | trackType = MEDIA_TRACK_TYPE_UNKNOWN; |
| 1169 | } |
| 1170 | format->setInt32("type", trackType); |
| 1171 | |
| 1172 | const char *lang; |
| 1173 | if (!meta->findCString(kKeyMediaLanguage, &lang)) { |
| 1174 | lang = "und"; |
| 1175 | } |
| 1176 | format->setString("language", lang); |
| 1177 | |
| 1178 | if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
| 1179 | format->setString("mime", mime); |
| 1180 | |
| 1181 | int32_t isAutoselect = 1, isDefault = 0, isForced = 0; |
| 1182 | meta->findInt32(kKeyTrackIsAutoselect, &isAutoselect); |
| 1183 | meta->findInt32(kKeyTrackIsDefault, &isDefault); |
| 1184 | meta->findInt32(kKeyTrackIsForced, &isForced); |
| 1185 | |
| 1186 | format->setInt32("auto", !!isAutoselect); |
| 1187 | format->setInt32("default", !!isDefault); |
| 1188 | format->setInt32("forced", !!isForced); |
| 1189 | } |
| 1190 | |
| 1191 | return format; |
| 1192 | } |
| 1193 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1194 | ssize_t NuPlayer::GenericSource::getSelectedTrack(media_track_type type) const { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1195 | sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id()); |
| 1196 | msg->setInt32("type", type); |
| 1197 | |
| 1198 | sp<AMessage> response; |
| 1199 | int32_t index; |
| 1200 | status_t err = msg->postAndAwaitResponse(&response); |
| 1201 | if (err == OK && response != NULL) { |
| 1202 | CHECK(response->findInt32("index", &index)); |
| 1203 | return index; |
| 1204 | } else { |
| 1205 | return -1; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | void NuPlayer::GenericSource::onGetSelectedTrack(sp<AMessage> msg) const { |
| 1210 | int32_t tmpType; |
| 1211 | CHECK(msg->findInt32("type", &tmpType)); |
| 1212 | media_track_type type = (media_track_type)tmpType; |
| 1213 | |
| 1214 | sp<AMessage> response = new AMessage; |
| 1215 | ssize_t index = doGetSelectedTrack(type); |
| 1216 | response->setInt32("index", index); |
| 1217 | |
| 1218 | uint32_t replyID; |
| 1219 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 1220 | response->postReply(replyID); |
| 1221 | } |
| 1222 | |
| 1223 | ssize_t NuPlayer::GenericSource::doGetSelectedTrack(media_track_type type) const { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1224 | const Track *track = NULL; |
| 1225 | switch (type) { |
| 1226 | case MEDIA_TRACK_TYPE_VIDEO: |
| 1227 | track = &mVideoTrack; |
| 1228 | break; |
| 1229 | case MEDIA_TRACK_TYPE_AUDIO: |
| 1230 | track = &mAudioTrack; |
| 1231 | break; |
| 1232 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 1233 | track = &mTimedTextTrack; |
| 1234 | break; |
| 1235 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 1236 | track = &mSubtitleTrack; |
| 1237 | break; |
| 1238 | default: |
| 1239 | break; |
| 1240 | } |
| 1241 | |
| 1242 | if (track != NULL && track->mSource != NULL) { |
| 1243 | return track->mIndex; |
| 1244 | } |
| 1245 | |
| 1246 | return -1; |
| 1247 | } |
| 1248 | |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1249 | status_t NuPlayer::GenericSource::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1250 | ALOGV("%s track: %zu", select ? "select" : "deselect", trackIndex); |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1251 | sp<AMessage> msg = new AMessage(kWhatSelectTrack, id()); |
| 1252 | msg->setInt32("trackIndex", trackIndex); |
Robert Shih | da23ab9 | 2014-09-16 11:34:08 -0700 | [diff] [blame] | 1253 | msg->setInt32("select", select); |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1254 | msg->setInt64("timeUs", timeUs); |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1255 | |
| 1256 | sp<AMessage> response; |
| 1257 | status_t err = msg->postAndAwaitResponse(&response); |
| 1258 | if (err == OK && response != NULL) { |
| 1259 | CHECK(response->findInt32("err", &err)); |
| 1260 | } |
| 1261 | |
| 1262 | return err; |
| 1263 | } |
| 1264 | |
| 1265 | void NuPlayer::GenericSource::onSelectTrack(sp<AMessage> msg) { |
| 1266 | int32_t trackIndex, select; |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1267 | int64_t timeUs; |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1268 | CHECK(msg->findInt32("trackIndex", &trackIndex)); |
| 1269 | CHECK(msg->findInt32("select", &select)); |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1270 | CHECK(msg->findInt64("timeUs", &timeUs)); |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1271 | |
| 1272 | sp<AMessage> response = new AMessage; |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1273 | status_t err = doSelectTrack(trackIndex, select, timeUs); |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1274 | response->setInt32("err", err); |
| 1275 | |
| 1276 | uint32_t replyID; |
| 1277 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 1278 | response->postReply(replyID); |
| 1279 | } |
| 1280 | |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1281 | status_t NuPlayer::GenericSource::doSelectTrack(size_t trackIndex, bool select, int64_t timeUs) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1282 | if (trackIndex >= mSources.size()) { |
| 1283 | return BAD_INDEX; |
| 1284 | } |
| 1285 | |
| 1286 | if (!select) { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1287 | Track* track = NULL; |
| 1288 | if (mSubtitleTrack.mSource != NULL && trackIndex == mSubtitleTrack.mIndex) { |
| 1289 | track = &mSubtitleTrack; |
| 1290 | mFetchSubtitleDataGeneration++; |
| 1291 | } else if (mTimedTextTrack.mSource != NULL && trackIndex == mTimedTextTrack.mIndex) { |
| 1292 | track = &mTimedTextTrack; |
| 1293 | mFetchTimedTextDataGeneration++; |
| 1294 | } |
| 1295 | if (track == NULL) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1296 | return INVALID_OPERATION; |
| 1297 | } |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1298 | track->mSource->stop(); |
| 1299 | track->mSource = NULL; |
| 1300 | track->mPackets->clear(); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1301 | return OK; |
| 1302 | } |
| 1303 | |
| 1304 | const sp<MediaSource> source = mSources.itemAt(trackIndex); |
| 1305 | sp<MetaData> meta = source->getFormat(); |
| 1306 | const char *mime; |
| 1307 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 1308 | if (!strncasecmp(mime, "text/", 5)) { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1309 | bool isSubtitle = strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP); |
| 1310 | Track *track = isSubtitle ? &mSubtitleTrack : &mTimedTextTrack; |
| 1311 | if (track->mSource != NULL && track->mIndex == trackIndex) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1312 | return OK; |
| 1313 | } |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1314 | track->mIndex = trackIndex; |
| 1315 | if (track->mSource != NULL) { |
| 1316 | track->mSource->stop(); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1317 | } |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1318 | track->mSource = mSources.itemAt(trackIndex); |
| 1319 | track->mSource->start(); |
| 1320 | if (track->mPackets == NULL) { |
| 1321 | track->mPackets = new AnotherPacketSource(track->mSource->getFormat()); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1322 | } else { |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1323 | track->mPackets->clear(); |
| 1324 | track->mPackets->setFormat(track->mSource->getFormat()); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1325 | |
| 1326 | } |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1327 | |
| 1328 | if (isSubtitle) { |
| 1329 | mFetchSubtitleDataGeneration++; |
| 1330 | } else { |
| 1331 | mFetchTimedTextDataGeneration++; |
| 1332 | } |
| 1333 | |
Robert Shih | 6ffb1fd | 2014-10-29 16:24:32 -0700 | [diff] [blame] | 1334 | status_t eosResult; // ignored |
| 1335 | if (mSubtitleTrack.mSource != NULL |
| 1336 | && !mSubtitleTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 1337 | sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id()); |
| 1338 | msg->setInt64("timeUs", timeUs); |
| 1339 | msg->setInt32("generation", mFetchSubtitleDataGeneration); |
| 1340 | msg->post(); |
| 1341 | } |
| 1342 | |
| 1343 | if (mTimedTextTrack.mSource != NULL |
| 1344 | && !mTimedTextTrack.mPackets->hasBufferAvailable(&eosResult)) { |
| 1345 | sp<AMessage> msg = new AMessage(kWhatFetchTimedTextData, id()); |
| 1346 | msg->setInt64("timeUs", timeUs); |
| 1347 | msg->setInt32("generation", mFetchTimedTextDataGeneration); |
| 1348 | msg->post(); |
| 1349 | } |
| 1350 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1351 | return OK; |
| 1352 | } else if (!strncasecmp(mime, "audio/", 6) || !strncasecmp(mime, "video/", 6)) { |
| 1353 | bool audio = !strncasecmp(mime, "audio/", 6); |
| 1354 | Track *track = audio ? &mAudioTrack : &mVideoTrack; |
| 1355 | if (track->mSource != NULL && track->mIndex == trackIndex) { |
| 1356 | return OK; |
| 1357 | } |
| 1358 | |
| 1359 | sp<AMessage> msg = new AMessage(kWhatChangeAVSource, id()); |
| 1360 | msg->setInt32("trackIndex", trackIndex); |
| 1361 | msg->post(); |
| 1362 | return OK; |
| 1363 | } |
| 1364 | |
| 1365 | return INVALID_OPERATION; |
| 1366 | } |
| 1367 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1368 | status_t NuPlayer::GenericSource::seekTo(int64_t seekTimeUs) { |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1369 | sp<AMessage> msg = new AMessage(kWhatSeek, id()); |
| 1370 | msg->setInt64("seekTimeUs", seekTimeUs); |
| 1371 | |
| 1372 | sp<AMessage> response; |
| 1373 | status_t err = msg->postAndAwaitResponse(&response); |
| 1374 | if (err == OK && response != NULL) { |
| 1375 | CHECK(response->findInt32("err", &err)); |
| 1376 | } |
| 1377 | |
| 1378 | return err; |
| 1379 | } |
| 1380 | |
| 1381 | void NuPlayer::GenericSource::onSeek(sp<AMessage> msg) { |
| 1382 | int64_t seekTimeUs; |
| 1383 | CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); |
| 1384 | |
| 1385 | sp<AMessage> response = new AMessage; |
| 1386 | status_t err = doSeek(seekTimeUs); |
| 1387 | response->setInt32("err", err); |
| 1388 | |
| 1389 | uint32_t replyID; |
| 1390 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 1391 | response->postReply(replyID); |
| 1392 | } |
| 1393 | |
| 1394 | status_t NuPlayer::GenericSource::doSeek(int64_t seekTimeUs) { |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 1395 | // If the Widevine source is stopped, do not attempt to read any |
| 1396 | // more buffers. |
| 1397 | if (mStopRead) { |
| 1398 | return INVALID_OPERATION; |
| 1399 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1400 | if (mVideoTrack.mSource != NULL) { |
| 1401 | int64_t actualTimeUs; |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1402 | readBuffer(MEDIA_TRACK_TYPE_VIDEO, seekTimeUs, &actualTimeUs); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1403 | |
| 1404 | seekTimeUs = actualTimeUs; |
Robert Shih | 5c67ddc | 2014-11-04 17:46:05 -0800 | [diff] [blame] | 1405 | mVideoLastDequeueTimeUs = seekTimeUs; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | if (mAudioTrack.mSource != NULL) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1409 | readBuffer(MEDIA_TRACK_TYPE_AUDIO, seekTimeUs); |
Robert Shih | 5c67ddc | 2014-11-04 17:46:05 -0800 | [diff] [blame] | 1410 | mAudioLastDequeueTimeUs = seekTimeUs; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 1413 | setDrmPlaybackStatusIfNeeded(Playback::START, seekTimeUs / 1000); |
| 1414 | if (!mStarted) { |
| 1415 | setDrmPlaybackStatusIfNeeded(Playback::PAUSE, 0); |
| 1416 | } |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 1417 | |
| 1418 | // If currently buffering, post kWhatBufferingEnd first, so that |
| 1419 | // NuPlayer resumes. Otherwise, if cache hits high watermark |
| 1420 | // before new polling happens, no one will resume the playback. |
| 1421 | stopBufferingIfNecessary(); |
| 1422 | restartPollBuffering(); |
| 1423 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1424 | return OK; |
| 1425 | } |
| 1426 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1427 | sp<ABuffer> NuPlayer::GenericSource::mediaBufferToABuffer( |
| 1428 | MediaBuffer* mb, |
| 1429 | media_track_type trackType, |
Wei Jia | 474d7c7 | 2014-12-04 15:12:13 -0800 | [diff] [blame] | 1430 | int64_t /* seekTimeUs */, |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1431 | int64_t *actualTimeUs) { |
| 1432 | bool audio = trackType == MEDIA_TRACK_TYPE_AUDIO; |
| 1433 | size_t outLength = mb->range_length(); |
| 1434 | |
| 1435 | if (audio && mAudioIsVorbis) { |
| 1436 | outLength += sizeof(int32_t); |
| 1437 | } |
| 1438 | |
| 1439 | sp<ABuffer> ab; |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 1440 | if (mIsSecure && !audio) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1441 | // data is already provided in the buffer |
| 1442 | ab = new ABuffer(NULL, mb->range_length()); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1443 | mb->add_ref(); |
Wei Jia | 96e92b5 | 2014-09-18 17:36:20 -0700 | [diff] [blame] | 1444 | ab->setMediaBufferBase(mb); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1445 | } else { |
| 1446 | ab = new ABuffer(outLength); |
| 1447 | memcpy(ab->data(), |
| 1448 | (const uint8_t *)mb->data() + mb->range_offset(), |
| 1449 | mb->range_length()); |
| 1450 | } |
| 1451 | |
| 1452 | if (audio && mAudioIsVorbis) { |
| 1453 | int32_t numPageSamples; |
| 1454 | if (!mb->meta_data()->findInt32(kKeyValidSamples, &numPageSamples)) { |
| 1455 | numPageSamples = -1; |
| 1456 | } |
| 1457 | |
| 1458 | uint8_t* abEnd = ab->data() + mb->range_length(); |
| 1459 | memcpy(abEnd, &numPageSamples, sizeof(numPageSamples)); |
| 1460 | } |
| 1461 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1462 | sp<AMessage> meta = ab->meta(); |
| 1463 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1464 | int64_t timeUs; |
| 1465 | CHECK(mb->meta_data()->findInt64(kKeyTime, &timeUs)); |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1466 | meta->setInt64("timeUs", timeUs); |
| 1467 | |
Wei Jia | 474d7c7 | 2014-12-04 15:12:13 -0800 | [diff] [blame] | 1468 | #if 0 |
| 1469 | // Temporarily disable pre-roll till we have a full solution to handle |
| 1470 | // both single seek and continous seek gracefully. |
| 1471 | if (seekTimeUs > timeUs) { |
| 1472 | sp<AMessage> extra = new AMessage; |
| 1473 | extra->setInt64("resume-at-mediaTimeUs", seekTimeUs); |
| 1474 | meta->setMessage("extra", extra); |
| 1475 | } |
| 1476 | #endif |
| 1477 | |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1478 | if (trackType == MEDIA_TRACK_TYPE_TIMEDTEXT) { |
| 1479 | const char *mime; |
| 1480 | CHECK(mTimedTextTrack.mSource != NULL |
| 1481 | && mTimedTextTrack.mSource->getFormat()->findCString(kKeyMIMEType, &mime)); |
| 1482 | meta->setString("mime", mime); |
| 1483 | } |
| 1484 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1485 | int64_t durationUs; |
| 1486 | if (mb->meta_data()->findInt64(kKeyDuration, &durationUs)) { |
| 1487 | meta->setInt64("durationUs", durationUs); |
| 1488 | } |
| 1489 | |
| 1490 | if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { |
| 1491 | meta->setInt32("trackIndex", mSubtitleTrack.mIndex); |
| 1492 | } |
| 1493 | |
| 1494 | if (actualTimeUs) { |
| 1495 | *actualTimeUs = timeUs; |
| 1496 | } |
| 1497 | |
| 1498 | mb->release(); |
| 1499 | mb = NULL; |
| 1500 | |
| 1501 | return ab; |
| 1502 | } |
| 1503 | |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1504 | void NuPlayer::GenericSource::postReadBuffer(media_track_type trackType) { |
Lajos Molnar | 84f5278 | 2014-09-11 10:01:55 -0700 | [diff] [blame] | 1505 | Mutex::Autolock _l(mReadBufferLock); |
| 1506 | |
| 1507 | if ((mPendingReadBufferTypes & (1 << trackType)) == 0) { |
| 1508 | mPendingReadBufferTypes |= (1 << trackType); |
| 1509 | sp<AMessage> msg = new AMessage(kWhatReadBuffer, id()); |
| 1510 | msg->setInt32("trackType", trackType); |
| 1511 | msg->post(); |
| 1512 | } |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | void NuPlayer::GenericSource::onReadBuffer(sp<AMessage> msg) { |
| 1516 | int32_t tmpType; |
| 1517 | CHECK(msg->findInt32("trackType", &tmpType)); |
| 1518 | media_track_type trackType = (media_track_type)tmpType; |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 1519 | readBuffer(trackType); |
Lajos Molnar | 84f5278 | 2014-09-11 10:01:55 -0700 | [diff] [blame] | 1520 | { |
| 1521 | // only protect the variable change, as readBuffer may |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 1522 | // take considerable time. |
Lajos Molnar | 84f5278 | 2014-09-11 10:01:55 -0700 | [diff] [blame] | 1523 | Mutex::Autolock _l(mReadBufferLock); |
| 1524 | mPendingReadBufferTypes &= ~(1 << trackType); |
| 1525 | } |
Robert Shih | 17f6dd6 | 2014-08-20 17:00:21 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1528 | void NuPlayer::GenericSource::readBuffer( |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1529 | media_track_type trackType, int64_t seekTimeUs, int64_t *actualTimeUs, bool formatChange) { |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 1530 | // Do not read data if Widevine source is stopped |
| 1531 | if (mStopRead) { |
| 1532 | return; |
| 1533 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1534 | Track *track; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1535 | size_t maxBuffers = 1; |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1536 | switch (trackType) { |
| 1537 | case MEDIA_TRACK_TYPE_VIDEO: |
| 1538 | track = &mVideoTrack; |
Jeff Tinker | a28785a | 2014-09-23 22:24:26 -0700 | [diff] [blame] | 1539 | if (mIsWidevine) { |
| 1540 | maxBuffers = 2; |
| 1541 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1542 | break; |
| 1543 | case MEDIA_TRACK_TYPE_AUDIO: |
| 1544 | track = &mAudioTrack; |
Jeff Tinker | a28785a | 2014-09-23 22:24:26 -0700 | [diff] [blame] | 1545 | if (mIsWidevine) { |
| 1546 | maxBuffers = 8; |
| 1547 | } else { |
| 1548 | maxBuffers = 64; |
| 1549 | } |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1550 | break; |
| 1551 | case MEDIA_TRACK_TYPE_SUBTITLE: |
| 1552 | track = &mSubtitleTrack; |
| 1553 | break; |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1554 | case MEDIA_TRACK_TYPE_TIMEDTEXT: |
| 1555 | track = &mTimedTextTrack; |
| 1556 | break; |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1557 | default: |
| 1558 | TRESPASS(); |
| 1559 | } |
| 1560 | |
| 1561 | if (track->mSource == NULL) { |
| 1562 | return; |
| 1563 | } |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1564 | |
| 1565 | if (actualTimeUs) { |
| 1566 | *actualTimeUs = seekTimeUs; |
| 1567 | } |
| 1568 | |
| 1569 | MediaSource::ReadOptions options; |
| 1570 | |
| 1571 | bool seeking = false; |
| 1572 | |
| 1573 | if (seekTimeUs >= 0) { |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1574 | options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1575 | seeking = true; |
| 1576 | } |
| 1577 | |
Chong Zhang | 42e8153 | 2014-12-01 13:44:26 -0800 | [diff] [blame] | 1578 | if (mIsWidevine) { |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 1579 | options.setNonBlocking(); |
| 1580 | } |
| 1581 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1582 | for (size_t numBuffers = 0; numBuffers < maxBuffers; ) { |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1583 | MediaBuffer *mbuf; |
| 1584 | status_t err = track->mSource->read(&mbuf, &options); |
| 1585 | |
| 1586 | options.clearSeekTo(); |
| 1587 | |
| 1588 | if (err == OK) { |
Ronghua Wu | 8027687 | 2014-08-28 15:50:29 -0700 | [diff] [blame] | 1589 | int64_t timeUs; |
| 1590 | CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs)); |
| 1591 | if (trackType == MEDIA_TRACK_TYPE_AUDIO) { |
| 1592 | mAudioTimeUs = timeUs; |
| 1593 | } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { |
| 1594 | mVideoTimeUs = timeUs; |
| 1595 | } |
| 1596 | |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1597 | // formatChange && seeking: track whose source is changed during selection |
| 1598 | // formatChange && !seeking: track whose source is not changed during selection |
| 1599 | // !formatChange: normal seek |
Lajos Molnar | e26940f | 2014-07-31 10:31:26 -0700 | [diff] [blame] | 1600 | if ((seeking || formatChange) |
| 1601 | && (trackType == MEDIA_TRACK_TYPE_AUDIO |
| 1602 | || trackType == MEDIA_TRACK_TYPE_VIDEO)) { |
Wei Jia | fef808d | 2014-10-31 17:57:05 -0700 | [diff] [blame] | 1603 | ATSParser::DiscontinuityType type = (formatChange && seeking) |
| 1604 | ? ATSParser::DISCONTINUITY_FORMATCHANGE |
| 1605 | : ATSParser::DISCONTINUITY_NONE; |
Robert Shih | 3423bbd | 2014-07-16 15:47:09 -0700 | [diff] [blame] | 1606 | track->mPackets->queueDiscontinuity( type, NULL, true /* discard */); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Wei Jia | 474d7c7 | 2014-12-04 15:12:13 -0800 | [diff] [blame] | 1609 | sp<ABuffer> buffer = mediaBufferToABuffer( |
| 1610 | mbuf, trackType, seekTimeUs, actualTimeUs); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1611 | track->mPackets->queueAccessUnit(buffer); |
Marco Nelissen | 317a49a | 2014-09-16 21:32:33 -0700 | [diff] [blame] | 1612 | formatChange = false; |
| 1613 | seeking = false; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 1614 | ++numBuffers; |
Lajos Molnar | cc22703 | 2014-07-17 15:33:06 -0700 | [diff] [blame] | 1615 | } else if (err == WOULD_BLOCK) { |
| 1616 | break; |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1617 | } else if (err == INFO_FORMAT_CHANGED) { |
| 1618 | #if 0 |
| 1619 | track->mPackets->queueDiscontinuity( |
Chong Zhang | 632740c | 2014-06-26 13:03:47 -0700 | [diff] [blame] | 1620 | ATSParser::DISCONTINUITY_FORMATCHANGE, |
| 1621 | NULL, |
| 1622 | false /* discard */); |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1623 | #endif |
| 1624 | } else { |
| 1625 | track->mPackets->signalEOS(err); |
| 1626 | break; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | |
Andreas Huber | afed0e1 | 2011-09-20 15:39:58 -0700 | [diff] [blame] | 1631 | } // namespace android |