HTTPLiveSource.cpp revision 9575c96b6e418914e2ffc6741ecc8d71e3968dbe
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::prepareAsync() { 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 notifyVideoSizeChanged(0, 0); 86 87 uint32_t flags = FLAG_CAN_PAUSE; 88 if (mLiveSession->isSeekable()) { 89 flags |= FLAG_CAN_SEEK; 90 flags |= FLAG_CAN_SEEK_BACKWARD; 91 flags |= FLAG_CAN_SEEK_FORWARD; 92 } 93 94 if (mLiveSession->hasDynamicDuration()) { 95 flags |= FLAG_DYNAMIC_DURATION; 96 } 97 98 notifyFlagsChanged(flags); 99 100 notifyPrepared(); 101} 102 103void NuPlayer::HTTPLiveSource::start() { 104} 105 106sp<MetaData> NuPlayer::HTTPLiveSource::getFormatMeta(bool audio) { 107 ATSParser::SourceType type = 108 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 109 110 sp<AnotherPacketSource> source = 111 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 112 113 if (source == NULL) { 114 return NULL; 115 } 116 117 return source->getFormat(); 118} 119 120status_t NuPlayer::HTTPLiveSource::feedMoreTSData() { 121 if (mFinalResult != OK) { 122 return mFinalResult; 123 } 124 125 sp<LiveDataSource> source = 126 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get()); 127 128 for (int32_t i = 0; i < 50; ++i) { 129 char buffer[188]; 130 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer)); 131 132 if (n == -EWOULDBLOCK) { 133 break; 134 } else if (n < 0) { 135 if (n != ERROR_END_OF_STREAM) { 136 ALOGI("input data EOS reached, error %ld", n); 137 } else { 138 ALOGI("input data EOS reached."); 139 } 140 mTSParser->signalEOS(n); 141 mFinalResult = n; 142 break; 143 } else { 144 if (buffer[0] == 0x00) { 145 // XXX legacy 146 147 uint8_t type = buffer[1]; 148 149 sp<AMessage> extra = new AMessage; 150 151 if (type & 2) { 152 int64_t mediaTimeUs; 153 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs)); 154 155 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs); 156 } 157 158 mTSParser->signalDiscontinuity( 159 ((type & 1) == 0) 160 ? ATSParser::DISCONTINUITY_SEEK 161 : ATSParser::DISCONTINUITY_FORMATCHANGE, 162 extra); 163 } else { 164 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer)); 165 166 if (err != OK) { 167 ALOGE("TS Parser returned error %d", err); 168 mTSParser->signalEOS(err); 169 mFinalResult = err; 170 break; 171 } 172 } 173 174 mOffset += n; 175 } 176 } 177 178 return OK; 179} 180 181status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit( 182 bool audio, sp<ABuffer> *accessUnit) { 183 ATSParser::SourceType type = 184 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 185 186 sp<AnotherPacketSource> source = 187 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 188 189 if (source == NULL) { 190 return -EWOULDBLOCK; 191 } 192 193 status_t finalResult; 194 if (!source->hasBufferAvailable(&finalResult)) { 195 return finalResult == OK ? -EWOULDBLOCK : finalResult; 196 } 197 198 return source->dequeueAccessUnit(accessUnit); 199} 200 201status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) { 202 return mLiveSession->getDuration(durationUs); 203} 204 205status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) { 206 // We need to make sure we're not seeking until we have seen the very first 207 // PTS timestamp in the whole stream (from the beginning of the stream). 208 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData() == OK) { 209 usleep(100000); 210 } 211 212 mLiveSession->seekTo(seekTimeUs); 213 214 return OK; 215} 216 217} // namespace android 218 219