summaryrefslogtreecommitdiffstats
path: root/audio_hw.c
blob: e2cb1c30fbd97f2fcf862252c61476f6996b82d4 (plain)
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
/*
 * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
 * 
 * This is based on Galaxy Nexus audio.primary.tuna implementation:
 * Copyright 2011, The Android Open-Source Project
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define LOG_TAG "TinyALSA-Audio Hardware"

#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>

#include <cutils/log.h>

#include "audio_hw.h"
#include "mixer.h"

/*
 * Functions
 */

static uint32_t audio_hw_get_supported_devices(const struct audio_hw_device *dev)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p)", __func__, dev);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(device->mixer == NULL)
		return -1;

	return (uint32_t) tinyalsa_mixer_get_supported_devices(device->mixer);
}

static int audio_hw_init_check(const struct audio_hw_device *dev)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p)", __func__, dev);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(device->mixer == NULL)
		return -1;

	return 0;
}

static int audio_hw_set_voice_volume(struct audio_hw_device *dev, float volume)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p, %f)", __func__, dev, volume);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(device->mixer == NULL)
		return -1;

	if(device->mode == AUDIO_MODE_IN_CALL) {
		// FIXME: Select the device from ril interface
		tinyalsa_mixer_set_voice_volume(device->mixer,
			AUDIO_DEVICE_IN_DEFAULT, device->mode, volume);
	}

	return 0;
}

static int audio_hw_set_master_volume(struct audio_hw_device *dev, float volume)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p, %f)", __func__, dev, volume);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(device->mixer == NULL)
		return -1;

	tinyalsa_mixer_set_master_volume(device->mixer, volume);

	return 0;
}

static int audio_hw_set_mode(struct audio_hw_device *dev, int mode)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p, %d)", __func__, dev, mode);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	device->mode = mode;

	// TODO: If mode changed, change routing

	return 0;
}

static int audio_hw_set_mic_mute(struct audio_hw_device *dev, bool state)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p, %d)", __func__, dev, state);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(device->mixer == NULL)
		return -1;

	if(device->mic_mute != state) {
		device->mic_mute = state;

		// FIXME: Select the device from input if mode isn't in-call,
		// select it from ril interface if mode is in-call
		tinyalsa_mixer_set_mic_mute(device->mixer,
			AUDIO_DEVICE_IN_DEFAULT, device->mode, state);
	}

	return 0;
}

static int audio_hw_get_mic_mute(const struct audio_hw_device *dev, bool *state)
{
	struct tinyalsa_audio_device *device;

	LOGD("%s(%p, %p)", __func__, dev, state);

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	*state = device->mic_mute;

	return 0;
}

static int audio_hw_set_parameters(struct audio_hw_device *dev,
	const char *kvpairs)
{
	LOGD("%s(%p, %s)", __func__, dev, kvpairs);

	return -ENOSYS;
}

static char *audio_hw_get_parameters(const struct audio_hw_device *dev,
	const char *keys)
{
	LOGD("%s(%p, %s)", __func__, dev, keys);

	return NULL;
}

static size_t audio_hw_get_input_buffer_size(const struct audio_hw_device *dev,
	uint32_t sample_rate, int format, int channel_count)
{
	LOGD("%s(%p, %d, %d, %d)", __func__, dev, sample_rate, format, channel_count);

	return 320;
}

static int audio_hw_dump(const audio_hw_device_t *device, int fd)
{
	LOGD("%s(%p, %d)", __func__, device, fd);

	return 0;
}

/*
 * Interface
 */

int audio_hw_close(hw_device_t *device)
{
	struct tinyalsa_audio_device *tinyalsa_audio_device;

	LOGD("%s(%p)", __func__, device);

	if(device != NULL) {
		tinyalsa_audio_device = (struct tinyalsa_audio_device *) device;

		if(tinyalsa_audio_device != NULL && tinyalsa_audio_device->mixer != NULL) {
			tinyalsa_mixer_close(tinyalsa_audio_device->mixer);
			tinyalsa_audio_device->mixer = NULL;
		}

		free(device);
	}

	return 0;
}

int audio_hw_open(const hw_module_t *module, const char *name,
	hw_device_t **device)
{
	struct tinyalsa_audio_device *tinyalsa_audio_device = NULL;
	struct tinyalsa_mixer *tinyalsa_mixer = NULL;
	struct audio_hw_device *dev;
	int rc;

	LOGD("%s(%p, %s, %p)", __func__, module, name, device);

	if(strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
		return -EINVAL;

	tinyalsa_audio_device = calloc(1, sizeof(struct tinyalsa_audio_device));
	if(tinyalsa_audio_device == NULL)
		return -ENOMEM;

	dev = &(tinyalsa_audio_device->device);

	dev->common.tag = HARDWARE_DEVICE_TAG;
	dev->common.version = 0;
	dev->common.module = (struct hw_module_t *) module;
	dev->common.close = audio_hw_close;

	dev->get_supported_devices = audio_hw_get_supported_devices;
	dev->init_check = audio_hw_init_check;
	dev->set_voice_volume = audio_hw_set_voice_volume;
	dev->set_master_volume = audio_hw_set_master_volume;
	dev->set_mode = audio_hw_set_mode;
	dev->set_mic_mute = audio_hw_set_mic_mute;
	dev->get_mic_mute = audio_hw_get_mic_mute;
	dev->set_parameters = audio_hw_set_parameters;
	dev->get_parameters = audio_hw_get_parameters;
	dev->get_input_buffer_size = audio_hw_get_input_buffer_size;

	dev->open_output_stream = audio_hw_open_output_stream;
	dev->close_output_stream = audio_hw_close_output_stream;

	dev->open_input_stream = audio_hw_open_input_stream;
	dev->close_input_stream = audio_hw_close_input_stream;

	dev->dump = audio_hw_dump;

	rc = tinyalsa_mixer_open(&tinyalsa_mixer, TINYALSA_MIXER_CONFIG_FILE);
	if(rc < 0 || tinyalsa_mixer == NULL) {
		LOGE("Failed to open mixer!");
		goto error_device;
	}

	rc = tinyalsa_mixer_set_route(tinyalsa_mixer, AUDIO_DEVICE_OUT_DEFAULT, AUDIO_MODE_NORMAL);
	if(rc < 0) {
		LOGE("Failed to set default mixer route");
	}

	tinyalsa_audio_device->mixer = tinyalsa_mixer;

	*device = &(dev->common);

	return 0;

error_device:
	*device = NULL;
	free(tinyalsa_audio_device);

	return -1;
}

struct hw_module_methods_t audio_hw_module_methods = {
	.open = audio_hw_open,
};

struct audio_module HAL_MODULE_INFO_SYM = {
	.common = {
		.tag = HARDWARE_MODULE_TAG,
		.version_major = 1,
		.version_minor = 0,
		.id = AUDIO_HARDWARE_MODULE_ID,
		.name = "TinyALSA-Audio",
		.author = "Paul Kocialkowski",
		.methods = &audio_hw_module_methods,
	},
};