summaryrefslogtreecommitdiff
path: root/avsys-audio-pasimple.c
blob: 29c459395a6d3c85fc056bad4a32266f28acf27c (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
/*
 * avsystem
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
 *
 * 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 <stdlib.h>
#include <string.h>

#include "avsys-audio-pasimple.h"
#include "avsys-types.h"
#include "avsys-error.h"
#include "avsys-debug.h"

#define PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT				1536	/* frames */
#define PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE				4
#define PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT				6
#define PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK				8
#define PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE				12
#define PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO					10

#define PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC			20
#define PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC			25
#define PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC			50
#define PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC			75

#define MSEC_TO_SAMPLE(samplerate,period_time)		(samplerate*period_time/1000)

#define CHECK_VALID_HANDLE(handle) \
do { \
	if (handle == NULL) { \
		return AVSYS_STATE_ERR_NULL_POINTER; \
	} \
	device = (avsys_audio_pasimple_handle_t *)handle->device; \
		if (device == NULL) { \
			return AVSYS_STATE_ERR_NULL_POINTER; \
		} \
	if (device->pasimple_handle == NULL) { \
		return AVSYS_STATE_ERR_NULL_POINTER; \
	} \
} while (0)

#define SET_PA_ATTR(pt,spp,ppb,pb,mr,tl,ml,fs)	\
do { \
	period_time = pt; \
	samples_per_period = spp; \
	periods_per_buffer = ppb; \
	attr.prebuf = pb; \
	attr.minreq = mr; \
	attr.tlength = tl; \
	attr.maxlength = ml; \
	attr.fragsize = fs; \
} while (0)

int avsys_audio_pasimple_open_device(const int mode, const unsigned int format, const unsigned int channel, const unsigned int samplerate, avsys_audio_handle_t *handle, int policy)
{
	pa_simple *s = NULL;
	pa_sample_spec ss;
	avsys_audio_pasimple_handle_t *device = NULL;
	pa_buffer_attr attr;
	int err = AVSYS_STATE_SUCCESS;
	int period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;

	int samples_per_period = PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT;
	int periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;

	avsys_info(AVAUDIO, ">>>[%s] mode=%d, format=%d, channel=%d, samplerate=%d\n", __func__, mode, format, channel, samplerate);
	avsys_assert(handle != NULL);

	if (channel < 1 || channel > 2)
		return AVSYS_STATE_ERR_DEVICE_NOT_SUPPORT;

	device = (avsys_audio_pasimple_handle_t *)malloc(sizeof(avsys_audio_pasimple_handle_t));
	if (device == NULL) {
		avsys_critical(AVAUDIO, "PA Simple handle alloc fail\n");
		return AVSYS_STATE_ERR_ALLOCATION;
	}

	ss.rate = samplerate;
	ss.channels = channel;

	switch (format) {
	case AVSYS_AUDIO_FORMAT_8BIT:
		ss.format = PA_SAMPLE_U8;
		device->samplesize = 1 * channel;
		break;
	case AVSYS_AUDIO_FORMAT_16BIT:

		ss.format = PA_SAMPLE_S16LE;
		device->samplesize = 2 * channel;
		break;
	default:
		free(device);
		avsys_error(AVAUDIO, "Invalid format\n");
		return AVSYS_STATE_ERR_DEVICE_NOT_SUPPORT;
	}
	handle->device = (void *)device;

	pa_proplist *proplist = pa_proplist_new();

	/* Set policy property */
	avsys_info(AVAUDIO, ">>>[%s] policy=[%d], vol_type=[%d]\n", __func__, policy, handle->gain_setting.vol_type);
	if (policy == AVSYS_AUDIO_HANDLE_ROUTE_HANDSET_ONLY) {
		avsys_info(AVAUDIO, ": set media plicy to PHONE\n");
		pa_proplist_sets(proplist, PA_PROP_MEDIA_POLICY, "phone");
	} else {
		/* AVSYS_AUDIO_HANDLE_ROUTE_FOLLOWING_POLICY */
		/* check stream type (vol type) */
		if (handle->gain_setting.vol_type == AVSYS_AUDIO_VOLUME_TYPE_NOTIFICATION ||
			handle->gain_setting.vol_type == AVSYS_AUDIO_VOLUME_TYPE_ALARM) {
			avsys_info(AVAUDIO, ": set media plicy to ALL\n");
			pa_proplist_sets(proplist, PA_PROP_MEDIA_POLICY, "all");
		} else {
			avsys_info(AVAUDIO, ": set media plicy to AUTO\n");
			pa_proplist_sets(proplist, PA_PROP_MEDIA_POLICY, "auto");
		}
	}

	handle->handle_route = policy;

	memset(&attr, '\0', sizeof(attr));

	switch (mode) {
	case AVSYS_AUDIO_MODE_INPUT:
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT,
					0, -1, -1, -1, samples_per_period * device->samplesize);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_RECORD, NULL, "CAPTURE", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_INPUT_LOW_LATENCY:
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE,
					0, -1, -1, -1, samples_per_period * device->samplesize);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_RECORD, NULL, "LOW LATENCY CAPTURE", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_INPUT_HIGH_LATENCY:
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE,
					0, -1, -1, -1, samples_per_period * device->samplesize);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_RECORD, NULL, "HIGH LATENCY CAPTURE", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_OUTPUT:	/* mid latency playback for normal audio case. */
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC,
						MSEC_TO_SAMPLE(samplerate,period_time),
						PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT,
						-1, -1, periods_per_buffer * samples_per_period * device->samplesize, attr.tlength, 0);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_PLAYBACK, NULL, "PLAYBACK", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_OUTPUT_LOW_LATENCY:	/* This is special case for touch sound playback */
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE,
					samples_per_period * device->samplesize, -1, samples_per_period * device->samplesize + 3430, (uint32_t)-1, 0);

		s = pa_simple_new_proplist(NULL,"AVSYSTEM", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", &ss, NULL, &attr, proplist, &err);
		break;
	case AVSYS_AUDIO_MODE_OUTPUT_CLOCK: /* high latency playback - lager buffer size */
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK,
					(uint32_t) -1, (uint32_t) -1, periods_per_buffer * samples_per_period * device->samplesize, (uint32_t)-1, 0);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_PLAYBACK, NULL, "HIGH LATENCY PLAYBACK", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_OUTPUT_VIDEO:	/* low latency playback */
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO,
					4*(samples_per_period * device->samplesize), samples_per_period * device->samplesize, periods_per_buffer * samples_per_period * device->samplesize, (uint32_t)-1, 0);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", &ss, NULL, &attr, proplist, &err);
		break;

	case AVSYS_AUDIO_MODE_OUTPUT_AP_CALL:
#if defined(_MMFW_I386_ALL_SIMULATOR)
		avsys_warning(AVAUDIO, "Does not support AP call mode at i386 simulator\n");
		s = NULL;
#else
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT,
					(uint32_t) -1, (uint32_t) -1, periods_per_buffer * samples_per_period * device->samplesize, attr.tlength, 0);


		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_PLAYBACK, NULL, "VoIP PLAYBACK", &ss, NULL, &attr, proplist, &err);
#endif
		break;
	case AVSYS_AUDIO_MODE_INPUT_AP_CALL:
#if defined(_MMFW_I386_ALL_SIMULATOR)
		avsys_warning(AVAUDIO, "Does not support AP call mode at i386 simulator\n");
		s = NULL;
#else
		SET_PA_ATTR(PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC,
					MSEC_TO_SAMPLE(samplerate,period_time),
					PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT,
					0, (uint32_t) -1, (uint32_t) -1, (uint32_t) -1, samples_per_period * device->samplesize);

		s = pa_simple_new_proplist(NULL, "AVSYSTEM", PA_STREAM_RECORD, NULL, "VoIP CAPTURE", &ss, NULL, &attr, proplist, &err);
#endif
		break;
	case AVSYS_AUDIO_MODE_CALL_OUT:
	case AVSYS_AUDIO_MODE_CALL_IN:
		//TODO
		avsys_error(AVAUDIO, "Does not support call device handling\n");
		avsys_assert_r(0);
		break;
	default:
		avsys_critical_r(AVAUDIO, "Invalid open mode %d\n", mode);
		avsys_assert_r(0);
		return AVSYS_STATE_ERR_INVALID_MODE;
		break;
	}

	if (!s) {
		avsys_error_r(AVAUDIO, "Open pulseaudio handle has failed - %s\n", pa_strerror(err));
		err = AVSYS_STATE_ERR_INTERNAL;
		goto fail;
	}

	avsys_info(AVAUDIO, "Samples(per period) : %d\t Periods(per buffer) : %d\n", samples_per_period, periods_per_buffer);

	device->pasimple_handle = (void *)s;
	device->mode = mode;
	device->period_frames = samples_per_period;
	device->buffer_frames = periods_per_buffer * device->period_frames;
	device->periods_per_buffer = periods_per_buffer;
	handle->period = device->period_frames * device->samplesize;
	handle->msec_per_period = period_time;
	if (0 > pa_simple_get_stream_index(s, &handle->stream_index, &err)) {
		avsys_error(AVAUDIO, "Can not get stream index %s\n", pa_strerror(err));
		err = AVSYS_STATE_ERR_INVALID_HANDLE;
	}

fail:
	if (proplist)
		pa_proplist_free(proplist);

	return err;
}

int avsys_audio_pasimple_close_device(avsys_audio_handle_t *handle)
{
	int err = 0;
	avsys_audio_pasimple_handle_t *device = NULL;
	pa_simple *s = NULL;

	avsys_info(AVAUDIO, "%s\n", __func__);

	avsys_assert(handle != NULL);
	CHECK_VALID_HANDLE(handle);

	switch (handle->mode) {
	case AVSYS_AUDIO_MODE_CALL_OUT:
	case AVSYS_AUDIO_MODE_CALL_IN:
		avsys_warning(AVAUDIO, "Unsupported close mode in pa function\n");
		return AVSYS_STATE_ERR_INVALID_MODE;
	case AVSYS_AUDIO_MODE_OUTPUT_AP_CALL:
	case AVSYS_AUDIO_MODE_INPUT_AP_CALL:
#if defined(_MMFW_I386_ALL_SIMULATOR)
		avsys_warning(AVAUDIO, "Skip close call device in SDK");
		return AVSYS_STATE_SUCCESS;
#endif
	default:
		break;
	}

	s = (pa_simple *)device->pasimple_handle;
	avsys_assert(s != NULL);

	switch (handle->mode) {
	case AVSYS_AUDIO_MODE_OUTPUT:
	case AVSYS_AUDIO_MODE_OUTPUT_CLOCK:
	case AVSYS_AUDIO_MODE_OUTPUT_LOW_LATENCY:
	case AVSYS_AUDIO_MODE_OUTPUT_AP_CALL:
	case AVSYS_AUDIO_MODE_OUTPUT_VIDEO:
		if (0 > pa_simple_flush(s, &err)) {
			avsys_error(AVAUDIO, "pa_simple_flush() failed with %s\n", pa_strerror(err));
		}
		break;
	default:
		break;
	}

	pa_simple_free(s);

	device->pasimple_handle = NULL;
	free(device);

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_pasimple_write(avsys_audio_handle_t *handle, const void *buf, int size)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;

	if (buf == NULL)
			return AVSYS_STATE_ERR_NULL_POINTER;
	CHECK_VALID_HANDLE(handle);

	if (size < 0)
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	else if (size == 0)
		return 0;

	s = (pa_simple *)device->pasimple_handle;

	if (0 > pa_simple_write(s, buf, size, &err)) {
		avsys_error(AVAUDIO, "pa_simple_write() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	return size;
}

int avsys_audio_pasimple_read(avsys_audio_handle_t *handle, void *buf, int size)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;

	if (buf == NULL)
			return AVSYS_STATE_ERR_NULL_POINTER;
	CHECK_VALID_HANDLE(handle);

	if (size < 0)
		return AVSYS_STATE_ERR_INVALID_PARAMETER;
	else if (size == 0)
		return 0;

	s = (pa_simple *)device->pasimple_handle;

	if (0 > pa_simple_read(s, buf, size, &err)) {
		avsys_error(AVAUDIO, "pa_simple_read() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	return size;
}

int avsys_audio_pasimple_reset(avsys_audio_handle_t *handle)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;

	CHECK_VALID_HANDLE(handle);

	if (handle->mode == AVSYS_AUDIO_MODE_INPUT || handle->mode == AVSYS_AUDIO_MODE_INPUT_LOW_LATENCY) {
		avsys_warning(AVAUDIO, "Skip pa_simple_flush() when input mode\n");
		return AVSYS_STATE_SUCCESS;
	}

	s = (pa_simple *)device->pasimple_handle;

	if (0 > pa_simple_flush(s, &err)) {
		avsys_error(AVAUDIO, "pa_simple_flush() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_pasimple_drain(avsys_audio_handle_t *handle)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;

	CHECK_VALID_HANDLE(handle);

	s = (pa_simple *)device->pasimple_handle;

	if (0 > pa_simple_drain(s, &err)) {
		avsys_error(AVAUDIO, "pa_simple_drain() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_pasimple_set_volume(avsys_audio_handle_t *handle, int volume)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;

	CHECK_VALID_HANDLE(handle);

	s = (pa_simple *)device->pasimple_handle;

	if (0 > pa_simple_set_volume(s, volume, &err)) {
		avsys_error(AVAUDIO, "pa_simple_set_volume() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	return AVSYS_STATE_SUCCESS;
}

#define USEC_TO_SAMPLE(usec, rate)	((usec*rate)/1000000)
#define SAMPLES_TO_USEC(samples,rate)	((samples*1000000)/rate)
#define BYTES_TO_USEC(bytes,size_per_sample,rate)	((bytes*1000000)/(size_per_sample*rate))

int avsys_audio_pasimple_delay(avsys_audio_handle_t *handle, int *delay)
{
	pa_simple *s = NULL;
	avsys_audio_pasimple_handle_t *device = NULL;
	int err = 0;
	pa_usec_t latency_time = 0;
	unsigned int latency_frames = 0;

	if (delay == NULL) {
		return AVSYS_STATE_ERR_NULL_POINTER;
	}
	CHECK_VALID_HANDLE(handle);

	s = (pa_simple *)device->pasimple_handle;

	latency_time = pa_simple_get_latency(s, &err);
	if (err > 0 && latency_time == 0) {
		avsys_error(AVAUDIO, "pa_simple_get_latency() failed with %s\n", pa_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}
	/* convert time to sample */
	latency_frames = USEC_TO_SAMPLE(latency_time, handle->samplerate);
	*delay = latency_frames;

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_pasimple_get_period_buffer_time(avsys_audio_handle_t *handle, unsigned int *period_time, unsigned int *buffer_time)
{
	avsys_audio_pasimple_handle_t *device = NULL;

	if ((period_time == NULL) || (buffer_time == NULL))
		return AVSYS_STATE_ERR_INTERNAL;

	CHECK_VALID_HANDLE(handle);

	*period_time = SAMPLES_TO_USEC(device->period_frames,handle->samplerate);
	*buffer_time = *period_time * device->periods_per_buffer;

	avsys_info(AVAUDIO, "[%s][%d] period = %d, buffer = %d\n", __func__, __LINE__, *period_time, *buffer_time);

	return AVSYS_STATE_SUCCESS;
}