1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
/*
**
** Copyright 2012, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#define LOG_TAG "AudioHAL:AudioStreamIn"
#include <utils/Log.h>
#include "AudioStreamIn.h"
#include "AudioHardwareInput.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utils/String8.h>
#include <media/AudioParameter.h>
// for turning Remote mic on/off
#ifdef REMOTE_CONTROL_INTERFACE
#include <IRemoteControlService.h>
#endif
namespace android {
const audio_format_t AudioStreamIn::kAudioFormat = AUDIO_FORMAT_PCM_16_BIT;
const uint32_t AudioStreamIn::kChannelMask = AUDIO_CHANNEL_IN_MONO;
// number of periods in the ALSA buffer
const int AudioStreamIn::kPeriodCount = 4;
AudioStreamIn::AudioStreamIn(AudioHardwareInput& owner)
: mOwnerHAL(owner)
, mCurrentDeviceInfo(NULL)
, mRequestedSampleRate(0)
, mStandby(true)
, mDisabled(false)
, mPcm(NULL)
, mResampler(NULL)
, mBuffer(NULL)
, mBufferSize(0)
, mInputSource(AUDIO_SOURCE_DEFAULT)
, mReadStatus(0)
, mFramesIn(0)
{
struct resampler_buffer_provider& provider =
mResamplerProviderWrapper.provider;
provider.get_next_buffer = getNextBufferThunk;
provider.release_buffer = releaseBufferThunk;
mResamplerProviderWrapper.thiz = this;
}
AudioStreamIn::~AudioStreamIn()
{
Mutex::Autolock _l(mLock);
standby_l();
}
// Perform stream initialization that may fail.
// Must only be called once at construction time.
status_t AudioStreamIn::set(audio_format_t *pFormat, uint32_t *pChannelMask,
uint32_t *pRate)
{
Mutex::Autolock _l(mLock);
assert(mRequestedSampleRate == 0);
// Respond with a request for mono if a different format is given.
if (*pChannelMask != kChannelMask) {
*pChannelMask = kChannelMask;
return BAD_VALUE;
}
if (*pFormat != kAudioFormat) {
*pFormat = kAudioFormat;
return BAD_VALUE;
}
mRequestedSampleRate = *pRate;
return NO_ERROR;
}
uint32_t AudioStreamIn::getSampleRate()
{
Mutex::Autolock _l(mLock);
return mRequestedSampleRate;
}
status_t AudioStreamIn::setSampleRate(uint32_t rate)
{
(void) rate;
// this is a no-op in other audio HALs
return NO_ERROR;
}
size_t AudioStreamIn::getBufferSize()
{
Mutex::Autolock _l(mLock);
size_t size = AudioHardwareInput::calculateInputBufferSize(
mRequestedSampleRate, kAudioFormat, getChannelCount());
return size;
}
uint32_t AudioStreamIn::getChannelMask()
{
return kChannelMask;
}
audio_format_t AudioStreamIn::getFormat()
{
return kAudioFormat;
}
status_t AudioStreamIn::setFormat(audio_format_t format)
{
(void) format;
// other audio HALs fail any call to this API (even if the format matches
// the current format)
return INVALID_OPERATION;
}
status_t AudioStreamIn::standby()
{
Mutex::Autolock _l(mLock);
return standby_l();
}
status_t AudioStreamIn::standby_l()
{
if (mStandby) {
return NO_ERROR;
}
if (mPcm) {
ALOGD("AudioStreamIn::standby_l, call pcm_close()");
pcm_close(mPcm);
mPcm = NULL;
}
// Turn OFF Remote MIC if we were recording from Remote.
if (mCurrentDeviceInfo != NULL) {
if (mCurrentDeviceInfo->forVoiceRecognition) {
setRemoteControlMicEnabled(false);
}
}
if (mResampler) {
release_resampler(mResampler);
mResampler = NULL;
}
if (mBuffer) {
delete [] mBuffer;
mBuffer = NULL;
}
mCurrentDeviceInfo = NULL;
mStandby = true;
mDisabled = false;
return NO_ERROR;
}
#define DUMP(a...) \
snprintf(buffer, SIZE, a); \
buffer[SIZE - 1] = 0; \
result.append(buffer);
status_t AudioStreamIn::dump(int fd)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
DUMP("\n AudioStreamIn::dump\n");
{
DUMP("\toutput sample rate: %d\n", mRequestedSampleRate);
if (mPcm) {
DUMP("\tinput sample rate: %d\n", mPcmConfig.rate);
DUMP("\tinput channels: %d\n", mPcmConfig.channels);
}
}
::write(fd, result.string(), result.size());
return NO_ERROR;
}
status_t AudioStreamIn::setParameters(struct audio_stream* stream,
const char* kvpairs)
{
(void) stream;
AudioParameter param = AudioParameter(String8(kvpairs));
status_t status = NO_ERROR;
String8 keySource = String8(AudioParameter::keyInputSource);
int intVal;
if (param.getInt(keySource, intVal) == NO_ERROR) {
ALOGI("AudioStreamIn::setParameters, mInputSource set to %d", intVal);
mInputSource = intVal;
}
return status;
}
char* AudioStreamIn::getParameters(const char* keys)
{
(void) keys;
return strdup("");
}
status_t AudioStreamIn::setGain(float gain)
{
(void) gain;
// In other HALs, this is a no-op and returns success.
return NO_ERROR;
}
uint32_t AudioStreamIn::getInputFramesLost()
{
return 0;
}
status_t AudioStreamIn::addAudioEffect(effect_handle_t effect)
{
(void) effect;
// In other HALs, this is a no-op and returns success.
return 0;
}
status_t AudioStreamIn::removeAudioEffect(effect_handle_t effect)
{
(void) effect;
// In other HALs, this is a no-op and returns success.
return 0;
}
ssize_t AudioStreamIn::read(void* buffer, size_t bytes)
{
Mutex::Autolock _l(mLock);
status_t status = NO_ERROR;
if (mStandby) {
status = startInputStream_l();
// Only try to start once to prevent pointless spew.
// If mic is not available then read will return silence.
// This is needed to prevent apps from hanging.
mStandby = false;
if (status != NO_ERROR) {
mDisabled = true;
}
}
if ((status == NO_ERROR) && !mDisabled) {
int ret = readFrames_l(buffer, bytes / getFrameSize());
status = (ret < 0) ? INVALID_OPERATION : NO_ERROR;
}
if ((status != NO_ERROR) || mDisabled) {
memset(buffer, 0, bytes);
// TODO: This code needs to project a timeline based on the number
// of audio frames synthesized from the last time we returned data
// from an actual audio device (or establish a fake timeline to obey
// if we have never returned any data from an actual device and need
// to synth on the first call to read)
usleep(bytes * 1000000 / getFrameSize() / mRequestedSampleRate);
} else {
bool mute;
mOwnerHAL.getMicMute(&mute);
if (mute) {
memset(buffer, 0, bytes);
}
}
return bytes;
}
void AudioStreamIn::setRemoteControlMicEnabled(bool flag)
{
#ifdef REMOTE_CONTROL_INTERFACE
sp<IRemoteControlService> service = IRemoteControlService::getInstance();
if (service == NULL) {
ALOGE("%s: No RemoteControl service detected, ignoring\n", __func__);
return;
}
service->setMicEnabled(flag);
#else
(void)flag;
#endif
}
status_t AudioStreamIn::startInputStream_l()
{
ALOGI("AudioStreamIn::startInputStream_l, entry");
// Get the most appropriate device for the given input source, eg VOICE_RECOGNITION
const AudioHotplugThread::DeviceInfo *deviceInfo = mOwnerHAL.getBestDevice(mInputSource);
if (deviceInfo == NULL) {
return INVALID_OPERATION;
}
memset(&mPcmConfig, 0, sizeof(mPcmConfig));
unsigned int requestedChannelCount = getChannelCount();
// Clip to min/max available.
if (requestedChannelCount < deviceInfo->minChannelCount ) {
mPcmConfig.channels = deviceInfo->minChannelCount;
} else if (requestedChannelCount > deviceInfo->maxChannelCount ) {
mPcmConfig.channels = deviceInfo->maxChannelCount;
} else {
mPcmConfig.channels = requestedChannelCount;
}
ALOGD("AudioStreamIn::startInputStream_l, mRequestedSampleRate = %d",
mRequestedSampleRate);
// Clip to min/max available from driver.
uint32_t chosenSampleRate = mRequestedSampleRate;
if (chosenSampleRate < deviceInfo->minSampleRate) {
chosenSampleRate = deviceInfo->minSampleRate;
} else if (chosenSampleRate > deviceInfo->maxSampleRate) {
chosenSampleRate = deviceInfo->maxSampleRate;
}
// Turn on RemoteControl MIC if we are recording from it.
if (deviceInfo->forVoiceRecognition) {
setRemoteControlMicEnabled(true);
}
mPcmConfig.rate = chosenSampleRate;
mPcmConfig.period_size =
AudioHardwareInput::kPeriodMsec * mPcmConfig.rate / 1000;
mPcmConfig.period_count = kPeriodCount;
mPcmConfig.format = PCM_FORMAT_S16_LE;
ALOGD("AudioStreamIn::startInputStream_l, call pcm_open()");
struct pcm* pcm = pcm_open(deviceInfo->pcmCard, deviceInfo->pcmDevice,
PCM_IN, &mPcmConfig);
if (!pcm_is_ready(pcm)) {
ALOGE("ERROR AudioStreamIn::startInputStream_l, pcm_open failed");
pcm_close(pcm);
if (deviceInfo->forVoiceRecognition) {
setRemoteControlMicEnabled(false);
}
return NO_MEMORY;
}
mCurrentDeviceInfo = deviceInfo;
mBufferSize = pcm_frames_to_bytes(pcm, mPcmConfig.period_size);
if (mBuffer) {
delete [] mBuffer;
}
mBuffer = new int16_t[mBufferSize / sizeof(uint16_t)];
if (mResampler) {
release_resampler(mResampler);
mResampler = NULL;
}
if (mPcmConfig.rate != mRequestedSampleRate) {
ALOGD("AudioStreamIn::startInputStream_l, call create_resampler( %d to %d)",
mPcmConfig.rate, mRequestedSampleRate);
int ret = create_resampler(mPcmConfig.rate,
mRequestedSampleRate,
1,
RESAMPLER_QUALITY_DEFAULT,
&mResamplerProviderWrapper.provider,
&mResampler);
if (ret != 0) {
ALOGW("AudioStreamIn: unable to create resampler");
pcm_close(pcm);
return static_cast<status_t>(ret);
}
}
mPcm = pcm;
return NO_ERROR;
}
// readFrames() reads frames from kernel driver, down samples to the capture
// rate if necessary and outputs the number of frames requested to the buffer
// specified
ssize_t AudioStreamIn::readFrames_l(void* buffer, ssize_t frames)
{
ssize_t framesWr = 0;
size_t frameSize = getFrameSize();
while (framesWr < frames) {
size_t framesRd = frames - framesWr;
if (mResampler) {
char* outFrame = static_cast<char*>(buffer) +
(framesWr * frameSize);
mResampler->resample_from_provider(
mResampler,
reinterpret_cast<int16_t*>(outFrame),
&framesRd);
} else {
struct resampler_buffer buf;
buf.raw = NULL;
buf.frame_count = framesRd;
getNextBuffer(&buf);
if (buf.raw != NULL) {
memcpy(static_cast<char*>(buffer) + (framesWr * frameSize),
buf.raw,
buf.frame_count * frameSize);
framesRd = buf.frame_count;
}
releaseBuffer(&buf);
}
// mReadStatus is updated by getNextBuffer(), which is called by the
// resampler
if (mReadStatus != 0)
return mReadStatus;
framesWr += framesRd;
}
return framesWr;
}
int AudioStreamIn::getNextBufferThunk(
struct resampler_buffer_provider* bufferProvider,
struct resampler_buffer* buffer)
{
ResamplerBufferProviderWrapper* wrapper =
reinterpret_cast<ResamplerBufferProviderWrapper*>(
reinterpret_cast<char*>(bufferProvider) -
offsetof(ResamplerBufferProviderWrapper, provider));
return wrapper->thiz->getNextBuffer(buffer);
}
void AudioStreamIn::releaseBufferThunk(
struct resampler_buffer_provider* bufferProvider,
struct resampler_buffer* buffer)
{
ResamplerBufferProviderWrapper* wrapper =
reinterpret_cast<ResamplerBufferProviderWrapper*>(
reinterpret_cast<char*>(bufferProvider) -
offsetof(ResamplerBufferProviderWrapper, provider));
wrapper->thiz->releaseBuffer(buffer);
}
// called while holding mLock
int AudioStreamIn::getNextBuffer(struct resampler_buffer* buffer)
{
if (buffer == NULL) {
return -EINVAL;
}
if (mPcm == NULL) {
buffer->raw = NULL;
buffer->frame_count = 0;
mReadStatus = -ENODEV;
return -ENODEV;
}
if (mFramesIn == 0) {
mReadStatus = pcm_read(mPcm, mBuffer, mBufferSize);
if (mReadStatus) {
ALOGE("get_next_buffer() pcm_read error %d", mReadStatus);
buffer->raw = NULL;
buffer->frame_count = 0;
return mReadStatus;
}
mFramesIn = mPcmConfig.period_size;
if (mPcmConfig.channels == 2) {
// Discard the right channel.
// TODO: this is what other HALs are doing to handle stereo input
// devices. Need to verify if this is appropriate for ATV Remote.
for (unsigned int i = 1; i < mFramesIn; i++) {
mBuffer[i] = mBuffer[i * 2];
}
}
}
buffer->frame_count = (buffer->frame_count > mFramesIn) ?
mFramesIn : buffer->frame_count;
buffer->i16 = mBuffer + (mPcmConfig.period_size - mFramesIn);
return mReadStatus;
}
// called while holding mLock
void AudioStreamIn::releaseBuffer(struct resampler_buffer* buffer)
{
if (buffer == NULL) {
return;
}
mFramesIn -= buffer->frame_count;
}
}; // namespace android
|