Init pulseaudio at startup
[profile/ivi/lemolo.git] / utils / pulseaudio.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include <Elementary.h>
5 #include <pulse/context.h>
6 #include <pulse/pulseaudio.h>
7 #include <pulse/glib-mainloop.h>
8
9 #include "pulseaudio.h"
10 #include "log.h"
11
12 static pa_glib_mainloop *mainloop = NULL;
13 static pa_context *pa_ctx = NULL;
14
15 Eina_Bool pa_init(void)
16 {
17         if (!mainloop)
18                 mainloop = pa_glib_mainloop_new(NULL);
19
20         pa_ctx = pa_context_new(pa_glib_mainloop_get_api(mainloop),
21                                  "lemolo");
22
23         // connects to the pulse server
24         if (pa_context_connect(pa_ctx,
25                                 NULL,
26                                 PA_CONTEXT_NOFAIL, NULL) < 0)
27         {
28                 ERR("Failed to connect to pulseaudio daemon");
29                 pa_glib_mainloop_free(mainloop);
30                 mainloop = NULL;
31                 return EINA_FALSE;
32         }
33
34         return EINA_TRUE;
35 }
36
37 void pa_shutdown(void)
38 {
39         if (pa_ctx) {
40                 pa_context_disconnect(pa_ctx);
41                 pa_context_unref(pa_ctx);
42         }
43
44         if (mainloop) {
45                 pa_glib_mainloop_free(mainloop);
46                 mainloop = NULL;
47         }
48 }