3 * \brief PulseAudio support library for LibVLC plugins
5 /*****************************************************************************
6 * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
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.
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.
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 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <pulse/pulseaudio.h>
38 void vlc_pa_error (vlc_object_t *obj, const char *msg, pa_context *ctx)
40 msg_Err (obj, "%s: %s", msg, pa_strerror (pa_context_errno (ctx)));
43 static void context_state_cb (pa_context *ctx, void *userdata)
45 pa_threaded_mainloop *mainloop = userdata;
47 switch (pa_context_get_state(ctx))
49 case PA_CONTEXT_READY:
50 case PA_CONTEXT_FAILED:
51 case PA_CONTEXT_TERMINATED:
52 pa_threaded_mainloop_signal (mainloop, 0);
58 static bool context_wait (pa_context *ctx, pa_threaded_mainloop *mainloop)
60 pa_context_state_t state;
62 while ((state = pa_context_get_state (ctx)) != PA_CONTEXT_READY)
64 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
66 pa_threaded_mainloop_wait (mainloop);
71 static void context_event_cb(pa_context *c, const char *name, pa_proplist *pl,
74 vlc_object_t *obj = userdata;
76 msg_Warn (obj, "unhandled context event \"%s\"", name);
82 * Initializes the PulseAudio main loop and connects to the PulseAudio server.
83 * @return a PulseAudio context on success, or NULL on error
85 pa_context *vlc_pa_connect (vlc_object_t *obj, pa_threaded_mainloop **mlp)
87 msg_Dbg (obj, "using library version %s", pa_get_library_version ());
88 msg_Dbg (obj, " (compiled with version %s, protocol %u)",
89 pa_get_headers_version (), PA_PROTOCOL_VERSION);
91 /* Initialize main loop */
92 pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new ();
93 if (unlikely(mainloop == NULL))
96 if (pa_threaded_mainloop_start (mainloop) < 0)
98 pa_threaded_mainloop_free (mainloop);
102 /* Fill in context (client) properties */
103 char *ua = var_InheritString (obj, "user-agent");
104 pa_proplist *props = pa_proplist_new ();
105 if (likely(props != NULL))
107 pa_proplist_sets (props, PA_PROP_APPLICATION_NAME, ua);
108 pa_proplist_sets (props, PA_PROP_APPLICATION_ID, "org.VideoLAN.VLC");
109 pa_proplist_sets (props, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION);
110 pa_proplist_sets (props, PA_PROP_APPLICATION_ICON_NAME, PACKAGE_NAME);
111 //pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE, _("C"));
112 pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE,
113 setlocale (LC_MESSAGES, NULL));
115 pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_ID, "%lu",
116 (unsigned long) getpid ());
117 //pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_BINARY,
120 for (size_t max = sysconf (_SC_GETPW_R_SIZE_MAX), len = max % 1024 + 1024;
121 len < max; len += 1024)
123 struct passwd pwbuf, *pw;
126 if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0
128 pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_USER,
132 for (size_t max = sysconf (_SC_HOST_NAME_MAX), len = max % 1024 + 1024;
133 len < max; len += 1024)
137 if (gethostname (hostname, sizeof (hostname)) == 0)
138 pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_HOST,
142 const char *session = getenv ("XDG_SESSION_COOKIE");
145 pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_MACHINE_ID,
146 "%.32s", session); /* XXX: is this valid? */
147 pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_SESSION_ID,
152 /* Connect to PulseAudio daemon */
154 pa_mainloop_api *api;
156 pa_threaded_mainloop_lock (mainloop);
157 api = pa_threaded_mainloop_get_api (mainloop);
158 ctx = pa_context_new_with_proplist (api, ua, props);
161 pa_proplist_free (props);
162 if (unlikely(ctx == NULL))
165 pa_context_set_state_callback (ctx, context_state_cb, mainloop);
166 pa_context_set_event_callback (ctx, context_event_cb, obj);
167 if (pa_context_connect (ctx, NULL, 0, NULL) < 0
168 || context_wait (ctx, mainloop))
170 vlc_pa_error (obj, "PulseAudio server connection failure", ctx);
171 pa_context_unref (ctx);
174 msg_Dbg (obj, "connected %s to %s as client #%"PRIu32,
175 pa_context_is_local (ctx) ? "locally" : "remotely",
176 pa_context_get_server (ctx), pa_context_get_index (ctx));
177 msg_Dbg (obj, "using protocol %"PRIu32", server protocol %"PRIu32,
178 pa_context_get_protocol_version (ctx),
179 pa_context_get_server_protocol_version (ctx));
181 pa_threaded_mainloop_unlock (mainloop);
186 pa_threaded_mainloop_unlock (mainloop);
187 pa_threaded_mainloop_stop (mainloop);
188 pa_threaded_mainloop_free (mainloop);
193 * Closes a connection to PulseAudio.
195 void vlc_pa_disconnect (vlc_object_t *obj, pa_context *ctx,
196 pa_threaded_mainloop *mainloop)
198 pa_threaded_mainloop_lock (mainloop);
199 pa_context_disconnect (ctx);
200 pa_context_set_event_callback (ctx, NULL, NULL);
201 pa_context_set_state_callback (ctx, NULL, NULL);
202 pa_context_unref (ctx);
203 pa_threaded_mainloop_unlock (mainloop);
205 pa_threaded_mainloop_stop (mainloop);
206 pa_threaded_mainloop_free (mainloop);
211 * Frees a timer event.
212 * \note Timer events can be created with pa_context_rttime_new().
213 * \warning This function must be called from the mainloop,
214 * or with the mainloop lock held.
216 void vlc_pa_rttime_free (pa_threaded_mainloop *mainloop, pa_time_event *e)
218 pa_mainloop_api *api = pa_threaded_mainloop_get_api (mainloop);
223 #undef vlc_pa_get_latency
225 * Gets latency of a PulseAudio stream.
226 * \return the latency or VLC_TS_INVALID on error.
228 mtime_t vlc_pa_get_latency(vlc_object_t *obj, pa_context *ctx, pa_stream *s)
233 if (pa_stream_get_latency(s, &latency, &negative)) {
234 if (pa_context_errno (ctx) != PA_ERR_NODATA)
235 vlc_pa_error(obj, "unknown latency", ctx);
236 return VLC_TS_INVALID;
238 return negative ? -latency : +latency;