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