summaryrefslogtreecommitdiffstats
path: root/audio_hw.c
blob: ce1509f7b7e170fd598da151f80df4d46f4e4be6 (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
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
/*
 * 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 <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>

#include <cutils/str_parms.h>
#include <cutils/log.h>

#ifdef YAMAHA_MC1N2_AUDIO
#include <yamaha-mc1n2-audio.h>
#endif

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

/*
 * Functions
 */

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

	ALOGD("%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;
	audio_devices_t device_modem;

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

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

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

	if(volume != device->voice_volume) {
		pthread_mutex_lock(&device->lock);

		if(device->mode == AUDIO_MODE_IN_CALL) {
			if(device->ril_interface != NULL)
				device_modem = device->ril_interface->device_current;
			else if(device->stream_out != NULL)
				device_modem = device->stream_out->device_current;
			else
				device_modem = AUDIO_DEVICE_OUT_EARPIECE;

			tinyalsa_mixer_set_voice_volume(device->mixer,
				device_modem, volume);

			if(device->ril_interface != NULL)
				audio_ril_interface_set_voice_volume(device->ril_interface, device_modem, volume);
		}

		device->voice_volume = volume;

		pthread_mutex_unlock(&device->lock);
	}

	return 0;
}

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

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

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

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

	pthread_mutex_lock(&device->lock);
	tinyalsa_mixer_set_master_volume(device->mixer, volume);
	pthread_mutex_unlock(&device->lock);

	return 0;
}

static int audio_hw_set_mode(struct audio_hw_device *dev, int mode)
{
	struct tinyalsa_audio_ril_interface *ril_interface;
	struct tinyalsa_audio_device *device;
	audio_devices_t device_modem;
	int rc;

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

	if(dev == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

	if(mode != device->mode) {
		pthread_mutex_lock(&device->lock);

		if(mode == AUDIO_MODE_IN_CALL) {
			tinyalsa_mixer_set_modem_state(device->mixer, 1);

			if(device->stream_out != NULL)
				device_modem = device->stream_out->device_current;
			else
				device_modem = AUDIO_DEVICE_OUT_EARPIECE;

			tinyalsa_mixer_set_device(device->mixer, device_modem);

#ifdef YAMAHA_MC1N2_AUDIO
			rc = yamaha_mc1n2_audio_modem_start(device->mc1n2_pdata);
			if(rc < 0) {
				ALOGE("Failed to set Yamaha-MC1N2-Audio route");
			}
#endif

			rc = audio_ril_interface_open((struct audio_hw_device *) device, device_modem, &ril_interface);
			if(rc < 0 || ril_interface == NULL) {
				ALOGE("Failed to open RIL interface");
				device->ril_interface = NULL;
			} else {
				device->ril_interface = ril_interface;

				if(device->voice_volume)
					audio_ril_interface_set_voice_volume(ril_interface, device_modem, device->voice_volume);
			}
		} else if(device->mode == AUDIO_MODE_IN_CALL) {
			tinyalsa_mixer_set_modem_state(device->mixer, 0);

#ifdef YAMAHA_MC1N2_AUDIO
			rc = yamaha_mc1n2_audio_modem_stop(device->mc1n2_pdata);
			if(rc < 0) {
				ALOGE("Failed to set Yamaha-MC1N2-Audio route");
			}
#endif

			if(device->ril_interface != NULL)
				audio_ril_interface_close((struct audio_hw_device *) device, device->ril_interface);
		}

		device->mode = mode;

		pthread_mutex_unlock(&device->lock);
	}

	return 0;
}

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

	ALOGD("%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) {
		pthread_mutex_lock(&device->lock);

		if(device->mode == AUDIO_MODE_IN_CALL) {
			if(device->ril_interface != NULL)
				device_modem = device->ril_interface->device_current;
			else if(device->stream_out != NULL)
				device_modem = device->stream_out->device_current;
			else
				device_modem = AUDIO_DEVICE_OUT_EARPIECE;

			tinyalsa_mixer_set_mic_mute(device->mixer,
				device_modem, state);

			if(device->ril_interface != NULL)
				audio_ril_interface_set_mic_mute(device->ril_interface, state);
		} else {
			if(device->stream_in != NULL) {
				tinyalsa_mixer_set_mic_mute(device->mixer,
					device->stream_in->device_current, state);
			}
		}

		device->mic_mute = state;

		pthread_mutex_unlock(&device->lock);
	}

	return 0;
}

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

	ALOGD("%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)
{
	struct tinyalsa_audio_device *device;
	struct str_parms *parms;
	char value_string[32] = { 0 };
	int value;
	int rc;

	ALOGD("%s(%p, %s)", __func__, dev, kvpairs);

	if(dev == NULL || kvpairs == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

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

	parms = str_parms_create_str(kvpairs);
	if(parms == NULL)
		return -1;

	rc = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value_string, sizeof(value_string));
	if(rc < 0)
		goto error_params;

	value = atoi(value_string);

	pthread_mutex_lock(&device->lock);

	if(audio_is_output_device((audio_devices_t) value)) {
		if(device->stream_out != NULL && device->stream_out->device_current != (audio_devices_t) value) {
			pthread_mutex_lock(&device->stream_out->lock);
			audio_out_set_route(device->stream_out, (audio_devices_t) value);
			pthread_mutex_unlock(&device->stream_out->lock);
		}
		if(device->ril_interface != NULL && device->ril_interface->device_current != (audio_devices_t) value) {
			audio_ril_interface_set_route(device->ril_interface, (audio_devices_t) value);
		}
	} else if(audio_is_input_device((audio_devices_t) value)) {
		if(device->stream_in != NULL && device->stream_in->device_current != (audio_devices_t) value) {
			pthread_mutex_lock(&device->stream_in->lock);
			audio_in_set_route(device->stream_in, (audio_devices_t) value);
			pthread_mutex_unlock(&device->stream_in->lock);
		}
	}

	pthread_mutex_unlock(&device->lock);

	str_parms_destroy(parms);

	return 0;

error_params:
	str_parms_destroy(parms);

	return -1;
}

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

	return strdup("");
}

static size_t audio_hw_get_input_buffer_size(const struct audio_hw_device *dev,
	const struct audio_config *config)
{
	struct tinyalsa_audio_device *device;
	struct tinyalsa_mixer_io_props *mixer_props;
	int channel_count;
	size_t size;

	ALOGD("%s(%p, %p)", __func__, dev, config);

	if(dev == NULL || config == NULL)
		return -1;

	device = (struct tinyalsa_audio_device *) dev;

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

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

	// Default value
	if(mixer_props->rate == 0)
		mixer_props->rate = 44100;

	channel_count = popcount(config->channel_mask);

	size = (mixer_props->period_size * config->sample_rate) / mixer_props->rate;
	size = ((size + 15) / 16) * 16;
	size = size * channel_count * audio_bytes_per_sample(config->format);

	return size;
}

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

	return 0;
}

/*
 * Interface
 */

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

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

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

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

#ifdef YAMAHA_MC1N2_AUDIO
		if(tinyalsa_audio_device->mc1n2_pdata != NULL) {
			yamaha_mc1n2_audio_stop(tinyalsa_audio_device->mc1n2_pdata);
			tinyalsa_audio_device->mc1n2_pdata = NULL;
		}
#endif

		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;

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

	if(device == NULL)
		return -EINVAL;

	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 = AUDIO_DEVICE_API_VERSION_2_0;
	dev->common.module = (struct hw_module_t *) module;
	dev->common.close = audio_hw_close;

	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;

#ifdef YAMAHA_MC1N2_AUDIO
	rc = yamaha_mc1n2_audio_start(&tinyalsa_audio_device->mc1n2_pdata,
		YAMAHA_MC1N2_AUDIO_DEVICE);
	if(rc < 0) {
		ALOGE("Failed to open Yamaha-MC1N2-Audio");
		goto error_device;
	}

	rc = yamaha_mc1n2_audio_init(tinyalsa_audio_device->mc1n2_pdata);
	if(rc < 0) {
		ALOGE("Failed to init Yamaha-MC1N2-Audio");
	}
#endif

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

	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,
		.module_api_version = AUDIO_MODULE_API_VERSION_0_1,
		.hal_api_version = HARDWARE_HAL_API_VERSION,
		.id = AUDIO_HARDWARE_MODULE_ID,
		.name = "TinyALSA-Audio",
		.author = "Paul Kocialkowski",
		.methods = &audio_hw_module_methods,
	},
};