AnotherPacketSource.cpp revision f933441648ef6a71dee783d733aac17b9508b452
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 buffer->clear(); 67 68 return INFO_DISCONTINUITY; 69 } 70 71 return OK; 72 } 73 74 return mEOSResult; 75} 76 77status_t AnotherPacketSource::read( 78 MediaBuffer **out, const ReadOptions *) { 79 *out = NULL; 80 81 Mutex::Autolock autoLock(mLock); 82 while (mEOSResult == OK && mBuffers.empty()) { 83 mCondition.wait(mLock); 84 } 85 86 if (!mBuffers.empty()) { 87 const sp<ABuffer> buffer = *mBuffers.begin(); 88 mBuffers.erase(mBuffers.begin()); 89 90 int32_t discontinuity; 91 if (buffer->meta()->findInt32("discontinuity", &discontinuity) 92 && discontinuity) { 93 return INFO_DISCONTINUITY; 94 } else { 95 int64_t timeUs; 96 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 97 98 MediaBuffer *mediaBuffer = new MediaBuffer(buffer->size()); 99 mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs); 100 101 // hexdump(buffer->data(), buffer->size()); 102 103 memcpy(mediaBuffer->data(), buffer->data(), buffer->size()); 104 *out = mediaBuffer; 105 return OK; 106 } 107 } 108 109 return mEOSResult; 110} 111 112void AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) { 113 int32_t damaged; 114 if (buffer->meta()->findInt32("damaged", &damaged) && damaged) { 115 // LOG(VERBOSE) << "discarding damaged AU"; 116 return; 117 } 118 119 int64_t timeUs; 120 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 121 LOGV("queueAccessUnit timeUs=%lld us (%.2f secs)", timeUs, timeUs / 1E6); 122 123 Mutex::Autolock autoLock(mLock); 124 mBuffers.push_back(buffer); 125 mCondition.signal(); 126} 127 128void AnotherPacketSource::queueDiscontinuity() { 129 sp<ABuffer> buffer = new ABuffer(0); 130 buffer->meta()->setInt32("discontinuity", true); 131 132 Mutex::Autolock autoLock(mLock); 133 134 mBuffers.push_back(buffer); 135 mCondition.signal(); 136} 137 138void AnotherPacketSource::clear() { 139 Mutex::Autolock autoLock(mLock); 140 mBuffers.clear(); 141 mEOSResult = OK; 142} 143 144void AnotherPacketSource::signalEOS(status_t result) { 145 CHECK(result != OK); 146 147 Mutex::Autolock autoLock(mLock); 148 mEOSResult = result; 149 mCondition.signal(); 150} 151 152bool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) { 153 Mutex::Autolock autoLock(mLock); 154 if (!mBuffers.empty()) { 155 return true; 156 } 157 158 *finalResult = mEOSResult; 159 return false; 160} 161 162status_t AnotherPacketSource::nextBufferTime(int64_t *timeUs) { 163 *timeUs = 0; 164 165 Mutex::Autolock autoLock(mLock); 166 167 if (mBuffers.empty()) { 168 return mEOSResult != OK ? mEOSResult : -EWOULDBLOCK; 169 } 170 171 sp<ABuffer> buffer = *mBuffers.begin(); 172 CHECK(buffer->meta()->findInt64("timeUs", timeUs)); 173 174 return OK; 175} 176 177} // namespace android 178