AudioSource.cpp revision f1ae1963f5028a670573b50a9c1cfb504fc426b4
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#include <cutils/properties.h>
29#include <stdlib.h>
30
31namespace android {
32
33AudioSource::AudioSource(
34        int inputSource, uint32_t sampleRate, uint32_t channels)
35    : mStarted(false),
36      mCollectStats(false),
37      mPrevSampleTimeUs(0),
38      mTotalLostFrames(0),
39      mPrevLostBytes(0),
40      mGroup(NULL) {
41
42    LOGV("sampleRate: %d, channels: %d", sampleRate, channels);
43    CHECK(channels == 1 || channels == 2);
44    uint32_t flags = AudioRecord::RECORD_AGC_ENABLE |
45                     AudioRecord::RECORD_NS_ENABLE  |
46                     AudioRecord::RECORD_IIR_ENABLE;
47
48    mRecord = new AudioRecord(
49                inputSource, sampleRate, AudioSystem::PCM_16_BIT,
50                channels > 1? AudioSystem::CHANNEL_IN_STEREO: AudioSystem::CHANNEL_IN_MONO,
51                4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */
52                flags);
53
54    mInitCheck = mRecord->initCheck();
55}
56
57AudioSource::~AudioSource() {
58    if (mStarted) {
59        stop();
60    }
61
62    delete mRecord;
63    mRecord = NULL;
64}
65
66status_t AudioSource::initCheck() const {
67    return mInitCheck;
68}
69
70status_t AudioSource::start(MetaData *params) {
71    if (mStarted) {
72        return UNKNOWN_ERROR;
73    }
74
75    char value[PROPERTY_VALUE_MAX];
76    if (property_get("media.stagefright.record-stats", value, NULL)
77        && (!strcmp(value, "1") || !strcasecmp(value, "true"))) {
78        mCollectStats = true;
79    }
80
81    mTrackMaxAmplitude = false;
82    mMaxAmplitude = 0;
83    mStartTimeUs = 0;
84    int64_t startTimeUs;
85    if (params && params->findInt64(kKeyTime, &startTimeUs)) {
86        mStartTimeUs = startTimeUs;
87    }
88    status_t err = mRecord->start();
89
90    if (err == OK) {
91        mGroup = new MediaBufferGroup;
92        mGroup->add_buffer(new MediaBuffer(kMaxBufferSize));
93
94        mStarted = true;
95    }
96
97    return err;
98}
99
100status_t AudioSource::stop() {
101    if (!mStarted) {
102        return UNKNOWN_ERROR;
103    }
104
105    mRecord->stop();
106
107    delete mGroup;
108    mGroup = NULL;
109
110    mStarted = false;
111
112    if (mCollectStats) {
113        LOGI("Total lost audio frames: %lld",
114            mTotalLostFrames + (mPrevLostBytes >> 1));
115    }
116
117    return OK;
118}
119
120sp<MetaData> AudioSource::getFormat() {
121    sp<MetaData> meta = new MetaData;
122    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
123    meta->setInt32(kKeySampleRate, mRecord->getSampleRate());
124    meta->setInt32(kKeyChannelCount, mRecord->channelCount());
125    meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
126
127    return meta;
128}
129
130/*
131 * Returns -1 if frame skipping request is too long.
132 * Returns  0 if there is no need to skip frames.
133 * Returns  1 if we need to skip frames.
134 */
135static int skipFrame(int64_t timestampUs,
136        const MediaSource::ReadOptions *options) {
137
138    int64_t skipFrameUs;
139    if (!options || !options->getSkipFrame(&skipFrameUs)) {
140        return 0;
141    }
142
143    if (skipFrameUs <= timestampUs) {
144        return 0;
145    }
146
147    // Safe guard against the abuse of the kSkipFrame_Option.
148    if (skipFrameUs - timestampUs >= 1E6) {
149        LOGE("Frame skipping requested is way too long: %lld us",
150            skipFrameUs - timestampUs);
151
152        return -1;
153    }
154
155    LOGV("skipFrame: %lld us > timestamp: %lld us",
156        skipFrameUs, timestampUs);
157
158    return 1;
159
160}
161
162void AudioSource::rampVolume(
163        int32_t startFrame, int32_t rampDurationFrames,
164        uint8_t *data,   size_t bytes) {
165
166    const int32_t kShift = 14;
167    int32_t fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
168    const int32_t nChannels = mRecord->channelCount();
169    int32_t stopFrame = startFrame + bytes / sizeof(int16_t);
170    int16_t *frame = (int16_t *) data;
171    if (stopFrame > rampDurationFrames) {
172        stopFrame = rampDurationFrames;
173    }
174
175    while (startFrame < stopFrame) {
176        if (nChannels == 1) {  // mono
177            frame[0] = (frame[0] * fixedMultiplier) >> kShift;
178            ++frame;
179            ++startFrame;
180        } else {               // stereo
181            frame[0] = (frame[0] * fixedMultiplier) >> kShift;
182            frame[1] = (frame[1] * fixedMultiplier) >> kShift;
183            frame += 2;
184            startFrame += 2;
185        }
186
187        // Update the multiplier every 4 frames
188        if ((startFrame & 3) == 0) {
189            fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
190        }
191    }
192}
193
194status_t AudioSource::read(
195        MediaBuffer **out, const ReadOptions *options) {
196    *out = NULL;
197
198    MediaBuffer *buffer;
199    CHECK_EQ(mGroup->acquire_buffer(&buffer), OK);
200
201    int err = 0;
202    while (mStarted) {
203
204        uint32_t numFramesRecorded;
205        mRecord->getPosition(&numFramesRecorded);
206
207
208        if (numFramesRecorded == 0 && mPrevSampleTimeUs == 0) {
209            // Initial delay
210            if (mStartTimeUs > 0) {
211                mStartTimeUs = systemTime() / 1000 - mStartTimeUs;
212            } else {
213                // Assume latency is constant.
214                mStartTimeUs += mRecord->latency() * 1000;
215            }
216            mPrevSampleTimeUs = mStartTimeUs;
217        }
218
219        uint32_t sampleRate = mRecord->getSampleRate();
220
221        // Insert null frames when lost frames are detected.
222        int64_t timestampUs = mPrevSampleTimeUs;
223        uint32_t numLostBytes = mRecord->getInputFramesLost() << 1;
224        numLostBytes += mPrevLostBytes;
225#if 0
226        // Simulate lost frames
227        numLostBytes = ((rand() * 1.0 / RAND_MAX)) * 2 * kMaxBufferSize;
228        numLostBytes &= 0xFFFFFFFE; // Alignment requirement
229
230        // Reduce the chance to lose
231        if (rand() * 1.0 / RAND_MAX >= 0.05) {
232            numLostBytes = 0;
233        }
234#endif
235        if (numLostBytes > 0) {
236            if (numLostBytes > kMaxBufferSize) {
237                mPrevLostBytes = numLostBytes - kMaxBufferSize;
238                numLostBytes = kMaxBufferSize;
239            }
240
241            CHECK_EQ(numLostBytes & 1, 0);
242            timestampUs += ((1000000LL * (numLostBytes >> 1)) +
243                    (sampleRate >> 1)) / sampleRate;
244
245            CHECK(timestampUs > mPrevSampleTimeUs);
246            if (mCollectStats) {
247                mTotalLostFrames += (numLostBytes >> 1);
248            }
249            if ((err = skipFrame(timestampUs, options)) == -1) {
250                buffer->release();
251                return UNKNOWN_ERROR;
252            } else if (err != 0) {
253                continue;
254            }
255            memset(buffer->data(), 0, numLostBytes);
256            buffer->set_range(0, numLostBytes);
257            buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs);
258            mPrevSampleTimeUs = timestampUs;
259            *out = buffer;
260            return OK;
261        }
262
263        ssize_t n = mRecord->read(buffer->data(), buffer->size());
264        if (n < 0) {
265            buffer->release();
266            return (status_t)n;
267        }
268
269        int64_t recordDurationUs = (1000000LL * n >> 1) / sampleRate;
270        timestampUs += recordDurationUs;
271        if ((err = skipFrame(timestampUs, options)) == -1) {
272            buffer->release();
273            return UNKNOWN_ERROR;
274        } else if (err != 0) {
275            continue;
276        }
277
278        if (mPrevSampleTimeUs - mStartTimeUs < kAutoRampStartUs) {
279            // Mute the initial video recording signal
280            memset((uint8_t *) buffer->data(), 0, n);
281        } else if (mPrevSampleTimeUs - mStartTimeUs < kAutoRampStartUs + kAutoRampDurationUs) {
282            int32_t autoRampDurationFrames =
283                    (kAutoRampDurationUs * sampleRate + 500000LL) / 1000000LL;
284
285            int32_t autoRampStartFrames =
286                    (kAutoRampStartUs * sampleRate + 500000LL) / 1000000LL;
287
288            int32_t nFrames = numFramesRecorded - autoRampStartFrames;
289            rampVolume(nFrames, autoRampDurationFrames, (uint8_t *) buffer->data(), n);
290        }
291        if (mTrackMaxAmplitude) {
292            trackMaxAmplitude((int16_t *) buffer->data(), n >> 1);
293        }
294
295        buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs);
296        CHECK(timestampUs > mPrevSampleTimeUs);
297        if (mTotalLostFrames == 0) {
298            CHECK_EQ(mPrevSampleTimeUs,
299                mStartTimeUs + (1000000LL * numFramesRecorded) / sampleRate);
300        }
301        mPrevSampleTimeUs = timestampUs;
302        LOGV("initial delay: %lld, sample rate: %d, timestamp: %lld",
303                mStartTimeUs, sampleRate, timestampUs);
304
305        buffer->set_range(0, n);
306
307        *out = buffer;
308        return OK;
309    }
310
311    return OK;
312}
313
314void AudioSource::trackMaxAmplitude(int16_t *data, int nSamples) {
315    for (int i = nSamples; i > 0; --i) {
316        int16_t value = *data++;
317        if (value < 0) {
318            value = -value;
319        }
320        if (mMaxAmplitude < value) {
321            mMaxAmplitude = value;
322        }
323    }
324}
325
326int16_t AudioSource::getMaxAmplitude() {
327    // First call activates the tracking.
328    if (!mTrackMaxAmplitude) {
329        mTrackMaxAmplitude = true;
330    }
331    int16_t value = mMaxAmplitude;
332    mMaxAmplitude = 0;
333    LOGV("max amplitude since last call: %d", value);
334    return value;
335}
336
337}  // namespace android
338