diff options
author | Andreas Huber <andih@google.com> | 2010-01-25 14:27:12 -0800 |
---|---|---|
committer | Andreas Huber <andih@google.com> | 2010-01-25 16:17:40 -0800 |
commit | e7c9cb48fec02697227bd847cd2e69432659adfd (patch) | |
tree | ab833cb8951fc46fa4dcef5569d818378ca14b8c /media/libstagefright/AudioSource.cpp | |
parent | 213addfaf4b359c69da4e9b4490c511d116845bb (diff) | |
download | frameworks_av-e7c9cb48fec02697227bd847cd2e69432659adfd.zip frameworks_av-e7c9cb48fec02697227bd847cd2e69432659adfd.tar.gz frameworks_av-e7c9cb48fec02697227bd847cd2e69432659adfd.tar.bz2 |
Initial checkin of AudioSource and AMRWriter, a pair of classes supporting pure-audio recording in stagefright.
related-to-bug: 2295449
Diffstat (limited to 'media/libstagefright/AudioSource.cpp')
-rw-r--r-- | media/libstagefright/AudioSource.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/media/libstagefright/AudioSource.cpp b/media/libstagefright/AudioSource.cpp new file mode 100644 index 0000000..edabaf9 --- /dev/null +++ b/media/libstagefright/AudioSource.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <media/stagefright/AudioSource.h> + +#include <media/AudioRecord.h> +#include <media/stagefright/MediaBufferGroup.h> +#include <media/stagefright/MediaDebug.h> +#include <media/stagefright/MediaDefs.h> +#include <media/stagefright/MetaData.h> + +namespace android { + +AudioSource::AudioSource( + int inputSource, uint32_t sampleRate, uint32_t channels) + : mRecord(new AudioRecord( + inputSource, sampleRate, AudioSystem::PCM_16_BIT, channels)), + mInitCheck(mRecord->initCheck()), + mStarted(false), + mGroup(NULL) { +} + +AudioSource::~AudioSource() { + if (mStarted) { + stop(); + } + + delete mRecord; + mRecord = NULL; +} + +status_t AudioSource::initCheck() const { + return mInitCheck; +} + +status_t AudioSource::start(MetaData *params) { + if (mStarted) { + return UNKNOWN_ERROR; + } + + status_t err = mRecord->start(); + + if (err == OK) { + mGroup = new MediaBufferGroup; + mGroup->add_buffer(new MediaBuffer(kMaxBufferSize)); + + mStarted = true; + } + + return err; +} + +status_t AudioSource::stop() { + if (!mStarted) { + return UNKNOWN_ERROR; + } + + mRecord->stop(); + + delete mGroup; + mGroup = NULL; + + mStarted = false; + + return OK; +} + +sp<MetaData> AudioSource::getFormat() { + sp<MetaData> meta = new MetaData; + meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); + meta->setInt32(kKeySampleRate, mRecord->getSampleRate()); + meta->setInt32(kKeyChannelCount, mRecord->channelCount()); + meta->setInt32(kKeyMaxInputSize, kMaxBufferSize); + + return meta; +} + +status_t AudioSource::read( + MediaBuffer **out, const ReadOptions *options) { + *out = NULL; + + MediaBuffer *buffer; + CHECK_EQ(mGroup->acquire_buffer(&buffer), OK); + + uint32_t numFramesRecorded; + mRecord->getPosition(&numFramesRecorded); + + buffer->meta_data()->setInt64( + kKeyTime, + (1000000ll * numFramesRecorded) / mRecord->getSampleRate() + - mRecord->latency() * 1000); + + ssize_t n = mRecord->read(buffer->data(), buffer->size()); + + if (n < 0) { + buffer->release(); + buffer = NULL; + + return (status_t)n; + } + + buffer->set_range(0, n); + + *out = buffer; + + return OK; +} + +} // namespace android |