VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/vkatCommon.cpp@ 91413

Last change on this file since 91413 was 91198, checked in by vboxsync, 3 years ago

Audio/Validation Kit: Some more diagnostics to find out why some testbox guests refuse to play any test tones. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.2 KB
Line 
1/* $Id: vkatCommon.cpp 91198 2021-09-10 12:55:54Z vboxsync $ */
2/** @file
3 * Validation Kit Audio Test (VKAT) - Self test 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#define LOG_GROUP LOG_GROUP_AUDIO_TEST
32#include <iprt/log.h>
33
34#include <iprt/ctype.h>
35#include <iprt/dir.h>
36#include <iprt/errcore.h>
37#include <iprt/getopt.h>
38#include <iprt/message.h>
39#include <iprt/rand.h>
40#include <iprt/test.h>
41
42#include "Audio/AudioHlp.h"
43#include "Audio/AudioTest.h"
44#include "Audio/AudioTestService.h"
45#include "Audio/AudioTestServiceClient.h"
46
47#include "vkatInternal.h"
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53
54
55/*********************************************************************************************************************************
56* Internal Functions *
57*********************************************************************************************************************************/
58static int audioTestStreamInit(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream, PDMAUDIODIR enmDir, PCPDMAUDIOPCMPROPS pProps, bool fWithMixer, uint32_t cMsBufferSize, uint32_t cMsPreBuffer, uint32_t cMsSchedulingHint);
59static int audioTestStreamDestroy(PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream);
60
61
62/*********************************************************************************************************************************
63* Device enumeration + handling. *
64*********************************************************************************************************************************/
65
66/**
67 * Enumerates audio devices and optionally searches for a specific device.
68 *
69 * @returns VBox status code.
70 * @param pDrvStack Driver stack to use for enumeration.
71 * @param pszDev Device name to search for. Can be NULL if the default device shall be used.
72 * @param ppDev Where to return the pointer of the device enumeration of \a pTstEnv when a
73 * specific device was found.
74 */
75int audioTestDevicesEnumerateAndCheck(PAUDIOTESTDRVSTACK pDrvStack, const char *pszDev, PPDMAUDIOHOSTDEV *ppDev)
76{
77 RTTestSubF(g_hTest, "Enumerating audio devices and checking for device '%s'", pszDev && *pszDev ? pszDev : "[Default]");
78
79 if (!pDrvStack->pIHostAudio->pfnGetDevices)
80 {
81 RTTestSkipped(g_hTest, "Backend does not support device enumeration, skipping");
82 return VINF_NOT_SUPPORTED;
83 }
84
85 Assert(pszDev == NULL || ppDev);
86
87 if (ppDev)
88 *ppDev = NULL;
89
90 int rc = pDrvStack->pIHostAudio->pfnGetDevices(pDrvStack->pIHostAudio, &pDrvStack->DevEnum);
91 if (RT_SUCCESS(rc))
92 {
93 PPDMAUDIOHOSTDEV pDev;
94 RTListForEach(&pDrvStack->DevEnum.LstDevices, pDev, PDMAUDIOHOSTDEV, ListEntry)
95 {
96 char szFlags[PDMAUDIOHOSTDEV_MAX_FLAGS_STRING_LEN];
97 if (pDev->pszId)
98 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Device '%s' (ID '%s'):\n", pDev->pszName, pDev->pszId);
99 else
100 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Device '%s':\n", pDev->pszName);
101 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Usage = %s\n", PDMAudioDirGetName(pDev->enmUsage));
102 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Flags = %s\n", PDMAudioHostDevFlagsToString(szFlags, pDev->fFlags));
103 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Input channels = %RU8\n", pDev->cMaxInputChannels);
104 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Output channels = %RU8\n", pDev->cMaxOutputChannels);
105
106 if ( (pszDev && *pszDev)
107 && !RTStrCmp(pDev->pszName, pszDev))
108 {
109 *ppDev = pDev;
110 }
111 }
112 }
113 else
114 RTTestFailed(g_hTest, "Enumerating audio devices failed with %Rrc", rc);
115
116 if (RT_SUCCESS(rc))
117 {
118 if ( (pszDev && *pszDev)
119 && *ppDev == NULL)
120 {
121 RTTestFailed(g_hTest, "Audio device '%s' not found", pszDev);
122 rc = VERR_NOT_FOUND;
123 }
124 }
125
126 RTTestSubDone(g_hTest);
127 return rc;
128}
129
130static int audioTestStreamInit(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream,
131 PDMAUDIODIR enmDir, PCPDMAUDIOPCMPROPS pProps, bool fWithMixer,
132 uint32_t cMsBufferSize, uint32_t cMsPreBuffer, uint32_t cMsSchedulingHint)
133{
134 int rc;
135
136 if (enmDir == PDMAUDIODIR_IN)
137 rc = audioTestDriverStackStreamCreateInput(pDrvStack, pProps, cMsBufferSize,
138 cMsPreBuffer, cMsSchedulingHint, &pStream->pStream, &pStream->Cfg);
139 else if (enmDir == PDMAUDIODIR_OUT)
140 rc = audioTestDriverStackStreamCreateOutput(pDrvStack, pProps, cMsBufferSize,
141 cMsPreBuffer, cMsSchedulingHint, &pStream->pStream, &pStream->Cfg);
142 else
143 rc = VERR_NOT_SUPPORTED;
144
145 if (RT_SUCCESS(rc))
146 {
147 if (!pDrvStack->pIAudioConnector)
148 {
149 pStream->pBackend = &((PAUDIOTESTDRVSTACKSTREAM)pStream->pStream)->Backend;
150 }
151 else
152 pStream->pBackend = NULL;
153
154 /*
155 * Automatically enable the mixer if the PCM properties don't match.
156 */
157 if ( !fWithMixer
158 && !PDMAudioPropsAreEqual(pProps, &pStream->Cfg.Props))
159 {
160 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enabling stream mixer\n");
161 fWithMixer = true;
162 }
163
164 rc = AudioTestMixStreamInit(&pStream->Mix, pDrvStack, pStream->pStream,
165 fWithMixer ? pProps : NULL, 100 /* ms */); /** @todo Configure mixer buffer? */
166 }
167
168 if (RT_FAILURE(rc))
169 RTTestFailed(g_hTest, "Initializing %s stream failed with %Rrc", enmDir == PDMAUDIODIR_IN ? "input" : "output", rc);
170
171 return rc;
172}
173
174/**
175 * Destroys an audio test stream.
176 *
177 * @returns VBox status code.
178 * @param pTstEnv Test environment the stream to destroy contains.
179 * @param pStream Audio stream to destroy.
180 */
181static int audioTestStreamDestroy(PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream)
182{
183 int rc = VINF_SUCCESS;
184 if (pStream && pStream->pStream)
185 {
186 /** @todo Anything else to do here, e.g. test if there are left over samples or some such? */
187
188 audioTestDriverStackStreamDestroy(pTstEnv->pDrvStack, pStream->pStream);
189 pStream->pStream = NULL;
190 pStream->pBackend = NULL;
191 }
192
193 AudioTestMixStreamTerm(&pStream->Mix);
194
195 return rc;
196}
197
198
199/*********************************************************************************************************************************
200* Test Primitives *
201*********************************************************************************************************************************/
202
203#if 0 /* Unused */
204/**
205 * Returns a random scheduling hint (in ms).
206 */
207DECLINLINE(uint32_t) audioTestEnvGetRandomSchedulingHint(void)
208{
209 static const unsigned s_aSchedulingHintsMs[] =
210 {
211 10,
212 25,
213 50,
214 100,
215 200,
216 250
217 };
218
219 return s_aSchedulingHintsMs[RTRandU32Ex(0, RT_ELEMENTS(s_aSchedulingHintsMs) - 1)];
220}
221#endif
222
223/**
224 * Plays a test tone on a specific audio test stream.
225 *
226 * @returns VBox status code.
227 * @param pTstEnv Test environment to use for running the test.
228 * Optional and can be NULL (for simple playback only).
229 * @param pStream Stream to use for playing the tone.
230 * @param pParms Tone parameters to use.
231 *
232 * @note Blocking function.
233 */
234int audioTestPlayTone(PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream, PAUDIOTESTTONEPARMS pParms)
235{
236 AUDIOTESTTONE TstTone;
237 AudioTestToneInit(&TstTone, &pStream->Cfg.Props, pParms->dbFreqHz);
238
239 char const *pcszPathOut = NULL;
240 if (pTstEnv)
241 pcszPathOut = pTstEnv->Set.szPathAbs;
242
243 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Playing test tone (tone frequency is %RU16Hz, %RU32ms)\n", (uint16_t)pParms->dbFreqHz, pParms->msDuration);
244 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using %RU32ms stream scheduling hint\n", pStream->Cfg.Device.cMsSchedulingHint);
245 if (pcszPathOut)
246 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Writing to '%s'\n", pcszPathOut);
247
248 int rc;
249
250 /** @todo Use .WAV here? */
251 AUDIOTESTOBJ Obj;
252 RT_ZERO(Obj); /* Shut up MSVC. */
253 if (pTstEnv)
254 {
255 rc = AudioTestSetObjCreateAndRegister(&pTstEnv->Set, "guest-tone-play.pcm", &Obj);
256 AssertRCReturn(rc, rc);
257 }
258
259 rc = AudioTestMixStreamEnable(&pStream->Mix);
260 if ( RT_SUCCESS(rc)
261 && AudioTestMixStreamIsOkay(&pStream->Mix))
262 {
263 uint8_t abBuf[_4K];
264
265 uint32_t cbToPlayTotal = PDMAudioPropsMilliToBytes(&pStream->Cfg.Props, pParms->msDuration);
266 AssertStmt(cbToPlayTotal, rc = VERR_INVALID_PARAMETER);
267 uint32_t cbPlayedTotal = 0;
268
269 RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Playing %RU32 bytes total\n", cbToPlayTotal);
270
271 if (pTstEnv)
272 {
273 AudioTestObjAddMetadataStr(Obj, "stream_to_play_bytes=%RU32\n", cbToPlayTotal);
274 AudioTestObjAddMetadataStr(Obj, "stream_period_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesPeriod);
275 AudioTestObjAddMetadataStr(Obj, "stream_buffer_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesBufferSize);
276 AudioTestObjAddMetadataStr(Obj, "stream_prebuf_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesPreBuffering);
277 /* Note: This mostly is provided by backend (e.g. PulseAudio / ALSA / ++) and
278 * has nothing to do with the device emulation scheduling hint. */
279 AudioTestObjAddMetadataStr(Obj, "device_scheduling_hint_ms=%RU32\n", pStream->Cfg.Device.cMsSchedulingHint);
280 }
281
282 PAUDIOTESTDRVMIXSTREAM pMix = &pStream->Mix;
283
284 uint32_t const cbPreBuffer = PDMAudioPropsFramesToBytes(pMix->pProps, pStream->Cfg.Backend.cFramesPreBuffering);
285 uint64_t const nsStarted = RTTimeNanoTS();
286 uint64_t nsDonePreBuffering = 0;
287
288 uint64_t offStream = 0;
289 uint64_t nsTimeout = RT_MS_5MIN_64 * RT_NS_1MS;
290 uint64_t nsLastMsgCantWrite = 0; /* Timestamp (in ns) when the last message of an unwritable stream was shown. */
291
292 while (cbPlayedTotal < cbToPlayTotal)
293 {
294 uint64_t const nsNow = RTTimeNanoTS();
295
296 /* Pace ourselves a little. */
297 if (offStream >= cbPreBuffer)
298 {
299 if (!nsDonePreBuffering)
300 nsDonePreBuffering = nsNow;
301 uint64_t const cNsWritten = PDMAudioPropsBytesToNano64(pMix->pProps, offStream - cbPreBuffer);
302 uint64_t const cNsElapsed = nsNow - nsStarted;
303 if (cNsWritten > cNsElapsed + RT_NS_10MS)
304 RTThreadSleep((cNsWritten - cNsElapsed - RT_NS_10MS / 2) / RT_NS_1MS);
305 }
306
307 uint32_t cbPlayed = 0;
308 uint32_t const cbCanWrite = AudioTestMixStreamGetWritable(&pStream->Mix);
309 if (cbCanWrite)
310 {
311 if (g_uVerbosity)
312 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Stream is writable with %RU32ms (%RU32 bytes)\n",
313 PDMAudioPropsBytesToMilli(pMix->pProps, cbCanWrite), cbCanWrite);
314
315 uint32_t const cbToGenerate = RT_MIN(RT_MIN(cbToPlayTotal - cbPlayedTotal, sizeof(abBuf)), cbCanWrite);
316 uint32_t cbToPlay;
317 rc = AudioTestToneGenerate(&TstTone, abBuf, cbToGenerate, &cbToPlay);
318 if (RT_SUCCESS(rc))
319 {
320 if (pTstEnv)
321 {
322 /* Write stuff to disk before trying to play it. Help analysis later. */
323 rc = AudioTestObjWrite(Obj, abBuf, cbToPlay);
324 }
325 if (RT_SUCCESS(rc))
326 {
327 rc = AudioTestMixStreamPlay(&pStream->Mix, abBuf, cbToPlay, &cbPlayed);
328 if (RT_SUCCESS(rc))
329 {
330 AssertBreakStmt(cbPlayed <= cbToPlay, rc = VERR_TOO_MUCH_DATA);
331
332 offStream += cbPlayed;
333
334 if (cbPlayed != cbToPlay)
335 RTTestFailed(g_hTest, "Only played %RU32/%RU32 bytes", cbPlayed, cbToPlay);
336 }
337 }
338 }
339
340 if (RT_FAILURE(rc))
341 break;
342
343 nsLastMsgCantWrite = 0;
344 }
345 else if (AudioTestMixStreamIsOkay(&pStream->Mix))
346 {
347 RTMSINTERVAL const msSleep = RT_MIN(RT_MAX(1, pStream->Cfg.Device.cMsSchedulingHint), 256);
348
349 if (!nsLastMsgCantWrite || nsNow - nsLastMsgCantWrite > RT_NS_10SEC) /* Don't spam the output too much. */
350 {
351 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Waiting %RU32ms for stream to be writable again ...\n", msSleep);
352 nsLastMsgCantWrite = nsNow;
353 }
354
355 RTThreadSleep(msSleep);
356 }
357 else
358 AssertFailedBreakStmt(rc = VERR_AUDIO_STREAM_NOT_READY);
359
360 cbPlayedTotal += cbPlayed;
361 AssertBreakStmt(cbPlayedTotal <= cbToPlayTotal, VERR_BUFFER_OVERFLOW);
362
363 /* Fail-safe in case something screwed up while playing back. */
364 uint64_t const cNsElapsed = nsNow - nsStarted;
365 if (cNsElapsed > nsTimeout)
366 {
367 RTTestFailed(g_hTest, "Playback took too long (running %RU64 vs. timeout %RU64), aborting\n", cNsElapsed, nsTimeout);
368 rc = VERR_TIMEOUT;
369 }
370
371 if (RT_FAILURE(rc))
372 break;
373 }
374
375 if (cbPlayedTotal != cbToPlayTotal)
376 RTTestFailed(g_hTest, "Playback ended unexpectedly (%RU32/%RU32 played)\n", cbPlayedTotal, cbToPlayTotal);
377
378 if (RT_SUCCESS(rc))
379 {
380 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Draining stream ...\n");
381 rc = AudioTestMixStreamDrain(&pStream->Mix, true /*fSync*/);
382 }
383 }
384 else
385 rc = VERR_AUDIO_STREAM_NOT_READY;
386
387 if (pTstEnv)
388 {
389 int rc2 = AudioTestObjClose(Obj);
390 if (RT_SUCCESS(rc))
391 rc = rc2;
392 }
393
394 if (RT_FAILURE(rc))
395 RTTestFailed(g_hTest, "Playing tone failed with %Rrc\n", rc);
396
397 return rc;
398}
399
400/**
401 * Records a test tone from a specific audio test stream.
402 *
403 * @returns VBox status code.
404 * @param pTstEnv Test environment to use for running the test.
405 * @param pStream Stream to use for recording the tone.
406 * @param pParms Tone parameters to use.
407 *
408 * @note Blocking function.
409 */
410static int audioTestRecordTone(PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream, PAUDIOTESTTONEPARMS pParms)
411{
412 const char *pcszPathOut = pTstEnv->Set.szPathAbs;
413
414 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Recording test tone (tone frequency is %RU16Hz, %RU32ms)\n", (uint16_t)pParms->dbFreqHz, pParms->msDuration);
415 RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Writing to '%s'\n", pcszPathOut);
416
417 /** @todo Use .WAV here? */
418 AUDIOTESTOBJ Obj;
419 int rc = AudioTestSetObjCreateAndRegister(&pTstEnv->Set, "guest-tone-rec.pcm", &Obj);
420 AssertRCReturn(rc, rc);
421
422 PAUDIOTESTDRVMIXSTREAM pMix = &pStream->Mix;
423
424 rc = AudioTestMixStreamEnable(pMix);
425 if (RT_SUCCESS(rc))
426 {
427 uint64_t cbToRecTotal = PDMAudioPropsMilliToBytes(&pStream->Cfg.Props, pParms->msDuration);
428
429 RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Recording %RU32 bytes total\n", cbToRecTotal);
430
431 AudioTestObjAddMetadataStr(Obj, "stream_to_record_bytes=%RU32\n", cbToRecTotal);
432 AudioTestObjAddMetadataStr(Obj, "stream_buffer_size_ms=%RU32\n", pTstEnv->cMsBufferSize);
433 AudioTestObjAddMetadataStr(Obj, "stream_prebuf_size_ms=%RU32\n", pTstEnv->cMsPreBuffer);
434 /* Note: This mostly is provided by backend (e.g. PulseAudio / ALSA / ++) and
435 * has nothing to do with the device emulation scheduling hint. */
436 AudioTestObjAddMetadataStr(Obj, "device_scheduling_hint_ms=%RU32\n", pTstEnv->cMsSchedulingHint);
437
438 uint8_t abSamples[16384];
439 uint32_t const cbSamplesAligned = PDMAudioPropsFloorBytesToFrame(pMix->pProps, sizeof(abSamples));
440 uint64_t cbRecTotal = 0;
441
442 uint64_t const nsStarted = RTTimeNanoTS();
443
444 uint64_t nsTimeout = RT_MS_5MIN_64 * RT_NS_1MS;
445 uint64_t nsLastMsgCantRead = 0; /* Timestamp (in ns) when the last message of an unreadable stream was shown. */
446
447 while (!g_fTerminate && cbRecTotal < cbToRecTotal)
448 {
449 uint64_t const nsNow = RTTimeNanoTS();
450
451 /*
452 * Anything we can read?
453 */
454 uint32_t const cbCanRead = AudioTestMixStreamGetReadable(pMix);
455 if (cbCanRead)
456 {
457 if (g_uVerbosity)
458 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Stream is readable with %RU32ms (%RU32 bytes)\n",
459 PDMAudioPropsBytesToMilli(pMix->pProps, cbCanRead), cbCanRead);
460
461 uint32_t const cbToRead = RT_MIN(cbCanRead, cbSamplesAligned);
462 uint32_t cbRecorded = 0;
463 rc = AudioTestMixStreamCapture(pMix, abSamples, cbToRead, &cbRecorded);
464 if (RT_SUCCESS(rc))
465 {
466 if (cbRecorded)
467 {
468 rc = AudioTestObjWrite(Obj, abSamples, cbRecorded);
469 if (RT_SUCCESS(rc))
470 {
471 cbRecTotal += cbRecorded;
472
473 /** @todo Clamp result? */
474 }
475 }
476 }
477 }
478 else if (AudioTestMixStreamIsOkay(pMix))
479 {
480 RTMSINTERVAL const msSleep = RT_MIN(RT_MAX(1, pStream->Cfg.Device.cMsSchedulingHint), 256);
481
482 if (!nsLastMsgCantRead || nsNow - nsLastMsgCantRead > RT_NS_10SEC) /* Don't spam the output too much. */
483 {
484 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Waiting %RU32ms for stream to be readable again ...\n", msSleep);
485 nsLastMsgCantRead = nsNow;
486 }
487
488 RTThreadSleep(msSleep);
489 }
490
491 /* Fail-safe in case something screwed up while playing back. */
492 uint64_t const cNsElapsed = nsNow - nsStarted;
493 if (cNsElapsed > nsTimeout)
494 {
495 RTTestFailed(g_hTest, "Recording took too long (running %RU64 vs. timeout %RU64), aborting\n", cNsElapsed, nsTimeout);
496 rc = VERR_TIMEOUT;
497 }
498
499 if (RT_FAILURE(rc))
500 break;
501 }
502
503 if (cbRecTotal != cbToRecTotal)
504 RTTestFailed(g_hTest, "Recording ended unexpectedly (%RU32/%RU32 recorded)\n", cbRecTotal, cbToRecTotal);
505
506 int rc2 = AudioTestMixStreamDisable(pMix);
507 if (RT_SUCCESS(rc))
508 rc = rc2;
509 }
510
511 int rc2 = AudioTestObjClose(Obj);
512 if (RT_SUCCESS(rc))
513 rc = rc2;
514
515 if (RT_FAILURE(rc))
516 RTTestFailed(g_hTest, "Recording tone done failed with %Rrc\n", rc);
517
518 return rc;
519}
520
521
522/*********************************************************************************************************************************
523* ATS Callback Implementations *
524*********************************************************************************************************************************/
525
526/** @copydoc ATSCALLBACKS::pfnHowdy
527 *
528 * @note Runs as part of the guest ATS.
529 */
530static DECLCALLBACK(int) audioTestGstAtsHowdyCallback(void const *pvUser)
531{
532 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
533
534 AssertReturn(pCtx->cClients <= UINT8_MAX - 1, VERR_BUFFER_OVERFLOW);
535
536 pCtx->cClients++;
537
538 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "New client connected, now %RU8 total\n", pCtx->cClients);
539
540 return VINF_SUCCESS;
541}
542
543/** @copydoc ATSCALLBACKS::pfnBye
544 *
545 * @note Runs as part of the guest ATS.
546 */
547static DECLCALLBACK(int) audioTestGstAtsByeCallback(void const *pvUser)
548{
549 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
550
551 AssertReturn(pCtx->cClients, VERR_WRONG_ORDER);
552 pCtx->cClients--;
553
554 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Client wants to disconnect, %RU8 remaining\n", pCtx->cClients);
555
556 if (0 == pCtx->cClients) /* All clients disconnected? Tear things down. */
557 {
558 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Last client disconnected, terminating server ...\n");
559 ASMAtomicWriteBool(&g_fTerminate, true);
560 }
561
562 return VINF_SUCCESS;
563}
564
565/** @copydoc ATSCALLBACKS::pfnTestSetBegin
566 *
567 * @note Runs as part of the guest ATS.
568 */
569static DECLCALLBACK(int) audioTestGstAtsTestSetBeginCallback(void const *pvUser, const char *pszTag)
570{
571 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
572 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
573
574 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for beginning test set '%s' in '%s'\n", pszTag, pTstEnv->szPathTemp);
575
576 return AudioTestSetCreate(&pTstEnv->Set, pTstEnv->szPathTemp, pszTag);
577}
578
579/** @copydoc ATSCALLBACKS::pfnTestSetEnd
580 *
581 * @note Runs as part of the guest ATS.
582 */
583static DECLCALLBACK(int) audioTestGstAtsTestSetEndCallback(void const *pvUser, const char *pszTag)
584{
585 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
586 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
587
588 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for ending test set '%s'\n", pszTag);
589
590 /* Pack up everything to be ready for transmission. */
591 return audioTestEnvPrologue(pTstEnv, true /* fPack */, pCtx->szTestSetArchive, sizeof(pCtx->szTestSetArchive));
592}
593
594/** @copydoc ATSCALLBACKS::pfnTonePlay
595 *
596 * @note Runs as part of the guest ATS.
597 */
598static DECLCALLBACK(int) audioTestGstAtsTonePlayCallback(void const *pvUser, PAUDIOTESTTONEPARMS pToneParms)
599{
600 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
601 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
602
603 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for playing test tone #%RU32 (%RU16Hz, %RU32ms) ...\n",
604 pToneParms->Hdr.idxSeq, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
605
606 char szTimeCreated[RTTIME_STR_LEN];
607 RTTimeToString(&pToneParms->Hdr.tsCreated, szTimeCreated, sizeof(szTimeCreated));
608 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test created (caller UTC): %s\n", szTimeCreated);
609
610 const PAUDIOTESTSTREAM pTstStream = &pTstEnv->aStreams[0]; /** @todo Make this dynamic. */
611
612 int rc = audioTestStreamInit(pTstEnv->pDrvStack, pTstStream, PDMAUDIODIR_OUT, &pTstEnv->Props, false /* fWithMixer */,
613 pTstEnv->cMsBufferSize, pTstEnv->cMsPreBuffer, pTstEnv->cMsSchedulingHint);
614 if (RT_SUCCESS(rc))
615 {
616 AUDIOTESTPARMS TstParms;
617 RT_ZERO(TstParms);
618 TstParms.enmType = AUDIOTESTTYPE_TESTTONE_PLAY;
619 TstParms.enmDir = PDMAUDIODIR_OUT;
620 TstParms.TestTone = *pToneParms;
621
622 PAUDIOTESTENTRY pTst;
623 rc = AudioTestSetTestBegin(&pTstEnv->Set, "Playing test tone", &TstParms, &pTst);
624 if (RT_SUCCESS(rc))
625 {
626 rc = audioTestPlayTone(pTstEnv, pTstStream, pToneParms);
627 if (RT_SUCCESS(rc))
628 {
629 AudioTestSetTestDone(pTst);
630 }
631 else
632 AudioTestSetTestFailed(pTst, rc, "Playing tone failed");
633 }
634
635 int rc2 = audioTestStreamDestroy(pTstEnv, pTstStream);
636 if (RT_SUCCESS(rc))
637 rc = rc2;
638 }
639 else
640 RTTestFailed(g_hTest, "Error creating output stream, rc=%Rrc\n", rc);
641
642 return rc;
643}
644
645/** @copydoc ATSCALLBACKS::pfnToneRecord */
646static DECLCALLBACK(int) audioTestGstAtsToneRecordCallback(void const *pvUser, PAUDIOTESTTONEPARMS pToneParms)
647{
648 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
649 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
650
651 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for recording test tone #%RU32 (%RU32ms) ...\n",
652 pToneParms->Hdr.idxSeq, pToneParms->msDuration);
653
654 char szTimeCreated[RTTIME_STR_LEN];
655 RTTimeToString(&pToneParms->Hdr.tsCreated, szTimeCreated, sizeof(szTimeCreated));
656 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test created (caller UTC): %s\n", szTimeCreated);
657
658 const PAUDIOTESTSTREAM pTstStream = &pTstEnv->aStreams[0]; /** @todo Make this dynamic. */
659
660 int rc = audioTestStreamInit(pTstEnv->pDrvStack, pTstStream, PDMAUDIODIR_IN, &pTstEnv->Props, false /* fWithMixer */,
661 pTstEnv->cMsBufferSize, pTstEnv->cMsPreBuffer, pTstEnv->cMsSchedulingHint);
662 if (RT_SUCCESS(rc))
663 {
664 AUDIOTESTPARMS TstParms;
665 RT_ZERO(TstParms);
666 TstParms.enmType = AUDIOTESTTYPE_TESTTONE_RECORD;
667 TstParms.enmDir = PDMAUDIODIR_IN;
668 TstParms.Props = pToneParms->Props;
669 TstParms.TestTone = *pToneParms;
670
671 PAUDIOTESTENTRY pTst;
672 rc = AudioTestSetTestBegin(&pTstEnv->Set, "Recording test tone from host", &TstParms, &pTst);
673 if (RT_SUCCESS(rc))
674 {
675 rc = audioTestRecordTone(pTstEnv, pTstStream, pToneParms);
676 if (RT_SUCCESS(rc))
677 {
678 AudioTestSetTestDone(pTst);
679 }
680 else
681 AudioTestSetTestFailed(pTst, rc, "Recording tone failed");
682 }
683
684 int rc2 = audioTestStreamDestroy(pTstEnv, pTstStream);
685 if (RT_SUCCESS(rc))
686 rc = rc2;
687 }
688 else
689 RTTestFailed(g_hTest, "Error creating input stream, rc=%Rrc\n", rc);
690
691 return rc;
692}
693
694/** @copydoc ATSCALLBACKS::pfnTestSetSendBegin */
695static DECLCALLBACK(int) audioTestGstAtsTestSetSendBeginCallback(void const *pvUser, const char *pszTag)
696{
697 RT_NOREF(pszTag);
698
699 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
700
701 if (!RTFileExists(pCtx->szTestSetArchive)) /* Has the archive successfully been created yet? */
702 return VERR_WRONG_ORDER;
703
704 int rc = RTFileOpen(&pCtx->hTestSetArchive, pCtx->szTestSetArchive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
705 if (RT_SUCCESS(rc))
706 {
707 uint64_t uSize;
708 rc = RTFileQuerySize(pCtx->hTestSetArchive, &uSize);
709 if (RT_SUCCESS(rc))
710 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Sending test set '%s' (%zu bytes)\n", pCtx->szTestSetArchive, uSize);
711 }
712
713 return rc;
714}
715
716/** @copydoc ATSCALLBACKS::pfnTestSetSendRead */
717static DECLCALLBACK(int) audioTestGstAtsTestSetSendReadCallback(void const *pvUser,
718 const char *pszTag, void *pvBuf, size_t cbBuf, size_t *pcbRead)
719{
720 RT_NOREF(pszTag);
721
722 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
723
724 return RTFileRead(pCtx->hTestSetArchive, pvBuf, cbBuf, pcbRead);
725}
726
727/** @copydoc ATSCALLBACKS::pfnTestSetSendEnd */
728static DECLCALLBACK(int) audioTestGstAtsTestSetSendEndCallback(void const *pvUser, const char *pszTag)
729{
730 RT_NOREF(pszTag);
731
732 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
733
734 int rc = RTFileClose(pCtx->hTestSetArchive);
735 if (RT_SUCCESS(rc))
736 {
737 pCtx->hTestSetArchive = NIL_RTFILE;
738 }
739
740 return rc;
741}
742
743
744/*********************************************************************************************************************************
745* Implementation of audio test environment handling *
746*********************************************************************************************************************************/
747
748/**
749 * Connects an ATS client via TCP/IP to a peer.
750 *
751 * @returns VBox status code.
752 * @param pTstEnv Test environment to use.
753 * @param pClient Client to connect.
754 * @param pszWhat Hint of what to connect to where.
755 * @param pTcpOpts Pointer to TCP options to use.
756 */
757int audioTestEnvConnectViaTcp(PAUDIOTESTENV pTstEnv, PATSCLIENT pClient, const char *pszWhat, PAUDIOTESTENVTCPOPTS pTcpOpts)
758{
759 RT_NOREF(pTstEnv);
760
761 RTGETOPTUNION Val;
762 RT_ZERO(Val);
763
764 Val.u32 = pTcpOpts->enmConnMode;
765 int rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONN_MODE, &Val);
766 AssertRCReturn(rc, rc);
767
768 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
769 || pTcpOpts->enmConnMode == ATSCONNMODE_SERVER)
770 {
771 Assert(pTcpOpts->uBindPort); /* Always set by the caller. */
772 Val.u16 = pTcpOpts->uBindPort;
773 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_BIND_PORT, &Val);
774 AssertRCReturn(rc, rc);
775
776 if (pTcpOpts->szBindAddr[0])
777 {
778 Val.psz = pTcpOpts->szBindAddr;
779 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_BIND_ADDRESS, &Val);
780 AssertRCReturn(rc, rc);
781 }
782 else
783 {
784 RTTestFailed(g_hTest, "No bind address specified!\n");
785 return VERR_INVALID_PARAMETER;
786 }
787
788 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Connecting %s by listening as server at %s:%RU32 ...\n",
789 pszWhat, pTcpOpts->szBindAddr, pTcpOpts->uBindPort);
790 }
791
792
793 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
794 || pTcpOpts->enmConnMode == ATSCONNMODE_CLIENT)
795 {
796 Assert(pTcpOpts->uConnectPort); /* Always set by the caller. */
797 Val.u16 = pTcpOpts->uConnectPort;
798 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONNECT_PORT, &Val);
799 AssertRCReturn(rc, rc);
800
801 if (pTcpOpts->szConnectAddr[0])
802 {
803 Val.psz = pTcpOpts->szConnectAddr;
804 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONNECT_ADDRESS, &Val);
805 AssertRCReturn(rc, rc);
806 }
807 else
808 {
809 RTTestFailed(g_hTest, "No connect address specified!\n");
810 return VERR_INVALID_PARAMETER;
811 }
812
813 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Connecting %s by connecting as client to %s:%RU32 ...\n",
814 pszWhat, pTcpOpts->szConnectAddr, pTcpOpts->uConnectPort);
815 }
816
817 rc = AudioTestSvcClientConnect(pClient);
818 if (RT_FAILURE(rc))
819 {
820 RTTestFailed(g_hTest, "Connecting %s failed with %Rrc\n", pszWhat, rc);
821 return rc;
822 }
823
824 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Successfully connected %s\n", pszWhat);
825 return rc;
826}
827
828/**
829 * Configures and starts an ATS TCP/IP server.
830 *
831 * @returns VBox status code.
832 * @param pSrv ATS server instance to configure and start.
833 * @param pCallbacks ATS callback table to use.
834 * @param pszDesc Hint of server type which is being started.
835 * @param pTcpOpts TCP options to use.
836 */
837int audioTestEnvConfigureAndStartTcpServer(PATSSERVER pSrv, PCATSCALLBACKS pCallbacks, const char *pszDesc,
838 PAUDIOTESTENVTCPOPTS pTcpOpts)
839{
840 RTGETOPTUNION Val;
841 RT_ZERO(Val);
842
843 int rc = AudioTestSvcInit(pSrv, pCallbacks);
844 if (RT_FAILURE(rc))
845 return rc;
846
847 Val.u32 = pTcpOpts->enmConnMode;
848 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONN_MODE, &Val);
849 AssertRCReturn(rc, rc);
850
851 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
852 || pTcpOpts->enmConnMode == ATSCONNMODE_SERVER)
853 {
854 Assert(pTcpOpts->uBindPort); /* Always set by the caller. */
855 Val.u16 = pTcpOpts->uBindPort;
856 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_BIND_PORT, &Val);
857 AssertRCReturn(rc, rc);
858
859 if (pTcpOpts->szBindAddr[0])
860 {
861 Val.psz = pTcpOpts->szBindAddr;
862 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_BIND_ADDRESS, &Val);
863 AssertRCReturn(rc, rc);
864 }
865 else
866 {
867 RTTestFailed(g_hTest, "No bind address specified!\n");
868 return VERR_INVALID_PARAMETER;
869 }
870
871 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Starting server for %s at %s:%RU32 ...\n",
872 pszDesc, pTcpOpts->szBindAddr, pTcpOpts->uBindPort);
873 }
874
875
876 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
877 || pTcpOpts->enmConnMode == ATSCONNMODE_CLIENT)
878 {
879 Assert(pTcpOpts->uConnectPort); /* Always set by the caller. */
880 Val.u16 = pTcpOpts->uConnectPort;
881 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONNECT_PORT, &Val);
882 AssertRCReturn(rc, rc);
883
884 if (pTcpOpts->szConnectAddr[0])
885 {
886 Val.psz = pTcpOpts->szConnectAddr;
887 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONNECT_ADDRESS, &Val);
888 AssertRCReturn(rc, rc);
889 }
890 else
891 {
892 RTTestFailed(g_hTest, "No connect address specified!\n");
893 return VERR_INVALID_PARAMETER;
894 }
895
896 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Starting server for %s by connecting as client to %s:%RU32 ...\n",
897 pszDesc, pTcpOpts->szConnectAddr, pTcpOpts->uConnectPort);
898 }
899
900 if (RT_SUCCESS(rc))
901 {
902 rc = AudioTestSvcStart(pSrv);
903 if (RT_FAILURE(rc))
904 RTTestFailed(g_hTest, "Starting server for %s failed with %Rrc\n", pszDesc, rc);
905 }
906
907 return rc;
908}
909
910/**
911 * Initializes an audio test environment.
912 *
913 * @returns VBox status code.
914 * @param pTstEnv Audio test environment to initialize.
915 * @param pDrvStack Driver stack to use.
916 */
917int audioTestEnvInit(PAUDIOTESTENV pTstEnv, PAUDIOTESTDRVSTACK pDrvStack)
918{
919 int rc = VINF_SUCCESS;
920
921 pTstEnv->pDrvStack = pDrvStack;
922
923 /*
924 * Set sane defaults if not already set.
925 */
926 if (!RTStrNLen(pTstEnv->szTag, sizeof(pTstEnv->szTag)))
927 {
928 rc = AudioTestGenTag(pTstEnv->szTag, sizeof(pTstEnv->szTag));
929 AssertRCReturn(rc, rc);
930 }
931
932 if (!RTStrNLen(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp)))
933 {
934 rc = AudioTestPathGetTemp(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp));
935 AssertRCReturn(rc, rc);
936 }
937
938 if (!RTStrNLen(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut)))
939 {
940 rc = RTPathJoin(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut), pTstEnv->szPathTemp, "vkat-temp");
941 AssertRCReturn(rc, rc);
942 }
943
944 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Initializing environment for mode '%s'\n", pTstEnv->enmMode == AUDIOTESTMODE_HOST ? "host" : "guest");
945 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using tag '%s'\n", pTstEnv->szTag);
946 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Output directory is '%s'\n", pTstEnv->szPathOut);
947 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Temp directory is '%s'\n", pTstEnv->szPathTemp);
948
949 if (!pTstEnv->cMsBufferSize)
950 pTstEnv->cMsBufferSize = UINT32_MAX;
951 if (!pTstEnv->cMsPreBuffer)
952 pTstEnv->cMsPreBuffer = UINT32_MAX;
953 if (!pTstEnv->cMsSchedulingHint)
954 pTstEnv->cMsSchedulingHint = UINT32_MAX;
955
956 char szPathTemp[RTPATH_MAX];
957 if ( !strlen(pTstEnv->szPathTemp)
958 || !strlen(pTstEnv->szPathOut))
959 rc = RTPathTemp(szPathTemp, sizeof(szPathTemp));
960
961 if ( RT_SUCCESS(rc)
962 && !strlen(pTstEnv->szPathTemp))
963 rc = RTPathJoin(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp), szPathTemp, "vkat-temp");
964
965 if (RT_SUCCESS(rc))
966 {
967 rc = RTDirCreate(pTstEnv->szPathTemp, RTFS_UNIX_IRWXU, 0 /* fFlags */);
968 if (rc == VERR_ALREADY_EXISTS)
969 rc = VINF_SUCCESS;
970 }
971
972 if ( RT_SUCCESS(rc)
973 && !strlen(pTstEnv->szPathOut))
974 rc = RTPathJoin(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut), szPathTemp, "vkat");
975
976 if (RT_SUCCESS(rc))
977 {
978 rc = RTDirCreate(pTstEnv->szPathOut, RTFS_UNIX_IRWXU, 0 /* fFlags */);
979 if (rc == VERR_ALREADY_EXISTS)
980 rc = VINF_SUCCESS;
981 }
982
983 if (RT_FAILURE(rc))
984 return rc;
985
986 /**
987 * For NAT'ed VMs we use (default):
988 * - client mode (uConnectAddr / uConnectPort) on the guest.
989 * - server mode (uBindAddr / uBindPort) on the host.
990 */
991 if ( !pTstEnv->TcpOpts.szConnectAddr[0]
992 && !pTstEnv->TcpOpts.szBindAddr[0])
993 RTStrCopy(pTstEnv->TcpOpts.szBindAddr, sizeof(pTstEnv->TcpOpts.szBindAddr), "0.0.0.0");
994
995 /*
996 * Determine connection mode based on set variables.
997 */
998 if ( pTstEnv->TcpOpts.szBindAddr[0]
999 && pTstEnv->TcpOpts.szConnectAddr[0])
1000 {
1001 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_BOTH;
1002 }
1003 else if (pTstEnv->TcpOpts.szBindAddr[0])
1004 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_SERVER;
1005 else /* "Reversed mode", i.e. used for NATed VMs. */
1006 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_CLIENT;
1007
1008 /* Set a back reference to the test environment for the callback context. */
1009 pTstEnv->CallbackCtx.pTstEnv = pTstEnv;
1010
1011 ATSCALLBACKS Callbacks;
1012 RT_ZERO(Callbacks);
1013 Callbacks.pvUser = &pTstEnv->CallbackCtx;
1014
1015 if (pTstEnv->enmMode == AUDIOTESTMODE_GUEST)
1016 {
1017 Callbacks.pfnHowdy = audioTestGstAtsHowdyCallback;
1018 Callbacks.pfnBye = audioTestGstAtsByeCallback;
1019 Callbacks.pfnTestSetBegin = audioTestGstAtsTestSetBeginCallback;
1020 Callbacks.pfnTestSetEnd = audioTestGstAtsTestSetEndCallback;
1021 Callbacks.pfnTonePlay = audioTestGstAtsTonePlayCallback;
1022 Callbacks.pfnToneRecord = audioTestGstAtsToneRecordCallback;
1023 Callbacks.pfnTestSetSendBegin = audioTestGstAtsTestSetSendBeginCallback;
1024 Callbacks.pfnTestSetSendRead = audioTestGstAtsTestSetSendReadCallback;
1025 Callbacks.pfnTestSetSendEnd = audioTestGstAtsTestSetSendEndCallback;
1026
1027 if (!pTstEnv->TcpOpts.uBindPort)
1028 pTstEnv->TcpOpts.uBindPort = ATS_TCP_DEF_BIND_PORT_GUEST;
1029
1030 if (!pTstEnv->TcpOpts.uConnectPort)
1031 pTstEnv->TcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_GUEST;
1032
1033 pTstEnv->pSrv = (PATSSERVER)RTMemAlloc(sizeof(ATSSERVER));
1034 AssertPtrReturn(pTstEnv->pSrv, VERR_NO_MEMORY);
1035
1036 /*
1037 * Start the ATS (Audio Test Service) on the guest side.
1038 * That service then will perform playback and recording operations on the guest, triggered from the host.
1039 *
1040 * When running this in self-test mode, that service also can be run on the host if nothing else is specified.
1041 * Note that we have to bind to "0.0.0.0" by default so that the host can connect to it.
1042 */
1043 rc = audioTestEnvConfigureAndStartTcpServer(pTstEnv->pSrv, &Callbacks, "guest", &pTstEnv->TcpOpts);
1044 }
1045 else /* Host mode */
1046 {
1047 if (!pTstEnv->TcpOpts.uBindPort)
1048 pTstEnv->TcpOpts.uBindPort = ATS_TCP_DEF_BIND_PORT_HOST;
1049
1050 if (!pTstEnv->TcpOpts.uConnectPort)
1051 pTstEnv->TcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_HOST_PORT_FWD;
1052
1053 /**
1054 * Note: Don't set pTstEnv->TcpOpts.szTcpConnectAddr by default here, as this specifies what connection mode
1055 * (client / server / both) we use on the host.
1056 */
1057
1058 /* We need to start a server on the host so that VMs configured with NAT networking
1059 * can connect to it as well. */
1060 rc = AudioTestSvcClientCreate(&pTstEnv->u.Host.AtsClGuest);
1061 if (RT_SUCCESS(rc))
1062 rc = audioTestEnvConnectViaTcp(pTstEnv, &pTstEnv->u.Host.AtsClGuest,
1063 "host -> guest", &pTstEnv->TcpOpts);
1064 if (RT_SUCCESS(rc))
1065 {
1066 AUDIOTESTENVTCPOPTS ValKitTcpOpts;
1067 RT_ZERO(ValKitTcpOpts);
1068
1069 /* We only connect as client to the Validation Kit audio driver ATS. */
1070 ValKitTcpOpts.enmConnMode = ATSCONNMODE_CLIENT;
1071
1072 /* For now we ASSUME that the Validation Kit audio driver ATS runs on the same host as VKAT (this binary) runs on. */
1073 ValKitTcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_VALKIT; /** @todo Make this dynamic. */
1074 RTStrCopy(ValKitTcpOpts.szConnectAddr, sizeof(ValKitTcpOpts.szConnectAddr), ATS_TCP_DEF_CONNECT_HOST_ADDR_STR); /** @todo Ditto. */
1075
1076 rc = AudioTestSvcClientCreate(&pTstEnv->u.Host.AtsClValKit);
1077 if (RT_SUCCESS(rc))
1078 {
1079 rc = audioTestEnvConnectViaTcp(pTstEnv, &pTstEnv->u.Host.AtsClValKit,
1080 "host -> valkit", &ValKitTcpOpts);
1081 if (RT_FAILURE(rc))
1082 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Unable to connect to the Validation Kit audio driver!\n"
1083 "There could be multiple reasons:\n\n"
1084 " - Wrong host being used\n"
1085 " - VirtualBox host version is too old\n"
1086 " - Audio debug mode is not enabled\n"
1087 " - Support for Validation Kit audio driver is not included\n"
1088 " - Firewall / network configuration problem\n");
1089 }
1090 }
1091 }
1092
1093 return rc;
1094}
1095
1096/**
1097 * Destroys an audio test environment.
1098 *
1099 * @param pTstEnv Audio test environment to destroy.
1100 */
1101void audioTestEnvDestroy(PAUDIOTESTENV pTstEnv)
1102{
1103 if (!pTstEnv)
1104 return;
1105
1106 /* When in host mode, we need to destroy our ATS clients in order to also let
1107 * the ATS server(s) know we're going to quit. */
1108 if (pTstEnv->enmMode == AUDIOTESTMODE_HOST)
1109 {
1110 AudioTestSvcClientDestroy(&pTstEnv->u.Host.AtsClValKit);
1111 AudioTestSvcClientDestroy(&pTstEnv->u.Host.AtsClGuest);
1112 }
1113
1114 if (pTstEnv->pSrv)
1115 {
1116 int rc2 = AudioTestSvcDestroy(pTstEnv->pSrv);
1117 AssertRC(rc2);
1118
1119 RTMemFree(pTstEnv->pSrv);
1120 pTstEnv->pSrv = NULL;
1121 }
1122
1123 for (unsigned i = 0; i < RT_ELEMENTS(pTstEnv->aStreams); i++)
1124 {
1125 int rc2 = audioTestStreamDestroy(pTstEnv, &pTstEnv->aStreams[i]);
1126 if (RT_FAILURE(rc2))
1127 RTTestFailed(g_hTest, "Stream destruction for stream #%u failed with %Rrc\n", i, rc2);
1128 }
1129
1130 /* Try cleaning up a bit. */
1131 RTDirRemove(pTstEnv->szPathTemp);
1132 RTDirRemove(pTstEnv->szPathOut);
1133
1134 pTstEnv->pDrvStack = NULL;
1135}
1136
1137/**
1138 * Closes, packs up and destroys a test environment.
1139 *
1140 * @returns VBox status code.
1141 * @param pTstEnv Test environment to handle.
1142 * @param fPack Whether to pack the test set up before destroying / wiping it.
1143 * @param pszPackFile Where to store the packed test set file on success. Can be NULL if \a fPack is \c false.
1144 * @param cbPackFile Size (in bytes) of \a pszPackFile. Can be 0 if \a fPack is \c false.
1145 */
1146int audioTestEnvPrologue(PAUDIOTESTENV pTstEnv, bool fPack, char *pszPackFile, size_t cbPackFile)
1147{
1148 /* Close the test set first. */
1149 AudioTestSetClose(&pTstEnv->Set);
1150
1151 int rc = VINF_SUCCESS;
1152
1153 if (fPack)
1154 {
1155 /* Before destroying the test environment, pack up the test set so
1156 * that it's ready for transmission. */
1157 rc = AudioTestSetPack(&pTstEnv->Set, pTstEnv->szPathOut, pszPackFile, cbPackFile);
1158 if (RT_SUCCESS(rc))
1159 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set packed up to '%s'\n", pszPackFile);
1160 }
1161
1162 if (!g_fDrvAudioDebug) /* Don't wipe stuff when debugging. Can be useful for introspecting data. */
1163 /* ignore rc */ AudioTestSetWipe(&pTstEnv->Set);
1164
1165 AudioTestSetDestroy(&pTstEnv->Set);
1166
1167 if (RT_FAILURE(rc))
1168 RTTestFailed(g_hTest, "Test set prologue failed with %Rrc\n", rc);
1169
1170 return rc;
1171}
1172
1173/**
1174 * Initializes an audio test parameters set.
1175 *
1176 * @param pTstParms Test parameters set to initialize.
1177 */
1178void audioTestParmsInit(PAUDIOTESTPARMS pTstParms)
1179{
1180 RT_ZERO(*pTstParms);
1181}
1182
1183/**
1184 * Destroys an audio test parameters set.
1185 *
1186 * @param pTstParms Test parameters set to destroy.
1187 */
1188void audioTestParmsDestroy(PAUDIOTESTPARMS pTstParms)
1189{
1190 if (!pTstParms)
1191 return;
1192
1193 return;
1194}
1195
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