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