4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include <mm_sound_pa_client.h>
33 #define MM_SOUND_CHANNEL_MIN 1
34 #define MM_SOUND_CHANNEL_MAX 6
36 typedef struct _mm_sound_handle_t {
46 int period; /* open api retrun value.*/
48 unsigned int stream_idx;
52 #define MM_SOUND_HANDLE_MAX 32
54 uint32_t handle_count; /* use amotic operations */
59 pa_threaded_mainloop *mainloop;
61 } mm_sound_handle_mgr;
63 #define CHECK_HANDLE_RANGE(x) \
66 debug_msg("invalid handle(%d)", x); \
67 return MM_ERROR_INVALID_ARGUMENT; \
71 #define CHECK_VOLUME_TYPE_RANGE(x) \
73 if(x < VOLUME_TYPE_SYSTEM || x >= VOLUME_TYPE_MAX) { \
74 debug_msg("invalid volume type(%d)", x); \
75 return MM_ERROR_INVALID_ARGUMENT; \
79 #define ATOMIC_INC(l, x) \
81 pthread_mutex_lock(l); \
85 pthread_mutex_unlock(l); \
88 // phandle(ret), GList, userdata, coimpare func
89 #define GET_HANDLE_DATA(p, l, u, func) \
92 list = g_list_find_custom(l, u, func); \
94 p = (mm_sound_handle_t*)list->data; \
100 // should be call after pa_ext function.
101 #define WAIT_PULSEAUDIO_OPERATION(x, y) \
103 while (pa_operation_get_state(y) == PA_OPERATION_RUNNING) { \
104 debug_msg("waiting.................."); \
105 pa_threaded_mainloop_wait(x.mainloop); \
106 debug_msg("waiting DONE"); \
110 #define PA_SIMPLE_FADE_INTERVAL_USEC 20000
112 #define PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT 1536 /* frames */
113 #define PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE 4
114 #define PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT 6
115 #define PA_SIMPLE_PERIODS_PER_BUFFER_VOIP 2
116 #define PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK 8
117 #define PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE 12
118 #define PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO 10
120 #define PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC 20
121 #define PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC 25
122 #define PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC 50
123 #define PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC 75
124 #define PA_SIMPLE_PERIOD_TIME_FOR_VERY_HIGH_LATENCY_MSEC 150
125 #define PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC 20
127 #define IS_INPUT_HANDLE(x) \
128 if( x == HANDLE_MODE_INPUT || x == HANDLE_MODE_INPUT_HIGH_LATENCY || \
129 x == HANDLE_MODE_INPUT_LOW_LATENCY || x == HANDLE_MODE_INPUT_AP_CALL )
131 __attribute__ ((constructor)) void __mm_sound_pa_init(void)
133 memset(&mm_sound_handle_mgr, 0, sizeof(mm_sound_handle_mgr));
134 mm_sound_handle_mgr.state = FALSE;
136 mm_sound_handle_mgr.handles = g_list_alloc();
137 mm_sound_handle_mgr.handle_count = 1;
138 pthread_mutex_init(&mm_sound_handle_mgr.lock, NULL);
141 __attribute__ ((destructor)) void __mm_sound_pa_deinit(void)
143 g_list_free(mm_sound_handle_mgr.handles);
144 pthread_mutex_destroy(&mm_sound_handle_mgr.lock);
145 mm_sound_handle_mgr.handle_count = 0;
147 if(mm_sound_handle_mgr.state) {
148 debug_msg("mainloop(%x), context(%x)", mm_sound_handle_mgr.mainloop, mm_sound_handle_mgr.context);
149 pa_threaded_mainloop_stop(mm_sound_handle_mgr.mainloop);
150 pa_context_disconnect(mm_sound_handle_mgr.context);
151 pa_context_unref(mm_sound_handle_mgr.context);
152 pa_threaded_mainloop_free(mm_sound_handle_mgr.mainloop);
156 gint __mm_sound_handle_comparefunc(gconstpointer a, gconstpointer b)
158 mm_sound_handle_t* phandle = (mm_sound_handle_t*)a;
159 int* handle = (int*)b;
164 if(phandle->handle == *handle)
171 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)
177 int prop_vol_type = 0;
178 int prop_gain_type = VOLUME_GAIN_DEFAULT;
180 int err = MM_ERROR_SOUND_INTERNAL;
181 int period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
182 int samples_per_period = PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT;
183 int periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
185 int handle_mode = mode;
187 pa_proplist *proplist = NULL;
189 mm_sound_handle_t* handle = NULL;
191 if (ss->channels < MM_SOUND_CHANNEL_MIN || ss->channels > MM_SOUND_CHANNEL_MAX)
192 return MM_ERROR_INVALID_ARGUMENT;
194 proplist = pa_proplist_new();
196 if(channel_map == NULL) {
197 pa_channel_map_init_auto(&maps, ss->channels, PA_CHANNEL_MAP_ALSA);
203 sample_size = 1 * ss->channels;
205 case PA_SAMPLE_S16LE:
206 sample_size = 2 * ss->channels;
210 debug_error("Invalid sample size (%d)", sample_size);
214 /* Set volume type of stream */
215 if(volume_config > 0) {
216 debug_log("setting gain type");
217 prop_vol_type = 0; /* not used, set it system(0) temporarily */
219 /* Set gain type of stream */
220 prop_gain_type = (volume_config >> 8) & 0x000000FF;
222 pa_proplist_setf(proplist, PA_PROP_MEDIA_TIZEN_GAIN_TYPE, "%d", prop_gain_type);
225 if (stream_index != -1) {
226 char stream_index_s[11];
227 debug_msg("Set stream index [%d]", stream_index);
229 snprintf(stream_index_s, sizeof(stream_index_s)-1, "%d", stream_index);
230 debug_msg("stream_index[%d] converted to string[%s]", stream_index, stream_index_s);
231 pa_proplist_sets(proplist, PA_PROP_MEDIA_PARENT_ID, stream_index_s);
233 /* Set stream type */
234 pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, stream_type);
236 memset(&attr, '\0', sizeof(attr));
238 switch (handle_mode) {
239 case HANDLE_MODE_INPUT:
240 period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
241 samples_per_period = (ss->rate * period_time) / 1000;
242 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
247 attr.fragsize = samples_per_period * pa_sample_size(ss);
249 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "CAPTURE", ss, channel_map, &attr, proplist, &err);
252 case HANDLE_MODE_INPUT_LOW_LATENCY:
253 period_time = PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC;
254 samples_per_period = (ss->rate * period_time) / 1000;
255 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE;
260 attr.fragsize = samples_per_period * pa_sample_size(ss);
262 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "LOW LATENCY CAPTURE", ss, channel_map, &attr, proplist, &err);
265 case HANDLE_MODE_INPUT_HIGH_LATENCY:
266 period_time = PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC;
267 samples_per_period = (ss->rate * period_time) / 1000;
268 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE;
273 attr.fragsize = samples_per_period * pa_sample_size(ss);
275 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "HIGH LATENCY CAPTURE", ss, channel_map, &attr, proplist, &err);
278 case HANDLE_MODE_OUTPUT:
279 period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
280 samples_per_period = (ss->rate * period_time) / 1000;
281 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
284 attr.tlength = (ss->rate / 10) * pa_sample_size(ss) * ss->channels;
288 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "PLAYBACK", ss, channel_map, &attr, proplist, &err);
291 case HANDLE_MODE_OUTPUT_LOW_LATENCY:
292 period_time = PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC;
293 samples_per_period = (ss->rate * period_time) / 1000;
294 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE;
295 attr.prebuf = (ss->rate / 100) * pa_sample_size(ss) * ss->channels;
297 attr.tlength = (ss->rate / 10) * pa_sample_size(ss) * ss->channels;
300 debug_msg("rate(%d), samplesize(%d), ch(%d) format(%d)", ss->rate, pa_sample_size(ss), ss->channels, ss->format);
302 debug_msg("prebuf(%d), minreq(%d), tlength(%d), maxlength(%d), fragsize(%d)", attr.prebuf, attr.minreq, attr.tlength, attr.maxlength, attr.fragsize);
304 s = pa_simple_new_proplist(NULL,"MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
307 case HANDLE_MODE_OUTPUT_CLOCK:
308 period_time = PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC;
309 samples_per_period = (ss->rate * period_time) / 1000;
310 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK;
313 attr.tlength = (uint32_t)-1;
317 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "HIGH LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
320 case HANDLE_MODE_OUTPUT_VIDEO: /* low latency playback */
321 period_time = PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC;
322 samples_per_period = (ss->rate * period_time) / 1000;
323 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO;
324 attr.prebuf = 4*(samples_per_period * pa_sample_size(ss));
325 attr.minreq = samples_per_period * pa_sample_size(ss);
326 attr.tlength = periods_per_buffer * samples_per_period * pa_sample_size(ss);
330 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
333 case HANDLE_MODE_OUTPUT_AP_CALL:
334 #if defined(_MMFW_I386_ALL_SIMULATOR)
335 debug_msg("Does not support AP call mode at i386 simulator\n");
338 period_time = PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC;
339 samples_per_period = (ss->rate * period_time) / 1000;
340 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VOIP;
342 attr.minreq = pa_usec_to_bytes(20*PA_USEC_PER_MSEC, ss);
343 attr.tlength = pa_usec_to_bytes(100*PA_USEC_PER_MSEC, ss);
347 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "VoIP PLAYBACK", ss, channel_map, &attr, proplist, &err);
350 case HANDLE_MODE_INPUT_AP_CALL:
351 #if defined(_MMFW_I386_ALL_SIMULATOR)
352 debug_msg("Does not support AP call mode at i386 simulator\n");
355 period_time = PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC;
356 samples_per_period = (ss->rate * period_time) / 1000;
357 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VOIP;
362 attr.fragsize = samples_per_period * pa_sample_size(ss);
364 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "VoIP CAPTURE", ss, channel_map, &attr, proplist, &err);
368 err = MM_ERROR_SOUND_INTERNAL;
374 debug_error("Open pulseaudio handle has failed - %s\n", pa_strerror(err));
375 if (!strncmp(pa_strerror(err), "Access denied by security check",strlen(pa_strerror(err)))) {
376 err = MM_ERROR_SOUND_PERMISSION_DENIED;
378 err = MM_ERROR_SOUND_INTERNAL;
383 handle = (mm_sound_handle_t*)malloc(sizeof(mm_sound_handle_t));
384 memset(handle, 0, sizeof(mm_sound_handle_t));
386 handle->volume_type = prop_vol_type;
387 handle->gain_type = prop_gain_type;
388 handle->rate = ss->rate;
389 handle->channels = ss->channels;
391 handle->period = samples_per_period * sample_size;
392 *size = handle->period;
393 handle->handle = mm_sound_handle_mgr.handle_count;
394 ATOMIC_INC(&mm_sound_handle_mgr.lock, mm_sound_handle_mgr.handle_count); // 0 is not used
396 if (0 > pa_simple_get_stream_index(s, &handle->stream_idx, &err)) {
397 debug_msg("Can not get stream index %s\n", pa_strerror(err));
398 err = MM_ERROR_SOUND_INTERNAL;
401 mm_sound_handle_mgr.handles = g_list_append(mm_sound_handle_mgr.handles, handle);
403 if(handle->handle == 0) {
404 debug_msg("out of range. handle(%d)\n", handle->handle);
408 debug_msg("created handle[%d]. mode(%d), volumetype(%d), gain(%d), rate(%d), channels(%d), format(%d), stream_idx(%d), s(%x), source_type(%d)",
409 handle->handle, handle->mode, handle->volume_type, handle->gain_type,
410 handle->rate, handle->channels, ss->format, handle->stream_idx, handle->s, handle->source_type);
413 pa_proplist_free(proplist);
415 return handle->handle;
419 pa_proplist_free(proplist);
429 int mm_sound_pa_read(const int handle, void* buf, const int size)
431 mm_sound_handle_t* phandle = NULL;
432 int err = MM_ERROR_NONE;
434 #ifdef __STREAM_DEBUG__
435 debug_msg("handle(%d), buf(%p), size(%d)", handle, buf, size);
438 return MM_ERROR_INVALID_ARGUMENT;
441 return MM_ERROR_INVALID_ARGUMENT;
445 CHECK_HANDLE_RANGE(handle);
446 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
447 if(phandle == NULL) {
448 debug_msg("phandle is null");
449 return MM_ERROR_SOUND_INTERNAL;
452 if (0 > pa_simple_read(phandle->s, buf, size, &err)) {
453 debug_error("pa_simple_read() failed with %s", pa_strerror(err));
454 return MM_ERROR_SOUND_INTERNAL;
461 int mm_sound_pa_write(const int handle, void* buf, const int size)
463 mm_sound_handle_t* phandle = NULL;
464 int err = MM_ERROR_NONE;
467 return MM_ERROR_INVALID_ARGUMENT;
470 return MM_ERROR_INVALID_ARGUMENT;
472 return MM_ERROR_NONE;
474 CHECK_HANDLE_RANGE(handle);
475 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
477 #ifdef __STREAM_DEBUG__
478 debug_msg("phandle(%x) s(%x), handle(%d), rate(%d), ch(%d) stream_idx(%d), buf(%p), size(%d)",
479 phandle, phandle->s, phandle->handle, phandle->rate, phandle->channels, phandle->stream_idx. buf, size);
482 if(phandle == NULL) {
483 debug_msg("phandle is null");
484 return MM_ERROR_SOUND_INTERNAL;
487 if (0 > pa_simple_write(phandle->s, buf, size, &err)) {
488 debug_error("pa_simple_write() failed with %s\n", pa_strerror(err));
489 return MM_ERROR_SOUND_INTERNAL;
497 int mm_sound_pa_close(const int handle)
499 mm_sound_handle_t* phandle = NULL;
500 int err = MM_ERROR_NONE;
502 CHECK_HANDLE_RANGE(handle);
503 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
504 if(phandle == NULL) {
505 debug_msg("phandle is null");
506 return MM_ERROR_SOUND_INTERNAL;
509 debug_msg("phandle(%x) s(%x), handle(%d), rate(%d), ch(%d) stream_idx(%d)",
510 phandle, phandle->s, phandle->handle, phandle->rate, phandle->channels, phandle->stream_idx);
512 switch (phandle->mode) {
513 case HANDLE_MODE_OUTPUT:
514 case HANDLE_MODE_OUTPUT_CLOCK:
515 case HANDLE_MODE_OUTPUT_LOW_LATENCY:
516 case HANDLE_MODE_OUTPUT_AP_CALL:
517 case HANDLE_MODE_OUTPUT_VIDEO:
518 if (0 > pa_simple_flush(phandle->s, &err)) {
519 err = MM_ERROR_SOUND_INTERNAL;
520 debug_msg("pa_simple_flush() failed with %s\n", pa_strerror(err));
527 pa_simple_free(phandle->s);
530 debug_msg("leave: handle[%d] stream_index[%d]\n", handle, phandle->stream_idx);
532 mm_sound_handle_mgr.handles = g_list_remove(mm_sound_handle_mgr.handles, phandle);
533 if(phandle != NULL) {
543 int mm_sound_pa_drain(const int handle)
545 mm_sound_handle_t* phandle = NULL;
546 int err = MM_ERROR_NONE;
548 CHECK_HANDLE_RANGE(handle);
549 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
551 return MM_ERROR_SOUND_INTERNAL;
553 if (0 > pa_simple_drain(phandle->s, &err)) {
554 debug_error("pa_simple_drain() failed with %s\n", pa_strerror(err));
555 err = MM_ERROR_SOUND_INTERNAL;
562 int mm_sound_pa_flush(const int handle)
564 mm_sound_handle_t* phandle = NULL;
565 int err = MM_ERROR_NONE;
567 CHECK_HANDLE_RANGE(handle);
568 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
570 return MM_ERROR_SOUND_INTERNAL;
572 if (0 > pa_simple_flush(phandle->s, &err)) {
573 debug_error("pa_simple_flush() failed with %s\n", pa_strerror(err));
574 err = MM_ERROR_SOUND_INTERNAL;
580 typedef struct _get_volume_max_userdata_t
582 pa_threaded_mainloop* mainloop;
584 } get_volume_max_userdata_t;