VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostPulseAudio.cpp@ 65744

Last change on this file since 65744 was 65744, checked in by vboxsync, 8 years ago

Logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.9 KB
Line 
1/* $Id: DrvHostPulseAudio.cpp 65744 2017-02-10 16:50:00Z vboxsync $ */
2/** @file
3 * VBox audio devices: Pulse Audio audio driver.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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_DRV_HOST_AUDIO
23#include <VBox/log.h>
24#include <VBox/vmm/pdmaudioifs.h>
25
26#include <stdio.h>
27
28#include <iprt/alloc.h>
29#include <iprt/mem.h>
30#include <iprt/uuid.h>
31
32RT_C_DECLS_BEGIN
33 #include "pulse_mangling.h"
34 #include "pulse_stubs.h"
35RT_C_DECLS_END
36
37#include <pulse/pulseaudio.h>
38
39#include "DrvAudio.h"
40#include "VBoxDD.h"
41
42
43/*********************************************************************************************************************************
44* Defines *
45*********************************************************************************************************************************/
46#define VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS 32 /** @todo Make this configurable thru driver options. */
47
48#ifndef PA_STREAM_NOFLAGS
49# define PA_STREAM_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
50#endif
51
52#ifndef PA_CONTEXT_NOFLAGS
53# define PA_CONTEXT_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
54#endif
55
56/** No flags specified. */
57#define PULSEAUDIOENUMCBFLAGS_NONE 0
58/** (Release) log found devices. */
59#define PULSEAUDIOENUMCBFLAGS_LOG RT_BIT(0)
60
61/** Makes DRVHOSTPULSEAUDIO out of PDMIHOSTAUDIO. */
62#define PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface) \
63 ( (PDRVHOSTPULSEAUDIO)((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTPULSEAUDIO, IHostAudio)) )
64
65
66/*********************************************************************************************************************************
67* Structures *
68*********************************************************************************************************************************/
69
70/**
71 * Host Pulse audio driver instance data.
72 * @implements PDMIAUDIOCONNECTOR
73 */
74typedef struct DRVHOSTPULSEAUDIO
75{
76 /** Pointer to the driver instance structure. */
77 PPDMDRVINS pDrvIns;
78 /** Pointer to PulseAudio's threaded main loop. */
79 pa_threaded_mainloop *pMainLoop;
80 /**
81 * Pointer to our PulseAudio context.
82 * Note: We use a pMainLoop in a separate thread (pContext).
83 * So either use callback functions or protect these functions
84 * by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
85 */
86 pa_context *pContext;
87 /** Shutdown indicator. */
88 volatile bool fAbortLoop;
89 /** Pointer to host audio interface. */
90 PDMIHOSTAUDIO IHostAudio;
91 /** Error count for not flooding the release log.
92 * Specify UINT32_MAX for unlimited logging. */
93 uint32_t cLogErrors;
94} DRVHOSTPULSEAUDIO, *PDRVHOSTPULSEAUDIO;
95
96typedef struct PULSEAUDIOSTREAM
97{
98 /** The stream's acquired configuration. */
99 PPDMAUDIOSTREAMCFG pCfg;
100 /** Pointer to driver instance. */
101 PDRVHOSTPULSEAUDIO pDrv;
102 /** Pointer to opaque PulseAudio stream. */
103 pa_stream *pStream;
104 /** Pulse sample format and attribute specification. */
105 pa_sample_spec SampleSpec;
106 /** Pulse playback and buffer metrics. */
107 pa_buffer_attr BufAttr;
108 int fOpSuccess;
109 /** Pointer to Pulse sample peeking buffer. */
110 const uint8_t *pu8PeekBuf;
111 /** Current size (in bytes) of peeking data in
112 * buffer. */
113 size_t cbPeekBuf;
114 /** Our offset (in bytes) in peeking buffer. */
115 size_t offPeekBuf;
116 pa_operation *pDrainOp;
117} PULSEAUDIOSTREAM, *PPULSEAUDIOSTREAM;
118
119/* The desired buffer length in milliseconds. Will be the target total stream
120 * latency on newer version of pulse. Apparent latency can be less (or more.)
121 */
122typedef struct PULSEAUDIOCFG
123{
124 RTMSINTERVAL buffer_msecs_out;
125 RTMSINTERVAL buffer_msecs_in;
126} PULSEAUDIOCFG, *PPULSEAUDIOCFG;
127
128static PULSEAUDIOCFG s_pulseCfg =
129{
130 100, /* buffer_msecs_out */
131 100 /* buffer_msecs_in */
132};
133
134/**
135 * Callback context for server enumeration callbacks.
136 */
137typedef struct PULSEAUDIOENUMCBCTX
138{
139 /** Pointer to host backend driver. */
140 PDRVHOSTPULSEAUDIO pDrv;
141 /** Enumeration flags. */
142 uint32_t fFlags;
143 /** Number of found input devices. */
144 uint8_t cDevIn;
145 /** Number of found output devices. */
146 uint8_t cDevOut;
147 /** Name of default sink being used. Must be free'd using RTStrFree(). */
148 char *pszDefaultSink;
149 /** Name of default source being used. Must be free'd using RTStrFree(). */
150 char *pszDefaultSource;
151} PULSEAUDIOENUMCBCTX, *PPULSEAUDIOENUMCBCTX;
152
153#ifndef PA_CONTEXT_IS_GOOD /* To allow running on systems with PulseAudio < 0.9.11. */
154static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
155 return
156 x == PA_CONTEXT_CONNECTING ||
157 x == PA_CONTEXT_AUTHORIZING ||
158 x == PA_CONTEXT_SETTING_NAME ||
159 x == PA_CONTEXT_READY;
160}
161#endif /* !PA_CONTEXT_IS_GOOD */
162
163#ifndef PA_STREAM_IS_GOOD /* To allow running on systems with PulseAudio < 0.9.11. */
164static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
165 return
166 x == PA_STREAM_CREATING ||
167 x == PA_STREAM_READY;
168}
169#endif /* !PA_STREAM_IS_GOOD */
170
171/*********************************************************************************************************************************
172* Prototypes *
173*********************************************************************************************************************************/
174
175static int paEnumerate(PDRVHOSTPULSEAUDIO pThis, PPDMAUDIOBACKENDCFG pCfg, uint32_t fEnum);
176static int paError(PDRVHOSTPULSEAUDIO pThis, const char *szMsg);
177static void paStreamCbSuccess(pa_stream *pStream, int fSuccess, void *pvContext);
178
179
180
181/**
182 * Signal the main loop to abort. Just signalling isn't sufficient as the
183 * mainloop might not have been entered yet.
184 */
185static void paSignalWaiter(PDRVHOSTPULSEAUDIO pThis)
186{
187 if (!pThis)
188 {
189// LogRel(("DEBUG: paSignalWaiter return because of !pThis\n"));
190 return;
191 }
192
193// LogRel(("DEBUG: paSignalWaiter set fLoopWait=true and calling pa_threaded_mainloop_signal()\n"));
194 pThis->fAbortLoop = true;
195 pa_threaded_mainloop_signal(pThis->pMainLoop, 0);
196}
197
198
199static pa_sample_format_t paAudioPropsToPulse(PPDMAUDIOPCMPROPS pProps)
200{
201 switch (pProps->cBits)
202 {
203 case 8:
204 if (!pProps->fSigned)
205 return PA_SAMPLE_U8;
206 break;
207
208 case 16:
209 if (pProps->fSigned)
210 return PA_SAMPLE_S16LE;
211 break;
212
213#ifdef PA_SAMPLE_S32LE
214 case 32:
215 if (pProps->fSigned)
216 return PA_SAMPLE_S32LE;
217 break;
218#endif
219
220 default:
221 break;
222 }
223
224 AssertMsgFailed(("%RU8%s not supported\n", pProps->cBits, pProps->fSigned ? "S" : "U"));
225 return PA_SAMPLE_INVALID;
226}
227
228
229static int paPulseToAudioProps(pa_sample_format_t pulsefmt, PPDMAUDIOPCMPROPS pProps)
230{
231 switch (pulsefmt)
232 {
233 case PA_SAMPLE_U8:
234 pProps->cBits = 8;
235 pProps->fSigned = false;
236 break;
237
238 case PA_SAMPLE_S16LE:
239 pProps->cBits = 16;
240 pProps->fSigned = true;
241 break;
242
243 case PA_SAMPLE_S16BE:
244 pProps->cBits = 16;
245 pProps->fSigned = true;
246 /** @todo Handle Endianess. */
247 break;
248
249#ifdef PA_SAMPLE_S32LE
250 case PA_SAMPLE_S32LE:
251 pProps->cBits = 32;
252 pProps->fSigned = true;
253 break;
254#endif
255
256#ifdef PA_SAMPLE_S32BE
257 case PA_SAMPLE_S32BE:
258 pProps->cBits = 32;
259 pProps->fSigned = true;
260 /** @todo Handle Endianess. */
261 break;
262#endif
263
264 default:
265 AssertLogRelMsg(("PulseAudio: Format (%ld) not supported\n", pulsefmt));
266 return VERR_NOT_SUPPORTED;
267 }
268
269 return VINF_SUCCESS;
270}
271
272
273/**
274 * Synchronously wait until an operation completed.
275 */
276static int paWaitForEx(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOP, RTMSINTERVAL cMsTimeout)
277{
278 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
279 AssertPtrReturn(pOP, VERR_INVALID_POINTER);
280
281 int rc = VINF_SUCCESS;
282
283 uint64_t u64StartMs = RTTimeMilliTS();
284 while (pa_operation_get_state(pOP) == PA_OPERATION_RUNNING)
285 {
286 if (!pThis->fAbortLoop)
287 {
288 AssertPtr(pThis->pMainLoop);
289 pa_threaded_mainloop_wait(pThis->pMainLoop);
290 }
291 pThis->fAbortLoop = false;
292
293 uint64_t u64ElapsedMs = RTTimeMilliTS() - u64StartMs;
294 if (u64ElapsedMs >= cMsTimeout)
295 {
296 rc = VERR_TIMEOUT;
297 break;
298 }
299// LogRel(("DEBUG: paWaitForEx next turn (elapsed=%RU64ms)\n", u64ElapsedMs));
300 }
301
302 pa_operation_unref(pOP);
303
304 return rc;
305}
306
307
308static int paWaitFor(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOP)
309{
310 return paWaitForEx(pThis, pOP, 10 * 1000 /* 10s timeout */);
311}
312
313
314/**
315 * Context status changed.
316 */
317static void paContextCbStateChanged(pa_context *pCtx, void *pvUser)
318{
319 AssertPtrReturnVoid(pCtx);
320
321 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
322 AssertPtrReturnVoid(pThis);
323
324 switch (pa_context_get_state(pCtx))
325 {
326 case PA_CONTEXT_READY:
327 case PA_CONTEXT_TERMINATED:
328 paSignalWaiter(pThis);
329 break;
330
331 case PA_CONTEXT_FAILED:
332 LogRel(("PulseAudio: Audio context has failed, stopping\n"));
333 paSignalWaiter(pThis);
334 break;
335
336 default:
337 break;
338 }
339}
340
341
342/**
343 * Callback called when our pa_stream_drain operation was completed.
344 */
345static void paStreamCbDrain(pa_stream *pStream, int fSuccess, void *pvUser)
346{
347 AssertPtrReturnVoid(pStream);
348
349 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvUser;
350 AssertPtrReturnVoid(pStrm);
351
352 pStrm->fOpSuccess = fSuccess;
353 if (fSuccess)
354 {
355 pa_operation_unref(pa_stream_cork(pStream, 1,
356 paStreamCbSuccess, pvUser));
357 }
358 else
359 paError(pStrm->pDrv, "Failed to drain stream");
360
361 if (pStrm->pDrainOp)
362 {
363 pa_operation_unref(pStrm->pDrainOp);
364 pStrm->pDrainOp = NULL;
365 }
366}
367
368
369/**
370 * Stream status changed.
371 */
372static void paStreamCbStateChanged(pa_stream *pStream, void *pvUser)
373{
374 AssertPtrReturnVoid(pStream);
375
376 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
377 AssertPtrReturnVoid(pThis);
378
379 switch (pa_stream_get_state(pStream))
380 {
381 case PA_STREAM_READY:
382 case PA_STREAM_FAILED:
383 case PA_STREAM_TERMINATED:
384 paSignalWaiter(pThis);
385 break;
386
387 default:
388 break;
389 }
390}
391
392
393static void paStreamCbSuccess(pa_stream *pStream, int fSuccess, void *pvUser)
394{
395 AssertPtrReturnVoid(pStream);
396
397 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvUser;
398 AssertPtrReturnVoid(pStrm);
399
400 pStrm->fOpSuccess = fSuccess;
401
402 if (fSuccess)
403 paSignalWaiter(pStrm->pDrv);
404 else
405 paError(pStrm->pDrv, "Failed to finish stream operation");
406}
407
408
409static int paStreamOpen(PDRVHOSTPULSEAUDIO pThis, bool fIn, const char *pszName,
410 pa_sample_spec *pSampleSpec, pa_buffer_attr *pBufAttr,
411 pa_stream **ppStream)
412{
413 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
414 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
415 AssertPtrReturn(pSampleSpec, VERR_INVALID_POINTER);
416 AssertPtrReturn(pBufAttr, VERR_INVALID_POINTER);
417 AssertPtrReturn(ppStream, VERR_INVALID_POINTER);
418
419 if (!pa_sample_spec_valid(pSampleSpec))
420 {
421 LogRel(("PulseAudio: Unsupported sample specification for stream '%s'\n",
422 pszName));
423 return VERR_NOT_SUPPORTED;
424 }
425
426 int rc = VINF_SUCCESS;
427
428 pa_stream *pStream = NULL;
429 uint32_t flags = PA_STREAM_NOFLAGS;
430
431 LogFunc(("Opening '%s', rate=%dHz, channels=%d, format=%s\n",
432 pszName, pSampleSpec->rate, pSampleSpec->channels,
433 pa_sample_format_to_string(pSampleSpec->format)));
434
435 pa_threaded_mainloop_lock(pThis->pMainLoop);
436
437 do
438 {
439 /** @todo r=andy Use pa_stream_new_with_proplist instead. */
440 if (!(pStream = pa_stream_new(pThis->pContext, pszName, pSampleSpec,
441 NULL /* pa_channel_map */)))
442 {
443 LogRel(("PulseAudio: Could not create stream '%s'\n", pszName));
444 rc = VERR_NO_MEMORY;
445 break;
446 }
447
448 pa_stream_set_state_callback(pStream, paStreamCbStateChanged, pThis);
449
450#if PA_API_VERSION >= 12
451 /* XXX */
452 flags |= PA_STREAM_ADJUST_LATENCY;
453#endif
454
455#if 0
456 /* Not applicable as we don't use pa_stream_get_latency() and pa_stream_get_time(). */
457 flags |= PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
458#endif
459 /* No input/output right away after the stream was started. */
460 flags |= PA_STREAM_START_CORKED;
461
462 if (fIn)
463 {
464 LogFunc(("Input stream attributes: maxlength=%d fragsize=%d\n",
465 pBufAttr->maxlength, pBufAttr->fragsize));
466
467 if (pa_stream_connect_record(pStream, /*dev=*/NULL, pBufAttr, (pa_stream_flags_t)flags) < 0)
468 {
469 LogRel(("PulseAudio: Could not connect input stream '%s': %s\n",
470 pszName, pa_strerror(pa_context_errno(pThis->pContext))));
471 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
472 break;
473 }
474 }
475 else
476 {
477 LogFunc(("Output buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
478 pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
479
480 if (pa_stream_connect_playback(pStream, /*dev=*/NULL, pBufAttr, (pa_stream_flags_t)flags,
481 /*cvolume=*/NULL, /*sync_stream=*/NULL) < 0)
482 {
483 LogRel(("PulseAudio: Could not connect playback stream '%s': %s\n",
484 pszName, pa_strerror(pa_context_errno(pThis->pContext))));
485 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
486 break;
487 }
488 }
489
490 /* Wait until the stream is ready. */
491 for (;;)
492 {
493 if (!pThis->fAbortLoop)
494 pa_threaded_mainloop_wait(pThis->pMainLoop);
495 pThis->fAbortLoop = false;
496
497 pa_stream_state_t streamSt = pa_stream_get_state(pStream);
498 if (streamSt == PA_STREAM_READY)
499 break;
500 else if ( streamSt == PA_STREAM_FAILED
501 || streamSt == PA_STREAM_TERMINATED)
502 {
503 LogRel(("PulseAudio: Failed to initialize stream '%s' (state %ld)\n", pszName, streamSt));
504 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
505 break;
506 }
507 }
508
509 if (RT_FAILURE(rc))
510 break;
511
512 const pa_buffer_attr *pBufAttrObtained = pa_stream_get_buffer_attr(pStream);
513 AssertPtr(pBufAttrObtained);
514 memcpy(pBufAttr, pBufAttrObtained, sizeof(pa_buffer_attr));
515
516 if (fIn)
517 LogFunc(("Obtained record buffer attributes: maxlength=%RU32, fragsize=%RU32\n",
518 pBufAttr->maxlength, pBufAttr->fragsize));
519 else
520 LogFunc(("Obtained playback buffer attributes: maxlength=%d, tlength=%d, prebuf=%d, minreq=%d\n",
521 pBufAttr->maxlength, pBufAttr->tlength, pBufAttr->prebuf, pBufAttr->minreq));
522
523 } while (0);
524
525 if ( RT_FAILURE(rc)
526 && pStream)
527 pa_stream_disconnect(pStream);
528
529 pa_threaded_mainloop_unlock(pThis->pMainLoop);
530
531 if (RT_FAILURE(rc))
532 {
533 if (pStream)
534 pa_stream_unref(pStream);
535 }
536 else
537 *ppStream = pStream;
538
539 LogFlowFuncLeaveRC(rc);
540 return rc;
541}
542
543
544/**
545 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
546 */
547static DECLCALLBACK(int) drvHostPulseAudioInit(PPDMIHOSTAUDIO pInterface)
548{
549 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
550
551 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
552
553 LogFlowFuncEnter();
554
555 int rc = audioLoadPulseLib();
556 if (RT_FAILURE(rc))
557 {
558 LogRel(("PulseAudio: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
559 return rc;
560 }
561
562 pThis->fAbortLoop = false;
563 pThis->pMainLoop = NULL;
564
565 bool fLocked = false;
566
567 do
568 {
569 if (!(pThis->pMainLoop = pa_threaded_mainloop_new()))
570 {
571 LogRel(("PulseAudio: Failed to allocate main loop: %s\n",
572 pa_strerror(pa_context_errno(pThis->pContext))));
573 rc = VERR_NO_MEMORY;
574 break;
575 }
576
577 if (!(pThis->pContext = pa_context_new(pa_threaded_mainloop_get_api(pThis->pMainLoop), "VirtualBox")))
578 {
579 LogRel(("PulseAudio: Failed to allocate context: %s\n",
580 pa_strerror(pa_context_errno(pThis->pContext))));
581 rc = VERR_NO_MEMORY;
582 break;
583 }
584
585 if (pa_threaded_mainloop_start(pThis->pMainLoop) < 0)
586 {
587 LogRel(("PulseAudio: Failed to start threaded mainloop: %s\n",
588 pa_strerror(pa_context_errno(pThis->pContext))));
589 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
590 break;
591 }
592
593 /* Install a global callback to known if something happens to our acquired context. */
594 pa_context_set_state_callback(pThis->pContext, paContextCbStateChanged, pThis /* pvUserData */);
595
596 pa_threaded_mainloop_lock(pThis->pMainLoop);
597 fLocked = true;
598
599 if (pa_context_connect(pThis->pContext, NULL /* pszServer */,
600 PA_CONTEXT_NOFLAGS, NULL) < 0)
601 {
602 LogRel(("PulseAudio: Failed to connect to server: %s\n",
603 pa_strerror(pa_context_errno(pThis->pContext))));
604 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
605 break;
606 }
607
608 /* Wait until the pThis->pContext is ready. */
609 for (;;)
610 {
611 if (!pThis->fAbortLoop)
612 pa_threaded_mainloop_wait(pThis->pMainLoop);
613 pThis->fAbortLoop = false;
614
615 pa_context_state_t cstate = pa_context_get_state(pThis->pContext);
616 if (cstate == PA_CONTEXT_READY)
617 break;
618 else if ( cstate == PA_CONTEXT_TERMINATED
619 || cstate == PA_CONTEXT_FAILED)
620 {
621 LogRel(("PulseAudio: Failed to initialize context (state %d)\n", cstate));
622 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
623 break;
624 }
625 }
626 }
627 while (0);
628
629 if (fLocked)
630 pa_threaded_mainloop_unlock(pThis->pMainLoop);
631
632 if (RT_FAILURE(rc))
633 {
634 if (pThis->pMainLoop)
635 pa_threaded_mainloop_stop(pThis->pMainLoop);
636
637 if (pThis->pContext)
638 {
639 pa_context_disconnect(pThis->pContext);
640 pa_context_unref(pThis->pContext);
641 pThis->pContext = NULL;
642 }
643
644 if (pThis->pMainLoop)
645 {
646 pa_threaded_mainloop_free(pThis->pMainLoop);
647 pThis->pMainLoop = NULL;
648 }
649 }
650
651 LogFlowFuncLeaveRC(rc);
652 return rc;
653}
654
655
656static int paCreateStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA,
657 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
658{
659 pStreamPA->pDrainOp = NULL;
660
661 pStreamPA->SampleSpec.format = paAudioPropsToPulse(&pCfgReq->Props);
662 pStreamPA->SampleSpec.rate = pCfgReq->Props.uHz;
663 pStreamPA->SampleSpec.channels = pCfgReq->Props.cChannels;
664
665 /* Note that setting maxlength to -1 does not work on PulseAudio servers
666 * older than 0.9.10. So use the suggested value of 3/2 of tlength */
667 pStreamPA->BufAttr.tlength = (pa_bytes_per_second(&pStreamPA->SampleSpec)
668 * s_pulseCfg.buffer_msecs_out) / 1000;
669 pStreamPA->BufAttr.maxlength = (pStreamPA->BufAttr.tlength * 3) / 2;
670 pStreamPA->BufAttr.prebuf = -1; /* Same as tlength */
671 pStreamPA->BufAttr.minreq = -1;
672
673 /* Note that the struct BufAttr is updated to the obtained values after this call! */
674 int rc = paStreamOpen(pThis, false /* fIn */, "PulseAudio (Out)",
675 &pStreamPA->SampleSpec, &pStreamPA->BufAttr, &pStreamPA->pStream);
676 if (RT_FAILURE(rc))
677 return rc;
678
679 rc = paPulseToAudioProps(pStreamPA->SampleSpec.format, &pCfgAcq->Props);
680 if (RT_FAILURE(rc))
681 {
682 LogRel(("PulseAudio: Cannot find audio output format %ld\n", pStreamPA->SampleSpec.format));
683 return rc;
684 }
685
686 pCfgAcq->Props.uHz = pStreamPA->SampleSpec.rate;
687 pCfgAcq->Props.cChannels = pStreamPA->SampleSpec.channels;
688 pCfgAcq->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfgAcq->Props.cBits, pCfgAcq->Props.cChannels);
689
690 uint32_t cbBuf = RT_MIN(pStreamPA->BufAttr.tlength * 2,
691 pStreamPA->BufAttr.maxlength); /** @todo Make this configurable! */
692 if (cbBuf)
693 {
694 pCfgAcq->cSampleBufferHint = PDMAUDIOSTREAMCFG_B2S(pCfgAcq, cbBuf);
695
696 pStreamPA->pDrv = pThis;
697 }
698 else
699 rc = VERR_INVALID_PARAMETER;
700
701 return rc;
702}
703
704
705static int paCreateStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA,
706 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
707{
708 pStreamPA->SampleSpec.format = paAudioPropsToPulse(&pCfgReq->Props);
709 pStreamPA->SampleSpec.rate = pCfgReq->Props.uHz;
710 pStreamPA->SampleSpec.channels = pCfgReq->Props.cChannels;
711
712 /** @todo Check these values! */
713 pStreamPA->BufAttr.fragsize = (pa_bytes_per_second(&pStreamPA->SampleSpec) * s_pulseCfg.buffer_msecs_in) / 1000;
714 pStreamPA->BufAttr.maxlength = (pStreamPA->BufAttr.fragsize * 3) / 2;
715
716 /* Note: Other members of BufAttr are ignored for record streams. */
717 int rc = paStreamOpen(pThis, true /* fIn */, "PulseAudio (In)", &pStreamPA->SampleSpec, &pStreamPA->BufAttr,
718 &pStreamPA->pStream);
719 if (RT_FAILURE(rc))
720 return rc;
721
722 rc = paPulseToAudioProps(pStreamPA->SampleSpec.format, &pCfgAcq->Props);
723 if (RT_FAILURE(rc))
724 {
725 LogRel(("PulseAudio: Cannot find audio capture format %ld\n", pStreamPA->SampleSpec.format));
726 return rc;
727 }
728
729 pStreamPA->pDrv = pThis;
730 pStreamPA->pu8PeekBuf = NULL;
731
732 pCfgAcq->Props.uHz = pStreamPA->SampleSpec.rate;
733 pCfgAcq->Props.cChannels = pStreamPA->SampleSpec.channels;
734 pCfgAcq->cSampleBufferHint = PDMAUDIOSTREAMCFG_B2S(pCfgAcq,
735 RT_MIN(pStreamPA->BufAttr.fragsize * 10, pStreamPA->BufAttr.maxlength));
736
737 LogFlowFuncLeaveRC(rc);
738 return rc;
739}
740
741
742/**
743 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
744 */
745static DECLCALLBACK(int) drvHostPulseAudioStreamCapture(PPDMIHOSTAUDIO pInterface,
746 PPDMAUDIOBACKENDSTREAM pStream, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
747{
748 RT_NOREF(pvBuf, cbBuf);
749 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
750 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
751 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
752 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
753 /* pcbRead is optional. */
754
755 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
756 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
757
758 /* We should only call pa_stream_readable_size() once and trust the first value. */
759 pa_threaded_mainloop_lock(pThis->pMainLoop);
760 size_t cbAvail = pa_stream_readable_size(pStreamPA->pStream);
761 pa_threaded_mainloop_unlock(pThis->pMainLoop);
762
763 if (cbAvail == (size_t)-1)
764 return paError(pStreamPA->pDrv, "Failed to determine input data size");
765
766 /* If the buffer was not dropped last call, add what remains. */
767 if (pStreamPA->pu8PeekBuf)
768 {
769 Assert(pStreamPA->cbPeekBuf >= pStreamPA->offPeekBuf);
770 cbAvail += (pStreamPA->cbPeekBuf - pStreamPA->offPeekBuf);
771 }
772
773 Log3Func(("cbAvail=%zu\n", cbAvail));
774
775 if (!cbAvail) /* No data? Bail out. */
776 {
777 if (pcbRead)
778 *pcbRead = 0;
779 return VINF_SUCCESS;
780 }
781
782 int rc = VINF_SUCCESS;
783
784 size_t cbToRead = RT_MIN(cbAvail, cbBuf);
785
786 Log3Func(("cbToRead=%zu, cbAvail=%zu, offPeekBuf=%zu, cbPeekBuf=%zu\n",
787 cbToRead, cbAvail, pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf));
788
789 uint32_t cbReadTotal = 0;
790
791 while (cbToRead)
792 {
793 /* If there is no data, do another peek. */
794 if (!pStreamPA->pu8PeekBuf)
795 {
796 pa_threaded_mainloop_lock(pThis->pMainLoop);
797 pa_stream_peek(pStreamPA->pStream,
798 (const void**)&pStreamPA->pu8PeekBuf, &pStreamPA->cbPeekBuf);
799 pa_threaded_mainloop_unlock(pThis->pMainLoop);
800
801 pStreamPA->offPeekBuf = 0;
802
803 /* No data anymore?
804 * Note: If there's a data hole (cbPeekBuf then contains the length of the hole)
805 * we need to drop the stream lateron. */
806 if ( !pStreamPA->pu8PeekBuf
807 && !pStreamPA->cbPeekBuf)
808 {
809 break;
810 }
811 }
812
813 Assert(pStreamPA->cbPeekBuf >= pStreamPA->offPeekBuf);
814 size_t cbToWrite = RT_MIN(pStreamPA->cbPeekBuf - pStreamPA->offPeekBuf, cbToRead);
815
816 Log3Func(("cbToRead=%zu, cbToWrite=%zu, offPeekBuf=%zu, cbPeekBuf=%zu, pu8PeekBuf=%p\n",
817 cbToRead, cbToWrite,
818 pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf, pStreamPA->pu8PeekBuf));
819
820 if (cbToWrite)
821 {
822 memcpy((uint8_t *)pvBuf + cbReadTotal, pStreamPA->pu8PeekBuf + pStreamPA->offPeekBuf, cbToWrite);
823
824 Assert(cbToRead >= cbToWrite);
825 cbToRead -= cbToWrite;
826 cbReadTotal += cbToWrite;
827
828 pStreamPA->offPeekBuf += cbToWrite;
829 Assert(pStreamPA->offPeekBuf <= pStreamPA->cbPeekBuf);
830 }
831
832 if (/* Nothing to write anymore? Drop the buffer. */
833 !cbToWrite
834 /* Was there a hole in the peeking buffer? Drop it. */
835 || !pStreamPA->pu8PeekBuf
836 /* If the buffer is done, drop it. */
837 || pStreamPA->offPeekBuf == pStreamPA->cbPeekBuf)
838 {
839 pa_threaded_mainloop_lock(pThis->pMainLoop);
840 pa_stream_drop(pStreamPA->pStream);
841 pa_threaded_mainloop_unlock(pThis->pMainLoop);
842
843 pStreamPA->pu8PeekBuf = NULL;
844 }
845 }
846
847 if (RT_SUCCESS(rc))
848 {
849 if (pcbRead)
850 *pcbRead = cbReadTotal;
851 }
852
853 return rc;
854}
855
856
857/**
858 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
859 */
860static DECLCALLBACK(int) drvHostPulseAudioStreamPlay(PPDMIHOSTAUDIO pInterface,
861 PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cbBuf,
862 uint32_t *pcbWritten)
863{
864 RT_NOREF(pvBuf, cbBuf);
865 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
866 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
867 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
868 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
869 /* pcbWritten is optional. */
870
871 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
872 PPULSEAUDIOSTREAM pPAStream = (PPULSEAUDIOSTREAM)pStream;
873
874 int rc = VINF_SUCCESS;
875
876 uint32_t cbWrittenTotal = 0;
877
878 pa_threaded_mainloop_lock(pThis->pMainLoop);
879
880 do
881 {
882 size_t cbWriteable = pa_stream_writable_size(pPAStream->pStream);
883 if (cbWriteable == (size_t)-1)
884 {
885 rc = paError(pPAStream->pDrv, "Failed to determine output data size");
886 break;
887 }
888
889 size_t cbToWrite = RT_MIN(cbWriteable, cbBuf);
890
891 uint32_t cbWritten;
892 while (cbToWrite)
893 {
894 cbWritten = cbToWrite;
895 if (pa_stream_write(pPAStream->pStream, (uint8_t *)pvBuf + cbWrittenTotal, cbWritten, NULL /* Cleanup callback */,
896 0, PA_SEEK_RELATIVE) < 0)
897 {
898 rc = paError(pPAStream->pDrv, "Failed to write to output stream");
899 break;
900 }
901
902 Assert(cbToWrite >= cbWritten);
903 cbToWrite -= cbWritten;
904 cbWrittenTotal += cbWritten;
905 }
906
907 } while (0);
908
909 pa_threaded_mainloop_unlock(pThis->pMainLoop);
910
911 if (RT_SUCCESS(rc))
912 {
913 if (pcbWritten)
914 *pcbWritten = cbWrittenTotal;
915 }
916
917 return rc;
918}
919
920
921/** @todo Implement va handling. */
922static int paError(PDRVHOSTPULSEAUDIO pThis, const char *szMsg)
923{
924 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
925 AssertPtrReturn(szMsg, VERR_INVALID_POINTER);
926
927 if (pThis->cLogErrors++ < VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS)
928 {
929 int rc2 = pa_context_errno(pThis->pContext);
930 LogRel2(("PulseAudio: %s: %s\n", szMsg, pa_strerror(rc2)));
931 }
932
933 /** @todo Implement some PulseAudio -> IPRT mapping here. */
934 return VERR_GENERAL_FAILURE;
935}
936
937
938static void paEnumSinkCb(pa_context *pCtx, const pa_sink_info *pInfo, int eol, void *pvUserData)
939{
940 if (eol != 0)
941 {
942// LogRel(("DEBUG: paEnumSinkCb return EOL=1\n"));
943 return;
944 }
945 if (!pCtx)
946 LogRel(("DEBUG: paEnumSinkCb return because of !pCtx\n"));
947 AssertPtrReturnVoid(pCtx);
948 if (!pInfo)
949 LogRel(("DEBUG: paEnumSinkCb return because of !pInfo\n"));
950 AssertPtrReturnVoid(pInfo);
951
952 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
953 if (!pCbCtx)
954 LogRel(("DEBUG: paEnumSinkCb return because of !pCbCtx\n"));
955 AssertPtrReturnVoid(pCbCtx);
956 if (!pCbCtx->pDrv)
957 LogRel(("DEBUG: paEnumSinkCb return because of !pCbCtx->pDrv\n"));
958 AssertPtrReturnVoid(pCbCtx->pDrv);
959
960 LogRel2(("PulseAudio: Using output sink '%s'\n", pInfo->name));
961
962 /** @todo Store sinks + channel mapping in callback context as soon as we have surround support. */
963 pCbCtx->cDevOut++;
964
965// LogRel(("DEBUG: pa_threaded_mainloop_signal() from paEnumSinkCb\n"));
966 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
967}
968
969
970static void paEnumSourceCb(pa_context *pCtx, const pa_source_info *pInfo, int eol, void *pvUserData)
971{
972 if (eol != 0)
973 return;
974
975 AssertPtrReturnVoid(pCtx);
976 AssertPtrReturnVoid(pInfo);
977
978 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
979 AssertPtrReturnVoid(pCbCtx);
980 AssertPtrReturnVoid(pCbCtx->pDrv);
981
982 LogRel2(("PulseAudio: Using input source '%s'\n", pInfo->name));
983
984 /** @todo Store sources + channel mapping in callback context as soon as we have surround support. */
985 pCbCtx->cDevIn++;
986
987// LogRel(("DEBUG: pa_threaded_mainloop_signal() from paEnumSourceCb\n"));
988 pa_threaded_mainloop_signal(pCbCtx->pDrv->pMainLoop, 0);
989}
990
991
992static void paEnumServerCb(pa_context *pCtx, const pa_server_info *pInfo, void *pvUserData)
993{
994 AssertPtrReturnVoid(pCtx);
995 AssertPtrReturnVoid(pInfo);
996
997 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
998 AssertPtrReturnVoid(pCbCtx);
999
1000 PDRVHOSTPULSEAUDIO pThis = pCbCtx->pDrv;
1001 AssertPtrReturnVoid(pThis);
1002
1003 if (pInfo->default_sink_name)
1004 {
1005 Assert(RTStrIsValidEncoding(pInfo->default_sink_name));
1006 pCbCtx->pszDefaultSink = RTStrDup(pInfo->default_sink_name);
1007 }
1008
1009 if (pInfo->default_sink_name)
1010 {
1011 Assert(RTStrIsValidEncoding(pInfo->default_source_name));
1012 pCbCtx->pszDefaultSource = RTStrDup(pInfo->default_source_name);
1013 }
1014
1015// LogRel(("DEBUG: pa_threaded_mainloop_signal() from paEnumServerCb\n"));
1016 pa_threaded_mainloop_signal(pThis->pMainLoop, 0);
1017}
1018
1019
1020static int paEnumerate(PDRVHOSTPULSEAUDIO pThis, PPDMAUDIOBACKENDCFG pCfg, uint32_t fEnum)
1021{
1022 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1023 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1024
1025 PDMAUDIOBACKENDCFG Cfg;
1026 RT_ZERO(Cfg);
1027
1028 Cfg.cbStreamOut = sizeof(PULSEAUDIOSTREAM);
1029 Cfg.cbStreamIn = sizeof(PULSEAUDIOSTREAM);
1030 Cfg.cMaxStreamsOut = UINT32_MAX;
1031 Cfg.cMaxStreamsIn = UINT32_MAX;
1032
1033 PULSEAUDIOENUMCBCTX cbCtx;
1034 RT_ZERO(cbCtx);
1035
1036 cbCtx.pDrv = pThis;
1037 cbCtx.fFlags = fEnum;
1038
1039 bool fLog = (fEnum & PULSEAUDIOENUMCBFLAGS_LOG);
1040
1041 int rc = paWaitFor(pThis, pa_context_get_server_info(pThis->pContext, paEnumServerCb, &cbCtx));
1042 if (RT_SUCCESS(rc))
1043 {
1044 if (cbCtx.pszDefaultSink)
1045 {
1046 if (fLog)
1047 LogRel2(("PulseAudio: Default output sink is '%s'\n", cbCtx.pszDefaultSink));
1048
1049 rc = paWaitFor(pThis, pa_context_get_sink_info_by_name(pThis->pContext, cbCtx.pszDefaultSink,
1050 paEnumSinkCb, &cbCtx));
1051 if ( RT_FAILURE(rc)
1052 && fLog)
1053 {
1054 LogRel(("PulseAudio: Error enumerating properties for default output sink '%s'\n", cbCtx.pszDefaultSink));
1055 }
1056 }
1057 else if (fLog)
1058 LogRel2(("PulseAudio: No default output sink found\n"));
1059
1060 if (RT_SUCCESS(rc))
1061 {
1062 if (cbCtx.pszDefaultSource)
1063 {
1064 if (fLog)
1065 LogRel2(("PulseAudio: Default input source is '%s'\n", cbCtx.pszDefaultSource));
1066
1067 rc = paWaitFor(pThis, pa_context_get_source_info_by_name(pThis->pContext, cbCtx.pszDefaultSource,
1068 paEnumSourceCb, &cbCtx));
1069 if ( RT_FAILURE(rc)
1070 && fLog)
1071 {
1072 LogRel(("PulseAudio: Error enumerating properties for default input source '%s'\n", cbCtx.pszDefaultSource));
1073 }
1074 }
1075 else if (fLog)
1076 LogRel2(("PulseAudio: No default input source found\n"));
1077 }
1078
1079 if (RT_SUCCESS(rc))
1080 {
1081 if (fLog)
1082 {
1083 LogRel2(("PulseAudio: Found %RU8 host playback device(s)\n", cbCtx.cDevOut));
1084 LogRel2(("PulseAudio: Found %RU8 host capturing device(s)\n", cbCtx.cDevIn));
1085 }
1086
1087 if (pCfg)
1088 memcpy(pCfg, &Cfg, sizeof(PDMAUDIOBACKENDCFG));
1089 }
1090
1091 if (cbCtx.pszDefaultSink)
1092 {
1093 RTStrFree(cbCtx.pszDefaultSink);
1094 cbCtx.pszDefaultSink = NULL;
1095 }
1096
1097 if (cbCtx.pszDefaultSource)
1098 {
1099 RTStrFree(cbCtx.pszDefaultSource);
1100 cbCtx.pszDefaultSource = NULL;
1101 }
1102 }
1103 else if (fLog)
1104 LogRel(("PulseAudio: Error enumerating PulseAudio server properties\n"));
1105
1106 LogFlowFuncLeaveRC(rc);
1107 return rc;
1108}
1109
1110
1111static int paDestroyStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1112{
1113 LogFlowFuncEnter();
1114
1115 if (pStreamPA->pStream)
1116 {
1117 pa_threaded_mainloop_lock(pThis->pMainLoop);
1118
1119 pa_stream_disconnect(pStreamPA->pStream);
1120 pa_stream_unref(pStreamPA->pStream);
1121
1122 pStreamPA->pStream = NULL;
1123
1124 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1125 }
1126
1127 return VINF_SUCCESS;
1128}
1129
1130
1131static int paDestroyStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1132{
1133 if (pStreamPA->pStream)
1134 {
1135 pa_threaded_mainloop_lock(pThis->pMainLoop);
1136
1137 /* Make sure to cancel a pending draining operation, if any. */
1138 if (pStreamPA->pDrainOp)
1139 {
1140 pa_operation_cancel(pStreamPA->pDrainOp);
1141 pStreamPA->pDrainOp = NULL;
1142 }
1143
1144 pa_stream_disconnect(pStreamPA->pStream);
1145 pa_stream_unref(pStreamPA->pStream);
1146
1147 pStreamPA->pStream = NULL;
1148
1149 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1150 }
1151
1152 return VINF_SUCCESS;
1153}
1154
1155
1156static int paControlStreamOut(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA, PDMAUDIOSTREAMCMD enmStreamCmd)
1157{
1158 int rc = VINF_SUCCESS;
1159
1160 switch (enmStreamCmd)
1161 {
1162 case PDMAUDIOSTREAMCMD_ENABLE:
1163 case PDMAUDIOSTREAMCMD_RESUME:
1164 {
1165 pa_threaded_mainloop_lock(pThis->pMainLoop);
1166
1167 if ( pStreamPA->pDrainOp
1168 && pa_operation_get_state(pStreamPA->pDrainOp) != PA_OPERATION_DONE)
1169 {
1170 pa_operation_cancel(pStreamPA->pDrainOp);
1171 pa_operation_unref(pStreamPA->pDrainOp);
1172
1173 pStreamPA->pDrainOp = NULL;
1174 }
1175 else
1176 {
1177 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 0, paStreamCbSuccess, pStreamPA));
1178 }
1179
1180 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1181 break;
1182 }
1183
1184 case PDMAUDIOSTREAMCMD_DISABLE:
1185 case PDMAUDIOSTREAMCMD_PAUSE:
1186 {
1187 /* Pause audio output (the Pause bit of the AC97 x_CR register is set).
1188 * Note that we must return immediately from here! */
1189 pa_threaded_mainloop_lock(pThis->pMainLoop);
1190 if (!pStreamPA->pDrainOp)
1191 {
1192 rc = paWaitFor(pThis, pa_stream_trigger(pStreamPA->pStream, paStreamCbSuccess, pStreamPA));
1193 if (RT_SUCCESS(rc))
1194 pStreamPA->pDrainOp = pa_stream_drain(pStreamPA->pStream, paStreamCbDrain, pStreamPA);
1195 }
1196 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1197 break;
1198 }
1199
1200 default:
1201 AssertMsgFailed(("Invalid command %ld\n", enmStreamCmd));
1202 rc = VERR_INVALID_PARAMETER;
1203 break;
1204 }
1205
1206 LogFlowFuncLeaveRC(rc);
1207 return rc;
1208}
1209
1210
1211static int paControlStreamIn(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA, PDMAUDIOSTREAMCMD enmStreamCmd)
1212{
1213 int rc = VINF_SUCCESS;
1214
1215 LogFlowFunc(("enmStreamCmd=%ld\n", enmStreamCmd));
1216
1217 switch (enmStreamCmd)
1218 {
1219 case PDMAUDIOSTREAMCMD_ENABLE:
1220 case PDMAUDIOSTREAMCMD_RESUME:
1221 {
1222 pa_threaded_mainloop_lock(pThis->pMainLoop);
1223 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 0 /* Play / resume */, paStreamCbSuccess, pStreamPA));
1224 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1225 break;
1226 }
1227
1228 case PDMAUDIOSTREAMCMD_DISABLE:
1229 case PDMAUDIOSTREAMCMD_PAUSE:
1230 {
1231 pa_threaded_mainloop_lock(pThis->pMainLoop);
1232 if (pStreamPA->pu8PeekBuf) /* Do we need to drop the peek buffer?*/
1233 {
1234 pa_stream_drop(pStreamPA->pStream);
1235 pStreamPA->pu8PeekBuf = NULL;
1236 }
1237
1238 rc = paWaitFor(pThis, pa_stream_cork(pStreamPA->pStream, 1 /* Stop / pause */, paStreamCbSuccess, pStreamPA));
1239 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1240 break;
1241 }
1242
1243 default:
1244 AssertMsgFailed(("Invalid command %ld\n", enmStreamCmd));
1245 rc = VERR_INVALID_PARAMETER;
1246 break;
1247 }
1248
1249 return rc;
1250}
1251
1252
1253/**
1254 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
1255 */
1256static DECLCALLBACK(void) drvHostPulseAudioShutdown(PPDMIHOSTAUDIO pInterface)
1257{
1258 AssertPtrReturnVoid(pInterface);
1259
1260 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1261
1262 LogFlowFuncEnter();
1263
1264 if (pThis->pMainLoop)
1265 pa_threaded_mainloop_stop(pThis->pMainLoop);
1266
1267 if (pThis->pContext)
1268 {
1269 pa_context_disconnect(pThis->pContext);
1270 pa_context_unref(pThis->pContext);
1271 pThis->pContext = NULL;
1272 }
1273
1274 if (pThis->pMainLoop)
1275 {
1276 pa_threaded_mainloop_free(pThis->pMainLoop);
1277 pThis->pMainLoop = NULL;
1278 }
1279
1280 LogFlowFuncLeave();
1281}
1282
1283
1284/**
1285 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
1286 */
1287static DECLCALLBACK(int) drvHostPulseAudioGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
1288{
1289 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1290 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
1291
1292 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1293
1294 return paEnumerate(pThis, pBackendCfg, PULSEAUDIOENUMCBFLAGS_LOG /* fEnum */);
1295}
1296
1297
1298/**
1299 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
1300 */
1301static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostPulseAudioGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
1302{
1303 RT_NOREF(enmDir);
1304 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
1305
1306 return PDMAUDIOBACKENDSTS_RUNNING;
1307}
1308
1309
1310/**
1311 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
1312 */
1313static DECLCALLBACK(int) drvHostPulseAudioStreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1314 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
1315{
1316 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1317 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1318 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
1319 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
1320
1321 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1322 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1323
1324 int rc;
1325 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
1326 rc = paCreateStreamIn (pThis, pStreamPA, pCfgReq, pCfgAcq);
1327 else if (pCfgReq->enmDir == PDMAUDIODIR_OUT)
1328 rc = paCreateStreamOut(pThis, pStreamPA, pCfgReq, pCfgAcq);
1329 else
1330 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
1331
1332 if (RT_SUCCESS(rc))
1333 {
1334 pStreamPA->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
1335 if (!pStreamPA->pCfg)
1336 rc = VERR_NO_MEMORY;
1337 }
1338
1339 return rc;
1340}
1341
1342
1343/**
1344 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
1345 */
1346static DECLCALLBACK(int) drvHostPulseAudioStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1347{
1348 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1349 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1350
1351 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1352 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1353
1354 if (!pStreamPA->pCfg) /* Not (yet) configured? Skip. */
1355 return VINF_SUCCESS;
1356
1357 int rc;
1358 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1359 rc = paDestroyStreamIn (pThis, pStreamPA);
1360 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1361 rc = paDestroyStreamOut(pThis, pStreamPA);
1362 else
1363 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1364
1365 if (RT_SUCCESS(rc))
1366 {
1367 DrvAudioHlpStreamCfgFree(pStreamPA->pCfg);
1368 pStreamPA->pCfg = NULL;
1369 }
1370
1371 return rc;
1372}
1373
1374
1375/**
1376 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
1377 */
1378static DECLCALLBACK(int) drvHostPulseAudioStreamControl(PPDMIHOSTAUDIO pInterface,
1379 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
1380{
1381 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1382 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1383
1384 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1385 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1386
1387 if (!pStreamPA->pCfg) /* Not (yet) configured? Skip. */
1388 return VINF_SUCCESS;
1389
1390 int rc;
1391 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1392 rc = paControlStreamIn (pThis, pStreamPA, enmStreamCmd);
1393 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1394 rc = paControlStreamOut(pThis, pStreamPA, enmStreamCmd);
1395 else
1396 AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
1397
1398 return rc;
1399}
1400
1401
1402static uint32_t paStreamGetAvail(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA)
1403{
1404 pa_threaded_mainloop_lock(pThis->pMainLoop);
1405
1406 uint32_t cbAvail = 0;
1407
1408 if (PA_STREAM_IS_GOOD(pa_stream_get_state(pStreamPA->pStream)))
1409 {
1410 if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_IN)
1411 {
1412 cbAvail = (uint32_t)pa_stream_readable_size(pStreamPA->pStream);
1413 Log3Func(("cbReadable=%RU32\n", cbAvail));
1414 }
1415 else if (pStreamPA->pCfg->enmDir == PDMAUDIODIR_OUT)
1416 {
1417 size_t cbWritable = pa_stream_writable_size(pStreamPA->pStream);
1418 Log3Func(("cbWritable=%zu, cbMinReq=%RU32\n", cbWritable, pStreamPA->BufAttr.minreq));
1419
1420 if (cbWritable >= pStreamPA->BufAttr.minreq)
1421 cbAvail = (uint32_t)cbWritable;
1422 }
1423 else
1424 AssertFailed();
1425 }
1426
1427 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1428
1429 return cbAvail;
1430}
1431
1432
1433/**
1434 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1435 */
1436static DECLCALLBACK(uint32_t) drvHostPulseAudioStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1437{
1438 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1439 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1440
1441 return paStreamGetAvail(pThis, pStreamPA);
1442}
1443
1444
1445/**
1446 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1447 */
1448static DECLCALLBACK(uint32_t) drvHostPulseAudioStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1449{
1450 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1451 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1452
1453 return paStreamGetAvail(pThis, pStreamPA);
1454}
1455
1456
1457
1458/**
1459 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
1460 */
1461static DECLCALLBACK(PDMAUDIOSTRMSTS) drvHostPulseAudioStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1462{
1463 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1464 RT_NOREF(pStream);
1465
1466 PDRVHOSTPULSEAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTPULSEAUDIO(pInterface);
1467
1468 PDMAUDIOSTRMSTS strmSts = PDMAUDIOSTRMSTS_FLAG_NONE;
1469
1470 /* Check PulseAudio's general status. */
1471 if ( pThis->pContext
1472 && PA_CONTEXT_IS_GOOD(pa_context_get_state(pThis->pContext)))
1473 {
1474 strmSts = PDMAUDIOSTRMSTS_FLAG_INITIALIZED | PDMAUDIOSTRMSTS_FLAG_ENABLED;
1475 }
1476
1477 return strmSts;
1478}
1479
1480
1481/**
1482 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
1483 */
1484static DECLCALLBACK(int) drvHostPulseAudioStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1485{
1486 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1487 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
1488
1489 LogFlowFuncEnter();
1490
1491 /* Nothing to do here for PulseAudio. */
1492 return VINF_SUCCESS;
1493}
1494
1495
1496/**
1497 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1498 */
1499static DECLCALLBACK(void *) drvHostPulseAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1500{
1501 AssertPtrReturn(pInterface, NULL);
1502 AssertPtrReturn(pszIID, NULL);
1503
1504 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1505 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1506 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1507 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1508
1509 return NULL;
1510}
1511
1512
1513/**
1514 * Destructs a PulseAudio Audio driver instance.
1515 *
1516 * @copydoc FNPDMDRVDESTRUCT
1517 */
1518static DECLCALLBACK(void) drvHostPulseAudioDestruct(PPDMDRVINS pDrvIns)
1519{
1520 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1521 LogFlowFuncEnter();
1522}
1523
1524
1525/**
1526 * Constructs a PulseAudio Audio driver instance.
1527 *
1528 * @copydoc FNPDMDRVCONSTRUCT
1529 */
1530static DECLCALLBACK(int) drvHostPulseAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1531{
1532 RT_NOREF(pCfg, fFlags);
1533 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1534 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1535
1536 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1537 LogRel(("Audio: Initializing PulseAudio driver\n"));
1538
1539 pThis->pDrvIns = pDrvIns;
1540 /* IBase */
1541 pDrvIns->IBase.pfnQueryInterface = drvHostPulseAudioQueryInterface;
1542 /* IHostAudio */
1543 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostPulseAudio);
1544
1545 return VINF_SUCCESS;
1546}
1547
1548
1549/**
1550 * Pulse audio driver registration record.
1551 */
1552const PDMDRVREG g_DrvHostPulseAudio =
1553{
1554 /* u32Version */
1555 PDM_DRVREG_VERSION,
1556 /* szName */
1557 "PulseAudio",
1558 /* szRCMod */
1559 "",
1560 /* szR0Mod */
1561 "",
1562 /* pszDescription */
1563 "Pulse Audio host driver",
1564 /* fFlags */
1565 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1566 /* fClass. */
1567 PDM_DRVREG_CLASS_AUDIO,
1568 /* cMaxInstances */
1569 ~0U,
1570 /* cbInstance */
1571 sizeof(DRVHOSTPULSEAUDIO),
1572 /* pfnConstruct */
1573 drvHostPulseAudioConstruct,
1574 /* pfnDestruct */
1575 drvHostPulseAudioDestruct,
1576 /* pfnRelocate */
1577 NULL,
1578 /* pfnIOCtl */
1579 NULL,
1580 /* pfnPowerOn */
1581 NULL,
1582 /* pfnReset */
1583 NULL,
1584 /* pfnSuspend */
1585 NULL,
1586 /* pfnResume */
1587 NULL,
1588 /* pfnAttach */
1589 NULL,
1590 /* pfnDetach */
1591 NULL,
1592 /* pfnPowerOff */
1593 NULL,
1594 /* pfnSoftReset */
1595 NULL,
1596 /* u32EndVersion */
1597 PDM_DRVREG_VERSION
1598};
1599
1600#if 0 /* unused */
1601static struct audio_option pulse_options[] =
1602{
1603 {"DAC_MS", AUD_OPT_INT, &s_pulseCfg.buffer_msecs_out,
1604 "DAC period size in milliseconds", NULL, 0},
1605 {"ADC_MS", AUD_OPT_INT, &s_pulseCfg.buffer_msecs_in,
1606 "ADC period size in milliseconds", NULL, 0}
1607};
1608#endif
1609
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette