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