Wei Jia | 53692fa | 2017-12-11 10:33:46 -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 "NuPlayer2StreamListener.h" |
| 26 | |
| 27 | #include <media/MediaSource.h> |
| 28 | #include <media/stagefright/foundation/ABuffer.h> |
| 29 | #include <media/stagefright/foundation/ADebug.h> |
| 30 | #include <media/stagefright/foundation/AMessage.h> |
| 31 | #include <media/stagefright/MetaData.h> |
| 32 | #include <media/stagefright/Utils.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | const int32_t kNumListenerQueuePackets = 80; |
| 37 | |
| 38 | NuPlayer2::StreamingSource::StreamingSource( |
| 39 | const sp<AMessage> ¬ify, |
| 40 | const sp<IStreamSource> &source) |
| 41 | : Source(notify), |
| 42 | mSource(source), |
| 43 | mFinalResult(OK), |
| 44 | mBuffering(false) { |
| 45 | } |
| 46 | |
| 47 | NuPlayer2::StreamingSource::~StreamingSource() { |
| 48 | if (mLooper != NULL) { |
| 49 | mLooper->unregisterHandler(id()); |
| 50 | mLooper->stop(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | status_t NuPlayer2::StreamingSource::getBufferingSettings( |
| 55 | BufferingSettings *buffering /* nonnull */) { |
| 56 | *buffering = BufferingSettings(); |
| 57 | return OK; |
| 58 | } |
| 59 | |
| 60 | status_t NuPlayer2::StreamingSource::setBufferingSettings( |
| 61 | const BufferingSettings & /* buffering */) { |
| 62 | return OK; |
| 63 | } |
| 64 | |
| 65 | void NuPlayer2::StreamingSource::prepareAsync() { |
| 66 | if (mLooper == NULL) { |
| 67 | mLooper = new ALooper; |
| 68 | mLooper->setName("streaming"); |
| 69 | mLooper->start(); |
| 70 | |
| 71 | mLooper->registerHandler(this); |
| 72 | } |
| 73 | |
| 74 | notifyVideoSizeChanged(); |
| 75 | notifyFlagsChanged(0); |
| 76 | notifyPrepared(); |
| 77 | } |
| 78 | |
| 79 | void NuPlayer2::StreamingSource::start() { |
| 80 | mStreamListener = new StreamListener(mSource, NULL); |
| 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); |
| 90 | |
| 91 | mStreamListener->start(); |
| 92 | |
| 93 | postReadBuffer(); |
| 94 | } |
| 95 | |
| 96 | status_t NuPlayer2::StreamingSource::feedMoreTSData() { |
| 97 | return postReadBuffer(); |
| 98 | } |
| 99 | |
| 100 | void NuPlayer2::StreamingSource::onReadBuffer() { |
| 101 | for (int32_t i = 0; i < kNumListenerQueuePackets; ++i) { |
| 102 | char buffer[188]; |
| 103 | sp<AMessage> extra; |
| 104 | ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra); |
| 105 | |
| 106 | if (n == 0) { |
| 107 | ALOGI("input data EOS reached."); |
| 108 | mTSParser->signalEOS(ERROR_END_OF_STREAM); |
| 109 | setError(ERROR_END_OF_STREAM); |
| 110 | break; |
| 111 | } else if (n == INFO_DISCONTINUITY) { |
| 112 | int32_t type = ATSParser::DISCONTINUITY_TIME; |
| 113 | |
| 114 | int32_t mask; |
| 115 | if (extra != NULL |
| 116 | && extra->findInt32( |
| 117 | IStreamListener::kKeyDiscontinuityMask, &mask)) { |
| 118 | if (mask == 0) { |
| 119 | ALOGE("Client specified an illegal discontinuity type."); |
| 120 | setError(ERROR_UNSUPPORTED); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | type = mask; |
| 125 | } |
| 126 | |
| 127 | mTSParser->signalDiscontinuity( |
| 128 | (ATSParser::DiscontinuityType)type, extra); |
| 129 | } else if (n < 0) { |
| 130 | break; |
| 131 | } else { |
| 132 | if (buffer[0] == 0x00) { |
| 133 | // XXX legacy |
| 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 | |
| 148 | mTSParser->signalDiscontinuity( |
| 149 | ((type & 1) == 0) |
| 150 | ? ATSParser::DISCONTINUITY_TIME |
| 151 | : ATSParser::DISCONTINUITY_FORMATCHANGE, |
| 152 | extra); |
| 153 | } else { |
| 154 | status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer)); |
| 155 | |
| 156 | if (err != OK) { |
| 157 | ALOGE("TS Parser returned error %d", err); |
| 158 | |
| 159 | mTSParser->signalEOS(err); |
| 160 | setError(err); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | status_t NuPlayer2::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 | |
| 180 | (new AMessage(kWhatReadBuffer, this))->post(); |
| 181 | return OK; |
| 182 | } |
| 183 | |
| 184 | bool NuPlayer2::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 NuPlayer2::StreamingSource::setError(status_t err) { |
| 217 | Mutex::Autolock _l(mBufferingLock); |
| 218 | mFinalResult = err; |
| 219 | } |
| 220 | |
| 221 | sp<AnotherPacketSource> NuPlayer2::StreamingSource::getSource(bool audio) { |
| 222 | if (mTSParser == NULL) { |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | sp<MediaSource> source = mTSParser->getSource( |
| 227 | audio ? ATSParser::AUDIO : ATSParser::VIDEO); |
| 228 | |
| 229 | return static_cast<AnotherPacketSource *>(source.get()); |
| 230 | } |
| 231 | |
| 232 | sp<AMessage> NuPlayer2::StreamingSource::getFormat(bool audio) { |
| 233 | sp<AnotherPacketSource> source = getSource(audio); |
| 234 | |
| 235 | sp<AMessage> format = new AMessage; |
| 236 | if (source == NULL) { |
| 237 | format->setInt32("err", -EWOULDBLOCK); |
| 238 | return format; |
| 239 | } |
| 240 | |
| 241 | sp<MetaData> meta = source->getFormat(); |
| 242 | if (meta == NULL) { |
| 243 | format->setInt32("err", -EWOULDBLOCK); |
| 244 | return format; |
| 245 | } |
| 246 | status_t err = convertMetaDataToMessage(meta, &format); |
| 247 | if (err != OK) { // format may have been cleared on error |
| 248 | return NULL; |
| 249 | } |
| 250 | return format; |
| 251 | } |
| 252 | |
| 253 | status_t NuPlayer2::StreamingSource::dequeueAccessUnit( |
| 254 | bool audio, sp<ABuffer> *accessUnit) { |
| 255 | sp<AnotherPacketSource> source = getSource(audio); |
| 256 | |
| 257 | if (source == NULL) { |
| 258 | return -EWOULDBLOCK; |
| 259 | } |
| 260 | |
| 261 | if (!haveSufficientDataOnAllTracks()) { |
| 262 | postReadBuffer(); |
| 263 | } |
| 264 | |
| 265 | status_t finalResult; |
| 266 | if (!source->hasBufferAvailable(&finalResult)) { |
| 267 | return finalResult == OK ? -EWOULDBLOCK : finalResult; |
| 268 | } |
| 269 | |
| 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; |
| 281 | } |
| 282 | |
| 283 | bool NuPlayer2::StreamingSource::isRealTime() const { |
| 284 | return mSource->flags() & IStreamSource::kFlagIsRealTimeData; |
| 285 | } |
| 286 | |
| 287 | void NuPlayer2::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 | |
| 308 | } // namespace android |
| 309 | |