AudioSource.cpp revision 050b28a593350047845a45a14cc5026221ac1620
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 "AudioSource"
19#include <utils/Log.h>
20
21#include <media/stagefright/AudioSource.h>
22
23#include <media/AudioRecord.h>
24#include <media/stagefright/MediaBufferGroup.h>
25#include <media/stagefright/MediaDebug.h>
26#include <media/stagefright/MediaDefs.h>
27#include <media/stagefright/MetaData.h>
28
29namespace android {
30
31AudioSource::AudioSource(
32        int inputSource, uint32_t sampleRate, uint32_t channels)
33    : mRecord(new AudioRecord(
34                inputSource, sampleRate, AudioSystem::PCM_16_BIT, channels)),
35      mInitCheck(mRecord->initCheck()),
36      mStarted(false),
37      mGroup(NULL) {
38}
39
40AudioSource::~AudioSource() {
41    if (mStarted) {
42        stop();
43    }
44
45    delete mRecord;
46    mRecord = NULL;
47}
48
49status_t AudioSource::initCheck() const {
50    return mInitCheck;
51}
52
53status_t AudioSource::start(MetaData *params) {
54    if (mStarted) {
55        return UNKNOWN_ERROR;
56    }
57
58    status_t err = mRecord->start();
59
60    if (err == OK) {
61        mGroup = new MediaBufferGroup;
62        mGroup->add_buffer(new MediaBuffer(kMaxBufferSize));
63
64        mStarted = true;
65    }
66
67    return err;
68}
69
70status_t AudioSource::stop() {
71    if (!mStarted) {
72        return UNKNOWN_ERROR;
73    }
74
75    mRecord->stop();
76
77    delete mGroup;
78    mGroup = NULL;
79
80    mStarted = false;
81
82    return OK;
83}
84
85sp<MetaData> AudioSource::getFormat() {
86    sp<MetaData> meta = new MetaData;
87    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
88    meta->setInt32(kKeySampleRate, mRecord->getSampleRate());
89    meta->setInt32(kKeyChannelCount, mRecord->channelCount());
90    meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
91
92    return meta;
93}
94
95status_t AudioSource::read(
96        MediaBuffer **out, const ReadOptions *options) {
97    *out = NULL;
98
99    MediaBuffer *buffer;
100    CHECK_EQ(mGroup->acquire_buffer(&buffer), OK);
101
102    uint32_t numFramesRecorded;
103    mRecord->getPosition(&numFramesRecorded);
104
105    buffer->meta_data()->setInt64(
106            kKeyTime,
107            (1000000ll * numFramesRecorded) / mRecord->getSampleRate()
108            - mRecord->latency() * 1000);
109
110    ssize_t n = mRecord->read(buffer->data(), buffer->size());
111
112    if (n < 0) {
113        buffer->release();
114        buffer = NULL;
115
116        return (status_t)n;
117    }
118
119    buffer->set_range(0, n);
120
121    *out = buffer;
122
123    return OK;
124}
125
126}  // namespace android
127