AnotherPacketSource.cpp revision 53df1a460bcfdd129ca2bc416dee2009e35c042e
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#include "AnotherPacketSource.h" 18 19#include <media/stagefright/foundation/ABuffer.h> 20#include <media/stagefright/foundation/ADebug.h> 21#include <media/stagefright/foundation/AMessage.h> 22#include <media/stagefright/foundation/AString.h> 23#include <media/stagefright/foundation/hexdump.h> 24#include <media/stagefright/MediaBuffer.h> 25#include <media/stagefright/MediaDefs.h> 26#include <media/stagefright/MetaData.h> 27#include <utils/Vector.h> 28 29namespace android { 30 31AnotherPacketSource::AnotherPacketSource(const sp<MetaData> &meta) 32 : mFormat(meta), 33 mEOSResult(OK) { 34} 35 36AnotherPacketSource::~AnotherPacketSource() { 37} 38 39status_t AnotherPacketSource::start(MetaData *params) { 40 return OK; 41} 42 43status_t AnotherPacketSource::stop() { 44 return OK; 45} 46 47sp<MetaData> AnotherPacketSource::getFormat() { 48 return mFormat; 49} 50 51status_t AnotherPacketSource::dequeueAccessUnit(sp<ABuffer> *buffer) { 52 buffer->clear(); 53 54 Mutex::Autolock autoLock(mLock); 55 while (mEOSResult == OK && mBuffers.empty()) { 56 mCondition.wait(mLock); 57 } 58 59 if (!mBuffers.empty()) { 60 *buffer = *mBuffers.begin(); 61 mBuffers.erase(mBuffers.begin()); 62 63 int32_t discontinuity; 64 if ((*buffer)->meta()->findInt32("discontinuity", &discontinuity) 65 && discontinuity) { 66 return INFO_DISCONTINUITY; 67 } 68 69 return OK; 70 } 71 72 return mEOSResult; 73} 74 75status_t AnotherPacketSource::read( 76 MediaBuffer **out, const ReadOptions *) { 77 *out = NULL; 78 79 Mutex::Autolock autoLock(mLock); 80 while (mEOSResult == OK && mBuffers.empty()) { 81 mCondition.wait(mLock); 82 } 83 84 if (!mBuffers.empty()) { 85 const sp<ABuffer> buffer = *mBuffers.begin(); 86 mBuffers.erase(mBuffers.begin()); 87 88 int32_t discontinuity; 89 if (buffer->meta()->findInt32("discontinuity", &discontinuity) 90 && discontinuity) { 91 return INFO_DISCONTINUITY; 92 } else { 93 int64_t timeUs; 94 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 95 96 MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size()); 97 mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs); 98 99 // hexdump(buffer->data(), buffer->size()); 100 101 memcpy(mediaBuffer->data(), buffer->data(), buffer->size()); 102 *out = mediaBuffer; 103 return OK; 104 } 105 } 106 107 return mEOSResult; 108} 109 110void AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) { 111 int32_t damaged; 112 if (buffer->meta()->findInt32("damaged", &damaged) && damaged) { 113 // LOG(VERBOSE) << "discarding damaged AU"; 114 return; 115 } 116 117 int64_t timeUs; 118 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 119 LOGV("queueAccessUnit timeUs=%lld us (%.2f secs)", timeUs, timeUs / 1E6); 120 121 Mutex::Autolock autoLock(mLock); 122 mBuffers.push_back(buffer); 123 mCondition.signal(); 124} 125 126void AnotherPacketSource::queueDiscontinuity(bool formatChange) { 127 sp<ABuffer> buffer = new ABuffer(0); 128 buffer->meta()->setInt32("discontinuity", true); 129 130 if (formatChange) { 131 buffer->meta()->setInt32("format-change", true); 132 } 133 134 Mutex::Autolock autoLock(mLock); 135 136 mBuffers.push_back(buffer); 137 mCondition.signal(); 138} 139 140void AnotherPacketSource::clear() { 141 Mutex::Autolock autoLock(mLock); 142 mBuffers.clear(); 143 mEOSResult = OK; 144} 145 146void AnotherPacketSource::signalEOS(status_t result) { 147 CHECK(result != OK); 148 149 Mutex::Autolock autoLock(mLock); 150 mEOSResult = result; 151 mCondition.signal(); 152} 153 154bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) { 155 Mutex::Autolock autoLock(mLock); 156 if (!mBuffers.empty()) { 157 return true; 158 } 159 160 *finalResult = mEOSResult; 161 return false; 162} 163 164status_t AnotherPacketSource::nextBufferTime(int64_t *timeUs) { 165 *timeUs = 0; 166 167 Mutex::Autolock autoLock(mLock); 168 169 if (mBuffers.empty()) { 170 return mEOSResult != OK ? mEOSResult : -EWOULDBLOCK; 171 } 172 173 sp<ABuffer> buffer = *mBuffers.begin(); 174 CHECK(buffer->meta()->findInt64("timeUs", timeUs)); 175 176 return OK; 177} 178 179} // namespace android 180