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.
36 #include <mm_session.h>
37 #include <mm_session_private.h>
38 #include <mm_sound_pa_client.h>
41 #include <pulse/simple.h>
42 #include <pulse/proplist.h>
44 #include "include/mm_sound.h"
45 #include "include/mm_sound_private.h"
46 #include "include/mm_sound_utils.h"
47 #include "include/mm_sound_common.h"
49 #define MM_SOUND_CHANNEL_MIN 1
50 #define MM_SOUND_CHANNEL_MAX 6
52 typedef struct _mm_sound_handle_t {
63 int period; /* open api retrun value.*/
65 unsigned int stream_idx;
69 #define MM_SOUND_HANDLE_MAX 32
71 uint32_t handle_count; /* use amotic operations */
76 pa_threaded_mainloop *mainloop;
78 } mm_sound_handle_mgr;
80 #define CHECK_HANDLE_RANGE(x) \
83 debug_msg("invalid handle(%d)", x); \
84 return MM_ERROR_INVALID_ARGUMENT; \
88 #define CHECK_VOLUME_TYPE_RANGE(x) \
90 if(x < VOLUME_TYPE_SYSTEM || x >= VOLUME_TYPE_MAX) { \
91 debug_msg("invalid volume type(%d)", x); \
92 return MM_ERROR_INVALID_ARGUMENT; \
96 #define ATOMIC_INC(l, x) \
98 pthread_mutex_lock(l); \
102 pthread_mutex_unlock(l); \
105 // phandle(ret), GList, userdata, coimpare func
106 #define GET_HANDLE_DATA(p, l, u, func) \
109 list = g_list_find_custom(l, u, func); \
111 p = (mm_sound_handle_t*)list->data; \
117 #define CHECK_CONNECT_TO_PULSEAUDIO() __mm_sound_pa_connect_to_pa()
119 // should be call after pa_ext function.
120 #define WAIT_PULSEAUDIO_OPERATION(x, y) \
122 while (pa_operation_get_state(y) == PA_OPERATION_RUNNING) { \
123 debug_msg("waiting.................."); \
124 pa_threaded_mainloop_wait(x.mainloop); \
125 debug_msg("waiting DONE"); \
129 #define PA_SIMPLE_FADE_INTERVAL_USEC 20000
131 #define PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT 1536 /* frames */
132 #define PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE 4
133 #define PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT 6
134 #define PA_SIMPLE_PERIODS_PER_BUFFER_VOIP 2
135 #define PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK 8
136 #define PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE 12
137 #define PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO 10
139 #define PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC 20
140 #define PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC 25
141 #define PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC 50
142 #define PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC 75
143 #define PA_SIMPLE_PERIOD_TIME_FOR_VERY_HIGH_LATENCY_MSEC 150
144 #define PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC 20
146 #define IS_INPUT_HANDLE(x) \
147 if( x == HANDLE_MODE_INPUT || x == HANDLE_MODE_INPUT_HIGH_LATENCY || \
148 x == HANDLE_MODE_INPUT_LOW_LATENCY || x == HANDLE_MODE_INPUT_AP_CALL )
150 __attribute__ ((constructor)) void __mm_sound_pa_init(void)
152 memset(&mm_sound_handle_mgr, 0, sizeof(mm_sound_handle_mgr));
153 mm_sound_handle_mgr.state = FALSE;
155 mm_sound_handle_mgr.handles = g_list_alloc();
156 mm_sound_handle_mgr.handle_count = 1;
157 pthread_mutex_init(&mm_sound_handle_mgr.lock, NULL);
160 __attribute__ ((destructor)) void __mm_sound_pa_deinit(void)
162 g_list_free(mm_sound_handle_mgr.handles);
163 pthread_mutex_destroy(&mm_sound_handle_mgr.lock);
164 mm_sound_handle_mgr.handle_count = 0;
166 if(mm_sound_handle_mgr.state) {
167 debug_msg("mainloop(%x), context(%x)", mm_sound_handle_mgr.mainloop, mm_sound_handle_mgr.context);
168 pa_threaded_mainloop_stop(mm_sound_handle_mgr.mainloop);
169 pa_context_disconnect(mm_sound_handle_mgr.context);
170 pa_context_unref(mm_sound_handle_mgr.context);
171 pa_threaded_mainloop_free(mm_sound_handle_mgr.mainloop);
175 gint __mm_sound_handle_comparefunc(gconstpointer a, gconstpointer b)
177 mm_sound_handle_t* phandle = (mm_sound_handle_t*)a;
178 int* handle = (int*)b;
183 if(phandle->handle == *handle)
190 int mm_sound_pa_open(MMSoundHandleMode mode, mm_sound_handle_route_info *route_info, MMSoundHandlePriority priority, int volume_config, pa_sample_spec* ss, pa_channel_map* channel_map, int* size, char *stream_type, int stream_index)
197 int prop_vol_type, prop_gain_type;
199 int err = MM_ERROR_SOUND_INTERNAL;
200 int period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
201 int samples_per_period = PA_SIMPLE_SAMPLES_PER_PERIOD_DEFAULT;
202 int periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
204 int handle_mode = mode;
205 int handle_inout = HANDLE_DIRECTION_NONE;
207 pa_proplist *proplist = NULL;
209 mm_sound_handle_t* handle = NULL;
210 MMSoundHandleRoutePolicy policy = HANDLE_ROUTE_POLICY_DEFAULT;
213 policy = route_info->policy;
216 if (ss->channels < MM_SOUND_CHANNEL_MIN || ss->channels > MM_SOUND_CHANNEL_MAX)
217 return MM_ERROR_INVALID_ARGUMENT;
219 proplist = pa_proplist_new();
221 if(channel_map == NULL) {
222 pa_channel_map_init_auto(&maps, ss->channels, PA_CHANNEL_MAP_ALSA);
228 sample_size = 1 * ss->channels;
230 case PA_SAMPLE_S16LE:
231 sample_size = 2 * ss->channels;
235 debug_error("Invalid sample size (%d)", sample_size);
239 /* Set volume type of stream */
240 if(volume_config > 0) {
241 debug_log("setting gain type");
242 prop_vol_type = 0; /* not used, set it system(0) temporarily */
244 /* Set gain type of stream */
245 prop_gain_type = (volume_config >> 8) & 0x000000FF;
247 pa_proplist_setf(proplist, PA_PROP_MEDIA_TIZEN_GAIN_TYPE, "%d", prop_gain_type);
250 if (stream_index != -1) {
251 char stream_index_s[11];
252 debug_msg("Set stream index [%d]", stream_index);
254 snprintf(stream_index_s, sizeof(stream_index_s)-1, "%d", stream_index);
255 debug_msg("stream_index[%d] converted to string[%s]", stream_index, stream_index_s);
256 pa_proplist_sets(proplist, PA_PROP_MEDIA_PARENT_ID, stream_index_s);
258 /* Set stream type */
259 pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, stream_type);
261 memset(&attr, '\0', sizeof(attr));
263 switch (handle_mode) {
264 case HANDLE_MODE_INPUT:
265 period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
266 samples_per_period = (ss->rate * period_time) / 1000;
267 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
272 attr.fragsize = samples_per_period * pa_sample_size(ss);
274 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "CAPTURE", ss, channel_map, &attr, proplist, &err);
277 case HANDLE_MODE_INPUT_LOW_LATENCY:
278 period_time = PA_SIMPLE_PERIOD_TIME_FOR_ULOW_LATENCY_MSEC;
279 samples_per_period = (ss->rate * period_time) / 1000;
280 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE;
285 attr.fragsize = samples_per_period * pa_sample_size(ss);
287 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "LOW LATENCY CAPTURE", ss, channel_map, &attr, proplist, &err);
290 case HANDLE_MODE_INPUT_HIGH_LATENCY:
291 period_time = PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC;
292 samples_per_period = (ss->rate * period_time) / 1000;
293 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_CAPTURE;
298 attr.fragsize = samples_per_period * pa_sample_size(ss);
300 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "HIGH LATENCY CAPTURE", ss, channel_map, &attr, proplist, &err);
303 case HANDLE_MODE_OUTPUT:
304 period_time = PA_SIMPLE_PERIOD_TIME_FOR_MID_LATENCY_MSEC;
305 samples_per_period = (ss->rate * period_time) / 1000;
306 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_DEFAULT;
309 attr.tlength = (ss->rate / 10) * pa_sample_size(ss) * ss->channels;
313 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "PLAYBACK", ss, channel_map, &attr, proplist, &err);
316 case HANDLE_MODE_OUTPUT_LOW_LATENCY:
317 period_time = PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC;
318 samples_per_period = (ss->rate * period_time) / 1000;
319 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_FASTMODE;
320 attr.prebuf = (ss->rate / 100) * pa_sample_size(ss) * ss->channels;
322 attr.tlength = (ss->rate / 10) * pa_sample_size(ss) * ss->channels;
325 debug_msg("rate(%d), samplesize(%d), ch(%d) format(%d)", ss->rate, pa_sample_size(ss), ss->channels, ss->format);
327 debug_msg("prebuf(%d), minreq(%d), tlength(%d), maxlength(%d), fragsize(%d)", attr.prebuf, attr.minreq, attr.tlength, attr.maxlength, attr.fragsize);
329 s = pa_simple_new_proplist(NULL,"MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
332 case HANDLE_MODE_OUTPUT_CLOCK:
333 period_time = PA_SIMPLE_PERIOD_TIME_FOR_HIGH_LATENCY_MSEC;
334 samples_per_period = (ss->rate * period_time) / 1000;
335 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_PLAYBACK;
338 attr.tlength = (uint32_t)-1;
342 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "HIGH LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
345 case HANDLE_MODE_OUTPUT_VIDEO: /* low latency playback */
346 period_time = PA_SIMPLE_PERIOD_TIME_FOR_LOW_LATENCY_MSEC;
347 samples_per_period = (ss->rate * period_time) / 1000;
348 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VIDEO;
349 attr.prebuf = 4*(samples_per_period * pa_sample_size(ss));
350 attr.minreq = samples_per_period * pa_sample_size(ss);
351 attr.tlength = periods_per_buffer * samples_per_period * pa_sample_size(ss);
355 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "LOW LATENCY PLAYBACK", ss, channel_map, &attr, proplist, &err);
358 case HANDLE_MODE_OUTPUT_AP_CALL:
359 #if defined(_MMFW_I386_ALL_SIMULATOR)
360 debug_msg("Does not support AP call mode at i386 simulator\n");
363 period_time = PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC;
364 samples_per_period = (ss->rate * period_time) / 1000;
365 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VOIP;
367 attr.minreq = pa_usec_to_bytes(20*PA_USEC_PER_MSEC, ss);
368 attr.tlength = pa_usec_to_bytes(100*PA_USEC_PER_MSEC, ss);
372 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_PLAYBACK, NULL, "VoIP PLAYBACK", ss, channel_map, &attr, proplist, &err);
375 case HANDLE_MODE_INPUT_AP_CALL:
376 #if defined(_MMFW_I386_ALL_SIMULATOR)
377 debug_msg("Does not support AP call mode at i386 simulator\n");
380 period_time = PA_SIMPLE_PERIOD_TIME_FOR_VOIP_LATENCY_MSEC;
381 samples_per_period = (ss->rate * period_time) / 1000;
382 periods_per_buffer = PA_SIMPLE_PERIODS_PER_BUFFER_VOIP;
387 attr.fragsize = samples_per_period * pa_sample_size(ss);
389 s = pa_simple_new_proplist(NULL, "MM_SOUND_PA_CLIENT", PA_STREAM_RECORD, NULL, "VoIP CAPTURE", ss, channel_map, &attr, proplist, &err);
392 case HANDLE_MODE_CALL_OUT:
393 case HANDLE_MODE_CALL_IN:
394 debug_error("Does not support call device handling\n");
398 return MM_ERROR_SOUND_INTERNAL;
404 debug_error("Open pulseaudio handle has failed - %s\n", pa_strerror(err));
405 if (!strncmp(pa_strerror(err), "Access denied by security check",strlen(pa_strerror(err)))) {
406 err = MM_ERROR_SOUND_PERMISSION_DENIED;
408 err = MM_ERROR_SOUND_INTERNAL;
413 handle = (mm_sound_handle_t*)malloc(sizeof(mm_sound_handle_t));
416 handle->policy = policy;
417 handle->volume_type = prop_vol_type;
418 handle->gain_type = prop_gain_type;
419 handle->rate = ss->rate;
420 handle->channels = ss->channels;
422 handle->period = samples_per_period * sample_size;
423 *size = handle->period;
424 handle->handle = mm_sound_handle_mgr.handle_count;
425 ATOMIC_INC(&mm_sound_handle_mgr.lock, mm_sound_handle_mgr.handle_count); // 0 is not used
427 if (0 > pa_simple_get_stream_index(s, &handle->stream_idx, &err)) {
428 debug_msg("Can not get stream index %s\n", pa_strerror(err));
429 err = MM_ERROR_SOUND_INTERNAL;
432 g_list_append(mm_sound_handle_mgr.handles, handle);
434 if(handle->handle == 0) {
435 debug_msg("out of range. handle(%d)\n", handle->handle);
439 debug_msg("created handle[%d]. mode(%d), policy(%d), volumetype(%d), gain(%d), rate(%d), channels(%d), format(%d), stream_idx(%d), s(%x), source_type(%d) handle_inout(%d)",
440 handle->handle, handle->mode, handle->policy, handle->volume_type, handle->gain_type,
441 handle->rate, handle->channels, ss->format, handle->stream_idx, handle->s, handle->source_type, handle_inout);
443 return handle->handle;
447 pa_proplist_free(proplist);
457 int mm_sound_pa_read(const int handle, void* buf, const int size)
459 mm_sound_handle_t* phandle = NULL;
460 int err = MM_ERROR_NONE;
462 #ifdef __STREAM_DEBUG__
463 debug_msg("handle(%d), buf(%p), size(%d)", handle, buf, size);
466 return MM_ERROR_INVALID_ARGUMENT;
469 return MM_ERROR_INVALID_ARGUMENT;
473 CHECK_HANDLE_RANGE(handle);
474 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
475 if(phandle == NULL) {
476 debug_msg("phandle is null");
477 return MM_ERROR_SOUND_INTERNAL;
480 if (0 > pa_simple_read(phandle->s, buf, size, &err)) {
481 debug_error("pa_simple_read() failed with %s", pa_strerror(err));
482 return MM_ERROR_SOUND_INTERNAL;
489 int mm_sound_pa_write(const int handle, void* buf, const int size)
491 mm_sound_handle_t* phandle = NULL;
492 int err = MM_ERROR_NONE;
495 return MM_ERROR_INVALID_ARGUMENT;
498 return MM_ERROR_INVALID_ARGUMENT;
500 return MM_ERROR_NONE;
502 CHECK_HANDLE_RANGE(handle);
503 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
505 #ifdef __STREAM_DEBUG__
506 debug_msg("phandle(%x) s(%x), handle(%d), rate(%d), ch(%d) stream_idx(%d), buf(%p), size(%d)",
507 phandle, phandle->s, phandle->handle, phandle->rate, phandle->channels, phandle->stream_idx. buf, size);
510 if(phandle == NULL) {
511 debug_msg("phandle is null");
512 return MM_ERROR_SOUND_INTERNAL;
515 if (0 > pa_simple_write(phandle->s, buf, size, &err)) {
516 debug_error("pa_simple_write() failed with %s\n", pa_strerror(err));
517 return MM_ERROR_SOUND_INTERNAL;
525 int mm_sound_pa_close(const int handle)
527 mm_sound_handle_t* phandle = NULL;
528 int err = MM_ERROR_NONE;
530 CHECK_HANDLE_RANGE(handle);
531 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
532 if(phandle == NULL) {
533 debug_msg("phandle is null");
534 return MM_ERROR_SOUND_INTERNAL;
537 debug_msg("phandle(%x) s(%x), handle(%d), rate(%d), ch(%d) stream_idx(%d)",
538 phandle, phandle->s, phandle->handle, phandle->rate, phandle->channels, phandle->stream_idx);
540 switch (phandle->mode) {
541 case HANDLE_MODE_OUTPUT:
542 case HANDLE_MODE_OUTPUT_CLOCK:
543 case HANDLE_MODE_OUTPUT_LOW_LATENCY:
544 case HANDLE_MODE_OUTPUT_AP_CALL:
545 case HANDLE_MODE_OUTPUT_VIDEO:
546 if (0 > pa_simple_flush(phandle->s, &err)) {
547 err = MM_ERROR_SOUND_INTERNAL;
548 debug_msg("pa_simple_flush() failed with %s\n", pa_strerror(err));
555 pa_simple_free(phandle->s);
558 debug_msg("leave: handle[%d] stream_index[%d]\n", handle, phandle->stream_idx);
560 g_list_remove(mm_sound_handle_mgr.handles, phandle);
561 if(phandle != NULL) {
571 int mm_sound_pa_cork(const int handle, const int cork)
573 mm_sound_handle_t* phandle = NULL;
574 int err = MM_ERROR_NONE;
576 CHECK_HANDLE_RANGE(handle);
577 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
578 if(phandle == NULL) {
579 debug_msg("phandle is null");
580 return MM_ERROR_SOUND_INTERNAL;
583 if (0 > pa_simple_cork(phandle->s, cork, &err)) {
584 debug_error("pa_simple_cork() failed with %s\n", pa_strerror(err));
585 err = MM_ERROR_SOUND_INTERNAL;
592 int mm_sound_pa_drain(const int handle)
594 mm_sound_handle_t* phandle = NULL;
595 int err = MM_ERROR_NONE;
597 CHECK_HANDLE_RANGE(handle);
598 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
600 return MM_ERROR_SOUND_INTERNAL;
602 if (0 > pa_simple_drain(phandle->s, &err)) {
603 debug_error("pa_simple_drain() failed with %s\n", pa_strerror(err));
604 err = MM_ERROR_SOUND_INTERNAL;
611 int mm_sound_pa_flush(const int handle)
613 mm_sound_handle_t* phandle = NULL;
614 int err = MM_ERROR_NONE;
616 CHECK_HANDLE_RANGE(handle);
617 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
619 return MM_ERROR_SOUND_INTERNAL;
621 if (0 > pa_simple_flush(phandle->s, &err)) {
622 debug_error("pa_simple_flush() failed with %s\n", pa_strerror(err));
623 err = MM_ERROR_SOUND_INTERNAL;
630 int mm_sound_pa_get_latency(const int handle, int* latency)
632 mm_sound_handle_t* phandle = NULL;
633 int err = MM_ERROR_NONE;
634 pa_usec_t latency_time = 0;
636 CHECK_HANDLE_RANGE(handle);
637 GET_HANDLE_DATA(phandle, mm_sound_handle_mgr.handles, &handle, __mm_sound_handle_comparefunc);
638 if(phandle == NULL) {
639 debug_msg("phandle is null");
640 return MM_ERROR_SOUND_INTERNAL;
643 latency_time = pa_simple_get_final_latency(phandle->s, &err);
644 if (err > 0 && latency_time == 0) {
645 debug_error("pa_simple_get_latency() failed with %s\n", pa_strerror(err));
646 err = MM_ERROR_SOUND_INTERNAL;
648 *latency = latency_time / 1000; // usec to msec
653 static void __mm_sound_pa_state_cb(pa_context *c, void *userdata)
655 pa_threaded_mainloop *mainloop = userdata;
657 switch (pa_context_get_state(c)) {
658 case PA_CONTEXT_READY:
659 pa_threaded_mainloop_signal(mainloop, 0);
661 case PA_CONTEXT_TERMINATED:
662 case PA_CONTEXT_FAILED:
663 pa_threaded_mainloop_signal(mainloop, 0);
666 case PA_CONTEXT_UNCONNECTED:
667 case PA_CONTEXT_CONNECTING:
668 case PA_CONTEXT_AUTHORIZING:
669 case PA_CONTEXT_SETTING_NAME:
674 static void __mm_sound_pa_connect_to_pa()
676 if(mm_sound_handle_mgr.state)
679 if (!(mm_sound_handle_mgr.mainloop = pa_threaded_mainloop_new())) {
680 debug_error("mainloop create failed");
683 if (!(mm_sound_handle_mgr.context = pa_context_new(pa_threaded_mainloop_get_api(mm_sound_handle_mgr.mainloop), NULL))) {
684 debug_error("context create failed");
687 pa_threaded_mainloop_lock(mm_sound_handle_mgr.mainloop);
688 pa_context_set_state_callback(mm_sound_handle_mgr.context, __mm_sound_pa_state_cb, (void *)mm_sound_handle_mgr.mainloop);
690 if(pa_threaded_mainloop_start(mm_sound_handle_mgr.mainloop) < 0) {
691 debug_error("mainloop start failed");
694 if (pa_context_connect(mm_sound_handle_mgr.context, NULL, 0, NULL) < 0) {
695 debug_error("context connect failed");
699 pa_context_state_t state = pa_context_get_state(mm_sound_handle_mgr.context);
701 if (!PA_CONTEXT_IS_GOOD (state)) {
705 if (state == PA_CONTEXT_READY) {
709 debug_msg("waiting..................");
710 pa_threaded_mainloop_wait(mm_sound_handle_mgr.mainloop);
711 debug_msg("waiting DONE. check again...");
714 mm_sound_handle_mgr.state = TRUE;
716 pa_threaded_mainloop_unlock(mm_sound_handle_mgr.mainloop);
720 static void __mm_sound_pa_success_cb(pa_context *c, int success, void *userdata)
722 pa_threaded_mainloop *mainloop = (pa_threaded_mainloop *)userdata;
725 debug_error("pa control failed: %s\n", pa_strerror(pa_context_errno(c)));
727 debug_msg("pa control success\n");
729 pa_threaded_mainloop_signal(mainloop, 0);
732 typedef struct _get_volume_max_userdata_t
734 pa_threaded_mainloop* mainloop;
736 } get_volume_max_userdata_t;
738 static void __mm_sound_pa_get_cb(pa_context *c, uint32_t value, void *userdata)
740 get_volume_max_userdata_t* u = (get_volume_max_userdata_t*)userdata;
747 pa_threaded_mainloop_signal(u->mainloop, 0);
751 int mm_sound_pa_corkall(int cork)
753 pa_operation *o = NULL;
755 CHECK_CONNECT_TO_PULSEAUDIO();
757 pa_threaded_mainloop_lock(mm_sound_handle_mgr.mainloop);
759 o = pa_context_set_cork_all(mm_sound_handle_mgr.context, cork, __mm_sound_pa_success_cb, (void *)mm_sound_handle_mgr.mainloop);
760 WAIT_PULSEAUDIO_OPERATION(mm_sound_handle_mgr, o);
763 pa_operation_unref(o);
765 pa_threaded_mainloop_unlock(mm_sound_handle_mgr.mainloop);
767 return MM_ERROR_NONE;