VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostAudioPulseAudio.cpp@ 88512

Last change on this file since 88512 was 88512, checked in by vboxsync, 4 years ago

DrvHostAudioPulseAudio: Don't clamp the pfnStreamWritable result to BufAttr.maxlength, we deal with that by looping in pfnStreamPlay instead. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 76.5 KB
Line 
1/* $Id: DrvHostAudioPulseAudio.cpp 88512 2021-04-14 19:04:27Z vboxsync $ */
2/** @file
3 * Host audio driver - Pulse Audio.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#include <VBox/vmm/pdmaudioinline.h>
26#include <VBox/vmm/pdmaudiohostenuminline.h>
27
28#include <stdio.h>
29
30#include <iprt/alloc.h>
31#include <iprt/mem.h>
32#include <iprt/uuid.h>
33#include <iprt/semaphore.h>
34
35#include "DrvHostAudioPulseAudioStubsMangling.h"
36#include "DrvHostAudioPulseAudioStubs.h"
37
38#include <pulse/pulseaudio.h>
39#ifndef PA_STREAM_NOFLAGS
40# define PA_STREAM_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
41#endif
42#ifndef PA_CONTEXT_NOFLAGS
43# define PA_CONTEXT_NOFLAGS (pa_context_flags_t)0x0000U /* since 0.9.19 */
44#endif
45
46#include "VBoxDD.h"
47
48
49/*********************************************************************************************************************************
50* Defines *
51*********************************************************************************************************************************/
52/** Max number of errors reported by drvHostAudioPaError per instance.
53 * @todo Make this configurable thru driver config. */
54#define VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS 99
55
56
57/** @name PULSEAUDIOENUMCBFLAGS_XXX
58 * @{ */
59/** No flags specified. */
60#define PULSEAUDIOENUMCBFLAGS_NONE 0
61/** (Release) log found devices. */
62#define PULSEAUDIOENUMCBFLAGS_LOG RT_BIT(0)
63/** Only do default devices. */
64#define PULSEAUDIOENUMCBFLAGS_DEFAULT_ONLY RT_BIT(1)
65/** @} */
66
67
68/*********************************************************************************************************************************
69* Structures *
70*********************************************************************************************************************************/
71/** Pointer to the instance data for a pulse audio host audio driver. */
72typedef struct DRVHOSTPULSEAUDIO *PDRVHOSTPULSEAUDIO;
73
74
75/**
76 * Callback context for the server init context state changed callback.
77 */
78typedef struct PULSEAUDIOSTATECHGCTX
79{
80 /** The event semaphore. */
81 RTSEMEVENT hEvtInit;
82 /** The returned context state. */
83 pa_context_state_t volatile enmCtxState;
84} PULSEAUDIOSTATECHGCTX;
85/** Pointer to a server init context state changed callback context. */
86typedef PULSEAUDIOSTATECHGCTX *PPULSEAUDIOSTATECHGCTX;
87
88
89/**
90 * Enumeration callback context used by the pfnGetConfig code.
91 */
92typedef struct PULSEAUDIOENUMCBCTX
93{
94 /** Pointer to PulseAudio's threaded main loop. */
95 pa_threaded_mainloop *pMainLoop;
96 /** Enumeration flags, PULSEAUDIOENUMCBFLAGS_XXX. */
97 uint32_t fFlags;
98 /** VBox status code for the operation.
99 * The caller sets this to VERR_AUDIO_ENUMERATION_FAILED, the callback never
100 * uses that status code. */
101 int32_t rcEnum;
102 /** Name of default sink being used. Must be free'd using RTStrFree(). */
103 char *pszDefaultSink;
104 /** Name of default source being used. Must be free'd using RTStrFree(). */
105 char *pszDefaultSource;
106 /** The device enumeration to fill, NULL if pfnGetConfig context. */
107 PPDMAUDIOHOSTENUM pDeviceEnum;
108} PULSEAUDIOENUMCBCTX;
109/** Pointer to an enumeration callback context. */
110typedef PULSEAUDIOENUMCBCTX *PPULSEAUDIOENUMCBCTX;
111
112
113/**
114 * Pulse audio device enumeration entry.
115 */
116typedef struct PULSEAUDIODEVENTRY
117{
118 /** The part we share with others. */
119 PDMAUDIOHOSTDEV Core;
120 /** The pulse audio name.
121 * @note Kind of must use fixed size field here as that allows
122 * PDMAudioHostDevDup() and PDMAudioHostEnumCopy() to work. */
123 RT_FLEXIBLE_ARRAY_EXTENSION
124 char szPulseName[RT_FLEXIBLE_ARRAY];
125} PULSEAUDIODEVENTRY;
126/** Pointer to a pulse audio device enumeration entry. */
127typedef PULSEAUDIODEVENTRY *PPULSEAUDIODEVENTRY;
128
129
130/**
131 * Pulse audio stream data.
132 */
133typedef struct PULSEAUDIOSTREAM
134{
135 /** The stream's acquired configuration. */
136 PDMAUDIOSTREAMCFG Cfg;
137 /** Pointer to driver instance. */
138 PDRVHOSTPULSEAUDIO pDrv;
139 /** Pointer to opaque PulseAudio stream. */
140 pa_stream *pStream;
141 /** Pulse sample format and attribute specification. */
142 pa_sample_spec SampleSpec;
143 /** Pulse playback and buffer metrics. */
144 pa_buffer_attr BufAttr;
145 /** Input: Pointer to Pulse sample peek buffer. */
146 const uint8_t *pbPeekBuf;
147 /** Input: Current size (in bytes) of peeked data in buffer. */
148 size_t cbPeekBuf;
149 /** Input: Our offset (in bytes) in peek data buffer. */
150 size_t offPeekBuf;
151 /** Output: Asynchronous drain operation. This is used as an indicator of
152 * whether we're currently draining the stream (will be cleaned up before
153 * resume/re-enable). */
154 pa_operation *pDrainOp;
155 /** Asynchronous cork/uncork operation.
156 * (This solely for cancelling before destroying the stream, so the callback
157 * won't do any after-freed accesses.) */
158 pa_operation *pCorkOp;
159 /** Asynchronous trigger operation.
160 * (This solely for cancelling before destroying the stream, so the callback
161 * won't do any after-freed accesses.) */
162 pa_operation *pTriggerOp;
163 /** Output: Current latency (in microsecs). */
164 uint64_t cUsLatency;
165#ifdef LOG_ENABLED
166 /** Creation timestamp (in microsecs) of stream playback / recording. */
167 pa_usec_t tsStartUs;
168 /** Timestamp (in microsecs) when last read from / written to the stream. */
169 pa_usec_t tsLastReadWrittenUs;
170#endif
171#ifdef DEBUG
172 /** Number of occurred audio data underflows. */
173 uint32_t cUnderflows;
174#endif
175} PULSEAUDIOSTREAM;
176/** Pointer to pulse audio stream data. */
177typedef PULSEAUDIOSTREAM *PPULSEAUDIOSTREAM;
178
179
180/**
181 * Pulse audio host audio driver instance data.
182 * @implements PDMIAUDIOCONNECTOR
183 */
184typedef struct DRVHOSTPULSEAUDIO
185{
186 /** Pointer to the driver instance structure. */
187 PPDMDRVINS pDrvIns;
188 /** Pointer to PulseAudio's threaded main loop. */
189 pa_threaded_mainloop *pMainLoop;
190 /**
191 * Pointer to our PulseAudio context.
192 * @note We use a pMainLoop in a separate thread (pContext).
193 * So either use callback functions or protect these functions
194 * by pa_threaded_mainloop_lock() / pa_threaded_mainloop_unlock().
195 */
196 pa_context *pContext;
197 /** Shutdown indicator. */
198 volatile bool fAbortLoop;
199 /** Error count for not flooding the release log.
200 * Specify UINT32_MAX for unlimited logging. */
201 uint32_t cLogErrors;
202 /** The stream (base) name; needed for distinguishing
203 * streams in the PulseAudio mixer controls if multiple
204 * VMs are running at the same time. */
205 char szStreamName[64];
206 /** Don't want to put this on the stack... */
207 PULSEAUDIOSTATECHGCTX InitStateChgCtx;
208 /** Pointer to host audio interface. */
209 PDMIHOSTAUDIO IHostAudio;
210} DRVHOSTPULSEAUDIO;
211
212
213
214/*
215 * Glue to make the code work systems with PulseAudio < 0.9.11.
216 */
217#if !defined(PA_CONTEXT_IS_GOOD) && PA_API_VERSION < 12 /* 12 = 0.9.11 where PA_STREAM_IS_GOOD was added */
218DECLINLINE(bool) PA_CONTEXT_IS_GOOD(pa_context_state_t enmState)
219{
220 return enmState == PA_CONTEXT_CONNECTING
221 || enmState == PA_CONTEXT_AUTHORIZING
222 || enmState == PA_CONTEXT_SETTING_NAME
223 || enmState == PA_CONTEXT_READY;
224}
225#endif
226
227#if !defined(PA_STREAM_IS_GOOD) && PA_API_VERSION < 12 /* 12 = 0.9.11 where PA_STREAM_IS_GOOD was added */
228DECLINLINE(bool) PA_STREAM_IS_GOOD(pa_stream_state_t enmState)
229{
230 return enmState == PA_STREAM_CREATING
231 || enmState == PA_STREAM_READY;
232}
233#endif
234
235
236/**
237 * Converts a pulse audio error to a VBox status.
238 *
239 * @returns VBox status code.
240 * @param rcPa The error code to convert.
241 */
242static int drvHostAudioPaErrorToVBox(int rcPa)
243{
244 /** @todo Implement some PulseAudio -> VBox mapping here. */
245 RT_NOREF(rcPa);
246 return VERR_GENERAL_FAILURE;
247}
248
249
250/**
251 * Logs a pulse audio (from context) and converts it to VBox status.
252 *
253 * @returns VBox status code.
254 * @param pThis Our instance data.
255 * @param pszFormat The format string for the release log (no newline) .
256 * @param ... Format string arguments.
257 */
258static int drvHostAudioPaError(PDRVHOSTPULSEAUDIO pThis, const char *pszFormat, ...)
259{
260 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
261 AssertPtr(pszFormat);
262
263 int const rcPa = pa_context_errno(pThis->pContext);
264 int const rcVBox = drvHostAudioPaErrorToVBox(rcPa);
265
266 if ( pThis->cLogErrors < VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS
267 && LogRelIs2Enabled())
268 {
269 va_list va;
270 va_start(va, pszFormat);
271 LogRel(("PulseAudio: %N: %s (%d, %Rrc)\n", pszFormat, &va, pa_strerror(rcPa), rcPa, rcVBox));
272 va_end(va);
273
274 if (++pThis->cLogErrors == VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS)
275 LogRel(("PulseAudio: muting errors (max %u)\n", VBOX_PULSEAUDIO_MAX_LOG_REL_ERRORS));
276 }
277
278 return rcVBox;
279}
280
281
282/**
283 * Signal the main loop to abort. Just signalling isn't sufficient as the
284 * mainloop might not have been entered yet.
285 */
286static void drvHostAudioPaSignalWaiter(PDRVHOSTPULSEAUDIO pThis)
287{
288 if (pThis)
289 {
290 pThis->fAbortLoop = true;
291 pa_threaded_mainloop_signal(pThis->pMainLoop, 0);
292 }
293}
294
295
296/**
297 * Wrapper around pa_threaded_mainloop_wait().
298 */
299static void drvHostAudioPaMainloopWait(PDRVHOSTPULSEAUDIO pThis)
300{
301 /** @todo r=bird: explain this logic. */
302 if (!pThis->fAbortLoop)
303 pa_threaded_mainloop_wait(pThis->pMainLoop);
304 pThis->fAbortLoop = false;
305}
306
307
308/**
309 * Pulse audio callback for context status changes, init variant.
310 */
311static void drvHostAudioPaCtxCallbackStateChanged(pa_context *pCtx, void *pvUser)
312{
313 AssertPtrReturnVoid(pCtx);
314
315 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
316 AssertPtrReturnVoid(pThis);
317
318 switch (pa_context_get_state(pCtx))
319 {
320 case PA_CONTEXT_READY:
321 case PA_CONTEXT_TERMINATED:
322 case PA_CONTEXT_FAILED:
323 drvHostAudioPaSignalWaiter(pThis);
324 break;
325
326 default:
327 break;
328 }
329}
330
331
332/**
333 * Synchronously wait until an operation completed.
334 *
335 * This will consume the pOperation reference.
336 */
337static int drvHostAudioPaWaitForEx(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOperation, RTMSINTERVAL cMsTimeout)
338{
339 AssertPtrReturn(pOperation, VERR_INVALID_POINTER);
340
341 uint64_t const msStart = RTTimeMilliTS();
342 pa_operation_state_t enmOpState;
343 while ((enmOpState = pa_operation_get_state(pOperation)) == PA_OPERATION_RUNNING)
344 {
345 if (!pThis->fAbortLoop) /** @todo r=bird: I do _not_ get the logic behind this fAbortLoop mechanism, it looks more
346 * than a little mixed up and too much generalized see drvHostAudioPaSignalWaiter. */
347 {
348 AssertPtr(pThis->pMainLoop);
349 pa_threaded_mainloop_wait(pThis->pMainLoop);
350 if ( !pThis->pContext
351 || pa_context_get_state(pThis->pContext) != PA_CONTEXT_READY)
352 {
353 pa_operation_cancel(pOperation);
354 pa_operation_unref(pOperation);
355 LogRel(("PulseAudio: pa_context_get_state context not ready\n"));
356 return VERR_INVALID_STATE;
357 }
358 }
359 pThis->fAbortLoop = false;
360
361 /*
362 * Note! This timeout business is a bit bogus as pa_threaded_mainloop_wait is indefinite.
363 */
364 if (RTTimeMilliTS() - msStart >= cMsTimeout)
365 {
366 enmOpState = pa_operation_get_state(pOperation);
367 if (enmOpState != PA_OPERATION_RUNNING)
368 break;
369 pa_operation_cancel(pOperation);
370 pa_operation_unref(pOperation);
371 return VERR_TIMEOUT;
372 }
373 }
374
375 pa_operation_unref(pOperation);
376 if (enmOpState == PA_OPERATION_DONE)
377 return VINF_SUCCESS;
378 return VERR_CANCELLED;
379}
380
381
382static int drvHostAudioPaWaitFor(PDRVHOSTPULSEAUDIO pThis, pa_operation *pOP)
383{
384 return drvHostAudioPaWaitForEx(pThis, pOP, 10 * RT_MS_1SEC);
385}
386
387
388
389/*********************************************************************************************************************************
390* PDMIHOSTAUDIO *
391*********************************************************************************************************************************/
392
393/**
394 * Worker for drvHostAudioPaEnumSourceCallback() and
395 * drvHostAudioPaEnumSinkCallback() that adds an entry to the enumeration
396 * result.
397 */
398static void drvHostAudioPaEnumAddDevice(PPULSEAUDIOENUMCBCTX pCbCtx, PDMAUDIODIR enmDir, const char *pszName,
399 const char *pszDesc, uint8_t cChannelsInput, uint8_t cChannelsOutput,
400 const char *pszDefaultName)
401{
402 size_t const cchName = strlen(pszName);
403 PPULSEAUDIODEVENTRY pDev = (PPULSEAUDIODEVENTRY)PDMAudioHostDevAlloc(RT_UOFFSETOF(PULSEAUDIODEVENTRY, szPulseName)
404 + RT_ALIGN_Z(cchName + 1, 16));
405 if (pDev != NULL)
406 {
407 memcpy(pDev->szPulseName, pszName, cchName);
408 pDev->szPulseName[cchName] = '\0';
409
410 pDev->Core.enmUsage = enmDir;
411 pDev->Core.enmType = RTStrIStr(pszDesc, "built-in") != NULL
412 ? PDMAUDIODEVICETYPE_BUILTIN : PDMAUDIODEVICETYPE_UNKNOWN;
413 pDev->Core.fFlags = RTStrCmp(pszName, pszDefaultName) == 0
414 ? PDMAUDIOHOSTDEV_F_DEFAULT : PDMAUDIOHOSTDEV_F_NONE;
415 pDev->Core.cMaxInputChannels = cChannelsInput;
416 pDev->Core.cMaxOutputChannels = cChannelsOutput;
417 RTStrCopy(pDev->Core.szName, sizeof(pDev->Core.szName),
418 pszDesc && *pszDesc ? pszDesc : pszName);
419
420 PDMAudioHostEnumAppend(pCbCtx->pDeviceEnum, &pDev->Core);
421 }
422 else
423 pCbCtx->rcEnum = VERR_NO_MEMORY;
424}
425
426
427/**
428 * Enumeration callback - source info.
429 *
430 * @param pCtx The context (DRVHOSTPULSEAUDIO::pContext).
431 * @param pInfo The info. NULL when @a eol is not zero.
432 * @param eol Error-or-last indicator or something like that:
433 * - 0: Normal call with info.
434 * - 1: End of list, no info.
435 * - -1: Error callback, no info.
436 * @param pvUserData Pointer to our PULSEAUDIOENUMCBCTX structure.
437 */
438static void drvHostAudioPaEnumSourceCallback(pa_context *pCtx, const pa_source_info *pInfo, int eol, void *pvUserData)
439{
440 LogFlowFunc(("pCtx=%p pInfo=%p eol=%d pvUserData=%p\n", pCtx, pInfo, eol, pvUserData));
441 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
442 AssertPtrReturnVoid(pCbCtx);
443 Assert((pInfo == NULL) == (eol != 0));
444 RT_NOREF(pCtx);
445
446 if (eol == 0 && pInfo != NULL)
447 {
448 LogRel2(("Pulse Audio: Source #%u: %u Hz %uch format=%u name='%s' desc='%s' driver='%s' flags=%#x\n",
449 pInfo->index, pInfo->sample_spec.rate, pInfo->sample_spec.channels, pInfo->sample_spec.format,
450 pInfo->name, pInfo->description, pInfo->driver, pInfo->flags));
451 drvHostAudioPaEnumAddDevice(pCbCtx, PDMAUDIODIR_IN, pInfo->name, pInfo->description,
452 pInfo->sample_spec.channels, 0 /*cChannelsOutput*/, pCbCtx->pszDefaultSource);
453 }
454 else if (eol == 1 && !pInfo && pCbCtx->rcEnum == VERR_AUDIO_ENUMERATION_FAILED)
455 pCbCtx->rcEnum = VINF_SUCCESS;
456
457 /* Wake up the calling thread when done: */
458 if (eol != 0)
459 pa_threaded_mainloop_signal(pCbCtx->pMainLoop, 0);
460}
461
462
463/**
464 * Enumeration callback - sink info.
465 *
466 * @param pCtx The context (DRVHOSTPULSEAUDIO::pContext).
467 * @param pInfo The info. NULL when @a eol is not zero.
468 * @param eol Error-or-last indicator or something like that:
469 * - 0: Normal call with info.
470 * - 1: End of list, no info.
471 * - -1: Error callback, no info.
472 * @param pvUserData Pointer to our PULSEAUDIOENUMCBCTX structure.
473 */
474static void drvHostAudioPaEnumSinkCallback(pa_context *pCtx, const pa_sink_info *pInfo, int eol, void *pvUserData)
475{
476 LogFlowFunc(("pCtx=%p pInfo=%p eol=%d pvUserData=%p\n", pCtx, pInfo, eol, pvUserData));
477 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
478 AssertPtrReturnVoid(pCbCtx);
479 Assert((pInfo == NULL) == (eol != 0));
480 RT_NOREF(pCtx);
481
482 if (eol == 0 && pInfo != NULL)
483 {
484 LogRel2(("Pulse Audio: Sink #%u: %u Hz %uch format=%u name='%s' desc='%s' driver='%s' flags=%#x\n",
485 pInfo->index, pInfo->sample_spec.rate, pInfo->sample_spec.channels, pInfo->sample_spec.format,
486 pInfo->name, pInfo->description, pInfo->driver, pInfo->flags));
487 drvHostAudioPaEnumAddDevice(pCbCtx, PDMAUDIODIR_OUT, pInfo->name, pInfo->description,
488 0 /*cChannelsInput*/, pInfo->sample_spec.channels, pCbCtx->pszDefaultSink);
489 }
490 else if (eol == 1 && !pInfo && pCbCtx->rcEnum == VERR_AUDIO_ENUMERATION_FAILED)
491 pCbCtx->rcEnum = VINF_SUCCESS;
492
493 /* Wake up the calling thread when done: */
494 if (eol != 0)
495 pa_threaded_mainloop_signal(pCbCtx->pMainLoop, 0);
496}
497
498
499/**
500 * Enumeration callback - service info.
501 *
502 * Copy down the default names.
503 */
504static void drvHostAudioPaEnumServerCallback(pa_context *pCtx, const pa_server_info *pInfo, void *pvUserData)
505{
506 LogFlowFunc(("pCtx=%p pInfo=%p pvUserData=%p\n", pCtx, pInfo, pvUserData));
507 PPULSEAUDIOENUMCBCTX pCbCtx = (PPULSEAUDIOENUMCBCTX)pvUserData;
508 AssertPtrReturnVoid(pCbCtx);
509 RT_NOREF(pCtx);
510
511 if (pInfo)
512 {
513 LogRel2(("PulseAudio: Server info: user=%s host=%s ver=%s name=%s defsink=%s defsrc=%s spec: %d %uHz %uch\n",
514 pInfo->user_name, pInfo->host_name, pInfo->server_version, pInfo->server_name,
515 pInfo->default_sink_name, pInfo->default_source_name,
516 pInfo->sample_spec.format, pInfo->sample_spec.rate, pInfo->sample_spec.channels));
517
518 Assert(!pCbCtx->pszDefaultSink);
519 Assert(!pCbCtx->pszDefaultSource);
520 Assert(pCbCtx->rcEnum == VERR_AUDIO_ENUMERATION_FAILED);
521 pCbCtx->rcEnum = VINF_SUCCESS;
522
523 if (pInfo->default_sink_name)
524 {
525 Assert(RTStrIsValidEncoding(pInfo->default_sink_name));
526 pCbCtx->pszDefaultSink = RTStrDup(pInfo->default_sink_name);
527 AssertStmt(pCbCtx->pszDefaultSink, pCbCtx->rcEnum = VERR_NO_STR_MEMORY);
528 }
529
530 if (pInfo->default_source_name)
531 {
532 Assert(RTStrIsValidEncoding(pInfo->default_source_name));
533 pCbCtx->pszDefaultSource = RTStrDup(pInfo->default_source_name);
534 AssertStmt(pCbCtx->pszDefaultSource, pCbCtx->rcEnum = VERR_NO_STR_MEMORY);
535 }
536 }
537 else
538 pCbCtx->rcEnum = VERR_INVALID_POINTER;
539
540 pa_threaded_mainloop_signal(pCbCtx->pMainLoop, 0);
541}
542
543
544/**
545 * @note Called with the PA main loop locked.
546 */
547static int drvHostAudioPaEnumerate(PDRVHOSTPULSEAUDIO pThis, uint32_t fEnum, PPDMAUDIOHOSTENUM pDeviceEnum)
548{
549 PULSEAUDIOENUMCBCTX CbCtx = { pThis->pMainLoop, fEnum, VERR_AUDIO_ENUMERATION_FAILED, NULL, NULL, pDeviceEnum };
550 bool const fLog = (fEnum & PULSEAUDIOENUMCBFLAGS_LOG);
551 bool const fOnlyDefault = (fEnum & PULSEAUDIOENUMCBFLAGS_DEFAULT_ONLY);
552 int rc;
553
554 /*
555 * Check if server information is available and bail out early if it isn't.
556 * This should give us a default (playback) sink and (recording) source.
557 */
558 LogRel(("PulseAudio: Retrieving server information ...\n"));
559 CbCtx.rcEnum = VERR_AUDIO_ENUMERATION_FAILED;
560 pa_operation *paOpServerInfo = pa_context_get_server_info(pThis->pContext, drvHostAudioPaEnumServerCallback, &CbCtx);
561 if (paOpServerInfo)
562 rc = drvHostAudioPaWaitFor(pThis, paOpServerInfo);
563 else
564 {
565 LogRel(("PulseAudio: Server information not available, skipping enumeration.\n"));
566 return VINF_SUCCESS;
567 }
568 if (RT_SUCCESS(rc))
569 rc = CbCtx.rcEnum;
570 if (RT_FAILURE(rc))
571 {
572 if (fLog)
573 LogRel(("PulseAudio: Error enumerating PulseAudio server properties: %Rrc\n", rc));
574 return rc;
575 }
576
577 /*
578 * Get info about the playback sink.
579 */
580 if (fLog && CbCtx.pszDefaultSink)
581 LogRel2(("PulseAudio: Default output sink is '%s'\n", CbCtx.pszDefaultSink));
582 else if (fLog)
583 LogRel2(("PulseAudio: No default output sink found\n"));
584
585 if (CbCtx.pszDefaultSink || !fOnlyDefault)
586 {
587 CbCtx.rcEnum = VERR_AUDIO_ENUMERATION_FAILED;
588 if (!fOnlyDefault)
589 rc = drvHostAudioPaWaitFor(pThis,
590 pa_context_get_sink_info_list(pThis->pContext, drvHostAudioPaEnumSinkCallback, &CbCtx));
591 else
592 rc = drvHostAudioPaWaitFor(pThis, pa_context_get_sink_info_by_name(pThis->pContext, CbCtx.pszDefaultSink,
593 drvHostAudioPaEnumSinkCallback, &CbCtx));
594 if (RT_SUCCESS(rc))
595 rc = CbCtx.rcEnum;
596 if (fLog && RT_FAILURE(rc))
597 LogRel(("PulseAudio: Error enumerating properties for default output sink '%s': %Rrc\n",
598 CbCtx.pszDefaultSink, rc));
599 }
600
601 /*
602 * Get info about the recording source.
603 */
604 if (fLog && CbCtx.pszDefaultSource)
605 LogRel2(("PulseAudio: Default input source is '%s'\n", CbCtx.pszDefaultSource));
606 else if (fLog)
607 LogRel2(("PulseAudio: No default input source found\n"));
608 if (CbCtx.pszDefaultSource || !fOnlyDefault)
609 {
610 CbCtx.rcEnum = VERR_AUDIO_ENUMERATION_FAILED;
611 int rc2;
612 if (!fOnlyDefault)
613 rc2 = drvHostAudioPaWaitFor(pThis, pa_context_get_source_info_list(pThis->pContext,
614 drvHostAudioPaEnumSourceCallback, &CbCtx));
615 else
616 rc2 = drvHostAudioPaWaitFor(pThis, pa_context_get_source_info_by_name(pThis->pContext, CbCtx.pszDefaultSource,
617 drvHostAudioPaEnumSourceCallback, &CbCtx));
618 if (RT_SUCCESS(rc2))
619 rc2 = CbCtx.rcEnum;
620 if (fLog && RT_FAILURE(rc2))
621 LogRel(("PulseAudio: Error enumerating properties for default input source '%s': %Rrc\n",
622 CbCtx.pszDefaultSource, rc));
623 if (RT_SUCCESS(rc))
624 rc = rc2;
625 }
626
627 /* clean up */
628 RTStrFree(CbCtx.pszDefaultSink);
629 RTStrFree(CbCtx.pszDefaultSource);
630
631 LogFlowFuncLeaveRC(rc);
632 return rc;
633}
634
635
636/**
637 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
638 */
639static DECLCALLBACK(int) drvHostAudioPaHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
640{
641 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
642 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
643
644 /*
645 * The configuration.
646 */
647 RTStrCopy(pBackendCfg->szName, sizeof(pBackendCfg->szName), "PulseAudio");
648 pBackendCfg->cbStreamOut = sizeof(PULSEAUDIOSTREAM);
649 pBackendCfg->cbStreamIn = sizeof(PULSEAUDIOSTREAM);
650 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
651 pBackendCfg->cMaxStreamsIn = UINT32_MAX;
652
653#if 0
654 /*
655 * In case we want to gather info about default devices, we can do this:
656 */
657 PDMAUDIOHOSTENUM DeviceEnum;
658 PDMAudioHostEnumInit(&DeviceEnum);
659 pa_threaded_mainloop_lock(pThis->pMainLoop);
660 int rc = drvHostAudioPaEnumerate(pThis, PULSEAUDIOENUMCBFLAGS_DEFAULT_ONLY | PULSEAUDIOENUMCBFLAGS_LOG, &DeviceEnum);
661 pa_threaded_mainloop_unlock(pThis->pMainLoop);
662 AssertRCReturn(rc, rc);
663 /** @todo do stuff with DeviceEnum. */
664 PDMAudioHostEnumDelete(&DeviceEnum);
665#else
666 RT_NOREF(pThis);
667#endif
668 return VINF_SUCCESS;
669}
670
671
672/**
673 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
674 */
675static DECLCALLBACK(int) drvHostAudioPaHA_GetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHOSTENUM pDeviceEnum)
676{
677 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
678 AssertPtrReturn(pDeviceEnum, VERR_INVALID_POINTER);
679 PDMAudioHostEnumInit(pDeviceEnum);
680
681 /* Refine it or something (currently only some LogRel2 stuff): */
682 pa_threaded_mainloop_lock(pThis->pMainLoop);
683 int rc = drvHostAudioPaEnumerate(pThis, PULSEAUDIOENUMCBFLAGS_NONE, pDeviceEnum);
684 pa_threaded_mainloop_unlock(pThis->pMainLoop);
685 return rc;
686}
687
688
689/**
690 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
691 */
692static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostAudioPaHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
693{
694 RT_NOREF(pInterface, enmDir);
695 return PDMAUDIOBACKENDSTS_RUNNING;
696}
697
698
699/**
700 * Stream status changed.
701 */
702static void drvHostAudioPaStreamStateChangedCallback(pa_stream *pStream, void *pvUser)
703{
704 AssertPtrReturnVoid(pStream);
705
706 PDRVHOSTPULSEAUDIO pThis = (PDRVHOSTPULSEAUDIO)pvUser;
707 AssertPtrReturnVoid(pThis);
708
709 switch (pa_stream_get_state(pStream))
710 {
711 case PA_STREAM_READY:
712 case PA_STREAM_FAILED:
713 case PA_STREAM_TERMINATED:
714 drvHostAudioPaSignalWaiter(pThis);
715 break;
716
717 default:
718 break;
719 }
720}
721
722#ifdef DEBUG
723
724static void drvHostAudioPaStreamReqWriteDebugCallback(pa_stream *pStream, size_t cbLen, void *pvContext)
725{
726 RT_NOREF(cbLen, pvContext);
727
728 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvContext;
729 AssertPtrReturnVoid(pStrm);
730
731 pa_usec_t usec = 0;
732 int neg = 0;
733 pa_stream_get_latency(pStream, &usec, &neg);
734
735 Log2Func(("Requested %zu bytes -- Current latency is %RU64ms\n", cbLen, usec / 1000));
736}
737
738
739static void drvHostAudioPaStreamUnderflowDebugCallback(pa_stream *pStream, void *pvContext)
740{
741 PPULSEAUDIOSTREAM pStrm = (PPULSEAUDIOSTREAM)pvContext;
742 AssertPtrReturnVoid(pStrm);
743
744 pStrm->cUnderflows++;
745
746 LogRel2(("PulseAudio: Warning: Hit underflow #%RU32\n", pStrm->cUnderflows));
747
748 if ( pStrm->cUnderflows >= 6 /** @todo Make this check configurable. */
749 && pStrm->cUsLatency < 2000000 /* 2s */)
750 {
751 pStrm->cUsLatency = (pStrm->cUsLatency * 3) / 2;
752
753 LogRel2(("PulseAudio: Output latency increased to %RU64 us\n", pStrm->cUsLatency));
754
755 pStrm->BufAttr.maxlength = pa_usec_to_bytes(pStrm->cUsLatency, &pStrm->SampleSpec);
756 pStrm->BufAttr.tlength = pa_usec_to_bytes(pStrm->cUsLatency, &pStrm->SampleSpec);
757
758 pa_stream_set_buffer_attr(pStream, &pStrm->BufAttr, NULL, NULL);
759
760 pStrm->cUnderflows = 0;
761 }
762
763 pa_usec_t cUsLatency = 0;
764 pa_stream_get_latency(pStream, &cUsLatency, NULL /* Neg */);
765
766 LogRel2(("PulseAudio: Latency now is %RU64 us\n", cUsLatency));
767
768# ifdef LOG_ENABLED
769 const pa_timing_info *pTInfo = pa_stream_get_timing_info(pStream);
770 const pa_sample_spec *pSpec = pa_stream_get_sample_spec(pStream);
771
772 pa_usec_t curPosWritesUs = pa_bytes_to_usec(pTInfo->write_index, pSpec);
773 pa_usec_t curPosReadsUs = pa_bytes_to_usec(pTInfo->read_index, pSpec);
774 pa_usec_t curTsUs = pa_rtclock_now() - pStrm->tsStartUs;
775
776 Log2Func(("curPosWrite=%RU64ms, curPosRead=%RU64ms, curTs=%RU64ms, curLatency=%RU64ms (%RU32Hz, %RU8 channels)\n",
777 curPosWritesUs / RT_US_1MS_64, curPosReadsUs / RT_US_1MS_64,
778 curTsUs / RT_US_1MS_64, cUsLatency / RT_US_1MS_64, pSpec->rate, pSpec->channels));
779# endif
780}
781
782
783static void drvHostAudioPaStreamOverflowDebugCallback(pa_stream *pStream, void *pvContext)
784{
785 RT_NOREF(pStream, pvContext);
786
787 Log2Func(("Warning: Hit overflow\n"));
788}
789
790#endif /* DEBUG */
791
792/**
793 * Converts from PDM PCM properties to pulse audio format.
794 *
795 * Worker for the stream creation code.
796 *
797 * @returns PA format.
798 * @retval PA_SAMPLE_INVALID if format not supported.
799 * @param pProps The PDM audio source properties.
800 */
801static pa_sample_format_t drvHostAudioPaPropsToPulse(PCPDMAUDIOPCMPROPS pProps)
802{
803 switch (PDMAudioPropsSampleSize(pProps))
804 {
805 case 1:
806 if (!PDMAudioPropsIsSigned(pProps))
807 return PA_SAMPLE_U8;
808 break;
809
810 case 2:
811 if (PDMAudioPropsIsSigned(pProps))
812 return PDMAudioPropsIsLittleEndian(pProps) ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
813 break;
814
815#ifdef PA_SAMPLE_S32LE
816 case 4:
817 if (PDMAudioPropsIsSigned(pProps))
818 return PDMAudioPropsIsLittleEndian(pProps) ? PA_SAMPLE_S32LE : PA_SAMPLE_S32BE;
819 break;
820#endif
821 }
822
823 AssertMsgFailed(("%RU8%s not supported\n", PDMAudioPropsSampleSize(pProps), PDMAudioPropsIsSigned(pProps) ? "S" : "U"));
824 return PA_SAMPLE_INVALID;
825}
826
827
828/**
829 * Converts from pulse audio sample specification to PDM PCM audio properties.
830 *
831 * Worker for the stream creation code.
832 *
833 * @returns VBox status code.
834 * @param pProps The PDM audio source properties.
835 * @param enmPulseFmt The PA format.
836 * @param cChannels The number of channels.
837 * @param uHz The frequency.
838 */
839static int drvHostAudioPaToAudioProps(PPDMAUDIOPCMPROPS pProps, pa_sample_format_t enmPulseFmt, uint8_t cChannels, uint32_t uHz)
840{
841 AssertReturn(cChannels > 0, VERR_INVALID_PARAMETER);
842 AssertReturn(cChannels < 16, VERR_INVALID_PARAMETER);
843
844 switch (enmPulseFmt)
845 {
846 case PA_SAMPLE_U8:
847 PDMAudioPropsInit(pProps, 1 /*8-bit*/, false /*signed*/, cChannels, uHz);
848 break;
849
850 case PA_SAMPLE_S16LE:
851 PDMAudioPropsInitEx(pProps, 2 /*16-bit*/, true /*signed*/, cChannels, uHz, true /*fLittleEndian*/, false /*fRaw*/);
852 break;
853
854 case PA_SAMPLE_S16BE:
855 PDMAudioPropsInitEx(pProps, 2 /*16-bit*/, true /*signed*/, cChannels, uHz, false /*fLittleEndian*/, false /*fRaw*/);
856 break;
857
858#ifdef PA_SAMPLE_S32LE
859 case PA_SAMPLE_S32LE:
860 PDMAudioPropsInitEx(pProps, 4 /*32-bit*/, true /*signed*/, cChannels, uHz, true /*fLittleEndian*/, false /*fRaw*/);
861 break;
862#endif
863
864#ifdef PA_SAMPLE_S32BE
865 case PA_SAMPLE_S32BE:
866 PDMAudioPropsInitEx(pProps, 4 /*32-bit*/, true /*signed*/, cChannels, uHz, false /*fLittleEndian*/, false /*fRaw*/);
867 break;
868#endif
869
870 default:
871 AssertLogRelMsgFailed(("PulseAudio: Format (%d) not supported\n", enmPulseFmt));
872 return VERR_NOT_SUPPORTED;
873 }
874
875 return VINF_SUCCESS;
876}
877
878
879/**
880 * Worker that does the actual creation of an PA stream.
881 *
882 * @returns VBox status code.
883 * @param pThis Our driver instance data.
884 * @param pStreamPA Our stream data.
885 * @param pszName How we name the stream.
886 * @param pCfgAcq The requested stream properties, the Props member is
887 * updated upon successful return.
888 *
889 * @note Caller owns the mainloop lock.
890 */
891static int drvHostAudioPaStreamCreateLocked(PDRVHOSTPULSEAUDIO pThis, PPULSEAUDIOSTREAM pStreamPA,
892 const char *pszName, PPDMAUDIOSTREAMCFG pCfgAcq)
893{
894 /*
895 * Create the stream.
896 */
897 pa_stream *pStream = pa_stream_new(pThis->pContext, pszName, &pStreamPA->SampleSpec, NULL /* pa_channel_map */);
898 if (!pStream)
899 {
900 LogRel(("PulseAudio: Failed to create stream '%s': %s (%d)\n",
901 pszName, pa_strerror(pa_context_errno(pThis->pContext)), pa_context_errno(pThis->pContext)));
902 return VERR_AUDIO_STREAM_COULD_NOT_CREATE;
903 }
904
905 /*
906 * Set the state callback, and in debug builds a few more...
907 */
908#ifdef DEBUG
909 pa_stream_set_write_callback( pStream, drvHostAudioPaStreamReqWriteDebugCallback, pStreamPA);
910 pa_stream_set_underflow_callback( pStream, drvHostAudioPaStreamUnderflowDebugCallback, pStreamPA);
911 if (pCfgAcq->enmDir == PDMAUDIODIR_OUT)
912 pa_stream_set_overflow_callback(pStream, drvHostAudioPaStreamOverflowDebugCallback, pStreamPA);
913#endif
914 pa_stream_set_state_callback( pStream, drvHostAudioPaStreamStateChangedCallback, pThis);
915
916 /*
917 * Connect the stream.
918 */
919 int rc;
920 unsigned const fFlags = PA_STREAM_START_CORKED /* Require explicit starting (uncorking). */
921 /* For using pa_stream_get_latency() and pa_stream_get_time(). */
922 | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE
923#if PA_API_VERSION >= 12
924 | PA_STREAM_ADJUST_LATENCY
925#endif
926 ;
927 if (pCfgAcq->enmDir == PDMAUDIODIR_IN)
928 {
929 LogFunc(("Input stream attributes: maxlength=%d fragsize=%d\n",
930 pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.fragsize));
931 rc = pa_stream_connect_record(pStream, NULL /*dev*/, &pStreamPA->BufAttr, (pa_stream_flags_t)fFlags);
932 }
933 else
934 {
935 LogFunc(("Output buffer attributes: maxlength=%d tlength=%d prebuf=%d minreq=%d\n",
936 pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.tlength, pStreamPA->BufAttr.prebuf, pStreamPA->BufAttr.minreq));
937 rc = pa_stream_connect_playback(pStream, NULL /*dev*/, &pStreamPA->BufAttr, (pa_stream_flags_t)fFlags,
938 NULL /*volume*/, NULL /*sync_stream*/);
939 }
940 if (rc >= 0)
941 {
942 /*
943 * Wait for the stream to become ready.
944 */
945 uint64_t const nsStart = RTTimeNanoTS();
946 pa_stream_state_t enmStreamState;
947 while ( (enmStreamState = pa_stream_get_state(pStream)) != PA_STREAM_READY
948 && PA_STREAM_IS_GOOD(enmStreamState)
949 && RTTimeNanoTS() - nsStart < RT_NS_10SEC /* not really timed */ )
950 drvHostAudioPaMainloopWait(pThis);
951 if (enmStreamState == PA_STREAM_READY)
952 {
953 LogFunc(("Connecting stream took %'RU64 ns\n", RTTimeNanoTS() - nsStart));
954#ifdef LOG_ENABLED
955 pStreamPA->tsStartUs = pa_rtclock_now();
956#endif
957 /*
958 * Update the buffer attributes.
959 */
960 const pa_buffer_attr *pBufAttribs = pa_stream_get_buffer_attr(pStream);
961 AssertPtr(pBufAttribs);
962 if (pBufAttribs)
963 {
964 pStreamPA->BufAttr = *pBufAttribs;
965 LogFunc(("Obtained %s buffer attributes: maxlength=%RU32 tlength=%RU32 prebuf=%RU32 minreq=%RU32 fragsize=%RU32\n",
966 pCfgAcq->enmDir == PDMAUDIODIR_IN ? "input" : "output", pBufAttribs->maxlength, pBufAttribs->tlength,
967 pBufAttribs->prebuf, pBufAttribs->minreq, pBufAttribs->fragsize));
968
969 /*
970 * Convert the sample spec back to PDM speak.
971 * Note! This isn't strictly speaking needed as SampleSpec has *not* been
972 * modified since the caller converted it from pCfgReq.
973 */
974 rc = drvHostAudioPaToAudioProps(&pCfgAcq->Props, pStreamPA->SampleSpec.format,
975 pStreamPA->SampleSpec.channels, pStreamPA->SampleSpec.rate);
976 if (RT_SUCCESS(rc))
977 {
978 pStreamPA->pStream = pStream;
979 LogFlowFunc(("returns VINF_SUCCESS\n"));
980 return VINF_SUCCESS;
981 }
982 }
983 else
984 {
985 LogRelMax(99, ("PulseAudio: Failed to get buffer attribs for stream '%s': %s (%d)\n",
986 pszName, pa_strerror(pa_context_errno(pThis->pContext)), pa_context_errno(pThis->pContext)));
987 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
988 }
989 }
990 else
991 {
992 LogRelMax(99, ("PulseAudio: Failed to initialize stream '%s': state=%d, waited %'RU64 ns\n",
993 pszName, enmStreamState, RTTimeNanoTS() - nsStart));
994 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
995 }
996 pa_stream_disconnect(pStream);
997 }
998 else
999 {
1000 LogRelMax(99, ("PulseAudio: Could not connect %s stream '%s': %s (%d/%d)\n",
1001 pCfgAcq->enmDir == PDMAUDIODIR_IN ? "input" : "output",
1002 pszName, pa_strerror(pa_context_errno(pThis->pContext)), pa_context_errno(pThis->pContext), rc));
1003 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
1004 }
1005
1006 pa_stream_unref(pStream);
1007 Assert(RT_FAILURE_NP(rc));
1008 LogFlowFunc(("returns %Rrc\n", rc));
1009 return rc;
1010}
1011
1012
1013/**
1014 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
1015 */
1016static DECLCALLBACK(int) drvHostAudioPaHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1017 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
1018{
1019 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1020 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1021 AssertPtrReturn(pStreamPA, VERR_INVALID_POINTER);
1022 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
1023 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
1024 AssertReturn(pCfgReq->enmDir == PDMAUDIODIR_IN || pCfgReq->enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
1025 Assert(PDMAudioStrmCfgEquals(pCfgReq, pCfgAcq));
1026 int rc;
1027
1028 /*
1029 * Prepare name, sample spec and the stream instance data.
1030 */
1031 char szName[256];
1032 RTStrPrintf(szName, sizeof(szName), "VirtualBox %s [%s]",
1033 pCfgReq->enmDir == PDMAUDIODIR_IN
1034 ? PDMAudioRecSrcGetName(pCfgReq->u.enmSrc) : PDMAudioPlaybackDstGetName(pCfgReq->u.enmDst),
1035 pThis->szStreamName);
1036
1037 pStreamPA->pDrv = pThis;
1038 pStreamPA->pDrainOp = NULL;
1039 pStreamPA->pbPeekBuf = NULL;
1040 pStreamPA->SampleSpec.rate = PDMAudioPropsHz(&pCfgReq->Props);
1041 pStreamPA->SampleSpec.channels = PDMAudioPropsChannels(&pCfgReq->Props);
1042 pStreamPA->SampleSpec.format = drvHostAudioPaPropsToPulse(&pCfgReq->Props);
1043
1044 LogFunc(("Opening '%s', rate=%dHz, channels=%d, format=%s\n", szName, pStreamPA->SampleSpec.rate,
1045 pStreamPA->SampleSpec.channels, pa_sample_format_to_string(pStreamPA->SampleSpec.format)));
1046
1047 if (pa_sample_spec_valid(&pStreamPA->SampleSpec))
1048 {
1049 /*
1050 * Set up buffer attributes according to the stream type.
1051 *
1052 * For output streams we configure pre-buffering as requested, since
1053 * there is little point in using a different size than DrvAudio. This
1054 * assumes that a 'drain' request will override the prebuf size.
1055 */
1056 pStreamPA->BufAttr.maxlength = UINT32_MAX; /* Let the PulseAudio server choose the biggest size it can handle. */
1057 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
1058 {
1059 pStreamPA->BufAttr.fragsize = PDMAudioPropsFramesToBytes(&pCfgReq->Props, pCfgReq->Backend.cFramesPeriod);
1060 LogFunc(("Requesting: BufAttr: fragsize=%RU32\n", pStreamPA->BufAttr.fragsize));
1061 /* (rlength, minreq and prebuf are playback only) */
1062 }
1063 else
1064 {
1065 pStreamPA->cUsLatency = PDMAudioPropsFramesToMicro(&pCfgReq->Props, pCfgReq->Backend.cFramesBufferSize);
1066 pStreamPA->BufAttr.tlength = pa_usec_to_bytes(pStreamPA->cUsLatency, &pStreamPA->SampleSpec);
1067 pStreamPA->BufAttr.minreq = PDMAudioPropsFramesToBytes(&pCfgReq->Props, pCfgReq->Backend.cFramesPeriod);
1068 pStreamPA->BufAttr.prebuf = pa_usec_to_bytes(PDMAudioPropsFramesToMicro(&pCfgReq->Props,
1069 pCfgReq->Backend.cFramesPreBuffering),
1070 &pStreamPA->SampleSpec);
1071 /* (fragsize is capture only) */
1072 LogRel2(("PulseAudio: Initial output latency is %RU64 us (%RU32 bytes)\n",
1073 pStreamPA->cUsLatency, pStreamPA->BufAttr.tlength));
1074 LogFunc(("Requesting: BufAttr: tlength=%RU32 maxLength=%RU32 minReq=%RU32 maxlength=-1\n",
1075 pStreamPA->BufAttr.tlength, pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.minreq));
1076 }
1077
1078 /*
1079 * Do the actual PA stream creation.
1080 */
1081 pa_threaded_mainloop_lock(pThis->pMainLoop);
1082 rc = drvHostAudioPaStreamCreateLocked(pThis, pStreamPA, szName, pCfgAcq);
1083 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1084 if (RT_SUCCESS(rc))
1085 {
1086 /*
1087 * Set the acquired stream config according to the actual buffer
1088 * attributes we got and the stream type.
1089 */
1090 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
1091 {
1092 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsBytesToFrames(&pCfgAcq->Props, pStreamPA->BufAttr.fragsize);
1093 pCfgAcq->Backend.cFramesBufferSize = pStreamPA->BufAttr.maxlength != UINT32_MAX /* paranoia */
1094 ? PDMAudioPropsBytesToFrames(&pCfgAcq->Props, pStreamPA->BufAttr.maxlength)
1095 : pCfgAcq->Backend.cFramesPeriod * 2 /* whatever */;
1096 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod;
1097 }
1098 else
1099 {
1100 pCfgAcq->Backend.cFramesPeriod = PDMAudioPropsBytesToFrames(&pCfgAcq->Props, pStreamPA->BufAttr.minreq);
1101 pCfgAcq->Backend.cFramesBufferSize = PDMAudioPropsBytesToFrames(&pCfgAcq->Props, pStreamPA->BufAttr.tlength);
1102 pCfgAcq->Backend.cFramesPreBuffering = pCfgReq->Backend.cFramesPreBuffering
1103 * pCfgAcq->Backend.cFramesBufferSize
1104 / RT_MAX(pCfgReq->Backend.cFramesBufferSize, 1);
1105 }
1106 PDMAudioStrmCfgCopy(&pStreamPA->Cfg, pCfgAcq);
1107 }
1108 }
1109 else
1110 {
1111 LogRel(("PulseAudio: Unsupported sample specification for stream '%s'\n", szName));
1112 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
1113 }
1114
1115 LogFlowFuncLeaveRC(rc);
1116 return rc;
1117}
1118
1119/**
1120 * Cancel and release any pending stream requests (drain and cork/uncork).
1121 *
1122 * @note Caller has locked the mainloop.
1123 */
1124static void drvHostAudioPaStreamCancelAndReleaseOperations(PPULSEAUDIOSTREAM pStreamPA)
1125{
1126 if (pStreamPA->pDrainOp)
1127 {
1128 LogFlowFunc(("drain operation (%p) status: %d\n", pStreamPA->pDrainOp, pa_operation_get_state(pStreamPA->pDrainOp)));
1129 pa_operation_cancel(pStreamPA->pDrainOp);
1130 pa_operation_unref(pStreamPA->pDrainOp);
1131 pStreamPA->pDrainOp = NULL;
1132 }
1133
1134 if (pStreamPA->pCorkOp)
1135 {
1136 LogFlowFunc(("cork operation (%p) status: %d\n", pStreamPA->pCorkOp, pa_operation_get_state(pStreamPA->pCorkOp)));
1137 pa_operation_cancel(pStreamPA->pCorkOp);
1138 pa_operation_unref(pStreamPA->pCorkOp);
1139 pStreamPA->pCorkOp = NULL;
1140 }
1141
1142 if (pStreamPA->pTriggerOp)
1143 {
1144 LogFlowFunc(("trigger operation (%p) status: %d\n", pStreamPA->pTriggerOp, pa_operation_get_state(pStreamPA->pTriggerOp)));
1145 pa_operation_cancel(pStreamPA->pTriggerOp);
1146 pa_operation_unref(pStreamPA->pTriggerOp);
1147 pStreamPA->pTriggerOp = NULL;
1148 }
1149}
1150
1151
1152/**
1153 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
1154 */
1155static DECLCALLBACK(int) drvHostAudioPaHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1156{
1157 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1158 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1159 AssertPtrReturn(pStreamPA, VERR_INVALID_POINTER);
1160
1161 if (pStreamPA->pStream)
1162 {
1163 pa_threaded_mainloop_lock(pThis->pMainLoop);
1164
1165 drvHostAudioPaStreamCancelAndReleaseOperations(pStreamPA);
1166 pa_stream_disconnect(pStreamPA->pStream);
1167
1168 pa_stream_unref(pStreamPA->pStream);
1169 pStreamPA->pStream = NULL;
1170
1171 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1172 }
1173
1174 return VINF_SUCCESS;
1175}
1176
1177
1178/**
1179 * Common worker for the cork/uncork completion callbacks.
1180 * @note This is fully async, so nobody is waiting for this.
1181 */
1182static void drvHostAudioPaStreamCorkUncorkCommon(PPULSEAUDIOSTREAM pStreamPA, int fSuccess, const char *pszOperation)
1183{
1184 AssertPtrReturnVoid(pStreamPA);
1185 LogFlowFunc(("%s '%s': fSuccess=%RTbool\n", pszOperation, pStreamPA->Cfg.szName, fSuccess));
1186
1187 if (!fSuccess)
1188 drvHostAudioPaError(pStreamPA->pDrv, "%s stream '%s' failed", pszOperation, pStreamPA->Cfg.szName);
1189
1190 if (pStreamPA->pCorkOp)
1191 {
1192 pa_operation_unref(pStreamPA->pCorkOp);
1193 pStreamPA->pCorkOp = NULL;
1194 }
1195}
1196
1197
1198/**
1199 * Completion callback used with pa_stream_cork(,false,).
1200 */
1201static void drvHostAudioPaStreamUncorkCompletionCallback(pa_stream *pStream, int fSuccess, void *pvUser)
1202{
1203 RT_NOREF(pStream);
1204 drvHostAudioPaStreamCorkUncorkCommon((PPULSEAUDIOSTREAM)pvUser, fSuccess, "Uncorking");
1205}
1206
1207
1208/**
1209 * Completion callback used with pa_stream_cork(,true,).
1210 */
1211static void drvHostAudioPaStreamCorkCompletionCallback(pa_stream *pStream, int fSuccess, void *pvUser)
1212{
1213 RT_NOREF(pStream);
1214 drvHostAudioPaStreamCorkUncorkCommon((PPULSEAUDIOSTREAM)pvUser, fSuccess, "Corking");
1215}
1216
1217
1218/**
1219 * @ interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
1220 */
1221static DECLCALLBACK(int) drvHostAudioPaHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1222{
1223 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1224 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1225 LogFlowFunc(("\n"));
1226
1227 /*
1228 * Uncork (start or resume playback/capture) the stream.
1229 */
1230 pa_threaded_mainloop_lock(pThis->pMainLoop);
1231
1232 drvHostAudioPaStreamCancelAndReleaseOperations(pStreamPA);
1233 pStreamPA->pCorkOp = pa_stream_cork(pStreamPA->pStream, 0 /*uncork it*/,
1234 drvHostAudioPaStreamUncorkCompletionCallback, pStreamPA);
1235 LogFlowFunc(("Uncorking '%s': %p (async)\n", pStreamPA->Cfg.szName, pStreamPA->pCorkOp));
1236 int const rc = pStreamPA->pCorkOp ? VINF_SUCCESS
1237 : drvHostAudioPaError(pThis, "pa_stream_cork('%s', 0 /*uncork it*/,,) failed", pStreamPA->Cfg.szName);
1238
1239
1240 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1241
1242 LogFlowFunc(("returns %Rrc\n", rc));
1243 return rc;
1244}
1245
1246
1247/**
1248 * @ interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
1249 */
1250static DECLCALLBACK(int) drvHostAudioPaHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1251{
1252 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1253 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1254 LogFlowFunc(("\n"));
1255
1256 pa_threaded_mainloop_lock(pThis->pMainLoop);
1257
1258 /*
1259 * For output streams, we will ignore the request if there is a pending drain
1260 * as it will cork the stream in the end.
1261 */
1262 if (pStreamPA->Cfg.enmDir == PDMAUDIODIR_OUT)
1263 {
1264 if (pStreamPA->pDrainOp)
1265 {
1266 pa_operation_state_t const enmOpState = pa_operation_get_state(pStreamPA->pDrainOp);
1267 if (enmOpState == PA_OPERATION_RUNNING)
1268 {
1269 LogFlowFunc(("Drain (%p) already running on '%s', skipping.\n", pStreamPA->pDrainOp, pStreamPA->Cfg.szName));
1270 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1271 return VINF_SUCCESS;
1272 }
1273 LogFlowFunc(("Drain (%p) not running: %d\n", pStreamPA->pDrainOp, enmOpState));
1274 }
1275 }
1276 /*
1277 * For input stream we always cork it, but we clean up the peek buffer first.
1278 */
1279 /** @todo r=bird: It is (probably) not technically be correct to drop the peek buffer
1280 * here when we're only pausing the stream (VM paused) as it means we'll
1281 * risk underruns when later resuming. */
1282 else if (pStreamPA->pbPeekBuf) /** @todo Do we need to drop the peek buffer?*/
1283 {
1284 pStreamPA->pbPeekBuf = NULL;
1285 pStreamPA->cbPeekBuf = 0;
1286 pa_stream_drop(pStreamPA->pStream);
1287 }
1288
1289 /*
1290 * Cork (pause playback/capture) the stream.
1291 */
1292 drvHostAudioPaStreamCancelAndReleaseOperations(pStreamPA);
1293 pStreamPA->pCorkOp = pa_stream_cork(pStreamPA->pStream, 1 /* cork it */,
1294 drvHostAudioPaStreamCorkCompletionCallback, pStreamPA);
1295 LogFlowFunc(("Corking '%s': %p (async)\n", pStreamPA->Cfg.szName, pStreamPA->pCorkOp));
1296 int const rc = pStreamPA->pCorkOp ? VINF_SUCCESS
1297 : drvHostAudioPaError(pThis, "pa_stream_cork('%s', 1 /*cork*/,,) failed", pStreamPA->Cfg.szName);
1298
1299 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1300 LogFlowFunc(("returns %Rrc\n", rc));
1301 return rc;
1302}
1303
1304
1305/**
1306 * @ interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
1307 */
1308static DECLCALLBACK(int) drvHostAudioPaHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1309{
1310 /* Same as disable. */
1311 return drvHostAudioPaHA_StreamDisable(pInterface, pStream);
1312}
1313
1314
1315/**
1316 * @ interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
1317 */
1318static DECLCALLBACK(int) drvHostAudioPaHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1319{
1320 /* Same as enable. */
1321 return drvHostAudioPaHA_StreamEnable(pInterface, pStream);
1322}
1323
1324
1325/**
1326 * Pulse audio pa_stream_drain() completion callback.
1327 * @note This is fully async, so nobody is waiting for this.
1328 */
1329static void drvHostAudioPaStreamDrainCompletionCallback(pa_stream *pStream, int fSuccess, void *pvUser)
1330{
1331 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pvUser;
1332 AssertPtrReturnVoid(pStreamPA);
1333 Assert(pStreamPA->pStream == pStream);
1334 LogFlowFunc(("'%s': fSuccess=%RTbool\n", pStreamPA->Cfg.szName, fSuccess));
1335
1336 if (!fSuccess)
1337 drvHostAudioPaError(pStreamPA->pDrv, "Draining stream '%s' failed", pStreamPA->Cfg.szName);
1338
1339 /* Now cork the stream (doing it unconditionally atm). */
1340 if (pStreamPA->pCorkOp)
1341 {
1342 LogFlowFunc(("Cancelling & releasing cork/uncork operation %p (state: %d)\n",
1343 pStreamPA->pCorkOp, pa_operation_get_state(pStreamPA->pCorkOp)));
1344 pa_operation_cancel(pStreamPA->pCorkOp);
1345 pa_operation_unref(pStreamPA->pCorkOp);
1346 }
1347
1348 pStreamPA->pCorkOp = pa_stream_cork(pStream, 1 /* cork it*/, drvHostAudioPaStreamCorkCompletionCallback, pStreamPA);
1349 if (pStreamPA->pCorkOp)
1350 LogFlowFunc(("Started cork operation %p of %s (following drain)\n", pStreamPA->pCorkOp, pStreamPA->Cfg.szName));
1351 else
1352 drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_cork failed on '%s' (following drain)", pStreamPA->Cfg.szName);
1353}
1354
1355
1356/**
1357 * Callback used with pa_stream_tigger(), starts draining.
1358 */
1359static void drvHostAudioPaStreamTriggerCompletionCallback(pa_stream *pStream, int fSuccess, void *pvUser)
1360{
1361 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pvUser;
1362 AssertPtrReturnVoid(pStreamPA);
1363 RT_NOREF(pStream);
1364 LogFlowFunc(("'%s': fSuccess=%RTbool\n", pStreamPA->Cfg.szName, fSuccess));
1365
1366 if (!fSuccess)
1367 drvHostAudioPaError(pStreamPA->pDrv, "Forcing playback before drainig '%s' failed", pStreamPA->Cfg.szName);
1368
1369 if (pStreamPA->pTriggerOp)
1370 {
1371 pa_operation_unref(pStreamPA->pTriggerOp);
1372 pStreamPA->pTriggerOp = NULL;
1373 }
1374}
1375
1376
1377/**
1378 * @ interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
1379 */
1380static DECLCALLBACK(int) drvHostAudioPaHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1381{
1382 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1383 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1384 AssertReturn(pStreamPA->Cfg.enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
1385 LogFlowFunc(("\n"));
1386
1387 pa_threaded_mainloop_lock(pThis->pMainLoop);
1388
1389 /*
1390 * If there is a drain running already, don't try issue another as pulse
1391 * doesn't support more than one concurrent drain per stream.
1392 */
1393 if (pStreamPA->pDrainOp)
1394 {
1395 if (pa_operation_get_state(pStreamPA->pDrainOp) == PA_OPERATION_RUNNING)
1396 {
1397 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1398 LogFlowFunc(("returns VINF_SUCCESS (drain already running)\n"));
1399 return VINF_SUCCESS;
1400 }
1401 LogFlowFunc(("Releasing drain operation %p (state: %d)\n", pStreamPA->pDrainOp, pa_operation_get_state(pStreamPA->pDrainOp)));
1402 pa_operation_unref(pStreamPA->pDrainOp);
1403 pStreamPA->pDrainOp = NULL;
1404 }
1405
1406 /*
1407 * Make sure pre-buffered data is played before we drain it.
1408 *
1409 * ASSUMES that the async stream requests are executed in the order they're
1410 * issued here, so that we avoid waiting for the trigger request to complete.
1411 */
1412 int rc = VINF_SUCCESS;
1413 if (true /** @todo skip this if we're already playing or haven't written any data to the stream since xxxx. */)
1414 {
1415 if (pStreamPA->pTriggerOp)
1416 {
1417 LogFlowFunc(("Cancelling & releasing trigger operation %p (state: %d)\n",
1418 pStreamPA->pTriggerOp, pa_operation_get_state(pStreamPA->pTriggerOp)));
1419 pa_operation_cancel(pStreamPA->pTriggerOp);
1420 pa_operation_unref(pStreamPA->pTriggerOp);
1421 }
1422 pStreamPA->pTriggerOp = pa_stream_trigger(pStreamPA->pStream, drvHostAudioPaStreamTriggerCompletionCallback, pStreamPA);
1423 if (pStreamPA->pTriggerOp)
1424 LogFlowFunc(("Started tigger operation %p on %s\n", pStreamPA->pTriggerOp, pStreamPA->Cfg.szName));
1425 else
1426 rc = drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_trigger failed on '%s'", pStreamPA->Cfg.szName);
1427 }
1428
1429 /*
1430 * Initiate the draining (async), will cork the stream when it completes.
1431 */
1432 pStreamPA->pDrainOp = pa_stream_drain(pStreamPA->pStream, drvHostAudioPaStreamDrainCompletionCallback, pStreamPA);
1433 if (pStreamPA->pDrainOp)
1434 LogFlowFunc(("Started drain operation %p of %s\n", pStreamPA->pDrainOp, pStreamPA->Cfg.szName));
1435 else
1436 rc = drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_drain failed on '%s'", pStreamPA->Cfg.szName);
1437
1438 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1439 LogFlowFunc(("returns %Rrc\n", rc));
1440 return rc;
1441}
1442
1443
1444/**
1445 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
1446 */
1447static DECLCALLBACK(int) drvHostAudioPaHA_StreamControl(PPDMIHOSTAUDIO pInterface,
1448 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
1449{
1450 /** @todo r=bird: I'd like to get rid of this pfnStreamControl method,
1451 * replacing it with individual StreamXxxx methods. That would save us
1452 * potentally huge switches and more easily see which drivers implement
1453 * which operations (grep for pfnStreamXxxx). */
1454 switch (enmStreamCmd)
1455 {
1456 case PDMAUDIOSTREAMCMD_ENABLE:
1457 return drvHostAudioPaHA_StreamEnable(pInterface, pStream);
1458 case PDMAUDIOSTREAMCMD_DISABLE:
1459 return drvHostAudioPaHA_StreamDisable(pInterface, pStream);
1460 case PDMAUDIOSTREAMCMD_PAUSE:
1461 return drvHostAudioPaHA_StreamPause(pInterface, pStream);
1462 case PDMAUDIOSTREAMCMD_RESUME:
1463 return drvHostAudioPaHA_StreamResume(pInterface, pStream);
1464 case PDMAUDIOSTREAMCMD_DRAIN:
1465 return drvHostAudioPaHA_StreamDrain(pInterface, pStream);
1466
1467 case PDMAUDIOSTREAMCMD_END:
1468 case PDMAUDIOSTREAMCMD_32BIT_HACK:
1469 case PDMAUDIOSTREAMCMD_INVALID:
1470 /* no default*/
1471 break;
1472 }
1473 return VERR_NOT_SUPPORTED;
1474}
1475
1476
1477/**
1478 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
1479 */
1480static DECLCALLBACK(uint32_t) drvHostAudioPaHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1481{
1482 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1483 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1484 uint32_t cbReadable = 0;
1485 if (pStreamPA->Cfg.enmDir == PDMAUDIODIR_IN)
1486 {
1487 pa_threaded_mainloop_lock(pThis->pMainLoop);
1488
1489 pa_stream_state_t const enmState = pa_stream_get_state(pStreamPA->pStream);
1490 if (PA_STREAM_IS_GOOD(enmState))
1491 {
1492 size_t cbReadablePa = pa_stream_readable_size(pStreamPA->pStream);
1493 if (cbReadablePa != (size_t)-1)
1494 cbReadable = (uint32_t)cbReadablePa;
1495 else
1496 drvHostAudioPaError(pThis, "pa_stream_readable_size failed on '%s'", pStreamPA->Cfg.szName);
1497 }
1498 else
1499 LogFunc(("non-good stream state: %d\n", enmState));
1500
1501 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1502 }
1503 Log3Func(("returns %#x (%u)\n", cbReadable, cbReadable));
1504 return cbReadable;
1505}
1506
1507
1508/**
1509 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
1510 */
1511static DECLCALLBACK(uint32_t) drvHostAudioPaHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1512{
1513 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1514 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1515 uint32_t cbWritable = 0;
1516 if (pStreamPA->Cfg.enmDir == PDMAUDIODIR_OUT)
1517 {
1518 pa_threaded_mainloop_lock(pThis->pMainLoop);
1519
1520 pa_stream_state_t const enmState = pa_stream_get_state(pStreamPA->pStream);
1521 if (PA_STREAM_IS_GOOD(enmState))
1522 {
1523 size_t cbWritablePa = pa_stream_writable_size(pStreamPA->pStream);
1524 if (cbWritablePa != (size_t)-1)
1525 cbWritable = cbWritablePa <= UINT32_MAX ? (uint32_t)cbWritablePa : UINT32_MAX;
1526 else
1527 drvHostAudioPaError(pThis, "pa_stream_writable_size failed on '%s'", pStreamPA->Cfg.szName);
1528 }
1529 else
1530 LogFunc(("non-good stream state: %d\n", enmState));
1531
1532 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1533 }
1534 Log3Func(("returns %#x (%u) [max=%#RX32 min=%#RX32]\n",
1535 cbWritable, cbWritable, pStreamPA->BufAttr.maxlength, pStreamPA->BufAttr.minreq));
1536 return cbWritable;
1537}
1538
1539
1540/**
1541 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
1542 */
1543static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostAudioPaHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
1544{
1545 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1546 RT_NOREF(pStream);
1547
1548 /* Check PulseAudio's general status. */
1549 if (pThis->pContext)
1550 {
1551 pa_context_state_t const enmState = pa_context_get_state(pThis->pContext);
1552 if (PA_CONTEXT_IS_GOOD(enmState))
1553 {
1554 /** @todo should we check the actual stream state? */
1555 return PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
1556 }
1557 LogFunc(("non-good context state: %d\n", enmState));
1558 }
1559 else
1560 LogFunc(("No context!\n"));
1561 return PDMAUDIOSTREAMSTS_FLAGS_NONE;
1562}
1563
1564
1565/**
1566 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
1567 */
1568static DECLCALLBACK(int) drvHostAudioPaHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1569 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1570{
1571 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1572 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1573 AssertPtrReturn(pStreamPA, VERR_INVALID_POINTER);
1574 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1575 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1576 AssertPtrReturn(pcbWritten, VERR_INVALID_POINTER);
1577
1578 pa_threaded_mainloop_lock(pThis->pMainLoop);
1579
1580#ifdef LOG_ENABLED
1581 const pa_usec_t tsNowUs = pa_rtclock_now();
1582 Log3Func(("play delta: %'RU64 us; cbBuf=%#x\n", tsNowUs - pStreamPA->tsLastReadWrittenUs, cbBuf));
1583 pStreamPA->tsLastReadWrittenUs = tsNowUs;
1584#endif
1585
1586 /*
1587 * Using a loop here so we can take maxlength into account when writing.
1588 */
1589 int rc = VINF_SUCCESS;
1590 uint32_t cbTotalWritten = 0;
1591 uint32_t iLoop;
1592 for (iLoop = 0; ; iLoop++)
1593 {
1594 size_t const cbWriteable = pa_stream_writable_size(pStreamPA->pStream);
1595 if ( cbWriteable != (size_t)-1
1596 && cbWriteable >= PDMAudioPropsFrameSize(&pStreamPA->Cfg.Props))
1597 {
1598 uint32_t cbToWrite = (uint32_t)RT_MIN(RT_MIN(cbWriteable, pStreamPA->BufAttr.maxlength), cbBuf);
1599 cbToWrite = PDMAudioPropsFloorBytesToFrame(&pStreamPA->Cfg.Props, cbToWrite);
1600 if (pa_stream_write(pStreamPA->pStream, pvBuf, cbToWrite, NULL /*pfnFree*/, 0 /*offset*/, PA_SEEK_RELATIVE) >= 0)
1601 {
1602 cbTotalWritten += cbToWrite;
1603 cbBuf -= cbToWrite;
1604 if (!cbBuf)
1605 break;
1606 pvBuf = (uint8_t const *)pvBuf + cbToWrite;
1607 Log3Func(("%#x left to write\n", cbBuf));
1608 }
1609 else
1610 {
1611 rc = drvHostAudioPaError(pStreamPA->pDrv, "Failed to write to output stream");
1612 break;
1613 }
1614 }
1615 else
1616 {
1617 if (cbWriteable == (size_t)-1)
1618 rc = drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_writable_size failed on '%s'", pStreamPA->Cfg.szName);
1619 break;
1620 }
1621 }
1622
1623 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1624
1625 *pcbWritten = cbTotalWritten;
1626 if (RT_SUCCESS(rc) || cbTotalWritten == 0)
1627 { /* likely */ }
1628 else
1629 {
1630 LogFunc(("Supressing %Rrc because we wrote %#x bytes\n", rc, cbTotalWritten));
1631 rc = VINF_SUCCESS;
1632 }
1633 Log3Func(("returns %Rrc *pcbWritten=%#x iLoop=%u\n", rc, cbTotalWritten, iLoop));
1634 return rc;
1635}
1636
1637
1638/**
1639 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
1640 */
1641static DECLCALLBACK(int) drvHostAudioPaHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
1642 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1643{
1644 PDRVHOSTPULSEAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPULSEAUDIO, IHostAudio);
1645 PPULSEAUDIOSTREAM pStreamPA = (PPULSEAUDIOSTREAM)pStream;
1646 AssertPtrReturn(pStreamPA, VERR_INVALID_POINTER);
1647 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1648 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1649 AssertPtrReturn(pcbRead, VERR_INVALID_POINTER);
1650
1651#ifdef LOG_ENABLED
1652 const pa_usec_t tsNowUs = pa_rtclock_now();
1653 Log3Func(("capture delta: %'RU64 us; cbBuf=%#x\n", tsNowUs - pStreamPA->tsLastReadWrittenUs, cbBuf));
1654 pStreamPA->tsLastReadWrittenUs = tsNowUs;
1655#endif
1656
1657 /*
1658 * If we have left over peek buffer space from the last call,
1659 * copy out the data from there.
1660 */
1661 uint32_t cbTotalRead = 0;
1662 if ( pStreamPA->pbPeekBuf
1663 && pStreamPA->offPeekBuf < pStreamPA->cbPeekBuf)
1664 {
1665 uint32_t cbToCopy = pStreamPA->cbPeekBuf - pStreamPA->offPeekBuf;
1666 if (cbToCopy >= cbBuf)
1667 {
1668 memcpy(pvBuf, &pStreamPA->pbPeekBuf[pStreamPA->offPeekBuf], cbBuf);
1669 pStreamPA->offPeekBuf += cbBuf;
1670 *pcbRead = cbBuf;
1671 if (cbToCopy == cbBuf)
1672 {
1673 pa_threaded_mainloop_lock(pThis->pMainLoop);
1674 pStreamPA->pbPeekBuf = NULL;
1675 pStreamPA->cbPeekBuf = 0;
1676 pa_stream_drop(pStreamPA->pStream);
1677 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1678 }
1679 Log3Func(("returns *pcbRead=%#x from prev peek buf (%#x/%#x)\n", cbBuf, pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf));
1680 return VINF_SUCCESS;
1681 }
1682
1683 memcpy(pvBuf, &pStreamPA->pbPeekBuf[pStreamPA->offPeekBuf], cbToCopy);
1684 cbBuf -= cbToCopy;
1685 pvBuf = (uint8_t *)pvBuf + cbToCopy;
1686 cbTotalRead += cbToCopy;
1687 pStreamPA->offPeekBuf = pStreamPA->cbPeekBuf;
1688 }
1689
1690 /*
1691 * Copy out what we can.
1692 */
1693 int rc = VINF_SUCCESS;
1694 pa_threaded_mainloop_lock(pThis->pMainLoop);
1695 while (cbBuf > 0)
1696 {
1697 /*
1698 * Drop the old peek buffer first, if we have one.
1699 */
1700 if (pStreamPA->pbPeekBuf)
1701 {
1702 Assert(pStreamPA->offPeekBuf >= pStreamPA->cbPeekBuf);
1703 pStreamPA->pbPeekBuf = NULL;
1704 pStreamPA->cbPeekBuf = 0;
1705 pa_stream_drop(pStreamPA->pStream);
1706 }
1707
1708 /*
1709 * Check if there is anything to read, the get the peek buffer for it.
1710 */
1711 size_t cbAvail = pa_stream_readable_size(pStreamPA->pStream);
1712 if (cbAvail > 0 && cbAvail != (size_t)-1)
1713 {
1714 pStreamPA->pbPeekBuf = NULL;
1715 pStreamPA->cbPeekBuf = 0;
1716 int rcPa = pa_stream_peek(pStreamPA->pStream, (const void **)&pStreamPA->pbPeekBuf, &pStreamPA->cbPeekBuf);
1717 if (rcPa == 0)
1718 {
1719 if (pStreamPA->cbPeekBuf)
1720 {
1721 if (pStreamPA->pbPeekBuf)
1722 {
1723 /*
1724 * We got data back. Copy it into the return buffer, return if it's full.
1725 */
1726 if (cbBuf < pStreamPA->cbPeekBuf)
1727 {
1728 memcpy(pvBuf, pStreamPA->pbPeekBuf, cbBuf);
1729 cbTotalRead += cbBuf;
1730 pStreamPA->offPeekBuf = cbBuf;
1731 cbBuf = 0;
1732 break;
1733 }
1734 memcpy(pvBuf, pStreamPA->pbPeekBuf, pStreamPA->cbPeekBuf);
1735 cbBuf -= pStreamPA->cbPeekBuf;
1736 pvBuf = (uint8_t *)pvBuf + pStreamPA->cbPeekBuf;
1737 cbTotalRead += pStreamPA->cbPeekBuf;
1738
1739 pStreamPA->pbPeekBuf = NULL;
1740 }
1741 else
1742 {
1743 /*
1744 * We got a hole (drop needed). We will skip it as we leave it to
1745 * the device's DMA engine to fill in buffer gaps with silence.
1746 */
1747 LogFunc(("pa_stream_peek returned a %#zx (%zu) byte hole - skipping.\n",
1748 pStreamPA->cbPeekBuf, pStreamPA->cbPeekBuf));
1749 }
1750 pStreamPA->cbPeekBuf = 0;
1751 pa_stream_drop(pStreamPA->pStream);
1752 }
1753 else
1754 {
1755 Assert(!pStreamPA->pbPeekBuf);
1756 LogFunc(("pa_stream_peek returned empty buffer\n"));
1757 break;
1758 }
1759 }
1760 else
1761 {
1762 rc = drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_peek failed on '%s' (%d)", pStreamPA->Cfg.szName, rcPa);
1763 pStreamPA->pbPeekBuf = NULL;
1764 pStreamPA->cbPeekBuf = 0;
1765 break;
1766 }
1767 }
1768 else
1769 {
1770 if (cbAvail != (size_t)-1)
1771 rc = drvHostAudioPaError(pStreamPA->pDrv, "pa_stream_readable_size failed on '%s'", pStreamPA->Cfg.szName);
1772 break;
1773 }
1774 }
1775 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1776
1777 *pcbRead = cbTotalRead;
1778 if (RT_SUCCESS(rc) || cbTotalRead == 0)
1779 { /* likely */ }
1780 else
1781 {
1782 LogFunc(("Supressing %Rrc because we're returning %#x bytes\n", rc, cbTotalRead));
1783 rc = VINF_SUCCESS;
1784 }
1785 Log3Func(("returns %Rrc *pcbRead=%#x (%#x left, peek %#x/%#x)\n",
1786 rc, cbTotalRead, cbBuf, pStreamPA->offPeekBuf, pStreamPA->cbPeekBuf));
1787 return rc;
1788}
1789
1790
1791/*********************************************************************************************************************************
1792* PDMIBASE *
1793*********************************************************************************************************************************/
1794
1795/**
1796 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1797 */
1798static DECLCALLBACK(void *) drvHostAudioPaQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1799{
1800 AssertPtrReturn(pInterface, NULL);
1801 AssertPtrReturn(pszIID, NULL);
1802
1803 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1804 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1805 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1806 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
1807
1808 return NULL;
1809}
1810
1811
1812/*********************************************************************************************************************************
1813* PDMDRVREG *
1814*********************************************************************************************************************************/
1815
1816/**
1817 * @interface_method_impl{PDMDRVREG,pfnPowerOff}
1818 */
1819static DECLCALLBACK(void) drvHostAudioPaPowerOff(PPDMDRVINS pDrvIns)
1820{
1821 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1822 LogFlowFuncEnter();
1823
1824 if (pThis->pMainLoop)
1825 pa_threaded_mainloop_stop(pThis->pMainLoop);
1826
1827 if (pThis->pContext)
1828 {
1829 pa_context_disconnect(pThis->pContext);
1830 pa_context_unref(pThis->pContext);
1831 pThis->pContext = NULL;
1832 }
1833
1834 if (pThis->pMainLoop)
1835 {
1836 pa_threaded_mainloop_free(pThis->pMainLoop);
1837 pThis->pMainLoop = NULL;
1838 }
1839
1840 LogFlowFuncLeave();
1841}
1842
1843
1844/**
1845 * Destructs a PulseAudio Audio driver instance.
1846 *
1847 * @copydoc FNPDMDRVDESTRUCT
1848 */
1849static DECLCALLBACK(void) drvHostAudioPaDestruct(PPDMDRVINS pDrvIns)
1850{
1851 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1852 LogFlowFuncEnter();
1853 drvHostAudioPaPowerOff(pDrvIns);
1854 LogFlowFuncLeave();
1855}
1856
1857
1858/**
1859 * Pulse audio callback for context status changes, init variant.
1860 *
1861 * Signalls our event semaphore so we can do a timed wait from
1862 * drvHostAudioPaConstruct().
1863 */
1864static void drvHostAudioPaCtxCallbackStateChangedInit(pa_context *pCtx, void *pvUser)
1865{
1866 AssertPtrReturnVoid(pCtx);
1867 PPULSEAUDIOSTATECHGCTX pStateChgCtx = (PPULSEAUDIOSTATECHGCTX)pvUser;
1868 pa_context_state_t enmCtxState = pa_context_get_state(pCtx);
1869 switch (enmCtxState)
1870 {
1871 case PA_CONTEXT_READY:
1872 case PA_CONTEXT_TERMINATED:
1873 case PA_CONTEXT_FAILED:
1874 AssertPtrReturnVoid(pStateChgCtx);
1875 pStateChgCtx->enmCtxState = enmCtxState;
1876 RTSemEventSignal(pStateChgCtx->hEvtInit);
1877 break;
1878
1879 default:
1880 break;
1881 }
1882}
1883
1884
1885/**
1886 * Constructs a PulseAudio Audio driver instance.
1887 *
1888 * @copydoc FNPDMDRVCONSTRUCT
1889 */
1890static DECLCALLBACK(int) drvHostAudioPaConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1891{
1892 RT_NOREF(pCfg, fFlags);
1893 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1894 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1895
1896 PDRVHOSTPULSEAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPULSEAUDIO);
1897 LogRel(("Audio: Initializing PulseAudio driver\n"));
1898
1899 /*
1900 * Initialize instance data.
1901 */
1902 pThis->pDrvIns = pDrvIns;
1903 /* IBase */
1904 pDrvIns->IBase.pfnQueryInterface = drvHostAudioPaQueryInterface;
1905 /* IHostAudio */
1906 pThis->IHostAudio.pfnGetConfig = drvHostAudioPaHA_GetConfig;
1907 pThis->IHostAudio.pfnGetDevices = drvHostAudioPaHA_GetDevices;
1908 pThis->IHostAudio.pfnGetStatus = drvHostAudioPaHA_GetStatus;
1909 pThis->IHostAudio.pfnStreamCreate = drvHostAudioPaHA_StreamCreate;
1910 pThis->IHostAudio.pfnStreamDestroy = drvHostAudioPaHA_StreamDestroy;
1911 pThis->IHostAudio.pfnStreamControl = drvHostAudioPaHA_StreamControl;
1912 pThis->IHostAudio.pfnStreamGetReadable = drvHostAudioPaHA_StreamGetReadable;
1913 pThis->IHostAudio.pfnStreamGetWritable = drvHostAudioPaHA_StreamGetWritable;
1914 pThis->IHostAudio.pfnStreamGetPending = NULL;
1915 pThis->IHostAudio.pfnStreamGetStatus = drvHostAudioPaHA_StreamGetStatus;
1916 pThis->IHostAudio.pfnStreamPlay = drvHostAudioPaHA_StreamPlay;
1917 pThis->IHostAudio.pfnStreamCapture = drvHostAudioPaHA_StreamCapture;
1918
1919 /*
1920 * Read configuration.
1921 */
1922 int rc2 = CFGMR3QueryString(pCfg, "StreamName", pThis->szStreamName, sizeof(pThis->szStreamName));
1923 AssertMsgRCReturn(rc2, ("Confguration error: No/bad \"StreamName\" value, rc=%Rrc\n", rc2), rc2);
1924
1925 /*
1926 * Load the pulse audio library.
1927 */
1928 int rc = audioLoadPulseLib();
1929 if (RT_SUCCESS(rc))
1930 LogRel(("PulseAudio: Using version %s\n", pa_get_library_version()));
1931 else
1932 {
1933 LogRel(("PulseAudio: Failed to load the PulseAudio shared library! Error %Rrc\n", rc));
1934 return rc;
1935 }
1936
1937 /*
1938 * Set up the basic pulse audio bits (remember the destructore is always called).
1939 */
1940 //pThis->fAbortLoop = false;
1941 pThis->pMainLoop = pa_threaded_mainloop_new();
1942 if (!pThis->pMainLoop)
1943 {
1944 LogRel(("PulseAudio: Failed to allocate main loop: %s\n", pa_strerror(pa_context_errno(pThis->pContext))));
1945 return VERR_NO_MEMORY;
1946 }
1947
1948 pThis->pContext = pa_context_new(pa_threaded_mainloop_get_api(pThis->pMainLoop), "VirtualBox");
1949 if (!pThis->pContext)
1950 {
1951 LogRel(("PulseAudio: Failed to allocate context: %s\n", pa_strerror(pa_context_errno(pThis->pContext))));
1952 return VERR_NO_MEMORY;
1953 }
1954
1955 if (pa_threaded_mainloop_start(pThis->pMainLoop) < 0)
1956 {
1957 LogRel(("PulseAudio: Failed to start threaded mainloop: %s\n", pa_strerror(pa_context_errno(pThis->pContext))));
1958 return VERR_AUDIO_BACKEND_INIT_FAILED;
1959 }
1960
1961 /*
1962 * Connect to the pulse audio server.
1963 *
1964 * We install an init state callback so we can do a timed wait in case
1965 * connecting to the pulseaudio server should take too long.
1966 */
1967 pThis->InitStateChgCtx.hEvtInit = NIL_RTSEMEVENT;
1968 pThis->InitStateChgCtx.enmCtxState = PA_CONTEXT_UNCONNECTED;
1969 rc = RTSemEventCreate(&pThis->InitStateChgCtx.hEvtInit);
1970 AssertLogRelRCReturn(rc, rc);
1971
1972 pa_threaded_mainloop_lock(pThis->pMainLoop);
1973 pa_context_set_state_callback(pThis->pContext, drvHostAudioPaCtxCallbackStateChangedInit, &pThis->InitStateChgCtx);
1974 if (!pa_context_connect(pThis->pContext, NULL /* pszServer */, PA_CONTEXT_NOFLAGS, NULL))
1975 {
1976 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1977
1978 rc = RTSemEventWait(pThis->InitStateChgCtx.hEvtInit, RT_MS_10SEC); /* 10 seconds should be plenty. */
1979 if (RT_SUCCESS(rc))
1980 {
1981 if (pThis->InitStateChgCtx.enmCtxState == PA_CONTEXT_READY)
1982 {
1983 /* Install the main state changed callback to know if something happens to our acquired context. */
1984 pa_threaded_mainloop_lock(pThis->pMainLoop);
1985 pa_context_set_state_callback(pThis->pContext, drvHostAudioPaCtxCallbackStateChanged, pThis /* pvUserData */);
1986 pa_threaded_mainloop_unlock(pThis->pMainLoop);
1987 }
1988 else
1989 {
1990 LogRel(("PulseAudio: Failed to initialize context (state %d, rc=%Rrc)\n", pThis->InitStateChgCtx.enmCtxState, rc));
1991 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
1992 }
1993 }
1994 else
1995 {
1996 LogRel(("PulseAudio: Waiting for context to become ready failed: %Rrc\n", rc));
1997 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
1998 }
1999 }
2000 else
2001 {
2002 pa_threaded_mainloop_unlock(pThis->pMainLoop);
2003 LogRel(("PulseAudio: Failed to connect to server: %s\n", pa_strerror(pa_context_errno(pThis->pContext))));
2004 rc = VERR_AUDIO_BACKEND_INIT_FAILED; /* bird: This used to be VINF_SUCCESS. */
2005 }
2006
2007 RTSemEventDestroy(pThis->InitStateChgCtx.hEvtInit);
2008 pThis->InitStateChgCtx.hEvtInit = NIL_RTSEMEVENT;
2009
2010 return rc;
2011}
2012
2013
2014/**
2015 * Pulse audio driver registration record.
2016 */
2017const PDMDRVREG g_DrvHostPulseAudio =
2018{
2019 /* u32Version */
2020 PDM_DRVREG_VERSION,
2021 /* szName */
2022 "PulseAudio",
2023 /* szRCMod */
2024 "",
2025 /* szR0Mod */
2026 "",
2027 /* pszDescription */
2028 "Pulse Audio host driver",
2029 /* fFlags */
2030 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2031 /* fClass. */
2032 PDM_DRVREG_CLASS_AUDIO,
2033 /* cMaxInstances */
2034 ~0U,
2035 /* cbInstance */
2036 sizeof(DRVHOSTPULSEAUDIO),
2037 /* pfnConstruct */
2038 drvHostAudioPaConstruct,
2039 /* pfnDestruct */
2040 drvHostAudioPaDestruct,
2041 /* pfnRelocate */
2042 NULL,
2043 /* pfnIOCtl */
2044 NULL,
2045 /* pfnPowerOn */
2046 NULL,
2047 /* pfnReset */
2048 NULL,
2049 /* pfnSuspend */
2050 NULL,
2051 /* pfnResume */
2052 NULL,
2053 /* pfnAttach */
2054 NULL,
2055 /* pfnDetach */
2056 NULL,
2057 /* pfnPowerOff */
2058 drvHostAudioPaPowerOff,
2059 /* pfnSoftReset */
2060 NULL,
2061 /* u32EndVersion */
2062 PDM_DRVREG_VERSION
2063};
2064
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