VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvAudio.cpp@ 59420

Last change on this file since 59420 was 59420, checked in by vboxsync, 9 years ago

Audio/DrvAudio: Use a critical section for (most) driver callbacks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.0 KB
Line 
1/* $Id: DrvAudio.cpp 59420 2016-01-20 14:53:24Z vboxsync $ */
2/** @file
3 * Intermediate audio driver header.
4 *
5 * @remarks Intermediate audio driver for connecting the audio device emulation
6 * with the host backend.
7 */
8
9/*
10 * Copyright (C) 2006-2015 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 * --------------------------------------------------------------------
20 *
21 * This code is based on: audio.c from QEMU AUDIO subsystem.
22 *
23 * QEMU Audio subsystem
24 *
25 * Copyright (c) 2003-2005 Vassili Karpov (malc)
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a copy
28 * of this software and associated documentation files (the "Software"), to deal
29 * in the Software without restriction, including without limitation the rights
30 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31 * copies of the Software, and to permit persons to whom the Software is
32 * furnished to do so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be included in
35 * all copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43 * THE SOFTWARE.
44 */
45#define LOG_GROUP LOG_GROUP_DRV_AUDIO
46#include <VBox/log.h>
47#include <VBox/vmm/pdm.h>
48#include <VBox/err.h>
49#include <VBox/vmm/mm.h>
50#include <VBox/vmm/pdmaudioifs.h>
51
52#include <iprt/alloc.h>
53#include <iprt/asm-math.h>
54#include <iprt/assert.h>
55#include <iprt/circbuf.h>
56#include <iprt/string.h>
57#include <iprt/uuid.h>
58
59#include "VBoxDD.h"
60
61#include <ctype.h>
62#include <stdlib.h>
63
64#include "DrvAudio.h"
65#include "AudioMixBuffer.h"
66
67static int drvAudioDestroyGstIn(PDRVAUDIO pThis, PPDMAUDIOGSTSTRMIN pGstStrmIn);
68
69static int drvAudioAllocHstIn(PDRVAUDIO pThis, const char *pszName, PPDMAUDIOSTREAMCFG pCfg, PDMAUDIORECSOURCE enmRecSource, PPDMAUDIOHSTSTRMIN *ppHstStrmIn);
70static int drvAudioDestroyHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn);
71
72int drvAudioAddHstOut(PDRVAUDIO pThis, const char *pszName, PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOHSTSTRMOUT *ppHstStrmOut)
73{
74 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
75 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
76 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
77
78 PPDMAUDIOHSTSTRMOUT pHstStrmOut;
79
80 int rc;
81 if ( conf.fixed_out.enabled /** @todo Get rid of these settings! */
82 && conf.fixed_out.greedy)
83 {
84 rc = drvAudioAllocHstOut(pThis, pszName, pCfg, &pHstStrmOut);
85 }
86 else
87 rc = VERR_NOT_FOUND;
88
89 if (RT_FAILURE(rc))
90 {
91 pHstStrmOut = drvAudioFindSpecificOut(pThis, NULL, pCfg);
92 if (!pHstStrmOut)
93 {
94 rc = drvAudioAllocHstOut(pThis, pszName, pCfg, &pHstStrmOut);
95 if (RT_FAILURE(rc))
96 pHstStrmOut = drvAudioFindAnyHstOut(pThis, NULL /* pHstStrmOut */);
97 }
98
99 rc = pHstStrmOut ? VINF_SUCCESS : rc;
100 }
101
102 if (RT_SUCCESS(rc))
103 *ppHstStrmOut = pHstStrmOut;
104
105 return rc;
106}
107
108static PDMAUDIOFMT drvAudioGetConfFormat(PCFGMNODE pCfgHandle, const char *pszKey,
109 PDMAUDIOFMT enmDefault, bool *pfDefault)
110{
111 if ( pCfgHandle == NULL
112 || pszKey == NULL)
113 {
114 *pfDefault = true;
115 return enmDefault;
116 }
117
118 char *pszValue = NULL;
119 int rc = CFGMR3QueryStringAlloc(pCfgHandle, pszKey, &pszValue);
120 if (RT_FAILURE(rc))
121 {
122 *pfDefault = true;
123 return enmDefault;
124 }
125
126 PDMAUDIOFMT fmt = drvAudioHlpStringToFormat(pszValue);
127 if (fmt == AUD_FMT_INVALID)
128 {
129 *pfDefault = true;
130 return enmDefault;
131 }
132
133 *pfDefault = false;
134 return fmt;
135}
136
137static int drvAudioGetConfInt(PCFGMNODE pCfgHandle, const char *pszKey,
138 int iDefault, bool *pfDefault)
139{
140
141 if ( pCfgHandle == NULL
142 || pszKey == NULL)
143 {
144 *pfDefault = true;
145 return iDefault;
146 }
147
148 uint64_t u64Data = 0;
149 int rc = CFGMR3QueryInteger(pCfgHandle, pszKey, &u64Data);
150 if (RT_FAILURE(rc))
151 {
152 *pfDefault = true;
153 return iDefault;
154
155 }
156
157 *pfDefault = false;
158 return u64Data;
159}
160
161static const char *drvAudioGetConfStr(PCFGMNODE pCfgHandle, const char *pszKey,
162 const char *pszDefault, bool *pfDefault)
163{
164 if ( pCfgHandle == NULL
165 || pszKey == NULL)
166 {
167 *pfDefault = true;
168 return pszDefault;
169 }
170
171 char *pszValue = NULL;
172 int rc = CFGMR3QueryStringAlloc(pCfgHandle, pszKey, &pszValue);
173 if (RT_FAILURE(rc))
174 {
175 *pfDefault = true;
176 return pszDefault;
177 }
178
179 *pfDefault = false;
180 return pszValue;
181}
182
183static int drvAudioProcessOptions(PCFGMNODE pCfgHandle, const char *pszPrefix, struct audio_option *opt)
184{
185 AssertPtrReturn(pCfgHandle, VERR_INVALID_POINTER);
186 AssertPtrReturn(pszPrefix, VERR_INVALID_POINTER);
187 AssertPtrReturn(opt, VERR_INVALID_POINTER);
188
189 PCFGMNODE pCfgChildHandle = NULL;
190 PCFGMNODE pCfgChildChildHandle = NULL;
191
192 /* If pCfgHandle is NULL, let NULL be passed to get int and get string functions..
193 * The getter function will return default values.
194 */
195 if (pCfgHandle != NULL)
196 {
197 /* If its audio general setting, need to traverse to one child node.
198 * /Devices/ichac97/0/LUN#0/Config/Audio
199 */
200 if(!strncmp(pszPrefix, "AUDIO", 5)) /** @todo Use a \#define */
201 {
202 pCfgChildHandle = CFGMR3GetFirstChild(pCfgHandle);
203 if(pCfgChildHandle)
204 pCfgHandle = pCfgChildHandle;
205 }
206 else
207 {
208 /* If its driver specific configuration , then need to traverse two level deep child
209 * child nodes. for eg. in case of DirectSoundConfiguration item
210 * /Devices/ichac97/0/LUN#0/Config/Audio/DirectSoundConfig
211 */
212 pCfgChildHandle = CFGMR3GetFirstChild(pCfgHandle);
213 if (pCfgChildHandle)
214 {
215 pCfgChildChildHandle = CFGMR3GetFirstChild(pCfgChildHandle);
216 if (pCfgChildChildHandle)
217 pCfgHandle = pCfgChildChildHandle;
218 }
219 }
220 }
221
222 for (; opt->name; opt++)
223 {
224 LogFlowFunc(("Option value pointer for `%s' is not set\n",
225 opt->name));
226 if (!opt->valp) {
227 LogFlowFunc(("Option value pointer for `%s' is not set\n",
228 opt->name));
229 continue;
230 }
231
232 bool fUseDefault;
233
234 switch (opt->tag)
235 {
236 case AUD_OPT_BOOL:
237 case AUD_OPT_INT:
238 {
239 int *intp = (int *)opt->valp;
240 *intp = drvAudioGetConfInt(pCfgHandle, opt->name, *intp, &fUseDefault);
241
242 break;
243 }
244
245 case AUD_OPT_FMT:
246 {
247 PDMAUDIOFMT *fmtp = (PDMAUDIOFMT *)opt->valp;
248 *fmtp = drvAudioGetConfFormat(pCfgHandle, opt->name, *fmtp, &fUseDefault);
249
250 break;
251 }
252
253 case AUD_OPT_STR:
254 {
255 const char **strp = (const char **)opt->valp;
256 *strp = drvAudioGetConfStr(pCfgHandle, opt->name, *strp, &fUseDefault);
257
258 break;
259 }
260
261 default:
262 LogFlowFunc(("Bad value tag for option `%s' - %d\n", opt->name, opt->tag));
263 fUseDefault = false;
264 break;
265 }
266
267 if (!opt->overridenp)
268 opt->overridenp = &opt->overriden;
269
270 *opt->overridenp = !fUseDefault;
271 }
272
273 return VINF_SUCCESS;
274}
275
276static bool drvAudioStreamCfgIsValid(PPDMAUDIOSTREAMCFG pCfg)
277{
278 bool fValid = ( pCfg->cChannels == 1
279 || pCfg->cChannels == 2); /* Either stereo (2) or mono (1), per stream. */
280
281 fValid |= ( pCfg->enmEndianness == PDMAUDIOENDIANNESS_LITTLE
282 || pCfg->enmEndianness == PDMAUDIOENDIANNESS_BIG);
283
284 if (fValid)
285 {
286 switch (pCfg->enmFormat)
287 {
288 case AUD_FMT_S8:
289 case AUD_FMT_U8:
290 case AUD_FMT_S16:
291 case AUD_FMT_U16:
292 case AUD_FMT_S32:
293 case AUD_FMT_U32:
294 break;
295 default:
296 fValid = false;
297 break;
298 }
299 }
300
301 /** @todo Check for defined frequencies supported. */
302 fValid |= pCfg->uHz > 0;
303
304#ifdef DEBUG
305 drvAudioStreamCfgPrint(pCfg);
306#endif
307
308 LogFlowFunc(("pCfg=%p, fValid=%RTbool\n", pCfg, fValid));
309 return fValid;
310}
311
312/**
313 * Clears a sample buffer by the given amount of audio samples.
314 *
315 * @return IPRT status code.
316 * @param pPCMProps PCM properties to use for the buffer to clear.
317 * @param pvBuf Buffer to clear.
318 * @param cbBuf Size (in bytes) of the buffer.
319 * @param cSamples Number of audio samples to clear in the buffer.
320 */
321void DrvAudioClearBuf(PPDMPCMPROPS pPCMProps, void *pvBuf, size_t cbBuf, uint32_t cSamples)
322{
323 AssertPtrReturnVoid(pPCMProps);
324 AssertPtrReturnVoid(pvBuf);
325
326 if (!cbBuf || !cSamples)
327 return;
328
329 Log2Func(("pPCMInfo=%p, pvBuf=%p, cSamples=%RU32, fSigned=%RTbool, cBits=%RU8, cShift=%RU8\n",
330 pPCMProps, pvBuf, cSamples, pPCMProps->fSigned, pPCMProps->cBits, pPCMProps->cShift));
331
332 if (pPCMProps->fSigned)
333 {
334 memset(pvBuf, 0, cSamples << pPCMProps->cShift);
335 }
336 else
337 {
338 switch (pPCMProps->cBits)
339 {
340 case 8:
341 {
342 memset(pvBuf, 0x80, cSamples << pPCMProps->cShift);
343 break;
344 }
345
346 case 16:
347 {
348 uint16_t *p = (uint16_t *)pvBuf;
349 int shift = pPCMProps->cChannels - 1;
350 short s = INT16_MAX;
351
352 if (pPCMProps->fSwapEndian)
353 s = RT_BSWAP_U16(s);
354
355 for (unsigned i = 0; i < cSamples << shift; i++)
356 p[i] = s;
357
358 break;
359 }
360
361 case 32:
362 {
363 uint32_t *p = (uint32_t *)pvBuf;
364 int shift = pPCMProps->cChannels - 1;
365 int32_t s = INT32_MAX;
366
367 if (pPCMProps->fSwapEndian)
368 s = RT_BSWAP_U32(s);
369
370 for (unsigned i = 0; i < cSamples << shift; i++)
371 p[i] = s;
372
373 break;
374 }
375
376 default:
377 {
378 AssertMsgFailed(("Invalid bits: %RU8\n", pPCMProps->cBits));
379 break;
380 }
381 }
382 }
383}
384
385static int drvAudioControlHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn, PDMAUDIOSTREAMCMD enmStreamCmd)
386{
387 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
388 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
389
390 int rc = RTCritSectEnter(&pHstStrmIn->CritSect);
391 if (RT_FAILURE(rc))
392 return rc;
393
394 switch (enmStreamCmd)
395 {
396 case PDMAUDIOSTREAMCMD_ENABLE:
397 {
398 if (!(pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED))
399 {
400 rc = pThis->pHostDrvAudio->pfnControlIn(pThis->pHostDrvAudio, pHstStrmIn, PDMAUDIOSTREAMCMD_ENABLE);
401 if (RT_SUCCESS(rc))
402 {
403 pHstStrmIn->fStatus |= PDMAUDIOSTRMSTS_FLAG_ENABLED;
404 }
405 else
406 LogFlowFunc(("Backend reported an error when opening input stream, rc=%Rrc\n", rc));
407 }
408 else
409 rc = VINF_SUCCESS;
410
411 break;
412 }
413
414 case PDMAUDIOSTREAMCMD_DISABLE:
415 {
416 if (pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
417 {
418 rc = pThis->pHostDrvAudio->pfnControlIn(pThis->pHostDrvAudio, pHstStrmIn, PDMAUDIOSTREAMCMD_DISABLE);
419 if (RT_SUCCESS(rc))
420 {
421 pHstStrmIn->fStatus = PDMAUDIOSTRMSTS_FLAG_NONE; /* Clear all. */
422 AudioMixBufClear(&pHstStrmIn->MixBuf);
423 }
424 else
425 LogFlowFunc(("Backend vetoed closing output stream, rc=%Rrc\n", rc));
426 }
427 else
428 rc = VINF_SUCCESS;
429
430 break;
431 }
432
433 case PDMAUDIOSTREAMCMD_PAUSE:
434 {
435 if (!(pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_PAUSED))
436 {
437 Assert(pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED);
438 rc = pThis->pHostDrvAudio->pfnControlIn(pThis->pHostDrvAudio, pHstStrmIn, PDMAUDIOSTREAMCMD_PAUSE);
439 if (RT_SUCCESS(rc))
440 {
441 LogFunc(("[%s] Pausing stream\n", pHstStrmIn->MixBuf.pszName));
442 pHstStrmIn->fStatus |= PDMAUDIOSTRMSTS_FLAG_PAUSED;
443 }
444 else
445 LogFlowFunc(("Backend vetoed pausing input stream, rc=%Rrc\n", rc));
446 }
447 else
448 rc = VINF_SUCCESS;
449
450 break;
451 }
452
453 case PDMAUDIOSTREAMCMD_RESUME:
454 {
455 if (pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_PAUSED)
456 {
457 Assert(pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED);
458 rc = pThis->pHostDrvAudio->pfnControlIn(pThis->pHostDrvAudio, pHstStrmIn, PDMAUDIOSTREAMCMD_RESUME);
459 if (RT_SUCCESS(rc))
460 {
461 pHstStrmIn->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PAUSED;
462 LogFunc(("[%s] Resumed stream\n", pHstStrmIn->MixBuf.pszName));
463 }
464 else
465 LogFlowFunc(("Backend vetoed resuming input stream, rc=%Rrc\n", rc));
466 }
467 else
468 rc = VINF_SUCCESS;
469
470 break;
471 }
472
473 default:
474 AssertMsgFailed(("Command %ld not implemented\n", enmStreamCmd));
475 rc = VERR_NOT_IMPLEMENTED;
476 break;
477 }
478
479 int rc2 = RTCritSectLeave(&pHstStrmIn->CritSect);
480 if (RT_SUCCESS(rc))
481 rc = rc2;
482
483 return rc;
484}
485
486static int drvAudioControlHstOut(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMOUT pHstStrmOut, PDMAUDIOSTREAMCMD enmStreamCmd)
487{
488 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
489 AssertPtrReturn(pHstStrmOut, VERR_INVALID_POINTER);
490
491 int rc = RTCritSectEnter(&pHstStrmOut->CritSect);
492 if (RT_FAILURE(rc))
493 return rc;
494
495 switch (enmStreamCmd)
496 {
497 case PDMAUDIOSTREAMCMD_ENABLE:
498 {
499 if (!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED))
500 {
501 rc = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut, PDMAUDIOSTREAMCMD_ENABLE);
502 if (RT_SUCCESS(rc))
503 {
504 Assert(!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE));
505 pHstStrmOut->fStatus |= PDMAUDIOSTRMSTS_FLAG_ENABLED;
506 LogFunc(("[%s] Enabled stream\n", pHstStrmOut->MixBuf.pszName));
507 }
508 else
509 LogFlowFunc(("[%s] Backend reported an error when enabling output stream, rc=%Rrc\n",
510 pHstStrmOut->MixBuf.pszName, rc));
511 }
512 else
513 rc = VINF_SUCCESS;
514
515 break;
516 }
517
518 case PDMAUDIOSTREAMCMD_DISABLE:
519 {
520 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
521 {
522 rc = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
523 if (RT_SUCCESS(rc))
524 {
525 pHstStrmOut->fStatus = PDMAUDIOSTRMSTS_FLAG_NONE; /* Clear all. */
526 AudioMixBufClear(&pHstStrmOut->MixBuf);
527
528 LogFunc(("[%s] Disabled stream\n", pHstStrmOut->MixBuf.pszName));
529 }
530 else
531 LogFlowFunc(("[%s] Backend vetoed disabling output stream, rc=%Rrc\n", pHstStrmOut->MixBuf.pszName, rc));
532 }
533 else
534 rc = VINF_SUCCESS;
535
536 break;
537 }
538
539 case PDMAUDIOSTREAMCMD_PAUSE:
540 {
541 if (!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PAUSED))
542 {
543 Assert(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED);
544 rc = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut, PDMAUDIOSTREAMCMD_PAUSE);
545 if (RT_SUCCESS(rc))
546 {
547 pHstStrmOut->fStatus |= PDMAUDIOSTRMSTS_FLAG_PAUSED;
548 LogFunc(("[%s] Pausing stream\n", pHstStrmOut->MixBuf.pszName));
549 }
550 else
551 LogFlowFunc(("[%s] Backend vetoed pausing output stream, rc=%Rrc\n", pHstStrmOut->MixBuf.pszName, rc));
552 }
553 else
554 rc = VINF_SUCCESS;
555
556 break;
557 }
558
559 case PDMAUDIOSTREAMCMD_RESUME:
560 {
561 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PAUSED)
562 {
563 Assert(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED);
564 rc = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut, PDMAUDIOSTREAMCMD_RESUME);
565 if (RT_SUCCESS(rc))
566 {
567 pHstStrmOut->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PAUSED;
568 LogFunc(("[%s] Resumed stream\n", pHstStrmOut->MixBuf.pszName));
569 }
570 else
571 LogFlowFunc(("[%s] Backend vetoed resuming output stream, rc=%Rrc\n", pHstStrmOut->MixBuf.pszName, rc));
572 }
573 else
574 rc = VINF_SUCCESS;
575
576 break;
577 }
578
579 default:
580 AssertMsgFailed(("Command %ld not implemented\n", enmStreamCmd));
581 rc = VERR_NOT_IMPLEMENTED;
582 break;
583 }
584
585 int rc2 = RTCritSectLeave(&pHstStrmOut->CritSect);
586 if (RT_SUCCESS(rc))
587 rc = rc2;
588
589 return rc;
590}
591
592int drvAudioDestroyHstOut(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMOUT pHstStrmOut)
593{
594 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
595 AssertPtrReturn(pHstStrmOut, VERR_INVALID_POINTER);
596
597 LogFlowFunc(("%s\n", pHstStrmOut->MixBuf.pszName));
598
599 int rc;
600 if (RTListIsEmpty(&pHstStrmOut->lstGstStrmOut))
601 {
602 rc = pThis->pHostDrvAudio->pfnFiniOut(pThis->pHostDrvAudio, pHstStrmOut);
603 if (RT_SUCCESS(rc))
604 {
605 drvAudioHstOutFreeRes(pHstStrmOut);
606
607 /* Remove from driver instance list. */
608 RTListNodeRemove(&pHstStrmOut->Node);
609
610 if (RTCritSectIsInitialized(&pHstStrmOut->CritSect))
611 {
612 int rc2 = RTCritSectDelete(&pHstStrmOut->CritSect);
613 AssertRC(rc2);
614 }
615
616 RTMemFree(pHstStrmOut);
617 pThis->cFreeOutputStreams++;
618 return VINF_SUCCESS;
619 }
620 }
621 else
622 {
623 rc = VERR_ACCESS_DENIED;
624 LogFlowFunc(("[%s] Still is being used, rc=%Rrc\n", pHstStrmOut->MixBuf.pszName, rc));
625 }
626
627 return rc;
628}
629
630int drvAudioDestroyGstOut(PDRVAUDIO pThis, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
631{
632 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
633
634 if (!pGstStrmOut)
635 return VINF_SUCCESS;
636
637 if (pGstStrmOut->State.cRefs > 1) /* Do other objects still have a reference to it? Bail out. */
638 return VERR_WRONG_ORDER;
639
640 drvAudioGstOutFreeRes(pGstStrmOut);
641
642 if (pGstStrmOut->pHstStrmOut)
643 {
644 /* Unregister from parent first. */
645 RTListNodeRemove(&pGstStrmOut->Node);
646
647 /* Try destroying the associated host output stream. This could
648 * be skipped if there are other guest output streams with this
649 * host stream. */
650 drvAudioDestroyHstOut(pThis, pGstStrmOut->pHstStrmOut);
651 }
652
653 RTMemFree(pGstStrmOut);
654
655 return VINF_SUCCESS;
656}
657
658PPDMAUDIOHSTSTRMIN drvAudioFindNextHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn)
659{
660 if (pHstStrmIn)
661 {
662 if (RTListNodeIsLast(&pThis->lstHstStrmIn, &pHstStrmIn->Node))
663 return NULL;
664
665 return RTListNodeGetNext(&pHstStrmIn->Node, PDMAUDIOHSTSTRMIN, Node);
666 }
667
668 return RTListGetFirst(&pThis->lstHstStrmIn, PDMAUDIOHSTSTRMIN, Node);
669}
670
671PPDMAUDIOHSTSTRMIN drvAudioFindNextEnabledHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn)
672{
673 while ((pHstStrmIn = drvAudioFindNextHstIn(pThis, pHstStrmIn)))
674 if (pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
675 return pHstStrmIn;
676
677 return NULL;
678}
679
680PPDMAUDIOHSTSTRMIN drvAudioFindNextEqHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn,
681 PPDMAUDIOSTREAMCFG pCfg)
682{
683 while ((pHstStrmIn = drvAudioFindNextHstIn(pThis, pHstStrmIn)))
684 if (drvAudioPCMPropsAreEqual(&pHstStrmIn->Props, pCfg))
685 return pHstStrmIn;
686
687 return NULL;
688}
689
690static int drvAudioHstInAdd(PDRVAUDIO pThis, const char *pszName, PPDMAUDIOSTREAMCFG pCfg, PDMAUDIORECSOURCE enmRecSource,
691 PPDMAUDIOHSTSTRMIN *ppHstStrmIn)
692{
693 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
694 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
695 AssertPtrReturn(ppHstStrmIn, VERR_INVALID_POINTER);
696
697 PPDMAUDIOHSTSTRMIN pHstStrmIn;
698 int rc = drvAudioAllocHstIn(pThis, pszName, pCfg, enmRecSource, &pHstStrmIn);
699 if (RT_SUCCESS(rc))
700 *ppHstStrmIn = pHstStrmIn;
701
702 LogFlowFuncLeaveRC(rc);
703 return rc;
704}
705
706int drvAudioGstOutInit(PPDMAUDIOGSTSTRMOUT pGstStrmOut, PPDMAUDIOHSTSTRMOUT pHostStrmOut,
707 const char *pszName, PPDMAUDIOSTREAMCFG pCfg)
708{
709 AssertPtrReturn(pGstStrmOut, VERR_INVALID_POINTER);
710 AssertPtrReturn(pHostStrmOut, VERR_INVALID_POINTER);
711 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
712 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
713
714 int rc = DrvAudioStreamCfgToProps(pCfg, &pGstStrmOut->Props);
715 if (RT_SUCCESS(rc))
716 {
717 char *pszTemp;
718 if (RTStrAPrintf(&pszTemp, "%s (Guest)", pszName) <= 0)
719 return VERR_NO_MEMORY;
720
721 rc = AudioMixBufInit(&pGstStrmOut->MixBuf, pszTemp, &pGstStrmOut->Props, AudioMixBufSize(&pHostStrmOut->MixBuf));
722 if (RT_SUCCESS(rc))
723 rc = AudioMixBufLinkTo(&pGstStrmOut->MixBuf, &pHostStrmOut->MixBuf);
724
725 RTStrFree(pszTemp);
726
727 if (RT_SUCCESS(rc))
728 {
729 pGstStrmOut->State.cRefs = 1;
730 pGstStrmOut->State.fActive = false;
731 pGstStrmOut->State.fEmpty = true;
732
733 pGstStrmOut->State.pszName = RTStrDup(pszName);
734 if (!pGstStrmOut->State.pszName)
735 return VERR_NO_MEMORY;
736
737 pGstStrmOut->pHstStrmOut = pHostStrmOut;
738 }
739 }
740
741 LogFlowFunc(("pszName=%s, rc=%Rrc\n", pszName, rc));
742 return rc;
743}
744
745int drvAudioAllocHstOut(PDRVAUDIO pThis, const char *pszName, PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOHSTSTRMOUT *ppHstStrmOut)
746{
747 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
748 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
749 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
750
751 if (!pThis->cFreeOutputStreams)
752 {
753 LogFlowFunc(("Maximum number of host output streams reached\n"));
754 return VERR_NO_MORE_HANDLES;
755 }
756
757 /* Validate backend configuration. */
758 if (!pThis->BackendCfg.cbStreamOut)
759 {
760 LogFlowFunc(("Backend output configuration not valid, bailing out\n"));
761 return VERR_INVALID_PARAMETER;
762 }
763
764 PPDMAUDIOHSTSTRMOUT pHstStrmOut = (PPDMAUDIOHSTSTRMOUT)RTMemAllocZ(pThis->BackendCfg.cbStreamOut);
765 if (!pHstStrmOut)
766 {
767 LogFlowFunc(("Error allocating host output stream with %zu bytes\n",
768 pThis->BackendCfg.cbStreamOut));
769 return VERR_NO_MEMORY;
770 }
771
772 int rc;
773 bool fInitialized = false;
774
775 do
776 {
777 RTListInit(&pHstStrmOut->lstGstStrmOut);
778
779 uint32_t cSamples;
780 rc = pThis->pHostDrvAudio->pfnInitOut(pThis->pHostDrvAudio, pHstStrmOut, pCfg, &cSamples);
781 if (RT_FAILURE(rc))
782 {
783 LogFlowFunc(("Initializing host backend failed with rc=%Rrc\n", rc));
784 break;
785 }
786
787 fInitialized = true;
788
789 char *pszTemp;
790 if (RTStrAPrintf(&pszTemp, "%s (Host)", pszName) <= 0)
791 {
792 rc = VERR_NO_MEMORY;
793 break;
794 }
795
796 rc = AudioMixBufInit(&pHstStrmOut->MixBuf, pszTemp, &pHstStrmOut->Props, cSamples);
797 if (RT_SUCCESS(rc))
798 rc = RTCritSectInit(&pHstStrmOut->CritSect);
799
800 if (RT_SUCCESS(rc))
801 {
802 RTListPrepend(&pThis->lstHstStrmOut, &pHstStrmOut->Node);
803 pThis->cFreeOutputStreams--;
804 }
805
806 RTStrFree(pszTemp);
807
808 } while (0);
809
810 if (RT_FAILURE(rc))
811 {
812 if (fInitialized)
813 {
814 int rc2 = pThis->pHostDrvAudio->pfnFiniOut(pThis->pHostDrvAudio, pHstStrmOut);
815 AssertRC(rc2);
816 }
817
818 drvAudioHstOutFreeRes(pHstStrmOut);
819 RTMemFree(pHstStrmOut);
820 }
821 else
822 *ppHstStrmOut = pHstStrmOut;
823
824 LogFlowFuncLeaveRC(rc);
825 return rc;
826}
827
828int drvAudioCreateStreamPairOut(PDRVAUDIO pThis, const char *pszName,
829 PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOGSTSTRMOUT *ppGstStrmOut)
830{
831 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
832 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
833 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
834
835 /*
836 * Try figuring out which audio stream configuration this backend
837 * should use. If fixed output is enabled the backend will be tied
838 * to a fixed rate (in Hz, among other parameters), regardless of
839 * what the backend could do else.
840 */
841 PPDMAUDIOSTREAMCFG pBackendCfg;
842 if (conf.fixed_out.enabled)
843 pBackendCfg = &conf.fixed_out.settings;
844 else
845 pBackendCfg = pCfg;
846
847 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
848
849 LogFlowFunc(("Using fixed audio output settings: %RTbool\n",
850 RT_BOOL(conf.fixed_out.enabled)));
851
852 PPDMAUDIOGSTSTRMOUT pGstStrmOut =
853 (PPDMAUDIOGSTSTRMOUT)RTMemAllocZ(sizeof(PDMAUDIOGSTSTRMOUT));
854 if (!pGstStrmOut)
855 {
856 LogFlowFunc(("Failed to allocate memory for guest output stream \"%s\"\n", pszName));
857 return VERR_NO_MEMORY;
858 }
859
860 /*
861 * The host stream always will get the backend audio stream configuration.
862 */
863 PPDMAUDIOHSTSTRMOUT pHstStrmOut;
864 int rc = drvAudioAddHstOut(pThis, pszName, pBackendCfg, &pHstStrmOut);
865 if (RT_FAILURE(rc))
866 {
867 LogFlowFunc(("Error adding host output stream \"%s\", rc=%Rrc\n", pszName, rc));
868
869 RTMemFree(pGstStrmOut);
870 return rc;
871 }
872
873 /*
874 * The guest stream always will get the audio stream configuration told
875 * by the device emulation (which in turn was/could be set by the guest OS).
876 */
877 rc = drvAudioGstOutInit(pGstStrmOut, pHstStrmOut, pszName, pCfg);
878 if (RT_SUCCESS(rc))
879 {
880 RTListPrepend(&pHstStrmOut->lstGstStrmOut, &pGstStrmOut->Node);
881
882 if (ppGstStrmOut)
883 *ppGstStrmOut = pGstStrmOut;
884 }
885
886 if (RT_FAILURE(rc))
887 drvAudioDestroyGstOut(pThis, pGstStrmOut);
888
889 LogFlowFuncLeaveRC(rc);
890 return rc;
891}
892
893static int drvAudioCreateStreamPairIn(PDRVAUDIO pThis, const char *pszName, PDMAUDIORECSOURCE enmRecSource,
894 PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOGSTSTRMIN *ppGstStrmIn)
895{
896 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
897 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
898
899/*
900 * Try figuring out which audio stream configuration this backend
901 * should use for the audio input data. If fixed input is enabled
902 * the backend will be tied to a fixed rate (in Hz, among other parameters),
903 * regardless of what the backend initially wanted to use.
904 */
905 PPDMAUDIOSTREAMCFG pBackendCfg;
906 if (conf.fixed_in.enabled)
907 pBackendCfg = &conf.fixed_in.settings;
908 else
909 pBackendCfg = pCfg;
910
911 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
912
913 LogFlowFunc(("Using fixed audio input settings: %RTbool\n",
914 RT_BOOL(conf.fixed_in.enabled)));
915
916 PPDMAUDIOGSTSTRMIN pGstStrmIn = (PPDMAUDIOGSTSTRMIN)RTMemAllocZ(sizeof(PDMAUDIOGSTSTRMIN));
917 if (!pGstStrmIn)
918 return VERR_NO_MEMORY;
919
920 /*
921 * The host stream always will get the backend audio stream configuration.
922 */
923 PPDMAUDIOHSTSTRMIN pHstStrmIn;
924 int rc = drvAudioHstInAdd(pThis, pszName, pBackendCfg, enmRecSource, &pHstStrmIn);
925 if (RT_FAILURE(rc))
926 {
927 LogFunc(("Failed to add host audio input stream \"%s\", rc=%Rrc\n", pszName, rc));
928
929 RTMemFree(pGstStrmIn);
930 return rc;
931 }
932
933 /*
934 * The guest stream always will get the audio stream configuration told
935 * by the device emulation (which in turn was/could be set by the guest OS).
936 */
937 rc = drvAudioGstInInit(pGstStrmIn, pHstStrmIn, pszName, pCfg);
938 if (RT_SUCCESS(rc))
939 {
940 pHstStrmIn->pGstStrmIn = pGstStrmIn;
941
942 if (ppGstStrmIn)
943 *ppGstStrmIn = pGstStrmIn;
944 }
945 else
946 drvAudioDestroyGstIn(pThis, pGstStrmIn);
947
948 LogFlowFuncLeaveRC(rc);
949 return rc;
950}
951
952/**
953 * Initializes a guest input stream.
954 *
955 * @return IPRT status code.
956 * @param pGstStrmIn Pointer to guest stream to initialize.
957 * @param pHstStrmIn Pointer to host input stream to associate this guest
958 * stream with.
959 * @param pszName Pointer to stream name to use for this stream.
960 * @param pCfg Pointer to stream configuration to use.
961 */
962int drvAudioGstInInit(PPDMAUDIOGSTSTRMIN pGstStrmIn, PPDMAUDIOHSTSTRMIN pHstStrmIn,
963 const char *pszName, PPDMAUDIOSTREAMCFG pCfg)
964{
965 AssertPtrReturn(pGstStrmIn, VERR_INVALID_POINTER);
966 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
967 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
968 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
969
970 int rc = DrvAudioStreamCfgToProps(pCfg, &pGstStrmIn->Props);
971 if (RT_SUCCESS(rc))
972 {
973 char *pszTemp;
974 if (RTStrAPrintf(&pszTemp, "%s (Guest)", pszName) <= 0)
975 return VERR_NO_MEMORY;
976
977 rc = AudioMixBufInit(&pGstStrmIn->MixBuf, pszTemp, &pGstStrmIn->Props, AudioMixBufSize(&pHstStrmIn->MixBuf));
978 if (RT_SUCCESS(rc))
979 rc = AudioMixBufLinkTo(&pHstStrmIn->MixBuf, &pGstStrmIn->MixBuf);
980
981 RTStrFree(pszTemp);
982
983 if (RT_SUCCESS(rc))
984 {
985#ifdef DEBUG
986 drvAudioStreamCfgPrint(pCfg);
987#endif
988 pGstStrmIn->State.cRefs = 1;
989 pGstStrmIn->State.fActive = false;
990 pGstStrmIn->State.fEmpty = true;
991
992 pGstStrmIn->State.pszName = RTStrDup(pszName);
993 if (!pGstStrmIn->State.pszName)
994 return VERR_NO_MEMORY;
995
996 pGstStrmIn->pHstStrmIn = pHstStrmIn;
997 }
998 }
999
1000 LogFlowFunc(("pszName=%s, rc=%Rrc\n", pszName, rc));
1001 return rc;
1002}
1003
1004static int drvAudioAllocHstIn(PDRVAUDIO pThis, const char *pszName, PPDMAUDIOSTREAMCFG pCfg,
1005 PDMAUDIORECSOURCE enmRecSource, PPDMAUDIOHSTSTRMIN *ppHstStrmIn)
1006{
1007 if (!pThis->cFreeInputStreams)
1008 {
1009 LogFlowFunc(("No more input streams free to use, bailing out\n"));
1010 return VERR_NO_MORE_HANDLES;
1011 }
1012
1013 /* Validate backend configuration. */
1014 if (!pThis->BackendCfg.cbStreamIn)
1015 {
1016 LogFlowFunc(("Backend input configuration not valid, bailing out\n"));
1017 return VERR_INVALID_PARAMETER;
1018 }
1019
1020 PPDMAUDIOHSTSTRMIN pHstStrmIn =
1021 (PPDMAUDIOHSTSTRMIN)RTMemAllocZ(pThis->BackendCfg.cbStreamIn);
1022 if (!pHstStrmIn)
1023 {
1024 LogFlowFunc(("Error allocating host innput stream with %RU32 bytes\n",
1025 pThis->BackendCfg.cbStreamOut));
1026 return VERR_NO_MEMORY;
1027 }
1028
1029 int rc;
1030 bool fInitialized = false;
1031
1032 do
1033 {
1034 uint32_t cSamples;
1035 rc = pThis->pHostDrvAudio->pfnInitIn(pThis->pHostDrvAudio, pHstStrmIn,
1036 pCfg, enmRecSource, &cSamples);
1037 if (RT_FAILURE(rc))
1038 {
1039 LogFlowFunc(("Initializing host backend failed with rc=%Rrc\n", rc));
1040 break;
1041 }
1042
1043 fInitialized = true;
1044
1045 char *pszTemp;
1046 if (RTStrAPrintf(&pszTemp, "%s (Host)", pszName) <= 0)
1047 {
1048 rc = VERR_NO_MEMORY;
1049 break;
1050 }
1051
1052 rc = AudioMixBufInit(&pHstStrmIn->MixBuf, pszTemp, &pHstStrmIn->Props, cSamples);
1053 if (RT_SUCCESS(rc))
1054 rc = RTCritSectInit(&pHstStrmIn->CritSect);
1055
1056 if (RT_SUCCESS(rc))
1057 {
1058 RTListPrepend(&pThis->lstHstStrmIn, &pHstStrmIn->Node);
1059 pThis->cFreeInputStreams--;
1060 }
1061
1062 RTStrFree(pszTemp);
1063
1064 } while (0);
1065
1066 if (RT_FAILURE(rc))
1067 {
1068 if (fInitialized)
1069 {
1070 int rc2 = pThis->pHostDrvAudio->pfnFiniIn(pThis->pHostDrvAudio,
1071 pHstStrmIn);
1072 AssertRC(rc2);
1073 }
1074
1075 drvAudioHstInFreeRes(pHstStrmIn);
1076 RTMemFree(pHstStrmIn);
1077 }
1078 else
1079 *ppHstStrmIn = pHstStrmIn;
1080
1081 LogFlowFuncLeaveRC(rc);
1082 return rc;
1083}
1084
1085/**
1086 * Writes VM audio output data from the guest stream into the host stream.
1087 * The attached host driver backend then will play out the audio in a
1088 * later step then.
1089 *
1090 * @return IPRT status code.
1091 * @return int
1092 * @param pThis
1093 * @param pGstStrmOut
1094 * @param pvBuf
1095 * @param cbBuf
1096 * @param pcbWritten
1097 */
1098static DECLCALLBACK(int) drvAudioWrite(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut,
1099 const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
1100{
1101 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1102 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1103
1104 AssertPtrReturn(pGstStrmOut, VERR_INVALID_POINTER);
1105 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1106 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1107 /* pcbWritten is optional. */
1108
1109 int rc = RTCritSectEnter(&pThis->CritSect);
1110 if (RT_FAILURE(rc))
1111 return rc;
1112
1113 if (!pThis->pHostDrvAudio->pfnIsEnabled(pThis->pHostDrvAudio, PDMAUDIODIR_OUT))
1114 {
1115 rc = RTCritSectLeave(&pThis->CritSect);
1116 AssertRC(rc);
1117
1118 return VERR_NOT_AVAILABLE;
1119 }
1120
1121 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
1122 AssertPtrReturn(pHstStrmOut, VERR_INVALID_POINTER);
1123
1124 AssertMsg(pGstStrmOut->pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED,
1125 ("Writing to disabled host output stream \"%s\" not possible\n",
1126 pHstStrmOut->MixBuf.pszName));
1127
1128 if (!AudioMixBufFreeBytes(&pGstStrmOut->MixBuf))
1129 {
1130 if (pcbWritten)
1131 *pcbWritten = 0;
1132
1133 return RTCritSectLeave(&pThis->CritSect);
1134 }
1135
1136 /*
1137 * First, write data from the device emulation into our
1138 * guest mixing buffer.
1139 */
1140 uint32_t cWritten;
1141 rc = AudioMixBufWriteAt(&pGstStrmOut->MixBuf, 0 /* Offset in samples */, pvBuf, cbBuf, &cWritten);
1142
1143 /*
1144 * Second, mix the guest mixing buffer with the host mixing
1145 * buffer so that the host backend can play the data lateron.
1146 */
1147 uint32_t cMixed;
1148 if ( RT_SUCCESS(rc)
1149 && cWritten)
1150 {
1151 rc = AudioMixBufMixToParent(&pGstStrmOut->MixBuf, cWritten, &cMixed);
1152 }
1153 else
1154 cMixed = 0;
1155
1156 if (RT_SUCCESS(rc))
1157 {
1158 /*
1159 * Return the number of samples which actually have been mixed
1160 * down to the parent, regardless how much samples were written
1161 * into the children buffer.
1162 */
1163 if (pcbWritten)
1164 *pcbWritten = AUDIOMIXBUF_S2B(&pGstStrmOut->MixBuf, cMixed);
1165 }
1166
1167 LogFlowFunc(("%s -> %s: Written pvBuf=%p, cbBuf=%RU32, cWritten=%RU32 (%RU32 bytes), cMixed=%RU32, rc=%Rrc\n",
1168 pGstStrmOut->MixBuf.pszName, pHstStrmOut->MixBuf.pszName, pvBuf, cbBuf, cWritten,
1169 AUDIOMIXBUF_S2B(&pGstStrmOut->MixBuf, cWritten), cMixed, rc));
1170
1171 int rc2 = RTCritSectLeave(&pThis->CritSect);
1172 if (RT_SUCCESS(rc))
1173 rc = rc2;
1174
1175 return rc;
1176}
1177
1178PPDMAUDIOHSTSTRMOUT drvAudioFindAnyHstOut(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMOUT pHstStrmOut)
1179{
1180 if (pHstStrmOut)
1181 {
1182 if (RTListNodeIsLast(&pThis->lstHstStrmOut, &pHstStrmOut->Node))
1183 return NULL;
1184
1185 return RTListNodeGetNext(&pHstStrmOut->Node, PDMAUDIOHSTSTRMOUT, Node);
1186 }
1187
1188 return RTListGetFirst(&pThis->lstHstStrmOut, PDMAUDIOHSTSTRMOUT, Node);
1189}
1190
1191PPDMAUDIOHSTSTRMOUT drvAudioHstFindAnyEnabledOut(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMOUT pHostStrmOut)
1192{
1193 while ((pHostStrmOut = drvAudioFindAnyHstOut(pThis, pHostStrmOut)))
1194 {
1195 if (pHostStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
1196 return pHostStrmOut;
1197 }
1198
1199 return NULL;
1200}
1201
1202PPDMAUDIOHSTSTRMOUT drvAudioFindSpecificOut(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMOUT pHstStrmOut,
1203 PPDMAUDIOSTREAMCFG pCfg)
1204{
1205 while ((pHstStrmOut = drvAudioFindAnyHstOut(pThis, pHstStrmOut)))
1206 {
1207 if (drvAudioPCMPropsAreEqual(&pHstStrmOut->Props, pCfg))
1208 return pHstStrmOut;
1209 }
1210
1211 return NULL;
1212}
1213
1214int drvAudioDestroyHstIn(PDRVAUDIO pThis, PPDMAUDIOHSTSTRMIN pHstStrmIn)
1215{
1216 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1217 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
1218
1219 LogFlowFunc(("%s\n", pHstStrmIn->MixBuf.pszName));
1220
1221 int rc;
1222 if (!pHstStrmIn->pGstStrmIn) /* No parent anymore? */
1223 {
1224 rc = pThis->pHostDrvAudio->pfnFiniIn(pThis->pHostDrvAudio, pHstStrmIn);
1225 if (RT_SUCCESS(rc))
1226 {
1227 drvAudioHstInFreeRes(pHstStrmIn);
1228
1229 if (RTCritSectIsInitialized(&pHstStrmIn->CritSect))
1230 {
1231 int rc2 = RTCritSectDelete(&pHstStrmIn->CritSect);
1232 AssertRC(rc2);
1233 }
1234
1235 /* Remove from driver instance list. */
1236 RTListNodeRemove(&pHstStrmIn->Node);
1237
1238 RTMemFree(pHstStrmIn);
1239 pThis->cFreeInputStreams++;
1240 }
1241 }
1242 else
1243 {
1244 rc = VERR_ACCESS_DENIED;
1245 LogFlowFunc(("[%s] Still is being used, rc=%Rrc\n", pHstStrmIn->MixBuf.pszName, rc));
1246 }
1247
1248 return rc;
1249}
1250
1251static int drvAudioDestroyGstIn(PDRVAUDIO pThis, PPDMAUDIOGSTSTRMIN pGstStrmIn)
1252{
1253 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1254
1255 LogFlowFunc(("%s\n", pGstStrmIn->MixBuf.pszName));
1256
1257 if (!pGstStrmIn)
1258 return VINF_SUCCESS;
1259
1260 if (pGstStrmIn->State.cRefs > 1) /* Do other objects still have a reference to it? Bail out. */
1261 return VERR_WRONG_ORDER;
1262
1263 drvAudioGstInFreeRes(pGstStrmIn);
1264
1265 if (pGstStrmIn->pHstStrmIn)
1266 {
1267 /* Unlink child. */
1268 pGstStrmIn->pHstStrmIn->pGstStrmIn = NULL;
1269
1270 /* Try destroying the associated host input stream. This could
1271 * be skipped if there are other guest input streams with this
1272 * host stream. */
1273 drvAudioDestroyHstIn(pThis, pGstStrmIn->pHstStrmIn);
1274 }
1275
1276 RTMemFree(pGstStrmIn);
1277
1278 return VINF_SUCCESS;
1279}
1280
1281static DECLCALLBACK(int) drvAudioQueryStatus(PPDMIAUDIOCONNECTOR pInterface,
1282 uint32_t *pcbAvailIn, uint32_t *pcbFreeOut,
1283 uint32_t *pcSamplesLive)
1284{
1285 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1286 /* pcbAvailIn is optional. */
1287 /* pcbFreeOut is optional. */
1288 /* pcSamplesLive is optional. */
1289
1290 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1291
1292 int rc = RTCritSectEnter(&pThis->CritSect);
1293 if (RT_FAILURE(rc))
1294 return rc;
1295
1296 /*
1297 * Playback.
1298 */
1299 uint32_t cSamplesLive = 0;
1300 uint32_t cbFreeOut = UINT32_MAX;
1301
1302 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
1303 while ((pHstStrmOut = drvAudioHstFindAnyEnabledOut(pThis, pHstStrmOut)))
1304 {
1305 cSamplesLive = AudioMixBufAvail(&pHstStrmOut->MixBuf);
1306
1307 /* Has this stream marked as disabled but there still were guest streams relying
1308 * on it? Check if this stream now can be closed and do so, if possible. */
1309 if ( (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE)
1310 && !cSamplesLive)
1311 {
1312 /* Stop playing the current (pending) stream. */
1313 int rc2 = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1314 if (RT_SUCCESS(rc2))
1315 {
1316 pHstStrmOut->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1317
1318 LogFunc(("[%s] Disabling stream\n", pHstStrmOut->MixBuf.pszName));
1319 }
1320 else
1321 LogFunc(("[%s] Backend vetoed against closing output stream, rc=%Rrc\n", pHstStrmOut->MixBuf.pszName, rc2));
1322
1323 continue;
1324 }
1325
1326 LogFlowFunc(("[%s] cSamplesLive=%RU32\n", pHstStrmOut->MixBuf.pszName, cSamplesLive));
1327
1328 /*
1329 * No live samples to play at the moment?
1330 *
1331 * Tell the device emulation for each connected guest stream how many
1332 * bytes are free so that the device emulation can continue writing data to
1333 * these streams.
1334 */
1335 PPDMAUDIOGSTSTRMOUT pGstStrmOut;
1336 uint32_t cbFree2 = UINT32_MAX;
1337 RTListForEach(&pHstStrmOut->lstGstStrmOut, pGstStrmOut, PDMAUDIOGSTSTRMOUT, Node)
1338 {
1339 if (pGstStrmOut->State.fActive)
1340 {
1341 /* Tell the sound device emulation how many samples are free
1342 * so that it can start writing PCM data to us. */
1343 cbFree2 = RT_MIN(cbFree2, AUDIOMIXBUF_S2B_RATIO(&pGstStrmOut->MixBuf,
1344 AudioMixBufFree(&pGstStrmOut->MixBuf)));
1345#ifdef DEBUG_andy
1346 LogFlowFunc(("\t[%s] cbFreeOut=%RU32\n", pGstStrmOut->MixBuf.pszName, cbFree2));
1347#endif
1348 }
1349 }
1350
1351 cbFreeOut = RT_MIN(cbFreeOut, cbFree2);
1352 }
1353
1354 /*
1355 * Recording.
1356 */
1357 uint32_t cbAvailIn = 0;
1358
1359 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
1360 while ((pHstStrmIn = drvAudioFindNextEnabledHstIn(pThis, pHstStrmIn)))
1361 {
1362 /* Call the host backend to capture the audio input data. */
1363 uint32_t cSamplesCaptured;
1364 int rc2 = pThis->pHostDrvAudio->pfnCaptureIn(pThis->pHostDrvAudio, pHstStrmIn,
1365 &cSamplesCaptured);
1366 if (RT_FAILURE(rc2))
1367 continue;
1368
1369 PPDMAUDIOGSTSTRMIN pGstStrmIn = pHstStrmIn->pGstStrmIn;
1370 AssertPtrBreak(pGstStrmIn);
1371
1372 if (pGstStrmIn->State.fActive)
1373 {
1374 cbAvailIn = RT_MAX(cbAvailIn, AUDIOMIXBUF_S2B(&pHstStrmIn->MixBuf,
1375 AudioMixBufMixed(&pHstStrmIn->MixBuf)));
1376#ifdef DEBUG_andy
1377 LogFlowFunc(("\t[%s] cbAvailIn=%RU32\n", pHstStrmIn->MixBuf.pszName, cbAvailIn));
1378#endif
1379 }
1380 }
1381
1382 if (RT_SUCCESS(rc))
1383 {
1384 if (cbFreeOut == UINT32_MAX)
1385 cbFreeOut = 0;
1386
1387 if (pcbAvailIn)
1388 *pcbAvailIn = cbAvailIn;
1389
1390 if (pcbFreeOut)
1391 *pcbFreeOut = cbFreeOut;
1392
1393 if (pcSamplesLive)
1394 *pcSamplesLive = cSamplesLive;
1395 }
1396
1397 int rc2 = RTCritSectLeave(&pThis->CritSect);
1398 if (RT_SUCCESS(rc))
1399 rc = rc2;
1400
1401 return rc;
1402}
1403
1404static DECLCALLBACK(int) drvAudioPlayOut(PPDMIAUDIOCONNECTOR pInterface, uint32_t *pcSamplesPlayed)
1405{
1406 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1407 /* pcSamplesPlayed is optional. */
1408
1409 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1410
1411 int rc = RTCritSectEnter(&pThis->CritSect);
1412 if (RT_FAILURE(rc))
1413 return rc;
1414
1415 /*
1416 * Process all enabled host output streams.
1417 */
1418 uint32_t cSamplesPlayedMax = 0;
1419 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
1420 while ((pHstStrmOut = drvAudioHstFindAnyEnabledOut(pThis, pHstStrmOut)))
1421 {
1422#if 0
1423 uint32_t cStreamsLive;
1424 uint32_t cSamplesLive = drvAudioHstOutSamplesLive(pHstStrmOut, &cStreamsLive);
1425 if (!cStreamsLive)
1426 cSamplesLive = 0;
1427
1428 /* Has this stream marked as disabled but there still were guest streams relying
1429 * on it? Check if this stream now can be closed and do so, if possible. */
1430 if ( pHstStrmOut->fPendingDisable
1431 && !cStreamsLive)
1432 {
1433 /* Stop playing the current (pending) stream. */
1434 int rc2 = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut,
1435 PDMAUDIOSTREAMCMD_DISABLE);
1436 if (RT_SUCCESS(rc2))
1437 {
1438 pHstStrmOut->fEnabled = false;
1439 pHstStrmOut->fPendingDisable = false;
1440
1441 LogFunc(("\t%p: Disabling stream\n", pHstStrmOut));
1442 }
1443 else
1444 LogFunc(("\t%p: Backend vetoed against closing output stream, rc=%Rrc\n",
1445 pHstStrmOut, rc2));
1446
1447 continue;
1448 }
1449#endif
1450
1451 uint32_t cSamplesPlayed = 0;
1452 int rc2 = pThis->pHostDrvAudio->pfnPlayOut(pThis->pHostDrvAudio, pHstStrmOut,
1453 &cSamplesPlayed);
1454 if (RT_SUCCESS(rc2))
1455 cSamplesPlayedMax = RT_MAX(cSamplesPlayed, cSamplesPlayedMax);
1456
1457 LogFlowFunc(("\t[%s] cSamplesPlayed=%RU32, cSamplesPlayedMax=%RU32, rc=%Rrc\n",
1458 pHstStrmOut->MixBuf.pszName, cSamplesPlayed, cSamplesPlayedMax, rc2));
1459
1460 bool fNeedsCleanup = false;
1461
1462 PPDMAUDIOGSTSTRMOUT pGstStrmOut;
1463 RTListForEach(&pHstStrmOut->lstGstStrmOut, pGstStrmOut, PDMAUDIOGSTSTRMOUT, Node)
1464 {
1465 if ( !pGstStrmOut->State.fActive
1466 && pGstStrmOut->State.fEmpty)
1467 continue;
1468
1469 if (AudioMixBufIsEmpty(&pGstStrmOut->MixBuf))
1470 {
1471 pGstStrmOut->State.fEmpty = true;
1472 fNeedsCleanup |= !pGstStrmOut->State.fActive;
1473 }
1474 }
1475
1476 if (fNeedsCleanup)
1477 {
1478 RTListForEach(&pHstStrmOut->lstGstStrmOut, pGstStrmOut, PDMAUDIOGSTSTRMOUT, Node)
1479 {
1480 if (!pGstStrmOut->State.fActive)
1481 drvAudioDestroyGstOut(pThis, pGstStrmOut);
1482 }
1483 }
1484 }
1485
1486 if (RT_SUCCESS(rc))
1487 {
1488 if (pcSamplesPlayed)
1489 *pcSamplesPlayed = cSamplesPlayedMax;
1490 }
1491
1492 int rc2 = RTCritSectLeave(&pThis->CritSect);
1493 if (RT_SUCCESS(rc))
1494 rc = rc2;
1495
1496 return rc;
1497}
1498
1499#ifdef VBOX_WITH_AUDIO_CALLBACKS
1500static PPDMAUDIOCALLBACK drvAudioCallbackDuplicate(PPDMAUDIOCALLBACK pCB)
1501{
1502 PPDMAUDIOCALLBACK pCBCopy = (PPDMAUDIOCALLBACK)RTMemDup((void *)pCB, sizeof(PDMAUDIOCALLBACK));
1503 if (!pCBCopy)
1504 return NULL;
1505
1506 if (pCB->pvCtx)
1507 {
1508 pCBCopy->pvCtx = RTMemDup(pCB->pvCtx, pCB->cbCtx);
1509 if (!pCBCopy->pvCtx)
1510 {
1511 RTMemFree(pCBCopy);
1512 return NULL;
1513 }
1514
1515 pCBCopy->cbCtx = pCB->cbCtx;
1516 }
1517
1518 return pCBCopy;
1519}
1520
1521static void drvAudioCallbackDestroy(PPDMAUDIOCALLBACK pCB)
1522{
1523 if (!pCB)
1524 return;
1525
1526 RTListNodeRemove(&pCB->Node);
1527 if (pCB->pvCtx)
1528 {
1529 Assert(pCB->cbCtx);
1530 RTMemFree(pCB->pvCtx);
1531 }
1532 RTMemFree(pCB);
1533}
1534
1535static DECLCALLBACK(int) drvAudioRegisterCallbacks(PPDMIAUDIOCONNECTOR pInterface,
1536 PPDMAUDIOCALLBACK paCallbacks, size_t cCallbacks)
1537{
1538 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1539 AssertPtrReturn(paCallbacks, VERR_INVALID_POINTER);
1540 AssertReturn(cCallbacks, VERR_INVALID_PARAMETER);
1541
1542 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1543
1544 int rc = RTCritSectEnter(&pThis->CritSect);
1545 if (RT_FAILURE(rc))
1546 return rc;
1547
1548 for (size_t i = 0; i < cCallbacks; i++)
1549 {
1550 PPDMAUDIOCALLBACK pCB = drvAudioCallbackDuplicate(&paCallbacks[i]);
1551 if (!pCB)
1552 {
1553 rc = VERR_NO_MEMORY;
1554 break;
1555 }
1556
1557 switch (pCB->enmType)
1558 {
1559 case PDMAUDIOCALLBACKTYPE_INPUT:
1560 RTListAppend(&pThis->lstCBIn, &pCB->Node);
1561 break;
1562
1563 case PDMAUDIOCALLBACKTYPE_OUTPUT:
1564 RTListAppend(&pThis->lstCBOut, &pCB->Node);
1565 break;
1566
1567 default:
1568 AssertMsgFailed(("Not supported\n"));
1569 break;
1570 }
1571 }
1572
1573 /** @todo Undo allocations on error. */
1574
1575 int rc2 = RTCritSectLeave(&pThis->CritSect);
1576 if (RT_SUCCESS(rc))
1577 rc = rc2;
1578
1579 return rc;
1580}
1581
1582static DECLCALLBACK(int) drvAudioCallback(PPDMIAUDIOCONNECTOR pInterface, PDMAUDIOCALLBACKTYPE enmType,
1583 void *pvUser, size_t cbUser)
1584{
1585 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1586 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
1587 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
1588
1589 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1590 PRTLISTANCHOR pListAnchor = NULL;
1591
1592 switch (enmType)
1593 {
1594 case PDMAUDIOCALLBACKTYPE_INPUT:
1595 pListAnchor = &pThis->lstCBIn;
1596 break;
1597
1598 case PDMAUDIOCALLBACKTYPE_OUTPUT:
1599 pListAnchor = &pThis->lstCBOut;
1600 break;
1601
1602 default:
1603 AssertMsgFailed(("Not supported\n"));
1604 break;
1605 }
1606
1607 if (pListAnchor)
1608 {
1609 PPDMAUDIOCALLBACK pCB;
1610 RTListForEach(pListAnchor, pCB, PDMAUDIOCALLBACK, Node)
1611 {
1612 Assert(pCB->enmType == enmType);
1613 pCB->pfnCallback(enmType, pCB->pvCtx, pCB->cbCtx, pvUser, cbUser);
1614 }
1615 }
1616
1617 return VINF_SUCCESS;
1618}
1619#endif
1620
1621static int drvAudioHostInit(PCFGMNODE pCfgHandle, PDRVAUDIO pThis)
1622{
1623 /* pCfgHandle is optional. */
1624 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1625
1626 NOREF(pCfgHandle);
1627
1628 LogFlowFuncEnter();
1629
1630 int rc = pThis->pHostDrvAudio->pfnInit(pThis->pHostDrvAudio);
1631 if (RT_FAILURE(rc))
1632 {
1633 LogFlowFunc(("Initialization of lower driver failed with rc=%Rrc\n", rc));
1634 return rc;
1635 }
1636
1637 uint32_t cMaxHstStrmsOut = pThis->BackendCfg.cMaxHstStrmsOut;
1638 uint32_t cbHstStrmsOut = pThis->BackendCfg.cbStreamOut;
1639
1640 if (cbHstStrmsOut)
1641 {
1642 pThis->cFreeOutputStreams = 1; /** @todo Make this configurable. */
1643 if (pThis->cFreeOutputStreams > cMaxHstStrmsOut)
1644 {
1645 LogRel(("Audio: Warning: %RU32 output streams were requested, host driver only supports %RU32\n",
1646 pThis->cFreeOutputStreams, cMaxHstStrmsOut));
1647 pThis->cFreeOutputStreams = cMaxHstStrmsOut;
1648 }
1649 }
1650 else
1651 pThis->cFreeOutputStreams = 0;
1652
1653 uint32_t cMaxHstStrmsIn = pThis->BackendCfg.cMaxHstStrmsIn;
1654 uint32_t cbHstStrmIn = pThis->BackendCfg.cbStreamIn;
1655
1656 if (cbHstStrmIn)
1657 {
1658 /*
1659 * Note:
1660 * - Our AC'97 emulation has two inputs, line (P.IN) and microphone (P.MIC).
1661 ** @todo Document HDA.
1662 */
1663 pThis->cFreeInputStreams = 2; /** @todo Make this configurable. */
1664 if (pThis->cFreeInputStreams > cMaxHstStrmsIn)
1665 {
1666 LogRel(("Audio: Warning: %RU32 input streams were requested, host driver only supports %RU32\n",
1667 pThis->cFreeInputStreams, cMaxHstStrmsIn));
1668 pThis->cFreeInputStreams = cMaxHstStrmsIn;
1669 }
1670 }
1671 else
1672 pThis->cFreeInputStreams = 0;
1673
1674 LogFlowFunc(("cMaxHstStrmsOut=%RU32 (cb=%RU32), cMaxHstStrmsIn=%RU32 (cb=%RU32)\n",
1675 cMaxHstStrmsOut, cbHstStrmsOut, cMaxHstStrmsIn, cbHstStrmIn));
1676
1677 LogFlowFunc(("cFreeInputStreams=%RU8, cFreeOutputStreams=%RU8\n",
1678 pThis->cFreeInputStreams, pThis->cFreeOutputStreams));
1679
1680 LogFlowFuncLeave();
1681 return VINF_SUCCESS;
1682}
1683
1684static void drvAudioStateHandler(PPDMDRVINS pDrvIns, PDMAUDIOSTREAMCMD enmCmd)
1685{
1686 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1687 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1688
1689 LogFlowFunc(("enmCmd=%ld\n", enmCmd));
1690
1691 if (!pThis->pHostDrvAudio)
1692 return;
1693
1694 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
1695 while ((pHstStrmOut = drvAudioHstFindAnyEnabledOut(pThis, pHstStrmOut)))
1696 drvAudioControlHstOut(pThis, pHstStrmOut, enmCmd);
1697
1698 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
1699 while ((pHstStrmIn = drvAudioFindNextEnabledHstIn(pThis, pHstStrmIn)))
1700 drvAudioControlHstIn(pThis, pHstStrmIn, enmCmd);
1701}
1702
1703static struct audio_option audio_options[] =
1704{
1705 /* DAC */
1706 {"DACFixedSettings", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1707 "Use fixed settings for host DAC", NULL, 0},
1708
1709 {"DACFixedFreq", AUD_OPT_INT, &conf.fixed_out.settings.uHz,
1710 "Frequency for fixed host DAC", NULL, 0},
1711
1712 {"DACFixedFmt", AUD_OPT_FMT, &conf.fixed_out.settings.enmFormat,
1713 "Format for fixed host DAC", NULL, 0},
1714
1715 {"DACFixedChannels", AUD_OPT_INT, &conf.fixed_out.settings.cChannels,
1716 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1717
1718 {"DACVoices", AUD_OPT_INT, &conf.fixed_out.cStreams, /** @todo Rename! */
1719 "Number of streams for DAC", NULL, 0},
1720
1721 /* ADC */
1722 {"ADCFixedSettings", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1723 "Use fixed settings for host ADC", NULL, 0},
1724
1725 {"ADCFixedFreq", AUD_OPT_INT, &conf.fixed_in.settings.uHz,
1726 "Frequency for fixed host ADC", NULL, 0},
1727
1728 {"ADCFixedFmt", AUD_OPT_FMT, &conf.fixed_in.settings.enmFormat,
1729 "Format for fixed host ADC", NULL, 0},
1730
1731 {"ADCFixedChannels", AUD_OPT_INT, &conf.fixed_in.settings.cChannels,
1732 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1733
1734 {"ADCVoices", AUD_OPT_INT, &conf.fixed_in.cStreams, /** @todo Rename! */
1735 "Number of streams for ADC", NULL, 0},
1736
1737 /* Misc */
1738 {"TimerFreq", AUD_OPT_INT, &conf.period.hz,
1739 "Timer frequency in Hz (0 - use lowest possible)", NULL, 0},
1740
1741 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1742 "(undocumented)", NULL, 0}, /** @todo What is this? */
1743
1744 NULL
1745};
1746
1747static DECLCALLBACK(int) drvAudioInit(PCFGMNODE pCfgHandle, PPDMDRVINS pDrvIns)
1748{
1749 AssertPtrReturn(pCfgHandle, VERR_INVALID_POINTER);
1750 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1751
1752 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1753 LogFlowFunc(("pDrvAudio=%p, pDrvIns=%p\n", pThis, pDrvIns));
1754
1755 RTListInit(&pThis->lstHstStrmIn);
1756 RTListInit(&pThis->lstHstStrmOut);
1757#ifdef VBOX_WITH_AUDIO_CALLBACKS
1758 RTListInit(&pThis->lstCBIn);
1759 RTListInit(&pThis->lstCBOut);
1760#endif
1761
1762 int rc = RTCritSectInit(&pThis->CritSect);
1763
1764 /* Get the configuration data from the selected backend (if available). */
1765 AssertPtr(pThis->pHostDrvAudio);
1766 if ( RT_SUCCESS(rc)
1767 && RT_LIKELY(pThis->pHostDrvAudio->pfnGetConf))
1768 rc = pThis->pHostDrvAudio->pfnGetConf(pThis->pHostDrvAudio, &pThis->BackendCfg);
1769
1770 if (RT_SUCCESS(rc))
1771 {
1772 rc = drvAudioProcessOptions(pCfgHandle, "AUDIO", audio_options);
1773 /** @todo Check for invalid options? */
1774
1775 pThis->cFreeOutputStreams = conf.fixed_out.cStreams;
1776 pThis->cFreeInputStreams = conf.fixed_in.cStreams;
1777
1778 if (!pThis->cFreeOutputStreams)
1779 pThis->cFreeOutputStreams = 1;
1780
1781 if (!pThis->cFreeInputStreams)
1782 pThis->cFreeInputStreams = 1;
1783 }
1784
1785 /*
1786 * If everything went well, initialize the lower driver.
1787 */
1788 if (RT_SUCCESS(rc))
1789 rc = drvAudioHostInit(pCfgHandle, pThis);
1790
1791 LogFlowFuncLeaveRC(rc);
1792 return rc;
1793}
1794
1795static DECLCALLBACK(int) drvAudioRead(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn,
1796 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1797{
1798 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1799 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1800
1801 AssertPtrReturn(pGstStrmIn, VERR_INVALID_POINTER);
1802 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1803 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1804 /* pcbWritten is optional. */
1805
1806 int rc = RTCritSectEnter(&pThis->CritSect);
1807 if (RT_FAILURE(rc))
1808 return rc;
1809
1810 if (!pThis->pHostDrvAudio->pfnIsEnabled(pThis->pHostDrvAudio, PDMAUDIODIR_IN))
1811 {
1812 if (pcbRead)
1813 *pcbRead = 0;
1814
1815 return RTCritSectLeave(&pThis->CritSect);
1816 }
1817
1818 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1819 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
1820
1821 AssertMsg(pGstStrmIn->pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED,
1822 ("Reading from disabled host input stream \"%s\" not possible\n", pGstStrmIn->MixBuf.pszName));
1823
1824 /*
1825 * Read from the parent buffer (that is, the guest buffer) which
1826 * should have the audio data in the format the guest needs.
1827 */
1828 uint32_t cRead;
1829 rc = AudioMixBufReadCirc(&pGstStrmIn->MixBuf, pvBuf, cbBuf, &cRead);
1830 if (RT_SUCCESS(rc))
1831 {
1832 AudioMixBufFinish(&pGstStrmIn->MixBuf, cRead);
1833
1834 if (pcbRead)
1835 *pcbRead = AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead);
1836 }
1837
1838 LogFlowFunc(("cRead=%RU32 (%RU32 bytes), rc=%Rrc\n",
1839 cRead, AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead), rc));
1840
1841 int rc2 = RTCritSectLeave(&pThis->CritSect);
1842 if (RT_SUCCESS(rc))
1843 rc = rc2;
1844
1845 return rc;
1846}
1847
1848static DECLCALLBACK(int) drvAudioEnableOut(PPDMIAUDIOCONNECTOR pInterface,
1849 PPDMAUDIOGSTSTRMOUT pGstStrmOut, bool fEnable)
1850{
1851 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1852 /* pGstStrmOut is optional. */
1853
1854 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1855
1856 int rc = VINF_SUCCESS;
1857
1858 if (pGstStrmOut)
1859 {
1860 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
1861 AssertPtr(pHstStrmOut);
1862
1863 if (fEnable)
1864 {
1865 /* Is a pending disable outstanding? Then disable first. */
1866 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE)
1867 {
1868 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1869 if (RT_SUCCESS(rc))
1870 pHstStrmOut->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1871 }
1872
1873 if (RT_SUCCESS(rc))
1874 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_ENABLE);
1875 }
1876 else /* Disable */
1877 {
1878 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
1879 {
1880 uint32_t cGstStrmsActive = 0;
1881
1882 /*
1883 * Check if there are any active guest streams assigned
1884 * to this host stream which still are being marked as active.
1885 *
1886 * In that case we have to defer closing the host stream and
1887 * wait until all guest streams have been finished.
1888 */
1889 PPDMAUDIOGSTSTRMOUT pIter;
1890 RTListForEach(&pHstStrmOut->lstGstStrmOut, pIter, PDMAUDIOGSTSTRMOUT, Node)
1891 {
1892 if (pIter->State.fActive)
1893 {
1894 cGstStrmsActive++;
1895 break; /* At least one assigned & active guest stream is enough. */
1896 }
1897 }
1898
1899 /* Do we need to defer closing the host stream? */
1900 if (cGstStrmsActive >= 1)
1901 pHstStrmOut->fStatus |= PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1902
1903 /* Can we close the host stream now instead of deferring it? */
1904 if (!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE))
1905 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1906 }
1907 }
1908
1909 if (RT_SUCCESS(rc))
1910 pGstStrmOut->State.fActive = fEnable;
1911
1912 LogFlowFunc(("%s: fEnable=%RTbool, fStatus=0x%x, rc=%Rrc\n",
1913 pGstStrmOut->MixBuf.pszName, fEnable, pHstStrmOut->fStatus, rc));
1914 }
1915
1916 return rc;
1917}
1918
1919static DECLCALLBACK(int) drvAudioEnableIn(PPDMIAUDIOCONNECTOR pInterface,
1920 PPDMAUDIOGSTSTRMIN pGstStrmIn, bool fEnable)
1921{
1922 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1923 /* pGstStrmIn is optional. */
1924
1925 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1926
1927 int rc = VINF_SUCCESS;
1928
1929 if (pGstStrmIn)
1930 {
1931 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1932 AssertPtr(pHstStrmIn);
1933
1934 LogFlowFunc(("%s: fEnable=%RTbool\n", pGstStrmIn->MixBuf.pszName, fEnable));
1935
1936 rc = drvAudioControlHstIn(pThis, pHstStrmIn,
1937 fEnable ? PDMAUDIOSTREAMCMD_ENABLE : PDMAUDIOSTREAMCMD_DISABLE);
1938 if (RT_SUCCESS(rc))
1939 pGstStrmIn->State.fActive = fEnable;
1940
1941 LogFlowFunc(("%s: fEnable=%RTbool, rc=%Rrc\n", pGstStrmIn->MixBuf.pszName, fEnable, rc));
1942 }
1943
1944 return rc;
1945}
1946
1947static DECLCALLBACK(bool) drvAudioIsValidIn(PPDMIAUDIOCONNECTOR pInterface,
1948 PPDMAUDIOGSTSTRMIN pGstStrmIn)
1949{
1950 return (pGstStrmIn != NULL);
1951}
1952
1953static DECLCALLBACK(bool) drvAudioIsValidOut(PPDMIAUDIOCONNECTOR pInterface,
1954 PPDMAUDIOGSTSTRMOUT pGstStrmOut)
1955{
1956 return (pGstStrmOut != NULL);
1957}
1958
1959static DECLCALLBACK(int) drvAudioCreateIn(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
1960 PDMAUDIORECSOURCE enmRecSource, PPDMAUDIOSTREAMCFG pCfg,
1961 PPDMAUDIOGSTSTRMIN *ppGstStrmIn)
1962{
1963 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1964 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1965 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1966 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1967 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1968
1969 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1970
1971 int rc = RTCritSectEnter(&pThis->CritSect);
1972 if (RT_FAILURE(rc))
1973 return rc;
1974
1975 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
1976
1977 if (!drvAudioStreamCfgIsValid(pCfg))
1978 {
1979 LogFunc(("Input stream configuration is not valid, bailing out\n"));
1980 rc = VERR_INVALID_PARAMETER;
1981 }
1982
1983 PPDMAUDIOGSTSTRMIN pGstStrmIn = *ppGstStrmIn;
1984 if ( RT_SUCCESS(rc)
1985 && pGstStrmIn
1986 && drvAudioPCMPropsAreEqual(&pGstStrmIn->Props, pCfg))
1987 {
1988 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
1989 pGstStrmIn->MixBuf.pszName));
1990 rc = VWRN_ALREADY_EXISTS;
1991 }
1992
1993 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
1994 {
1995 int rc2 = RTCritSectLeave(&pThis->CritSect);
1996 AssertRC(rc2);
1997
1998 return rc;
1999 }
2000
2001 if ( !conf.fixed_in.enabled
2002 && pGstStrmIn)
2003 {
2004 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2005 pGstStrmIn = NULL;
2006 }
2007
2008 if (pGstStrmIn)
2009 {
2010 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
2011 AssertPtr(pHstStrmIn);
2012
2013 drvAudioGstInFreeRes(pGstStrmIn);
2014
2015 char *pszTemp;
2016 if (RTStrAPrintf(&pszTemp, "%s (Guest)", pszName) <= 0)
2017 {
2018 RTMemFree(pGstStrmIn);
2019
2020 int rc2 = RTCritSectLeave(&pThis->CritSect);
2021 AssertRC(rc2);
2022
2023 return VERR_NO_MEMORY;
2024 }
2025
2026 rc = drvAudioGstInInit(pGstStrmIn, pHstStrmIn, pszName, pCfg);
2027
2028 RTStrFree(pszTemp);
2029 }
2030 else
2031 rc = drvAudioCreateStreamPairIn(pThis, pszName, enmRecSource, pCfg, &pGstStrmIn);
2032
2033 if (pGstStrmIn)
2034 *ppGstStrmIn = pGstStrmIn;
2035
2036 int rc2 = RTCritSectLeave(&pThis->CritSect);
2037 if (RT_SUCCESS(rc))
2038 rc = rc2;
2039
2040 LogFlowFuncLeaveRC(rc);
2041 return rc;
2042}
2043
2044static DECLCALLBACK(int) drvAudioCreateOut(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
2045 PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOGSTSTRMOUT *ppGstStrmOut)
2046{
2047 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2048 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
2049 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2050 AssertPtrReturn(ppGstStrmOut, VERR_INVALID_POINTER);
2051
2052 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2053
2054 int rc = RTCritSectEnter(&pThis->CritSect);
2055 if (RT_FAILURE(rc))
2056 return rc;
2057
2058 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
2059
2060 if (!drvAudioStreamCfgIsValid(pCfg))
2061 {
2062 LogFunc(("Output stream configuration is not valid, bailing out\n"));
2063 rc = VERR_INVALID_PARAMETER;
2064 }
2065
2066 PPDMAUDIOGSTSTRMOUT pGstStrmOut = *ppGstStrmOut;
2067 if ( RT_SUCCESS(rc)
2068 && pGstStrmOut
2069 && drvAudioPCMPropsAreEqual(&pGstStrmOut->Props, pCfg))
2070 {
2071 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
2072 pGstStrmOut->MixBuf.pszName));
2073
2074 rc = VWRN_ALREADY_EXISTS;
2075 }
2076
2077 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
2078 {
2079 int rc2 = RTCritSectLeave(&pThis->CritSect);
2080 AssertRC(rc2);
2081
2082 return rc;
2083 }
2084
2085#if 0
2086 /* Any live samples that need to be updated after
2087 * we set the new parameters? */
2088 PPDMAUDIOGSTSTRMOUT pOldGstStrmOut = NULL;
2089 uint32_t cLiveSamples = 0;
2090
2091 if ( conf.plive
2092 && pGstStrmOut
2093 && ( !pGstStrmOut->State.fActive
2094 && !pGstStrmOut->State.fEmpty))
2095 {
2096 cLiveSamples = pGstStrmOut->cTotalSamplesWritten;
2097 if (cLiveSamples)
2098 {
2099 pOldGstStrmOut = pGstStrmOut;
2100 pGstStrmOut = NULL;
2101 }
2102 }
2103#endif
2104
2105 if ( pGstStrmOut
2106 && !conf.fixed_out.enabled)
2107 {
2108 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2109 pGstStrmOut = NULL;
2110 }
2111
2112 if (pGstStrmOut)
2113 {
2114 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
2115 AssertPtr(pHstStrmOut);
2116
2117 drvAudioGstOutFreeRes(pGstStrmOut);
2118
2119 rc = drvAudioGstOutInit(pGstStrmOut, pHstStrmOut, pszName, pCfg);
2120 }
2121 else
2122 {
2123 rc = drvAudioCreateStreamPairOut(pThis, pszName, pCfg, &pGstStrmOut);
2124 if (RT_FAILURE(rc))
2125 LogFunc(("Failed to create output stream \"%s\", rc=%Rrc\n", pszName, rc));
2126 }
2127
2128 if (RT_SUCCESS(rc))
2129 {
2130 AssertPtr(pGstStrmOut);
2131 *ppGstStrmOut = pGstStrmOut;
2132#if 0
2133 /* Update remaining live samples with new rate. */
2134 if (cLiveSamples)
2135 {
2136 AssertPtr(pOldGstStrmOut);
2137
2138 uint32_t cSamplesMixed =
2139 (cLiveSamples << pOldGstStrmOut->Props.cShift)
2140 * pOldGstStrmOut->Props.cbPerSec
2141 / (*ppGstStrmOut)->Props.cbPerSec;
2142
2143 pGstStrmOut->cTotalSamplesWritten += cSamplesMixed;
2144 }
2145#endif
2146 }
2147
2148 int rc2 = RTCritSectLeave(&pThis->CritSect);
2149 if (RT_SUCCESS(rc))
2150 rc = rc2;
2151
2152 LogFlowFuncLeaveRC(rc);
2153 return rc;
2154}
2155
2156static DECLCALLBACK(bool) drvAudioIsActiveIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2157{
2158 AssertPtrReturn(pInterface, false);
2159 /* pGstStrmIn is optional. */
2160
2161 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2162
2163 int rc2 = RTCritSectEnter(&pThis->CritSect);
2164 AssertRC(rc2);
2165
2166 bool fRet = pGstStrmIn ? pGstStrmIn->State.fActive : false;
2167
2168 rc2 = RTCritSectLeave(&pThis->CritSect);
2169 AssertRC(rc2);
2170
2171 return fRet;
2172}
2173
2174static DECLCALLBACK(bool) drvAudioIsActiveOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2175{
2176 AssertPtrReturn(pInterface, false);
2177 /* pGstStrmOut is optional. */
2178
2179 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2180
2181 int rc2 = RTCritSectEnter(&pThis->CritSect);
2182 AssertRC(rc2);
2183
2184 bool fRet = pGstStrmOut ? pGstStrmOut->State.fActive : false;
2185
2186 rc2 = RTCritSectLeave(&pThis->CritSect);
2187 AssertRC(rc2);
2188
2189 return fRet;
2190}
2191
2192static DECLCALLBACK(void) drvAudioDestroyIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2193{
2194 AssertPtrReturnVoid(pInterface);
2195 /* pGstStrmIn is optional. */
2196
2197 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2198
2199 int rc2 = RTCritSectEnter(&pThis->CritSect);
2200 AssertRC(rc2);
2201
2202 if (pGstStrmIn)
2203 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2204
2205 rc2 = RTCritSectLeave(&pThis->CritSect);
2206 AssertRC(rc2);
2207}
2208
2209static DECLCALLBACK(void) drvAudioDestroyOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2210{
2211 AssertPtrReturnVoid(pInterface);
2212 /* pGstStrmOut is optional. */
2213
2214 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2215
2216 int rc2 = RTCritSectEnter(&pThis->CritSect);
2217 AssertRC(rc2);
2218
2219 if (pGstStrmOut)
2220 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2221
2222 rc2 = RTCritSectLeave(&pThis->CritSect);
2223 AssertRC(rc2);
2224}
2225
2226/********************************************************************/
2227
2228/**
2229 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2230 */
2231static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2232{
2233 LogFlowFunc(("pInterface=%p, pszIID=%s\n", pInterface, pszIID));
2234
2235 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2236 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2237
2238 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
2239 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIAUDIOCONNECTOR, &pThis->IAudioConnector);
2240
2241 return NULL;
2242}
2243
2244/**
2245 * Power Off notification.
2246 *
2247 * @param pDrvIns The driver instance data.
2248 */
2249static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
2250{
2251 LogFlowFuncEnter();
2252 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2253
2254 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2255
2256 if (!pThis->pHostDrvAudio)
2257 return;
2258
2259 /* Tear down all host output streams. */
2260 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
2261 while ((pHstStrmOut = drvAudioFindAnyHstOut(pThis, pHstStrmOut)))
2262 {
2263 drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
2264 pThis->pHostDrvAudio->pfnFiniOut(pThis->pHostDrvAudio, pHstStrmOut);
2265 }
2266
2267 /* Tear down all host input streams. */
2268 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
2269 while ((pHstStrmIn = drvAudioFindNextHstIn(pThis, pHstStrmIn)))
2270 {
2271 drvAudioControlHstIn(pThis, pHstStrmIn, PDMAUDIOSTREAMCMD_DISABLE);
2272 pThis->pHostDrvAudio->pfnFiniIn(pThis->pHostDrvAudio, pHstStrmIn);
2273 }
2274
2275 if (pThis->pHostDrvAudio->pfnShutdown)
2276 pThis->pHostDrvAudio->pfnShutdown(pThis->pHostDrvAudio);
2277
2278#ifdef VBOX_WITH_AUDIO_CALLBACKS
2279 PPDMAUDIOCALLBACK pCB, pCBNext;
2280 RTListForEachSafe(&pThis->lstCBIn, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2281 drvAudioCallbackDestroy(pCB);
2282
2283 RTListForEachSafe(&pThis->lstCBOut, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2284 drvAudioCallbackDestroy(pCB);
2285#endif
2286
2287 LogFlowFuncLeave();
2288}
2289
2290/**
2291 * Constructs an audio driver instance.
2292 *
2293 * @copydoc FNPDMDRVCONSTRUCT
2294 */
2295static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
2296{
2297 LogFlowFunc(("pDrvIns=%#p, pCfgHandle=%#p, fFlags=%x\n", pDrvIns, pCfgHandle, fFlags));
2298
2299 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2300 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
2301
2302 /*
2303 * Init the static parts.
2304 */
2305 pThis->pDrvIns = pDrvIns;
2306 /* IBase. */
2307 pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
2308 /* IAudioConnector. */
2309 pThis->IAudioConnector.pfnQueryStatus = drvAudioQueryStatus;
2310 pThis->IAudioConnector.pfnRead = drvAudioRead;
2311 pThis->IAudioConnector.pfnWrite = drvAudioWrite;
2312 pThis->IAudioConnector.pfnIsActiveIn = drvAudioIsActiveIn;
2313 pThis->IAudioConnector.pfnIsActiveOut = drvAudioIsActiveOut;
2314 pThis->IAudioConnector.pfnIsValidIn = drvAudioIsValidIn;
2315 pThis->IAudioConnector.pfnIsValidOut = drvAudioIsValidOut;
2316 pThis->IAudioConnector.pfnEnableOut = drvAudioEnableOut;
2317 pThis->IAudioConnector.pfnEnableIn = drvAudioEnableIn;
2318 pThis->IAudioConnector.pfnDestroyIn = drvAudioDestroyIn;
2319 pThis->IAudioConnector.pfnDestroyOut = drvAudioDestroyOut;
2320 pThis->IAudioConnector.pfnCreateIn = drvAudioCreateIn;
2321 pThis->IAudioConnector.pfnCreateOut = drvAudioCreateOut;
2322 pThis->IAudioConnector.pfnPlayOut = drvAudioPlayOut;
2323#ifdef VBOX_WITH_AUDIO_CALLBACKS
2324 pThis->IAudioConnector.pfnRegisterCallbacks = drvAudioRegisterCallbacks;
2325 pThis->IAudioConnector.pfnCallback = drvAudioCallback;
2326#endif
2327
2328 /*
2329 * Attach driver below and query its connector interface.
2330 */
2331 PPDMIBASE pDownBase;
2332 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
2333 if (RT_FAILURE(rc))
2334 {
2335 LogRel(("Audio: Failed to attach to driver %p below (flags=0x%x), rc=%Rrc\n",
2336 pDrvIns, fFlags, rc));
2337 return rc;
2338 }
2339
2340 pThis->pHostDrvAudio = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIHOSTAUDIO);
2341 if (!pThis->pHostDrvAudio)
2342 {
2343 LogRel(("Audio: Failed to query interface for underlying host driver\n"));
2344 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
2345 N_("Host audio backend missing or invalid"));
2346 }
2347
2348#ifdef DEBUG_andy
2349 CFGMR3Dump(pCfgHandle);
2350#endif
2351
2352 rc = drvAudioInit(pCfgHandle, pDrvIns);
2353 if (RT_SUCCESS(rc))
2354 {
2355 pThis->fTerminate = false;
2356 pThis->pDrvIns = pDrvIns;
2357 }
2358
2359 LogFlowFuncLeaveRC(rc);
2360 return rc;
2361}
2362
2363/**
2364 * Destructs an audio driver instance.
2365 *
2366 * @copydoc FNPDMDRVDESTRUCT
2367 */
2368static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
2369{
2370 LogFlowFuncEnter();
2371
2372 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2373 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2374
2375 if (RTCritSectIsInitialized(&pThis->CritSect))
2376 {
2377 int rc2 = RTCritSectDelete(&pThis->CritSect);
2378 AssertRC(rc2);
2379 }
2380}
2381
2382/**
2383 * Suspend notification.
2384 *
2385 * @param pDrvIns The driver instance data.
2386 */
2387static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
2388{
2389 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_PAUSE);
2390}
2391
2392/**
2393 * Resume notification.
2394 *
2395 * @param pDrvIns The driver instance data.
2396 */
2397static DECLCALLBACK(void) drvAudioResume(PPDMDRVINS pDrvIns)
2398{
2399 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_RESUME);
2400}
2401
2402/**
2403 * Audio driver registration record.
2404 */
2405const PDMDRVREG g_DrvAUDIO =
2406{
2407 /* u32Version */
2408 PDM_DRVREG_VERSION,
2409 /* szName */
2410 "AUDIO",
2411 /* szRCMod */
2412 "",
2413 /* szR0Mod */
2414 "",
2415 /* pszDescription */
2416 "Audio connector driver",
2417 /* fFlags */
2418 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2419 /* fClass */
2420 PDM_DRVREG_CLASS_AUDIO,
2421 /* cMaxInstances */
2422 2,
2423 /* cbInstance */
2424 sizeof(DRVAUDIO),
2425 /* pfnConstruct */
2426 drvAudioConstruct,
2427 /* pfnDestruct */
2428 drvAudioDestruct,
2429 /* pfnRelocate */
2430 NULL,
2431 /* pfnIOCtl */
2432 NULL,
2433 /* pfnPowerOn */
2434 NULL,
2435 /* pfnReset */
2436 NULL,
2437 /* pfnSuspend */
2438 drvAudioSuspend,
2439 /* pfnResume */
2440 drvAudioResume,
2441 /* pfnAttach */
2442 NULL,
2443 /* pfnDetach */
2444 NULL,
2445 /* pfnPowerOff */
2446 drvAudioPowerOff,
2447 /* pfnSoftReset */
2448 NULL,
2449 /* u32EndVersion */
2450 PDM_DRVREG_VERSION
2451};
Note: See TracBrowser for help on using the repository browser.

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