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