]> git.sesse.net Git - vlc/blob - modules/audio_output/vlcpulse.c
Remove mingw-w64 2.0 workarounds
[vlc] / modules / audio_output / vlcpulse.c
1 /**
2  * \file vlcpulse.c
3  * \brief PulseAudio support library for LibVLC plugins
4  */
5 /*****************************************************************************
6  * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <vlc_common.h>
28 #include <pulse/pulseaudio.h>
29
30 #include "vlcpulse.h"
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <locale.h>
34 #include <unistd.h>
35 #include <pwd.h>
36
37 #undef vlc_pa_error
38 void vlc_pa_error (vlc_object_t *obj, const char *msg, pa_context *ctx)
39 {
40     msg_Err (obj, "%s: %s", msg, pa_strerror (pa_context_errno (ctx)));
41 }
42
43 static void context_state_cb (pa_context *ctx, void *userdata)
44 {
45     pa_threaded_mainloop *mainloop = userdata;
46
47     switch (pa_context_get_state(ctx))
48     {
49         case PA_CONTEXT_READY:
50         case PA_CONTEXT_FAILED:
51         case PA_CONTEXT_TERMINATED:
52             pa_threaded_mainloop_signal (mainloop, 0);
53         default:
54             break;
55     }
56 }
57
58 static bool context_wait (pa_context *ctx, pa_threaded_mainloop *mainloop)
59 {
60     pa_context_state_t state;
61
62     while ((state = pa_context_get_state (ctx)) != PA_CONTEXT_READY)
63     {
64         if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
65             return -1;
66         pa_threaded_mainloop_wait (mainloop);
67     }
68     return 0;
69 }
70
71 /**
72  * Initializes the PulseAudio main loop and connects to the PulseAudio server.
73  * @return a PulseAudio context on success, or NULL on error
74  */
75 pa_context *vlc_pa_connect (vlc_object_t *obj, pa_threaded_mainloop **mlp)
76 {
77     msg_Dbg (obj, "using library version %s", pa_get_library_version ());
78     msg_Dbg (obj, " (compiled with version %s, protocol %u)",
79              pa_get_headers_version (), PA_PROTOCOL_VERSION);
80
81     /* Initialize main loop */
82     pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new ();
83     if (unlikely(mainloop == NULL))
84         return NULL;
85
86     if (pa_threaded_mainloop_start (mainloop) < 0)
87     {
88         pa_threaded_mainloop_free (mainloop);
89         return NULL;
90     }
91
92     /* Fill in context (client) properties */
93     char *ua = var_InheritString (obj, "user-agent");
94     pa_proplist *props = pa_proplist_new ();
95     if (likely(props != NULL))
96     {
97         pa_proplist_sets (props, PA_PROP_APPLICATION_NAME, ua);
98         pa_proplist_sets (props, PA_PROP_APPLICATION_ID, "org.VideoLAN.VLC");
99         pa_proplist_sets (props, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION);
100         pa_proplist_sets (props, PA_PROP_APPLICATION_ICON_NAME, PACKAGE_NAME);
101         //pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE, _("C"));
102         pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE,
103                           setlocale (LC_MESSAGES, NULL));
104
105         pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_ID, "%lu",
106                           (unsigned long) getpid ());
107         //pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_BINARY,
108         //                  PACKAGE_NAME);
109
110         char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
111         struct passwd pwbuf, *pw;
112
113         if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0
114          && pw != NULL)
115             pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_USER,
116                               pw->pw_name);
117
118         char hostname[sysconf (_SC_HOST_NAME_MAX)];
119         if (gethostname (hostname, sizeof (hostname)) == 0)
120             pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_HOST,
121                               hostname);
122
123         const char *session = getenv ("XDG_SESSION_COOKIE");
124         if (session != NULL)
125         {
126             pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_MACHINE_ID,
127                               "%.32s", session); /* XXX: is this valid? */
128             pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_SESSION_ID,
129                               session);
130         }
131     }
132
133     /* Connect to PulseAudio daemon */
134     pa_context *ctx;
135     pa_mainloop_api *api;
136
137     pa_threaded_mainloop_lock (mainloop);
138     api = pa_threaded_mainloop_get_api (mainloop);
139     ctx = pa_context_new_with_proplist (api, ua, props);
140     free (ua);
141     if (props != NULL)
142         pa_proplist_free (props);
143     if (unlikely(ctx == NULL))
144         goto fail;
145
146     pa_context_set_state_callback (ctx, context_state_cb, mainloop);
147     if (pa_context_connect (ctx, NULL, 0, NULL) < 0
148      || context_wait (ctx, mainloop))
149     {
150         vlc_pa_error (obj, "PulseAudio server connection failure", ctx);
151         pa_context_unref (ctx);
152         goto fail;
153     }
154     msg_Dbg (obj, "connected %s to %s as client #%"PRIu32,
155              pa_context_is_local (ctx) ? "locally" : "remotely",
156              pa_context_get_server (ctx), pa_context_get_index (ctx));
157     msg_Dbg (obj, "using protocol %"PRIu32", server protocol %"PRIu32,
158              pa_context_get_protocol_version (ctx),
159              pa_context_get_server_protocol_version (ctx));
160
161     pa_threaded_mainloop_unlock (mainloop);
162     *mlp = mainloop;
163     return ctx;
164
165 fail:
166     pa_threaded_mainloop_unlock (mainloop);
167     pa_threaded_mainloop_stop (mainloop);
168     pa_threaded_mainloop_free (mainloop);
169     return NULL;
170 }
171
172 /**
173  * Closes a connection to PulseAudio.
174  */
175 void vlc_pa_disconnect (vlc_object_t *obj, pa_context *ctx,
176                         pa_threaded_mainloop *mainloop)
177 {
178     pa_threaded_mainloop_lock (mainloop);
179     pa_context_disconnect (ctx);
180     pa_context_set_state_callback (ctx, NULL, NULL);
181     pa_context_unref (ctx);
182     pa_threaded_mainloop_unlock (mainloop);
183
184     pa_threaded_mainloop_stop (mainloop);
185     pa_threaded_mainloop_free (mainloop);
186     (void) obj;
187 }
188
189 /**
190  * Frees a timer event.
191  * \note Timer events can be created with pa_context_rttime_new().
192  * \warning This function must be called from the mainloop,
193  * or with the mainloop lock held.
194  */
195 void vlc_pa_rttime_free (pa_threaded_mainloop *mainloop, pa_time_event *e)
196 {
197     pa_mainloop_api *api = pa_threaded_mainloop_get_api (mainloop);
198
199     api->time_free (e);
200 }
201
202 #undef vlc_pa_get_latency
203 /**
204  * Gets latency of a PulseAudio stream.
205  * \return the latency or VLC_TS_INVALID on error.
206  */
207 mtime_t vlc_pa_get_latency(vlc_object_t *obj, pa_context *ctx, pa_stream *s)
208 {
209     pa_usec_t latency;
210     int negative;
211
212     if (pa_stream_get_latency(s, &latency, &negative)) {
213         if (pa_context_errno (ctx) != PA_ERR_NODATA)
214             vlc_pa_error(obj, "unknown latency", ctx);
215         return VLC_TS_INVALID;
216     }
217     return negative ? -latency : +latency;
218 }