Debian Codegraph @

pulse/pulse.c

raw symbols SCIP
1 /*-*- linux-c -*-*/
2
3 /*
4 * ALSA <-> PulseAudio plugins
5 *
6 * Copyright (c) 2006 by Pierre Ossman <[email protected]>
7 *
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/poll.h>
27
28 #include "pulse.h"
29
30 int pulse_check_connection(snd_pulse_t * p)
31 {
32 pa_context_state_t state;
33
34 assert(p);
35
36 if (!p->context || !p->mainloop)
37 return -EBADFD;
38
39 state = pa_context_get_state(p->context);
40
41 if (!PA_CONTEXT_IS_GOOD(state))
42 return -EIO;
43
44 return 0;
45 }
46
47 void pulse_context_success_cb(pa_context * c, int success, void *userdata)
48 {
49 snd_pulse_t *p = userdata;
50
51 assert(c);
52 assert(p);
53
54 pa_threaded_mainloop_signal(p->mainloop, 0);
55 }
56
57 int pulse_wait_operation(snd_pulse_t * p, pa_operation * o)
58 {
59 assert(p);
60 assert(o);
61
62 for (;;) {
63 int err;
64
65 err = pulse_check_connection(p);
66 if (err < 0)
67 return err;
68
69 if (pa_operation_get_state(o) != PA_OPERATION_RUNNING)
70 break;
71
72 pa_threaded_mainloop_wait(p->mainloop);
73 }
74
75 return 0;
76 }
77
78 static void context_state_cb(pa_context * c, void *userdata)
79 {
80 pa_context_state_t state;
81 snd_pulse_t *p = userdata;
82 assert(c);
83
84 state = pa_context_get_state(c);
85
86 /* When we get disconnected, tell the process */
87 if (!PA_CONTEXT_IS_GOOD(state))
88 pulse_poll_activate(p);
89
90 switch (state) {
91 case PA_CONTEXT_READY:
92 case PA_CONTEXT_TERMINATED:
93 case PA_CONTEXT_FAILED:
94 pa_threaded_mainloop_signal(p->mainloop, 0);
95 break;
96
97 case PA_CONTEXT_UNCONNECTED:
98 case PA_CONTEXT_CONNECTING:
99 case PA_CONTEXT_AUTHORIZING:
100 case PA_CONTEXT_SETTING_NAME:
101 break;
102 }
103 }
104
105 static int make_nonblock(int fd) {
106 int fl;
107
108 if ((fl = fcntl(fd, F_GETFL)) < 0)
109 return fl;
110
111 if (fl & O_NONBLOCK)
112 return 0;
113
114 return fcntl(fd, F_SETFL, fl | O_NONBLOCK);
115 }
116
117 static int make_close_on_exec(int fd)
118 {
119 return fcntl(fd, F_SETFD, FD_CLOEXEC);
120 }
121
122 snd_pulse_t *pulse_new(void)
123 {
124 snd_pulse_t *p;
125 int fd[2] = { -1, -1 };
126 char proc[PATH_MAX], buf[PATH_MAX + 20];
127
128 p = calloc(1, sizeof(snd_pulse_t));
129
130 if (!p)
131 return NULL;
132
133 if (pipe(fd)) {
134 free(p);
135 return NULL;
136 }
137
138 p->main_fd = fd[0];
139 p->thread_fd = fd[1];
140
141 make_nonblock(p->main_fd);
142 make_close_on_exec(p->main_fd);
143 make_nonblock(p->thread_fd);
144 make_close_on_exec(p->thread_fd);
145
146 p->mainloop = pa_threaded_mainloop_new();
147 if (!p->mainloop)
148 goto fail;
149
150 if (pa_get_binary_name(proc, sizeof(proc)))
151 snprintf(buf, sizeof(buf), "ALSA plug-in [%s]",
152 pa_path_get_filename(proc));
153 else
154 snprintf(buf, sizeof(buf), "ALSA plug-in");
155 buf[sizeof(buf)-1] = 0;
156
157 p->context =
158 pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), buf);
159
160 if (!p->context)
161 goto fail;
162
163 pa_context_set_state_callback(p->context, context_state_cb, p);
164
165 if (pa_threaded_mainloop_start(p->mainloop) < 0)
166 goto fail;
167
168 return p;
169
170 fail:
171 pulse_free(p);
172
173 return NULL;
174 }
175
176 void pulse_free(snd_pulse_t * p)
177 {
178 if (p->mainloop)
179 pa_threaded_mainloop_stop(p->mainloop);
180
181 if (p->context) {
182 pa_context_disconnect(p->context);
183 pa_context_unref(p->context);
184 }
185
186 if (p->mainloop)
187 pa_threaded_mainloop_free(p->mainloop);
188
189 if (p->thread_fd >= 0)
190 close(p->thread_fd);
191
192 if (p->main_fd >= 0)
193 close(p->main_fd);
194
195 free(p);
196 }
197
198 int pulse_connect(snd_pulse_t * p, const char *server, int can_fallback)
199 {
200 int err;
201 pa_context_flags_t flags;
202 pa_context_state_t state;
203
204 assert(p);
205
206 if (can_fallback)
207 flags = PA_CONTEXT_NOAUTOSPAWN;
208 else
209 flags = 0;
210
211 if (!p->context || !p->mainloop)
212 return -EBADFD;
213
214 state = pa_context_get_state(p->context);
215 if (state != PA_CONTEXT_UNCONNECTED)
216 return -EBADFD;
217
218 pa_threaded_mainloop_lock(p->mainloop);
219
220 err = pa_context_connect(p->context, server, flags, NULL);
221 if (err < 0)
222 goto error;
223
224 for (;;) {
225 pa_context_state_t state = pa_context_get_state(p->context);
226
227 if (!PA_CONTEXT_IS_GOOD(state))
228 goto error;
229
230 if (state == PA_CONTEXT_READY)
231 break;
232
233 pa_threaded_mainloop_wait(p->mainloop);
234 }
235
236 pa_threaded_mainloop_unlock(p->mainloop);
237
238 return 0;
239
240 error:
241 if (!can_fallback)
242 SNDERR("PulseAudio: Unable to connect: %s\n",
243 pa_strerror(pa_context_errno(p->context)));
244
245 pa_threaded_mainloop_unlock(p->mainloop);
246
247 return -ECONNREFUSED;
248 }
249
250 void pulse_poll_activate(snd_pulse_t * p)
251 {
252 static const char x = 'x';
253 assert(p);
254
255 write(p->thread_fd, &x, 1);
256 }
257
258 void pulse_poll_deactivate(snd_pulse_t * p)
259 {
260 char buf[10];
261
262 assert(p);
263
264 /* Drain the pipe */
265 while (read(p->main_fd, buf, sizeof(buf)) > 0);
266 }