AnotherPacketSource.cpp revision 6e3d311b6631b12aac2879d1b08c3534aece78b1
14bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes/* 24bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Copyright (C) 2010 The Android Open Source Project 34bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * 44bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 54bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * you may not use this file except in compliance with the License. 64bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * You may obtain a copy of the License at 74bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * 84bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 94bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * 104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * Unless required by applicable law or agreed to in writing, software 114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * See the License for the specific language governing permissions and 144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes * limitations under the License. 154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes */ 164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include "AnotherPacketSource.h" 184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/foundation/ABuffer.h> 204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/foundation/ADebug.h> 214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/foundation/AMessage.h> 224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/foundation/AString.h> 234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/foundation/hexdump.h> 244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/MediaBuffer.h> 254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/MediaDefs.h> 264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <media/stagefright/MetaData.h> 274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes#include <utils/Vector.h> 284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesnamespace android { 304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott HughesAnotherPacketSource::AnotherPacketSource(const sp<MetaData> &meta) 324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes : mIsAudio(false), 334bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mFormat(meta), 344bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mEOSResult(OK) { 354bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes const char *mime; 364bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(meta->findCString(kKeyMIMEType, &mime)); 374bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 384bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (!strncasecmp("audio/", mime, 6)) { 394bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mIsAudio = true; 404bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } else { 414bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(!strncasecmp("video/", mime, 6)); 424bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 434bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 444bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 454bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesvoid AnotherPacketSource::setFormat(const sp<MetaData> &meta) { 464bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(mFormat == NULL); 474bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mFormat = meta; 484bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 494bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 504bd97cee28dd815fff54fc97560be60d566c1fa5Elliott HughesAnotherPacketSource::~AnotherPacketSource() { 514bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 524bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 534bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatus_t AnotherPacketSource::start(MetaData *params) { 544bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return OK; 554bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 564bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 574bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatus_t AnotherPacketSource::stop() { 584bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return OK; 594bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 604bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 614bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughessp<MetaData> AnotherPacketSource::getFormat() { 624bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return mFormat; 634bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 644bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 654bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatus_t AnotherPacketSource::dequeueAccessUnit(sp<ABuffer> *buffer) { 664bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes buffer->clear(); 674bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 684bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 694bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes while (mEOSResult == OK && mBuffers.empty()) { 704bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mCondition.wait(mLock); 714bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 724bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 734bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (!mBuffers.empty()) { 744bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *buffer = *mBuffers.begin(); 754bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mBuffers.erase(mBuffers.begin()); 764bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 774bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int32_t discontinuity; 784bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if ((*buffer)->meta()->findInt32("discontinuity", &discontinuity)) { 794bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (wasFormatChange(discontinuity)) { 804bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mFormat.clear(); 814bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 824bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 834bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return INFO_DISCONTINUITY; 844bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 854bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 864bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return OK; 874bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 884bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 894bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return mEOSResult; 904bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 914bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 924bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatus_t AnotherPacketSource::read( 934bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes MediaBuffer **out, const ReadOptions *) { 944bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *out = NULL; 954bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 964bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 974bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes while (mEOSResult == OK && mBuffers.empty()) { 984bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mCondition.wait(mLock); 994bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1004bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1014bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (!mBuffers.empty()) { 1024bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes const sp<ABuffer> buffer = *mBuffers.begin(); 1034bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mBuffers.erase(mBuffers.begin()); 1044bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1054bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int32_t discontinuity; 1064bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (buffer->meta()->findInt32("discontinuity", &discontinuity)) { 1074bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (wasFormatChange(discontinuity)) { 1084bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mFormat.clear(); 1094bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return INFO_DISCONTINUITY; 1124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } else { 1134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int64_t timeUs; 1144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 1154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes MediaBuffer *mediaBuffer = new MediaBuffer(buffer); 1174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1184bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs); 1194bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1204bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *out = mediaBuffer; 1214bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return OK; 1224bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1234bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1244bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1254bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return mEOSResult; 1264bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 1274bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1284bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesbool AnotherPacketSource::wasFormatChange( 1294bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int32_t discontinuityType) const { 1304bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (mIsAudio) { 1314bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return (discontinuityType & ATSParser::DISCONTINUITY_AUDIO_FORMAT) != 0; 1324bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1334bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1344bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return (discontinuityType & ATSParser::DISCONTINUITY_VIDEO_FORMAT) != 0; 1354bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 1364bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1374bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesvoid AnotherPacketSource::queueAccessUnit(const sp<ABuffer> &buffer) { 1384bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int32_t damaged; 1394bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (buffer->meta()->findInt32("damaged", &damaged) && damaged) { 1404bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes // LOG(VERBOSE) << "discarding damaged AU"; 1414bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return; 1424bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1434bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1444bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int64_t timeUs; 1454bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 1464bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes LOGV("queueAccessUnit timeUs=%lld us (%.2f secs)", timeUs, timeUs / 1E6); 1474bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1484bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 1494bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mBuffers.push_back(buffer); 1504bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mCondition.signal(); 1514bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 1524bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1534bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesvoid AnotherPacketSource::queueDiscontinuity( 1544bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes ATSParser::DiscontinuityType type, 1554bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes const sp<AMessage> &extra) { 1564bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 1574bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1584bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes // Leave only discontinuities in the queue. 1594bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes List<sp<ABuffer> >::iterator it = mBuffers.begin(); 1604bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes while (it != mBuffers.end()) { 1614bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes sp<ABuffer> oldBuffer = *it; 1624bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1634bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes int32_t oldDiscontinuityType; 1644bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (!oldBuffer->meta()->findInt32( 1654bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes "discontinuity", &oldDiscontinuityType)) { 1664bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes it = mBuffers.erase(it); 1674bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes continue; 1684bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1694bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1704bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes ++it; 1714bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1724bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1734bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mEOSResult = OK; 1744bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1754bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes sp<ABuffer> buffer = new ABuffer(0); 1764bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes buffer->meta()->setInt32("discontinuity", static_cast<int32_t>(type)); 1774bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes buffer->meta()->setMessage("extra", extra); 1784bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1794bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mBuffers.push_back(buffer); 1804bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mCondition.signal(); 1814bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 1824bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1834bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesvoid AnotherPacketSource::signalEOS(status_t result) { 1844bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(result != OK); 1854bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1864bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 1874bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mEOSResult = result; 1884bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes mCondition.signal(); 1894bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 1904bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1914bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesbool AnotherPacketSource::hasBufferAvailable(status_t *finalResult) { 1924bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 1934bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (!mBuffers.empty()) { 1944bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return true; 1954bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 1964bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 1974bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *finalResult = mEOSResult; 1984bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return false; 1994bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 2004bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2014bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughesstatus_t AnotherPacketSource::nextBufferTime(int64_t *timeUs) { 2024bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes *timeUs = 0; 2034bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2044bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes Mutex::Autolock autoLock(mLock); 2054bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2064bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes if (mBuffers.empty()) { 2074bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return mEOSResult != OK ? mEOSResult : -EWOULDBLOCK; 2084bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes } 2094bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2104bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes sp<ABuffer> buffer = *mBuffers.begin(); 2114bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes CHECK(buffer->meta()->findInt64("timeUs", timeUs)); 2124bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2134bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes return OK; 2144bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} 2154bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes 2164bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes} // namespace android 2174bd97cee28dd815fff54fc97560be60d566c1fa5Elliott Hughes