-
Notifications
You must be signed in to change notification settings - Fork 29
/
ALSAStreamOps.cpp
291 lines (245 loc) · 7.68 KB
/
ALSAStreamOps.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
/* ALSAStreamOps.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"
namespace android
{
// ----------------------------------------------------------------------------
ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) :
mParent(parent),
mHandle(handle),
mPowerLock(false)
{
}
ALSAStreamOps::~ALSAStreamOps()
{
AutoMutex lock(mLock);
close();
}
// use emulated popcount optimization
// http://www.df.lth.se/~john_e/gems/gem002d.html
static inline uint32_t popCount(uint32_t u)
{
u = ((u&0x55555555) + ((u>>1)&0x55555555));
u = ((u&0x33333333) + ((u>>2)&0x33333333));
u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
u = ( u&0x0000ffff) + (u>>16);
return u;
}
acoustic_device_t *ALSAStreamOps::acoustics()
{
return mParent->mAcousticDevice;
}
ALSAMixer *ALSAStreamOps::mixer()
{
return mParent->mMixer;
}
status_t ALSAStreamOps::set(int *format,
uint32_t *channels,
uint32_t *rate)
{
if (channels && *channels != 0) {
if (mHandle->channels != popCount(*channels))
return BAD_VALUE;
} else if (channels) {
*channels = 0;
if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL)
switch(mHandle->channels) {
case 4:
*channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
*channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
// Fall through...
default:
case 2:
*channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
// Fall through...
case 1:
*channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
break;
}
else
switch(mHandle->channels) {
default:
case 2:
*channels |= AudioSystem::CHANNEL_IN_RIGHT;
// Fall through...
case 1:
*channels |= AudioSystem::CHANNEL_IN_LEFT;
break;
}
}
if (rate && *rate > 0) {
if (mHandle->sampleRate != *rate)
return BAD_VALUE;
} else if (rate)
*rate = mHandle->sampleRate;
snd_pcm_format_t iformat = mHandle->format;
if (format) {
switch(*format) {
case AudioSystem::FORMAT_DEFAULT:
break;
case AudioSystem::PCM_16_BIT:
iformat = SND_PCM_FORMAT_S16_LE;
break;
case AudioSystem::PCM_8_BIT:
iformat = SND_PCM_FORMAT_S8;
break;
default:
LOGE("Unknown PCM format %i. Forcing default", *format);
break;
}
if (mHandle->format != iformat)
return BAD_VALUE;
switch(iformat) {
default:
case SND_PCM_FORMAT_S16_LE:
*format = AudioSystem::PCM_16_BIT;
break;
case SND_PCM_FORMAT_S8:
*format = AudioSystem::PCM_8_BIT;
break;
}
}
return NO_ERROR;
}
status_t ALSAStreamOps::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 key = String8(AudioParameter::keyRouting);
status_t status = NO_ERROR;
int device;
LOGV("setParameters() %s", keyValuePairs.string());
if (param.getInt(key, device) == NO_ERROR) {
mParent->mALSADevice->route(mHandle, (uint32_t)device, mParent->mode());
param.remove(key);
}
if (param.size()) {
status = BAD_VALUE;
}
return status;
}
String8 ALSAStreamOps::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
String8 value;
String8 key = String8(AudioParameter::keyRouting);
if (param.get(key, value) == NO_ERROR) {
param.addInt(key, (int)mHandle->curDev);
}
LOGV("getParameters() %s", param.toString().string());
return param.toString();
}
uint32_t ALSAStreamOps::sampleRate() const
{
return mHandle->sampleRate;
}
//
// Return the number of bytes (not frames)
//
size_t ALSAStreamOps::bufferSize() const
{
snd_pcm_uframes_t bufferSize = mHandle->bufferSize;
snd_pcm_uframes_t periodSize;
snd_pcm_get_params(mHandle->handle, &bufferSize, &periodSize);
size_t bytes = static_cast<size_t>(snd_pcm_frames_to_bytes(mHandle->handle, bufferSize));
// Not sure when this happened, but unfortunately it now
// appears that the bufferSize must be reported as a
// power of 2. This might be for OSS compatibility.
for (size_t i = 1; (bytes & ~i) != 0; i<<=1)
bytes &= ~i;
return bytes;
}
int ALSAStreamOps::format() const
{
int pcmFormatBitWidth;
int audioSystemFormat;
snd_pcm_format_t ALSAFormat = mHandle->format;
pcmFormatBitWidth = snd_pcm_format_physical_width(ALSAFormat);
switch(pcmFormatBitWidth) {
case 8:
audioSystemFormat = AudioSystem::PCM_8_BIT;
break;
default:
LOG_FATAL("Unknown AudioSystem bit width %i!", pcmFormatBitWidth);
case 16:
audioSystemFormat = AudioSystem::PCM_16_BIT;
break;
}
return audioSystemFormat;
}
uint32_t ALSAStreamOps::channels() const
{
unsigned int count = mHandle->channels;
uint32_t channels = 0;
if (mHandle->curDev & AudioSystem::DEVICE_OUT_ALL)
switch(count) {
case 4:
channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT;
channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT;
// Fall through...
default:
case 2:
channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT;
// Fall through...
case 1:
channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT;
break;
}
else
switch(count) {
default:
case 2:
channels |= AudioSystem::CHANNEL_IN_RIGHT;
// Fall through...
case 1:
channels |= AudioSystem::CHANNEL_IN_LEFT;
break;
}
return channels;
}
void ALSAStreamOps::close()
{
mParent->mALSADevice->close(mHandle);
}
//
// Set playback or capture PCM device. It's possible to support audio output
// or input from multiple devices by using the ALSA plugins, but this is
// not supported for simplicity.
//
// The AudioHardwareALSA API does not allow one to set the input routing.
//
// If the "routes" value does not map to a valid device, the default playback
// device is used.
//
status_t ALSAStreamOps::open(int mode)
{
return mParent->mALSADevice->open(mHandle, mHandle->curDev, mode);
}
} // namespace android