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 "StreamingSource" 19#include <utils/Log.h> 20 21#include "StreamingSource.h" 22 23#include "ATSParser.h" 24#include "AnotherPacketSource.h" 25#include "NuPlayerStreamListener.h" 26 27#include <media/stagefright/foundation/ABuffer.h> 28#include <media/stagefright/foundation/ADebug.h> 29#include <media/stagefright/foundation/AMessage.h> 30#include <media/stagefright/MediaSource.h> 31#include <media/stagefright/MetaData.h> 32 33namespace android { 34 35NuPlayer::StreamingSource::StreamingSource(const sp<IStreamSource> &source) 36 : mSource(source), 37 mFinalResult(OK) { 38} 39 40NuPlayer::StreamingSource::~StreamingSource() { 41} 42 43void NuPlayer::StreamingSource::start() { 44 mStreamListener = new NuPlayerStreamListener(mSource, 0); 45 46 uint32_t sourceFlags = mSource->flags(); 47 48 uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE; 49 if (sourceFlags & IStreamSource::kFlagAlignedVideoData) { 50 parserFlags |= ATSParser::ALIGNED_VIDEO_DATA; 51 } 52 53 mTSParser = new ATSParser(parserFlags); 54 55 mStreamListener->start(); 56} 57 58status_t NuPlayer::StreamingSource::feedMoreTSData() { 59 if (mFinalResult != OK) { 60 return mFinalResult; 61 } 62 63 for (int32_t i = 0; i < 50; ++i) { 64 char buffer[188]; 65 sp<AMessage> extra; 66 ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra); 67 68 if (n == 0) { 69 ALOGI("input data EOS reached."); 70 mTSParser->signalEOS(ERROR_END_OF_STREAM); 71 mFinalResult = ERROR_END_OF_STREAM; 72 break; 73 } else if (n == INFO_DISCONTINUITY) { 74 int32_t type = ATSParser::DISCONTINUITY_SEEK; 75 76 int32_t mask; 77 if (extra != NULL 78 && extra->findInt32( 79 IStreamListener::kKeyDiscontinuityMask, &mask)) { 80 if (mask == 0) { 81 ALOGE("Client specified an illegal discontinuity type."); 82 return ERROR_UNSUPPORTED; 83 } 84 85 type = mask; 86 } 87 88 mTSParser->signalDiscontinuity( 89 (ATSParser::DiscontinuityType)type, extra); 90 } else if (n < 0) { 91 CHECK_EQ(n, -EWOULDBLOCK); 92 break; 93 } else { 94 if (buffer[0] == 0x00) { 95 // XXX legacy 96 97 if (extra == NULL) { 98 extra = new AMessage; 99 } 100 101 uint8_t type = buffer[1]; 102 103 if (type & 2) { 104 int64_t mediaTimeUs; 105 memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs)); 106 107 extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs); 108 } 109 110 mTSParser->signalDiscontinuity( 111 ((type & 1) == 0) 112 ? ATSParser::DISCONTINUITY_SEEK 113 : ATSParser::DISCONTINUITY_FORMATCHANGE, 114 extra); 115 } else { 116 status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer)); 117 118 if (err != OK) { 119 ALOGE("TS Parser returned error %d", err); 120 121 mTSParser->signalEOS(err); 122 mFinalResult = err; 123 break; 124 } 125 } 126 } 127 } 128 129 return OK; 130} 131 132sp<MetaData> NuPlayer::StreamingSource::getFormatMeta(bool audio) { 133 ATSParser::SourceType type = 134 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 135 136 sp<AnotherPacketSource> source = 137 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 138 139 if (source == NULL) { 140 return NULL; 141 } 142 143 return source->getFormat(); 144} 145 146status_t NuPlayer::StreamingSource::dequeueAccessUnit( 147 bool audio, sp<ABuffer> *accessUnit) { 148 ATSParser::SourceType type = 149 audio ? ATSParser::AUDIO : ATSParser::VIDEO; 150 151 sp<AnotherPacketSource> source = 152 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get()); 153 154 if (source == NULL) { 155 return -EWOULDBLOCK; 156 } 157 158 status_t finalResult; 159 if (!source->hasBufferAvailable(&finalResult)) { 160 return finalResult == OK ? -EWOULDBLOCK : finalResult; 161 } 162 163 status_t err = source->dequeueAccessUnit(accessUnit); 164 165#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0 166 if (err == OK) { 167 int64_t timeUs; 168 CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs)); 169 ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs); 170 } 171#endif 172 173 return err; 174} 175 176uint32_t NuPlayer::StreamingSource::flags() const { 177 return 0; 178} 179 180} // namespace android 181 182