AudioSource.cpp revision f92eec53f886f43e4374a36195be55f2a7bbcf36
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/AudioRecord.h>
22#include <media/stagefright/AudioSource.h>
23#include <media/stagefright/MediaBuffer.h>
24#include <media/stagefright/MediaDefs.h>
25#include <media/stagefright/MetaData.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <cutils/properties.h>
28#include <stdlib.h>
29
30namespace android {
31
32static void AudioRecordCallbackFunction(int event, void *user, void *info) {
33    AudioSource *source = (AudioSource *) user;
34    switch (event) {
35        case AudioRecord::EVENT_MORE_DATA: {
36            source->dataCallbackTimestamp(*((AudioRecord::Buffer *) info), systemTime() / 1000);
37            break;
38        }
39        case AudioRecord::EVENT_OVERRUN: {
40            ALOGW("AudioRecord reported overrun!");
41            break;
42        }
43        default:
44            // does nothing
45            break;
46    }
47}
48
49AudioSource::AudioSource(
50        audio_source_t inputSource, uint32_t sampleRate, uint32_t channelCount)
51    : mStarted(false),
52      mSampleRate(sampleRate),
53      mPrevSampleTimeUs(0),
54      mNumFramesReceived(0),
55      mNumClientOwnedBuffers(0) {
56
57    ALOGV("sampleRate: %d, channelCount: %d", sampleRate, channelCount);
58    CHECK(channelCount == 1 || channelCount == 2);
59
60    int minFrameCount;
61    status_t status = AudioRecord::getMinFrameCount(&minFrameCount,
62                                           sampleRate,
63                                           AUDIO_FORMAT_PCM_16_BIT,
64                                           channelCount);
65    if (status == OK) {
66        // make sure that the AudioRecord callback never returns more than the maximum
67        // buffer size
68        int frameCount = kMaxBufferSize / sizeof(int16_t) / channelCount;
69
70        // make sure that the AudioRecord total buffer size is large enough
71        int bufCount = 2;
72        while ((bufCount * frameCount) < minFrameCount) {
73            bufCount++;
74        }
75
76        mRecord = new AudioRecord(
77                    inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT,
78                    audio_channel_in_mask_from_count(channelCount),
79                    bufCount * frameCount,
80                    AudioRecordCallbackFunction,
81                    this,
82                    frameCount);
83        mInitCheck = mRecord->initCheck();
84    } else {
85        mInitCheck = status;
86    }
87}
88
89AudioSource::~AudioSource() {
90    if (mStarted) {
91        reset();
92    }
93
94    delete mRecord;
95    mRecord = NULL;
96}
97
98status_t AudioSource::initCheck() const {
99    return mInitCheck;
100}
101
102status_t AudioSource::start(MetaData *params) {
103    Mutex::Autolock autoLock(mLock);
104    if (mStarted) {
105        return UNKNOWN_ERROR;
106    }
107
108    if (mInitCheck != OK) {
109        return NO_INIT;
110    }
111
112    mTrackMaxAmplitude = false;
113    mMaxAmplitude = 0;
114    mInitialReadTimeUs = 0;
115    mStartTimeUs = 0;
116    int64_t startTimeUs;
117    if (params && params->findInt64(kKeyTime, &startTimeUs)) {
118        mStartTimeUs = startTimeUs;
119    }
120    status_t err = mRecord->start();
121    if (err == OK) {
122        mStarted = true;
123    } else {
124        delete mRecord;
125        mRecord = NULL;
126    }
127
128
129    return err;
130}
131
132void AudioSource::releaseQueuedFrames_l() {
133    ALOGV("releaseQueuedFrames_l");
134    List<MediaBuffer *>::iterator it;
135    while (!mBuffersReceived.empty()) {
136        it = mBuffersReceived.begin();
137        (*it)->release();
138        mBuffersReceived.erase(it);
139    }
140}
141
142void AudioSource::waitOutstandingEncodingFrames_l() {
143    ALOGV("waitOutstandingEncodingFrames_l: %lld", mNumClientOwnedBuffers);
144    while (mNumClientOwnedBuffers > 0) {
145        mFrameEncodingCompletionCondition.wait(mLock);
146    }
147}
148
149status_t AudioSource::reset() {
150    Mutex::Autolock autoLock(mLock);
151    if (!mStarted) {
152        return UNKNOWN_ERROR;
153    }
154
155    if (mInitCheck != OK) {
156        return NO_INIT;
157    }
158
159    mStarted = false;
160    mRecord->stop();
161    waitOutstandingEncodingFrames_l();
162    releaseQueuedFrames_l();
163
164    return OK;
165}
166
167sp<MetaData> AudioSource::getFormat() {
168    Mutex::Autolock autoLock(mLock);
169    if (mInitCheck != OK) {
170        return 0;
171    }
172
173    sp<MetaData> meta = new MetaData;
174    meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
175    meta->setInt32(kKeySampleRate, mSampleRate);
176    meta->setInt32(kKeyChannelCount, mRecord->channelCount());
177    meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
178
179    return meta;
180}
181
182void AudioSource::rampVolume(
183        int32_t startFrame, int32_t rampDurationFrames,
184        uint8_t *data,   size_t bytes) {
185
186    const int32_t kShift = 14;
187    int32_t fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
188    const int32_t nChannels = mRecord->channelCount();
189    int32_t stopFrame = startFrame + bytes / sizeof(int16_t);
190    int16_t *frame = (int16_t *) data;
191    if (stopFrame > rampDurationFrames) {
192        stopFrame = rampDurationFrames;
193    }
194
195    while (startFrame < stopFrame) {
196        if (nChannels == 1) {  // mono
197            frame[0] = (frame[0] * fixedMultiplier) >> kShift;
198            ++frame;
199            ++startFrame;
200        } else {               // stereo
201            frame[0] = (frame[0] * fixedMultiplier) >> kShift;
202            frame[1] = (frame[1] * fixedMultiplier) >> kShift;
203            frame += 2;
204            startFrame += 2;
205        }
206
207        // Update the multiplier every 4 frames
208        if ((startFrame & 3) == 0) {
209            fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
210        }
211    }
212}
213
214status_t AudioSource::read(
215        MediaBuffer **out, const ReadOptions *options) {
216    Mutex::Autolock autoLock(mLock);
217    *out = NULL;
218
219    if (mInitCheck != OK) {
220        return NO_INIT;
221    }
222
223    while (mStarted && mBuffersReceived.empty()) {
224        mFrameAvailableCondition.wait(mLock);
225    }
226    if (!mStarted) {
227        return OK;
228    }
229    MediaBuffer *buffer = *mBuffersReceived.begin();
230    mBuffersReceived.erase(mBuffersReceived.begin());
231    ++mNumClientOwnedBuffers;
232    buffer->setObserver(this);
233    buffer->add_ref();
234
235    // Mute/suppress the recording sound
236    int64_t timeUs;
237    CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
238    int64_t elapsedTimeUs = timeUs - mStartTimeUs;
239    if (elapsedTimeUs < kAutoRampStartUs) {
240        memset((uint8_t *) buffer->data(), 0, buffer->range_length());
241    } else if (elapsedTimeUs < kAutoRampStartUs + kAutoRampDurationUs) {
242        int32_t autoRampDurationFrames =
243                    (kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL;
244
245        int32_t autoRampStartFrames =
246                    (kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL;
247
248        int32_t nFrames = mNumFramesReceived - autoRampStartFrames;
249        rampVolume(nFrames, autoRampDurationFrames,
250                (uint8_t *) buffer->data(), buffer->range_length());
251    }
252
253    // Track the max recording signal amplitude.
254    if (mTrackMaxAmplitude) {
255        trackMaxAmplitude(
256            (int16_t *) buffer->data(), buffer->range_length() >> 1);
257    }
258
259    *out = buffer;
260    return OK;
261}
262
263void AudioSource::signalBufferReturned(MediaBuffer *buffer) {
264    ALOGV("signalBufferReturned: %p", buffer->data());
265    Mutex::Autolock autoLock(mLock);
266    --mNumClientOwnedBuffers;
267    buffer->setObserver(0);
268    buffer->release();
269    mFrameEncodingCompletionCondition.signal();
270    return;
271}
272
273status_t AudioSource::dataCallbackTimestamp(
274        const AudioRecord::Buffer& audioBuffer, int64_t timeUs) {
275    ALOGV("dataCallbackTimestamp: %lld us", timeUs);
276    Mutex::Autolock autoLock(mLock);
277    if (!mStarted) {
278        ALOGW("Spurious callback from AudioRecord. Drop the audio data.");
279        return OK;
280    }
281
282    // Drop retrieved and previously lost audio data.
283    if (mNumFramesReceived == 0 && timeUs < mStartTimeUs) {
284        mRecord->getInputFramesLost();
285        ALOGV("Drop audio data at %lld/%lld us", timeUs, mStartTimeUs);
286        return OK;
287    }
288
289    if (mNumFramesReceived == 0 && mPrevSampleTimeUs == 0) {
290        mInitialReadTimeUs = timeUs;
291        // Initial delay
292        if (mStartTimeUs > 0) {
293            mStartTimeUs = timeUs - mStartTimeUs;
294        } else {
295            // Assume latency is constant.
296            mStartTimeUs += mRecord->latency() * 1000;
297        }
298        mPrevSampleTimeUs = mStartTimeUs;
299    }
300
301    size_t numLostBytes = 0;
302    if (mNumFramesReceived > 0) {  // Ignore earlier frame lost
303        // getInputFramesLost() returns the number of lost frames.
304        // Convert number of frames lost to number of bytes lost.
305        numLostBytes = mRecord->getInputFramesLost() * mRecord->frameSize();
306    }
307
308    CHECK_EQ(numLostBytes & 1, 0u);
309    CHECK_EQ(audioBuffer.size & 1, 0u);
310    if (numLostBytes > 0) {
311        // Loss of audio frames should happen rarely; thus the LOGW should
312        // not cause a logging spam
313        ALOGW("Lost audio record data: %d bytes", numLostBytes);
314    }
315
316    while (numLostBytes > 0) {
317        size_t bufferSize = numLostBytes;
318        if (numLostBytes > kMaxBufferSize) {
319            numLostBytes -= kMaxBufferSize;
320            bufferSize = kMaxBufferSize;
321        } else {
322            numLostBytes = 0;
323        }
324        MediaBuffer *lostAudioBuffer = new MediaBuffer(bufferSize);
325        memset(lostAudioBuffer->data(), 0, bufferSize);
326        lostAudioBuffer->set_range(0, bufferSize);
327        queueInputBuffer_l(lostAudioBuffer, timeUs);
328    }
329
330    if (audioBuffer.size == 0) {
331        ALOGW("Nothing is available from AudioRecord callback buffer");
332        return OK;
333    }
334
335    const size_t bufferSize = audioBuffer.size;
336    MediaBuffer *buffer = new MediaBuffer(bufferSize);
337    memcpy((uint8_t *) buffer->data(),
338            audioBuffer.i16, audioBuffer.size);
339    buffer->set_range(0, bufferSize);
340    queueInputBuffer_l(buffer, timeUs);
341    return OK;
342}
343
344void AudioSource::queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs) {
345    const size_t bufferSize = buffer->range_length();
346    const size_t frameSize = mRecord->frameSize();
347    const int64_t timestampUs =
348                mPrevSampleTimeUs +
349                    ((1000000LL * (bufferSize / frameSize)) +
350                        (mSampleRate >> 1)) / mSampleRate;
351
352    if (mNumFramesReceived == 0) {
353        buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs);
354    }
355
356    buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs);
357    buffer->meta_data()->setInt64(kKeyDriftTime, timeUs - mInitialReadTimeUs);
358    mPrevSampleTimeUs = timestampUs;
359    mNumFramesReceived += bufferSize / frameSize;
360    mBuffersReceived.push_back(buffer);
361    mFrameAvailableCondition.signal();
362}
363
364void AudioSource::trackMaxAmplitude(int16_t *data, int nSamples) {
365    for (int i = nSamples; i > 0; --i) {
366        int16_t value = *data++;
367        if (value < 0) {
368            value = -value;
369        }
370        if (mMaxAmplitude < value) {
371            mMaxAmplitude = value;
372        }
373    }
374}
375
376int16_t AudioSource::getMaxAmplitude() {
377    // First call activates the tracking.
378    if (!mTrackMaxAmplitude) {
379        mTrackMaxAmplitude = true;
380    }
381    int16_t value = mMaxAmplitude;
382    mMaxAmplitude = 0;
383    ALOGV("max amplitude since last call: %d", value);
384    return value;
385}
386
387}  // namespace android
388