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
|
/*
** Copyright 2010, 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.
*/
#include <math.h>
#define LOG_TAG "AudioHardware"
#include <utils/Log.h>
#include <utils/String8.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <fcntl.h>
#include "AudioHardware.h"
#include <media/AudioRecord.h>
extern "C" {
#include "alsa_audio.h"
}
namespace android {
// ----------------------------------------------------------------------------
AudioHardware::AudioHardware() :
mInit(false), mMicMute(true), mOutput(0)
{
mInit = true;
}
AudioHardware::~AudioHardware()
{
closeOutputStream((AudioStreamOut*)mOutput);
mInit = false;
}
status_t AudioHardware::initCheck()
{
return mInit ? NO_ERROR : NO_INIT;
}
AudioStreamOut* AudioHardware::openOutputStream(
uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status)
{
{ // scope for the lock
Mutex::Autolock lock(mLock);
// only one output stream allowed
if (mOutput) {
if (status) {
*status = INVALID_OPERATION;
}
return 0;
}
AudioStreamOutALSA* out = new AudioStreamOutALSA();
status_t rc = out->set(this, devices, format, channels, sampleRate);
if (rc) {
*status = rc;
}
if (rc == NO_ERROR) {
mOutput = out;
} else {
delete out;
}
}
return mOutput;
}
void AudioHardware::closeOutputStream(AudioStreamOut* out) {
Mutex::Autolock lock(mLock);
if (mOutput == 0 || mOutput != out) {
LOGW("Attempt to close invalid output stream");
} else {
delete mOutput;
mOutput = 0;
}
}
AudioStreamIn* AudioHardware::openInputStream(
uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status,
AudioSystem::audio_in_acoustics acoustic_flags)
{
return 0;
}
void AudioHardware::closeInputStream(AudioStreamIn* in) {
}
status_t AudioHardware::setMode(int mode)
{
return NO_ERROR;
}
status_t AudioHardware::setMicMute(bool state)
{
return NO_ERROR;
}
status_t AudioHardware::getMicMute(bool* state)
{
*state = mMicMute;
return NO_ERROR;
}
status_t AudioHardware::setParameters(const String8& keyValuePairs)
{
return NO_ERROR;
}
String8 AudioHardware::getParameters(const String8& keys)
{
AudioParameter request = AudioParameter(keys);
AudioParameter reply = AudioParameter();
LOGV("getParameters() %s", keys.string());
return reply.toString();
}
size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
return 4096;
}
status_t AudioHardware::setVoiceVolume(float v)
{
return NO_ERROR;
}
status_t AudioHardware::setMasterVolume(float v)
{
LOGI("Set master volume to %f.\n", v);
// We return an error code here to let the audioflinger do in-software
// volume on top of the maximum volume that we set through the SND API.
// return error - software mixer will handle it
return -1;
}
status_t AudioHardware::dump(int fd, const Vector<String16>& args)
{
return NO_ERROR;
}
AudioHardware::AudioStreamOutALSA::AudioStreamOutALSA() :
mHardware(0), mPcm(0), mMixer(0), mRouteCtl(0), mStartCount(0), mRetryCount(0),
mStandby(true), mDevices(0), mChannels(AUDIO_HW_OUT_CHANNELS),
mSampleRate(AUDIO_HW_OUT_SAMPLERATE), mBufferSize(AUDIO_HW_OUT_BUFSZ)
{
}
status_t AudioHardware::AudioStreamOutALSA::set(
AudioHardware* hw, uint32_t devices, int *pFormat,
uint32_t *pChannels, uint32_t *pRate)
{
int lFormat = pFormat ? *pFormat : 0;
uint32_t lChannels = pChannels ? *pChannels : 0;
uint32_t lRate = pRate ? *pRate : 0;
mHardware = hw;
mDevices = devices;
// fix up defaults
if (lFormat == 0) lFormat = format();
if (lChannels == 0) lChannels = channels();
if (lRate == 0) lRate = sampleRate();
// check values
if ((lFormat != format()) ||
(lChannels != channels()) ||
(lRate != sampleRate())) {
if (pFormat) *pFormat = format();
if (pChannels) *pChannels = channels();
if (pRate) *pRate = sampleRate();
return BAD_VALUE;
}
if (pFormat) *pFormat = lFormat;
if (pChannels) *pChannels = lChannels;
if (pRate) *pRate = lRate;
mChannels = lChannels;
mSampleRate = lRate;
mBufferSize = 4096;
return NO_ERROR;
}
AudioHardware::AudioStreamOutALSA::~AudioStreamOutALSA()
{
standby();
}
ssize_t AudioHardware::AudioStreamOutALSA::write(const void* buffer, size_t bytes)
{
// LOGD("AudioStreamOutALSA::write(%p, %u)", buffer, bytes);
status_t status = NO_INIT;
const uint8_t* p = static_cast<const uint8_t*>(buffer);
int ret;
if (mStandby) {
LOGV("open pcm_out driver");
next_route = "SPK";
mPcm = pcm_open(PCM_OUT);
if (!pcm_ready(mPcm)) {
LOGE("cannot open pcm_out driver: %s\n", pcm_error(mPcm));
pcm_close(mPcm);
mPcm = 0;
goto Error;
}
mMixer = mixer_open();
if (mMixer)
mRouteCtl = mixer_get_control(mMixer, "Playback Path", 0);
#if 0
LOGV("set pcm_out config");
config.channel_count = AudioSystem::popCount(channels());
config.sample_rate = mSampleRate;
config.buffer_size = mBufferSize;
config.buffer_count = AUDIO_HW_NUM_OUT_BUF;
// config.codec_type = CODEC_TYPE_PCM;
status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
if (status < 0) {
LOGE("Cannot set config");
goto Error;
}
LOGV("buffer_size: %u", config.buffer_size);
LOGV("buffer_count: %u", config.buffer_count);
LOGV("channel_count: %u", config.channel_count);
LOGV("sample_rate: %u", config.sample_rate);
#endif
mStandby = false;
}
ret = pcm_write(mPcm,(void*) p, bytes);
if (ret == 0) {
if (next_route && mRouteCtl) {
mixer_ctl_select(mRouteCtl, next_route);
next_route = 0;
}
return bytes;
}
LOGW("write error: %d", errno);
status = -errno;
Error:
standby();
// Simulate audio output timing in case of error
usleep(bytes * 1000000 / frameSize() / sampleRate());
return status;
}
status_t AudioHardware::AudioStreamOutALSA::standby()
{
if (mPcm) {
pcm_close(mPcm);
mPcm = 0;
}
if (mMixer) {
mixer_close(mMixer);
mMixer = 0;
if (mRouteCtl) {
mRouteCtl = 0;
}
}
mStandby = true;
LOGI("AudioHardware pcm playback is going to standby.");
return NO_ERROR;
}
status_t AudioHardware::AudioStreamOutALSA::dump(int fd, const Vector<String16>& args)
{
return NO_ERROR;
}
bool AudioHardware::AudioStreamOutALSA::checkStandby()
{
return mStandby;
}
status_t AudioHardware::AudioStreamOutALSA::setParameters(const String8& keyValuePairs)
{
return NO_ERROR;
}
String8 AudioHardware::AudioStreamOutALSA::getParameters(const String8& keys)
{
AudioParameter param = AudioParameter(keys);
LOGV("AudioStreamOutALSA::getParameters() %s", param.toString().string());
return param.toString();
}
status_t AudioHardware::AudioStreamOutALSA::getRenderPosition(uint32_t *dspFrames)
{
return INVALID_OPERATION;
}
extern "C" AudioHardwareInterface* createAudioHardware(void) {
return new AudioHardware();
}
}; // namespace android
|