blob: cbe709b97297bb4e836916e99e00b6d55864f8e4 [file] [log] [blame]
Andreas Huber07bf09d2010-01-25 14:27:12 -08001/*
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
James Dongabed93a2010-04-22 17:27:04 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "AudioSource"
19#include <utils/Log.h>
20
Andreas Huber07bf09d2010-01-25 14:27:12 -080021#include <media/AudioRecord.h>
James Dongd33a4cc2011-02-15 10:08:07 -080022#include <media/stagefright/AudioSource.h>
23#include <media/stagefright/MediaBuffer.h>
Andreas Huber07bf09d2010-01-25 14:27:12 -080024#include <media/stagefright/MediaDefs.h>
25#include <media/stagefright/MetaData.h>
James Dongd33a4cc2011-02-15 10:08:07 -080026#include <media/stagefright/foundation/ADebug.h>
James Dongdae9fd32010-06-04 13:59:27 -070027#include <cutils/properties.h>
James Dongc3ae9372010-07-29 18:06:08 -070028#include <stdlib.h>
Andreas Huber07bf09d2010-01-25 14:27:12 -080029
30namespace android {
31
James Dongd33a4cc2011-02-15 10:08:07 -080032static 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: {
Steve Block8564c8d2012-01-05 23:22:43 +000040 ALOGW("AudioRecord reported overrun!");
James Dongd33a4cc2011-02-15 10:08:07 -080041 break;
42 }
43 default:
44 // does nothing
45 break;
46 }
47}
48
Andreas Huber07bf09d2010-01-25 14:27:12 -080049AudioSource::AudioSource(
Glenn Kasten0f0fbd92012-01-23 13:58:49 -080050 audio_source_t inputSource, uint32_t sampleRate, uint32_t channels)
James Dongd77d2a92010-06-14 17:45:35 -070051 : mStarted(false),
James Dongd33a4cc2011-02-15 10:08:07 -080052 mSampleRate(sampleRate),
James Dongc3ae9372010-07-29 18:06:08 -070053 mPrevSampleTimeUs(0),
James Dongd33a4cc2011-02-15 10:08:07 -080054 mNumFramesReceived(0),
55 mNumClientOwnedBuffers(0) {
James Dongd77d2a92010-06-14 17:45:35 -070056
Steve Block71f2cf12011-10-20 11:56:00 +010057 ALOGV("sampleRate: %d, channels: %d", sampleRate, channels);
James Dongd7f1c3d2010-08-26 16:28:17 -070058 CHECK(channels == 1 || channels == 2);
Glenn Kastenf60a5d72012-03-07 09:04:02 -080059 AudioRecord::record_flags flags = (AudioRecord::record_flags)
60 (AudioRecord::RECORD_AGC_ENABLE |
James Dongd77d2a92010-06-14 17:45:35 -070061 AudioRecord::RECORD_NS_ENABLE |
Glenn Kastenf60a5d72012-03-07 09:04:02 -080062 AudioRecord::RECORD_IIR_ENABLE);
James Dongd77d2a92010-06-14 17:45:35 -070063 mRecord = new AudioRecord(
Dima Zavin24fc2fb2011-04-19 22:30:36 -070064 inputSource, sampleRate, AUDIO_FORMAT_PCM_16_BIT,
65 channels > 1? AUDIO_CHANNEL_IN_STEREO: AUDIO_CHANNEL_IN_MONO,
James Dongd77d2a92010-06-14 17:45:35 -070066 4 * kMaxBufferSize / sizeof(int16_t), /* Enable ping-pong buffers */
James Dongd33a4cc2011-02-15 10:08:07 -080067 flags,
68 AudioRecordCallbackFunction,
69 this);
James Dongd77d2a92010-06-14 17:45:35 -070070
71 mInitCheck = mRecord->initCheck();
Andreas Huber07bf09d2010-01-25 14:27:12 -080072}
73
74AudioSource::~AudioSource() {
75 if (mStarted) {
James Dong946ab0f2012-02-02 18:04:02 -080076 reset();
Andreas Huber07bf09d2010-01-25 14:27:12 -080077 }
78
79 delete mRecord;
80 mRecord = NULL;
81}
82
83status_t AudioSource::initCheck() const {
84 return mInitCheck;
85}
86
87status_t AudioSource::start(MetaData *params) {
James Dongd33a4cc2011-02-15 10:08:07 -080088 Mutex::Autolock autoLock(mLock);
Andreas Huber07bf09d2010-01-25 14:27:12 -080089 if (mStarted) {
90 return UNKNOWN_ERROR;
91 }
92
James Donga87544b2010-09-01 14:02:51 -070093 if (mInitCheck != OK) {
94 return NO_INIT;
95 }
96
James Dong57e7f832010-06-24 19:55:31 -070097 mTrackMaxAmplitude = false;
98 mMaxAmplitude = 0;
James Dong4c238152010-09-01 18:48:35 -070099 mInitialReadTimeUs = 0;
James Dong36e573b2010-06-19 09:04:18 -0700100 mStartTimeUs = 0;
101 int64_t startTimeUs;
102 if (params && params->findInt64(kKeyTime, &startTimeUs)) {
103 mStartTimeUs = startTimeUs;
104 }
Andreas Huber07bf09d2010-01-25 14:27:12 -0800105 status_t err = mRecord->start();
Andreas Huber07bf09d2010-01-25 14:27:12 -0800106 if (err == OK) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800107 mStarted = true;
James Dongd7ef5b62011-01-25 12:37:43 -0800108 } else {
109 delete mRecord;
110 mRecord = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800111 }
112
James Dongd7ef5b62011-01-25 12:37:43 -0800113
Andreas Huber07bf09d2010-01-25 14:27:12 -0800114 return err;
115}
116
James Dongd33a4cc2011-02-15 10:08:07 -0800117void AudioSource::releaseQueuedFrames_l() {
Steve Block71f2cf12011-10-20 11:56:00 +0100118 ALOGV("releaseQueuedFrames_l");
James Dongd33a4cc2011-02-15 10:08:07 -0800119 List<MediaBuffer *>::iterator it;
120 while (!mBuffersReceived.empty()) {
121 it = mBuffersReceived.begin();
122 (*it)->release();
123 mBuffersReceived.erase(it);
124 }
125}
126
127void AudioSource::waitOutstandingEncodingFrames_l() {
Steve Block71f2cf12011-10-20 11:56:00 +0100128 ALOGV("waitOutstandingEncodingFrames_l: %lld", mNumClientOwnedBuffers);
James Dongd33a4cc2011-02-15 10:08:07 -0800129 while (mNumClientOwnedBuffers > 0) {
130 mFrameEncodingCompletionCondition.wait(mLock);
131 }
132}
133
James Dong946ab0f2012-02-02 18:04:02 -0800134status_t AudioSource::reset() {
James Dongd33a4cc2011-02-15 10:08:07 -0800135 Mutex::Autolock autoLock(mLock);
Andreas Huber07bf09d2010-01-25 14:27:12 -0800136 if (!mStarted) {
137 return UNKNOWN_ERROR;
138 }
139
James Donga87544b2010-09-01 14:02:51 -0700140 if (mInitCheck != OK) {
141 return NO_INIT;
142 }
143
Andreas Huber07bf09d2010-01-25 14:27:12 -0800144 mStarted = false;
James Dongd33a4cc2011-02-15 10:08:07 -0800145 mRecord->stop();
146 waitOutstandingEncodingFrames_l();
147 releaseQueuedFrames_l();
James Dongdae9fd32010-06-04 13:59:27 -0700148
Andreas Huber07bf09d2010-01-25 14:27:12 -0800149 return OK;
150}
151
152sp<MetaData> AudioSource::getFormat() {
James Dongd33a4cc2011-02-15 10:08:07 -0800153 Mutex::Autolock autoLock(mLock);
James Donga87544b2010-09-01 14:02:51 -0700154 if (mInitCheck != OK) {
155 return 0;
156 }
157
Andreas Huber07bf09d2010-01-25 14:27:12 -0800158 sp<MetaData> meta = new MetaData;
159 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
James Dongd33a4cc2011-02-15 10:08:07 -0800160 meta->setInt32(kKeySampleRate, mSampleRate);
Andreas Huber07bf09d2010-01-25 14:27:12 -0800161 meta->setInt32(kKeyChannelCount, mRecord->channelCount());
162 meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
163
164 return meta;
165}
166
James Dongd7f1c3d2010-08-26 16:28:17 -0700167void AudioSource::rampVolume(
168 int32_t startFrame, int32_t rampDurationFrames,
169 uint8_t *data, size_t bytes) {
170
171 const int32_t kShift = 14;
172 int32_t fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
173 const int32_t nChannels = mRecord->channelCount();
174 int32_t stopFrame = startFrame + bytes / sizeof(int16_t);
175 int16_t *frame = (int16_t *) data;
176 if (stopFrame > rampDurationFrames) {
177 stopFrame = rampDurationFrames;
178 }
179
180 while (startFrame < stopFrame) {
181 if (nChannels == 1) { // mono
182 frame[0] = (frame[0] * fixedMultiplier) >> kShift;
183 ++frame;
184 ++startFrame;
185 } else { // stereo
186 frame[0] = (frame[0] * fixedMultiplier) >> kShift;
187 frame[1] = (frame[1] * fixedMultiplier) >> kShift;
188 frame += 2;
189 startFrame += 2;
190 }
191
192 // Update the multiplier every 4 frames
193 if ((startFrame & 3) == 0) {
194 fixedMultiplier = (startFrame << kShift) / rampDurationFrames;
195 }
196 }
197}
198
Andreas Huber07bf09d2010-01-25 14:27:12 -0800199status_t AudioSource::read(
200 MediaBuffer **out, const ReadOptions *options) {
James Dongd33a4cc2011-02-15 10:08:07 -0800201 Mutex::Autolock autoLock(mLock);
202 *out = NULL;
James Donga87544b2010-09-01 14:02:51 -0700203
204 if (mInitCheck != OK) {
205 return NO_INIT;
206 }
207
James Dongd33a4cc2011-02-15 10:08:07 -0800208 while (mStarted && mBuffersReceived.empty()) {
209 mFrameAvailableCondition.wait(mLock);
210 }
211 if (!mStarted) {
James Dong53d4e0d2010-07-21 14:51:35 -0700212 return OK;
James Dongdae9fd32010-06-04 13:59:27 -0700213 }
James Dongd33a4cc2011-02-15 10:08:07 -0800214 MediaBuffer *buffer = *mBuffersReceived.begin();
215 mBuffersReceived.erase(mBuffersReceived.begin());
216 ++mNumClientOwnedBuffers;
217 buffer->setObserver(this);
218 buffer->add_ref();
219
220 // Mute/suppress the recording sound
221 int64_t timeUs;
222 CHECK(buffer->meta_data()->findInt64(kKeyTime, &timeUs));
223 int64_t elapsedTimeUs = timeUs - mStartTimeUs;
224 if (elapsedTimeUs < kAutoRampStartUs) {
225 memset((uint8_t *) buffer->data(), 0, buffer->range_length());
226 } else if (elapsedTimeUs < kAutoRampStartUs + kAutoRampDurationUs) {
227 int32_t autoRampDurationFrames =
228 (kAutoRampDurationUs * mSampleRate + 500000LL) / 1000000LL;
229
230 int32_t autoRampStartFrames =
231 (kAutoRampStartUs * mSampleRate + 500000LL) / 1000000LL;
232
233 int32_t nFrames = mNumFramesReceived - autoRampStartFrames;
234 rampVolume(nFrames, autoRampDurationFrames,
235 (uint8_t *) buffer->data(), buffer->range_length());
236 }
237
238 // Track the max recording signal amplitude.
239 if (mTrackMaxAmplitude) {
240 trackMaxAmplitude(
241 (int16_t *) buffer->data(), buffer->range_length() >> 1);
242 }
243
244 *out = buffer;
245 return OK;
246}
247
248void AudioSource::signalBufferReturned(MediaBuffer *buffer) {
Steve Block71f2cf12011-10-20 11:56:00 +0100249 ALOGV("signalBufferReturned: %p", buffer->data());
James Dongd33a4cc2011-02-15 10:08:07 -0800250 Mutex::Autolock autoLock(mLock);
251 --mNumClientOwnedBuffers;
252 buffer->setObserver(0);
253 buffer->release();
254 mFrameEncodingCompletionCondition.signal();
255 return;
256}
257
258status_t AudioSource::dataCallbackTimestamp(
259 const AudioRecord::Buffer& audioBuffer, int64_t timeUs) {
Steve Block71f2cf12011-10-20 11:56:00 +0100260 ALOGV("dataCallbackTimestamp: %lld us", timeUs);
James Dongd33a4cc2011-02-15 10:08:07 -0800261 Mutex::Autolock autoLock(mLock);
262 if (!mStarted) {
Steve Block8564c8d2012-01-05 23:22:43 +0000263 ALOGW("Spurious callback from AudioRecord. Drop the audio data.");
James Dongd33a4cc2011-02-15 10:08:07 -0800264 return OK;
265 }
266
James Dongcbeebb12011-02-16 12:28:26 -0800267 // Drop retrieved and previously lost audio data.
268 if (mNumFramesReceived == 0 && timeUs < mStartTimeUs) {
269 mRecord->getInputFramesLost();
Steve Block71f2cf12011-10-20 11:56:00 +0100270 ALOGV("Drop audio data at %lld/%lld us", timeUs, mStartTimeUs);
James Dongcbeebb12011-02-16 12:28:26 -0800271 return OK;
272 }
273
James Dongd33a4cc2011-02-15 10:08:07 -0800274 if (mNumFramesReceived == 0 && mPrevSampleTimeUs == 0) {
275 mInitialReadTimeUs = timeUs;
276 // Initial delay
277 if (mStartTimeUs > 0) {
278 mStartTimeUs = timeUs - mStartTimeUs;
279 } else {
280 // Assume latency is constant.
281 mStartTimeUs += mRecord->latency() * 1000;
282 }
283 mPrevSampleTimeUs = mStartTimeUs;
284 }
285
James Dongcbeebb12011-02-16 12:28:26 -0800286 size_t numLostBytes = 0;
287 if (mNumFramesReceived > 0) { // Ignore earlier frame lost
288 // getInputFramesLost() returns the number of lost frames.
289 // Convert number of frames lost to number of bytes lost.
290 numLostBytes = mRecord->getInputFramesLost() * mRecord->frameSize();
291 }
292
James Dongd33a4cc2011-02-15 10:08:07 -0800293 CHECK_EQ(numLostBytes & 1, 0u);
294 CHECK_EQ(audioBuffer.size & 1, 0u);
James Dongd33a4cc2011-02-15 10:08:07 -0800295 if (numLostBytes > 0) {
James Dong745fcc02012-02-14 14:58:20 -0800296 // Loss of audio frames should happen rarely; thus the LOGW should
297 // not cause a logging spam
298 ALOGW("Lost audio record data: %d bytes", numLostBytes);
James Dongd33a4cc2011-02-15 10:08:07 -0800299 }
300
James Dong745fcc02012-02-14 14:58:20 -0800301 while (numLostBytes > 0) {
302 size_t bufferSize = numLostBytes;
303 if (numLostBytes > kMaxBufferSize) {
304 numLostBytes -= kMaxBufferSize;
305 bufferSize = kMaxBufferSize;
306 } else {
307 numLostBytes = 0;
308 }
309 MediaBuffer *lostAudioBuffer = new MediaBuffer(bufferSize);
310 memset(lostAudioBuffer->data(), 0, bufferSize);
311 lostAudioBuffer->set_range(0, bufferSize);
312 queueInputBuffer_l(lostAudioBuffer, timeUs);
313 }
314
315 if (audioBuffer.size == 0) {
316 ALOGW("Nothing is available from AudioRecord callback buffer");
317 return OK;
318 }
319
320 const size_t bufferSize = audioBuffer.size;
321 MediaBuffer *buffer = new MediaBuffer(bufferSize);
322 memcpy((uint8_t *) buffer->data(),
323 audioBuffer.i16, audioBuffer.size);
James Dongd33a4cc2011-02-15 10:08:07 -0800324 buffer->set_range(0, bufferSize);
James Dong745fcc02012-02-14 14:58:20 -0800325 queueInputBuffer_l(buffer, timeUs);
326 return OK;
327}
328
329void AudioSource::queueInputBuffer_l(MediaBuffer *buffer, int64_t timeUs) {
330 const size_t bufferSize = buffer->range_length();
331 const size_t frameSize = mRecord->frameSize();
332 const int64_t timestampUs =
333 mPrevSampleTimeUs +
334 ((1000000LL * (bufferSize / frameSize)) +
335 (mSampleRate >> 1)) / mSampleRate;
James Dongd33a4cc2011-02-15 10:08:07 -0800336
337 if (mNumFramesReceived == 0) {
338 buffer->meta_data()->setInt64(kKeyAnchorTime, mStartTimeUs);
339 }
James Dong745fcc02012-02-14 14:58:20 -0800340
James Dongd33a4cc2011-02-15 10:08:07 -0800341 buffer->meta_data()->setInt64(kKeyTime, mPrevSampleTimeUs);
342 buffer->meta_data()->setInt64(kKeyDriftTime, timeUs - mInitialReadTimeUs);
343 mPrevSampleTimeUs = timestampUs;
James Dong745fcc02012-02-14 14:58:20 -0800344 mNumFramesReceived += bufferSize / frameSize;
James Dongd33a4cc2011-02-15 10:08:07 -0800345 mBuffersReceived.push_back(buffer);
346 mFrameAvailableCondition.signal();
Andreas Huber07bf09d2010-01-25 14:27:12 -0800347}
348
James Dong57e7f832010-06-24 19:55:31 -0700349void AudioSource::trackMaxAmplitude(int16_t *data, int nSamples) {
350 for (int i = nSamples; i > 0; --i) {
351 int16_t value = *data++;
352 if (value < 0) {
353 value = -value;
354 }
355 if (mMaxAmplitude < value) {
356 mMaxAmplitude = value;
357 }
358 }
359}
360
361int16_t AudioSource::getMaxAmplitude() {
362 // First call activates the tracking.
363 if (!mTrackMaxAmplitude) {
364 mTrackMaxAmplitude = true;
365 }
366 int16_t value = mMaxAmplitude;
367 mMaxAmplitude = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100368 ALOGV("max amplitude since last call: %d", value);
James Dong57e7f832010-06-24 19:55:31 -0700369 return value;
370}
371
Andreas Huber07bf09d2010-01-25 14:27:12 -0800372} // namespace android