1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox PulseAudio backend
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DEV_AUDIO
|
---|
23 | #include <VBox/log.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 |
|
---|
26 | #include <pulse/pulseaudio.h>
|
---|
27 | #include "pulse_stubs.h"
|
---|
28 |
|
---|
29 | #include "vl_vbox.h"
|
---|
30 | #include "audio.h"
|
---|
31 | #define AUDIO_CAP "pulse"
|
---|
32 | #include "audio_int.h"
|
---|
33 | #include <stdio.h>
|
---|
34 |
|
---|
35 | #define MAX_LOG_REL_ERRORS 32
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * We use a g_pMainLoop in a separate thread g_pContext. We have to call functions for
|
---|
39 | * manipulating objects either from callback functions or we have to protect
|
---|
40 | * these functions by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
|
---|
41 | */
|
---|
42 | static struct pa_threaded_mainloop *g_pMainLoop;
|
---|
43 | static struct pa_context *g_pContext;
|
---|
44 |
|
---|
45 | typedef struct PulseVoice
|
---|
46 | {
|
---|
47 | /** not accessed from within this context */
|
---|
48 | union
|
---|
49 | {
|
---|
50 | HWVoiceOut In;
|
---|
51 | HWVoiceIn Out;
|
---|
52 | } hw;
|
---|
53 | /** DAC buffer */
|
---|
54 | void *pPCMBuf;
|
---|
55 | /** Pulse stream */
|
---|
56 | pa_stream *pStream;
|
---|
57 | /** Pulse sample format and attribute specification */
|
---|
58 | pa_sample_spec SampleSpec;
|
---|
59 | /** Pulse playback and buffer metrics */
|
---|
60 | pa_buffer_attr BufAttr;
|
---|
61 | int fOpSuccess;
|
---|
62 | /** number of logged errors */
|
---|
63 | unsigned cErrors;
|
---|
64 | /** Pulse record peek buffer */
|
---|
65 | const uint8_t *pu8PeekBuf;
|
---|
66 | size_t cbPeekBuf;
|
---|
67 | size_t offPeekBuf;
|
---|
68 | pa_operation *pDrainOp;
|
---|
69 | } PulseVoice;
|
---|
70 |
|
---|
71 | /* The desired buffer length in milliseconds. Will be the target total stream
|
---|
72 | * latency on newer version of pulse. Apparent latency can be less (or more.)
|
---|
73 | */
|
---|
74 | static struct
|
---|
75 | {
|
---|
76 | int buffer_msecs_out;
|
---|
77 | int buffer_msecs_in;
|
---|
78 | } conf
|
---|
79 | =
|
---|
80 | {
|
---|
81 | INIT_FIELD (.buffer_msecs_out = ) 100,
|
---|
82 | INIT_FIELD (.buffer_msecs_in = ) 100,
|
---|
83 | };
|
---|
84 |
|
---|
85 | static pa_sample_format_t aud_to_pulsefmt (audfmt_e fmt)
|
---|
86 | {
|
---|
87 | switch (fmt)
|
---|
88 | {
|
---|
89 | case AUD_FMT_U8:
|
---|
90 | return PA_SAMPLE_U8;
|
---|
91 |
|
---|
92 | case AUD_FMT_S16:
|
---|
93 | return PA_SAMPLE_S16LE;
|
---|
94 |
|
---|
95 | #ifdef PA_SAMPLE_S32LE
|
---|
96 | case AUD_FMT_S32:
|
---|
97 | return PA_SAMPLE_S32LE;
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | default:
|
---|
101 | dolog ("Bad audio format %d\n", fmt);
|
---|
102 | return PA_SAMPLE_U8;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | static int pulse_to_audfmt (pa_sample_format_t pulsefmt, audfmt_e *fmt, int *endianess)
|
---|
108 | {
|
---|
109 | switch (pulsefmt)
|
---|
110 | {
|
---|
111 | case PA_SAMPLE_U8:
|
---|
112 | *endianess = 0;
|
---|
113 | *fmt = AUD_FMT_U8;
|
---|
114 | break;
|
---|
115 |
|
---|
116 | case PA_SAMPLE_S16LE:
|
---|
117 | *fmt = AUD_FMT_S16;
|
---|
118 | *endianess = 0;
|
---|
119 | break;
|
---|
120 |
|
---|
121 | case PA_SAMPLE_S16BE:
|
---|
122 | *fmt = AUD_FMT_S16;
|
---|
123 | *endianess = 1;
|
---|
124 | break;
|
---|
125 |
|
---|
126 | #ifdef PA_SAMPLE_S32LE
|
---|
127 | case PA_SAMPLE_S32LE:
|
---|
128 | *fmt = AUD_FMT_S32;
|
---|
129 | *endianess = 0;
|
---|
130 | break;
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | #ifdef PA_SAMPLE_S32BE
|
---|
134 | case PA_SAMPLE_S32BE:
|
---|
135 | *fmt = AUD_FMT_S32;
|
---|
136 | *endianess = 1;
|
---|
137 | break;
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | default:
|
---|
141 | return -1;
|
---|
142 | }
|
---|
143 | return 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | static void stream_success_callback(pa_stream *pStream, int fSuccess, void *userdata)
|
---|
147 | {
|
---|
148 | PulseVoice *pPulse = (PulseVoice *)userdata;
|
---|
149 | pPulse->fOpSuccess = fSuccess;
|
---|
150 | if (!fSuccess)
|
---|
151 | {
|
---|
152 | if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
|
---|
153 | {
|
---|
154 | int rc = pa_context_errno(g_pContext);
|
---|
155 | pPulse->cErrors++;
|
---|
156 | LogRel(("Pulse: Failed stream operation: %s\n", pa_strerror(rc)));
|
---|
157 | }
|
---|
158 | }
|
---|
159 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Synchronously wait until an operation completed.
|
---|
164 | */
|
---|
165 | static int pulse_wait_for_operation (pa_operation *op)
|
---|
166 | {
|
---|
167 | if (op)
|
---|
168 | {
|
---|
169 | while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
|
---|
170 | pa_threaded_mainloop_wait(g_pMainLoop);
|
---|
171 | pa_operation_unref(op);
|
---|
172 | }
|
---|
173 |
|
---|
174 | return 1;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Context status changed.
|
---|
179 | */
|
---|
180 | static void context_state_callback(pa_context *pContext, void *userdata)
|
---|
181 | {
|
---|
182 | PulseVoice *pPulse = (PulseVoice *)userdata;
|
---|
183 | switch (pa_context_get_state(pContext))
|
---|
184 | {
|
---|
185 | case PA_CONTEXT_READY:
|
---|
186 | case PA_CONTEXT_TERMINATED:
|
---|
187 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
188 | break;
|
---|
189 |
|
---|
190 | case PA_CONTEXT_FAILED:
|
---|
191 | LogRel(("Pulse: Audio input/output stopped!\n"));
|
---|
192 | if (pPulse)
|
---|
193 | pPulse->cErrors = MAX_LOG_REL_ERRORS;
|
---|
194 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
195 | break;
|
---|
196 |
|
---|
197 | default:
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Stream status changed.
|
---|
204 | */
|
---|
205 | static void stream_state_callback(pa_stream *pStream, void *userdata)
|
---|
206 | {
|
---|
207 | switch (pa_stream_get_state(pStream))
|
---|
208 | {
|
---|
209 | case PA_STREAM_READY:
|
---|
210 | case PA_STREAM_FAILED:
|
---|
211 | case PA_STREAM_TERMINATED:
|
---|
212 | pa_threaded_mainloop_signal(g_pMainLoop, 0);
|
---|
213 | break;
|
---|
214 |
|
---|
215 | default:
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Callback called when our pa_stream_drain operation was completed.
|
---|
222 | */
|
---|
223 | static void stream_drain_callback(pa_stream *pStream, int fSuccess, void *userdata)
|
---|
224 | {
|
---|
225 | PulseVoice *pPulse = (PulseVoice *)userdata;
|
---|
226 | pPulse->fOpSuccess = fSuccess;
|
---|
227 | if (!fSuccess)
|
---|
228 | {
|
---|
229 | if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
|
---|
230 | {
|
---|
231 | int rc = pa_context_errno(g_pContext);
|
---|
232 | pPulse->cErrors++;
|
---|
233 | LogRel(("Pulse: Failed stream operation: %s\n", pa_strerror(rc)));
|
---|
234 | }
|
---|
235 | }
|
---|
236 | else
|
---|
237 | pa_operation_unref(pa_stream_cork(pStream, 1, stream_success_callback, userdata));
|
---|
238 |
|
---|
239 | pa_operation_unref(pPulse->pDrainOp);
|
---|
240 | pPulse->pDrainOp = NULL;
|
---|
241 | }
|
---|
242 |
|
---|
243 | static int pulse_open (int fIn, pa_stream **ppStream, pa_sample_spec *pSampleSpec,
|
---|
244 | pa_buffer_attr *pBufAttr)
|
---|
245 | {
|
---|
246 | const pa_buffer_attr *pBufAttrObtained;
|
---|
247 | pa_stream *pStream = NULL;
|
---|
248 | char achPCMName[64];
|
---|
249 | pa_stream_flags_t flags = 0;
|
---|
250 | const char *stream_name = audio_get_stream_name();
|
---|
251 |
|
---|
252 | RTStrPrintf(achPCMName, sizeof(achPCMName), "%.32s%s%s%s",
|
---|
253 | stream_name ? stream_name : "",
|
---|
254 | stream_name ? " (" : "",
|
---|
255 | fIn ? "pcm_in" : "pcm_out",
|
---|
256 | stream_name ? ")" : "");
|
---|
257 |
|
---|
258 | LogRel(("Pulse: open %s rate=%dHz channels=%d format=%s\n",
|
---|
259 | fIn ? "PCM_IN" : "PCM_OUT", pSampleSpec->rate, pSampleSpec->channels,
|
---|
260 | pa_sample_format_to_string(pSampleSpec->format)));
|
---|
261 |
|
---|
262 | if (!pa_sample_spec_valid(pSampleSpec))
|
---|
263 | {
|
---|
264 | LogRel(("Pulse: Unsupported sample specification\n"));
|
---|
265 | goto fail;
|
---|
266 | }
|
---|
267 |
|
---|
268 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
269 |
|
---|
270 | if (!(pStream = pa_stream_new(g_pContext, achPCMName, pSampleSpec, /*channel_map=*/NULL)))
|
---|
271 | {
|
---|
272 | LogRel(("Pulse: Cannot create stream %s\n", achPCMName));
|
---|
273 | goto unlock_and_fail;
|
---|
274 | }
|
---|
275 |
|
---|
276 | pa_stream_set_state_callback(pStream, stream_state_callback, NULL);
|
---|
277 |
|
---|
278 | #if PA_API_VERSION >= 12
|
---|
279 | /* XXX */
|
---|
280 | flags |= PA_STREAM_ADJUST_LATENCY;
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | #if 0
|
---|
284 | /* not applicable as we don't use pa_stream_get_latency() and pa_stream_get_time() */
|
---|
285 | flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | /* no input/output right away after the stream was started */
|
---|
289 | flags |= PA_STREAM_START_CORKED;
|
---|
290 |
|
---|
291 | if (fIn)
|
---|
292 | {
|
---|
293 | LogRel(("Pulse: Requested record buffer attributes: maxlength=%d fragsize=%d\n",
|
---|
294 | pBufAttr->maxlength, pBufAttr->fragsize));
|
---|
295 |
|
---|
296 | if (pa_stream_connect_record(pStream, /*dev=*/NULL, pBufAttr, flags) < 0)
|
---|
297 | {
|
---|
298 | LogRel(("Pulse: Cannot connect record stream: %s\n",
|
---|
299 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
300 | goto disconnect_unlock_and_fail;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | else
|
---|
304 | {
|
---|
305 | LogRel(("Pulse: Requested playback buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
|
---|
306 | pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
|
---|
307 |
|
---|
308 | if (pa_stream_connect_playback(pStream, /*dev=*/NULL, pBufAttr, flags,
|
---|
309 | /*cvolume=*/NULL, /*sync_stream=*/NULL) < 0)
|
---|
310 | {
|
---|
311 | LogRel(("Pulse: Cannot connect playback stream: %s\n",
|
---|
312 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
313 | goto disconnect_unlock_and_fail;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* Wait until the stream is ready */
|
---|
318 | for (;;)
|
---|
319 | {
|
---|
320 | pa_stream_state_t sstate;
|
---|
321 | pa_threaded_mainloop_wait(g_pMainLoop);
|
---|
322 |
|
---|
323 | sstate = pa_stream_get_state(pStream);
|
---|
324 | if (sstate == PA_STREAM_READY)
|
---|
325 | break;
|
---|
326 | else if (sstate == PA_STREAM_FAILED || sstate == PA_STREAM_TERMINATED)
|
---|
327 | {
|
---|
328 | LogRel(("Pulse: Failed to initialize stream (state %d)\n", sstate));
|
---|
329 | goto disconnect_unlock_and_fail;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | pBufAttrObtained = pa_stream_get_buffer_attr(pStream);
|
---|
334 | memcpy(pBufAttr, pBufAttrObtained, sizeof(pa_buffer_attr));
|
---|
335 |
|
---|
336 | if (fIn)
|
---|
337 | {
|
---|
338 | LogRel(("Pulse: Obtained record buffer attributes: maxlength=%d fragsize=%d\n",
|
---|
339 | pBufAttr->maxlength, pBufAttr->fragsize));
|
---|
340 | }
|
---|
341 | else
|
---|
342 | {
|
---|
343 | LogRel(("Pulse: Obtained playback buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
|
---|
344 | pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
|
---|
345 | }
|
---|
346 |
|
---|
347 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
348 | *ppStream = pStream;
|
---|
349 | return 0;
|
---|
350 |
|
---|
351 | disconnect_unlock_and_fail:
|
---|
352 | pa_stream_disconnect(pStream);
|
---|
353 |
|
---|
354 | unlock_and_fail:
|
---|
355 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
356 |
|
---|
357 | fail:
|
---|
358 | if (pStream)
|
---|
359 | pa_stream_unref(pStream);
|
---|
360 |
|
---|
361 | *ppStream = NULL;
|
---|
362 | return -1;
|
---|
363 | }
|
---|
364 |
|
---|
365 | static int pulse_init_out (HWVoiceOut *hw, audsettings_t *as)
|
---|
366 | {
|
---|
367 | PulseVoice *pPulse = (PulseVoice *) hw;
|
---|
368 | audsettings_t obt_as;
|
---|
369 | int cbBuf;
|
---|
370 |
|
---|
371 | pPulse->pDrainOp = NULL;
|
---|
372 |
|
---|
373 | pPulse->SampleSpec.format = aud_to_pulsefmt (as->fmt);
|
---|
374 | pPulse->SampleSpec.rate = as->freq;
|
---|
375 | pPulse->SampleSpec.channels = as->nchannels;
|
---|
376 |
|
---|
377 | /* Note that setting maxlength to -1 does not work on PulseAudio servers
|
---|
378 | * older than 0.9.10. So use the suggested value of 3/2 of tlength */
|
---|
379 | pPulse->BufAttr.tlength = (pa_bytes_per_second(&pPulse->SampleSpec)
|
---|
380 | * conf.buffer_msecs_out) / 1000;
|
---|
381 | pPulse->BufAttr.maxlength = (pPulse->BufAttr.tlength * 3) / 2;
|
---|
382 | pPulse->BufAttr.prebuf = -1; /* Same as tlength */
|
---|
383 | pPulse->BufAttr.minreq = -1; /* Pulse should set something sensible for minreq on it's own */
|
---|
384 |
|
---|
385 | /* Notice that the struct BufAttr is updated to the obtained values after this call */
|
---|
386 | if (pulse_open (0, &pPulse->pStream, &pPulse->SampleSpec, &pPulse->BufAttr))
|
---|
387 | return -1;
|
---|
388 |
|
---|
389 | if (pulse_to_audfmt (pPulse->SampleSpec.format, &obt_as.fmt, &obt_as.endianness))
|
---|
390 | {
|
---|
391 | LogRel(("Pulse: Cannot find audio format %d\n", pPulse->SampleSpec.format));
|
---|
392 | return -1;
|
---|
393 | }
|
---|
394 |
|
---|
395 | obt_as.freq = pPulse->SampleSpec.rate;
|
---|
396 | obt_as.nchannels = pPulse->SampleSpec.channels;
|
---|
397 |
|
---|
398 | audio_pcm_init_info (&hw->info, &obt_as);
|
---|
399 | cbBuf = audio_MIN(pPulse->BufAttr.tlength * 2, pPulse->BufAttr.maxlength);
|
---|
400 |
|
---|
401 | pPulse->pPCMBuf = RTMemAllocZ(cbBuf);
|
---|
402 | if (!pPulse->pPCMBuf)
|
---|
403 | {
|
---|
404 | LogRel(("Pulse: Could not allocate DAC buffer of %d bytes\n", cbBuf));
|
---|
405 | return -1;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /* Convert from bytes to frames (aka samples) */
|
---|
409 | hw->samples = cbBuf >> hw->info.shift;
|
---|
410 |
|
---|
411 | return 0;
|
---|
412 | }
|
---|
413 |
|
---|
414 | static void pulse_fini_out (HWVoiceOut *hw)
|
---|
415 | {
|
---|
416 | PulseVoice *pPulse = (PulseVoice *)hw;
|
---|
417 |
|
---|
418 | if (pPulse->pStream)
|
---|
419 | {
|
---|
420 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
421 | pa_stream_disconnect(pPulse->pStream);
|
---|
422 | pa_stream_unref(pPulse->pStream);
|
---|
423 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
424 | pPulse->pStream = NULL;
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (pPulse->pPCMBuf)
|
---|
428 | {
|
---|
429 | RTMemFree (pPulse->pPCMBuf);
|
---|
430 | pPulse->pPCMBuf = NULL;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | static int pulse_run_out (HWVoiceOut *hw)
|
---|
435 | {
|
---|
436 | PulseVoice *pPulse = (PulseVoice *) hw;
|
---|
437 | int cFramesLive;
|
---|
438 | int cFramesWritten = 0;
|
---|
439 | int csSamples;
|
---|
440 | int cFramesToWrite;
|
---|
441 | int cFramesAvail;
|
---|
442 | size_t cbAvail;
|
---|
443 | size_t cbToWrite;
|
---|
444 | uint8_t *pu8Dst;
|
---|
445 | st_sample_t *psSrc;
|
---|
446 |
|
---|
447 | cFramesLive = audio_pcm_hw_get_live_out (hw);
|
---|
448 | if (!cFramesLive)
|
---|
449 | return 0;
|
---|
450 |
|
---|
451 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
452 |
|
---|
453 | cbAvail = pa_stream_writable_size (pPulse->pStream);
|
---|
454 | if (cbAvail == (size_t)-1)
|
---|
455 | {
|
---|
456 | if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
|
---|
457 | {
|
---|
458 | int rc = pa_context_errno(g_pContext);
|
---|
459 | pPulse->cErrors++;
|
---|
460 | LogRel(("Pulse: Failed to determine the writable size: %s\n",
|
---|
461 | pa_strerror(rc)));
|
---|
462 | }
|
---|
463 | goto unlock_and_exit;
|
---|
464 | }
|
---|
465 |
|
---|
466 | cFramesAvail = cbAvail >> hw->info.shift; /* bytes => samples */
|
---|
467 | cFramesWritten = audio_MIN (cFramesLive, cFramesAvail);
|
---|
468 | csSamples = cFramesWritten;
|
---|
469 |
|
---|
470 | while (csSamples)
|
---|
471 | {
|
---|
472 | /* split request at the end of our samples buffer */
|
---|
473 | cFramesToWrite = audio_MIN (csSamples, hw->samples - hw->rpos);
|
---|
474 | cbToWrite = cFramesToWrite << hw->info.shift;
|
---|
475 | psSrc = hw->mix_buf + hw->rpos;
|
---|
476 | pu8Dst = advance (pPulse->pPCMBuf, hw->rpos << hw->info.shift);
|
---|
477 |
|
---|
478 | hw->clip (pu8Dst, psSrc, cFramesToWrite);
|
---|
479 |
|
---|
480 | if (pa_stream_write (pPulse->pStream, pu8Dst, cbToWrite,
|
---|
481 | /*cleanup_callback=*/NULL, 0, PA_SEEK_RELATIVE) < 0)
|
---|
482 | {
|
---|
483 | LogRel(("Pulse: Failed to write %d samples: %s\n",
|
---|
484 | cFramesToWrite, pa_strerror(pa_context_errno(g_pContext))));
|
---|
485 | break;
|
---|
486 | }
|
---|
487 | hw->rpos = (hw->rpos + cFramesToWrite) % hw->samples;
|
---|
488 | csSamples -= cFramesToWrite;
|
---|
489 | }
|
---|
490 |
|
---|
491 | unlock_and_exit:
|
---|
492 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
493 |
|
---|
494 | return cFramesWritten;
|
---|
495 | }
|
---|
496 |
|
---|
497 | static int pulse_write (SWVoiceOut *sw, void *buf, int len)
|
---|
498 | {
|
---|
499 | return audio_pcm_sw_write (sw, buf, len);
|
---|
500 | }
|
---|
501 |
|
---|
502 | static int pulse_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
---|
503 | {
|
---|
504 | PulseVoice *pPulse = (PulseVoice *) hw;
|
---|
505 |
|
---|
506 | switch (cmd)
|
---|
507 | {
|
---|
508 | case VOICE_ENABLE:
|
---|
509 | /* Start audio output. */
|
---|
510 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
511 | if ( pPulse->pDrainOp
|
---|
512 | && pa_operation_get_state(pPulse->pDrainOp) != PA_OPERATION_DONE)
|
---|
513 | {
|
---|
514 | pa_operation_cancel(pPulse->pDrainOp);
|
---|
515 | pa_operation_unref(pPulse->pDrainOp);
|
---|
516 | pPulse->pDrainOp = NULL;
|
---|
517 | }
|
---|
518 | else
|
---|
519 | {
|
---|
520 | /* should return immediately */
|
---|
521 | pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 0,
|
---|
522 | stream_success_callback, pPulse));
|
---|
523 | }
|
---|
524 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
525 | break;
|
---|
526 |
|
---|
527 | case VOICE_DISABLE:
|
---|
528 | /* Pause audio output (the Pause bit of the AC97 x_CR register is set).
|
---|
529 | * Note that we must return immediately from here! */
|
---|
530 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
531 | if (!pPulse->pDrainOp)
|
---|
532 | {
|
---|
533 | /* should return immediately */
|
---|
534 | pulse_wait_for_operation(pa_stream_trigger(pPulse->pStream,
|
---|
535 | stream_success_callback, pPulse));
|
---|
536 | pPulse->pDrainOp = pa_stream_drain(pPulse->pStream,
|
---|
537 | stream_drain_callback, pPulse);
|
---|
538 | }
|
---|
539 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
540 | break;
|
---|
541 |
|
---|
542 | default:
|
---|
543 | return -1;
|
---|
544 | }
|
---|
545 | return 0;
|
---|
546 | }
|
---|
547 |
|
---|
548 | static int pulse_init_in (HWVoiceIn *hw, audsettings_t *as)
|
---|
549 | {
|
---|
550 | PulseVoice *pPulse = (PulseVoice *) hw;
|
---|
551 | audsettings_t obt_as;
|
---|
552 |
|
---|
553 | pPulse->SampleSpec.format = aud_to_pulsefmt (as->fmt);
|
---|
554 | pPulse->SampleSpec.rate = as->freq;
|
---|
555 | pPulse->SampleSpec.channels = as->nchannels;
|
---|
556 |
|
---|
557 | /* XXX check these values */
|
---|
558 | pPulse->BufAttr.fragsize = (pa_bytes_per_second(&pPulse->SampleSpec)
|
---|
559 | * conf.buffer_msecs_in) / 1000;
|
---|
560 | pPulse->BufAttr.maxlength = (pPulse->BufAttr.fragsize * 3) / 2;
|
---|
561 | /* Other members of pa_buffer_attr are ignored for record streams */
|
---|
562 |
|
---|
563 | if (pulse_open (1, &pPulse->pStream, &pPulse->SampleSpec, &pPulse->BufAttr))
|
---|
564 | return -1;
|
---|
565 |
|
---|
566 | if (pulse_to_audfmt (pPulse->SampleSpec.format, &obt_as.fmt, &obt_as.endianness))
|
---|
567 | {
|
---|
568 | LogRel(("Pulse: Cannot find audio format %d\n", pPulse->SampleSpec.format));
|
---|
569 | return -1;
|
---|
570 | }
|
---|
571 |
|
---|
572 | obt_as.freq = pPulse->SampleSpec.rate;
|
---|
573 | obt_as.nchannels = pPulse->SampleSpec.channels;
|
---|
574 | audio_pcm_init_info (&hw->info, &obt_as);
|
---|
575 | hw->samples = audio_MIN(pPulse->BufAttr.fragsize * 10, pPulse->BufAttr.maxlength)
|
---|
576 | >> hw->info.shift;
|
---|
577 | pPulse->pu8PeekBuf = NULL;
|
---|
578 |
|
---|
579 | return 0;
|
---|
580 | }
|
---|
581 |
|
---|
582 | static void pulse_fini_in (HWVoiceIn *hw)
|
---|
583 | {
|
---|
584 | PulseVoice *pPulse = (PulseVoice *)hw;
|
---|
585 |
|
---|
586 | if (pPulse->pStream)
|
---|
587 | {
|
---|
588 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
589 | pa_stream_disconnect(pPulse->pStream);
|
---|
590 | pa_stream_unref(pPulse->pStream);
|
---|
591 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
592 | pPulse->pStream = NULL;
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | static int pulse_run_in (HWVoiceIn *hw)
|
---|
597 | {
|
---|
598 | PulseVoice *pPulse = (PulseVoice *) hw;
|
---|
599 | const int hwshift = hw->info.shift;
|
---|
600 | int cFramesRead = 0; /* total frames which have been read this call */
|
---|
601 | int cFramesAvail; /* total frames available from pulse at start of call */
|
---|
602 | int cFramesToRead; /* the largest amount we want/can get this call */
|
---|
603 | int cFramesToPeek; /* the largest amount we want/can get this peek */
|
---|
604 |
|
---|
605 | /* We should only call pa_stream_readable_size() once and trust the first value */
|
---|
606 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
607 | cFramesAvail = pa_stream_readable_size(pPulse->pStream) >> hwshift;
|
---|
608 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
609 |
|
---|
610 | if (cFramesAvail == -1)
|
---|
611 | {
|
---|
612 | if (pPulse->cErrors < MAX_LOG_REL_ERRORS)
|
---|
613 | {
|
---|
614 | int rc = pa_context_errno(g_pContext);
|
---|
615 | pPulse->cErrors++;
|
---|
616 | LogRel(("Pulse: Failed to determine the readable size: %s\n",
|
---|
617 | pa_strerror(rc)));
|
---|
618 | }
|
---|
619 | return 0;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /* If the buffer was not dropped last call, add what remains */
|
---|
623 | if (pPulse->pu8PeekBuf)
|
---|
624 | cFramesAvail += (pPulse->cbPeekBuf - pPulse->offPeekBuf) >> hwshift;
|
---|
625 |
|
---|
626 | cFramesToRead = audio_MIN(cFramesAvail, hw->samples - audio_pcm_hw_get_live_in(hw));
|
---|
627 | for (; cFramesToRead; cFramesToRead -= cFramesToPeek)
|
---|
628 | {
|
---|
629 | /* If there is no data, do another peek */
|
---|
630 | if (!pPulse->pu8PeekBuf)
|
---|
631 | {
|
---|
632 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
633 | pa_stream_peek(pPulse->pStream, (const void**)&pPulse->pu8PeekBuf, &pPulse->cbPeekBuf);
|
---|
634 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
635 | pPulse->offPeekBuf = 0;
|
---|
636 | if ( !pPulse->pu8PeekBuf
|
---|
637 | || !pPulse->cbPeekBuf)
|
---|
638 | break;
|
---|
639 | }
|
---|
640 |
|
---|
641 | cFramesToPeek = audio_MIN((signed)( pPulse->cbPeekBuf
|
---|
642 | - pPulse->offPeekBuf) >> hwshift,
|
---|
643 | cFramesToRead);
|
---|
644 |
|
---|
645 | /* Check for wrapping around the buffer end */
|
---|
646 | if (hw->wpos + cFramesToPeek > hw->samples)
|
---|
647 | {
|
---|
648 | int cFramesDelta = hw->samples - hw->wpos;
|
---|
649 |
|
---|
650 | hw->conv(hw->conv_buf + hw->wpos,
|
---|
651 | pPulse->pu8PeekBuf + pPulse->offPeekBuf,
|
---|
652 | cFramesDelta,
|
---|
653 | &nominal_volume);
|
---|
654 |
|
---|
655 | hw->conv(hw->conv_buf,
|
---|
656 | pPulse->pu8PeekBuf + pPulse->offPeekBuf + (cFramesDelta << hwshift),
|
---|
657 | cFramesToPeek - cFramesDelta,
|
---|
658 | &nominal_volume);
|
---|
659 | }
|
---|
660 | else
|
---|
661 | {
|
---|
662 | hw->conv(hw->conv_buf + hw->wpos,
|
---|
663 | pPulse->pu8PeekBuf + pPulse->offPeekBuf,
|
---|
664 | cFramesToPeek,
|
---|
665 | &nominal_volume);
|
---|
666 | }
|
---|
667 |
|
---|
668 | cFramesRead += cFramesToPeek;
|
---|
669 | hw->wpos = (hw->wpos + cFramesToPeek) % hw->samples;
|
---|
670 | pPulse->offPeekBuf += cFramesToPeek << hwshift;
|
---|
671 |
|
---|
672 | /* If the buffer is done, drop it */
|
---|
673 | if (pPulse->offPeekBuf == pPulse->cbPeekBuf)
|
---|
674 | {
|
---|
675 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
676 | pa_stream_drop(pPulse->pStream);
|
---|
677 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
678 | pPulse->pu8PeekBuf = NULL;
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | return cFramesRead;
|
---|
683 | }
|
---|
684 |
|
---|
685 | static int pulse_read (SWVoiceIn *sw, void *buf, int size)
|
---|
686 | {
|
---|
687 | return audio_pcm_sw_read (sw, buf, size);
|
---|
688 | }
|
---|
689 |
|
---|
690 | static int pulse_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
---|
691 | {
|
---|
692 | PulseVoice *pPulse = (PulseVoice *)hw;
|
---|
693 |
|
---|
694 | switch (cmd)
|
---|
695 | {
|
---|
696 | case VOICE_ENABLE:
|
---|
697 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
698 | /* should return immediately */
|
---|
699 | pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 0,
|
---|
700 | stream_success_callback, pPulse));
|
---|
701 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
702 | break;
|
---|
703 |
|
---|
704 | case VOICE_DISABLE:
|
---|
705 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
706 | if (pPulse->pu8PeekBuf)
|
---|
707 | {
|
---|
708 | pa_stream_drop(pPulse->pStream);
|
---|
709 | pPulse->pu8PeekBuf = NULL;
|
---|
710 | }
|
---|
711 | /* should return immediately */
|
---|
712 | pulse_wait_for_operation(pa_stream_cork(pPulse->pStream, 1,
|
---|
713 | stream_success_callback, pPulse));
|
---|
714 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
715 | break;
|
---|
716 |
|
---|
717 | default:
|
---|
718 | return -1;
|
---|
719 | }
|
---|
720 | return 0;
|
---|
721 | }
|
---|
722 |
|
---|
723 | static void *pulse_audio_init (void)
|
---|
724 | {
|
---|
725 | int rc;
|
---|
726 |
|
---|
727 | rc = audioLoadPulseLib();
|
---|
728 | if (RT_FAILURE(rc))
|
---|
729 | {
|
---|
730 | LogRel(("Pulse: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
|
---|
731 | return NULL;
|
---|
732 | }
|
---|
733 |
|
---|
734 | if (!(g_pMainLoop = pa_threaded_mainloop_new()))
|
---|
735 | {
|
---|
736 | LogRel(("Pulse: Failed to allocate main loop: %s\n",
|
---|
737 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
738 | goto fail;
|
---|
739 | }
|
---|
740 |
|
---|
741 | if (!(g_pContext = pa_context_new(pa_threaded_mainloop_get_api(g_pMainLoop), "VBox")))
|
---|
742 | {
|
---|
743 | LogRel(("Pulse: Failed to allocate context: %s\n",
|
---|
744 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
745 | goto fail;
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (pa_threaded_mainloop_start(g_pMainLoop) < 0)
|
---|
749 | {
|
---|
750 | LogRel(("Pulse: Failed to start threaded mainloop: %s\n",
|
---|
751 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
752 | goto fail;
|
---|
753 | }
|
---|
754 |
|
---|
755 | pa_context_set_state_callback(g_pContext, context_state_callback, NULL);
|
---|
756 | pa_threaded_mainloop_lock(g_pMainLoop);
|
---|
757 |
|
---|
758 | if (pa_context_connect(g_pContext, /*server=*/NULL, 0, NULL) < 0)
|
---|
759 | {
|
---|
760 | LogRel(("Pulse: Failed to connect to server: %s\n",
|
---|
761 | pa_strerror(pa_context_errno(g_pContext))));
|
---|
762 | goto unlock_and_fail;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /* Wait until the g_pContext is ready */
|
---|
766 | for (;;)
|
---|
767 | {
|
---|
768 | pa_context_state_t cstate;
|
---|
769 | pa_threaded_mainloop_wait(g_pMainLoop);
|
---|
770 | cstate = pa_context_get_state(g_pContext);
|
---|
771 | if (cstate == PA_CONTEXT_READY)
|
---|
772 | break;
|
---|
773 | else if (cstate == PA_CONTEXT_TERMINATED || cstate == PA_CONTEXT_FAILED)
|
---|
774 | {
|
---|
775 | LogRel(("Pulse: Failed to initialize context (state %d)\n", cstate));
|
---|
776 | goto unlock_and_fail;
|
---|
777 | }
|
---|
778 | }
|
---|
779 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
780 |
|
---|
781 | return &conf;
|
---|
782 |
|
---|
783 | unlock_and_fail:
|
---|
784 | if (g_pMainLoop)
|
---|
785 | pa_threaded_mainloop_unlock(g_pMainLoop);
|
---|
786 |
|
---|
787 | fail:
|
---|
788 | if (g_pMainLoop)
|
---|
789 | pa_threaded_mainloop_stop(g_pMainLoop);
|
---|
790 |
|
---|
791 | if (g_pContext)
|
---|
792 | {
|
---|
793 | pa_context_disconnect(g_pContext);
|
---|
794 | pa_context_unref(g_pContext);
|
---|
795 | g_pContext = NULL;
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (g_pMainLoop)
|
---|
799 | {
|
---|
800 | pa_threaded_mainloop_free(g_pMainLoop);
|
---|
801 | g_pMainLoop = NULL;
|
---|
802 | }
|
---|
803 |
|
---|
804 | return NULL;
|
---|
805 | }
|
---|
806 |
|
---|
807 | static void pulse_audio_fini (void *opaque)
|
---|
808 | {
|
---|
809 | if (g_pMainLoop)
|
---|
810 | pa_threaded_mainloop_stop(g_pMainLoop);
|
---|
811 |
|
---|
812 | if (g_pContext)
|
---|
813 | {
|
---|
814 | pa_context_disconnect(g_pContext);
|
---|
815 | pa_context_unref(g_pContext);
|
---|
816 | g_pContext = NULL;
|
---|
817 | }
|
---|
818 |
|
---|
819 | if (g_pMainLoop)
|
---|
820 | {
|
---|
821 | pa_threaded_mainloop_free(g_pMainLoop);
|
---|
822 | g_pMainLoop = NULL;
|
---|
823 | }
|
---|
824 |
|
---|
825 | (void) opaque;
|
---|
826 | }
|
---|
827 |
|
---|
828 | static struct audio_option pulse_options[] =
|
---|
829 | {
|
---|
830 | {"DAC_MS", AUD_OPT_INT, &conf.buffer_msecs_out,
|
---|
831 | "DAC period size in milliseconds", NULL, 0},
|
---|
832 | {"ADC_MS", AUD_OPT_INT, &conf.buffer_msecs_in,
|
---|
833 | "ADC period size in milliseconds", NULL, 0},
|
---|
834 | {NULL, 0, NULL, NULL, NULL, 0}
|
---|
835 | };
|
---|
836 |
|
---|
837 | static struct audio_pcm_ops pulse_pcm_ops =
|
---|
838 | {
|
---|
839 | pulse_init_out,
|
---|
840 | pulse_fini_out,
|
---|
841 | pulse_run_out,
|
---|
842 | pulse_write,
|
---|
843 | pulse_ctl_out,
|
---|
844 |
|
---|
845 | pulse_init_in,
|
---|
846 | pulse_fini_in,
|
---|
847 | pulse_run_in,
|
---|
848 | pulse_read,
|
---|
849 | pulse_ctl_in
|
---|
850 | };
|
---|
851 |
|
---|
852 | struct audio_driver pulse_audio_driver =
|
---|
853 | {
|
---|
854 | INIT_FIELD (name = ) "pulse",
|
---|
855 | INIT_FIELD (descr = ) "PulseAudio http://www.pulseaudio.org",
|
---|
856 | INIT_FIELD (options = ) pulse_options,
|
---|
857 | INIT_FIELD (init = ) pulse_audio_init,
|
---|
858 | INIT_FIELD (fini = ) pulse_audio_fini,
|
---|
859 | INIT_FIELD (pcm_ops = ) &pulse_pcm_ops,
|
---|
860 | INIT_FIELD (can_be_default = ) 1,
|
---|
861 | INIT_FIELD (max_voices_out = ) INT_MAX,
|
---|
862 | INIT_FIELD (max_voices_in = ) INT_MAX,
|
---|
863 | INIT_FIELD (voice_size_out = ) sizeof (PulseVoice),
|
---|
864 | INIT_FIELD (voice_size_in = ) sizeof (PulseVoice)
|
---|
865 | };
|
---|