forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlcpulse.c
291 lines (253 loc) · 9.24 KB
/
vlcpulse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* \file vlcpulse.c
* \brief PulseAudio support library for LibVLC plugins
*/
/*****************************************************************************
* Copyright (C) 2009-2011 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <pulse/pulseaudio.h>
#include "audio_output/vlcpulse.h"
#include <assert.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <pwd.h>
const char vlc_module_name[] = "vlcpulse";
#undef vlc_pa_error
void vlc_pa_error (vlc_object_t *obj, const char *msg, pa_context *ctx)
{
msg_Err (obj, "%s: %s", msg, pa_strerror (pa_context_errno (ctx)));
}
static void context_state_cb (pa_context *ctx, void *userdata)
{
pa_threaded_mainloop *mainloop = userdata;
switch (pa_context_get_state(ctx))
{
case PA_CONTEXT_READY:
case PA_CONTEXT_FAILED:
case PA_CONTEXT_TERMINATED:
pa_threaded_mainloop_signal (mainloop, 0);
default:
break;
}
}
static bool context_wait (pa_context *ctx, pa_threaded_mainloop *mainloop)
{
pa_context_state_t state;
while ((state = pa_context_get_state (ctx)) != PA_CONTEXT_READY)
{
if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
return -1;
pa_threaded_mainloop_wait (mainloop);
}
return 0;
}
static void context_event_cb(pa_context *c, const char *name, pa_proplist *pl,
void *userdata)
{
vlc_object_t *obj = userdata;
msg_Warn (obj, "unhandled context event \"%s\"", name);
(void) c;
(void) pl;
}
/**
* Initializes the PulseAudio main loop and connects to the PulseAudio server.
* @return a PulseAudio context on success, or NULL on error
*/
pa_context *vlc_pa_connect (vlc_object_t *obj, pa_threaded_mainloop **mlp)
{
msg_Dbg (obj, "using library version %s", pa_get_library_version ());
msg_Dbg (obj, " (compiled with version %s, protocol %u)",
pa_get_headers_version (), PA_PROTOCOL_VERSION);
/* Initialize main loop */
pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new ();
if (unlikely(mainloop == NULL))
return NULL;
if (pa_threaded_mainloop_start (mainloop) < 0)
{
pa_threaded_mainloop_free (mainloop);
return NULL;
}
/* Fill in context (client) properties */
char *ua = var_InheritString (obj, "user-agent");
pa_proplist *props = pa_proplist_new ();
if (likely(props != NULL))
{
char *str;
if (ua != NULL)
pa_proplist_sets (props, PA_PROP_APPLICATION_NAME, ua);
str = var_InheritString (obj, "app-id");
if (str != NULL)
{
pa_proplist_sets (props, PA_PROP_APPLICATION_ID, str);
free (str);
}
str = var_InheritString (obj, "app-version");
if (str != NULL)
{
pa_proplist_sets (props, PA_PROP_APPLICATION_VERSION, str);
free (str);
}
str = var_InheritString (obj, "app-icon-name");
if (str != NULL)
{
pa_proplist_sets (props, PA_PROP_APPLICATION_ICON_NAME, str);
free (str);
}
//pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE, _("C"));
pa_proplist_sets (props, PA_PROP_APPLICATION_LANGUAGE,
setlocale (LC_MESSAGES, NULL));
pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_ID, "%lu",
(unsigned long) getpid ());
//pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_BINARY,
// PACKAGE_NAME);
for (size_t max = sysconf (_SC_GETPW_R_SIZE_MAX), len = max % 1024 + 1024;
len < max; len += 1024)
{
struct passwd pwbuf, *pw;
char buf[len];
if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0)
{
if (pw != NULL)
pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_USER,
pw->pw_name);
break;
}
}
for (size_t max = sysconf (_SC_HOST_NAME_MAX), len = max % 1024 + 1024;
len < max; len += 1024)
{
char hostname[len];
if (gethostname (hostname, sizeof (hostname)) == 0)
{
pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_HOST,
hostname);
break;
}
}
const char *session = getenv ("XDG_SESSION_COOKIE");
if (session != NULL)
{
pa_proplist_setf (props, PA_PROP_APPLICATION_PROCESS_MACHINE_ID,
"%.32s", session); /* XXX: is this valid? */
pa_proplist_sets (props, PA_PROP_APPLICATION_PROCESS_SESSION_ID,
session);
}
}
/* Connect to PulseAudio daemon */
pa_context *ctx;
pa_mainloop_api *api;
pa_threaded_mainloop_lock (mainloop);
api = pa_threaded_mainloop_get_api (mainloop);
ctx = pa_context_new_with_proplist (api, ua, props);
free (ua);
if (props != NULL)
pa_proplist_free (props);
if (unlikely(ctx == NULL))
goto fail;
pa_context_set_state_callback (ctx, context_state_cb, mainloop);
pa_context_set_event_callback (ctx, context_event_cb, obj);
if (pa_context_connect (ctx, NULL, 0, NULL) < 0
|| context_wait (ctx, mainloop))
{
vlc_pa_error (obj, "PulseAudio server connection failure", ctx);
pa_context_unref (ctx);
goto fail;
}
msg_Dbg (obj, "connected %s to %s as client #%"PRIu32,
pa_context_is_local (ctx) ? "locally" : "remotely",
pa_context_get_server (ctx), pa_context_get_index (ctx));
msg_Dbg (obj, "using protocol %"PRIu32", server protocol %"PRIu32,
pa_context_get_protocol_version (ctx),
pa_context_get_server_protocol_version (ctx));
pa_threaded_mainloop_unlock (mainloop);
*mlp = mainloop;
return ctx;
fail:
pa_threaded_mainloop_unlock (mainloop);
pa_threaded_mainloop_stop (mainloop);
pa_threaded_mainloop_free (mainloop);
return NULL;
}
/**
* Closes a connection to PulseAudio.
*/
void vlc_pa_disconnect (vlc_object_t *obj, pa_context *ctx,
pa_threaded_mainloop *mainloop)
{
pa_threaded_mainloop_lock (mainloop);
pa_context_disconnect (ctx);
pa_context_set_event_callback (ctx, NULL, NULL);
pa_context_set_state_callback (ctx, NULL, NULL);
pa_context_unref (ctx);
pa_threaded_mainloop_unlock (mainloop);
pa_threaded_mainloop_stop (mainloop);
pa_threaded_mainloop_free (mainloop);
(void) obj;
}
/**
* Frees a timer event.
* \note Timer events can be created with pa_context_rttime_new().
* \warning This function must be called from the mainloop,
* or with the mainloop lock held.
*/
void vlc_pa_rttime_free (pa_threaded_mainloop *mainloop, pa_time_event *e)
{
pa_mainloop_api *api = pa_threaded_mainloop_get_api (mainloop);
api->time_free (e);
}
#undef vlc_pa_get_latency
/**
* Gets latency of a PulseAudio stream.
* \return the latency or VLC_TS_INVALID on error.
*/
mtime_t vlc_pa_get_latency(vlc_object_t *obj, pa_context *ctx, pa_stream *s)
{
/* NOTE: pa_stream_get_latency() will report 0 rather than negative latency
* when the write index of a playback stream is behind its read index.
* playback streams. So use the lower-level pa_stream_get_timing_info()
* directly to obtain the correct write index and convert it to a time,
* and compute the correct latency value by subtracting the stream (read)
* time.
*
* On the read side, pa_stream_get_time() is used instead of
* pa_stream_get_timing_info() for the sake of interpolation. */
const pa_sample_spec *ss = pa_stream_get_sample_spec(s);
const pa_timing_info *ti = pa_stream_get_timing_info(s);
if (ti == NULL) {
msg_Dbg(obj, "no timing infos");
return VLC_TS_INVALID;
}
if (ti->write_index_corrupt) {
msg_Dbg(obj, "write index corrupt");
return VLC_TS_INVALID;
}
pa_usec_t wt = pa_bytes_to_usec((uint64_t)ti->write_index, ss);
pa_usec_t rt;
if (pa_stream_get_time(s, &rt)) {
if (pa_context_errno(ctx) != PA_ERR_NODATA)
vlc_pa_error(obj, "unknown time", ctx);
return VLC_TS_INVALID;
}
union { uint64_t u; int64_t s; } d;
d.u = wt - rt;
return d.s; /* non-overflowing unsigned to signed conversion */
}