1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox PulseAudio backend
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DEV_AUDIO
|
---|
27 | #include <VBox/log.h>
|
---|
28 | #include <iprt/mem.h>
|
---|
29 |
|
---|
30 | #include <pulse/pulseaudio.h>
|
---|
31 | #include "pulse_stubs.h"
|
---|
32 |
|
---|
33 | #include "../../vl_vbox.h"
|
---|
34 | #include "audio.h"
|
---|
35 | #define AUDIO_CAP "pulse"
|
---|
36 | #include "audio_int.h"
|
---|
37 | #include <stdio.h>
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * We use a g_pMainLoop in a separate thread g_pContext. We have to call functions for
|
---|
41 | * manipulating objects either from callback functions or we have to protect
|
---|
42 | * these functions by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
|
---|
43 | */
|
---|
44 | static struct pa_threaded_mainloop *g_pMainLoop;
|
---|
45 | static struct pa_context *g_pContext;
|
---|
46 |
|
---|
47 | static void pulse_audio_fini (void *);
|
---|
48 |
|
---|
49 | typedef struct PulseVoice
|
---|
50 | {
|
---|
51 | HWVoiceOut hw;
|
---|
52 | void *pPCMBuf;
|
---|
53 | pa_stream *pStream;
|
---|
54 | int fOpSuccess;
|
---|
55 | } PulseVoice;
|
---|
56 |
|
---|
57 | static struct
|
---|
58 | {
|
---|
59 | int buffer_msecs_out;
|
---|
60 | int buffer_msecs_in;
|
---|
61 | } conf
|
---|
62 | =
|
---|
63 | {
|
---|
64 | INIT_FIELD (.buffer_msecs_out = ) 100,
|
---|
65 | INIT_FIELD (.buffer_msecs_in = ) 100,
|
---|
66 | };
|
---|
67 |
|
---|
68 | struct pulse_params_req
|
---|
69 | {
|
---|
70 | int freq;
|
---|
71 | pa_sample_format_t pa_format;
|
---|
72 | int nchannels;
|
---|
73 | };
|
---|
74 |
|
---|
75 | struct pulse_params_obt
|
---|
76 | {
|
---|
77 | int freq;
|
---|
78 | pa_sample_format_t pa_format;
|
---|
79 | int nchannels;
|
---|
80 | unsigned long buffer_size;
|
---|
81 | };
|
---|
82 |
|
---|
83 | static pa_sample_format_t aud_to_pulsefmt (audfmt_e fmt)
|
---|
84 | {
|
---|
85 | switch (fmt)
|
---|
86 | {
|
---|
87 | case AUD_FMT_U8:
|
---|
88 | return PA_SAMPLE_U8;
|
---|
89 |
|
---|
90 | case AUD_FMT_S16:
|
---|
91 | return PA_SAMPLE_S16LE;
|
---|
92 |
|
---|
93 | #ifdef PA_SAMPLE_S32LE
|
---|
94 | case AUD_FMT_S32:
|
---|
95 | return PA_SAMPLE_S32LE;
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | default:
|
---|
99 | dolog ("Bad audio format %d\n", fmt);
|
---|
100 | return PA_SAMPLE_U8;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | static int pulse_to_audfmt (pa_sample_format_t pulsefmt, audfmt_e *fmt, int *endianess)
|
---|
106 | {
|
---|
107 | switch (pulsefmt)
|
---|
108 | {
|
---|
109 | case PA_SAMPLE_U8:
|
---|
110 | *endianess = 0;
|
---|
111 | *fmt = AUD_FMT_U8;
|
---|
112 | break;
|
---|
113 |
|
---|
114 | case PA_SAMPLE_S16LE:
|
---|
115 | *fmt = AUD_FMT_S16;
|
---|
116 | *endianess = 0;
|
---|
117 | break;
|
---|
118 |
|
---|
119 | case PA_SAMPLE_S16BE:
|
---|
120 | *fmt = AUD_FMT_S16;
|
---|
121 | *endianess = 1;
|
---|
122 | break;
|
---|
123 |
|
---|
124 | #ifdef PA_SAMPLE_S32LE
|
---|
125 | case PA_SAMPLE_S32LE:
|
---|
126 | *fmt = AUD_FMT_S32;
|
---|
127 | *endianess = 0;
|
---|
128 | break;
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | #ifdef PA_SAMPLE_S32BE
|
---|
132 | case PA_SAMPLE_S32BE:
|
---|
133 | *fmt = AUD_FMT_S32;
|
---|
134 | *endianess = 1;
|
---|
135 | break;
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | default:
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void context_state_callback(pa_context *c, void *userdata)
|
---|
145 | {
|
---|
146 | switch (pa_context_get_state(c))
|
---|
147 | {
|
---|
148 | case PA_CONTEXT_READY:
|
---|
149 | case PA_CONTEXT_TERMINATED:
|
---|
150 | case PA_CONTEXT_FAILED:
|
---|
151 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
152 | break;
|
---|
153 | default:
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | static void stream_state_callback(pa_stream *s, void *userdata)
|
---|
159 | {
|
---|
160 | switch (pa_stream_get_state(s))
|
---|
161 | {
|
---|
162 | case PA_STREAM_READY:
|
---|
163 | case PA_STREAM_FAILED:
|
---|
164 | case PA_STREAM_TERMINATED:
|
---|
165 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
166 | break;
|
---|
167 | default:
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | static void stream_latency_update_callback(pa_stream *s, void *userdata)
|
---|
173 | {
|
---|
174 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
175 | }
|
---|
176 |
|
---|
177 | static int pulse_open (int fIn, struct pulse_params_req *req,
|
---|
178 | struct pulse_params_obt *obt, pa_stream **ppStream)
|
---|
179 | {
|
---|
180 | pa_sample_spec sspec;
|
---|
181 | pa_channel_map cmap;
|
---|
182 | pa_stream *pStream = NULL;
|
---|
183 | pa_buffer_attr bufAttr;
|
---|
184 | const pa_buffer_attr *pBufAttr;
|
---|
185 | const pa_sample_spec *pSampSpec;
|
---|
186 | char achPCMName[64];
|
---|
187 | pa_stream_flags_t flags;
|
---|
188 | int ms = fIn ? conf.buffer_msecs_in : conf.buffer_msecs_out;
|
---|
189 | const char *stream_name = audio_get_stream_name();
|
---|
190 |
|
---|
191 | RTStrPrintf(achPCMName, sizeof(achPCMName), "%.32s%s%s%s",
|
---|
192 | stream_name ? stream_name : "",
|
---|
193 | stream_name ? " (" : "",
|
---|
194 | fIn ? "pcm_in" : "pcm_out",
|
---|
195 | stream_name ? ")" : "");
|
---|
196 | sspec.rate = req->freq;
|
---|
197 | sspec.channels = req->nchannels;
|
---|
198 | sspec.format = req->pa_format;
|
---|
199 |
|
---|
200 | LogRel(("Pulse: open %s rate=%dHz channels=%d format=%s\n",
|
---|
201 | fIn ? "PCM_IN" : "PCM_OUT", req->freq, req->nchannels,
|
---|
202 | pa_sample_format_to_string(req->pa_format)));
|
---|
203 |
|
---|
204 | if (!pa_sample_spec_valid(&sspec))
|
---|
205 | {
|
---|
206 | LogRel(("Pulse: Unsupported sample specification\n"));
|
---|
207 | goto fail;
|
---|
208 | }
|
---|
209 |
|
---|
210 | pa_channel_map_init_auto(&cmap, sspec.channels, PA_CHANNEL_MAP_ALSA);
|
---|
211 |
|
---|
212 | #if 0
|
---|
213 | pa_cvolume_reset(&volume, sspec.channels);
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
217 |
|
---|
218 | if (!(pStream = pa_stream_new(g_pContext, achPCMName, &sspec, &cmap)))
|
---|
219 | {
|
---|
220 | LogRel(("Pulse: Cannot create stream %s\n", achPCMName));
|
---|
221 | goto unlock_and_fail;
|
---|
222 | }
|
---|
223 |
|
---|
224 | pSampSpec = pa_stream_get_sample_spec(pStream);
|
---|
225 | obt->pa_format = pSampSpec->format;
|
---|
226 | obt->nchannels = pSampSpec->channels;
|
---|
227 | obt->freq = pSampSpec->rate;
|
---|
228 |
|
---|
229 | pa_stream_set_state_callback(pStream, stream_state_callback, NULL);
|
---|
230 | pa_stream_set_latency_update_callback(pStream, stream_latency_update_callback, NULL);
|
---|
231 |
|
---|
232 | memset(&bufAttr, 0, sizeof(bufAttr));
|
---|
233 | bufAttr.tlength = (pa_bytes_per_second(pSampSpec) * ms) / 1000;
|
---|
234 | bufAttr.maxlength = (bufAttr.tlength*3) / 2;
|
---|
235 | bufAttr.minreq = pa_bytes_per_second(pSampSpec) / 100; /* 10ms */
|
---|
236 | bufAttr.prebuf = bufAttr.tlength - bufAttr.minreq;
|
---|
237 | bufAttr.fragsize = pa_bytes_per_second(pSampSpec) / 100; /* 10ms */
|
---|
238 |
|
---|
239 | flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
|
---|
240 | if (fIn)
|
---|
241 | {
|
---|
242 | if (pa_stream_connect_record(pStream, /*dev=*/NULL, &bufAttr, flags) < 0)
|
---|
243 | {
|
---|
244 | LogRel(("Pulse: Cannot connect record stream : %s\n",
|
---|
245 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
246 | goto disconnect_unlock_and_fail;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | if (pa_stream_connect_playback(pStream, /*dev=*/NULL, &bufAttr, flags,
|
---|
252 | NULL, NULL) < 0)
|
---|
253 | {
|
---|
254 | LogRel(("Pulse: Cannot connect playback stream: %s\n",
|
---|
255 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
256 | goto disconnect_unlock_and_fail;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* Wait until the stream is ready */
|
---|
261 | for (;;)
|
---|
262 | {
|
---|
263 | pa_stream_state_t sstate;
|
---|
264 | pa_threaded_mainloop_wait(g_pMainLoop);
|
---|
265 | sstate = pa_stream_get_state(pStream);
|
---|
266 | if (sstate == PA_STREAM_READY)
|
---|
267 | break;
|
---|
268 | else if (sstate == PA_STREAM_FAILED || sstate == PA_STREAM_TERMINATED)
|
---|
269 | {
|
---|
270 | LogRel(("Pulse: Failed to initialize stream (state %d)\n", sstate));
|
---|
271 | goto disconnect_unlock_and_fail;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | pBufAttr = pa_stream_get_buffer_attr(pStream);
|
---|
276 | obt->buffer_size = pBufAttr->maxlength;
|
---|
277 |
|
---|
278 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
279 |
|
---|
280 | LogRel(("Pulse: buffer settings: max=%d tlength=%d prebuf=%d minreq=%d\n",
|
---|
281 | pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
|
---|
282 |
|
---|
283 | *ppStream = pStream;
|
---|
284 | return 0;
|
---|
285 |
|
---|
286 | disconnect_unlock_and_fail:
|
---|
287 | pa_stream_disconnect(pStream);
|
---|
288 |
|
---|
289 | unlock_and_fail:
|
---|
290 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
291 |
|
---|
292 | fail:
|
---|
293 | if (pStream)
|
---|
294 | pa_stream_unref(pStream);
|
---|
295 |
|
---|
296 | *ppStream = NULL;
|
---|
297 | return -1;
|
---|
298 | }
|
---|
299 |
|
---|
300 | static int pulse_init_out (HWVoiceOut *hw, audsettings_t *as)
|
---|
301 | {
|
---|
302 | PulseVoice *pulse = (PulseVoice *) hw;
|
---|
303 | struct pulse_params_req req;
|
---|
304 | struct pulse_params_obt obt;
|
---|
305 | audfmt_e effective_fmt;
|
---|
306 | int endianness;
|
---|
307 | audsettings_t obt_as;
|
---|
308 |
|
---|
309 | req.pa_format = aud_to_pulsefmt (as->fmt);
|
---|
310 | req.freq = as->freq;
|
---|
311 | req.nchannels = as->nchannels;
|
---|
312 |
|
---|
313 | if (pulse_open (/*fIn=*/0, &req, &obt, &pulse->pStream))
|
---|
314 | return -1;
|
---|
315 |
|
---|
316 | if (pulse_to_audfmt (obt.pa_format, &effective_fmt, &endianness))
|
---|
317 | {
|
---|
318 | LogRel(("Pulse: Cannot find audio format %d\n", obt.pa_format));
|
---|
319 | return -1;
|
---|
320 | }
|
---|
321 |
|
---|
322 | obt_as.freq = obt.freq;
|
---|
323 | obt_as.nchannels = obt.nchannels;
|
---|
324 | obt_as.fmt = effective_fmt;
|
---|
325 | obt_as.endianness = endianness;
|
---|
326 |
|
---|
327 | audio_pcm_init_info (&hw->info, &obt_as);
|
---|
328 | hw->samples = obt.buffer_size >> hw->info.shift;
|
---|
329 |
|
---|
330 | pulse->pPCMBuf = RTMemAllocZ(obt.buffer_size);
|
---|
331 | if (!pulse->pPCMBuf)
|
---|
332 | {
|
---|
333 | LogRel(("Pulse: Could not allocate DAC buffer of %d bytes\n", obt.buffer_size));
|
---|
334 | return -1;
|
---|
335 | }
|
---|
336 |
|
---|
337 | return 0;
|
---|
338 | }
|
---|
339 |
|
---|
340 | static void pulse_fini_out (HWVoiceOut *hw)
|
---|
341 | {
|
---|
342 | PulseVoice *pulse = (PulseVoice *)hw;
|
---|
343 | if (pulse->pStream)
|
---|
344 | {
|
---|
345 | pa_stream_disconnect(pulse->pStream);
|
---|
346 | pa_stream_unref(pulse->pStream);
|
---|
347 | pulse->pStream = NULL;
|
---|
348 | }
|
---|
349 | if (pulse->pPCMBuf)
|
---|
350 | {
|
---|
351 | RTMemFree (pulse->pPCMBuf);
|
---|
352 | pulse->pPCMBuf = NULL;
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | static int pulse_run_out (HWVoiceOut *hw)
|
---|
357 | {
|
---|
358 | PulseVoice *pulse = (PulseVoice *) hw;
|
---|
359 | int csLive, csDecr, csSamples, csToWrite, csAvail;
|
---|
360 | size_t cbAvail, cbToWrite;
|
---|
361 | uint8_t *pu8Dst;
|
---|
362 | st_sample_t *psSrc;
|
---|
363 |
|
---|
364 | csLive = audio_pcm_hw_get_live_out (hw);
|
---|
365 | if (!csLive)
|
---|
366 | return 0;
|
---|
367 |
|
---|
368 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
369 |
|
---|
370 | cbAvail = pa_stream_writable_size (pulse->pStream);
|
---|
371 | if (cbAvail == (size_t)-1)
|
---|
372 | {
|
---|
373 | LogRel(("Pulse: Failed to determine the writable size: %s\n",
|
---|
374 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
375 | return 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | csAvail = cbAvail >> hw->info.shift; /* bytes => samples */
|
---|
379 | csDecr = audio_MIN (csLive, csAvail);
|
---|
380 | csSamples = csDecr;
|
---|
381 |
|
---|
382 | while (csSamples)
|
---|
383 | {
|
---|
384 | /* split request at the end of our samples buffer */
|
---|
385 | csToWrite = audio_MIN (csSamples, hw->samples - hw->rpos);
|
---|
386 | cbToWrite = csToWrite << hw->info.shift;
|
---|
387 | psSrc = hw->mix_buf + hw->rpos;
|
---|
388 | pu8Dst = advance (pulse->pPCMBuf, hw->rpos << hw->info.shift);
|
---|
389 |
|
---|
390 | hw->clip (pu8Dst, psSrc, csToWrite);
|
---|
391 |
|
---|
392 | if (pa_stream_write (pulse->pStream, pu8Dst, cbToWrite,
|
---|
393 | /*cleanup_callback=*/NULL, 0, PA_SEEK_RELATIVE) < 0)
|
---|
394 | {
|
---|
395 | LogRel(("Pulse: Failed to write %d samples: %s\n",
|
---|
396 | csToWrite, pa_strerror(pa_context_errno(g_pContext))));
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | hw->rpos = (hw->rpos + csToWrite) % hw->samples;
|
---|
400 | csSamples -= csToWrite;
|
---|
401 | }
|
---|
402 |
|
---|
403 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
404 |
|
---|
405 | return csDecr;
|
---|
406 | }
|
---|
407 |
|
---|
408 | static int pulse_write (SWVoiceOut *sw, void *buf, int len)
|
---|
409 | {
|
---|
410 | return audio_pcm_sw_write (sw, buf, len);
|
---|
411 | }
|
---|
412 |
|
---|
413 | static void stream_success_callback(pa_stream *pStream, int success, void *userdata)
|
---|
414 | {
|
---|
415 | PulseVoice *pulse = (PulseVoice *) userdata;
|
---|
416 | pulse->fOpSuccess = success;
|
---|
417 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
418 | }
|
---|
419 |
|
---|
420 | typedef enum
|
---|
421 | {
|
---|
422 | Unpause = 0,
|
---|
423 | Pause = 1,
|
---|
424 | Flush = 2,
|
---|
425 | Trigger = 3
|
---|
426 | } pulse_cmd_t;
|
---|
427 |
|
---|
428 | static int pulse_ctrl (HWVoiceOut *hw, pulse_cmd_t cmd)
|
---|
429 | {
|
---|
430 | PulseVoice *pulse = (PulseVoice *) hw;
|
---|
431 | pa_operation *op = NULL;
|
---|
432 |
|
---|
433 | if (!pulse->pStream)
|
---|
434 | return 0;
|
---|
435 |
|
---|
436 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
437 | switch (cmd)
|
---|
438 | {
|
---|
439 | case Pause:
|
---|
440 | op = pa_stream_cork(pulse->pStream, 1, stream_success_callback, pulse);
|
---|
441 | break;
|
---|
442 | case Unpause:
|
---|
443 | op = pa_stream_cork(pulse->pStream, 0, stream_success_callback, pulse);
|
---|
444 | break;
|
---|
445 | case Flush:
|
---|
446 | op = pa_stream_flush(pulse->pStream, stream_success_callback, pulse);
|
---|
447 | break;
|
---|
448 | case Trigger:
|
---|
449 | op = pa_stream_trigger(pulse->pStream, stream_success_callback, pulse);
|
---|
450 | break;
|
---|
451 | default:
|
---|
452 | goto fail;
|
---|
453 | }
|
---|
454 | if (!op)
|
---|
455 | LogRel(("Pulse: Failed ctrl cmd=%d to stream: %s\n",
|
---|
456 | cmd, pa_strerror(pa_context_errno(g_pContext))));
|
---|
457 | else
|
---|
458 | pa_operation_unref(op);
|
---|
459 |
|
---|
460 | fail:
|
---|
461 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
462 | return 0;
|
---|
463 | }
|
---|
464 |
|
---|
465 | static int pulse_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
---|
466 | {
|
---|
467 | switch (cmd)
|
---|
468 | {
|
---|
469 | case VOICE_ENABLE:
|
---|
470 | pulse_ctrl(hw, Unpause);
|
---|
471 | pulse_ctrl(hw, Trigger);
|
---|
472 | break;
|
---|
473 | case VOICE_DISABLE:
|
---|
474 | pulse_ctrl(hw, Flush);
|
---|
475 | break;
|
---|
476 | default:
|
---|
477 | return -1;
|
---|
478 | }
|
---|
479 | return 0;
|
---|
480 | }
|
---|
481 |
|
---|
482 | static int pulse_init_in (HWVoiceIn *hw, audsettings_t *as)
|
---|
483 | {
|
---|
484 | PulseVoice *pulse = (PulseVoice *) hw;
|
---|
485 | struct pulse_params_req req;
|
---|
486 | struct pulse_params_obt obt;
|
---|
487 | audfmt_e effective_fmt;
|
---|
488 | int endianness;
|
---|
489 | audsettings_t obt_as;
|
---|
490 |
|
---|
491 | req.pa_format = aud_to_pulsefmt (as->fmt);
|
---|
492 | req.freq = as->freq;
|
---|
493 | req.nchannels = as->nchannels;
|
---|
494 |
|
---|
495 | if (pulse_open (/*fIn=*/1, &req, &obt, &pulse->pStream))
|
---|
496 | return -1;
|
---|
497 |
|
---|
498 | if (pulse_to_audfmt (obt.pa_format, &effective_fmt, &endianness))
|
---|
499 | {
|
---|
500 | LogRel(("Pulse: Cannot find audio format %d\n", obt.pa_format));
|
---|
501 | return -1;
|
---|
502 | }
|
---|
503 |
|
---|
504 | obt_as.freq = obt.freq;
|
---|
505 | obt_as.nchannels = obt.nchannels;
|
---|
506 | obt_as.fmt = effective_fmt;
|
---|
507 | obt_as.endianness = endianness;
|
---|
508 |
|
---|
509 | audio_pcm_init_info (&hw->info, &obt_as);
|
---|
510 |
|
---|
511 | /* pcm_in: reserve twice as the maximum buffer length because of peek()/drop(). */
|
---|
512 | hw->samples = 2 * (obt.buffer_size >> hw->info.shift);
|
---|
513 |
|
---|
514 | /* no buffer for input */
|
---|
515 | pulse->pPCMBuf = NULL;
|
---|
516 |
|
---|
517 | return 0;
|
---|
518 | }
|
---|
519 |
|
---|
520 | static void pulse_fini_in (HWVoiceIn *hw)
|
---|
521 | {
|
---|
522 | PulseVoice *pulse = (PulseVoice *)hw;
|
---|
523 | if (pulse->pStream)
|
---|
524 | {
|
---|
525 | pa_stream_disconnect(pulse->pStream);
|
---|
526 | pa_stream_unref(pulse->pStream);
|
---|
527 | pulse->pStream = NULL;
|
---|
528 | }
|
---|
529 | if (pulse->pPCMBuf)
|
---|
530 | {
|
---|
531 | RTMemFree (pulse->pPCMBuf);
|
---|
532 | pulse->pPCMBuf = NULL;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | static int pulse_run_in (HWVoiceIn *hw)
|
---|
537 | {
|
---|
538 | PulseVoice *pulse = (PulseVoice *) hw;
|
---|
539 | int csDead, csDecr = 0, csSamples, csRead, csAvail;
|
---|
540 | size_t cbAvail;
|
---|
541 | const void *pu8Src;
|
---|
542 | st_sample_t *psDst;
|
---|
543 |
|
---|
544 | csDead = hw->samples - audio_pcm_hw_get_live_in (hw);
|
---|
545 |
|
---|
546 | if (!csDead)
|
---|
547 | return 0; /* no buffer available */
|
---|
548 |
|
---|
549 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
550 |
|
---|
551 | if (pa_stream_peek(pulse->pStream, &pu8Src, &cbAvail) < 0)
|
---|
552 | {
|
---|
553 | LogRel(("Pulse: Peek failed: %s\n",
|
---|
554 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
555 | goto exit;
|
---|
556 | }
|
---|
557 | if (!pu8Src)
|
---|
558 | goto exit;
|
---|
559 |
|
---|
560 | csAvail = cbAvail >> hw->info.shift;
|
---|
561 | csDecr = audio_MIN (csDead, csAvail);
|
---|
562 |
|
---|
563 | csSamples = csDecr;
|
---|
564 |
|
---|
565 | while (csSamples)
|
---|
566 | {
|
---|
567 | /* split request at the end of our samples buffer */
|
---|
568 | psDst = hw->conv_buf + hw->wpos;
|
---|
569 | csRead = audio_MIN (csSamples, hw->samples - hw->wpos);
|
---|
570 | hw->conv (psDst, pu8Src, csRead, &nominal_volume);
|
---|
571 | hw->wpos = (hw->wpos + csRead) % hw->samples;
|
---|
572 | csSamples -= csRead;
|
---|
573 | pu8Src = (const void*)((uint8_t*)pu8Src + (csRead << hw->info.shift));
|
---|
574 | }
|
---|
575 |
|
---|
576 | pa_stream_drop(pulse->pStream);
|
---|
577 |
|
---|
578 | exit:
|
---|
579 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
580 |
|
---|
581 | return csDecr;
|
---|
582 | }
|
---|
583 |
|
---|
584 | static int pulse_read (SWVoiceIn *sw, void *buf, int size)
|
---|
585 | {
|
---|
586 | return audio_pcm_sw_read (sw, buf, size);
|
---|
587 | }
|
---|
588 |
|
---|
589 | static int pulse_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
---|
590 | {
|
---|
591 | return 0;
|
---|
592 | }
|
---|
593 |
|
---|
594 | static void *pulse_audio_init (void)
|
---|
595 | {
|
---|
596 | int rc;
|
---|
597 |
|
---|
598 | rc = audioLoadPulseLib();
|
---|
599 | if (RT_FAILURE(rc))
|
---|
600 | {
|
---|
601 | LogRel(("Pulse: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
|
---|
602 | return NULL;
|
---|
603 | }
|
---|
604 | if (!(g_pMainLoop = pa_threaded_mainloop_new()))
|
---|
605 | {
|
---|
606 | LogRel(("Pulse: Failed to allocate main loop: %s\n",
|
---|
607 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
608 | goto fail;
|
---|
609 | }
|
---|
610 | if (!(g_pContext = pa_context_new(pa_threaded_mainloop_get_api(g_pMainLoop), "VBox")))
|
---|
611 | {
|
---|
612 | LogRel(("Pulse: Failed to allocate context: %s\n",
|
---|
613 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
614 | goto fail;
|
---|
615 | }
|
---|
616 | if (pa_threaded_mainloop_start(g_pMainLoop) < 0)
|
---|
617 | {
|
---|
618 | LogRel(("Pulse: Failed to start threaded mainloop: %s\n",
|
---|
619 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
620 | goto fail;
|
---|
621 | }
|
---|
622 |
|
---|
623 | pa_context_set_state_callback(g_pContext, context_state_callback, NULL);
|
---|
624 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
625 |
|
---|
626 | if (pa_context_connect(g_pContext, /*server=*/NULL, 0, NULL) < 0)
|
---|
627 | {
|
---|
628 | LogRel(("Pulse: Failed to connect to server: %s\n",
|
---|
629 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
630 | goto unlock_and_fail;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /* Wait until the g_pContext is ready */
|
---|
634 | for (;;)
|
---|
635 | {
|
---|
636 | pa_context_state_t cstate;
|
---|
637 | pa_threaded_mainloop_wait(g_pMainLoop);
|
---|
638 | cstate = pa_context_get_state(g_pContext);
|
---|
639 | if (cstate == PA_CONTEXT_READY)
|
---|
640 | break;
|
---|
641 | else if (cstate == PA_CONTEXT_TERMINATED || cstate == PA_CONTEXT_FAILED)
|
---|
642 | {
|
---|
643 | LogRel(("Pulse: Failed to initialize context (state %d)\n", cstate));
|
---|
644 | goto unlock_and_fail;
|
---|
645 | }
|
---|
646 | }
|
---|
647 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
648 |
|
---|
649 | return &conf;
|
---|
650 |
|
---|
651 | unlock_and_fail:
|
---|
652 | if (g_pMainLoop)
|
---|
653 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
654 |
|
---|
655 | fail:
|
---|
656 | if (g_pMainLoop)
|
---|
657 | pa_threaded_mainloop_stop(g_pMainLoop);
|
---|
658 |
|
---|
659 | if (g_pContext)
|
---|
660 | {
|
---|
661 | pa_context_disconnect(g_pContext);
|
---|
662 | pa_context_unref(g_pContext);
|
---|
663 | g_pContext = NULL;
|
---|
664 | }
|
---|
665 | if (g_pMainLoop)
|
---|
666 | {
|
---|
667 | pa_threaded_mainloop_free(g_pMainLoop);
|
---|
668 | g_pMainLoop = NULL;
|
---|
669 | }
|
---|
670 | return NULL;
|
---|
671 | }
|
---|
672 |
|
---|
673 | static void pulse_audio_fini (void *opaque)
|
---|
674 | {
|
---|
675 | if (g_pMainLoop)
|
---|
676 | pa_threaded_mainloop_stop(g_pMainLoop);
|
---|
677 | if (g_pContext)
|
---|
678 | {
|
---|
679 | pa_context_disconnect(g_pContext);
|
---|
680 | pa_context_unref(g_pContext);
|
---|
681 | g_pContext = NULL;
|
---|
682 | }
|
---|
683 | if (g_pMainLoop)
|
---|
684 | {
|
---|
685 | pa_threaded_mainloop_free(g_pMainLoop);
|
---|
686 | g_pMainLoop = NULL;
|
---|
687 | }
|
---|
688 | (void) opaque;
|
---|
689 | }
|
---|
690 |
|
---|
691 | static struct audio_option pulse_options[] =
|
---|
692 | {
|
---|
693 | {"DAC_MS", AUD_OPT_INT, &conf.buffer_msecs_out,
|
---|
694 | "DAC period size in milliseconds", NULL, 0},
|
---|
695 | {"ADC_MS", AUD_OPT_INT, &conf.buffer_msecs_in,
|
---|
696 | "ADC period size in milliseconds", NULL, 0},
|
---|
697 | {NULL, 0, NULL, NULL, NULL, 0}
|
---|
698 | };
|
---|
699 |
|
---|
700 | static struct audio_pcm_ops pulse_pcm_ops =
|
---|
701 | {
|
---|
702 | pulse_init_out,
|
---|
703 | pulse_fini_out,
|
---|
704 | pulse_run_out,
|
---|
705 | pulse_write,
|
---|
706 | pulse_ctl_out,
|
---|
707 |
|
---|
708 | pulse_init_in,
|
---|
709 | pulse_fini_in,
|
---|
710 | pulse_run_in,
|
---|
711 | pulse_read,
|
---|
712 | pulse_ctl_in
|
---|
713 | };
|
---|
714 |
|
---|
715 | struct audio_driver pulse_audio_driver =
|
---|
716 | {
|
---|
717 | INIT_FIELD (name = ) "pulse",
|
---|
718 | INIT_FIELD (descr = ) "PulseAudio http://www.pulseaudio.org",
|
---|
719 | INIT_FIELD (options = ) pulse_options,
|
---|
720 | INIT_FIELD (init = ) pulse_audio_init,
|
---|
721 | INIT_FIELD (fini = ) pulse_audio_fini,
|
---|
722 | INIT_FIELD (pcm_ops = ) &pulse_pcm_ops,
|
---|
723 | INIT_FIELD (can_be_default = ) 1,
|
---|
724 | INIT_FIELD (max_voices_out = ) INT_MAX,
|
---|
725 | INIT_FIELD (max_voices_in = ) INT_MAX,
|
---|
726 | INIT_FIELD (voice_size_out = ) sizeof (PulseVoice),
|
---|
727 | INIT_FIELD (voice_size_in = ) sizeof (PulseVoice)
|
---|
728 | };
|
---|