VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/vkatDriverStack.cpp@ 89545

Last change on this file since 89545 was 89535, checked in by vboxsync, 4 years ago

ValKit/AudioTest: Need the pfnSTAMRegister driver helper now. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.5 KB
Line 
1/* $Id: vkatDriverStack.cpp 89535 2021-06-07 00:56:57Z vboxsync $ */
2/** @file
3 * Validation Kit Audio Test (VKAT) - Driver stack code.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/errcore.h>
32#include <iprt/message.h>
33#include <iprt/stream.h>
34#include <iprt/string.h>
35#include <iprt/uuid.h>
36#include <iprt/test.h>
37
38
39/**
40 * Internal driver instance data
41 * @note This must be put here as it's needed before pdmdrv.h is included.
42 */
43typedef struct PDMDRVINSINT
44{
45 /** The stack the drive belongs to. */
46 struct AUDIOTESTDRVSTACK *pStack;
47} PDMDRVINSINT;
48#define PDMDRVINSINT_DECLARED
49
50#include "vkatInternal.h"
51#include "VBoxDD.h"
52
53
54
55/*********************************************************************************************************************************
56* Fake PDM Driver Handling. *
57*********************************************************************************************************************************/
58
59/** @name Driver Fakes/Stubs
60 *
61 * @note The VMM functions defined here will turn into driver helpers before
62 * long, as the drivers aren't supposed to import directly from the VMM in
63 * the future.
64 *
65 * @{ */
66
67VMMR3DECL(PCFGMNODE) CFGMR3GetChild(PCFGMNODE pNode, const char *pszPath)
68{
69 RT_NOREF(pNode, pszPath);
70 return NULL;
71}
72
73
74VMMR3DECL(int) CFGMR3QueryString(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString)
75{
76 if (pNode != NULL)
77 {
78 PCPDMDRVREG pDrvReg = (PCPDMDRVREG)pNode;
79 if (g_uVerbosity > 2)
80 RTPrintf("debug: CFGMR3QueryString([%s], %s, %p, %#x)\n", pDrvReg->szName, pszName, pszString, cchString);
81
82 if ( ( strcmp(pDrvReg->szName, "PulseAudio") == 0
83 || strcmp(pDrvReg->szName, "HostAudioWas") == 0)
84 && strcmp(pszName, "VmName") == 0)
85 return RTStrCopy(pszString, cchString, "vkat");
86
87 if ( strcmp(pDrvReg->szName, "HostAudioWas") == 0
88 && strcmp(pszName, "VmUuid") == 0)
89 return RTStrCopy(pszString, cchString, "794c9192-d045-4f28-91ed-46253ac9998e");
90 }
91 else if (g_uVerbosity > 2)
92 RTPrintf("debug: CFGMR3QueryString(%p, %s, %p, %#x)\n", pNode, pszName, pszString, cchString);
93
94 return VERR_CFGM_VALUE_NOT_FOUND;
95}
96
97
98VMMR3DECL(int) CFGMR3QueryStringAlloc(PCFGMNODE pNode, const char *pszName, char **ppszString)
99{
100 char szStr[128];
101 int rc = CFGMR3QueryString(pNode, pszName, szStr, sizeof(szStr));
102 if (RT_SUCCESS(rc))
103 *ppszString = RTStrDup(szStr);
104
105 return rc;
106}
107
108
109VMMR3DECL(void) MMR3HeapFree(void *pv)
110{
111 /* counterpart to CFGMR3QueryStringAlloc */
112 RTStrFree((char *)pv);
113}
114
115
116VMMR3DECL(int) CFGMR3QueryStringDef(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
117{
118 PCPDMDRVREG pDrvReg = (PCPDMDRVREG)pNode;
119 if (RT_VALID_PTR(pDrvReg))
120 {
121 const char *pszRet = pszDef;
122 if ( g_pszDrvAudioDebug
123 && strcmp(pDrvReg->szName, "AUDIO") == 0
124 && strcmp(pszName, "DebugPathOut") == 0)
125 pszRet = g_pszDrvAudioDebug;
126
127 int rc = RTStrCopy(pszString, cchString, pszRet);
128
129 if (g_uVerbosity > 2)
130 RTPrintf("debug: CFGMR3QueryStringDef([%s], %s, %p, %#x, %s) -> '%s' + %Rrc\n",
131 pDrvReg->szName, pszName, pszString, cchString, pszDef, pszRet, rc);
132 return rc;
133 }
134
135 if (g_uVerbosity > 2)
136 RTPrintf("debug: CFGMR3QueryStringDef(%p, %s, %p, %#x, %s)\n", pNode, pszName, pszString, cchString, pszDef);
137 return RTStrCopy(pszString, cchString, pszDef);
138}
139
140
141VMMR3DECL(int) CFGMR3QueryBoolDef(PCFGMNODE pNode, const char *pszName, bool *pf, bool fDef)
142{
143 PCPDMDRVREG pDrvReg = (PCPDMDRVREG)pNode;
144 if (RT_VALID_PTR(pDrvReg))
145 {
146 *pf = fDef;
147 if ( strcmp(pDrvReg->szName, "AUDIO") == 0
148 && strcmp(pszName, "DebugEnabled") == 0)
149 *pf = g_fDrvAudioDebug;
150
151 if (g_uVerbosity > 2)
152 RTPrintf("debug: CFGMR3QueryBoolDef([%s], %s, %p, %RTbool) -> %RTbool\n", pDrvReg->szName, pszName, pf, fDef, *pf);
153 return VINF_SUCCESS;
154 }
155 *pf = fDef;
156 return VINF_SUCCESS;
157}
158
159
160VMMR3DECL(int) CFGMR3QueryU8(PCFGMNODE pNode, const char *pszName, uint8_t *pu8)
161{
162 RT_NOREF(pNode, pszName, pu8);
163 return VERR_CFGM_VALUE_NOT_FOUND;
164}
165
166
167VMMR3DECL(int) CFGMR3QueryU32(PCFGMNODE pNode, const char *pszName, uint32_t *pu32)
168{
169 RT_NOREF(pNode, pszName, pu32);
170 return VERR_CFGM_VALUE_NOT_FOUND;
171}
172
173
174VMMR3DECL(int) CFGMR3ValidateConfig(PCFGMNODE pNode, const char *pszNode,
175 const char *pszValidValues, const char *pszValidNodes,
176 const char *pszWho, uint32_t uInstance)
177{
178 RT_NOREF(pNode, pszNode, pszValidValues, pszValidNodes, pszWho, uInstance);
179 return VINF_SUCCESS;
180}
181
182/** @} */
183
184/** @name Driver Helper Fakes
185 * @{ */
186
187static DECLCALLBACK(int) audioTestDrvHlp_Attach(PPDMDRVINS pDrvIns, uint32_t fFlags, PPDMIBASE *ppBaseInterface)
188{
189 /* DrvAudio must be allowed to attach the backend driver (paranoid
190 backend drivers may call us to check that nothing is attached). */
191 if (strcmp(pDrvIns->pReg->szName, "AUDIO") == 0)
192 {
193 PAUDIOTESTDRVSTACK pDrvStack = pDrvIns->Internal.s.pStack;
194 AssertReturn(pDrvStack->pDrvBackendIns == NULL, VERR_PDM_DRIVER_ALREADY_ATTACHED);
195
196 if (g_uVerbosity > 1)
197 RTMsgInfo("Attaching backend '%s' to DrvAudio...\n", pDrvStack->pDrvReg->szName);
198 int rc = audioTestDrvConstruct(pDrvStack, pDrvStack->pDrvReg, pDrvIns, &pDrvStack->pDrvBackendIns);
199 if (RT_SUCCESS(rc))
200 {
201 if (ppBaseInterface)
202 *ppBaseInterface = &pDrvStack->pDrvBackendIns->IBase;
203 }
204 else
205 RTMsgError("Failed to attach backend: %Rrc", rc);
206 return rc;
207 }
208 RT_NOREF(fFlags);
209 return VERR_PDM_NO_ATTACHED_DRIVER;
210}
211
212
213static DECLCALLBACK(void) audioTestDrvHlp_STAMRegister(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType, const char *pszName,
214 STAMUNIT enmUnit, const char *pszDesc)
215{
216 RT_NOREF(pDrvIns, pvSample, enmType, pszName, enmUnit, pszDesc);
217}
218
219
220static DECLCALLBACK(void) audioTestDrvHlp_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
221 STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
222 const char *pszName, ...)
223{
224 RT_NOREF(pDrvIns, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName);
225}
226
227
228static DECLCALLBACK(void) audioTestDrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
229 STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
230 const char *pszName, va_list args)
231{
232 RT_NOREF(pDrvIns, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
233}
234
235
236static DECLCALLBACK(int) audioTestDrvHlp_STAMDeregister(PPDMDRVINS pDrvIns, void *pvSample)
237{
238 RT_NOREF(pDrvIns, pvSample);
239 return VINF_SUCCESS;
240}
241
242
243static DECLCALLBACK(int) audioTestDrvHlp_STAMDeregisterByPrefix(PPDMDRVINS pDrvIns, const char *pszPrefix)
244{
245 RT_NOREF(pDrvIns, pszPrefix);
246 return VINF_SUCCESS;
247}
248
249/**
250 * Get the driver helpers.
251 */
252static const PDMDRVHLPR3 *audioTestFakeGetDrvHlp(void)
253{
254 /*
255 * Note! No initializer for s_DrvHlp (also why it's not a file global).
256 * We do not want to have to update this code every time PDMDRVHLPR3
257 * grows new entries or are otherwise modified. Only when the
258 * entries used by the audio driver changes do we want to change
259 * our code.
260 */
261 static PDMDRVHLPR3 s_DrvHlp;
262 if (s_DrvHlp.u32Version != PDM_DRVHLPR3_VERSION)
263 {
264 s_DrvHlp.u32Version = PDM_DRVHLPR3_VERSION;
265 s_DrvHlp.u32TheEnd = PDM_DRVHLPR3_VERSION;
266 s_DrvHlp.pfnAttach = audioTestDrvHlp_Attach;
267 s_DrvHlp.pfnSTAMRegister = audioTestDrvHlp_STAMRegister;
268 s_DrvHlp.pfnSTAMRegisterF = audioTestDrvHlp_STAMRegisterF;
269 s_DrvHlp.pfnSTAMRegisterV = audioTestDrvHlp_STAMRegisterV;
270 s_DrvHlp.pfnSTAMDeregister = audioTestDrvHlp_STAMDeregister;
271 s_DrvHlp.pfnSTAMDeregisterByPrefix = audioTestDrvHlp_STAMDeregisterByPrefix;
272 }
273 return &s_DrvHlp;
274}
275
276/** @} */
277
278
279/**
280 * Implementation of PDMIBASE::pfnQueryInterface for a fake device above
281 * DrvAudio.
282 */
283static DECLCALLBACK(void *) audioTestFakeDeviceIBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
284{
285 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, pInterface);
286 RTMsgWarning("audioTestFakeDeviceIBaseQueryInterface: Unknown interface: %s\n", pszIID);
287 return NULL;
288}
289
290/** IBase interface for a fake device above DrvAudio. */
291static PDMIBASE g_AudioTestFakeDeviceIBase = { audioTestFakeDeviceIBaseQueryInterface };
292
293
294static DECLCALLBACK(int) audioTestIHostAudioPort_DoOnWorkerThread(PPDMIHOSTAUDIOPORT pInterface, PPDMAUDIOBACKENDSTREAM pStream,
295 uintptr_t uUser, void *pvUser)
296{
297 RT_NOREF(pInterface, pStream, uUser, pvUser);
298 RTMsgWarning("audioTestIHostAudioPort_DoOnWorkerThread was called\n");
299 return VERR_NOT_IMPLEMENTED;
300}
301
302
303DECLCALLBACK(void) audioTestIHostAudioPort_NotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface, PDMAUDIODIR enmDir, void *pvUser)
304{
305 RT_NOREF(pInterface, enmDir, pvUser);
306 RTMsgWarning("audioTestIHostAudioPort_NotifyDeviceChanged was called\n");
307}
308
309
310static DECLCALLBACK(void) audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch(PPDMIHOSTAUDIOPORT pInterface,
311 PPDMAUDIOBACKENDSTREAM pStream)
312{
313 RT_NOREF(pInterface, pStream);
314 RTMsgWarning("audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch was called\n");
315}
316
317
318static DECLCALLBACK(void) audioTestIHostAudioPort_StreamNotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface,
319 PPDMAUDIOBACKENDSTREAM pStream, bool fReInit)
320{
321 RT_NOREF(pInterface, pStream, fReInit);
322 RTMsgWarning("audioTestIHostAudioPort_StreamNotifyDeviceChanged was called\n");
323}
324
325
326static DECLCALLBACK(void) audioTestIHostAudioPort_NotifyDevicesChanged(PPDMIHOSTAUDIOPORT pInterface)
327{
328 RT_NOREF(pInterface);
329 RTMsgWarning("audioTestIHostAudioPort_NotifyDevicesChanged was called\n");
330}
331
332
333static PDMIHOSTAUDIOPORT g_AudioTestIHostAudioPort =
334{
335 audioTestIHostAudioPort_DoOnWorkerThread,
336 audioTestIHostAudioPort_NotifyDeviceChanged,
337 audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch,
338 audioTestIHostAudioPort_StreamNotifyDeviceChanged,
339 audioTestIHostAudioPort_NotifyDevicesChanged,
340};
341
342
343/**
344 * Implementation of PDMIBASE::pfnQueryInterface for a fake DrvAudio above a
345 * backend.
346 */
347static DECLCALLBACK(void *) audioTestFakeDrvAudioIBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
348{
349 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, pInterface);
350 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIOPORT, &g_AudioTestIHostAudioPort);
351 RTMsgWarning("audioTestFakeDrvAudioIBaseQueryInterface: Unknown interface: %s\n", pszIID);
352 return NULL;
353}
354
355
356/** IBase interface for a fake DrvAudio above a lonesome backend. */
357static PDMIBASE g_AudioTestFakeDrvAudioIBase = { audioTestFakeDrvAudioIBaseQueryInterface };
358
359
360
361/**
362 * Constructs a PDM audio driver instance.
363 *
364 * @returns VBox status code.
365 * @param pDrvStack The stack this is associated with.
366 * @param pDrvReg PDM driver registration record to use for construction.
367 * @param pParentDrvIns The parent driver (if any).
368 * @param ppDrvIns Where to return the driver instance structure.
369 */
370int audioTestDrvConstruct(PAUDIOTESTDRVSTACK pDrvStack, PCPDMDRVREG pDrvReg, PPDMDRVINS pParentDrvIns,
371 PPPDMDRVINS ppDrvIns)
372{
373 /* The destruct function must have valid data to work with. */
374 *ppDrvIns = NULL;
375
376 /*
377 * Check registration structure validation (doesn't need to be too
378 * thorough, PDM check it in detail on every VM startup).
379 */
380 AssertPtrReturn(pDrvReg, VERR_INVALID_POINTER);
381 RTMsgInfo("Initializing backend '%s' ...\n", pDrvReg->szName);
382 AssertPtrReturn(pDrvReg->pfnConstruct, VERR_INVALID_PARAMETER);
383
384 /*
385 * Create the instance data structure.
386 */
387 PPDMDRVINS pDrvIns = (PPDMDRVINS)RTMemAllocZVar(RT_UOFFSETOF_DYN(PDMDRVINS, achInstanceData[pDrvReg->cbInstance]));
388 RTTEST_CHECK_RET(g_hTest, pDrvIns, VERR_NO_MEMORY);
389
390 pDrvIns->u32Version = PDM_DRVINS_VERSION;
391 pDrvIns->iInstance = 0;
392 pDrvIns->pHlpR3 = audioTestFakeGetDrvHlp();
393 pDrvIns->pvInstanceDataR3 = &pDrvIns->achInstanceData[0];
394 pDrvIns->pReg = pDrvReg;
395 pDrvIns->pCfg = (PCFGMNODE)pDrvReg;
396 pDrvIns->Internal.s.pStack = pDrvStack;
397 pDrvIns->pUpBase = NULL;
398 pDrvIns->pDownBase = NULL;
399 if (pParentDrvIns)
400 {
401 Assert(pParentDrvIns->pDownBase == NULL);
402 pParentDrvIns->pDownBase = &pDrvIns->IBase;
403 pDrvIns->pUpBase = &pParentDrvIns->IBase;
404 }
405 else if (strcmp(pDrvReg->szName, "AUDIO") == 0)
406 pDrvIns->pUpBase = &g_AudioTestFakeDeviceIBase;
407 else
408 pDrvIns->pUpBase = &g_AudioTestFakeDrvAudioIBase;
409
410 /*
411 * Invoke the constructor.
412 */
413 int rc = pDrvReg->pfnConstruct(pDrvIns, pDrvIns->pCfg, 0 /*fFlags*/);
414 if (RT_SUCCESS(rc))
415 {
416 *ppDrvIns = pDrvIns;
417 return VINF_SUCCESS;
418 }
419
420 RTTestFailed(g_hTest, "Failed to construct audio driver '%s': %Rrc", pDrvReg->szName, rc);
421 if (pDrvReg->pfnDestruct)
422 pDrvReg->pfnDestruct(pDrvIns);
423 RTMemFree(pDrvIns);
424 return rc;
425}
426
427
428/**
429 * Destructs a PDM audio driver instance.
430 *
431 * @param pDrvIns Driver instance to destruct.
432 */
433static void audioTestDrvDestruct(PPDMDRVINS pDrvIns)
434{
435 if (pDrvIns)
436 {
437 Assert(pDrvIns->u32Version == PDM_DRVINS_VERSION);
438
439 if (pDrvIns->pReg->pfnDestruct)
440 pDrvIns->pReg->pfnDestruct(pDrvIns);
441
442 pDrvIns->u32Version = 0;
443 pDrvIns->pReg = NULL;
444 RTMemFree(pDrvIns);
445 }
446}
447
448
449/**
450 * Sends the PDM driver a power off notification.
451 *
452 * @param pDrvIns Driver instance to notify.
453 */
454static void audioTestDrvNotifyPowerOff(PPDMDRVINS pDrvIns)
455{
456 if (pDrvIns)
457 {
458 Assert(pDrvIns->u32Version == PDM_DRVINS_VERSION);
459 if (pDrvIns->pReg->pfnPowerOff)
460 pDrvIns->pReg->pfnPowerOff(pDrvIns);
461 }
462}
463
464
465/**
466 * Deletes a driver stack.
467 *
468 * This will power off and destroy the drivers.
469 */
470void audioTestDriverStackDelete(PAUDIOTESTDRVSTACK pDrvStack)
471{
472 /*
473 * Do power off notifications (top to bottom).
474 */
475 audioTestDrvNotifyPowerOff(pDrvStack->pDrvAudioIns);
476 audioTestDrvNotifyPowerOff(pDrvStack->pDrvBackendIns);
477
478 /*
479 * Drivers are destroyed from bottom to top (closest to the device).
480 */
481 audioTestDrvDestruct(pDrvStack->pDrvBackendIns);
482 pDrvStack->pDrvBackendIns = NULL;
483 pDrvStack->pIHostAudio = NULL;
484
485 audioTestDrvDestruct(pDrvStack->pDrvAudioIns);
486 pDrvStack->pDrvAudioIns = NULL;
487 pDrvStack->pIAudioConnector = NULL;
488}
489
490
491/**
492 * Initializes a driver stack.
493 *
494 * @returns VBox status code.
495 * @param pDrvStack The driver stack to initialize.
496 * @param pDrvReg The backend driver to use.
497 * @param fWithDrvAudio Whether to include DrvAudio in the stack or not.
498 */
499int audioTestDriverStackInit(PAUDIOTESTDRVSTACK pDrvStack, PCPDMDRVREG pDrvReg, bool fWithDrvAudio)
500{
501 int rc;
502
503 RT_ZERO(*pDrvStack);
504 pDrvStack->pDrvReg = pDrvReg;
505
506 if (!fWithDrvAudio)
507 rc = audioTestDrvConstruct(pDrvStack, pDrvReg, NULL /*pParentDrvIns*/, &pDrvStack->pDrvBackendIns);
508 else
509 {
510 rc = audioTestDrvConstruct(pDrvStack, &g_DrvAUDIO, NULL /*pParentDrvIns*/, &pDrvStack->pDrvAudioIns);
511 if (RT_SUCCESS(rc))
512 {
513 Assert(pDrvStack->pDrvAudioIns);
514 PPDMIBASE const pIBase = &pDrvStack->pDrvAudioIns->IBase;
515 pDrvStack->pIAudioConnector = (PPDMIAUDIOCONNECTOR)pIBase->pfnQueryInterface(pIBase, PDMIAUDIOCONNECTOR_IID);
516 if (pDrvStack->pIAudioConnector)
517 {
518 /* Both input and output is disabled by default. Fix that: */
519 rc = pDrvStack->pIAudioConnector->pfnEnable(pDrvStack->pIAudioConnector, PDMAUDIODIR_OUT, true);
520 if (RT_SUCCESS(rc))
521 rc = pDrvStack->pIAudioConnector->pfnEnable(pDrvStack->pIAudioConnector, PDMAUDIODIR_IN, true);
522 if (RT_FAILURE(rc))
523 {
524 RTTestFailed(g_hTest, "Failed to enabled input and output: %Rrc", rc);
525 audioTestDriverStackDelete(pDrvStack);
526 }
527 }
528 else
529 {
530 RTTestFailed(g_hTest, "Failed to query PDMIAUDIOCONNECTOR");
531 audioTestDriverStackDelete(pDrvStack);
532 rc = VERR_PDM_MISSING_INTERFACE;
533 }
534 }
535 }
536
537 /*
538 * Get the IHostAudio interface and check that the host driver is working.
539 */
540 if (RT_SUCCESS(rc))
541 {
542 PPDMIBASE const pIBase = &pDrvStack->pDrvBackendIns->IBase;
543 pDrvStack->pIHostAudio = (PPDMIHOSTAUDIO)pIBase->pfnQueryInterface(pIBase, PDMIHOSTAUDIO_IID);
544 if (pDrvStack->pIHostAudio)
545 {
546 PDMAUDIOBACKENDSTS enmStatus = pDrvStack->pIHostAudio->pfnGetStatus(pDrvStack->pIHostAudio, PDMAUDIODIR_OUT);
547 if (enmStatus == PDMAUDIOBACKENDSTS_RUNNING)
548 return VINF_SUCCESS;
549
550 RTTestFailed(g_hTest, "Expected backend status RUNNING, got %d instead", enmStatus);
551 }
552 else
553 RTTestFailed(g_hTest, "Failed to query PDMIHOSTAUDIO for '%s'", pDrvReg->szName);
554 audioTestDriverStackDelete(pDrvStack);
555 }
556
557 return rc;
558}
559
560
561/**
562 * Wrapper around PDMIHOSTAUDIO::pfnSetDevice.
563 */
564int audioTestDriverStackSetDevice(PAUDIOTESTDRVSTACK pDrvStack, PDMAUDIODIR enmDir, const char *pszDevId)
565{
566 int rc;
567 if ( pDrvStack->pIHostAudio
568 && pDrvStack->pIHostAudio->pfnSetDevice)
569 rc = pDrvStack->pIHostAudio->pfnSetDevice(pDrvStack->pIHostAudio, enmDir, pszDevId);
570 else if (!pszDevId || *pszDevId)
571 rc = VINF_SUCCESS;
572 else
573 rc = VERR_INVALID_FUNCTION;
574 return rc;
575}
576
577
578/**
579 * Common stream creation code.
580 *
581 * @returns VBox status code.
582 * @param pDrvStack The audio driver stack to create it via.
583 * @param pCfgReq The requested config.
584 * @param ppStream Where to return the stream pointer on success.
585 * @param pCfgAcq Where to return the actual (well, not necessarily when
586 * using DrvAudio, but probably the same) stream config on
587 * success (not used as input).
588 */
589static int audioTestDriverStackStreamCreate(PAUDIOTESTDRVSTACK pDrvStack, PCPDMAUDIOSTREAMCFG pCfgReq,
590 PPDMAUDIOSTREAM *ppStream, PPDMAUDIOSTREAMCFG pCfgAcq)
591{
592 char szTmp[PDMAUDIOSTRMCFGTOSTRING_MAX + 16];
593 int rc;
594 *ppStream = NULL;
595
596 if (pDrvStack->pIAudioConnector)
597 {
598 /*
599 * DrvAudio does most of the work here.
600 */
601 rc = pDrvStack->pIAudioConnector->pfnStreamCreate(pDrvStack->pIAudioConnector, 0 /*fFlags*/, pCfgReq, ppStream);
602 if (RT_SUCCESS(rc))
603 {
604 *pCfgAcq = (*ppStream)->Cfg;
605 RTMsgInfo("Created backend stream: %s\n", PDMAudioStrmCfgToString(pCfgReq, szTmp, sizeof(szTmp)));
606 return rc;
607 }
608 RTTestFailed(g_hTest, "pfnStreamCreate failed: %Rrc", rc);
609 }
610 else
611 {
612 /*
613 * Get the config so we can see how big the PDMAUDIOBACKENDSTREAM
614 * structure actually is for this backend.
615 */
616 PDMAUDIOBACKENDCFG BackendCfg;
617 rc = pDrvStack->pIHostAudio->pfnGetConfig(pDrvStack->pIHostAudio, &BackendCfg);
618 if (RT_SUCCESS(rc))
619 {
620 if (BackendCfg.cbStream >= sizeof(PDMAUDIOBACKENDSTREAM))
621 {
622 /*
623 * Allocate and initialize the stream.
624 */
625 uint32_t const cbStream = sizeof(AUDIOTESTDRVSTACKSTREAM) - sizeof(PDMAUDIOBACKENDSTREAM) + BackendCfg.cbStream;
626 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)RTMemAllocZVar(cbStream);
627 if (pStreamAt)
628 {
629 pStreamAt->Core.uMagic = PDMAUDIOSTREAM_MAGIC;
630 pStreamAt->Core.Cfg = *pCfgReq;
631 pStreamAt->Core.cbBackend = cbStream;
632
633 pStreamAt->Backend.uMagic = PDMAUDIOBACKENDSTREAM_MAGIC;
634 pStreamAt->Backend.pStream = &pStreamAt->Core;
635
636 /*
637 * Call the backend to create the stream.
638 */
639 rc = pDrvStack->pIHostAudio->pfnStreamCreate(pDrvStack->pIHostAudio, &pStreamAt->Backend,
640 pCfgReq, &pStreamAt->Core.Cfg);
641 if (RT_SUCCESS(rc))
642 {
643 if (g_uVerbosity > 1)
644 RTMsgInfo("Created backend stream: %s\n",
645 PDMAudioStrmCfgToString(&pStreamAt->Core.Cfg, szTmp, sizeof(szTmp)));
646
647 /* Return if stream is ready: */
648 if (rc == VINF_SUCCESS)
649 {
650 *ppStream = &pStreamAt->Core;
651 *pCfgAcq = pStreamAt->Core.Cfg;
652 return VINF_SUCCESS;
653 }
654 if (rc == VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED)
655 {
656 /*
657 * Do async init right here and now.
658 */
659 rc = pDrvStack->pIHostAudio->pfnStreamInitAsync(pDrvStack->pIHostAudio, &pStreamAt->Backend,
660 false /*fDestroyed*/);
661 if (RT_SUCCESS(rc))
662 {
663 *ppStream = &pStreamAt->Core;
664 *pCfgAcq = pStreamAt->Core.Cfg;
665 return VINF_SUCCESS;
666 }
667
668 RTTestFailed(g_hTest, "pfnStreamInitAsync failed: %Rrc\n", rc);
669 }
670 else
671 {
672 RTTestFailed(g_hTest, "pfnStreamCreate returned unexpected info status: %Rrc", rc);
673 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
674 }
675 pDrvStack->pIHostAudio->pfnStreamDestroy(pDrvStack->pIHostAudio, &pStreamAt->Backend, true /*fImmediate*/);
676 }
677 else
678 RTTestFailed(g_hTest, "pfnStreamCreate failed: %Rrc\n", rc);
679 }
680 else
681 {
682 RTTestFailed(g_hTest, "Out of memory!\n");
683 rc = VERR_NO_MEMORY;
684 }
685 RTMemFree(pStreamAt);
686 }
687 else
688 {
689 RTTestFailed(g_hTest, "cbStream=%#x is too small, min %#zx!\n", BackendCfg.cbStream, sizeof(PDMAUDIOBACKENDSTREAM));
690 rc = VERR_OUT_OF_RANGE;
691 }
692 }
693 else
694 RTTestFailed(g_hTest, "pfnGetConfig failed: %Rrc\n", rc);
695 }
696 return rc;
697}
698
699
700/**
701 * Creates an output stream.
702 *
703 * @returns VBox status code.
704 * @param pDrvStack The audio driver stack to create it via.
705 * @param pProps The audio properties to use.
706 * @param cMsBufferSize The buffer size in milliseconds.
707 * @param cMsPreBuffer The pre-buffering amount in milliseconds.
708 * @param cMsSchedulingHint The scheduling hint in milliseconds.
709 * @param ppStream Where to return the stream pointer on success.
710 * @param pCfgAcq Where to return the actual (well, not
711 * necessarily when using DrvAudio, but probably
712 * the same) stream config on success (not used as
713 * input).
714 */
715int audioTestDriverStackStreamCreateOutput(PAUDIOTESTDRVSTACK pDrvStack, PCPDMAUDIOPCMPROPS pProps,
716 uint32_t cMsBufferSize, uint32_t cMsPreBuffer, uint32_t cMsSchedulingHint,
717 PPDMAUDIOSTREAM *ppStream, PPDMAUDIOSTREAMCFG pCfgAcq)
718{
719 /*
720 * Calculate the stream config.
721 */
722 PDMAUDIOSTREAMCFG CfgReq;
723 int rc = PDMAudioStrmCfgInitWithProps(&CfgReq, pProps);
724 AssertRC(rc);
725 CfgReq.enmDir = PDMAUDIODIR_OUT;
726 CfgReq.enmPath = PDMAUDIOPATH_OUT_FRONT;
727 CfgReq.Device.cMsSchedulingHint = cMsSchedulingHint == UINT32_MAX || cMsSchedulingHint == 0
728 ? 10 : cMsSchedulingHint;
729 if (pDrvStack->pIAudioConnector && (cMsBufferSize == UINT32_MAX || cMsBufferSize == 0))
730 CfgReq.Backend.cFramesBufferSize = 0; /* DrvAudio picks the default */
731 else
732 CfgReq.Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(pProps,
733 cMsBufferSize == UINT32_MAX || cMsBufferSize == 0
734 ? 300 : cMsBufferSize);
735 if (cMsPreBuffer == UINT32_MAX)
736 CfgReq.Backend.cFramesPreBuffering = pDrvStack->pIAudioConnector ? UINT32_MAX /*DrvAudo picks the default */
737 : CfgReq.Backend.cFramesBufferSize * 2 / 3;
738 else
739 CfgReq.Backend.cFramesPreBuffering = PDMAudioPropsMilliToFrames(pProps, cMsPreBuffer);
740 if ( CfgReq.Backend.cFramesPreBuffering >= CfgReq.Backend.cFramesBufferSize + 16
741 && !pDrvStack->pIAudioConnector /*DrvAudio deals with it*/ )
742 {
743 RTMsgWarning("Cannot pre-buffer %#x frames with only %#x frames of buffer!",
744 CfgReq.Backend.cFramesPreBuffering, CfgReq.Backend.cFramesBufferSize);
745 CfgReq.Backend.cFramesPreBuffering = CfgReq.Backend.cFramesBufferSize > 16
746 ? CfgReq.Backend.cFramesBufferSize - 16 : 0;
747 }
748
749 static uint32_t s_idxStream = 0;
750 uint32_t const idxStream = s_idxStream++;
751 RTStrPrintf(CfgReq.szName, sizeof(CfgReq.szName), "out-%u", idxStream);
752
753 /*
754 * Call common code to do the actual work.
755 */
756 return audioTestDriverStackStreamCreate(pDrvStack, &CfgReq, ppStream, pCfgAcq);
757}
758
759
760/**
761 * Creates an input stream.
762 *
763 * @returns VBox status code.
764 * @param pDrvStack The audio driver stack to create it via.
765 * @param pProps The audio properties to use.
766 * @param cMsBufferSize The buffer size in milliseconds.
767 * @param cMsPreBuffer The pre-buffering amount in milliseconds.
768 * @param cMsSchedulingHint The scheduling hint in milliseconds.
769 * @param ppStream Where to return the stream pointer on success.
770 * @param pCfgAcq Where to return the actual (well, not
771 * necessarily when using DrvAudio, but probably
772 * the same) stream config on success (not used as
773 * input).
774 */
775int audioTestDriverStackStreamCreateInput(PAUDIOTESTDRVSTACK pDrvStack, PCPDMAUDIOPCMPROPS pProps,
776 uint32_t cMsBufferSize, uint32_t cMsPreBuffer, uint32_t cMsSchedulingHint,
777 PPDMAUDIOSTREAM *ppStream, PPDMAUDIOSTREAMCFG pCfgAcq)
778{
779 /*
780 * Calculate the stream config.
781 */
782 PDMAUDIOSTREAMCFG CfgReq;
783 int rc = PDMAudioStrmCfgInitWithProps(&CfgReq, pProps);
784 AssertRC(rc);
785 CfgReq.enmDir = PDMAUDIODIR_IN;
786 CfgReq.enmPath = PDMAUDIOPATH_IN_LINE;
787 CfgReq.Device.cMsSchedulingHint = cMsSchedulingHint == UINT32_MAX || cMsSchedulingHint == 0
788 ? 10 : cMsSchedulingHint;
789 if (pDrvStack->pIAudioConnector && (cMsBufferSize == UINT32_MAX || cMsBufferSize == 0))
790 CfgReq.Backend.cFramesBufferSize = 0; /* DrvAudio picks the default */
791 else
792 CfgReq.Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(pProps,
793 cMsBufferSize == UINT32_MAX || cMsBufferSize == 0
794 ? 300 : cMsBufferSize);
795 if (cMsPreBuffer == UINT32_MAX)
796 CfgReq.Backend.cFramesPreBuffering = pDrvStack->pIAudioConnector ? UINT32_MAX /*DrvAudio picks the default */
797 : CfgReq.Backend.cFramesBufferSize / 2;
798 else
799 CfgReq.Backend.cFramesPreBuffering = PDMAudioPropsMilliToFrames(pProps, cMsPreBuffer);
800 if ( CfgReq.Backend.cFramesPreBuffering >= CfgReq.Backend.cFramesBufferSize + 16 /** @todo way to little */
801 && !pDrvStack->pIAudioConnector /*DrvAudio deals with it*/ )
802 {
803 RTMsgWarning("Cannot pre-buffer %#x frames with only %#x frames of buffer!",
804 CfgReq.Backend.cFramesPreBuffering, CfgReq.Backend.cFramesBufferSize);
805 CfgReq.Backend.cFramesPreBuffering = CfgReq.Backend.cFramesBufferSize > 16
806 ? CfgReq.Backend.cFramesBufferSize - 16 : 0;
807 }
808
809 static uint32_t s_idxStream = 0;
810 uint32_t const idxStream = s_idxStream++;
811 RTStrPrintf(CfgReq.szName, sizeof(CfgReq.szName), "in-%u", idxStream);
812
813 /*
814 * Call common code to do the actual work.
815 */
816 return audioTestDriverStackStreamCreate(pDrvStack, &CfgReq, ppStream, pCfgAcq);
817}
818
819
820/**
821 * Destroys a stream.
822 */
823void audioTestDriverStackStreamDestroy(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
824{
825 if (pStream)
826 {
827 if (pDrvStack->pIAudioConnector)
828 {
829 int rc = pDrvStack->pIAudioConnector->pfnStreamDestroy(pDrvStack->pIAudioConnector, pStream, true /*fImmediate*/);
830 if (RT_FAILURE(rc))
831 RTTestFailed(g_hTest, "pfnStreamDestroy failed: %Rrc", rc);
832 }
833 else
834 {
835 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
836 int rc = pDrvStack->pIHostAudio->pfnStreamDestroy(pDrvStack->pIHostAudio, &pStreamAt->Backend, true /*fImmediate*/);
837 if (RT_SUCCESS(rc))
838 {
839 pStreamAt->Core.uMagic = ~PDMAUDIOSTREAM_MAGIC;
840 pStreamAt->Backend.uMagic = ~PDMAUDIOBACKENDSTREAM_MAGIC;
841 RTMemFree(pStreamAt);
842 }
843 else
844 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamDestroy failed: %Rrc", rc);
845 }
846 }
847}
848
849
850/**
851 * Enables a stream.
852 */
853int audioTestDriverStackStreamEnable(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
854{
855 int rc;
856 if (pDrvStack->pIAudioConnector)
857 {
858 rc = pDrvStack->pIAudioConnector->pfnStreamControl(pDrvStack->pIAudioConnector, pStream, PDMAUDIOSTREAMCMD_ENABLE);
859 if (RT_FAILURE(rc))
860 RTTestFailed(g_hTest, "pfnStreamControl/ENABLE failed: %Rrc", rc);
861 }
862 else
863 {
864 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
865 rc = pDrvStack->pIHostAudio->pfnStreamEnable(pDrvStack->pIHostAudio, &pStreamAt->Backend);
866 if (RT_FAILURE(rc))
867 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamEnable failed: %Rrc", rc);
868 }
869 return rc;
870}
871
872
873/**
874 * Disables a stream.
875 */
876int AudioTestDriverStackStreamDisable(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
877{
878 int rc;
879 if (pDrvStack->pIAudioConnector)
880 {
881 rc = pDrvStack->pIAudioConnector->pfnStreamControl(pDrvStack->pIAudioConnector, pStream, PDMAUDIOSTREAMCMD_DISABLE);
882 if (RT_FAILURE(rc))
883 RTTestFailed(g_hTest, "pfnStreamControl/DISABLE failed: %Rrc", rc);
884 }
885 else
886 {
887 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
888 rc = pDrvStack->pIHostAudio->pfnStreamDisable(pDrvStack->pIHostAudio, &pStreamAt->Backend);
889 if (RT_FAILURE(rc))
890 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamDisable failed: %Rrc", rc);
891 }
892 return rc;
893}
894
895
896/**
897 * Drains an output stream.
898 */
899int audioTestDriverStackStreamDrain(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream, bool fSync)
900{
901 int rc;
902 if (pDrvStack->pIAudioConnector)
903 {
904 /*
905 * Issue the drain request.
906 */
907 rc = pDrvStack->pIAudioConnector->pfnStreamControl(pDrvStack->pIAudioConnector, pStream, PDMAUDIOSTREAMCMD_DRAIN);
908 if (RT_SUCCESS(rc) && fSync)
909 {
910 /*
911 * This is a synchronous drain, so wait for the driver to change state to inactive.
912 */
913 PDMAUDIOSTREAMSTATE enmState;
914 while ( (enmState = pDrvStack->pIAudioConnector->pfnStreamGetState(pDrvStack->pIAudioConnector, pStream))
915 >= PDMAUDIOSTREAMSTATE_ENABLED)
916 {
917 RTThreadSleep(2);
918 rc = pDrvStack->pIAudioConnector->pfnStreamIterate(pDrvStack->pIAudioConnector, pStream);
919 if (RT_FAILURE(rc))
920 {
921 RTTestFailed(g_hTest, "pfnStreamIterate/DRAIN failed: %Rrc", rc);
922 break;
923 }
924 }
925 if (enmState != PDMAUDIOSTREAMSTATE_INACTIVE)
926 {
927 RTTestFailed(g_hTest, "Stream state not INACTIVE after draining: %s", PDMAudioStreamStateGetName(enmState));
928 rc = VERR_AUDIO_STREAM_NOT_READY;
929 }
930 }
931 else if (RT_FAILURE(rc))
932 RTTestFailed(g_hTest, "pfnStreamControl/ENABLE failed: %Rrc", rc);
933 }
934 else
935 {
936 /*
937 * Issue the drain request.
938 */
939 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
940 rc = pDrvStack->pIHostAudio->pfnStreamDrain(pDrvStack->pIHostAudio, &pStreamAt->Backend);
941 if (RT_SUCCESS(rc) && fSync)
942 {
943 /*
944 * This is a synchronous drain, so wait for the driver to change state to inactive.
945 */
946 PDMHOSTAUDIOSTREAMSTATE enmHostState;
947 while ( (enmHostState = pDrvStack->pIHostAudio->pfnStreamGetState(pDrvStack->pIHostAudio, &pStreamAt->Backend))
948 == PDMHOSTAUDIOSTREAMSTATE_DRAINING)
949 {
950 RTThreadSleep(2);
951 uint32_t cbWritten = UINT32_MAX;
952 rc = pDrvStack->pIHostAudio->pfnStreamPlay(pDrvStack->pIHostAudio, &pStreamAt->Backend,
953 NULL /*pvBuf*/, 0 /*cbBuf*/, &cbWritten);
954 if (RT_FAILURE(rc))
955 {
956 RTTestFailed(g_hTest, "pfnStreamPlay/DRAIN failed: %Rrc", rc);
957 break;
958 }
959 if (cbWritten != 0)
960 {
961 RTTestFailed(g_hTest, "pfnStreamPlay/DRAIN did not set cbWritten to zero: %#x", cbWritten);
962 rc = VERR_MISSING;
963 break;
964 }
965 }
966 if (enmHostState != PDMHOSTAUDIOSTREAMSTATE_OKAY)
967 {
968 RTTestFailed(g_hTest, "Stream state not OKAY after draining: %s", PDMHostAudioStreamStateGetName(enmHostState));
969 rc = VERR_AUDIO_STREAM_NOT_READY;
970 }
971 }
972 else if (RT_FAILURE(rc))
973 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamControl/ENABLE failed: %Rrc", rc);
974 }
975 return rc;
976}
977
978
979/**
980 * Checks if the stream is okay.
981 * @returns true if okay, false if not.
982 */
983bool audioTestDriverStackStreamIsOkay(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
984{
985 /*
986 * Get the stream status and check if it means is okay or not.
987 */
988 bool fRc = false;
989 if (pDrvStack->pIAudioConnector)
990 {
991 PDMAUDIOSTREAMSTATE enmState = pDrvStack->pIAudioConnector->pfnStreamGetState(pDrvStack->pIAudioConnector, pStream);
992 switch (enmState)
993 {
994 case PDMAUDIOSTREAMSTATE_NOT_WORKING:
995 case PDMAUDIOSTREAMSTATE_NEED_REINIT:
996 break;
997 case PDMAUDIOSTREAMSTATE_INACTIVE:
998 case PDMAUDIOSTREAMSTATE_ENABLED:
999 case PDMAUDIOSTREAMSTATE_ENABLED_READABLE:
1000 case PDMAUDIOSTREAMSTATE_ENABLED_WRITABLE:
1001 fRc = true;
1002 break;
1003 /* no default */
1004 case PDMAUDIOSTREAMSTATE_INVALID:
1005 case PDMAUDIOSTREAMSTATE_END:
1006 case PDMAUDIOSTREAMSTATE_32BIT_HACK:
1007 break;
1008 }
1009 }
1010 else
1011 {
1012 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1013 PDMHOSTAUDIOSTREAMSTATE enmHostState = pDrvStack->pIHostAudio->pfnStreamGetState(pDrvStack->pIHostAudio,
1014 &pStreamAt->Backend);
1015 switch (enmHostState)
1016 {
1017 case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING:
1018 case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING:
1019 break;
1020 case PDMHOSTAUDIOSTREAMSTATE_OKAY:
1021 case PDMHOSTAUDIOSTREAMSTATE_DRAINING:
1022 case PDMHOSTAUDIOSTREAMSTATE_INACTIVE:
1023 fRc = true;
1024 break;
1025 /* no default */
1026 case PDMHOSTAUDIOSTREAMSTATE_INVALID:
1027 case PDMHOSTAUDIOSTREAMSTATE_END:
1028 case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
1029 break;
1030 }
1031 }
1032 return fRc;
1033}
1034
1035
1036/**
1037 * Gets the number of bytes it's currently possible to write to the stream.
1038 */
1039uint32_t audioTestDriverStackStreamGetWritable(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
1040{
1041 uint32_t cbWritable;
1042 if (pDrvStack->pIAudioConnector)
1043 cbWritable = pDrvStack->pIAudioConnector->pfnStreamGetWritable(pDrvStack->pIAudioConnector, pStream);
1044 else
1045 {
1046 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1047 cbWritable = pDrvStack->pIHostAudio->pfnStreamGetWritable(pDrvStack->pIHostAudio, &pStreamAt->Backend);
1048 }
1049 return cbWritable;
1050}
1051
1052
1053/**
1054 * Tries to play the @a cbBuf bytes of samples in @a pvBuf.
1055 */
1056int audioTestDriverStackStreamPlay(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream,
1057 void const *pvBuf, uint32_t cbBuf, uint32_t *pcbPlayed)
1058{
1059 int rc;
1060 if (pDrvStack->pIAudioConnector)
1061 {
1062 rc = pDrvStack->pIAudioConnector->pfnStreamPlay(pDrvStack->pIAudioConnector, pStream, pvBuf, cbBuf, pcbPlayed);
1063 if (RT_FAILURE(rc))
1064 RTTestFailed(g_hTest, "pfnStreamPlay(,,,%#x,) failed: %Rrc", cbBuf, rc);
1065 }
1066 else
1067 {
1068 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1069 rc = pDrvStack->pIHostAudio->pfnStreamPlay(pDrvStack->pIHostAudio, &pStreamAt->Backend, pvBuf, cbBuf, pcbPlayed);
1070 if (RT_FAILURE(rc))
1071 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamPlay(,,,%#x,) failed: %Rrc", cbBuf, rc);
1072 }
1073 return rc;
1074}
1075
1076
1077/**
1078 * Gets the number of bytes it's currently possible to write to the stream.
1079 */
1080uint32_t audioTestDriverStackStreamGetReadable(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream)
1081{
1082 uint32_t cbReadable;
1083 if (pDrvStack->pIAudioConnector)
1084 cbReadable = pDrvStack->pIAudioConnector->pfnStreamGetReadable(pDrvStack->pIAudioConnector, pStream);
1085 else
1086 {
1087 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1088 cbReadable = pDrvStack->pIHostAudio->pfnStreamGetReadable(pDrvStack->pIHostAudio, &pStreamAt->Backend);
1089 }
1090 return cbReadable;
1091}
1092
1093
1094/**
1095 * Tries to capture @a cbBuf bytes of samples in @a pvBuf.
1096 */
1097int audioTestDriverStackStreamCapture(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream,
1098 void *pvBuf, uint32_t cbBuf, uint32_t *pcbCaptured)
1099{
1100 int rc;
1101 if (pDrvStack->pIAudioConnector)
1102 {
1103 rc = pDrvStack->pIAudioConnector->pfnStreamCapture(pDrvStack->pIAudioConnector, pStream, pvBuf, cbBuf, pcbCaptured);
1104 if (RT_FAILURE(rc))
1105 RTTestFailed(g_hTest, "pfnStreamCapture(,,,%#x,) failed: %Rrc", cbBuf, rc);
1106 }
1107 else
1108 {
1109 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1110 rc = pDrvStack->pIHostAudio->pfnStreamCapture(pDrvStack->pIHostAudio, &pStreamAt->Backend, pvBuf, cbBuf, pcbCaptured);
1111 if (RT_FAILURE(rc))
1112 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamCapture(,,,%#x,) failed: %Rrc", cbBuf, rc);
1113 }
1114 return rc;
1115}
1116
1117
1118/*********************************************************************************************************************************
1119* Mixed streams *
1120*********************************************************************************************************************************/
1121
1122/**
1123 * Initializing mixing for a stream.
1124 *
1125 * This can be used as a do-nothing wrapper for the stack.
1126 *
1127 * @returns VBox status code.
1128 * @param pMix The mixing state.
1129 * @param pStream The stream to mix to/from.
1130 * @param pProps The mixer properties. Pass NULL for no mixing, just
1131 * wrap the driver stack functionality.
1132 * @param cMsBuffer The buffer size.
1133 */
1134int AudioTestMixStreamInit(PAUDIOTESTDRVMIXSTREAM pMix, PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream,
1135 PCPDMAUDIOPCMPROPS pProps, uint32_t cMsBuffer)
1136{
1137 RT_ZERO(*pMix);
1138
1139 AssertReturn(pDrvStack, VERR_INVALID_PARAMETER);
1140 AssertReturn(pStream, VERR_INVALID_PARAMETER);
1141
1142 pMix->pDrvStack = pDrvStack;
1143 pMix->pStream = pStream;
1144 if (!pProps)
1145 {
1146 pMix->pProps = &pStream->Cfg.Props;
1147 return VINF_SUCCESS;
1148 }
1149
1150 /*
1151 * Okay, we're doing mixing so we need to set up the mixer buffer
1152 * and associated states.
1153 */
1154 pMix->fDoMixing = true;
1155 int rc = AudioMixBufInit(&pMix->MixBuf, "mixer", pProps, PDMAudioPropsMilliToFrames(pProps, cMsBuffer));
1156 if (RT_SUCCESS(rc))
1157 {
1158 pMix->pProps = &pMix->MixBuf.Props;
1159
1160 if (pStream->Cfg.enmDir == PDMAUDIODIR_IN)
1161 {
1162 rc = AudioMixBufInitPeekState(&pMix->MixBuf, &pMix->PeekState, &pMix->MixBuf.Props);
1163 if (RT_SUCCESS(rc))
1164 {
1165 rc = AudioMixBufInitWriteState(&pMix->MixBuf, &pMix->WriteState, &pStream->Cfg.Props);
1166 if (RT_SUCCESS(rc))
1167 return rc;
1168 }
1169 }
1170 else if (pStream->Cfg.enmDir == PDMAUDIODIR_OUT)
1171 {
1172 rc = AudioMixBufInitWriteState(&pMix->MixBuf, &pMix->WriteState, &pMix->MixBuf.Props);
1173 if (RT_SUCCESS(rc))
1174 {
1175 rc = AudioMixBufInitPeekState(&pMix->MixBuf, &pMix->PeekState, &pStream->Cfg.Props);
1176 if (RT_SUCCESS(rc))
1177 return rc;
1178 }
1179 }
1180 else
1181 {
1182 RTTestFailed(g_hTest, "Bogus stream direction!");
1183 rc = VERR_INVALID_STATE;
1184 }
1185 }
1186 else
1187 RTTestFailed(g_hTest, "AudioMixBufInit failed: %Rrc", rc);
1188 RT_ZERO(*pMix);
1189 return rc;
1190}
1191
1192
1193/**
1194 * Terminate mixing (leaves the stream untouched).
1195 *
1196 * @param pMix The mixing state.
1197 */
1198void AudioTestMixStreamTerm(PAUDIOTESTDRVMIXSTREAM pMix)
1199{
1200 if (pMix->fDoMixing)
1201 {
1202 AudioMixBufTerm(&pMix->MixBuf);
1203 pMix->pStream = NULL;
1204 }
1205 RT_ZERO(*pMix);
1206}
1207
1208
1209/**
1210 * Worker that transports data between the mixer buffer and the drivers.
1211 *
1212 * @returns VBox status code.
1213 * @param pMix The mixer stream setup to do transfers for.
1214 */
1215static int audioTestMixStreamTransfer(PAUDIOTESTDRVMIXSTREAM pMix)
1216{
1217 uint8_t abBuf[16384];
1218 if (pMix->pStream->Cfg.enmDir == PDMAUDIODIR_IN)
1219 {
1220 /*
1221 * Try fill up the mixer buffer as much as possible.
1222 *
1223 * Slight fun part is that we have to calculate conversion
1224 * ratio and be rather pessimistic about it.
1225 */
1226 uint32_t const cbBuf = PDMAudioPropsFloorBytesToFrame(&pMix->pStream->Cfg.Props, sizeof(abBuf));
1227 for (;;)
1228 {
1229 /*
1230 * Figure out how much we can move in this iteration.
1231 */
1232 uint32_t cDstFrames = AudioMixBufFree(&pMix->MixBuf);
1233 if (!cDstFrames)
1234 break;
1235
1236 uint32_t cbReadable = audioTestDriverStackStreamGetReadable(pMix->pDrvStack, pMix->pStream);
1237 if (!cbReadable)
1238 break;
1239
1240 uint32_t cbToRead;
1241 if (PDMAudioPropsHz(&pMix->pStream->Cfg.Props) == PDMAudioPropsHz(&pMix->MixBuf.Props))
1242 cbToRead = PDMAudioPropsFramesToBytes(&pMix->pStream->Cfg.Props, cDstFrames);
1243 else
1244 cbToRead = PDMAudioPropsFramesToBytes(&pMix->pStream->Cfg.Props,
1245 (uint64_t)cDstFrames * PDMAudioPropsHz(&pMix->pStream->Cfg.Props)
1246 / PDMAudioPropsHz(&pMix->MixBuf.Props));
1247 cbToRead = RT_MIN(cbToRead, RT_MIN(cbReadable, cbBuf));
1248 if (!cbToRead)
1249 break;
1250
1251 /*
1252 * Get the data.
1253 */
1254 uint32_t cbCaptured = 0;
1255 int rc = audioTestDriverStackStreamCapture(pMix->pDrvStack, pMix->pStream, abBuf, cbToRead, &cbCaptured);
1256 if (RT_FAILURE(rc))
1257 return rc;
1258 Assert(cbCaptured == cbToRead);
1259 AssertBreak(cbCaptured > 0);
1260
1261 /*
1262 * Feed it to the mixer.
1263 */
1264 uint32_t cDstFramesWritten = 0;
1265 if ((abBuf[0] >> 4) & 1) /* some cheap random */
1266 AudioMixBufWrite(&pMix->MixBuf, &pMix->WriteState, abBuf, cbCaptured,
1267 0 /*offDstFrame*/, cDstFrames, &cDstFramesWritten);
1268 else
1269 {
1270 AudioMixBufSilence(&pMix->MixBuf, &pMix->WriteState, 0 /*offFrame*/, cDstFrames);
1271 AudioMixBufBlend(&pMix->MixBuf, &pMix->WriteState, abBuf, cbCaptured,
1272 0 /*offDstFrame*/, cDstFrames, &cDstFramesWritten);
1273 }
1274 AudioMixBufCommit(&pMix->MixBuf, cDstFramesWritten);
1275 }
1276 }
1277 else
1278 {
1279 /*
1280 * The goal here is to empty the mixer buffer by transfering all
1281 * the data to the drivers.
1282 */
1283 uint32_t const cbBuf = PDMAudioPropsFloorBytesToFrame(&pMix->MixBuf.Props, sizeof(abBuf));
1284 for (;;)
1285 {
1286 uint32_t cFrames = AudioMixBufUsed(&pMix->MixBuf);
1287 if (!cFrames)
1288 break;
1289
1290 uint32_t cbWritable = audioTestDriverStackStreamGetWritable(pMix->pDrvStack, pMix->pStream);
1291 if (!cbWritable)
1292 break;
1293
1294 uint32_t cSrcFramesPeeked;
1295 uint32_t cbDstPeeked;
1296 AudioMixBufPeek(&pMix->MixBuf, 0 /*offSrcFrame*/, cFrames, &cSrcFramesPeeked,
1297 &pMix->PeekState, abBuf, RT_MIN(cbBuf, cbWritable), &cbDstPeeked);
1298 AudioMixBufAdvance(&pMix->MixBuf, cSrcFramesPeeked);
1299
1300 if (!cbDstPeeked)
1301 break;
1302
1303 uint32_t offBuf = 0;
1304 while (offBuf < cbDstPeeked)
1305 {
1306 uint32_t cbPlayed = 0;
1307 int rc = audioTestDriverStackStreamPlay(pMix->pDrvStack, pMix->pStream,
1308 &abBuf[offBuf], cbDstPeeked - offBuf, &cbPlayed);
1309 if (RT_FAILURE(rc))
1310 return rc;
1311 if (!cbPlayed)
1312 RTThreadSleep(1);
1313 offBuf += cbPlayed;
1314 }
1315 }
1316 }
1317 return VINF_SUCCESS;
1318}
1319
1320
1321/**
1322 * Same as audioTestDriverStackStreamEnable.
1323 */
1324int AudioTestMixStreamEnable(PAUDIOTESTDRVMIXSTREAM pMix)
1325{
1326 return audioTestDriverStackStreamEnable(pMix->pDrvStack, pMix->pStream);
1327}
1328
1329
1330/**
1331 * Same as audioTestDriverStackStreamDrain.
1332 */
1333int AudioTestMixStreamDrain(PAUDIOTESTDRVMIXSTREAM pMix, bool fSync)
1334{
1335 /*
1336 * If we're mixing, we must first make sure the buffer is empty.
1337 */
1338 if (pMix->fDoMixing)
1339 {
1340 audioTestMixStreamTransfer(pMix);
1341 while (AudioMixBufUsed(&pMix->MixBuf) > 0)
1342 {
1343 RTThreadSleep(1);
1344 audioTestMixStreamTransfer(pMix);
1345 }
1346 }
1347
1348 /*
1349 * Then we do the regular workt.
1350 */
1351 return audioTestDriverStackStreamDrain(pMix->pDrvStack, pMix->pStream, fSync);
1352}
1353
1354/**
1355 * Same as audioTestDriverStackStreamDisable.
1356 */
1357int AudioTestMixStreamDisable(PAUDIOTESTDRVMIXSTREAM pMix)
1358{
1359 return AudioTestDriverStackStreamDisable(pMix->pDrvStack, pMix->pStream);
1360}
1361
1362
1363/**
1364 * Same as audioTestDriverStackStreamIsOkay.
1365 */
1366bool AudioTestMixStreamIsOkay(PAUDIOTESTDRVMIXSTREAM pMix)
1367{
1368 return audioTestDriverStackStreamIsOkay(pMix->pDrvStack, pMix->pStream);
1369}
1370
1371
1372/**
1373 * Same as audioTestDriverStackStreamGetWritable
1374 */
1375uint32_t AudioTestMixStreamGetWritable(PAUDIOTESTDRVMIXSTREAM pMix)
1376{
1377 if (!pMix->fDoMixing)
1378 return audioTestDriverStackStreamGetWritable(pMix->pDrvStack, pMix->pStream);
1379 uint32_t cbRet = AudioMixBufFreeBytes(&pMix->MixBuf);
1380 if (!cbRet)
1381 {
1382 audioTestMixStreamTransfer(pMix);
1383 cbRet = AudioMixBufFreeBytes(&pMix->MixBuf);
1384 }
1385 return cbRet;
1386}
1387
1388
1389
1390
1391/**
1392 * Same as audioTestDriverStackStreamPlay.
1393 */
1394int AudioTestMixStreamPlay(PAUDIOTESTDRVMIXSTREAM pMix, void const *pvBuf, uint32_t cbBuf, uint32_t *pcbPlayed)
1395{
1396 if (!pMix->fDoMixing)
1397 return audioTestDriverStackStreamPlay(pMix->pDrvStack, pMix->pStream, pvBuf, cbBuf, pcbPlayed);
1398
1399 *pcbPlayed = 0;
1400
1401 int rc = audioTestMixStreamTransfer(pMix);
1402 if (RT_FAILURE(rc))
1403 return rc;
1404
1405 uint32_t const cbFrame = PDMAudioPropsFrameSize(&pMix->MixBuf.Props);
1406 while (cbBuf >= cbFrame)
1407 {
1408 uint32_t const cFrames = AudioMixBufFree(&pMix->MixBuf);
1409 if (!cFrames)
1410 break;
1411 uint32_t cbToWrite = PDMAudioPropsFramesToBytes(&pMix->MixBuf.Props, cFrames);
1412 cbToWrite = RT_MIN(cbToWrite, cbBuf);
1413 cbToWrite = PDMAudioPropsFloorBytesToFrame(&pMix->MixBuf.Props, cbToWrite);
1414
1415 uint32_t cFramesWritten = 0;
1416 AudioMixBufWrite(&pMix->MixBuf, &pMix->WriteState, pvBuf, cbToWrite, 0 /*offDstFrame*/, cFrames, &cFramesWritten);
1417 Assert(cFramesWritten == PDMAudioPropsBytesToFrames(&pMix->MixBuf.Props, cbToWrite));
1418 AudioMixBufCommit(&pMix->MixBuf, cFramesWritten);
1419
1420 *pcbPlayed += cbToWrite;
1421 cbBuf -= cbToWrite;
1422 pvBuf = (uint8_t const *)pvBuf + cbToWrite;
1423
1424 rc = audioTestMixStreamTransfer(pMix);
1425 if (RT_FAILURE(rc))
1426 return *pcbPlayed ? VINF_SUCCESS : rc;
1427 }
1428
1429 return VINF_SUCCESS;
1430}
1431
1432
1433/**
1434 * Same as audioTestDriverStackStreamGetReadable
1435 */
1436uint32_t AudioTestMixStreamGetReadable(PAUDIOTESTDRVMIXSTREAM pMix)
1437{
1438 if (!pMix->fDoMixing)
1439 return audioTestDriverStackStreamGetReadable(pMix->pDrvStack, pMix->pStream);
1440
1441 audioTestMixStreamTransfer(pMix);
1442 uint32_t cbRet = AudioMixBufUsedBytes(&pMix->MixBuf);
1443 return cbRet;
1444}
1445
1446
1447
1448
1449/**
1450 * Same as audioTestDriverStackStreamCapture.
1451 */
1452int AudioTestMixStreamCapture(PAUDIOTESTDRVMIXSTREAM pMix, void *pvBuf, uint32_t cbBuf, uint32_t *pcbCaptured)
1453{
1454 if (!pMix->fDoMixing)
1455 return audioTestDriverStackStreamCapture(pMix->pDrvStack, pMix->pStream, pvBuf, cbBuf, pcbCaptured);
1456
1457 *pcbCaptured = 0;
1458
1459 int rc = audioTestMixStreamTransfer(pMix);
1460 if (RT_FAILURE(rc))
1461 return rc;
1462
1463 uint32_t const cbFrame = PDMAudioPropsFrameSize(&pMix->MixBuf.Props);
1464 while (cbBuf >= cbFrame)
1465 {
1466 uint32_t const cFrames = AudioMixBufUsed(&pMix->MixBuf);
1467 if (!cFrames)
1468 break;
1469 uint32_t cbToRead = PDMAudioPropsFramesToBytes(&pMix->MixBuf.Props, cFrames);
1470 cbToRead = RT_MIN(cbToRead, cbBuf);
1471 cbToRead = PDMAudioPropsFloorBytesToFrame(&pMix->MixBuf.Props, cbToRead);
1472
1473 uint32_t cFramesPeeked = 0;
1474 uint32_t cbPeeked = 0;
1475 AudioMixBufPeek(&pMix->MixBuf, 0 /*offSrcFrame*/, cFrames, &cFramesPeeked, &pMix->PeekState, pvBuf, cbToRead, &cbPeeked);
1476 Assert(cFramesPeeked == PDMAudioPropsBytesToFrames(&pMix->MixBuf.Props, cbPeeked));
1477 AudioMixBufAdvance(&pMix->MixBuf, cFramesPeeked);
1478
1479 *pcbCaptured += cbToRead;
1480 cbBuf -= cbToRead;
1481 pvBuf = (uint8_t *)pvBuf + cbToRead;
1482
1483 rc = audioTestMixStreamTransfer(pMix);
1484 if (RT_FAILURE(rc))
1485 return *pcbCaptured ? VINF_SUCCESS : rc;
1486 }
1487
1488 return VINF_SUCCESS;
1489}
1490
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