Debian Codegraph @

src/libout123/modules/pulse.c

raw
1 /*
2 pulse: audio output using PulseAudio server
3
4 copyright 2006-2016 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Nicholas J. Humfrey
7 */
8
9 #include "../out123_int.h"
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <math.h>
14
15 // Using simple API for playback, but now also async API for enumeration.
16 // Maybe we should switch to the latter for playback, too, let alone for
17 // PA_CONTEXT_NOAUTOSPAWN. Or we hack something only for probing.
18 #include <pulse/simple.h>
19 #include <pulse/error.h>
20 #include <pulse/pulseaudio.h>
21
22 #include "../../common/debug.h"
23
24 // Since we learned the async API for enumeration, let's abuse it for
25 // a simple check if a pulse server is active before accidentally
26 // starting one.
27
28 static void state_callback(pa_context *c, void *userdata)
29 {
30 pa_context_state_t state;
31 int *pa_ready = userdata;
32
33 state = pa_context_get_state(c);
34 switch(state)
35 {
36 case PA_CONTEXT_FAILED:
37 case PA_CONTEXT_TERMINATED:
38 *pa_ready = 2;
39 break;
40 case PA_CONTEXT_READY:
41 *pa_ready = 1;
42 break;
43 default: break; // Make -Wall happy.
44 }
45 }
46
47 static int check_for_server()
48 {
49 pa_mainloop *pa_ml;
50 pa_mainloop_api *pa_mlapi;
51 pa_context *pa_ctx;
52 int ret = 0;
53 int pa_ready = 0;
54
55 pa_ml = pa_mainloop_new();
56 pa_mlapi = pa_mainloop_get_api(pa_ml);
57 pa_ctx = pa_context_new(pa_mlapi, "out123 server check");
58 if(!pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL))
59 {
60 debug("context connection worked, checking for readiness");
61 pa_context_set_state_callback(pa_ctx, state_callback, &pa_ready);
62 while(!pa_ready)
63 {
64 if(pa_ready == 0)
65 {
66 pa_mainloop_iterate(pa_ml, 1, NULL);
67 continue;
68 }
69 if(pa_ready == 2)
70 {
71 ret = -1;
72 break;
73 }
74 pa_mainloop_iterate(pa_ml, 1, NULL);
75 }
76 pa_context_disconnect(pa_ctx);
77 } else
78 {
79 debug("pa_context_connect() failed right away");
80 ret = -1;
81 }
82
83 pa_context_unref(pa_ctx);
84 pa_mainloop_free(pa_ml);
85
86 return ret;
87 }
88
89
90 static int open_pulse(out123_handle *ao)
91 {
92 int err = 0;
93 pa_simple* pas = NULL;
94 pa_sample_spec ss;
95 /* Check if already open ? */
96 if (ao->userptr) {
97 if(!AOQUIET)
98 error("Pulse audio output is already open.");
99 return -1;
100 }
101
102 /* Open an audio I/O stream. */
103 /* When they are < 0, I shall set some default. */
104 if(ao->rate < 0 || ao->format < 0 || ao->channels < 0)
105 {
106 ao->rate = 44100;
107 ao->channels = 2;
108 ao->format = MPG123_ENC_SIGNED_16;
109 }
110
111 /* Fill out pulse audio's data structure */
112 ss.channels = ao->channels;
113 ss.rate = ao->rate;
114
115 switch(ao->format) {
116 case MPG123_ENC_SIGNED_16:
117 #ifdef WORDS_BIGENDIAN
118 ss.format=PA_SAMPLE_S16BE;
119 #else
120 ss.format=PA_SAMPLE_S16LE;
121 #endif
122 break;
123 case MPG123_ENC_SIGNED_24:
124 #ifdef WORDS_BIGENDIAN
125 ss.format=PA_SAMPLE_S24BE;
126 #else
127 ss.format=PA_SAMPLE_S24LE;
128 #endif
129 break;
130 case MPG123_ENC_SIGNED_32:
131 #ifdef WORDS_BIGENDIAN
132 ss.format=PA_SAMPLE_S32BE;
133 #else
134 ss.format=PA_SAMPLE_S32LE;
135 #endif
136 break;
137 case MPG123_ENC_FLOAT_32:
138 #ifdef WORDS_BIGENDIAN
139 ss.format=PA_SAMPLE_FLOAT32BE;
140 #else
141 ss.format=PA_SAMPLE_FLOAT32LE;
142 #endif
143 break;
144 case MPG123_ENC_ALAW_8:
145 ss.format=PA_SAMPLE_ALAW;
146 break;
147 case MPG123_ENC_ULAW_8:
148 ss.format=PA_SAMPLE_ULAW;
149 break;
150 case MPG123_ENC_UNSIGNED_8:
151 ss.format=PA_SAMPLE_U8;
152 break;
153 default:
154 if(!AOQUIET)
155 error1("Unsupported audio format: 0x%x", ao->format);
156 return -1;
157 break;
158 }
159
160 // It used to be a default config that pa_simple_new() just starts
161 // an instance of pulseaudio if it is not running. This is undesirable
162 // for probing. Libout123 should not change the system config like that.
163 // Nowadays, you got socket activation on Linux with systemd instead,
164 // anyway, but this little check should not hurt in the grand scheme. Also,
165 // we could use it as starting point to convert to the async API if it
166 // is deemed useful.
167 if(check_for_server())
168 {
169 if(!AOQUIET)
170 error("No PulseAudio running. I will not accidentally trigger starting one.");
171 return -1;
172 }
173
174 /* Perform the open */
175 pas = pa_simple_new(
176 NULL, /* Use the default server */
177 ao->name, /* Our application's name */
178 PA_STREAM_PLAYBACK,
179 ao->device, /* Use the default device if NULL */
180 "via out123", /* Description of our stream */
181 &ss, /* Our sample format */
182 NULL, /* Use default channel map */
183 NULL, /* Use default buffering attributes */
184 &err /* Error result code */
185 );
186
187 if(pas == NULL)
188 {
189 if(!AOQUIET)
190 error1("Failed to open pulse audio output: %s", pa_strerror(err));
191 return -1;
192 }
193
194 /* Store the pointer */
195 ao->userptr = (void*)pas;
196 return 0;
197 }
198
199
200 static int get_formats_pulse(out123_handle *ao)
201 {
202 return MPG123_ENC_SIGNED_16 | MPG123_ENC_SIGNED_24 |MPG123_ENC_SIGNED_32
203 | MPG123_ENC_FLOAT_32 | MPG123_ENC_ALAW_8 | MPG123_ENC_ULAW_8
204 | MPG123_ENC_UNSIGNED_8;
205 }
206
207
208 static int write_pulse(out123_handle *ao, unsigned char *buf, int len)
209 {
210 pa_simple *pas = (pa_simple*)ao->userptr;
211 int ret, err;
212 /* Doesn't return number of bytes but just success or not. */
213 ret = pa_simple_write( pas, buf, len, &err );
214 if(ret<0)
215 {
216 if(!AOQUIET)
217 error1("Failed to write audio: %s", pa_strerror(err));
218 return -1;
219 }
220 return len; /* If successful, everything has been written. */
221 }
222
223 static int close_pulse(out123_handle *ao)
224 {
225 pa_simple *pas = (pa_simple*)ao->userptr;
226
227 if (pas) {
228 int err = 0; /* Do we really want to handle errors here? End is the end. */
229 pa_simple_drain(pas, &err);
230 pa_simple_free(pas);
231 ao->userptr = NULL;
232 }
233
234 return 0;
235 }
236
237 static void flush_pulse(out123_handle *ao)
238 {
239 pa_simple *pas = (pa_simple*)ao->userptr;
240
241 if (pas) {
242 int err = 0;
243 pa_simple_flush( pas, &err );
244 if(err && !AOQUIET)
245 error1("Failed to flush audio: %s", pa_strerror(err));
246 }
247 }
248
249 struct enumerate_data
250 {
251 int (*store_device)(void *devlist
252 , const char *name, const char *description);
253 void *devlist;
254 int ret;
255 };
256
257 // Device enumeration is apparently not so simple. Seems to need the full API.
258 // I paraphrase the usage out of the example at
259 // https://gist.github.com/andrewrk/6470f3786d05999fcb48
260 // and assume that the code is in public domain or at least generic enough
261 // since it just shows the public API.
262
263 static void sinklist_callback( pa_context *c
264 , const pa_sink_info *l, int eol, void *userdata )
265 {
266 struct enumerate_data *ed = userdata;
267 if(!eol && !ed->ret)
268 ed->ret = ed->store_device(ed->devlist, l->name, l->description);
269 }
270
271 static int enumerate_pulse( out123_handle *ao, int (*store_device)(void *devlist
272 , const char *name, const char *description), void *devlist )
273 {
274 pa_mainloop *pa_ml;
275 pa_mainloop_api *pa_mlapi;
276 pa_operation *pa_op = NULL;
277 pa_context *pa_ctx;
278 int state = 0;
279 int pa_ready = 0;
280 struct enumerate_data ed;
281 ed.store_device = store_device;
282 ed.devlist = devlist;
283 ed.ret = 0;
284
285 pa_ml = pa_mainloop_new();
286 pa_mlapi = pa_mainloop_get_api(pa_ml);
287 pa_ctx = pa_context_new(pa_mlapi, "out123 enumeration");
288 if(pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL))
289 {
290 error("Connection to PulseAudio failed right away.");
291 ed.ret = -1;
292 goto enumerate_end;
293 }
294
295 pa_context_set_state_callback(pa_ctx, state_callback, &pa_ready);
296
297 while(state < 2)
298 {
299 debug("in the loop");
300 if(pa_ready == 0)
301 {
302 pa_mainloop_iterate(pa_ml, 1, NULL);
303 continue;
304 }
305 if(pa_ready == 2)
306 {
307 if(!AOQUIET)
308 error("Querying PulseAudio server failed.");
309 ed.ret = -1;
310 break;
311 }
312 switch(state)
313 {
314 case 0:
315 pa_op = pa_context_get_sink_info_list( pa_ctx,
316 sinklist_callback, &ed );
317 ++state;
318 break;
319 case 1:
320 if(pa_operation_get_state(pa_op) == PA_OPERATION_DONE)
321 goto enumerate_preend;
322 break;
323 }
324 pa_mainloop_iterate(pa_ml, 1, NULL);
325 }
326
327 enumerate_preend:
328 if(pa_op)
329 pa_operation_unref(pa_op);
330 pa_context_disconnect(pa_ctx);
331
332 enumerate_end:
333 pa_context_unref(pa_ctx);
334 pa_mainloop_free(pa_ml);
335
336 return ed.ret;
337 }
338
339 static int init_pulse(out123_handle* ao)
340 {
341 if (ao==NULL) return -1;
342
343 /* Set callbacks */
344 ao->open = open_pulse;
345 ao->flush = flush_pulse;
346 ao->write = write_pulse;
347 ao->get_formats = get_formats_pulse;
348 ao->close = close_pulse;
349 ao->enumerate = enumerate_pulse;
350
351 /* Success */
352 return 0;
353 }
354
355
356 /*
357 Module information data structure
358 */
359 mpg123_module_t mpg123_output_module_info = {
360 /* api_version */ MPG123_MODULE_API_VERSION,
361 /* name */ "pulse",
362 /* description */ "Output audio using PulseAudio Server",
363 /* revision */ "$Rev:$",
364 /* handle */ NULL,
365
366 /* init_output */ init_pulse,
367 };
368