VirtualBox

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

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

ValKit/AudioTest: leak. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.9 KB
Line 
1/* $Id: vkatDriverStack.cpp 89444 2021-06-01 20:50:54Z 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_STAMRegisterF(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
214 STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
215 const char *pszName, ...)
216{
217 RT_NOREF(pDrvIns, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName);
218}
219
220
221static DECLCALLBACK(void) audioTestDrvHlp_STAMRegisterV(PPDMDRVINS pDrvIns, void *pvSample, STAMTYPE enmType,
222 STAMVISIBILITY enmVisibility, STAMUNIT enmUnit, const char *pszDesc,
223 const char *pszName, va_list args)
224{
225 RT_NOREF(pDrvIns, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
226}
227
228
229static DECLCALLBACK(int) audioTestDrvHlp_STAMDeregister(PPDMDRVINS pDrvIns, void *pvSample)
230{
231 RT_NOREF(pDrvIns, pvSample);
232 return VINF_SUCCESS;
233}
234
235
236static DECLCALLBACK(int) audioTestDrvHlp_STAMDeregisterByPrefix(PPDMDRVINS pDrvIns, const char *pszPrefix)
237{
238 RT_NOREF(pDrvIns, pszPrefix);
239 return VINF_SUCCESS;
240}
241
242/**
243 * Get the driver helpers.
244 */
245static const PDMDRVHLPR3 *audioTestFakeGetDrvHlp(void)
246{
247 /*
248 * Note! No initializer for s_DrvHlp (also why it's not a file global).
249 * We do not want to have to update this code every time PDMDRVHLPR3
250 * grows new entries or are otherwise modified. Only when the
251 * entries used by the audio driver changes do we want to change
252 * our code.
253 */
254 static PDMDRVHLPR3 s_DrvHlp;
255 if (s_DrvHlp.u32Version != PDM_DRVHLPR3_VERSION)
256 {
257 s_DrvHlp.u32Version = PDM_DRVHLPR3_VERSION;
258 s_DrvHlp.u32TheEnd = PDM_DRVHLPR3_VERSION;
259 s_DrvHlp.pfnAttach = audioTestDrvHlp_Attach;
260 s_DrvHlp.pfnSTAMRegisterF = audioTestDrvHlp_STAMRegisterF;
261 s_DrvHlp.pfnSTAMRegisterV = audioTestDrvHlp_STAMRegisterV;
262 s_DrvHlp.pfnSTAMDeregister = audioTestDrvHlp_STAMDeregister;
263 s_DrvHlp.pfnSTAMDeregisterByPrefix = audioTestDrvHlp_STAMDeregisterByPrefix;
264 }
265 return &s_DrvHlp;
266}
267
268/** @} */
269
270
271/**
272 * Implementation of PDMIBASE::pfnQueryInterface for a fake device above
273 * DrvAudio.
274 */
275static DECLCALLBACK(void *) audioTestFakeDeviceIBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
276{
277 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, pInterface);
278 RTMsgWarning("audioTestFakeDeviceIBaseQueryInterface: Unknown interface: %s\n", pszIID);
279 return NULL;
280}
281
282/** IBase interface for a fake device above DrvAudio. */
283static PDMIBASE g_AudioTestFakeDeviceIBase = { audioTestFakeDeviceIBaseQueryInterface };
284
285
286static DECLCALLBACK(int) audioTestIHostAudioPort_DoOnWorkerThread(PPDMIHOSTAUDIOPORT pInterface, PPDMAUDIOBACKENDSTREAM pStream,
287 uintptr_t uUser, void *pvUser)
288{
289 RT_NOREF(pInterface, pStream, uUser, pvUser);
290 RTMsgWarning("audioTestIHostAudioPort_DoOnWorkerThread was called\n");
291 return VERR_NOT_IMPLEMENTED;
292}
293
294
295DECLCALLBACK(void) audioTestIHostAudioPort_NotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface, PDMAUDIODIR enmDir, void *pvUser)
296{
297 RT_NOREF(pInterface, enmDir, pvUser);
298 RTMsgWarning("audioTestIHostAudioPort_NotifyDeviceChanged was called\n");
299}
300
301
302static DECLCALLBACK(void) audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch(PPDMIHOSTAUDIOPORT pInterface,
303 PPDMAUDIOBACKENDSTREAM pStream)
304{
305 RT_NOREF(pInterface, pStream);
306 RTMsgWarning("audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch was called\n");
307}
308
309
310static DECLCALLBACK(void) audioTestIHostAudioPort_StreamNotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface,
311 PPDMAUDIOBACKENDSTREAM pStream, bool fReInit)
312{
313 RT_NOREF(pInterface, pStream, fReInit);
314 RTMsgWarning("audioTestIHostAudioPort_StreamNotifyDeviceChanged was called\n");
315}
316
317
318static DECLCALLBACK(void) audioTestIHostAudioPort_NotifyDevicesChanged(PPDMIHOSTAUDIOPORT pInterface)
319{
320 RT_NOREF(pInterface);
321 RTMsgWarning("audioTestIHostAudioPort_NotifyDevicesChanged was called\n");
322}
323
324
325static PDMIHOSTAUDIOPORT g_AudioTestIHostAudioPort =
326{
327 audioTestIHostAudioPort_DoOnWorkerThread,
328 audioTestIHostAudioPort_NotifyDeviceChanged,
329 audioTestIHostAudioPort_StreamNotifyPreparingDeviceSwitch,
330 audioTestIHostAudioPort_StreamNotifyDeviceChanged,
331 audioTestIHostAudioPort_NotifyDevicesChanged,
332};
333
334
335/**
336 * Implementation of PDMIBASE::pfnQueryInterface for a fake DrvAudio above a
337 * backend.
338 */
339static DECLCALLBACK(void *) audioTestFakeDrvAudioIBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
340{
341 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, pInterface);
342 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIOPORT, &g_AudioTestIHostAudioPort);
343 RTMsgWarning("audioTestFakeDrvAudioIBaseQueryInterface: Unknown interface: %s\n", pszIID);
344 return NULL;
345}
346
347
348/** IBase interface for a fake DrvAudio above a lonesome backend. */
349static PDMIBASE g_AudioTestFakeDrvAudioIBase = { audioTestFakeDrvAudioIBaseQueryInterface };
350
351
352
353/**
354 * Constructs a PDM audio driver instance.
355 *
356 * @returns VBox status code.
357 * @param pDrvStack The stack this is associated with.
358 * @param pDrvReg PDM driver registration record to use for construction.
359 * @param pParentDrvIns The parent driver (if any).
360 * @param ppDrvIns Where to return the driver instance structure.
361 */
362int audioTestDrvConstruct(PAUDIOTESTDRVSTACK pDrvStack, PCPDMDRVREG pDrvReg, PPDMDRVINS pParentDrvIns,
363 PPPDMDRVINS ppDrvIns)
364{
365 /* The destruct function must have valid data to work with. */
366 *ppDrvIns = NULL;
367
368 /*
369 * Check registration structure validation (doesn't need to be too
370 * thorough, PDM check it in detail on every VM startup).
371 */
372 AssertPtrReturn(pDrvReg, VERR_INVALID_POINTER);
373 RTMsgInfo("Initializing backend '%s' ...\n", pDrvReg->szName);
374 AssertPtrReturn(pDrvReg->pfnConstruct, VERR_INVALID_PARAMETER);
375
376 /*
377 * Create the instance data structure.
378 */
379 PPDMDRVINS pDrvIns = (PPDMDRVINS)RTMemAllocZVar(RT_UOFFSETOF_DYN(PDMDRVINS, achInstanceData[pDrvReg->cbInstance]));
380 RTTEST_CHECK_RET(g_hTest, pDrvIns, VERR_NO_MEMORY);
381
382 pDrvIns->u32Version = PDM_DRVINS_VERSION;
383 pDrvIns->iInstance = 0;
384 pDrvIns->pHlpR3 = audioTestFakeGetDrvHlp();
385 pDrvIns->pvInstanceDataR3 = &pDrvIns->achInstanceData[0];
386 pDrvIns->pReg = pDrvReg;
387 pDrvIns->pCfg = (PCFGMNODE)pDrvReg;
388 pDrvIns->Internal.s.pStack = pDrvStack;
389 pDrvIns->pUpBase = NULL;
390 pDrvIns->pDownBase = NULL;
391 if (pParentDrvIns)
392 {
393 Assert(pParentDrvIns->pDownBase == NULL);
394 pParentDrvIns->pDownBase = &pDrvIns->IBase;
395 pDrvIns->pUpBase = &pParentDrvIns->IBase;
396 }
397 else if (strcmp(pDrvReg->szName, "AUDIO") == 0)
398 pDrvIns->pUpBase = &g_AudioTestFakeDeviceIBase;
399 else
400 pDrvIns->pUpBase = &g_AudioTestFakeDrvAudioIBase;
401
402 /*
403 * Invoke the constructor.
404 */
405 int rc = pDrvReg->pfnConstruct(pDrvIns, pDrvIns->pCfg, 0 /*fFlags*/);
406 if (RT_SUCCESS(rc))
407 {
408 *ppDrvIns = pDrvIns;
409 return VINF_SUCCESS;
410 }
411
412 RTTestFailed(g_hTest, "Failed to construct audio driver '%s': %Rrc", pDrvReg->szName, rc);
413 if (pDrvReg->pfnDestruct)
414 pDrvReg->pfnDestruct(pDrvIns);
415 RTMemFree(pDrvIns);
416 return rc;
417}
418
419
420/**
421 * Destructs a PDM audio driver instance.
422 *
423 * @param pDrvIns Driver instance to destruct.
424 */
425static void audioTestDrvDestruct(PPDMDRVINS pDrvIns)
426{
427 if (pDrvIns)
428 {
429 Assert(pDrvIns->u32Version == PDM_DRVINS_VERSION);
430
431 if (pDrvIns->pReg->pfnDestruct)
432 pDrvIns->pReg->pfnDestruct(pDrvIns);
433
434 pDrvIns->u32Version = 0;
435 pDrvIns->pReg = NULL;
436 RTMemFree(pDrvIns);
437 }
438}
439
440
441/**
442 * Sends the PDM driver a power off notification.
443 *
444 * @param pDrvIns Driver instance to notify.
445 */
446static void audioTestDrvNotifyPowerOff(PPDMDRVINS pDrvIns)
447{
448 if (pDrvIns)
449 {
450 Assert(pDrvIns->u32Version == PDM_DRVINS_VERSION);
451 if (pDrvIns->pReg->pfnPowerOff)
452 pDrvIns->pReg->pfnPowerOff(pDrvIns);
453 }
454}
455
456
457/**
458 * Deletes a driver stack.
459 *
460 * This will power off and destroy the drivers.
461 */
462void audioTestDriverStackDelete(PAUDIOTESTDRVSTACK pDrvStack)
463{
464 /*
465 * Do power off notifications (top to bottom).
466 */
467 audioTestDrvNotifyPowerOff(pDrvStack->pDrvAudioIns);
468 audioTestDrvNotifyPowerOff(pDrvStack->pDrvBackendIns);
469
470 /*
471 * Drivers are destroyed from bottom to top (closest to the device).
472 */
473 audioTestDrvDestruct(pDrvStack->pDrvBackendIns);
474 pDrvStack->pDrvBackendIns = NULL;
475 pDrvStack->pIHostAudio = NULL;
476
477 audioTestDrvDestruct(pDrvStack->pDrvAudioIns);
478 pDrvStack->pDrvAudioIns = NULL;
479 pDrvStack->pIAudioConnector = NULL;
480}
481
482
483/**
484 * Initializes a driver stack.
485 *
486 * @returns VBox status code.
487 * @param pDrvStack The driver stack to initialize.
488 * @param pDrvReg The backend driver to use.
489 * @param fWithDrvAudio Whether to include DrvAudio in the stack or not.
490 */
491int audioTestDriverStackInit(PAUDIOTESTDRVSTACK pDrvStack, PCPDMDRVREG pDrvReg, bool fWithDrvAudio)
492{
493 int rc;
494
495 RT_ZERO(*pDrvStack);
496 pDrvStack->pDrvReg = pDrvReg;
497
498 if (!fWithDrvAudio)
499 rc = audioTestDrvConstruct(pDrvStack, pDrvReg, NULL /*pParentDrvIns*/, &pDrvStack->pDrvBackendIns);
500 else
501 {
502 rc = audioTestDrvConstruct(pDrvStack, &g_DrvAUDIO, NULL /*pParentDrvIns*/, &pDrvStack->pDrvAudioIns);
503 if (RT_SUCCESS(rc))
504 {
505 Assert(pDrvStack->pDrvAudioIns);
506 PPDMIBASE const pIBase = &pDrvStack->pDrvAudioIns->IBase;
507 pDrvStack->pIAudioConnector = (PPDMIAUDIOCONNECTOR)pIBase->pfnQueryInterface(pIBase, PDMIAUDIOCONNECTOR_IID);
508 if (pDrvStack->pIAudioConnector)
509 {
510 /* Both input and output is disabled by default. Fix that: */
511 rc = pDrvStack->pIAudioConnector->pfnEnable(pDrvStack->pIAudioConnector, PDMAUDIODIR_OUT, true);
512 if (RT_SUCCESS(rc))
513 rc = pDrvStack->pIAudioConnector->pfnEnable(pDrvStack->pIAudioConnector, PDMAUDIODIR_IN, true);
514 if (RT_FAILURE(rc))
515 {
516 RTTestFailed(g_hTest, "Failed to enabled input and output: %Rrc", rc);
517 audioTestDriverStackDelete(pDrvStack);
518 }
519 }
520 else
521 {
522 RTTestFailed(g_hTest, "Failed to query PDMIAUDIOCONNECTOR");
523 audioTestDriverStackDelete(pDrvStack);
524 rc = VERR_PDM_MISSING_INTERFACE;
525 }
526 }
527 }
528
529 /*
530 * Get the IHostAudio interface and check that the host driver is working.
531 */
532 if (RT_SUCCESS(rc))
533 {
534 PPDMIBASE const pIBase = &pDrvStack->pDrvBackendIns->IBase;
535 pDrvStack->pIHostAudio = (PPDMIHOSTAUDIO)pIBase->pfnQueryInterface(pIBase, PDMIHOSTAUDIO_IID);
536 if (pDrvStack->pIHostAudio)
537 {
538 PDMAUDIOBACKENDSTS enmStatus = pDrvStack->pIHostAudio->pfnGetStatus(pDrvStack->pIHostAudio, PDMAUDIODIR_OUT);
539 if (enmStatus == PDMAUDIOBACKENDSTS_RUNNING)
540 return VINF_SUCCESS;
541
542 RTTestFailed(g_hTest, "Expected backend status RUNNING, got %d instead", enmStatus);
543 }
544 else
545 RTTestFailed(g_hTest, "Failed to query PDMIHOSTAUDIO for '%s'", pDrvReg->szName);
546 audioTestDriverStackDelete(pDrvStack);
547 }
548
549 return rc;
550}
551
552
553/**
554 * Wrapper around PDMIHOSTAUDIO::pfnSetDevice.
555 */
556int audioTestDriverStackSetDevice(PAUDIOTESTDRVSTACK pDrvStack, PDMAUDIODIR enmDir, const char *pszDevId)
557{
558 int rc;
559 if ( pDrvStack->pIHostAudio
560 && pDrvStack->pIHostAudio->pfnSetDevice)
561 rc = pDrvStack->pIHostAudio->pfnSetDevice(pDrvStack->pIHostAudio, enmDir, pszDevId);
562 else if (!pszDevId || *pszDevId)
563 rc = VINF_SUCCESS;
564 else
565 rc = VERR_INVALID_FUNCTION;
566 return rc;
567}
568
569
570/**
571 * Common stream creation code.
572 *
573 * @returns VBox status code.
574 * @param pDrvStack The audio driver stack to create it via.
575 * @param pCfgReq The requested config.
576 * @param ppStream Where to return the stream pointer on success.
577 * @param pCfgAcq Where to return the actual (well, not necessarily when
578 * using DrvAudio, but probably the same) stream config on
579 * success (not used as input).
580 */
581static int audioTestDriverStackStreamCreate(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAMCFG pCfgReq,
582 PPDMAUDIOSTREAM *ppStream, PPDMAUDIOSTREAMCFG pCfgAcq)
583{
584 char szTmp[PDMAUDIOSTRMCFGTOSTRING_MAX + 16];
585 int rc;
586 *ppStream = NULL;
587
588 if (pDrvStack->pIAudioConnector)
589 {
590 /*
591 * DrvAudio does most of the work here.
592 */
593 PDMAUDIOSTREAMCFG CfgGst = *pCfgReq;
594 rc = pDrvStack->pIAudioConnector->pfnStreamCreate(pDrvStack->pIAudioConnector, PDMAUDIOSTREAM_CREATE_F_NO_MIXBUF,
595 pCfgReq, &CfgGst, ppStream);
596 if (RT_SUCCESS(rc))
597 {
598 *pCfgAcq = *pCfgReq; /** @todo PDMIAUDIOCONNECTOR::pfnStreamCreate only does one utterly pointless change to the two configs (enmLayout) from what I can tell... */
599 pCfgAcq->Props = (*ppStream)->Props;
600 RTMsgInfo("Created backend stream: %s\n", PDMAudioStrmCfgToString(pCfgReq, szTmp, sizeof(szTmp)));
601 return rc;
602 }
603 RTTestFailed(g_hTest, "pfnStreamCreate failed: %Rrc", rc);
604 }
605 else
606 {
607 /*
608 * Get the config so we can see how big the PDMAUDIOBACKENDSTREAM
609 * structure actually is for this backend.
610 */
611 PDMAUDIOBACKENDCFG BackendCfg;
612 rc = pDrvStack->pIHostAudio->pfnGetConfig(pDrvStack->pIHostAudio, &BackendCfg);
613 if (RT_SUCCESS(rc))
614 {
615 if (BackendCfg.cbStream >= sizeof(PDMAUDIOBACKENDSTREAM))
616 {
617 /*
618 * Allocate and initialize the stream.
619 */
620 uint32_t const cbStream = sizeof(AUDIOTESTDRVSTACKSTREAM) - sizeof(PDMAUDIOBACKENDSTREAM) + BackendCfg.cbStream;
621 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)RTMemAllocZVar(cbStream);
622 if (pStreamAt)
623 {
624 pStreamAt->Core.uMagic = PDMAUDIOSTREAM_MAGIC;
625 pStreamAt->Core.enmDir = PDMAUDIODIR_OUT;
626 pStreamAt->Core.cbBackend = cbStream;
627 pStreamAt->Core.Props = pCfgReq->Props;
628 RTStrPrintf(pStreamAt->Core.szName, sizeof(pStreamAt->Core.szName), pCfgReq->szName);
629
630 pStreamAt->Backend.uMagic = PDMAUDIOBACKENDSTREAM_MAGIC;
631 pStreamAt->Backend.pStream = &pStreamAt->Core;
632
633 /*
634 * Call the backend to create the stream.
635 */
636 pStreamAt->Cfg = *pCfgReq;
637
638 rc = pDrvStack->pIHostAudio->pfnStreamCreate(pDrvStack->pIHostAudio, &pStreamAt->Backend,
639 pCfgReq, &pStreamAt->Cfg);
640 if (RT_SUCCESS(rc))
641 {
642 pStreamAt->Core.Props = pStreamAt->Cfg.Props;
643 if (g_uVerbosity > 1)
644 RTMsgInfo("Created backend stream: %s\n",
645 PDMAudioStrmCfgToString(&pStreamAt->Cfg, szTmp, sizeof(szTmp)));
646
647 /* Return if stream is ready: */
648 if (rc == VINF_SUCCESS)
649 {
650 *ppStream = &pStreamAt->Core;
651 *pCfgAcq = pStreamAt->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->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->pfnStreamControl(pDrvStack->pIHostAudio, &pStreamAt->Backend, PDMAUDIOSTREAMCMD_ENABLE);
866 if (RT_FAILURE(rc))
867 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamControl/ENABLE 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->pfnStreamControl(pDrvStack->pIHostAudio, &pStreamAt->Backend, PDMAUDIOSTREAMCMD_DISABLE);
889 if (RT_FAILURE(rc))
890 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamControl/DISABLE 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->pfnStreamControl(pDrvStack->pIHostAudio, &pStreamAt->Backend, PDMAUDIOSTREAMCMD_DRAIN);
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 * Tries to capture @a cbBuf bytes of samples in @a pvBuf.
1079 */
1080int audioTestDriverStackStreamCapture(PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream,
1081 void *pvBuf, uint32_t cbBuf, uint32_t *pcbCaptured)
1082{
1083 int rc;
1084 if (pDrvStack->pIAudioConnector)
1085 {
1086 rc = pDrvStack->pIAudioConnector->pfnStreamCapture(pDrvStack->pIAudioConnector, pStream, pvBuf, cbBuf, pcbCaptured);
1087 if (RT_FAILURE(rc))
1088 RTTestFailed(g_hTest, "pfnStreamCapture(,,,%#x) failed: %Rrc", cbBuf, rc);
1089 }
1090 else
1091 {
1092 PAUDIOTESTDRVSTACKSTREAM pStreamAt = (PAUDIOTESTDRVSTACKSTREAM)pStream;
1093 rc = pDrvStack->pIHostAudio->pfnStreamCapture(pDrvStack->pIHostAudio, &pStreamAt->Backend, pvBuf, cbBuf, pcbCaptured);
1094 if (RT_FAILURE(rc))
1095 RTTestFailed(g_hTest, "PDMIHOSTAUDIO::pfnStreamCapture(,,,%#x) failed: %Rrc", cbBuf, rc);
1096 }
1097 return rc;
1098}
1099
1100
1101/*********************************************************************************************************************************
1102* Mixed streams *
1103*********************************************************************************************************************************/
1104
1105/**
1106 * Initializing mixing for a stream.
1107 *
1108 * This can be used as a do-nothing wrapper for the stack.
1109 *
1110 * @returns VBox status code.
1111 * @param pMix The mixing state.
1112 * @param pStream The stream to mix to/from.
1113 * @param pProps The mixer properties. Pass NULL for no mixing, just
1114 * wrap the driver stack functionality.
1115 * @param cMsBuffer The buffer size.
1116 */
1117int AudioTestMixStreamInit(PAUDIOTESTDRVMIXSTREAM pMix, PAUDIOTESTDRVSTACK pDrvStack, PPDMAUDIOSTREAM pStream,
1118 PCPDMAUDIOPCMPROPS pProps, uint32_t cMsBuffer)
1119{
1120 RT_ZERO(*pMix);
1121
1122 AssertReturn(pDrvStack, VERR_INVALID_PARAMETER);
1123 AssertReturn(pStream, VERR_INVALID_PARAMETER);
1124
1125 pMix->pDrvStack = pDrvStack;
1126 pMix->pStream = pStream;
1127 if (!pProps)
1128 {
1129 pMix->pProps = &pStream->Props;
1130 return VINF_SUCCESS;
1131 }
1132
1133 /*
1134 * Okay, we're doing mixing so we need to set up the mixer buffer
1135 * and associated states.
1136 */
1137 pMix->fDoMixing = true;
1138 int rc = AudioMixBufInit(&pMix->MixBuf, "mixer", pProps, PDMAudioPropsMilliToFrames(pProps, cMsBuffer));
1139 if (RT_SUCCESS(rc))
1140 {
1141 pMix->pProps = &pMix->MixBuf.Props;
1142
1143 if (pStream->enmDir == PDMAUDIODIR_IN)
1144 {
1145 rc = AudioMixBufInitPeekState(&pMix->MixBuf, &pMix->PeekState, &pMix->MixBuf.Props);
1146 if (RT_SUCCESS(rc))
1147 {
1148 rc = AudioMixBufInitWriteState(&pMix->MixBuf, &pMix->WriteState, &pStream->Props);
1149 if (RT_SUCCESS(rc))
1150 return rc;
1151 }
1152 }
1153 else if (pStream->enmDir == PDMAUDIODIR_OUT)
1154 {
1155 rc = AudioMixBufInitWriteState(&pMix->MixBuf, &pMix->WriteState, &pMix->MixBuf.Props);
1156 if (RT_SUCCESS(rc))
1157 {
1158 rc = AudioMixBufInitPeekState(&pMix->MixBuf, &pMix->PeekState, &pStream->Props);
1159 if (RT_SUCCESS(rc))
1160 return rc;
1161 }
1162 }
1163 else
1164 {
1165 RTTestFailed(g_hTest, "Bogus stream direction!");
1166 rc = VERR_INVALID_STATE;
1167 }
1168 }
1169 else
1170 RTTestFailed(g_hTest, "AudioMixBufInit failed: %Rrc", rc);
1171 RT_ZERO(*pMix);
1172 return rc;
1173}
1174
1175
1176/**
1177 * Terminate mixing (leaves the stream untouched).
1178 *
1179 * @param pMix The mixing state.
1180 */
1181void AudioTestMixStreamTerm(PAUDIOTESTDRVMIXSTREAM pMix)
1182{
1183 if (pMix->fDoMixing)
1184 {
1185 AudioMixBufTerm(&pMix->MixBuf);
1186 pMix->pStream = NULL;
1187 }
1188 RT_ZERO(*pMix);
1189}
1190
1191
1192/**
1193 * Worker that transports data between the mixer buffer and the drivers.
1194 *
1195 * @returns VBox status code.
1196 * @param pMix The mixer stream setup to do transfers for.
1197 */
1198static int audioTestMixStreamTransfer(PAUDIOTESTDRVMIXSTREAM pMix)
1199{
1200 uint8_t abBuf[16384];
1201 if (pMix->pStream->enmDir == PDMAUDIODIR_IN)
1202 {
1203 //uint32_t const cbBuf = PDMAudioPropsFloorBytesToFrame(&pMix->pStream->Props, sizeof(abBuf));
1204
1205 }
1206 else
1207 {
1208 /*
1209 * The goal here is to empty the mixer buffer by transfering all
1210 * the data to the drivers.
1211 */
1212 uint32_t const cbBuf = PDMAudioPropsFloorBytesToFrame(&pMix->MixBuf.Props, sizeof(abBuf));
1213 for (;;)
1214 {
1215 uint32_t cFrames = AudioMixBufUsed(&pMix->MixBuf);
1216 if (!cFrames)
1217 break;
1218
1219 uint32_t cbWritable = audioTestDriverStackStreamGetWritable(pMix->pDrvStack, pMix->pStream);
1220 if (!cbWritable)
1221 break;
1222
1223 uint32_t cSrcFramesPeeked;
1224 uint32_t cbDstPeeked;
1225 AudioMixBufPeek(&pMix->MixBuf, 0 /*offSrcFrame*/, cFrames, &cSrcFramesPeeked,
1226 &pMix->PeekState, abBuf, RT_MIN(cbBuf, cbWritable), &cbDstPeeked);
1227 AudioMixBufAdvance(&pMix->MixBuf, cSrcFramesPeeked);
1228
1229 if (!cbDstPeeked)
1230 break;
1231
1232 uint32_t offBuf = 0;
1233 while (offBuf < cbDstPeeked)
1234 {
1235 uint32_t cbPlayed = 0;
1236 int rc = audioTestDriverStackStreamPlay(pMix->pDrvStack, pMix->pStream,
1237 &abBuf[offBuf], cbDstPeeked - offBuf, &cbPlayed);
1238 if (RT_FAILURE(rc))
1239 return rc;
1240 if (!cbPlayed)
1241 RTThreadSleep(1);
1242 offBuf += cbPlayed;
1243 }
1244 }
1245 }
1246 return VINF_SUCCESS;
1247}
1248
1249
1250/**
1251 * Same as audioTestDriverStackStreamDrain.
1252 */
1253int AudioTestMixStreamDrain(PAUDIOTESTDRVMIXSTREAM pMix, bool fSync)
1254{
1255 /*
1256 * If we're mixing, we must first make sure the buffer is empty.
1257 */
1258 if (pMix->fDoMixing)
1259 {
1260 audioTestMixStreamTransfer(pMix);
1261 while (AudioMixBufUsed(&pMix->MixBuf) > 0)
1262 {
1263 RTThreadSleep(1);
1264 audioTestMixStreamTransfer(pMix);
1265 }
1266 }
1267
1268 /*
1269 * Then we do the regular workt.
1270 */
1271 return audioTestDriverStackStreamDrain(pMix->pDrvStack, pMix->pStream, fSync);
1272}
1273
1274
1275/**
1276 * Same as audioTestDriverStackStreamEnable.
1277 */
1278int AudioTestMixStreamEnable(PAUDIOTESTDRVMIXSTREAM pMix)
1279{
1280 return audioTestDriverStackStreamEnable(pMix->pDrvStack, pMix->pStream);
1281}
1282
1283
1284/**
1285 * Same as audioTestDriverStackStreamGetWritable
1286 */
1287uint32_t AudioTestMixStreamGetWritable(PAUDIOTESTDRVMIXSTREAM pMix)
1288{
1289 if (!pMix->fDoMixing)
1290 return audioTestDriverStackStreamGetWritable(pMix->pDrvStack, pMix->pStream);
1291 uint32_t cbRet = AudioMixBufFreeBytes(&pMix->MixBuf);
1292 if (!cbRet)
1293 {
1294 audioTestMixStreamTransfer(pMix);
1295 cbRet = AudioMixBufFreeBytes(&pMix->MixBuf);
1296 }
1297 return cbRet;
1298}
1299
1300
1301/**
1302 * Same as audioTestDriverStackStreamIsOkay.
1303 */
1304bool AudioTestMixStreamIsOkay(PAUDIOTESTDRVMIXSTREAM pMix)
1305{
1306 return audioTestDriverStackStreamIsOkay(pMix->pDrvStack, pMix->pStream);
1307}
1308
1309
1310/**
1311 * Same as audioTestDriverStackStreamPlay.
1312 */
1313int AudioTestMixStreamPlay(PAUDIOTESTDRVMIXSTREAM pMix, void const *pvBuf, uint32_t cbBuf, uint32_t *pcbPlayed)
1314{
1315 if (!pMix->fDoMixing)
1316 return audioTestDriverStackStreamPlay(pMix->pDrvStack, pMix->pStream, pvBuf, cbBuf, pcbPlayed);
1317
1318 *pcbPlayed = 0;
1319
1320 int rc = audioTestMixStreamTransfer(pMix);
1321 if (RT_FAILURE(rc))
1322 return rc;
1323
1324 uint32_t const cbFrame = PDMAudioPropsFrameSize(&pMix->MixBuf.Props);
1325 while (cbBuf >= cbFrame)
1326 {
1327 uint32_t const cFrames = AudioMixBufFree(&pMix->MixBuf);
1328 if (!cFrames)
1329 break;
1330 uint32_t cbToWrite = PDMAudioPropsFramesToBytes(&pMix->MixBuf.Props, cFrames);
1331 cbToWrite = RT_MIN(cbToWrite, cbBuf);
1332 cbToWrite = PDMAudioPropsFloorBytesToFrame(&pMix->MixBuf.Props, cbToWrite);
1333
1334 uint32_t cFramesWritten = 0;
1335 AudioMixBufWrite(&pMix->MixBuf, &pMix->WriteState, pvBuf, cbToWrite, 0 /*offDstFrame*/, cFrames, &cFramesWritten);
1336 Assert(cFramesWritten == PDMAudioPropsBytesToFrames(&pMix->MixBuf.Props, cbToWrite));
1337 AudioMixBufCommit(&pMix->MixBuf, cFramesWritten);
1338
1339 *pcbPlayed += cbToWrite;
1340 cbBuf -= cbToWrite;
1341 pvBuf = (uint8_t const *)pvBuf + cbToWrite;
1342
1343 rc = audioTestMixStreamTransfer(pMix);
1344 if (RT_FAILURE(rc))
1345 return *pcbPlayed ? VINF_SUCCESS : rc;
1346 }
1347
1348 return VINF_SUCCESS;
1349}
1350
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