-
Notifications
You must be signed in to change notification settings - Fork 29
/
AudioHardwareALSA.cpp
267 lines (225 loc) · 7.02 KB
/
AudioHardwareALSA.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
/* AudioHardwareALSA.cpp
**
** Copyright 2008-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.
*/
#include <errno.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#define LOG_TAG "AudioHardwareALSA"
#include <utils/Log.h>
#include <utils/String8.h>
#include <cutils/properties.h>
#include <media/AudioRecord.h>
#include <hardware_legacy/power.h>
#include "AudioHardwareALSA.h"
extern "C"
{
//
// Function for dlsym() to look up for creating a new AudioHardwareInterface.
//
android::AudioHardwareInterface *createAudioHardware(void) {
return android::AudioHardwareALSA::create();
}
} // extern "C"
namespace android
{
// ----------------------------------------------------------------------------
static void ALSAErrorHandler(const char *file,
int line,
const char *function,
int err,
const char *fmt,
...)
{
char buf[BUFSIZ];
va_list arg;
int l;
va_start(arg, fmt);
l = snprintf(buf, BUFSIZ, "%s:%i:(%s) ", file, line, function);
vsnprintf(buf + l, BUFSIZ - l, fmt, arg);
buf[BUFSIZ-1] = '\0';
LOG(LOG_ERROR, "ALSALib", "%s", buf);
va_end(arg);
}
AudioHardwareInterface *AudioHardwareALSA::create() {
return new AudioHardwareALSA();
}
AudioHardwareALSA::AudioHardwareALSA() :
mALSADevice(0),
mAcousticDevice(0)
{
snd_lib_error_set_handler(&ALSAErrorHandler);
mMixer = new ALSAMixer;
hw_module_t *module;
int err = hw_get_module(ALSA_HARDWARE_MODULE_ID,
(hw_module_t const**)&module);
if (err == 0) {
hw_device_t* device;
err = module->methods->open(module, ALSA_HARDWARE_NAME, &device);
if (err == 0) {
mALSADevice = (alsa_device_t *)device;
mALSADevice->init(mALSADevice, mDeviceList);
} else
LOGE("ALSA Module could not be opened!!!");
} else
LOGE("ALSA Module not found!!!");
err = hw_get_module(ACOUSTICS_HARDWARE_MODULE_ID,
(hw_module_t const**)&module);
if (err == 0) {
hw_device_t* device;
err = module->methods->open(module, ACOUSTICS_HARDWARE_NAME, &device);
if (err == 0)
mAcousticDevice = (acoustic_device_t *)device;
else
LOGE("Acoustics Module not found.");
}
}
AudioHardwareALSA::~AudioHardwareALSA()
{
if (mMixer) delete mMixer;
if (mALSADevice)
mALSADevice->common.close(&mALSADevice->common);
if (mAcousticDevice)
mAcousticDevice->common.close(&mAcousticDevice->common);
}
status_t AudioHardwareALSA::initCheck()
{
if (mALSADevice && mMixer && mMixer->isValid())
return NO_ERROR;
else
return NO_INIT;
}
status_t AudioHardwareALSA::setVoiceVolume(float volume)
{
// The voice volume is used by the VOICE_CALL audio stream.
if (mMixer)
return mMixer->setVolume(AudioSystem::DEVICE_OUT_EARPIECE, volume, volume);
else
return INVALID_OPERATION;
}
status_t AudioHardwareALSA::setMasterVolume(float volume)
{
if (mMixer)
return mMixer->setMasterVolume(volume);
else
return INVALID_OPERATION;
}
status_t AudioHardwareALSA::setMode(int mode)
{
status_t status = NO_ERROR;
if (mode != mMode) {
status = AudioHardwareBase::setMode(mode);
if (status == NO_ERROR) {
// take care of mode change.
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it) {
status = mALSADevice->route(&(*it), it->curDev, mode);
if (status != NO_ERROR)
break;
}
}
}
return status;
}
AudioStreamOut *
AudioHardwareALSA::openOutputStream(uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status)
{
AutoMutex lock(mLock);
LOGD("openOutputStream called for devices: 0x%08x", devices);
status_t err = BAD_VALUE;
AudioStreamOutALSA *out = 0;
if (devices & (devices - 1)) {
if (status) *status = err;
LOGD("openOutputStream called with bad devices");
return out;
}
// Find the appropriate alsa device
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it)
if (it->devices & devices) {
err = mALSADevice->open(&(*it), devices, mode());
if (err) break;
out = new AudioStreamOutALSA(this, &(*it));
err = out->set(format, channels, sampleRate);
break;
}
if (status) *status = err;
return out;
}
void
AudioHardwareALSA::closeOutputStream(AudioStreamOut* out)
{
AutoMutex lock(mLock);
delete out;
}
AudioStreamIn *
AudioHardwareALSA::openInputStream(uint32_t devices,
int *format,
uint32_t *channels,
uint32_t *sampleRate,
status_t *status,
AudioSystem::audio_in_acoustics acoustics)
{
AutoMutex lock(mLock);
status_t err = BAD_VALUE;
AudioStreamInALSA *in = 0;
if (devices & (devices - 1)) {
if (status) *status = err;
return in;
}
// Find the appropriate alsa device
for(ALSAHandleList::iterator it = mDeviceList.begin();
it != mDeviceList.end(); ++it)
if (it->devices & devices) {
err = mALSADevice->open(&(*it), devices, mode());
if (err) break;
in = new AudioStreamInALSA(this, &(*it), acoustics);
err = in->set(format, channels, sampleRate);
break;
}
if (status) *status = err;
return in;
}
void
AudioHardwareALSA::closeInputStream(AudioStreamIn* in)
{
AutoMutex lock(mLock);
delete in;
}
status_t AudioHardwareALSA::setMicMute(bool state)
{
if (mMixer)
return mMixer->setCaptureMuteState(AudioSystem::DEVICE_OUT_EARPIECE, state);
return NO_INIT;
}
status_t AudioHardwareALSA::getMicMute(bool *state)
{
if (mMixer)
return mMixer->getCaptureMuteState(AudioSystem::DEVICE_OUT_EARPIECE, state);
return NO_ERROR;
}
status_t AudioHardwareALSA::dump(int fd, const Vector<String16>& args)
{
return NO_ERROR;
}
} // namespace android