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