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 "HTTPLiveSource" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "HTTPLiveSource.h" |
| 22 | |
| 23 | #include "ATSParser.h" |
| 24 | #include "AnotherPacketSource.h" |
| 25 | #include "LiveDataSource.h" |
| 26 | #include "LiveSession.h" |
| 27 | |
| 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/MediaErrors.h> |
| 32 | #include <media/stagefright/MetaData.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame^] | 36 | NuPlayer::HTTPLiveSource::HTTPLiveSource( |
| 37 | const char *url, |
| 38 | const KeyedVector<String8, String8> *headers) |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 39 | : mURL(url), |
Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame^] | 40 | mFlags(0), |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 41 | mEOS(false), |
| 42 | mOffset(0) { |
Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame^] | 43 | if (headers) { |
| 44 | mExtraHeaders = *headers; |
| 45 | |
| 46 | ssize_t index = |
| 47 | mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log")); |
| 48 | |
| 49 | if (index >= 0) { |
| 50 | mFlags |= kFlagIncognito; |
| 51 | |
| 52 | mExtraHeaders.removeItemsAt(index); |
| 53 | } |
| 54 | } |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | NuPlayer::HTTPLiveSource::~HTTPLiveSource() { |
| 58 | mLiveSession->disconnect(); |
| 59 | mLiveLooper->stop(); |
| 60 | } |
| 61 | |
| 62 | void NuPlayer::HTTPLiveSource::start() { |
| 63 | mLiveLooper = new ALooper; |
| 64 | mLiveLooper->setName("http live"); |
| 65 | mLiveLooper->start(); |
| 66 | |
Andreas Huber | 7314fa1 | 2011-02-24 14:42:48 -0800 | [diff] [blame] | 67 | mLiveSession = new LiveSession( |
| 68 | (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0); |
| 69 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 70 | mLiveLooper->registerHandler(mLiveSession); |
| 71 | |
Andreas Huber | ad0d9c9 | 2011-04-19 11:50:27 -0700 | [diff] [blame^] | 72 | mLiveSession->connect( |
| 73 | mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 74 | |
| 75 | mTSParser = new ATSParser; |
| 76 | } |
| 77 | |
| 78 | sp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) { |
| 79 | ATSParser::SourceType type = |
| 80 | audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO; |
| 81 | |
| 82 | sp<AnotherPacketSource> source = |
| 83 | static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); |
| 84 | |
| 85 | if (source == NULL) { |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | return source->getFormat(); |
| 90 | } |
| 91 | |
| 92 | bool NuPlayer::HTTPLiveSource::feedMoreTSData() { |
| 93 | if (mEOS) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | sp<LiveDataSource> source = |
| 98 | static_cast<LiveDataSource *>(mLiveSession->getDataSource().get()); |
| 99 | |
Andreas Huber | 22fc52f | 2011-01-05 16:24:27 -0800 | [diff] [blame] | 100 | for (int32_t i = 0; i < 50; ++i) { |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 101 | char buffer[188]; |
| 102 | ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer)); |
| 103 | |
| 104 | if (n == -EWOULDBLOCK) { |
| 105 | break; |
| 106 | } else if (n < 0) { |
| 107 | LOGI("input data EOS reached."); |
Andreas Huber | 1aef211 | 2011-01-04 14:01:29 -0800 | [diff] [blame] | 108 | mTSParser->signalEOS(n); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 109 | mEOS = true; |
| 110 | break; |
| 111 | } else { |
| 112 | if (buffer[0] == 0x00) { |
| 113 | // XXX legacy |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 114 | sp<AMessage> extra; |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 115 | mTSParser->signalDiscontinuity( |
| 116 | buffer[1] == 0x00 |
| 117 | ? ATSParser::DISCONTINUITY_SEEK |
Andreas Huber | 32f3cef | 2011-03-02 15:34:46 -0800 | [diff] [blame] | 118 | : ATSParser::DISCONTINUITY_FORMATCHANGE, |
| 119 | extra); |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 120 | } else { |
| 121 | mTSParser->feedTSPacket(buffer, sizeof(buffer)); |
| 122 | } |
| 123 | |
| 124 | mOffset += n; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit( |
| 132 | bool audio, sp<ABuffer> *accessUnit) { |
| 133 | ATSParser::SourceType type = |
| 134 | audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO; |
| 135 | |
| 136 | sp<AnotherPacketSource> source = |
| 137 | static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); |
| 138 | |
| 139 | if (source == NULL) { |
| 140 | return -EWOULDBLOCK; |
| 141 | } |
| 142 | |
| 143 | status_t finalResult; |
| 144 | if (!source->hasBufferAvailable(&finalResult)) { |
| 145 | return finalResult == OK ? -EWOULDBLOCK : finalResult; |
| 146 | } |
| 147 | |
| 148 | return source->dequeueAccessUnit(accessUnit); |
| 149 | } |
| 150 | |
Andreas Huber | 43c3e6c | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 151 | status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) { |
| 152 | return mLiveSession->getDuration(durationUs); |
| 153 | } |
| 154 | |
| 155 | status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) { |
| 156 | // We need to make sure we're not seeking until we have seen the very first |
| 157 | // PTS timestamp in the whole stream (from the beginning of the stream). |
| 158 | while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData()) { |
| 159 | usleep(100000); |
| 160 | } |
| 161 | |
| 162 | mLiveSession->seekTo(seekTimeUs); |
| 163 | |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | bool NuPlayer::HTTPLiveSource::isSeekable() { |
| 168 | return mLiveSession->isSeekable(); |
| 169 | } |
| 170 | |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 171 | } // namespace android |
| 172 | |