-
Notifications
You must be signed in to change notification settings - Fork 29
/
alsa_default.cpp
535 lines (441 loc) · 16.5 KB
/
alsa_default.cpp
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* alsa_default.cpp
**
** Copyright 2009 Wind River Systems
**
** 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 "ALSAModule"
#include <utils/Log.h>
#include "AudioHardwareALSA.h"
#include <media/AudioRecord.h>
#undef DISABLE_HARWARE_RESAMPLING
#define ALSA_NAME_MAX 128
#define ALSA_STRCAT(x,y) \
if (strlen(x) + strlen(y) < ALSA_NAME_MAX) \
strcat(x, y);
#ifndef ALSA_DEFAULT_SAMPLE_RATE
#define ALSA_DEFAULT_SAMPLE_RATE 44100 // in Hz
#endif
namespace android
{
static int s_device_open(const hw_module_t*, const char*, hw_device_t**);
static int s_device_close(hw_device_t*);
static status_t s_init(alsa_device_t *, ALSAHandleList &);
static status_t s_open(alsa_handle_t *, uint32_t, int);
static status_t s_close(alsa_handle_t *);
static status_t s_route(alsa_handle_t *, uint32_t, int);
static hw_module_methods_t s_module_methods = {
open : s_device_open
};
extern "C" const hw_module_t HAL_MODULE_INFO_SYM = {
tag : HARDWARE_MODULE_TAG,
version_major : 1,
version_minor : 0,
id : ALSA_HARDWARE_MODULE_ID,
name : "ALSA module",
author : "Wind River",
methods : &s_module_methods,
dso : 0,
reserved : { 0, },
};
static int s_device_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
alsa_device_t *dev;
dev = (alsa_device_t *) malloc(sizeof(*dev));
if (!dev) return -ENOMEM;
memset(dev, 0, sizeof(*dev));
/* initialize the procs */
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t *) module;
dev->common.close = s_device_close;
dev->init = s_init;
dev->open = s_open;
dev->close = s_close;
dev->route = s_route;
*device = &dev->common;
return 0;
}
static int s_device_close(hw_device_t* device)
{
free(device);
return 0;
}
// ----------------------------------------------------------------------------
static const int DEFAULT_SAMPLE_RATE = ALSA_DEFAULT_SAMPLE_RATE;
static const char *devicePrefix[SND_PCM_STREAM_LAST + 1] = {
/* SND_PCM_STREAM_PLAYBACK : */"AndroidPlayback",
/* SND_PCM_STREAM_CAPTURE : */"AndroidCapture",
};
static alsa_handle_t _defaultsOut = {
module : 0,
devices : AudioSystem::DEVICE_OUT_ALL,
curDev : 0,
curMode : 0,
handle : 0,
format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT
channels : 2,
sampleRate : DEFAULT_SAMPLE_RATE,
latency : 200000, // Desired Delay in usec
bufferSize : DEFAULT_SAMPLE_RATE / 5, // Desired Number of samples
modPrivate : 0,
};
static alsa_handle_t _defaultsIn = {
module : 0,
devices : AudioSystem::DEVICE_IN_ALL,
curDev : 0,
curMode : 0,
handle : 0,
format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT
channels : 1,
sampleRate : AudioRecord::DEFAULT_SAMPLE_RATE,
latency : 250000, // Desired Delay in usec
bufferSize : 2048, // Desired Number of samples
modPrivate : 0,
};
struct device_suffix_t {
const AudioSystem::audio_devices device;
const char *suffix;
};
/* The following table(s) need to match in order of the route bits
*/
static const device_suffix_t deviceSuffix[] = {
{AudioSystem::DEVICE_OUT_EARPIECE, "_Earpiece"},
{AudioSystem::DEVICE_OUT_SPEAKER, "_Speaker"},
{AudioSystem::DEVICE_OUT_BLUETOOTH_SCO, "_Bluetooth"},
{AudioSystem::DEVICE_OUT_WIRED_HEADSET, "_Headset"},
{AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP, "_Bluetooth-A2DP"},
};
static const int deviceSuffixLen = (sizeof(deviceSuffix)
/ sizeof(device_suffix_t));
// ----------------------------------------------------------------------------
snd_pcm_stream_t direction(alsa_handle_t *handle)
{
return (handle->devices & AudioSystem::DEVICE_OUT_ALL) ? SND_PCM_STREAM_PLAYBACK
: SND_PCM_STREAM_CAPTURE;
}
const char *deviceName(alsa_handle_t *handle, uint32_t device, int mode)
{
static char devString[ALSA_NAME_MAX];
int hasDevExt = 0;
strcpy(devString, devicePrefix[direction(handle)]);
for (int dev = 0; device && dev < deviceSuffixLen; dev++)
if (device & deviceSuffix[dev].device) {
ALSA_STRCAT (devString, deviceSuffix[dev].suffix);
device &= ~deviceSuffix[dev].device;
hasDevExt = 1;
}
if (hasDevExt) switch (mode) {
case AudioSystem::MODE_NORMAL:
ALSA_STRCAT (devString, "_normal")
;
break;
case AudioSystem::MODE_RINGTONE:
ALSA_STRCAT (devString, "_ringtone")
;
break;
case AudioSystem::MODE_IN_CALL:
ALSA_STRCAT (devString, "_incall")
;
break;
};
return devString;
}
const char *streamName(alsa_handle_t *handle)
{
return snd_pcm_stream_name(direction(handle));
}
status_t setHardwareParams(alsa_handle_t *handle)
{
snd_pcm_hw_params_t *hardwareParams;
status_t err;
snd_pcm_uframes_t bufferSize = handle->bufferSize;
unsigned int requestedRate = handle->sampleRate;
unsigned int latency = handle->latency;
// snd_pcm_format_description() and snd_pcm_format_name() do not perform
// proper bounds checking.
bool validFormat = (static_cast<int> (handle->format)
> SND_PCM_FORMAT_UNKNOWN) && (static_cast<int> (handle->format)
<= SND_PCM_FORMAT_LAST);
const char *formatDesc = validFormat ? snd_pcm_format_description(
handle->format) : "Invalid Format";
const char *formatName = validFormat ? snd_pcm_format_name(handle->format)
: "UNKNOWN";
if (snd_pcm_hw_params_malloc(&hardwareParams) < 0) {
LOG_ALWAYS_FATAL("Failed to allocate ALSA hardware parameters!");
return NO_INIT;
}
err = snd_pcm_hw_params_any(handle->handle, hardwareParams);
if (err < 0) {
LOGE("Unable to configure hardware: %s", snd_strerror(err));
goto done;
}
// Set the interleaved read and write format.
err = snd_pcm_hw_params_set_access(handle->handle, hardwareParams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
LOGE("Unable to configure PCM read/write format: %s",
snd_strerror(err));
goto done;
}
err = snd_pcm_hw_params_set_format(handle->handle, hardwareParams,
handle->format);
if (err < 0) {
LOGE("Unable to configure PCM format %s (%s): %s",
formatName, formatDesc, snd_strerror(err));
goto done;
}
LOGV("Set %s PCM format to %s (%s)", streamName(), formatName, formatDesc);
err = snd_pcm_hw_params_set_channels(handle->handle, hardwareParams,
handle->channels);
if (err < 0) {
LOGE("Unable to set channel count to %i: %s",
handle->channels, snd_strerror(err));
goto done;
}
LOGV("Using %i %s for %s.", handle->channels,
handle->channels == 1 ? "channel" : "channels", streamName());
err = snd_pcm_hw_params_set_rate_near(handle->handle, hardwareParams,
&requestedRate, 0);
if (err < 0)
LOGE("Unable to set %s sample rate to %u: %s",
streamName(handle), handle->sampleRate, snd_strerror(err));
else if (requestedRate != handle->sampleRate)
// Some devices have a fixed sample rate, and can not be changed.
// This may cause resampling problems; i.e. PCM playback will be too
// slow or fast.
LOGW("Requested rate (%u HZ) does not match actual rate (%u HZ)",
handle->sampleRate, requestedRate);
else
LOGV("Set %s sample rate to %u HZ", stream, requestedRate);
#ifdef DISABLE_HARWARE_RESAMPLING
// Disable hardware re-sampling.
err = snd_pcm_hw_params_set_rate_resample(handle->handle,
hardwareParams,
static_cast<int>(resample));
if (err < 0) {
LOGE("Unable to %s hardware resampling: %s",
resample ? "enable" : "disable",
snd_strerror(err));
goto done;
}
#endif
// Make sure we have at least the size we originally wanted
err = snd_pcm_hw_params_set_buffer_size_near(handle->handle, hardwareParams,
&bufferSize);
if (err < 0) {
LOGE("Unable to set buffer size to %d: %s",
(int)bufferSize, snd_strerror(err));
goto done;
}
// Setup buffers for latency
err = snd_pcm_hw_params_set_buffer_time_near(handle->handle,
hardwareParams, &latency, NULL);
if (err < 0) {
/* That didn't work, set the period instead */
unsigned int periodTime = latency / 4;
err = snd_pcm_hw_params_set_period_time_near(handle->handle,
hardwareParams, &periodTime, NULL);
if (err < 0) {
LOGE("Unable to set the period time for latency: %s", snd_strerror(err));
goto done;
}
snd_pcm_uframes_t periodSize;
err = snd_pcm_hw_params_get_period_size(hardwareParams, &periodSize,
NULL);
if (err < 0) {
LOGE("Unable to get the period size for latency: %s", snd_strerror(err));
goto done;
}
bufferSize = periodSize * 4;
if (bufferSize < handle->bufferSize) bufferSize = handle->bufferSize;
err = snd_pcm_hw_params_set_buffer_size_near(handle->handle,
hardwareParams, &bufferSize);
if (err < 0) {
LOGE("Unable to set the buffer size for latency: %s", snd_strerror(err));
goto done;
}
} else {
// OK, we got buffer time near what we expect. See what that did for bufferSize.
err = snd_pcm_hw_params_get_buffer_size(hardwareParams, &bufferSize);
if (err < 0) {
LOGE("Unable to get the buffer size for latency: %s", snd_strerror(err));
goto done;
}
// Does set_buffer_time_near change the passed value? It should.
err = snd_pcm_hw_params_get_buffer_time(hardwareParams, &latency, NULL);
if (err < 0) {
LOGE("Unable to get the buffer time for latency: %s", snd_strerror(err));
goto done;
}
unsigned int periodTime = latency / 4;
err = snd_pcm_hw_params_set_period_time_near(handle->handle,
hardwareParams, &periodTime, NULL);
if (err < 0) {
LOGE("Unable to set the period time for latency: %s", snd_strerror(err));
goto done;
}
}
LOGV("Buffer size: %d", (int)bufferSize);
LOGV("Latency: %d", (int)latency);
handle->bufferSize = bufferSize;
handle->latency = latency;
// Commit the hardware parameters back to the device.
err = snd_pcm_hw_params(handle->handle, hardwareParams);
if (err < 0) LOGE("Unable to set hardware parameters: %s", snd_strerror(err));
done:
snd_pcm_hw_params_free(hardwareParams);
return err;
}
status_t setSoftwareParams(alsa_handle_t *handle)
{
snd_pcm_sw_params_t * softwareParams;
int err;
snd_pcm_uframes_t bufferSize = 0;
snd_pcm_uframes_t periodSize = 0;
snd_pcm_uframes_t startThreshold, stopThreshold;
if (snd_pcm_sw_params_malloc(&softwareParams) < 0) {
LOG_ALWAYS_FATAL("Failed to allocate ALSA software parameters!");
return NO_INIT;
}
// Get the current software parameters
err = snd_pcm_sw_params_current(handle->handle, softwareParams);
if (err < 0) {
LOGE("Unable to get software parameters: %s", snd_strerror(err));
goto done;
}
// Configure ALSA to start the transfer when the buffer is almost full.
snd_pcm_get_params(handle->handle, &bufferSize, &periodSize);
if (handle->devices & AudioSystem::DEVICE_OUT_ALL) {
// For playback, configure ALSA to start the transfer when the
// buffer is full.
startThreshold = bufferSize - 1;
stopThreshold = bufferSize;
} else {
// For recording, configure ALSA to start the transfer on the
// first frame.
startThreshold = 1;
stopThreshold = bufferSize;
}
err = snd_pcm_sw_params_set_start_threshold(handle->handle, softwareParams,
startThreshold);
if (err < 0) {
LOGE("Unable to set start threshold to %lu frames: %s",
startThreshold, snd_strerror(err));
goto done;
}
err = snd_pcm_sw_params_set_stop_threshold(handle->handle, softwareParams,
stopThreshold);
if (err < 0) {
LOGE("Unable to set stop threshold to %lu frames: %s",
stopThreshold, snd_strerror(err));
goto done;
}
// Allow the transfer to start when at least periodSize samples can be
// processed.
err = snd_pcm_sw_params_set_avail_min(handle->handle, softwareParams,
periodSize);
if (err < 0) {
LOGE("Unable to configure available minimum to %lu: %s",
periodSize, snd_strerror(err));
goto done;
}
// Commit the software parameters back to the device.
err = snd_pcm_sw_params(handle->handle, softwareParams);
if (err < 0) LOGE("Unable to configure software parameters: %s",
snd_strerror(err));
done:
snd_pcm_sw_params_free(softwareParams);
return err;
}
// ----------------------------------------------------------------------------
static status_t s_init(alsa_device_t *module, ALSAHandleList &list)
{
list.clear();
snd_pcm_uframes_t bufferSize = _defaultsOut.bufferSize;
for (size_t i = 1; (bufferSize & ~i) != 0; i <<= 1)
bufferSize &= ~i;
_defaultsOut.module = module;
_defaultsOut.bufferSize = bufferSize;
list.push_back(_defaultsOut);
bufferSize = _defaultsIn.bufferSize;
for (size_t i = 1; (bufferSize & ~i) != 0; i <<= 1)
bufferSize &= ~i;
_defaultsIn.module = module;
_defaultsIn.bufferSize = bufferSize;
list.push_back(_defaultsIn);
return NO_ERROR;
}
static status_t s_open(alsa_handle_t *handle, uint32_t devices, int mode)
{
// Close off previously opened device.
// It would be nice to determine if the underlying device actually
// changes, but we might be recovering from an error or manipulating
// mixer settings (see asound.conf).
//
s_close(handle);
LOGD("open called for devices %08x in mode %d...", devices, mode);
const char *stream = streamName(handle);
const char *devName = deviceName(handle, devices, mode);
int err;
for (;;) {
// The PCM stream is opened in blocking mode, per ALSA defaults. The
// AudioFlinger seems to assume blocking mode too, so asynchronous mode
// should not be used.
err = snd_pcm_open(&handle->handle, devName, direction(handle),
SND_PCM_ASYNC);
if (err == 0) break;
// See if there is a less specific name we can try.
// Note: We are changing the contents of a const char * here.
char *tail = strrchr(devName, '_');
if (!tail) break;
*tail = 0;
}
if (err < 0) {
// None of the Android defined audio devices exist. Open a generic one.
devName = "default";
err = snd_pcm_open(&handle->handle, devName, direction(handle), 0);
}
if (err < 0) {
LOGE("Failed to Initialize any ALSA %s device: %s",
stream, strerror(err));
return NO_INIT;
}
err = setHardwareParams(handle);
if (err == NO_ERROR) err = setSoftwareParams(handle);
LOGI("Initialized ALSA %s device %s", stream, devName);
handle->curDev = devices;
handle->curMode = mode;
return err;
}
static status_t s_close(alsa_handle_t *handle)
{
status_t err = NO_ERROR;
snd_pcm_t *h = handle->handle;
handle->handle = 0;
handle->curDev = 0;
handle->curMode = 0;
if (h) {
snd_pcm_drain(h);
err = snd_pcm_close(h);
}
return err;
}
static status_t s_route(alsa_handle_t *handle, uint32_t devices, int mode)
{
LOGD("route called for devices %08x in mode %d...", devices, mode);
if (handle->handle && handle->curDev == devices && handle->curMode == mode) return NO_ERROR;
return s_open(handle, devices, mode);
}
}