VirtualBox

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

Last change on this file since 89522 was 89522, checked in by vboxsync, 3 years ago

DrvHostAudioPulseAudio: Use the default BufAttr.minreq value (output only), otherwise I'm getting slightly distored output here on a builtin intel HDA device. Logging adjustments. bugref:9890

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