HTTPLiveSource.cpp revision ad0d9c9c39a24b7fbd94e935a5855c9025341929
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 : mURL(url), 40 mFlags(0), 41 mEOS(false), 42 mOffset(0) { 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 } 55} 56 57NuPlayer::HTTPLiveSource::~HTTPLiveSource() { 58 mLiveSession->disconnect(); 59 mLiveLooper->stop(); 60} 61 62void NuPlayer::HTTPLiveSource::start() { 63 mLiveLooper = new ALooper; 64 mLiveLooper->setName("http live"); 65 mLiveLooper->start(); 66 67 mLiveSession = new LiveSession( 68 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0); 69 70 mLiveLooper->registerHandler(mLiveSession); 71 72 mLiveSession->connect( 73 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); 74 75 mTSParser = new ATSParser; 76} 77 78sp<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 92bool NuPlayer::HTTPLiveSource::feedMoreTSData() { 93 if (mEOS) { 94 return false; 95 } 96 97 sp<LiveDataSource> source = 98 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get()); 99 100 for (int32_t i = 0; i < 50; ++i) { 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."); 108 mTSParser->signalEOS(n); 109 mEOS = true; 110 break; 111 } else { 112 if (buffer[0] == 0x00) { 113 // XXX legacy 114 sp<AMessage> extra; 115 mTSParser->signalDiscontinuity( 116 buffer[1] == 0x00 117 ? ATSParser::DISCONTINUITY_SEEK 118 : ATSParser::DISCONTINUITY_FORMATCHANGE, 119 extra); 120 } else { 121 mTSParser->feedTSPacket(buffer, sizeof(buffer)); 122 } 123 124 mOffset += n; 125 } 126 } 127 128 return true; 129} 130 131status_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 151status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) { 152 return mLiveSession->getDuration(durationUs); 153} 154 155status_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 167bool NuPlayer::HTTPLiveSource::isSeekable() { 168 return mLiveSession->isSeekable(); 169} 170 171} // namespace android 172 173