summaryrefslogtreecommitdiff
path: root/mm_sound_pa_client.c
blob: a7278dfceaa9eaa3de4bafa3d17efca2ff7db7f4 (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
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <mm_error.h>
#include <mm_debug.h>

#include <mm_sound_pa_client.h>

#include <glib.h>

#define MM_SOUND_CHANNEL_MIN                1
#define MM_SOUND_CHANNEL_MAX                6

typedef struct _mm_sound_handle_t {
	uint32_t handle;

	int mode;
	int volume_type;
	int gain_type;
	int rate;
	int channels;
	pa_simple *s;

	int period;	/* open api retrun value. */

	int source_type;
} mm_sound_handle_t;

static struct {
	uint32_t handle_count;	/* use amotic operations */
	GList *handles;
	pthread_mutex_t lock;

	int state;
	pa_threaded_mainloop *mainloop;
	pa_context *context;
} mm_sound_handle_mgr;

#define CHECK_HANDLE_RANGE(x) \
	do { \
		if (x == 0) { \
			debug_msg("invalid handle(%d)", x); \
			return MM_ERROR_INVALID_ARGUMENT; \
		} \
	} while (0);

#define ATOMIC_INC(l, x) \
	do { \
		pthread_mutex_lock(l); \
		x = x + 1; \
		if (x == 0) \
		x = x + 1; \
		pthread_mutex_unlock(l); \
	} while (0);

// phandle(ret), GList, userdata, coimpare func
#define GET_HANDLE_DATA(p, l, u, func) \
	do { \
		GList* list = 0; \
		list = g_list_find_custom(l, u, func); \
		if (list != 0) \
			p = (mm_sound_handle_t*)list->data; \
		else \
			p = NULL; \
	} while (0);

#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_VOIP					2
#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 PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC			20

__attribute__ ((constructor))
void __mm_sound_pa_init(void)
{
	memset(&mm_sound_handle_mgr, 0, sizeof(mm_sound_handle_mgr));
	mm_sound_handle_mgr.state = FALSE;

	mm_sound_handle_mgr.handles = g_list_alloc();
	mm_sound_handle_mgr.handle_count = 1;
	pthread_mutex_init(&mm_sound_handle_mgr.lock, NULL);
}

__attribute__ ((destructor))
void __mm_sound_pa_deinit(void)
{
	g_list_free(mm_sound_handle_mgr.handles);
	pthread_mutex_destroy(&mm_sound_handle_mgr.lock);
	mm_sound_handle_mgr.handle_count = 0;

	if (mm_sound_handle_mgr.state) {
		debug_msg("mainloop(%p),  context(%p)", mm_sound_handle_mgr.mainloop, mm_sound_handle_mgr.context);
		pa_threaded_mainloop_stop(mm_sound_handle_mgr.mainloop);
		pa_context_disconnect(mm_sound_handle_mgr.context);
		pa_context_unref(mm_sound_handle_mgr.context);
		pa_threaded_mainloop_free(mm_sound_handle_mgr.mainloop);
	}
}

gint __mm_sound_handle_comparefunc(gconstpointer a, gconstpointer b)
{
	mm_sound_handle_t *phandle = (mm_sound_handle_t *) a;
	int *handle = (int *)b;

	if (phandle == NULL)
		return -1;

	if (phandle->handle == *handle)
		return 0;
	else
		return -1;
}

EXPORT_API
int mm_sound_pa_open(MMSoundHandleMode mode, int volume_config, pa_sample_spec * ss, pa_channel_map * channel_map,
					int *size, char *stream_type, int stream_index)
{
	pa_simple *s = NULL;
	pa_channel_map maps;
	pa_buffer_attr attr;

	int prop_vol_type = 0;
	int prop_gain_type = VOLUME_GAIN_DEFAULT;

	int err = MM_ERROR_SOUND_INTERNAL;
	int period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
	int samples_per_period = PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT;

	int handle_mode = mode;
	int sample_size = 0;
	pa_proplist *proplist = NULL;

	mm_sound_handle_t *handle = NULL;

	if (ss->channels < MM_SOUND_CHANNEL_MIN || ss->channels > MM_SOUND_CHANNEL_MAX)
		return MM_ERROR_INVALID_ARGUMENT;

	proplist = pa_proplist_new();

	if (channel_map == NULL) {
		pa_channel_map_init_auto(&maps, ss->channels, PA_CHANNEL_MAP_ALSA);
		channel_map = &maps;
	}

	switch (ss->format) {
	case PA_SAMPLE_U8:
		sample_size = 1 * ss->channels;
		break;
	case PA_SAMPLE_S16LE:
		sample_size = 2 * ss->channels;
		break;
	default:
		sample_size = 0;
		debug_error("Invalid sample size (%d)", sample_size);
		break;
	}

	if (stream_index != -1) {
		char stream_index_s[11];
		debug_msg("Set stream index [%d]", stream_index);

		snprintf(stream_index_s, sizeof(stream_index_s) - 1, "%d", stream_index);
		debug_msg("stream_index[%d] converted to string[%s]", stream_index, stream_index_s);
		pa_proplist_sets(proplist, PA_PROP_MEDIA_PARENT_ID, stream_index_s);
	}
	/* Set stream type */
	pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, stream_type);

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

	switch (handle_mode) {
	case HANDLE_MODE_OUTPUT:
		period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
		samples_per_period = (ss->rate * period_time) / 1000;
		attr.prebuf = -1;
		attr.minreq = -1;
		attr.tlength = (uint32_t)((ss->rate / 10) * pa_frame_size(ss));
		attr.maxlength = -1;
		attr.fragsize = 0;

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

	default:
		err = MM_ERROR_SOUND_INTERNAL;
		goto fail;
	}

	if (!s) {
		debug_error("Open pulseaudio handle has failed - %s", pa_strerror(err));
		if (!strncmp(pa_strerror(err), "Access denied by security check", strlen(pa_strerror(err))))
			err = MM_ERROR_SOUND_PERMISSION_DENIED;
		else
			err = MM_ERROR_SOUND_INTERNAL;
		goto fail;
	}

	handle = (mm_sound_handle_t *) malloc(sizeof(mm_sound_handle_t));
	if (!handle) {
		debug_error("Failed to alloc handle");
		err = MM_ERROR_OUT_OF_MEMORY;
		goto fail;
	}
	memset(handle, 0, sizeof(mm_sound_handle_t));
	handle->mode = mode;
	handle->volume_type = prop_vol_type;
	handle->gain_type = prop_gain_type;
	handle->rate = ss->rate;
	handle->channels = ss->channels;
	handle->s = s;
	handle->period = samples_per_period * sample_size;
	*size = handle->period;
	handle->handle = mm_sound_handle_mgr.handle_count;
	ATOMIC_INC(&mm_sound_handle_mgr.lock, mm_sound_handle_mgr.handle_count);	// 0 is not used

	mm_sound_handle_mgr.handles = g_list_append(mm_sound_handle_mgr.handles, handle);

	if (handle->handle == 0) {
		debug_msg("out of range. handle(%d)", handle->handle);
		goto fail;
	}

	debug_msg
		("created handle[%d]. mode(%d), volumetype(%d), gain(%d), rate(%d), channels(%d), format(%d), s(%p), source_type(%d)",
		 handle->handle, handle->mode, handle->volume_type, handle->gain_type, handle->rate, handle->channels, ss->format,
		 handle->s, handle->source_type);

	if (proplist)
		pa_proplist_free(proplist);

	return handle->handle;

 fail:
	if (proplist)
		pa_proplist_free(proplist);

	if (handle)
		free(handle);

	return err;

}

EXPORT_API
int mm_sound_pa_write(const int handle, void *buf, const int size)
{
	mm_sound_handle_t *phandle = NULL;
	int err = MM_ERROR_NONE;

	if (buf == NULL)
		return MM_ERROR_INVALID_ARGUMENT;

	if (size < 0)
		return MM_ERROR_INVALID_ARGUMENT;
	else if (size == 0)
		return MM_ERROR_NONE;

	CHECK_HANDLE_RANGE(handle);
	GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);

	if (phandle == NULL) {
		debug_msg("phandle is null");
		return MM_ERROR_SOUND_INTERNAL;
	}

	if (0 > pa_simple_write(phandle->s, buf, size, &err)) {
		debug_error("pa_simple_write() failed with %s", pa_strerror(err));
		return MM_ERROR_SOUND_INTERNAL;
	}

	return size;

}

EXPORT_API
int mm_sound_pa_close(const int handle)
{
	mm_sound_handle_t *phandle = NULL;
	int err = MM_ERROR_NONE;

	CHECK_HANDLE_RANGE(handle);
	GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
	if (phandle == NULL) {
		debug_msg("phandle is null");
		return MM_ERROR_SOUND_INTERNAL;
	}

	debug_msg("phandle(%p) s(%p), handle(%d), rate(%d), ch(%d)",
			  phandle, phandle->s, phandle->handle, phandle->rate, phandle->channels);

	switch (phandle->mode) {
	case HANDLE_MODE_OUTPUT:
		if (0 > pa_simple_flush(phandle->s, &err)) {
			err = MM_ERROR_SOUND_INTERNAL;
			debug_msg("pa_simple_flush() failed with %s", pa_strerror(err));
		}
		break;
	default:
		break;
	}

	pa_simple_free(phandle->s);
	phandle->s = NULL;

	debug_msg("leave: handle[%d]", handle);

	mm_sound_handle_mgr.handles = g_list_remove(mm_sound_handle_mgr.handles, phandle);
	if (phandle != NULL) {
		free(phandle);
		phandle = NULL;
	}

	return err;

}

EXPORT_API
int mm_sound_pa_drain(const int handle)
{
	mm_sound_handle_t *phandle = NULL;
	int err = MM_ERROR_NONE;

	CHECK_HANDLE_RANGE(handle);
	GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
	if (phandle == NULL)
		return MM_ERROR_SOUND_INTERNAL;

	if (0 > pa_simple_drain(phandle->s, &err)) {
		debug_error("pa_simple_drain() failed with %s", pa_strerror(err));
		err = MM_ERROR_SOUND_INTERNAL;
	}

	return err;
}