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