Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "StreamingSource" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "StreamingSource.h" |
| 22 | |
| 23 | #include "ATSParser.h" |
| 24 | #include "AnotherPacketSource.h" |
| 25 | #include "NuPlayerStreamListener.h" |
| 26 | |
Marco Nelissen | 7291da6 | 2019-12-17 13:01:55 -0800 | [diff] [blame] | 27 | #include <media/stagefright/MediaSource.h> |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 28 | #include <media/stagefright/foundation/ABuffer.h> |
| 29 | #include <media/stagefright/foundation/ADebug.h> |
| 30 | #include <media/stagefright/foundation/AMessage.h> |
Dongwon Kang | 15d02f8 | 2017-12-14 16:32:18 -0800 | [diff] [blame] | 31 | #include <media/stagefright/foundation/MediaKeys.h> |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 32 | #include <media/stagefright/MetaData.h> |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 33 | #include <media/stagefright/Utils.h> |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
osamu fujita | 8cf4ced | 2015-10-06 14:20:02 +0900 | [diff] [blame] | 37 | const int32_t kNumListenerQueuePackets = 80; |
| 38 | |
Andreas Huber | b5f25f0 | 2013-02-05 10:14:26 -0800 | [diff] [blame] | 39 | NuPlayer::StreamingSource::StreamingSource( |
| 40 | const sp<AMessage> ¬ify, |
| 41 | const sp<IStreamSource> &source) |
| 42 | : Source(notify), |
| 43 | mSource(source), |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 44 | mFinalResult(OK), |
| 45 | mBuffering(false) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | NuPlayer::StreamingSource::~StreamingSource() { |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 49 | if (mLooper != NULL) { |
| 50 | mLooper->unregisterHandler(id()); |
| 51 | mLooper->stop(); |
| 52 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 55 | status_t NuPlayer::StreamingSource::getBufferingSettings( |
Wei Jia | 48fa06d | 2016-12-20 15:30:49 -0800 | [diff] [blame] | 56 | BufferingSettings *buffering /* nonnull */) { |
| 57 | *buffering = BufferingSettings(); |
| 58 | return OK; |
| 59 | } |
| 60 | |
| 61 | status_t NuPlayer::StreamingSource::setBufferingSettings( |
Wei Jia | 9bb3803 | 2017-03-23 18:00:38 -0700 | [diff] [blame] | 62 | const BufferingSettings & /* buffering */) { |
Wei Jia | 48fa06d | 2016-12-20 15:30:49 -0800 | [diff] [blame] | 63 | return OK; |
| 64 | } |
| 65 | |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 66 | void NuPlayer::StreamingSource::prepareAsync() { |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 67 | if (mLooper == NULL) { |
| 68 | mLooper = new ALooper; |
| 69 | mLooper->setName("streaming"); |
| 70 | mLooper->start(); |
| 71 | |
| 72 | mLooper->registerHandler(this); |
| 73 | } |
| 74 | |
Chong Zhang | ced1c2f | 2014-08-08 15:22:35 -0700 | [diff] [blame] | 75 | notifyVideoSizeChanged(); |
Andreas Huber | 9575c96 | 2013-02-05 13:59:56 -0800 | [diff] [blame] | 76 | notifyFlagsChanged(0); |
| 77 | notifyPrepared(); |
| 78 | } |
| 79 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 80 | void NuPlayer::StreamingSource::start() { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 81 | mStreamListener = new NuPlayerStreamListener(mSource, NULL); |
Andrew Lewis | aad519d | 2020-05-20 16:49:23 +0100 | [diff] [blame] | 82 | mSource->setListener(mStreamListener); |
Andreas Huber | 87f2a55 | 2012-08-31 13:55:24 -0700 | [diff] [blame] | 83 | |
| 84 | uint32_t sourceFlags = mSource->flags(); |
| 85 | |
| 86 | uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE; |
| 87 | if (sourceFlags & IStreamSource::kFlagAlignedVideoData) { |
| 88 | parserFlags |= ATSParser::ALIGNED_VIDEO_DATA; |
| 89 | } |
| 90 | |
| 91 | mTSParser = new ATSParser(parserFlags); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 92 | |
| 93 | mStreamListener->start(); |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 94 | |
| 95 | postReadBuffer(); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 98 | status_t NuPlayer::StreamingSource::feedMoreTSData() { |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 99 | return postReadBuffer(); |
| 100 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 101 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 102 | void NuPlayer::StreamingSource::onReadBuffer() { |
osamu fujita | 8cf4ced | 2015-10-06 14:20:02 +0900 | [diff] [blame] | 103 | for (int32_t i = 0; i < kNumListenerQueuePackets; ++i) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 104 | char buffer[188]; |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 105 | sp<AMessage> extra; |
| 106 | ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 107 | |
| 108 | if (n == 0) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 109 | ALOGI("input data EOS reached."); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 110 | mTSParser->signalEOS(ERROR_END_OF_STREAM); |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 111 | setError(ERROR_END_OF_STREAM); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 112 | break; |
| 113 | } else if (n == INFO_DISCONTINUITY) { |
Wei Jia | fef808d | 2014-10-31 17:57:05 -0700 | [diff] [blame] | 114 | int32_t type = ATSParser::DISCONTINUITY_TIME; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 115 | |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 116 | int32_t mask; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 117 | if (extra != NULL |
| 118 | && extra->findInt32( |
Dongwon Kang | 15d02f8 | 2017-12-14 16:32:18 -0800 | [diff] [blame] | 119 | kIStreamListenerKeyDiscontinuityMask, &mask)) { |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 120 | if (mask == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 121 | ALOGE("Client specified an illegal discontinuity type."); |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 122 | setError(ERROR_UNSUPPORTED); |
| 123 | break; |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | type = mask; |
Andreas Huber | 42e549e | 2011-07-13 09:36:11 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Andreas Huber | bfcc8d8 | 2011-11-29 11:57:35 -0800 | [diff] [blame] | 129 | mTSParser->signalDiscontinuity( |
| 130 | (ATSParser::DiscontinuityType)type, extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 131 | } else if (n < 0) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 132 | break; |
| 133 | } else { |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 134 | if (buffer[0] == 0x00) { // OK to access buffer[0] since n must be > 0 here |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 135 | // XXX legacy |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 136 | |
| 137 | if (extra == NULL) { |
| 138 | extra = new AMessage; |
| 139 | } |
| 140 | |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 141 | uint8_t type = 0; |
| 142 | if (n > 1) { |
| 143 | type = buffer[1]; |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 144 | |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 145 | if ((type & 2) && (n >= 2 + sizeof(int64_t))) { |
| 146 | int64_t mediaTimeUs; |
| 147 | memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs)); |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 148 | |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 149 | extra->setInt64(kATSParserKeyMediaTimeUs, mediaTimeUs); |
| 150 | } |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 153 | mTSParser->signalDiscontinuity( |
Andreas Huber | b7c8e91 | 2012-11-27 15:02:53 -0800 | [diff] [blame] | 154 | ((type & 1) == 0) |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 155 | ? ATSParser::DISCONTINUITY_TIME |
| 156 | : ATSParser::DISCONTINUITY_FORMATCHANGE, |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 157 | extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 158 | } else { |
Chong Zhang | 2e26d81 | 2019-09-09 13:20:10 -0700 | [diff] [blame] | 159 | status_t err = mTSParser->feedTSPacket(buffer, n); |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 160 | |
| 161 | if (err != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 162 | ALOGE("TS Parser returned error %d", err); |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 163 | |
| 164 | mTSParser->signalEOS(err); |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 165 | setError(err); |
Andreas Huber | 06528d7 | 2011-08-31 16:29:05 -0700 | [diff] [blame] | 166 | break; |
| 167 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 171 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 172 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 173 | status_t NuPlayer::StreamingSource::postReadBuffer() { |
| 174 | { |
| 175 | Mutex::Autolock _l(mBufferingLock); |
| 176 | if (mFinalResult != OK) { |
| 177 | return mFinalResult; |
| 178 | } |
| 179 | if (mBuffering) { |
| 180 | return OK; |
| 181 | } |
| 182 | mBuffering = true; |
| 183 | } |
| 184 | |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 185 | (new AMessage(kWhatReadBuffer, this))->post(); |
Andreas Huber | eac68ba | 2011-09-27 12:12:25 -0700 | [diff] [blame] | 186 | return OK; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 189 | bool NuPlayer::StreamingSource::haveSufficientDataOnAllTracks() { |
| 190 | // We're going to buffer at least 2 secs worth data on all tracks before |
| 191 | // starting playback (both at startup and after a seek). |
| 192 | |
Chih-Hung Hsieh | 62309d5 | 2018-12-11 13:54:02 -0800 | [diff] [blame] | 193 | static const int64_t kMinDurationUs = 2000000LL; |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 194 | |
| 195 | sp<AnotherPacketSource> audioTrack = getSource(true /*audio*/); |
| 196 | sp<AnotherPacketSource> videoTrack = getSource(false /*audio*/); |
| 197 | |
| 198 | status_t err; |
| 199 | int64_t durationUs; |
| 200 | if (audioTrack != NULL |
| 201 | && (durationUs = audioTrack->getBufferedDurationUs(&err)) |
| 202 | < kMinDurationUs |
| 203 | && err == OK) { |
| 204 | ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)", |
| 205 | durationUs / 1E6); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (videoTrack != NULL |
| 210 | && (durationUs = videoTrack->getBufferedDurationUs(&err)) |
| 211 | < kMinDurationUs |
| 212 | && err == OK) { |
| 213 | ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)", |
| 214 | durationUs / 1E6); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void NuPlayer::StreamingSource::setError(status_t err) { |
| 222 | Mutex::Autolock _l(mBufferingLock); |
| 223 | mFinalResult = err; |
| 224 | } |
| 225 | |
| 226 | sp<AnotherPacketSource> NuPlayer::StreamingSource::getSource(bool audio) { |
Wei Jia | ab05b4c | 2014-12-02 09:41:21 -0800 | [diff] [blame] | 227 | if (mTSParser == NULL) { |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 231 | sp<MediaSource> source = mTSParser->getSource( |
| 232 | audio ? ATSParser::AUDIO : ATSParser::VIDEO); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 233 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 234 | return static_cast<AnotherPacketSource *>(source.get()); |
| 235 | } |
| 236 | |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 237 | sp<AMessage> NuPlayer::StreamingSource::getFormat(bool audio) { |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 238 | sp<AnotherPacketSource> source = getSource(audio); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 239 | |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 240 | sp<AMessage> format = new AMessage; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 241 | if (source == NULL) { |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 242 | format->setInt32("err", -EWOULDBLOCK); |
| 243 | return format; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 246 | sp<MetaData> meta = source->getFormat(); |
Roger1 Jonsson | c3fe699 | 2016-09-06 08:51:46 +0200 | [diff] [blame] | 247 | if (meta == NULL) { |
| 248 | format->setInt32("err", -EWOULDBLOCK); |
| 249 | return format; |
| 250 | } |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 251 | status_t err = convertMetaDataToMessage(meta, &format); |
Lajos Molnar | 5bd99f9 | 2016-08-10 10:30:07 -0700 | [diff] [blame] | 252 | if (err != OK) { // format may have been cleared on error |
Wei Jia | 9737d34 | 2016-12-28 14:59:59 -0800 | [diff] [blame] | 253 | return NULL; |
Robert Shih | 093024b | 2016-05-12 13:45:56 -0700 | [diff] [blame] | 254 | } |
| 255 | return format; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | status_t NuPlayer::StreamingSource::dequeueAccessUnit( |
| 259 | bool audio, sp<ABuffer> *accessUnit) { |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 260 | sp<AnotherPacketSource> source = getSource(audio); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 261 | |
| 262 | if (source == NULL) { |
| 263 | return -EWOULDBLOCK; |
| 264 | } |
| 265 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 266 | if (!haveSufficientDataOnAllTracks()) { |
| 267 | postReadBuffer(); |
| 268 | } |
| 269 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 270 | status_t finalResult; |
| 271 | if (!source->hasBufferAvailable(&finalResult)) { |
| 272 | return finalResult == OK ? -EWOULDBLOCK : finalResult; |
| 273 | } |
| 274 | |
Andreas Huber | 87f2a55 | 2012-08-31 13:55:24 -0700 | [diff] [blame] | 275 | status_t err = source->dequeueAccessUnit(accessUnit); |
| 276 | |
| 277 | #if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0 |
| 278 | if (err == OK) { |
| 279 | int64_t timeUs; |
| 280 | CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); |
| 281 | ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs); |
| 282 | } |
| 283 | #endif |
| 284 | |
| 285 | return err; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Andreas Huber | d5e5623 | 2013-03-12 11:01:43 -0700 | [diff] [blame] | 288 | bool NuPlayer::StreamingSource::isRealTime() const { |
| 289 | return mSource->flags() & IStreamSource::kFlagIsRealTimeData; |
| 290 | } |
| 291 | |
Chong Zhang | 180d1b9 | 2014-12-02 18:35:35 -0800 | [diff] [blame] | 292 | void NuPlayer::StreamingSource::onMessageReceived( |
| 293 | const sp<AMessage> &msg) { |
| 294 | switch (msg->what()) { |
| 295 | case kWhatReadBuffer: |
| 296 | { |
| 297 | onReadBuffer(); |
| 298 | |
| 299 | { |
| 300 | Mutex::Autolock _l(mBufferingLock); |
| 301 | mBuffering = false; |
| 302 | } |
| 303 | break; |
| 304 | } |
| 305 | default: |
| 306 | { |
| 307 | TRESPASS(); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 313 | } // namespace android |
| 314 | |