VirtualBox

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

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

Build fix, warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.7 KB
Line 
1/* $Id: DrvAudio.cpp 59989 2016-03-11 12:29:36Z 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-2016 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->cStreamsFreeOut++;
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->cStreamsFreeOut)
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->cStreamsFreeOut--;
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->cStreamsFreeIn)
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->cStreamsFreeIn--;
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->cStreamsFreeIn++;
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 if (RT_FAILURE(rc))
1402 LogFlowFuncLeaveRC(rc);
1403
1404 return rc;
1405}
1406
1407static DECLCALLBACK(int) drvAudioPlayOut(PPDMIAUDIOCONNECTOR pInterface, uint32_t *pcSamplesPlayed)
1408{
1409 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1410 /* pcSamplesPlayed is optional. */
1411
1412 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1413
1414 int rc = RTCritSectEnter(&pThis->CritSect);
1415 if (RT_FAILURE(rc))
1416 return rc;
1417
1418 /* Backend output (temporarily) disabled / unavailable? */
1419 if (!pThis->pHostDrvAudio->pfnIsEnabled(pThis->pHostDrvAudio, PDMAUDIODIR_OUT))
1420 {
1421 rc = pThis->pHostDrvAudio->pfnGetConf(pThis->pHostDrvAudio, &pThis->BackendCfg);
1422 AssertRC(rc);
1423
1424 if (!pThis->BackendCfg.cMaxStreamsOut)
1425 {
1426 int rc2 = RTCritSectLeave(&pThis->CritSect);
1427 AssertRC(rc2);
1428
1429 return VERR_NOT_AVAILABLE;
1430 }
1431 }
1432
1433 /*
1434 * Process all enabled host output streams.
1435 */
1436 uint32_t cSamplesPlayedMax = 0;
1437 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
1438 while ((pHstStrmOut = drvAudioHstFindAnyEnabledOut(pThis, pHstStrmOut)))
1439 {
1440#if 0
1441 uint32_t cStreamsLive;
1442 uint32_t cSamplesLive = drvAudioHstOutSamplesLive(pHstStrmOut, &cStreamsLive);
1443 if (!cStreamsLive)
1444 cSamplesLive = 0;
1445
1446 /* Has this stream marked as disabled but there still were guest streams relying
1447 * on it? Check if this stream now can be closed and do so, if possible. */
1448 if ( pHstStrmOut->fPendingDisable
1449 && !cStreamsLive)
1450 {
1451 /* Stop playing the current (pending) stream. */
1452 int rc2 = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut,
1453 PDMAUDIOSTREAMCMD_DISABLE);
1454 if (RT_SUCCESS(rc2))
1455 {
1456 pHstStrmOut->fEnabled = false;
1457 pHstStrmOut->fPendingDisable = false;
1458
1459 LogFunc(("\t%p: Disabling stream\n", pHstStrmOut));
1460 }
1461 else
1462 LogFunc(("\t%p: Backend vetoed against closing output stream, rc=%Rrc\n",
1463 pHstStrmOut, rc2));
1464
1465 continue;
1466 }
1467#endif
1468
1469 uint32_t cSamplesPlayed = 0;
1470 int rc2 = pThis->pHostDrvAudio->pfnPlayOut(pThis->pHostDrvAudio, pHstStrmOut, &cSamplesPlayed);
1471 if (RT_FAILURE(rc2))
1472 {
1473 rc2 = pThis->pHostDrvAudio->pfnControlOut(pThis->pHostDrvAudio, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1474 AssertRC(rc2);
1475 }
1476 else
1477 cSamplesPlayedMax = RT_MAX(cSamplesPlayed, cSamplesPlayedMax);
1478
1479 LogFlowFunc(("\t[%s] cSamplesPlayed=%RU32, cSamplesPlayedMax=%RU32, rc=%Rrc\n",
1480 pHstStrmOut->MixBuf.pszName, cSamplesPlayed, cSamplesPlayedMax, rc2));
1481
1482 bool fNeedsCleanup = false;
1483
1484 PPDMAUDIOGSTSTRMOUT pGstStrmOut;
1485 RTListForEach(&pHstStrmOut->lstGstStrmOut, pGstStrmOut, PDMAUDIOGSTSTRMOUT, Node)
1486 {
1487 if ( !pGstStrmOut->State.fActive
1488 && pGstStrmOut->State.fEmpty)
1489 continue;
1490
1491 if (AudioMixBufIsEmpty(&pGstStrmOut->MixBuf))
1492 {
1493 pGstStrmOut->State.fEmpty = true;
1494 fNeedsCleanup |= !pGstStrmOut->State.fActive;
1495 }
1496 }
1497
1498 if (fNeedsCleanup)
1499 {
1500 RTListForEach(&pHstStrmOut->lstGstStrmOut, pGstStrmOut, PDMAUDIOGSTSTRMOUT, Node)
1501 {
1502 if (!pGstStrmOut->State.fActive)
1503 drvAudioDestroyGstOut(pThis, pGstStrmOut);
1504 }
1505 }
1506 }
1507
1508 if (RT_SUCCESS(rc))
1509 {
1510 if (pcSamplesPlayed)
1511 *pcSamplesPlayed = cSamplesPlayedMax;
1512 }
1513
1514 int rc2 = RTCritSectLeave(&pThis->CritSect);
1515 if (RT_SUCCESS(rc))
1516 rc = rc2;
1517
1518 if (RT_FAILURE(rc))
1519 LogFlowFuncLeaveRC(rc);
1520
1521 return rc;
1522}
1523
1524#ifdef VBOX_WITH_AUDIO_CALLBACKS
1525static PPDMAUDIOCALLBACK drvAudioCallbackDuplicate(PPDMAUDIOCALLBACK pCB)
1526{
1527 PPDMAUDIOCALLBACK pCBCopy = (PPDMAUDIOCALLBACK)RTMemDup((void *)pCB, sizeof(PDMAUDIOCALLBACK));
1528 if (!pCBCopy)
1529 return NULL;
1530
1531 if (pCB->pvCtx)
1532 {
1533 pCBCopy->pvCtx = RTMemDup(pCB->pvCtx, pCB->cbCtx);
1534 if (!pCBCopy->pvCtx)
1535 {
1536 RTMemFree(pCBCopy);
1537 return NULL;
1538 }
1539
1540 pCBCopy->cbCtx = pCB->cbCtx;
1541 }
1542
1543 return pCBCopy;
1544}
1545
1546static void drvAudioCallbackDestroy(PPDMAUDIOCALLBACK pCB)
1547{
1548 if (!pCB)
1549 return;
1550
1551 RTListNodeRemove(&pCB->Node);
1552 if (pCB->pvCtx)
1553 {
1554 Assert(pCB->cbCtx);
1555 RTMemFree(pCB->pvCtx);
1556 }
1557 RTMemFree(pCB);
1558}
1559
1560static DECLCALLBACK(int) drvAudioRegisterCallbacks(PPDMIAUDIOCONNECTOR pInterface,
1561 PPDMAUDIOCALLBACK paCallbacks, size_t cCallbacks)
1562{
1563 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1564 AssertPtrReturn(paCallbacks, VERR_INVALID_POINTER);
1565 AssertReturn(cCallbacks, VERR_INVALID_PARAMETER);
1566
1567 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1568
1569 int rc = RTCritSectEnter(&pThis->CritSect);
1570 if (RT_FAILURE(rc))
1571 return rc;
1572
1573 for (size_t i = 0; i < cCallbacks; i++)
1574 {
1575 PPDMAUDIOCALLBACK pCB = drvAudioCallbackDuplicate(&paCallbacks[i]);
1576 if (!pCB)
1577 {
1578 rc = VERR_NO_MEMORY;
1579 break;
1580 }
1581
1582 switch (pCB->enmType)
1583 {
1584 case PDMAUDIOCALLBACKTYPE_INPUT:
1585 RTListAppend(&pThis->lstCBIn, &pCB->Node);
1586 break;
1587
1588 case PDMAUDIOCALLBACKTYPE_OUTPUT:
1589 RTListAppend(&pThis->lstCBOut, &pCB->Node);
1590 break;
1591
1592 default:
1593 AssertMsgFailed(("Not supported\n"));
1594 break;
1595 }
1596 }
1597
1598 /** @todo Undo allocations on error. */
1599
1600 int rc2 = RTCritSectLeave(&pThis->CritSect);
1601 if (RT_SUCCESS(rc))
1602 rc = rc2;
1603
1604 return rc;
1605}
1606
1607static DECLCALLBACK(int) drvAudioCallback(PPDMIAUDIOCONNECTOR pInterface, PDMAUDIOCALLBACKTYPE enmType,
1608 void *pvUser, size_t cbUser)
1609{
1610 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1611 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
1612 AssertReturn(cbUser, VERR_INVALID_PARAMETER);
1613
1614 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1615 PRTLISTANCHOR pListAnchor = NULL;
1616
1617 switch (enmType)
1618 {
1619 case PDMAUDIOCALLBACKTYPE_INPUT:
1620 pListAnchor = &pThis->lstCBIn;
1621 break;
1622
1623 case PDMAUDIOCALLBACKTYPE_OUTPUT:
1624 pListAnchor = &pThis->lstCBOut;
1625 break;
1626
1627 default:
1628 AssertMsgFailed(("Not supported\n"));
1629 break;
1630 }
1631
1632 if (pListAnchor)
1633 {
1634 PPDMAUDIOCALLBACK pCB;
1635 RTListForEach(pListAnchor, pCB, PDMAUDIOCALLBACK, Node)
1636 {
1637 Assert(pCB->enmType == enmType);
1638 pCB->pfnCallback(enmType, pCB->pvCtx, pCB->cbCtx, pvUser, cbUser);
1639 }
1640 }
1641
1642 return VINF_SUCCESS;
1643}
1644#endif
1645
1646static int drvAudioHostInit(PCFGMNODE pCfgHandle, PDRVAUDIO pThis)
1647{
1648 /* pCfgHandle is optional. */
1649 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1650
1651 NOREF(pCfgHandle);
1652
1653 LogFlowFuncEnter();
1654
1655 AssertPtr(pThis->pHostDrvAudio);
1656 int rc = pThis->pHostDrvAudio->pfnInit(pThis->pHostDrvAudio);
1657 if (RT_FAILURE(rc))
1658 {
1659 LogFlowFunc(("Initialization of lower driver failed with rc=%Rrc\n", rc));
1660 return rc;
1661 }
1662
1663 /* Get the configuration data from backend. */
1664 rc = pThis->pHostDrvAudio->pfnGetConf(pThis->pHostDrvAudio, &pThis->BackendCfg);
1665 if (RT_FAILURE(rc))
1666 {
1667 LogFlowFunc(("Getting backend configuration failed with rc=%Rrc\n", rc));
1668 return rc;
1669 }
1670
1671 if (pThis->BackendCfg.cbStreamOut)
1672 {
1673 pThis->cStreamsFreeOut = pThis->BackendCfg.cMaxStreamsOut;
1674 }
1675 else
1676 pThis->cStreamsFreeOut = 0;
1677
1678 if (pThis->BackendCfg.cbStreamIn)
1679 {
1680 /*
1681 * Note:
1682 * - Our AC'97 emulation has two inputs, line (ac97.pi) and microphone (ac97.mc).
1683 * - Our HDA emulation currently has only line input (hda.pi).
1684 */
1685 pThis->cStreamsFreeIn = pThis->BackendCfg.cMaxStreamsIn;
1686 }
1687 else
1688 pThis->cStreamsFreeIn = 0;
1689
1690 LogFlowFunc(("cStreamsFreeIn=%RU8, cStreamsFreeOut=%RU8\n", pThis->cStreamsFreeIn, pThis->cStreamsFreeOut));
1691
1692 LogRel2(("Audio: Host audio backend supports %RU32 input streams and %RU32 output streams at once\n",
1693 /* Clamp for logging. Unlimited streams are defined by UINT32_MAX. */
1694 RT_MIN(64, pThis->cStreamsFreeIn), RT_MIN(64, pThis->cStreamsFreeOut)));
1695
1696 LogFlowFuncLeave();
1697 return VINF_SUCCESS;
1698}
1699
1700static void drvAudioStateHandler(PPDMDRVINS pDrvIns, PDMAUDIOSTREAMCMD enmCmd)
1701{
1702 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1703 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1704
1705 LogFlowFunc(("enmCmd=%ld\n", enmCmd));
1706
1707 if (!pThis->pHostDrvAudio)
1708 return;
1709
1710 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
1711 while ((pHstStrmOut = drvAudioHstFindAnyEnabledOut(pThis, pHstStrmOut)))
1712 drvAudioControlHstOut(pThis, pHstStrmOut, enmCmd);
1713
1714 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
1715 while ((pHstStrmIn = drvAudioFindNextEnabledHstIn(pThis, pHstStrmIn)))
1716 drvAudioControlHstIn(pThis, pHstStrmIn, enmCmd);
1717}
1718
1719static struct audio_option audio_options[] =
1720{
1721 /* DAC */
1722 {"DACFixedSettings", AUD_OPT_BOOL, &conf.fixed_out.enabled,
1723 "Use fixed settings for host DAC", NULL, 0},
1724
1725 {"DACFixedFreq", AUD_OPT_INT, &conf.fixed_out.settings.uHz,
1726 "Frequency for fixed host DAC", NULL, 0},
1727
1728 {"DACFixedFmt", AUD_OPT_FMT, &conf.fixed_out.settings.enmFormat,
1729 "Format for fixed host DAC", NULL, 0},
1730
1731 {"DACFixedChannels", AUD_OPT_INT, &conf.fixed_out.settings.cChannels,
1732 "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0},
1733
1734 {"DACVoices", AUD_OPT_INT, &conf.fixed_out.cStreams, /** @todo Rename! */
1735 "Number of streams for DAC", NULL, 0},
1736
1737 /* ADC */
1738 {"ADCFixedSettings", AUD_OPT_BOOL, &conf.fixed_in.enabled,
1739 "Use fixed settings for host ADC", NULL, 0},
1740
1741 {"ADCFixedFreq", AUD_OPT_INT, &conf.fixed_in.settings.uHz,
1742 "Frequency for fixed host ADC", NULL, 0},
1743
1744 {"ADCFixedFmt", AUD_OPT_FMT, &conf.fixed_in.settings.enmFormat,
1745 "Format for fixed host ADC", NULL, 0},
1746
1747 {"ADCFixedChannels", AUD_OPT_INT, &conf.fixed_in.settings.cChannels,
1748 "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0},
1749
1750 {"ADCVoices", AUD_OPT_INT, &conf.fixed_in.cStreams, /** @todo Rename! */
1751 "Number of streams for ADC", NULL, 0},
1752
1753 /* Misc */
1754 {"TimerFreq", AUD_OPT_INT, &conf.period.hz,
1755 "Timer frequency in Hz (0 - use lowest possible)", NULL, 0},
1756
1757 {"PLIVE", AUD_OPT_BOOL, &conf.plive,
1758 "(undocumented)", NULL, 0} /** @todo What is this? */
1759};
1760
1761static DECLCALLBACK(int) drvAudioInit(PCFGMNODE pCfgHandle, PPDMDRVINS pDrvIns)
1762{
1763 AssertPtrReturn(pCfgHandle, VERR_INVALID_POINTER);
1764 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1765
1766 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1767 LogFlowFunc(("pDrvAudio=%p, pDrvIns=%p\n", pThis, pDrvIns));
1768
1769 RTListInit(&pThis->lstHstStrmIn);
1770 RTListInit(&pThis->lstHstStrmOut);
1771#ifdef VBOX_WITH_AUDIO_CALLBACKS
1772 RTListInit(&pThis->lstCBIn);
1773 RTListInit(&pThis->lstCBOut);
1774#endif
1775
1776 int rc = RTCritSectInit(&pThis->CritSect);
1777 if (RT_SUCCESS(rc))
1778 {
1779 rc = drvAudioProcessOptions(pCfgHandle, "AUDIO", audio_options);
1780 /** @todo Check for invalid options? */
1781
1782 pThis->cStreamsFreeOut = conf.fixed_out.cStreams;
1783 pThis->cStreamsFreeIn = conf.fixed_in.cStreams;
1784
1785 if (!pThis->cStreamsFreeOut)
1786 pThis->cStreamsFreeOut = 1;
1787
1788 if (!pThis->cStreamsFreeIn)
1789 pThis->cStreamsFreeIn = 1;
1790 }
1791
1792 /*
1793 * If everything went well, initialize the lower driver.
1794 */
1795 if (RT_SUCCESS(rc))
1796 rc = drvAudioHostInit(pCfgHandle, pThis);
1797
1798 LogFlowFuncLeaveRC(rc);
1799 return rc;
1800}
1801
1802static DECLCALLBACK(int) drvAudioRead(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn,
1803 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1804{
1805 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1806 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1807
1808 AssertPtrReturn(pGstStrmIn, VERR_INVALID_POINTER);
1809 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1810 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1811 /* pcbWritten is optional. */
1812
1813 int rc = RTCritSectEnter(&pThis->CritSect);
1814 if (RT_FAILURE(rc))
1815 return rc;
1816
1817 if (!pThis->pHostDrvAudio->pfnIsEnabled(pThis->pHostDrvAudio, PDMAUDIODIR_IN))
1818 {
1819 if (pcbRead)
1820 *pcbRead = 0;
1821
1822 return RTCritSectLeave(&pThis->CritSect);
1823 }
1824
1825 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1826 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
1827
1828 AssertMsg(pGstStrmIn->pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED,
1829 ("Reading from disabled host input stream \"%s\" not possible\n", pGstStrmIn->MixBuf.pszName));
1830
1831 /*
1832 * Read from the parent buffer (that is, the guest buffer) which
1833 * should have the audio data in the format the guest needs.
1834 */
1835 uint32_t cRead;
1836 rc = AudioMixBufReadCirc(&pGstStrmIn->MixBuf, pvBuf, cbBuf, &cRead);
1837 if (RT_SUCCESS(rc))
1838 {
1839 AudioMixBufFinish(&pGstStrmIn->MixBuf, cRead);
1840
1841 if (pcbRead)
1842 *pcbRead = AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead);
1843 }
1844
1845 LogFlowFunc(("cRead=%RU32 (%RU32 bytes), rc=%Rrc\n",
1846 cRead, AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead), rc));
1847
1848 int rc2 = RTCritSectLeave(&pThis->CritSect);
1849 if (RT_SUCCESS(rc))
1850 rc = rc2;
1851
1852 return rc;
1853}
1854
1855static DECLCALLBACK(int) drvAudioEnableOut(PPDMIAUDIOCONNECTOR pInterface,
1856 PPDMAUDIOGSTSTRMOUT pGstStrmOut, bool fEnable)
1857{
1858 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1859 /* pGstStrmOut is optional. */
1860
1861 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1862
1863 int rc = VINF_SUCCESS;
1864
1865 if (pGstStrmOut)
1866 {
1867 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
1868 AssertPtr(pHstStrmOut);
1869
1870 if (fEnable)
1871 {
1872 /* Is a pending disable outstanding? Then disable first. */
1873 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE)
1874 {
1875 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1876 if (RT_SUCCESS(rc))
1877 pHstStrmOut->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1878 }
1879
1880 if (RT_SUCCESS(rc))
1881 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_ENABLE);
1882 }
1883 else /* Disable */
1884 {
1885 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
1886 {
1887 uint32_t cGstStrmsActive = 0;
1888
1889 /*
1890 * Check if there are any active guest streams assigned
1891 * to this host stream which still are being marked as active.
1892 *
1893 * In that case we have to defer closing the host stream and
1894 * wait until all guest streams have been finished.
1895 */
1896 PPDMAUDIOGSTSTRMOUT pIter;
1897 RTListForEach(&pHstStrmOut->lstGstStrmOut, pIter, PDMAUDIOGSTSTRMOUT, Node)
1898 {
1899 if (pIter->State.fActive)
1900 {
1901 cGstStrmsActive++;
1902 break; /* At least one assigned & active guest stream is enough. */
1903 }
1904 }
1905
1906 /* Do we need to defer closing the host stream? */
1907 if (cGstStrmsActive >= 1)
1908 pHstStrmOut->fStatus |= PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1909
1910 /* Can we close the host stream now instead of deferring it? */
1911 if (!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE))
1912 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1913 }
1914 }
1915
1916 if (RT_SUCCESS(rc))
1917 pGstStrmOut->State.fActive = fEnable;
1918
1919 LogFlowFunc(("%s: fEnable=%RTbool, fStatus=0x%x, rc=%Rrc\n",
1920 pGstStrmOut->MixBuf.pszName, fEnable, pHstStrmOut->fStatus, rc));
1921 }
1922
1923 return rc;
1924}
1925
1926static DECLCALLBACK(int) drvAudioEnableIn(PPDMIAUDIOCONNECTOR pInterface,
1927 PPDMAUDIOGSTSTRMIN pGstStrmIn, bool fEnable)
1928{
1929 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1930 /* pGstStrmIn is optional. */
1931
1932 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1933
1934 int rc = VINF_SUCCESS;
1935
1936 if (pGstStrmIn)
1937 {
1938 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1939 AssertPtr(pHstStrmIn);
1940
1941 LogFlowFunc(("%s: fEnable=%RTbool\n", pGstStrmIn->MixBuf.pszName, fEnable));
1942
1943 rc = drvAudioControlHstIn(pThis, pHstStrmIn,
1944 fEnable ? PDMAUDIOSTREAMCMD_ENABLE : PDMAUDIOSTREAMCMD_DISABLE);
1945 if (RT_SUCCESS(rc))
1946 pGstStrmIn->State.fActive = fEnable;
1947
1948 LogFlowFunc(("%s: fEnable=%RTbool, rc=%Rrc\n", pGstStrmIn->MixBuf.pszName, fEnable, rc));
1949 }
1950
1951 return rc;
1952}
1953
1954static DECLCALLBACK(bool) drvAudioIsValidIn(PPDMIAUDIOCONNECTOR pInterface,
1955 PPDMAUDIOGSTSTRMIN pGstStrmIn)
1956{
1957 return (pGstStrmIn != NULL);
1958}
1959
1960static DECLCALLBACK(bool) drvAudioIsValidOut(PPDMIAUDIOCONNECTOR pInterface,
1961 PPDMAUDIOGSTSTRMOUT pGstStrmOut)
1962{
1963 return (pGstStrmOut != NULL);
1964}
1965
1966static DECLCALLBACK(int) drvAudioCreateIn(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
1967 PDMAUDIORECSOURCE enmRecSource, PPDMAUDIOSTREAMCFG pCfg,
1968 PPDMAUDIOGSTSTRMIN *ppGstStrmIn)
1969{
1970 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1971 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1972 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1973 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1974 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1975
1976 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1977
1978 int rc = RTCritSectEnter(&pThis->CritSect);
1979 if (RT_FAILURE(rc))
1980 return rc;
1981
1982 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
1983
1984 if (!drvAudioStreamCfgIsValid(pCfg))
1985 {
1986 LogFunc(("Input stream configuration is not valid, bailing out\n"));
1987 rc = VERR_INVALID_PARAMETER;
1988 }
1989
1990 PPDMAUDIOGSTSTRMIN pGstStrmIn = *ppGstStrmIn;
1991 if ( RT_SUCCESS(rc)
1992 && pGstStrmIn
1993 && drvAudioPCMPropsAreEqual(&pGstStrmIn->Props, pCfg))
1994 {
1995 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
1996 pGstStrmIn->MixBuf.pszName));
1997 rc = VWRN_ALREADY_EXISTS;
1998 }
1999
2000 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
2001 {
2002 int rc2 = RTCritSectLeave(&pThis->CritSect);
2003 AssertRC(rc2);
2004
2005 return rc;
2006 }
2007
2008 if ( !conf.fixed_in.enabled
2009 && pGstStrmIn)
2010 {
2011 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2012 pGstStrmIn = NULL;
2013 }
2014
2015 if (pGstStrmIn)
2016 {
2017 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
2018 AssertPtr(pHstStrmIn);
2019
2020 drvAudioGstInFreeRes(pGstStrmIn);
2021
2022 char *pszTemp;
2023 if (RTStrAPrintf(&pszTemp, "%s (Guest)", pszName) <= 0)
2024 {
2025 RTMemFree(pGstStrmIn);
2026
2027 int rc2 = RTCritSectLeave(&pThis->CritSect);
2028 AssertRC(rc2);
2029
2030 return VERR_NO_MEMORY;
2031 }
2032
2033 rc = drvAudioGstInInit(pGstStrmIn, pHstStrmIn, pszName, pCfg);
2034
2035 RTStrFree(pszTemp);
2036 }
2037 else
2038 rc = drvAudioCreateStreamPairIn(pThis, pszName, enmRecSource, pCfg, &pGstStrmIn);
2039
2040 if (RT_SUCCESS(rc))
2041 {
2042 if (pGstStrmIn)
2043 *ppGstStrmIn = pGstStrmIn;
2044 }
2045
2046 int rc2 = RTCritSectLeave(&pThis->CritSect);
2047 if (RT_SUCCESS(rc))
2048 rc = rc2;
2049
2050 LogFlowFuncLeaveRC(rc);
2051 return rc;
2052}
2053
2054static DECLCALLBACK(int) drvAudioCreateOut(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
2055 PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOGSTSTRMOUT *ppGstStrmOut)
2056{
2057 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2058 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
2059 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2060 AssertPtrReturn(ppGstStrmOut, VERR_INVALID_POINTER);
2061
2062 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2063
2064 int rc = RTCritSectEnter(&pThis->CritSect);
2065 if (RT_FAILURE(rc))
2066 return rc;
2067
2068 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
2069
2070 if (!drvAudioStreamCfgIsValid(pCfg))
2071 {
2072 LogFunc(("Output stream configuration is not valid, bailing out\n"));
2073 rc = VERR_INVALID_PARAMETER;
2074 }
2075
2076 PPDMAUDIOGSTSTRMOUT pGstStrmOut = *ppGstStrmOut;
2077 if ( RT_SUCCESS(rc)
2078 && pGstStrmOut
2079 && drvAudioPCMPropsAreEqual(&pGstStrmOut->Props, pCfg))
2080 {
2081 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
2082 pGstStrmOut->MixBuf.pszName));
2083
2084 rc = VWRN_ALREADY_EXISTS;
2085 }
2086
2087 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
2088 {
2089 int rc2 = RTCritSectLeave(&pThis->CritSect);
2090 AssertRC(rc2);
2091
2092 return rc;
2093 }
2094
2095#if 0
2096 /* Any live samples that need to be updated after
2097 * we set the new parameters? */
2098 PPDMAUDIOGSTSTRMOUT pOldGstStrmOut = NULL;
2099 uint32_t cLiveSamples = 0;
2100
2101 if ( conf.plive
2102 && pGstStrmOut
2103 && ( !pGstStrmOut->State.fActive
2104 && !pGstStrmOut->State.fEmpty))
2105 {
2106 cLiveSamples = pGstStrmOut->cTotalSamplesWritten;
2107 if (cLiveSamples)
2108 {
2109 pOldGstStrmOut = pGstStrmOut;
2110 pGstStrmOut = NULL;
2111 }
2112 }
2113#endif
2114
2115 if ( pGstStrmOut
2116 && !conf.fixed_out.enabled)
2117 {
2118 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2119 pGstStrmOut = NULL;
2120 }
2121
2122 if (pGstStrmOut)
2123 {
2124 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
2125 AssertPtr(pHstStrmOut);
2126
2127 drvAudioGstOutFreeRes(pGstStrmOut);
2128
2129 rc = drvAudioGstOutInit(pGstStrmOut, pHstStrmOut, pszName, pCfg);
2130 }
2131 else
2132 {
2133 rc = drvAudioCreateStreamPairOut(pThis, pszName, pCfg, &pGstStrmOut);
2134 if (RT_FAILURE(rc))
2135 LogFunc(("Failed to create output stream \"%s\", rc=%Rrc\n", pszName, rc));
2136 }
2137
2138 if (RT_SUCCESS(rc))
2139 {
2140 if (pGstStrmOut)
2141 *ppGstStrmOut = pGstStrmOut;
2142#if 0
2143 /* Update remaining live samples with new rate. */
2144 if (cLiveSamples)
2145 {
2146 AssertPtr(pOldGstStrmOut);
2147
2148 uint32_t cSamplesMixed =
2149 (cLiveSamples << pOldGstStrmOut->Props.cShift)
2150 * pOldGstStrmOut->Props.cbPerSec
2151 / (*ppGstStrmOut)->Props.cbPerSec;
2152
2153 pGstStrmOut->cTotalSamplesWritten += cSamplesMixed;
2154 }
2155#endif
2156 }
2157
2158 int rc2 = RTCritSectLeave(&pThis->CritSect);
2159 if (RT_SUCCESS(rc))
2160 rc = rc2;
2161
2162 LogFlowFuncLeaveRC(rc);
2163 return rc;
2164}
2165
2166static DECLCALLBACK(int) drvAudioGetConfiguration(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOBACKENDCFG pCfg)
2167{
2168 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2169 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2170
2171 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2172
2173 int rc = RTCritSectEnter(&pThis->CritSect);
2174 if (RT_FAILURE(rc))
2175 return rc;
2176
2177 rc = pThis->pHostDrvAudio->pfnGetConf(pThis->pHostDrvAudio, pCfg);
2178
2179 int rc2 = RTCritSectLeave(&pThis->CritSect);
2180 if (RT_SUCCESS(rc))
2181 rc = rc2;
2182
2183 LogFlowFuncLeaveRC(rc);
2184 return rc;
2185}
2186
2187static DECLCALLBACK(bool) drvAudioIsActiveIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2188{
2189 AssertPtrReturn(pInterface, false);
2190 /* pGstStrmIn is optional. */
2191
2192 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2193
2194 int rc2 = RTCritSectEnter(&pThis->CritSect);
2195 AssertRC(rc2);
2196
2197 bool fRet = pGstStrmIn ? pGstStrmIn->State.fActive : false;
2198
2199 rc2 = RTCritSectLeave(&pThis->CritSect);
2200 AssertRC(rc2);
2201
2202 return fRet;
2203}
2204
2205static DECLCALLBACK(bool) drvAudioIsActiveOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2206{
2207 AssertPtrReturn(pInterface, false);
2208 /* pGstStrmOut is optional. */
2209
2210 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2211
2212 int rc2 = RTCritSectEnter(&pThis->CritSect);
2213 AssertRC(rc2);
2214
2215 bool fRet = pGstStrmOut ? pGstStrmOut->State.fActive : false;
2216
2217 rc2 = RTCritSectLeave(&pThis->CritSect);
2218 AssertRC(rc2);
2219
2220 return fRet;
2221}
2222
2223static DECLCALLBACK(void) drvAudioDestroyIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2224{
2225 AssertPtrReturnVoid(pInterface);
2226 /* pGstStrmIn is optional. */
2227
2228 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2229
2230 int rc2 = RTCritSectEnter(&pThis->CritSect);
2231 AssertRC(rc2);
2232
2233 if (pGstStrmIn)
2234 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2235
2236 rc2 = RTCritSectLeave(&pThis->CritSect);
2237 AssertRC(rc2);
2238}
2239
2240static DECLCALLBACK(void) drvAudioDestroyOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2241{
2242 AssertPtrReturnVoid(pInterface);
2243 /* pGstStrmOut is optional. */
2244
2245 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2246
2247 int rc2 = RTCritSectEnter(&pThis->CritSect);
2248 AssertRC(rc2);
2249
2250 if (pGstStrmOut)
2251 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2252
2253 rc2 = RTCritSectLeave(&pThis->CritSect);
2254 AssertRC(rc2);
2255}
2256
2257/********************************************************************/
2258
2259/**
2260 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2261 */
2262static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2263{
2264 LogFlowFunc(("pInterface=%p, pszIID=%s\n", pInterface, pszIID));
2265
2266 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2267 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2268
2269 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
2270 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIAUDIOCONNECTOR, &pThis->IAudioConnector);
2271
2272 return NULL;
2273}
2274
2275/**
2276 * Power Off notification.
2277 *
2278 * @param pDrvIns The driver instance data.
2279 */
2280static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
2281{
2282 LogFlowFuncEnter();
2283 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2284
2285 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2286
2287 if (!pThis->pHostDrvAudio)
2288 return;
2289
2290 /* Tear down all host output streams. */
2291 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
2292 while ((pHstStrmOut = drvAudioFindAnyHstOut(pThis, pHstStrmOut)))
2293 {
2294 drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
2295 pThis->pHostDrvAudio->pfnFiniOut(pThis->pHostDrvAudio, pHstStrmOut);
2296 }
2297
2298 /* Tear down all host input streams. */
2299 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
2300 while ((pHstStrmIn = drvAudioFindNextHstIn(pThis, pHstStrmIn)))
2301 {
2302 drvAudioControlHstIn(pThis, pHstStrmIn, PDMAUDIOSTREAMCMD_DISABLE);
2303 pThis->pHostDrvAudio->pfnFiniIn(pThis->pHostDrvAudio, pHstStrmIn);
2304 }
2305
2306 if (pThis->pHostDrvAudio->pfnShutdown)
2307 pThis->pHostDrvAudio->pfnShutdown(pThis->pHostDrvAudio);
2308
2309#ifdef VBOX_WITH_AUDIO_CALLBACKS
2310 PPDMAUDIOCALLBACK pCB, pCBNext;
2311 RTListForEachSafe(&pThis->lstCBIn, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2312 drvAudioCallbackDestroy(pCB);
2313
2314 RTListForEachSafe(&pThis->lstCBOut, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2315 drvAudioCallbackDestroy(pCB);
2316#endif
2317
2318 LogFlowFuncLeave();
2319}
2320
2321/**
2322 * Constructs an audio driver instance.
2323 *
2324 * @copydoc FNPDMDRVCONSTRUCT
2325 */
2326static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
2327{
2328 LogFlowFunc(("pDrvIns=%#p, pCfgHandle=%#p, fFlags=%x\n", pDrvIns, pCfgHandle, fFlags));
2329
2330 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2331 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
2332
2333 /*
2334 * Init the static parts.
2335 */
2336 pThis->pDrvIns = pDrvIns;
2337 /* IBase. */
2338 pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
2339 /* IAudioConnector. */
2340 pThis->IAudioConnector.pfnQueryStatus = drvAudioQueryStatus;
2341 pThis->IAudioConnector.pfnRead = drvAudioRead;
2342 pThis->IAudioConnector.pfnWrite = drvAudioWrite;
2343 pThis->IAudioConnector.pfnGetConfiguration = drvAudioGetConfiguration;
2344 pThis->IAudioConnector.pfnIsActiveIn = drvAudioIsActiveIn;
2345 pThis->IAudioConnector.pfnIsActiveOut = drvAudioIsActiveOut;
2346 pThis->IAudioConnector.pfnIsValidIn = drvAudioIsValidIn;
2347 pThis->IAudioConnector.pfnIsValidOut = drvAudioIsValidOut;
2348 pThis->IAudioConnector.pfnEnableOut = drvAudioEnableOut;
2349 pThis->IAudioConnector.pfnEnableIn = drvAudioEnableIn;
2350 pThis->IAudioConnector.pfnDestroyIn = drvAudioDestroyIn;
2351 pThis->IAudioConnector.pfnDestroyOut = drvAudioDestroyOut;
2352 pThis->IAudioConnector.pfnCreateIn = drvAudioCreateIn;
2353 pThis->IAudioConnector.pfnCreateOut = drvAudioCreateOut;
2354 pThis->IAudioConnector.pfnPlayOut = drvAudioPlayOut;
2355#ifdef VBOX_WITH_AUDIO_CALLBACKS
2356 pThis->IAudioConnector.pfnRegisterCallbacks = drvAudioRegisterCallbacks;
2357 pThis->IAudioConnector.pfnCallback = drvAudioCallback;
2358#endif
2359
2360 /*
2361 * Attach driver below and query its connector interface.
2362 */
2363 PPDMIBASE pDownBase;
2364 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
2365 if (RT_FAILURE(rc))
2366 {
2367 LogRel(("Audio: Failed to attach to driver %p below (flags=0x%x), rc=%Rrc\n",
2368 pDrvIns, fFlags, rc));
2369 return rc;
2370 }
2371
2372 pThis->pHostDrvAudio = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIHOSTAUDIO);
2373 if (!pThis->pHostDrvAudio)
2374 {
2375 LogRel(("Audio: Failed to query interface for underlying host driver\n"));
2376 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
2377 N_("Host audio backend missing or invalid"));
2378 }
2379
2380#ifdef DEBUG_andy
2381 CFGMR3Dump(pCfgHandle);
2382#endif
2383
2384 rc = drvAudioInit(pCfgHandle, pDrvIns);
2385 if (RT_SUCCESS(rc))
2386 {
2387 pThis->fTerminate = false;
2388 pThis->pDrvIns = pDrvIns;
2389 }
2390
2391 LogFlowFuncLeaveRC(rc);
2392 return rc;
2393}
2394
2395/**
2396 * Destructs an audio driver instance.
2397 *
2398 * @copydoc FNPDMDRVDESTRUCT
2399 */
2400static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
2401{
2402 LogFlowFuncEnter();
2403
2404 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2405 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2406
2407 if (RTCritSectIsInitialized(&pThis->CritSect))
2408 {
2409 int rc2 = RTCritSectDelete(&pThis->CritSect);
2410 AssertRC(rc2);
2411 }
2412}
2413
2414/**
2415 * Suspend notification.
2416 *
2417 * @param pDrvIns The driver instance data.
2418 */
2419static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
2420{
2421 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_PAUSE);
2422}
2423
2424/**
2425 * Resume notification.
2426 *
2427 * @param pDrvIns The driver instance data.
2428 */
2429static DECLCALLBACK(void) drvAudioResume(PPDMDRVINS pDrvIns)
2430{
2431 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_RESUME);
2432}
2433
2434/**
2435 * Audio driver registration record.
2436 */
2437const PDMDRVREG g_DrvAUDIO =
2438{
2439 /* u32Version */
2440 PDM_DRVREG_VERSION,
2441 /* szName */
2442 "AUDIO",
2443 /* szRCMod */
2444 "",
2445 /* szR0Mod */
2446 "",
2447 /* pszDescription */
2448 "Audio connector driver",
2449 /* fFlags */
2450 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2451 /* fClass */
2452 PDM_DRVREG_CLASS_AUDIO,
2453 /* cMaxInstances */
2454 2,
2455 /* cbInstance */
2456 sizeof(DRVAUDIO),
2457 /* pfnConstruct */
2458 drvAudioConstruct,
2459 /* pfnDestruct */
2460 drvAudioDestruct,
2461 /* pfnRelocate */
2462 NULL,
2463 /* pfnIOCtl */
2464 NULL,
2465 /* pfnPowerOn */
2466 NULL,
2467 /* pfnReset */
2468 NULL,
2469 /* pfnSuspend */
2470 drvAudioSuspend,
2471 /* pfnResume */
2472 drvAudioResume,
2473 /* pfnAttach */
2474 NULL,
2475 /* pfnDetach */
2476 NULL,
2477 /* pfnPowerOff */
2478 drvAudioPowerOff,
2479 /* pfnSoftReset */
2480 NULL,
2481 /* u32EndVersion */
2482 PDM_DRVREG_VERSION
2483};
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