HTTPLiveSource.cpp revision 5ab368af38fefacc4009e3ab1c1bbd00e62b3bcf
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 34namespace android { 35 36NuPlayer::HTTPLiveSource::HTTPLiveSource( 37 const sp<AMessage> ¬ify, 38 const char *url, 39 const KeyedVector<String8, String8> *headers, 40 bool uidValid, uid_t uid) 41 : Source(notify), 42 mURL(url), 43 mUIDValid(uidValid), 44 mUID(uid), 45 mFlags(0), 46 mFinalResult(OK), 47 mOffset(0) { 48 if (headers) { 49 mExtraHeaders = *headers; 50 51 ssize_t index = 52 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log")); 53 54 if (index >= 0) { 55 mFlags |= kFlagIncognito; 56 57 mExtraHeaders.removeItemsAt(index); 58 } 59 } 60} 61 62NuPlayer::HTTPLiveSource::~HTTPLiveSource() { 63 if (mLiveSession != NULL) { 64 mLiveSession->disconnect(); 65 mLiveLooper->stop(); 66 } 67} 68 69void NuPlayer::HTTPLiveSource::start() { 70 mLiveLooper = new ALooper; 71 mLiveLooper->setName("http live"); 72 mLiveLooper->start(); 73 74 mLiveSession = new LiveSession( 75 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0, 76 mUIDValid, mUID); 77 78 mLiveLooper->registerHandler(mLiveSession); 79 80 mLiveSession->connect( 81 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); 82 83 mTSParser = new ATSParser; 84} 85 86sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) { 87 ATSParser::SourceType type = 88 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 89 90 sp<AnotherPacketSource> source = 91 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 92 93 if (source == NULL) { 94 return NULL; 95 } 96 97 return source->getFormat(); 98} 99 100status_t NuPlayer::HTTPLiveSource::feedMoreTSData() { 101 if (mFinalResult != OK) { 102 return mFinalResult; 103 } 104 105 sp<LiveDataSource> source = 106 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get()); 107 108 for (int32_t i = 0; i < 50; ++i) { 109 char buffer[188]; 110 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer)); 111 112 if (n == -EWOULDBLOCK) { 113 break; 114 } else if (n < 0) { 115 if (n != ERROR_END_OF_STREAM) { 116 ALOGI("input data EOS reached, error %ld", n); 117 } else { 118 ALOGI("input data EOS reached."); 119 } 120 mTSParser->signalEOS(n); 121 mFinalResult = n; 122 break; 123 } else { 124 if (buffer[0] == 0x00) { 125 // XXX legacy 126 127 uint8_t type = buffer[1]; 128 129 sp<AMessage> extra = new AMessage; 130 131 if (type & 2) { 132 int64_t mediaTimeUs; 133 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs)); 134 135 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs); 136 } 137 138 mTSParser->signalDiscontinuity( 139 ((type & 1) == 0) 140 ? ATSParser::DISCONTINUITY_SEEK 141 : ATSParser::DISCONTINUITY_FORMATCHANGE, 142 extra); 143 } else { 144 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer)); 145 146 if (err != OK) { 147 ALOGE("TS Parser returned error %d", err); 148 mTSParser->signalEOS(err); 149 mFinalResult = err; 150 break; 151 } 152 } 153 154 mOffset += n; 155 } 156 } 157 158 return OK; 159} 160 161status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit( 162 bool audio, sp<ABuffer> *accessUnit) { 163 ATSParser::SourceType type = 164 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 165 166 sp<AnotherPacketSource> source = 167 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 168 169 if (source == NULL) { 170 return -EWOULDBLOCK; 171 } 172 173 status_t finalResult; 174 if (!source->hasBufferAvailable(&finalResult)) { 175 return finalResult == OK ? -EWOULDBLOCK : finalResult; 176 } 177 178 return source->dequeueAccessUnit(accessUnit); 179} 180 181status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) { 182 return mLiveSession->getDuration(durationUs); 183} 184 185status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) { 186 // We need to make sure we're not seeking until we have seen the very first 187 // PTS timestamp in the whole stream (from the beginning of the stream). 188 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) { 189 usleep(100000); 190 } 191 192 mLiveSession->seekTo(seekTimeUs); 193 194 return OK; 195} 196 197uint32_t NuPlayer::HTTPLiveSource::flags() const { 198 uint32_t flags = 0; 199 if (mLiveSession->isSeekable()) { 200 flags |= FLAG_SEEKABLE; 201 } 202 203 if (mLiveSession->hasDynamicDuration()) { 204 flags |= FLAG_DYNAMIC_DURATION; 205 } 206 207 return flags; 208} 209 210} // namespace android 211 212