summaryrefslogtreecommitdiffstats
path: root/audio_out.c
blob: 573dd4e4d512412698808b98d8acda3d4b71d4db (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * 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 Output"

#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

#define EFFECT_UUID_NULL EFFECT_UUID_NULL_OUT
#define EFFECT_UUID_NULL_STR EFFECT_UUID_NULL_STR_OUT
#include "audio_hw.h"
#include "mixer.h"

/*
 * Functions
 */

int audio_out_pcm_open(struct tinyalsa_audio_stream_out *stream_out)
{
	struct pcm *pcm = NULL;
	struct pcm_config pcm_config;

	if(stream_out == NULL)
		return -1;

	memset(&pcm_config, 0, sizeof(pcm_config));
	pcm_config.channels = popcount(stream_out->mixer_props->channels);
	pcm_config.rate = stream_out->mixer_props->rate;
	switch(stream_out->mixer_props->format) {
		case AUDIO_FORMAT_PCM_16_BIT:
			pcm_config.format = PCM_FORMAT_S16_LE;
			break;
		case AUDIO_FORMAT_PCM_32_BIT:
			pcm_config.format = PCM_FORMAT_S32_LE;
			break;
		default:
			LOGE("Invalid format: 0x%x", stream_out->mixer_props->format);
			return -1;
	}
	pcm_config.period_size = stream_out->mixer_props->period_size;
	pcm_config.period_count = stream_out->mixer_props->period_count;

	pcm = pcm_open(stream_out->mixer_props->card,
		stream_out->mixer_props->device, PCM_OUT, &pcm_config);

	if(pcm == NULL || !pcm_is_ready(pcm)) {
		LOGE("Unable to open pcm device: %s", pcm_get_error(pcm));
		return -1;
	}

	stream_out->pcm = pcm;

	return 0;
}

void audio_out_pcm_close(struct tinyalsa_audio_stream_out *stream_out)
{
	if(stream_out->pcm == NULL)
		return;

	pcm_close(stream_out->pcm);
	stream_out->pcm = NULL;
}

int audio_out_set_route(struct tinyalsa_audio_stream_out *stream_out,
	audio_devices_t device)
{
	int rc;

	if(stream_out == NULL)
		return -1;

	stream_out->device_current = device;

	if(device == 0)
		return stream_out->stream.common.standby((struct audio_stream *) stream_out);

	tinyalsa_mixer_set_device(stream_out->device->mixer, stream_out->device_current);

#ifdef YAMAHA_MC1N2_AUDIO
	yamaha_mc1n2_audio_set_route(stream_out->device->mc1n2_pdata, device);
#endif

	return 0;
}

static uint32_t audio_out_get_sample_rate(const struct audio_stream *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	return stream_out->rate;
}

static int audio_out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
	struct tinyalsa_audio_stream_out *stream_out;

	LOGD("%s(%p, %d)", __func__, stream, rate);

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	// FIXME: If rate is different, change resampler
	stream_out->rate = rate;

	return 0;
}

static size_t audio_out_get_buffer_size(const struct audio_stream *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;
	size_t size;

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	size = (stream_out->mixer_props->period_size * stream_out->rate) /
		stream_out->rate;
	size = ((size + 15) / 16) * 16;
	size = size * audio_stream_frame_size((struct audio_stream *) stream);

	return size;
}

static uint32_t audio_out_get_channels(const struct audio_stream *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	return stream_out->channels;
}

static int audio_out_get_format(const struct audio_stream *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	return stream_out->format;
}

static int audio_out_set_format(struct audio_stream *stream, int format)
{
	struct tinyalsa_audio_stream_out *stream_out;

	LOGD("%s(%p, %d)", __func__, stream, format);

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	// FIXME: If format is different, change resampler
	stream_out->format = format;

	return 0;
}

static int audio_out_standby(struct audio_stream *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;
	int rc;

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

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	if(stream_out->pcm != NULL)
		audio_out_pcm_close(stream_out);

#ifdef YAMAHA_MC1N2_AUDIO
	if(!stream_out->standby) {
		rc = yamaha_mc1n2_audio_output_stop(stream_out->device->mc1n2_pdata);
		if(rc < 0) {
			LOGE("Failed to set Yamaha-MC1N2-Audio route");
		}
	}
#endif

	stream_out->standby = 1;

	return 0;
}

static int audio_out_dump(const struct audio_stream *stream, int fd)
{
	LOGD("%s(%p, %d)", __func__, stream, fd);

	return 0;
}

static int audio_out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
	struct tinyalsa_audio_stream_out *stream_out;
	struct str_parms *parms;
	char value_string[32] = { 0 };
	int value;
	int rc;

	LOGD("%s(%p, %s)", __func__, stream, kvpairs);

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

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

	if(stream_out->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);

	if(stream_out->device_current != (audio_devices_t) value)
		audio_out_set_route(stream_out, (audio_devices_t) value);

	str_parms_destroy(parms);

	return 0;

error_params:
	str_parms_destroy(parms);

	return -1;
}

static char *audio_out_get_parameters(const struct audio_stream *stream, const char *keys)
{
	LOGD("%s(%p, %s)", __func__, stream, keys);

	return strdup("");
}

static uint32_t audio_out_get_latency(const struct audio_stream_out *stream)
{
	LOGD("%s(%p)", __func__, stream);

	return 0;
}

static int audio_out_set_volume(struct audio_stream_out *stream, float left,
	float right)
{
	struct tinyalsa_audio_stream_out *stream_out;
	float volume;

	LOGD("%s(%p, %f, %f)", __func__, stream, left, right);

	if(stream == NULL)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

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

	volume = (left + right) / 2;

	tinyalsa_mixer_set_output_volume(stream_out->device->mixer,
		stream_out->device_current, volume);

	return 0;
}

static ssize_t audio_out_write(struct audio_stream_out *stream,
	const void *buffer, size_t bytes)
{
	struct tinyalsa_audio_stream_out *stream_out;
	int rc;

	if(stream == NULL || buffer == NULL || bytes <= 0)
		return -1;

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

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

		rc = audio_out_pcm_open(stream_out);
		if(rc < 0) {
			LOGE("Unable to open pcm device");
			return -1;
		}

		stream_out->standby = 0;
	}

	if(stream_out->pcm == NULL || !pcm_is_ready(stream_out->pcm)) {
		LOGE("pcm device is not ready");
		return -1;
	}

	rc = pcm_write(stream_out->pcm, (void *) buffer, (int) bytes);
	if(rc != 0) {
		LOGE("pcm write failed!");
		return -1;
	}

	return bytes;
}

static int audio_out_get_render_position(const struct audio_stream_out *stream,
	uint32_t *dsp_frames)
{
	LOGD("%s(%p, %p)", __func__, stream, dsp_frames);

	return -EINVAL;
}

static int audio_out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
	LOGD("%s(%p, %p)", __func__, stream, effect);

	return 0;
}

static int audio_out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
	LOGD("%s(%p, %p)", __func__, stream, effect);

	return 0;
}

/*
 * Interface
 */

void audio_hw_close_output_stream(struct audio_hw_device *dev,
	struct audio_stream_out *stream)
{
	struct tinyalsa_audio_stream_out *stream_out;
	struct tinyalsa_audio_device *tinyalsa_audio_device;

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

	stream_out = (struct tinyalsa_audio_stream_out *) stream;

#ifdef YAMAHA_MC1N2_AUDIO
	if(stream_out != NULL && !stream_out->standby)
		yamaha_mc1n2_audio_output_stop(stream_out->device->mc1n2_pdata);
#endif

	if(stream != NULL)
		free(stream);

	if(dev == NULL)
		return;

	tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev;

	tinyalsa_mixer_set_output_state(tinyalsa_audio_device->mixer, 0);

	tinyalsa_audio_device->stream_out = NULL;
}

int audio_hw_open_output_stream(struct audio_hw_device *dev,
	uint32_t devices, int *format, uint32_t *channels, uint32_t *sample_rate,
	struct audio_stream_out **stream_out)
{
	struct tinyalsa_audio_device *tinyalsa_audio_device;
	struct tinyalsa_audio_stream_out *tinyalsa_audio_stream_out;
	struct audio_stream_out *stream;
	int rc;

	LOGD("%s(%p, %d, %p, %p, %p, %p)",
		__func__, dev, devices, format, channels, sample_rate, stream_out);

	if(dev == NULL)
		return -EINVAL;

	tinyalsa_audio_device = (struct tinyalsa_audio_device *) dev;
	tinyalsa_audio_stream_out = calloc(1, sizeof(struct tinyalsa_audio_stream_out));

	if(tinyalsa_audio_stream_out == NULL)
		return -ENOMEM;

	tinyalsa_audio_stream_out->device = tinyalsa_audio_device;
	tinyalsa_audio_device->stream_out = tinyalsa_audio_stream_out;
	stream = &(tinyalsa_audio_stream_out->stream);

	stream->common.get_sample_rate = audio_out_get_sample_rate;
	stream->common.set_sample_rate = audio_out_set_sample_rate;
	stream->common.get_buffer_size = audio_out_get_buffer_size;
	stream->common.get_channels = audio_out_get_channels;
	stream->common.get_format = audio_out_get_format;
	stream->common.set_format = audio_out_set_format;
	stream->common.standby = audio_out_standby;
	stream->common.dump = audio_out_dump;
	stream->common.set_parameters = audio_out_set_parameters;
	stream->common.get_parameters = audio_out_get_parameters;
	stream->common.add_audio_effect = audio_out_add_audio_effect;
	stream->common.remove_audio_effect = audio_out_remove_audio_effect;
	stream->get_latency = audio_out_get_latency;
	stream->set_volume = audio_out_set_volume;
	stream->write = audio_out_write;
	stream->get_render_position = audio_out_get_render_position;

	if(tinyalsa_audio_device->mixer == NULL)
		goto error_stream;

	tinyalsa_audio_stream_out->mixer_props =
		tinyalsa_mixer_get_output_props(tinyalsa_audio_device->mixer);

	if(tinyalsa_audio_stream_out->mixer_props == NULL)
		goto error_stream;

	tinyalsa_audio_stream_out->rate =
		tinyalsa_audio_stream_out->mixer_props->rate;
	tinyalsa_audio_stream_out->channels =
		tinyalsa_audio_stream_out->mixer_props->channels;
	tinyalsa_audio_stream_out->format =
		tinyalsa_audio_stream_out->mixer_props->format;

	*sample_rate = (uint32_t) tinyalsa_audio_stream_out->rate;
	*channels = (uint32_t) tinyalsa_audio_stream_out->channels;
	*format = (uint32_t) tinyalsa_audio_stream_out->format;

	rc = tinyalsa_mixer_set_output_state(tinyalsa_audio_device->mixer, 1);
	if(rc < 0) {
		LOGE("Unable to set output state");
		goto error_stream;
	}

	audio_out_set_route(tinyalsa_audio_stream_out, devices);

	rc = audio_out_pcm_open(tinyalsa_audio_stream_out);
	if(rc < 0) {
		LOGE("Unable to open pcm device");
		goto error_stream;
	}

	audio_out_pcm_close(tinyalsa_audio_stream_out);

	tinyalsa_audio_stream_out->standby = 1;

	*stream_out = stream;

	return 0;

error_stream:
	*stream_out = NULL;

	return -1;
}