VirtualBox

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

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

Audio: Decoupled backend sinks and sources from the maximum of concurrent streams a backend can handle. Also, added some more enumeration code to the ALSA, PulseAudio and OSS backends, which later also can be used for configuration change callbacks. Some function renaming.

  • 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 59987 2016-03-11 12:03:37Z 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 NULL
1761};
1762
1763static DECLCALLBACK(int) drvAudioInit(PCFGMNODE pCfgHandle, PPDMDRVINS pDrvIns)
1764{
1765 AssertPtrReturn(pCfgHandle, VERR_INVALID_POINTER);
1766 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
1767
1768 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
1769 LogFlowFunc(("pDrvAudio=%p, pDrvIns=%p\n", pThis, pDrvIns));
1770
1771 RTListInit(&pThis->lstHstStrmIn);
1772 RTListInit(&pThis->lstHstStrmOut);
1773#ifdef VBOX_WITH_AUDIO_CALLBACKS
1774 RTListInit(&pThis->lstCBIn);
1775 RTListInit(&pThis->lstCBOut);
1776#endif
1777
1778 int rc = RTCritSectInit(&pThis->CritSect);
1779 if (RT_SUCCESS(rc))
1780 {
1781 rc = drvAudioProcessOptions(pCfgHandle, "AUDIO", audio_options);
1782 /** @todo Check for invalid options? */
1783
1784 pThis->cStreamsFreeOut = conf.fixed_out.cStreams;
1785 pThis->cStreamsFreeIn = conf.fixed_in.cStreams;
1786
1787 if (!pThis->cStreamsFreeOut)
1788 pThis->cStreamsFreeOut = 1;
1789
1790 if (!pThis->cStreamsFreeIn)
1791 pThis->cStreamsFreeIn = 1;
1792 }
1793
1794 /*
1795 * If everything went well, initialize the lower driver.
1796 */
1797 if (RT_SUCCESS(rc))
1798 rc = drvAudioHostInit(pCfgHandle, pThis);
1799
1800 LogFlowFuncLeaveRC(rc);
1801 return rc;
1802}
1803
1804static DECLCALLBACK(int) drvAudioRead(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn,
1805 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
1806{
1807 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1808 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1809
1810 AssertPtrReturn(pGstStrmIn, VERR_INVALID_POINTER);
1811 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1812 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
1813 /* pcbWritten is optional. */
1814
1815 int rc = RTCritSectEnter(&pThis->CritSect);
1816 if (RT_FAILURE(rc))
1817 return rc;
1818
1819 if (!pThis->pHostDrvAudio->pfnIsEnabled(pThis->pHostDrvAudio, PDMAUDIODIR_IN))
1820 {
1821 if (pcbRead)
1822 *pcbRead = 0;
1823
1824 return RTCritSectLeave(&pThis->CritSect);
1825 }
1826
1827 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1828 AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER);
1829
1830 AssertMsg(pGstStrmIn->pHstStrmIn->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED,
1831 ("Reading from disabled host input stream \"%s\" not possible\n", pGstStrmIn->MixBuf.pszName));
1832
1833 /*
1834 * Read from the parent buffer (that is, the guest buffer) which
1835 * should have the audio data in the format the guest needs.
1836 */
1837 uint32_t cRead;
1838 rc = AudioMixBufReadCirc(&pGstStrmIn->MixBuf, pvBuf, cbBuf, &cRead);
1839 if (RT_SUCCESS(rc))
1840 {
1841 AudioMixBufFinish(&pGstStrmIn->MixBuf, cRead);
1842
1843 if (pcbRead)
1844 *pcbRead = AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead);
1845 }
1846
1847 LogFlowFunc(("cRead=%RU32 (%RU32 bytes), rc=%Rrc\n",
1848 cRead, AUDIOMIXBUF_S2B(&pGstStrmIn->MixBuf, cRead), rc));
1849
1850 int rc2 = RTCritSectLeave(&pThis->CritSect);
1851 if (RT_SUCCESS(rc))
1852 rc = rc2;
1853
1854 return rc;
1855}
1856
1857static DECLCALLBACK(int) drvAudioEnableOut(PPDMIAUDIOCONNECTOR pInterface,
1858 PPDMAUDIOGSTSTRMOUT pGstStrmOut, bool fEnable)
1859{
1860 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1861 /* pGstStrmOut is optional. */
1862
1863 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1864
1865 int rc = VINF_SUCCESS;
1866
1867 if (pGstStrmOut)
1868 {
1869 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
1870 AssertPtr(pHstStrmOut);
1871
1872 if (fEnable)
1873 {
1874 /* Is a pending disable outstanding? Then disable first. */
1875 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE)
1876 {
1877 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1878 if (RT_SUCCESS(rc))
1879 pHstStrmOut->fStatus &= ~PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1880 }
1881
1882 if (RT_SUCCESS(rc))
1883 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_ENABLE);
1884 }
1885 else /* Disable */
1886 {
1887 if (pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_ENABLED)
1888 {
1889 uint32_t cGstStrmsActive = 0;
1890
1891 /*
1892 * Check if there are any active guest streams assigned
1893 * to this host stream which still are being marked as active.
1894 *
1895 * In that case we have to defer closing the host stream and
1896 * wait until all guest streams have been finished.
1897 */
1898 PPDMAUDIOGSTSTRMOUT pIter;
1899 RTListForEach(&pHstStrmOut->lstGstStrmOut, pIter, PDMAUDIOGSTSTRMOUT, Node)
1900 {
1901 if (pIter->State.fActive)
1902 {
1903 cGstStrmsActive++;
1904 break; /* At least one assigned & active guest stream is enough. */
1905 }
1906 }
1907
1908 /* Do we need to defer closing the host stream? */
1909 if (cGstStrmsActive >= 1)
1910 pHstStrmOut->fStatus |= PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE;
1911
1912 /* Can we close the host stream now instead of deferring it? */
1913 if (!(pHstStrmOut->fStatus & PDMAUDIOSTRMSTS_FLAG_PENDING_DISABLE))
1914 rc = drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
1915 }
1916 }
1917
1918 if (RT_SUCCESS(rc))
1919 pGstStrmOut->State.fActive = fEnable;
1920
1921 LogFlowFunc(("%s: fEnable=%RTbool, fStatus=0x%x, rc=%Rrc\n",
1922 pGstStrmOut->MixBuf.pszName, fEnable, pHstStrmOut->fStatus, rc));
1923 }
1924
1925 return rc;
1926}
1927
1928static DECLCALLBACK(int) drvAudioEnableIn(PPDMIAUDIOCONNECTOR pInterface,
1929 PPDMAUDIOGSTSTRMIN pGstStrmIn, bool fEnable)
1930{
1931 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1932 /* pGstStrmIn is optional. */
1933
1934 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1935
1936 int rc = VINF_SUCCESS;
1937
1938 if (pGstStrmIn)
1939 {
1940 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
1941 AssertPtr(pHstStrmIn);
1942
1943 LogFlowFunc(("%s: fEnable=%RTbool\n", pGstStrmIn->MixBuf.pszName, fEnable));
1944
1945 rc = drvAudioControlHstIn(pThis, pHstStrmIn,
1946 fEnable ? PDMAUDIOSTREAMCMD_ENABLE : PDMAUDIOSTREAMCMD_DISABLE);
1947 if (RT_SUCCESS(rc))
1948 pGstStrmIn->State.fActive = fEnable;
1949
1950 LogFlowFunc(("%s: fEnable=%RTbool, rc=%Rrc\n", pGstStrmIn->MixBuf.pszName, fEnable, rc));
1951 }
1952
1953 return rc;
1954}
1955
1956static DECLCALLBACK(bool) drvAudioIsValidIn(PPDMIAUDIOCONNECTOR pInterface,
1957 PPDMAUDIOGSTSTRMIN pGstStrmIn)
1958{
1959 return (pGstStrmIn != NULL);
1960}
1961
1962static DECLCALLBACK(bool) drvAudioIsValidOut(PPDMIAUDIOCONNECTOR pInterface,
1963 PPDMAUDIOGSTSTRMOUT pGstStrmOut)
1964{
1965 return (pGstStrmOut != NULL);
1966}
1967
1968static DECLCALLBACK(int) drvAudioCreateIn(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
1969 PDMAUDIORECSOURCE enmRecSource, PPDMAUDIOSTREAMCFG pCfg,
1970 PPDMAUDIOGSTSTRMIN *ppGstStrmIn)
1971{
1972 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
1973 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1974 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1975 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
1976 AssertPtrReturn(ppGstStrmIn, VERR_INVALID_POINTER);
1977
1978 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
1979
1980 int rc = RTCritSectEnter(&pThis->CritSect);
1981 if (RT_FAILURE(rc))
1982 return rc;
1983
1984 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
1985
1986 if (!drvAudioStreamCfgIsValid(pCfg))
1987 {
1988 LogFunc(("Input stream configuration is not valid, bailing out\n"));
1989 rc = VERR_INVALID_PARAMETER;
1990 }
1991
1992 PPDMAUDIOGSTSTRMIN pGstStrmIn = *ppGstStrmIn;
1993 if ( RT_SUCCESS(rc)
1994 && pGstStrmIn
1995 && drvAudioPCMPropsAreEqual(&pGstStrmIn->Props, pCfg))
1996 {
1997 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
1998 pGstStrmIn->MixBuf.pszName));
1999 rc = VWRN_ALREADY_EXISTS;
2000 }
2001
2002 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
2003 {
2004 int rc2 = RTCritSectLeave(&pThis->CritSect);
2005 AssertRC(rc2);
2006
2007 return rc;
2008 }
2009
2010 if ( !conf.fixed_in.enabled
2011 && pGstStrmIn)
2012 {
2013 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2014 pGstStrmIn = NULL;
2015 }
2016
2017 if (pGstStrmIn)
2018 {
2019 PPDMAUDIOHSTSTRMIN pHstStrmIn = pGstStrmIn->pHstStrmIn;
2020 AssertPtr(pHstStrmIn);
2021
2022 drvAudioGstInFreeRes(pGstStrmIn);
2023
2024 char *pszTemp;
2025 if (RTStrAPrintf(&pszTemp, "%s (Guest)", pszName) <= 0)
2026 {
2027 RTMemFree(pGstStrmIn);
2028
2029 int rc2 = RTCritSectLeave(&pThis->CritSect);
2030 AssertRC(rc2);
2031
2032 return VERR_NO_MEMORY;
2033 }
2034
2035 rc = drvAudioGstInInit(pGstStrmIn, pHstStrmIn, pszName, pCfg);
2036
2037 RTStrFree(pszTemp);
2038 }
2039 else
2040 rc = drvAudioCreateStreamPairIn(pThis, pszName, enmRecSource, pCfg, &pGstStrmIn);
2041
2042 if (RT_SUCCESS(rc))
2043 {
2044 if (pGstStrmIn)
2045 *ppGstStrmIn = pGstStrmIn;
2046 }
2047
2048 int rc2 = RTCritSectLeave(&pThis->CritSect);
2049 if (RT_SUCCESS(rc))
2050 rc = rc2;
2051
2052 LogFlowFuncLeaveRC(rc);
2053 return rc;
2054}
2055
2056static DECLCALLBACK(int) drvAudioCreateOut(PPDMIAUDIOCONNECTOR pInterface, const char *pszName,
2057 PPDMAUDIOSTREAMCFG pCfg, PPDMAUDIOGSTSTRMOUT *ppGstStrmOut)
2058{
2059 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2060 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
2061 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2062 AssertPtrReturn(ppGstStrmOut, VERR_INVALID_POINTER);
2063
2064 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2065
2066 int rc = RTCritSectEnter(&pThis->CritSect);
2067 if (RT_FAILURE(rc))
2068 return rc;
2069
2070 LogFlowFunc(("pszName=%s, pCfg=%p\n", pszName, pCfg));
2071
2072 if (!drvAudioStreamCfgIsValid(pCfg))
2073 {
2074 LogFunc(("Output stream configuration is not valid, bailing out\n"));
2075 rc = VERR_INVALID_PARAMETER;
2076 }
2077
2078 PPDMAUDIOGSTSTRMOUT pGstStrmOut = *ppGstStrmOut;
2079 if ( RT_SUCCESS(rc)
2080 && pGstStrmOut
2081 && drvAudioPCMPropsAreEqual(&pGstStrmOut->Props, pCfg))
2082 {
2083 LogFunc(("[%s] Exists and matches required configuration, skipping creation\n",
2084 pGstStrmOut->MixBuf.pszName));
2085
2086 rc = VWRN_ALREADY_EXISTS;
2087 }
2088
2089 if (rc != VINF_SUCCESS) /* Note: Can be VWRN_ALREADY_EXISTS, so don't use VINF_SUCCESS here. */
2090 {
2091 int rc2 = RTCritSectLeave(&pThis->CritSect);
2092 AssertRC(rc2);
2093
2094 return rc;
2095 }
2096
2097#if 0
2098 /* Any live samples that need to be updated after
2099 * we set the new parameters? */
2100 PPDMAUDIOGSTSTRMOUT pOldGstStrmOut = NULL;
2101 uint32_t cLiveSamples = 0;
2102
2103 if ( conf.plive
2104 && pGstStrmOut
2105 && ( !pGstStrmOut->State.fActive
2106 && !pGstStrmOut->State.fEmpty))
2107 {
2108 cLiveSamples = pGstStrmOut->cTotalSamplesWritten;
2109 if (cLiveSamples)
2110 {
2111 pOldGstStrmOut = pGstStrmOut;
2112 pGstStrmOut = NULL;
2113 }
2114 }
2115#endif
2116
2117 if ( pGstStrmOut
2118 && !conf.fixed_out.enabled)
2119 {
2120 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2121 pGstStrmOut = NULL;
2122 }
2123
2124 if (pGstStrmOut)
2125 {
2126 PPDMAUDIOHSTSTRMOUT pHstStrmOut = pGstStrmOut->pHstStrmOut;
2127 AssertPtr(pHstStrmOut);
2128
2129 drvAudioGstOutFreeRes(pGstStrmOut);
2130
2131 rc = drvAudioGstOutInit(pGstStrmOut, pHstStrmOut, pszName, pCfg);
2132 }
2133 else
2134 {
2135 rc = drvAudioCreateStreamPairOut(pThis, pszName, pCfg, &pGstStrmOut);
2136 if (RT_FAILURE(rc))
2137 LogFunc(("Failed to create output stream \"%s\", rc=%Rrc\n", pszName, rc));
2138 }
2139
2140 if (RT_SUCCESS(rc))
2141 {
2142 if (pGstStrmOut)
2143 *ppGstStrmOut = pGstStrmOut;
2144#if 0
2145 /* Update remaining live samples with new rate. */
2146 if (cLiveSamples)
2147 {
2148 AssertPtr(pOldGstStrmOut);
2149
2150 uint32_t cSamplesMixed =
2151 (cLiveSamples << pOldGstStrmOut->Props.cShift)
2152 * pOldGstStrmOut->Props.cbPerSec
2153 / (*ppGstStrmOut)->Props.cbPerSec;
2154
2155 pGstStrmOut->cTotalSamplesWritten += cSamplesMixed;
2156 }
2157#endif
2158 }
2159
2160 int rc2 = RTCritSectLeave(&pThis->CritSect);
2161 if (RT_SUCCESS(rc))
2162 rc = rc2;
2163
2164 LogFlowFuncLeaveRC(rc);
2165 return rc;
2166}
2167
2168static DECLCALLBACK(int) drvAudioGetConfiguration(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOBACKENDCFG pCfg)
2169{
2170 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2171 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
2172
2173 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2174
2175 int rc = RTCritSectEnter(&pThis->CritSect);
2176 if (RT_FAILURE(rc))
2177 return rc;
2178
2179 rc = pThis->pHostDrvAudio->pfnGetConf(pThis->pHostDrvAudio, pCfg);
2180
2181 int rc2 = RTCritSectLeave(&pThis->CritSect);
2182 if (RT_SUCCESS(rc))
2183 rc = rc2;
2184
2185 LogFlowFuncLeaveRC(rc);
2186 return rc;
2187}
2188
2189static DECLCALLBACK(bool) drvAudioIsActiveIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2190{
2191 AssertPtrReturn(pInterface, false);
2192 /* pGstStrmIn is optional. */
2193
2194 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2195
2196 int rc2 = RTCritSectEnter(&pThis->CritSect);
2197 AssertRC(rc2);
2198
2199 bool fRet = pGstStrmIn ? pGstStrmIn->State.fActive : false;
2200
2201 rc2 = RTCritSectLeave(&pThis->CritSect);
2202 AssertRC(rc2);
2203
2204 return fRet;
2205}
2206
2207static DECLCALLBACK(bool) drvAudioIsActiveOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2208{
2209 AssertPtrReturn(pInterface, false);
2210 /* pGstStrmOut is optional. */
2211
2212 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2213
2214 int rc2 = RTCritSectEnter(&pThis->CritSect);
2215 AssertRC(rc2);
2216
2217 bool fRet = pGstStrmOut ? pGstStrmOut->State.fActive : false;
2218
2219 rc2 = RTCritSectLeave(&pThis->CritSect);
2220 AssertRC(rc2);
2221
2222 return fRet;
2223}
2224
2225static DECLCALLBACK(void) drvAudioDestroyIn(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMIN pGstStrmIn)
2226{
2227 AssertPtrReturnVoid(pInterface);
2228 /* pGstStrmIn is optional. */
2229
2230 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2231
2232 int rc2 = RTCritSectEnter(&pThis->CritSect);
2233 AssertRC(rc2);
2234
2235 if (pGstStrmIn)
2236 drvAudioDestroyGstIn(pThis, pGstStrmIn);
2237
2238 rc2 = RTCritSectLeave(&pThis->CritSect);
2239 AssertRC(rc2);
2240}
2241
2242static DECLCALLBACK(void) drvAudioDestroyOut(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOGSTSTRMOUT pGstStrmOut)
2243{
2244 AssertPtrReturnVoid(pInterface);
2245 /* pGstStrmOut is optional. */
2246
2247 PDRVAUDIO pThis = PDMIAUDIOCONNECTOR_2_DRVAUDIO(pInterface);
2248
2249 int rc2 = RTCritSectEnter(&pThis->CritSect);
2250 AssertRC(rc2);
2251
2252 if (pGstStrmOut)
2253 drvAudioDestroyGstOut(pThis, pGstStrmOut);
2254
2255 rc2 = RTCritSectLeave(&pThis->CritSect);
2256 AssertRC(rc2);
2257}
2258
2259/********************************************************************/
2260
2261/**
2262 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
2263 */
2264static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2265{
2266 LogFlowFunc(("pInterface=%p, pszIID=%s\n", pInterface, pszIID));
2267
2268 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2269 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2270
2271 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
2272 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIAUDIOCONNECTOR, &pThis->IAudioConnector);
2273
2274 return NULL;
2275}
2276
2277/**
2278 * Power Off notification.
2279 *
2280 * @param pDrvIns The driver instance data.
2281 */
2282static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
2283{
2284 LogFlowFuncEnter();
2285 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2286
2287 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2288
2289 if (!pThis->pHostDrvAudio)
2290 return;
2291
2292 /* Tear down all host output streams. */
2293 PPDMAUDIOHSTSTRMOUT pHstStrmOut = NULL;
2294 while ((pHstStrmOut = drvAudioFindAnyHstOut(pThis, pHstStrmOut)))
2295 {
2296 drvAudioControlHstOut(pThis, pHstStrmOut, PDMAUDIOSTREAMCMD_DISABLE);
2297 pThis->pHostDrvAudio->pfnFiniOut(pThis->pHostDrvAudio, pHstStrmOut);
2298 }
2299
2300 /* Tear down all host input streams. */
2301 PPDMAUDIOHSTSTRMIN pHstStrmIn = NULL;
2302 while ((pHstStrmIn = drvAudioFindNextHstIn(pThis, pHstStrmIn)))
2303 {
2304 drvAudioControlHstIn(pThis, pHstStrmIn, PDMAUDIOSTREAMCMD_DISABLE);
2305 pThis->pHostDrvAudio->pfnFiniIn(pThis->pHostDrvAudio, pHstStrmIn);
2306 }
2307
2308 if (pThis->pHostDrvAudio->pfnShutdown)
2309 pThis->pHostDrvAudio->pfnShutdown(pThis->pHostDrvAudio);
2310
2311#ifdef VBOX_WITH_AUDIO_CALLBACKS
2312 PPDMAUDIOCALLBACK pCB, pCBNext;
2313 RTListForEachSafe(&pThis->lstCBIn, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2314 drvAudioCallbackDestroy(pCB);
2315
2316 RTListForEachSafe(&pThis->lstCBOut, pCB, pCBNext, PDMAUDIOCALLBACK, Node)
2317 drvAudioCallbackDestroy(pCB);
2318#endif
2319
2320 LogFlowFuncLeave();
2321}
2322
2323/**
2324 * Constructs an audio driver instance.
2325 *
2326 * @copydoc FNPDMDRVCONSTRUCT
2327 */
2328static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
2329{
2330 LogFlowFunc(("pDrvIns=%#p, pCfgHandle=%#p, fFlags=%x\n", pDrvIns, pCfgHandle, fFlags));
2331
2332 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2333 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
2334
2335 /*
2336 * Init the static parts.
2337 */
2338 pThis->pDrvIns = pDrvIns;
2339 /* IBase. */
2340 pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
2341 /* IAudioConnector. */
2342 pThis->IAudioConnector.pfnQueryStatus = drvAudioQueryStatus;
2343 pThis->IAudioConnector.pfnRead = drvAudioRead;
2344 pThis->IAudioConnector.pfnWrite = drvAudioWrite;
2345 pThis->IAudioConnector.pfnGetConfiguration = drvAudioGetConfiguration;
2346 pThis->IAudioConnector.pfnIsActiveIn = drvAudioIsActiveIn;
2347 pThis->IAudioConnector.pfnIsActiveOut = drvAudioIsActiveOut;
2348 pThis->IAudioConnector.pfnIsValidIn = drvAudioIsValidIn;
2349 pThis->IAudioConnector.pfnIsValidOut = drvAudioIsValidOut;
2350 pThis->IAudioConnector.pfnEnableOut = drvAudioEnableOut;
2351 pThis->IAudioConnector.pfnEnableIn = drvAudioEnableIn;
2352 pThis->IAudioConnector.pfnDestroyIn = drvAudioDestroyIn;
2353 pThis->IAudioConnector.pfnDestroyOut = drvAudioDestroyOut;
2354 pThis->IAudioConnector.pfnCreateIn = drvAudioCreateIn;
2355 pThis->IAudioConnector.pfnCreateOut = drvAudioCreateOut;
2356 pThis->IAudioConnector.pfnPlayOut = drvAudioPlayOut;
2357#ifdef VBOX_WITH_AUDIO_CALLBACKS
2358 pThis->IAudioConnector.pfnRegisterCallbacks = drvAudioRegisterCallbacks;
2359 pThis->IAudioConnector.pfnCallback = drvAudioCallback;
2360#endif
2361
2362 /*
2363 * Attach driver below and query its connector interface.
2364 */
2365 PPDMIBASE pDownBase;
2366 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
2367 if (RT_FAILURE(rc))
2368 {
2369 LogRel(("Audio: Failed to attach to driver %p below (flags=0x%x), rc=%Rrc\n",
2370 pDrvIns, fFlags, rc));
2371 return rc;
2372 }
2373
2374 pThis->pHostDrvAudio = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIHOSTAUDIO);
2375 if (!pThis->pHostDrvAudio)
2376 {
2377 LogRel(("Audio: Failed to query interface for underlying host driver\n"));
2378 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
2379 N_("Host audio backend missing or invalid"));
2380 }
2381
2382#ifdef DEBUG_andy
2383 CFGMR3Dump(pCfgHandle);
2384#endif
2385
2386 rc = drvAudioInit(pCfgHandle, pDrvIns);
2387 if (RT_SUCCESS(rc))
2388 {
2389 pThis->fTerminate = false;
2390 pThis->pDrvIns = pDrvIns;
2391 }
2392
2393 LogFlowFuncLeaveRC(rc);
2394 return rc;
2395}
2396
2397/**
2398 * Destructs an audio driver instance.
2399 *
2400 * @copydoc FNPDMDRVDESTRUCT
2401 */
2402static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
2403{
2404 LogFlowFuncEnter();
2405
2406 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2407 PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
2408
2409 if (RTCritSectIsInitialized(&pThis->CritSect))
2410 {
2411 int rc2 = RTCritSectDelete(&pThis->CritSect);
2412 AssertRC(rc2);
2413 }
2414}
2415
2416/**
2417 * Suspend notification.
2418 *
2419 * @param pDrvIns The driver instance data.
2420 */
2421static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
2422{
2423 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_PAUSE);
2424}
2425
2426/**
2427 * Resume notification.
2428 *
2429 * @param pDrvIns The driver instance data.
2430 */
2431static DECLCALLBACK(void) drvAudioResume(PPDMDRVINS pDrvIns)
2432{
2433 drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_RESUME);
2434}
2435
2436/**
2437 * Audio driver registration record.
2438 */
2439const PDMDRVREG g_DrvAUDIO =
2440{
2441 /* u32Version */
2442 PDM_DRVREG_VERSION,
2443 /* szName */
2444 "AUDIO",
2445 /* szRCMod */
2446 "",
2447 /* szR0Mod */
2448 "",
2449 /* pszDescription */
2450 "Audio connector driver",
2451 /* fFlags */
2452 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2453 /* fClass */
2454 PDM_DRVREG_CLASS_AUDIO,
2455 /* cMaxInstances */
2456 2,
2457 /* cbInstance */
2458 sizeof(DRVAUDIO),
2459 /* pfnConstruct */
2460 drvAudioConstruct,
2461 /* pfnDestruct */
2462 drvAudioDestruct,
2463 /* pfnRelocate */
2464 NULL,
2465 /* pfnIOCtl */
2466 NULL,
2467 /* pfnPowerOn */
2468 NULL,
2469 /* pfnReset */
2470 NULL,
2471 /* pfnSuspend */
2472 drvAudioSuspend,
2473 /* pfnResume */
2474 drvAudioResume,
2475 /* pfnAttach */
2476 NULL,
2477 /* pfnDetach */
2478 NULL,
2479 /* pfnPowerOff */
2480 drvAudioPowerOff,
2481 /* pfnSoftReset */
2482 NULL,
2483 /* u32EndVersion */
2484 PDM_DRVREG_VERSION
2485};
Note: See TracBrowser for help on using the repository browser.

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