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