VirtualBox

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

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

Audio/Validation Kit: Also use the test ID logging prefixes audioTestRecordTone(). bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 68.9 KB
Line 
1/* $Id: vkatCommon.cpp 92462 2021-11-16 15:26:30Z 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#ifdef VBOX_WITH_AUDIO_ALSA
35# include "DrvHostAudioAlsaStubsMangling.h"
36# include <alsa/asoundlib.h>
37# include <alsa/control.h> /* For device enumeration. */
38# include <alsa/version.h>
39# include "DrvHostAudioAlsaStubs.h"
40#endif
41#ifdef VBOX_WITH_AUDIO_OSS
42# include <errno.h>
43# include <fcntl.h>
44# include <sys/ioctl.h>
45# include <sys/mman.h>
46# include <sys/soundcard.h>
47# include <unistd.h>
48#endif
49#ifdef RT_OS_WINDOWS
50# include <iprt/win/windows.h>
51# include <iprt/win/audioclient.h>
52# include <endpointvolume.h> /* For IAudioEndpointVolume. */
53# include <audiopolicy.h> /* For IAudioSessionManager. */
54# include <AudioSessionTypes.h>
55# include <Mmdeviceapi.h>
56#endif
57
58#include <iprt/ctype.h>
59#include <iprt/dir.h>
60#include <iprt/errcore.h>
61#include <iprt/getopt.h>
62#include <iprt/message.h>
63#include <iprt/rand.h>
64#include <iprt/test.h>
65
66#include "Audio/AudioHlp.h"
67#include "Audio/AudioTest.h"
68#include "Audio/AudioTestService.h"
69#include "Audio/AudioTestServiceClient.h"
70
71#include "vkatInternal.h"
72
73
74/*********************************************************************************************************************************
75* Defined Constants And Macros *
76*********************************************************************************************************************************/
77
78
79/*********************************************************************************************************************************
80* Internal Functions *
81*********************************************************************************************************************************/
82static int audioTestStreamInit(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream, PDMAUDIODIR enmDir, PAUDIOTESTIOOPTS pPlayOpt);
83static int audioTestStreamDestroy(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream);
84
85
86/*********************************************************************************************************************************
87* Volume handling. *
88*********************************************************************************************************************************/
89
90#ifdef VBOX_WITH_AUDIO_ALSA
91/**
92 * Sets the system's master volume via ALSA, if available.
93 *
94 * @returns VBox status code.
95 * @param uVolPercent Volume (in percent) to set.
96 */
97static int audioTestSetMasterVolumeALSA(unsigned uVolPercent)
98{
99 int rc = audioLoadAlsaLib();
100 if (RT_FAILURE(rc))
101 return rc;
102
103 int err;
104 snd_mixer_t *handle;
105
106# define ALSA_CHECK_RET(a_Exp, a_Text) \
107 if (!(a_Exp)) \
108 { \
109 AssertLogRelMsg(a_Exp, a_Text); \
110 if (handle) \
111 snd_mixer_close(handle); \
112 return VERR_GENERAL_FAILURE; \
113 }
114
115# define ALSA_CHECK_ERR_RET(a_Text) \
116 ALSA_CHECK_RET(err >= 0, a_Text)
117
118 err = snd_mixer_open(&handle, 0 /* Index */);
119 ALSA_CHECK_ERR_RET(("ALSA: Failed to open mixer: %s\n", snd_strerror(err)));
120 err = snd_mixer_attach(handle, "default");
121 ALSA_CHECK_ERR_RET(("ALSA: Failed to attach to default sink: %s\n", snd_strerror(err)));
122 err = snd_mixer_selem_register(handle, NULL, NULL);
123 ALSA_CHECK_ERR_RET(("ALSA: Failed to attach to default sink: %s\n", snd_strerror(err)));
124 err = snd_mixer_load(handle);
125 ALSA_CHECK_ERR_RET(("ALSA: Failed to load mixer: %s\n", snd_strerror(err)));
126
127 snd_mixer_selem_id_t *sid = NULL;
128 snd_mixer_selem_id_alloca(&sid);
129
130 snd_mixer_selem_id_set_index(sid, 0 /* Index */);
131 snd_mixer_selem_id_set_name(sid, "Master");
132
133 snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, sid);
134 ALSA_CHECK_RET(elem != NULL, ("ALSA: Failed to find mixer element: %s\n", snd_strerror(err)));
135
136 long uVolMin, uVolMax;
137 snd_mixer_selem_get_playback_volume_range(elem, &uVolMin, &uVolMax);
138 ALSA_CHECK_ERR_RET(("ALSA: Failed to get playback volume range: %s\n", snd_strerror(err)));
139
140 long const uVol = RT_MIN(uVolPercent, 100) * uVolMax / 100;
141
142 err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, uVol);
143 ALSA_CHECK_ERR_RET(("ALSA: Failed to set playback volume left: %s\n", snd_strerror(err)));
144 err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, uVol);
145 ALSA_CHECK_ERR_RET(("ALSA: Failed to set playback volume right: %s\n", snd_strerror(err)));
146
147 snd_mixer_close(handle);
148
149 return VINF_SUCCESS;
150
151# undef ALSA_CHECK_RET
152# undef ALSA_CHECK_ERR_RET
153}
154#endif /* VBOX_WITH_AUDIO_ALSA */
155
156#ifdef VBOX_WITH_AUDIO_OSS
157/**
158 * Sets the system's master volume via OSS, if available.
159 *
160 * @returns VBox status code.
161 * @param uVolPercent Volume (in percent) to set.
162 */
163static int audioTestSetMasterVolumeOSS(unsigned uVolPercent)
164{
165 int hFile = open("/dev/dsp", O_WRONLY | O_NONBLOCK, 0);
166 if (hFile == -1)
167 {
168 /* Try opening the mixing device instead. */
169 hFile = open("/dev/mixer", O_RDONLY | O_NONBLOCK, 0);
170 }
171
172 if (hFile != -1)
173 {
174 /* OSS maps 0 (muted) - 100 (max), so just use uVolPercent unmodified here. */
175 uint16_t uVol = RT_MAKE_U16(uVolPercent, uVolPercent);
176 AssertLogRelMsgReturnStmt(ioctl(hFile, SOUND_MIXER_PCM /* SNDCTL_DSP_SETPLAYVOL */, &uVol) >= 0,
177 ("OSS: Failed to set DSP playback volume: %s (%d)\n",
178 strerror(errno), errno), close(hFile), RTErrConvertFromErrno(errno));
179 return VINF_SUCCESS;
180 }
181
182 return VERR_NOT_SUPPORTED;
183}
184#endif /* VBOX_WITH_AUDIO_OSS */
185
186#ifdef RT_OS_WINDOWS
187static int audioTestSetMasterVolumeWASAPI(unsigned uVolPercent)
188{
189 HRESULT hr;
190
191# define WASAPI_CHECK_HR_RET(a_Text) \
192 if (FAILED(hr)) \
193 { \
194 AssertLogRelMsgFailed(a_Text); \
195 return VERR_GENERAL_FAILURE; \
196 }
197
198 hr = CoInitialize(NULL);
199 WASAPI_CHECK_HR_RET(("CoInitialize() failed, hr=%Rhrc", hr));
200 IMMDeviceEnumerator* pIEnumerator = NULL;
201 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void **)&pIEnumerator);
202 WASAPI_CHECK_HR_RET(("WASAPI: Unable to create IMMDeviceEnumerator, hr=%Rhrc", hr));
203
204 IMMDevice *pIMMDevice = NULL;
205 hr = pIEnumerator->GetDefaultAudioEndpoint(EDataFlow::eRender, ERole::eConsole, &pIMMDevice);
206 WASAPI_CHECK_HR_RET(("WASAPI: Unable to get audio endpoint, hr=%Rhrc", hr));
207 pIEnumerator->Release();
208
209 IAudioEndpointVolume *pIAudioEndpointVolume = NULL;
210 hr = pIMMDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void **)&pIAudioEndpointVolume);
211 WASAPI_CHECK_HR_RET(("WASAPI: Unable to activate audio endpoint volume, hr=%Rhrc", hr));
212 pIMMDevice->Release();
213
214 float dbMin, dbMax, dbInc;
215 hr = pIAudioEndpointVolume->GetVolumeRange(&dbMin, &dbMax, &dbInc);
216 WASAPI_CHECK_HR_RET(("WASAPI: Unable to get volume range, hr=%Rhrc", hr));
217
218 float const dbSteps = (dbMax - dbMin) / dbInc;
219 float const dbStepsPerPercent = (dbSteps * dbInc) / 100;
220 float const dbVol = dbMin + (dbStepsPerPercent * (float(RT_MIN(uVolPercent, 100.0))));
221
222 hr = pIAudioEndpointVolume->SetMasterVolumeLevel(dbVol, NULL);
223 WASAPI_CHECK_HR_RET(("WASAPI: Unable to set master volume level, hr=%Rhrc", hr));
224 pIAudioEndpointVolume->Release();
225
226 return VINF_SUCCESS;
227
228# undef WASAPI_CHECK_HR_RET
229}
230#endif /* RT_OS_WINDOWS */
231
232/**
233 * Sets the system's master volume, if available.
234 *
235 * @returns VBox status code. VERR_NOT_SUPPORTED if not supported.
236 * @param uVolPercent Volume (in percent) to set.
237 */
238int audioTestSetMasterVolume(unsigned uVolPercent)
239{
240 int rc = VINF_SUCCESS;
241
242#ifdef VBOX_WITH_AUDIO_ALSA
243 rc = audioTestSetMasterVolumeALSA(uVolPercent);
244 if (RT_SUCCESS(rc))
245 return rc;
246 /* else try OSS (if available) below. */
247#endif /* VBOX_WITH_AUDIO_ALSA */
248
249#ifdef VBOX_WITH_AUDIO_OSS
250 rc = audioTestSetMasterVolumeOSS(uVolPercent);
251 if (RT_SUCCESS(rc))
252 return rc;
253#endif /* VBOX_WITH_AUDIO_OSS */
254
255#ifdef RT_OS_WINDOWS
256 rc = audioTestSetMasterVolumeWASAPI(uVolPercent);
257 if (RT_SUCCESS(rc))
258 return rc;
259#endif
260
261 RT_NOREF(rc, uVolPercent);
262 /** @todo Port other platforms. */
263 return VERR_NOT_SUPPORTED;
264}
265
266
267/*********************************************************************************************************************************
268* Device enumeration + handling. *
269*********************************************************************************************************************************/
270
271/**
272 * Enumerates audio devices and optionally searches for a specific device.
273 *
274 * @returns VBox status code.
275 * @param pDrvStack Driver stack to use for enumeration.
276 * @param pszDev Device name to search for. Can be NULL if the default device shall be used.
277 * @param ppDev Where to return the pointer of the device enumeration of \a pTstEnv when a
278 * specific device was found.
279 */
280int audioTestDevicesEnumerateAndCheck(PAUDIOTESTDRVSTACK pDrvStack, const char *pszDev, PPDMAUDIOHOSTDEV *ppDev)
281{
282 RTTestSubF(g_hTest, "Enumerating audio devices and checking for device '%s'", pszDev && *pszDev ? pszDev : "[Default]");
283
284 if (!pDrvStack->pIHostAudio->pfnGetDevices)
285 {
286 RTTestSkipped(g_hTest, "Backend does not support device enumeration, skipping");
287 return VINF_NOT_SUPPORTED;
288 }
289
290 Assert(pszDev == NULL || ppDev);
291
292 if (ppDev)
293 *ppDev = NULL;
294
295 int rc = pDrvStack->pIHostAudio->pfnGetDevices(pDrvStack->pIHostAudio, &pDrvStack->DevEnum);
296 if (RT_SUCCESS(rc))
297 {
298 PPDMAUDIOHOSTDEV pDev;
299 RTListForEach(&pDrvStack->DevEnum.LstDevices, pDev, PDMAUDIOHOSTDEV, ListEntry)
300 {
301 char szFlags[PDMAUDIOHOSTDEV_MAX_FLAGS_STRING_LEN];
302 if (pDev->pszId)
303 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Device '%s' (ID '%s'):\n", pDev->pszName, pDev->pszId);
304 else
305 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Device '%s':\n", pDev->pszName);
306 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Usage = %s\n", PDMAudioDirGetName(pDev->enmUsage));
307 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Flags = %s\n", PDMAudioHostDevFlagsToString(szFlags, pDev->fFlags));
308 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Input channels = %RU8\n", pDev->cMaxInputChannels);
309 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enum: Output channels = %RU8\n", pDev->cMaxOutputChannels);
310
311 if ( (pszDev && *pszDev)
312 && !RTStrCmp(pDev->pszName, pszDev))
313 {
314 *ppDev = pDev;
315 }
316 }
317 }
318 else
319 RTTestFailed(g_hTest, "Enumerating audio devices failed with %Rrc", rc);
320
321 if (RT_SUCCESS(rc))
322 {
323 if ( (pszDev && *pszDev)
324 && *ppDev == NULL)
325 {
326 RTTestFailed(g_hTest, "Audio device '%s' not found", pszDev);
327 rc = VERR_NOT_FOUND;
328 }
329 }
330
331 RTTestSubDone(g_hTest);
332 return rc;
333}
334
335static int audioTestStreamInit(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream,
336 PDMAUDIODIR enmDir, PAUDIOTESTIOOPTS pIoOpts)
337{
338 int rc;
339
340 if (enmDir == PDMAUDIODIR_IN)
341 rc = audioTestDriverStackStreamCreateInput(pDrvStack, &pIoOpts->Props, pIoOpts->cMsBufferSize,
342 pIoOpts->cMsPreBuffer, pIoOpts->cMsSchedulingHint, &pStream->pStream, &pStream->Cfg);
343 else if (enmDir == PDMAUDIODIR_OUT)
344 rc = audioTestDriverStackStreamCreateOutput(pDrvStack, &pIoOpts->Props, pIoOpts->cMsBufferSize,
345 pIoOpts->cMsPreBuffer, pIoOpts->cMsSchedulingHint, &pStream->pStream, &pStream->Cfg);
346 else
347 rc = VERR_NOT_SUPPORTED;
348
349 if (RT_SUCCESS(rc))
350 {
351 if (!pDrvStack->pIAudioConnector)
352 {
353 pStream->pBackend = &((PAUDIOTESTDRVSTACKSTREAM)pStream->pStream)->Backend;
354 }
355 else
356 pStream->pBackend = NULL;
357
358 /*
359 * Automatically enable the mixer if the PCM properties don't match.
360 */
361 if ( !pIoOpts->fWithMixer
362 && !PDMAudioPropsAreEqual(&pIoOpts->Props, &pStream->Cfg.Props))
363 {
364 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Enabling stream mixer\n");
365 pIoOpts->fWithMixer = true;
366 }
367
368 rc = AudioTestMixStreamInit(&pStream->Mix, pDrvStack, pStream->pStream,
369 pIoOpts->fWithMixer ? &pIoOpts->Props : NULL, 100 /* ms */); /** @todo Configure mixer buffer? */
370 }
371
372 if (RT_FAILURE(rc))
373 RTTestFailed(g_hTest, "Initializing %s stream failed with %Rrc", enmDir == PDMAUDIODIR_IN ? "input" : "output", rc);
374
375 return rc;
376}
377
378/**
379 * Destroys an audio test stream.
380 *
381 * @returns VBox status code.
382 * @param pDrvStack Driver stack the stream belongs to.
383 * @param pStream Audio stream to destroy.
384 */
385static int audioTestStreamDestroy(PAUDIOTESTDRVSTACK pDrvStack, PAUDIOTESTSTREAM pStream)
386{
387 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
388
389 if (pStream->pStream)
390 {
391 /** @todo Anything else to do here, e.g. test if there are left over samples or some such? */
392
393 audioTestDriverStackStreamDestroy(pDrvStack, pStream->pStream);
394 pStream->pStream = NULL;
395 pStream->pBackend = NULL;
396 }
397
398 AudioTestMixStreamTerm(&pStream->Mix);
399
400 return VINF_SUCCESS;
401}
402
403
404/*********************************************************************************************************************************
405* Test Primitives *
406*********************************************************************************************************************************/
407
408/**
409 * Initializes test tone parameters (partly with random values).
410
411 * @param pToneParms Test tone parameters to initialize.
412 */
413void audioTestToneParmsInit(PAUDIOTESTTONEPARMS pToneParms)
414{
415 RT_BZERO(pToneParms, sizeof(AUDIOTESTTONEPARMS));
416
417 /**
418 * Set default (randomized) test tone parameters if not set explicitly.
419 */
420 pToneParms->dbFreqHz = AudioTestToneGetRandomFreq();
421 pToneParms->msDuration = RTRandU32Ex(200, RT_MS_30SEC);
422 pToneParms->uVolumePercent = 100; /* We always go with maximum volume for now. */
423
424 PDMAudioPropsInit(&pToneParms->Props,
425 2 /* 16-bit */, true /* fPcmSigned */, 2 /* cPcmChannels */, 44100 /* uPcmHz */);
426}
427
428/**
429 * Initializes I/O options with some sane default values.
430 *
431 * @param pIoOpts I/O options to initialize.
432 */
433void audioTestIoOptsInitDefaults(PAUDIOTESTIOOPTS pIoOpts)
434{
435 RT_BZERO(pIoOpts, sizeof(AUDIOTESTIOOPTS));
436
437 /* Initialize the PCM properties to some sane values. */
438 PDMAudioPropsInit(&pIoOpts->Props,
439 2 /* 16-bit */, true /* fPcmSigned */, 2 /* cPcmChannels */, 44100 /* uPcmHz */);
440
441 pIoOpts->cMsBufferSize = UINT32_MAX;
442 pIoOpts->cMsPreBuffer = UINT32_MAX;
443 pIoOpts->cMsSchedulingHint = UINT32_MAX;
444 pIoOpts->uVolumePercent = 100; /* Use maximum volume by default. */
445}
446
447#if 0 /* Unused */
448/**
449 * Returns a random scheduling hint (in ms).
450 */
451DECLINLINE(uint32_t) audioTestEnvGetRandomSchedulingHint(void)
452{
453 static const unsigned s_aSchedulingHintsMs[] =
454 {
455 10,
456 25,
457 50,
458 100,
459 200,
460 250
461 };
462
463 return s_aSchedulingHintsMs[RTRandU32Ex(0, RT_ELEMENTS(s_aSchedulingHintsMs) - 1)];
464}
465#endif
466
467/**
468 * Plays a test tone on a specific audio test stream.
469 *
470 * @returns VBox status code.
471 * @param pIoOpts I/O options to use.
472 * @param pTstEnv Test environment to use for running the test.
473 * Optional and can be NULL (for simple playback only).
474 * @param pStream Stream to use for playing the tone.
475 * @param pParms Tone parameters to use.
476 *
477 * @note Blocking function.
478 */
479int audioTestPlayTone(PAUDIOTESTIOOPTS pIoOpts, PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream, PAUDIOTESTTONEPARMS pParms)
480{
481 uint32_t const idxTest = (uint8_t)pParms->Hdr.idxTest;
482
483 AUDIOTESTTONE TstTone;
484 AudioTestToneInit(&TstTone, &pStream->Cfg.Props, pParms->dbFreqHz);
485
486 char const *pcszPathOut = NULL;
487 if (pTstEnv)
488 pcszPathOut = pTstEnv->Set.szPathAbs;
489
490 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing test tone (tone frequency is %RU16Hz, %RU32ms, %RU8%% volume)\n",
491 idxTest, (uint16_t)pParms->dbFreqHz, pParms->msDuration, pParms->uVolumePercent);
492 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Using %RU32ms stream scheduling hint\n",
493 idxTest, pStream->Cfg.Device.cMsSchedulingHint);
494 if (pcszPathOut)
495 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Writing to '%s'\n", idxTest, pcszPathOut);
496
497 int rc;
498
499 /** @todo Use .WAV here? */
500 AUDIOTESTOBJ Obj;
501 RT_ZERO(Obj); /* Shut up MSVC. */
502 if (pTstEnv)
503 {
504 rc = AudioTestSetObjCreateAndRegister(&pTstEnv->Set, "guest-tone-play.pcm", &Obj);
505 AssertRCReturn(rc, rc);
506 }
507
508 uint8_t const uVolPercent = pIoOpts->uVolumePercent;
509 int rc2 = audioTestSetMasterVolume(uVolPercent);
510 if (RT_FAILURE(rc2))
511 {
512 if (rc2 == VERR_NOT_SUPPORTED)
513 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Setting system's master volume is not supported on this platform, skipping\n");
514 else
515 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Setting system's master volume failed with %Rrc\n", rc2);
516 }
517 else
518 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Set system's master volume to %RU8%%\n", uVolPercent);
519
520 rc = AudioTestMixStreamEnable(&pStream->Mix);
521 if ( RT_SUCCESS(rc)
522 && AudioTestMixStreamIsOkay(&pStream->Mix))
523 {
524 uint32_t cbToWriteTotal = PDMAudioPropsMilliToBytes(&pStream->Cfg.Props, pParms->msDuration);
525 AssertStmt(cbToWriteTotal, rc = VERR_INVALID_PARAMETER);
526 uint32_t cbWrittenTotal = 0;
527
528 /* We play a pre + post beacon before + after the actual test tone.
529 * We always start with the pre beacon. */
530 AUDIOTESTTONEBEACON Beacon;
531 AudioTestBeaconInit(&Beacon, (uint8_t)pParms->Hdr.idxTest, AUDIOTESTTONEBEACONTYPE_PLAY_PRE, &pStream->Cfg.Props);
532
533 uint32_t const cbBeacon = AudioTestBeaconGetSize(&Beacon);
534 if (cbBeacon)
535 {
536 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing 2 x %RU32 bytes pre/post beacons\n",
537 idxTest, cbBeacon);
538
539 if (g_uVerbosity >= 2)
540 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing %s beacon ...\n",
541 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
542 }
543
544 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing %RU32 bytes total\n", idxTest, cbToWriteTotal);
545
546 AudioTestObjAddMetadataStr(Obj, "test_id=%04RU32\n", pParms->Hdr.idxTest);
547 AudioTestObjAddMetadataStr(Obj, "beacon_type=%RU32\n", (uint32_t)AudioTestBeaconGetType(&Beacon));
548 AudioTestObjAddMetadataStr(Obj, "beacon_pre_bytes=%RU32\n", cbBeacon);
549 AudioTestObjAddMetadataStr(Obj, "beacon_post_bytes=%RU32\n", cbBeacon);
550 AudioTestObjAddMetadataStr(Obj, "stream_to_write_total_bytes=%RU32\n", cbToWriteTotal);
551 AudioTestObjAddMetadataStr(Obj, "stream_period_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesPeriod);
552 AudioTestObjAddMetadataStr(Obj, "stream_buffer_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesBufferSize);
553 AudioTestObjAddMetadataStr(Obj, "stream_prebuf_size_frames=%RU32\n", pStream->Cfg.Backend.cFramesPreBuffering);
554 /* Note: This mostly is provided by backend (e.g. PulseAudio / ALSA / ++) and
555 * has nothing to do with the device emulation scheduling hint. */
556 AudioTestObjAddMetadataStr(Obj, "device_scheduling_hint_ms=%RU32\n", pStream->Cfg.Device.cMsSchedulingHint);
557
558 PAUDIOTESTDRVMIXSTREAM pMix = &pStream->Mix;
559
560 uint32_t const cbPreBuffer = PDMAudioPropsFramesToBytes(pMix->pProps, pStream->Cfg.Backend.cFramesPreBuffering);
561 uint64_t const nsStarted = RTTimeNanoTS();
562 uint64_t nsDonePreBuffering = 0;
563
564 uint64_t offStream = 0;
565 uint64_t nsTimeout = RT_MS_5MIN_64 * RT_NS_1MS;
566 uint64_t nsLastMsgCantWrite = 0; /* Timestamp (in ns) when the last message of an unwritable stream was shown. */
567 uint64_t nsLastWrite = 0;
568
569 AUDIOTESTSTATE enmState = AUDIOTESTSTATE_PRE;
570 uint8_t abBuf[_16K];
571
572 for (;;)
573 {
574 uint64_t const nsNow = RTTimeNanoTS();
575 if (!nsLastWrite)
576 nsLastWrite = nsNow;
577
578 /* Pace ourselves a little. */
579 if (offStream >= cbPreBuffer)
580 {
581 if (!nsDonePreBuffering)
582 nsDonePreBuffering = nsNow;
583 uint64_t const cNsWritten = PDMAudioPropsBytesToNano64(pMix->pProps, offStream - cbPreBuffer);
584 uint64_t const cNsElapsed = nsNow - nsStarted;
585 if (cNsWritten > cNsElapsed + RT_NS_10MS)
586 RTThreadSleep((cNsWritten - cNsElapsed - RT_NS_10MS / 2) / RT_NS_1MS);
587 }
588
589 uint32_t cbWritten = 0;
590 uint32_t const cbCanWrite = AudioTestMixStreamGetWritable(&pStream->Mix);
591 if (cbCanWrite)
592 {
593 if (g_uVerbosity >= 4)
594 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Stream is writable with %RU64ms (%RU32 bytes)\n",
595 idxTest, PDMAudioPropsBytesToMilli(pMix->pProps, cbCanWrite), cbCanWrite);
596
597 switch (enmState)
598 {
599 case AUDIOTESTSTATE_PRE:
600 RT_FALL_THROUGH();
601 case AUDIOTESTSTATE_POST:
602 {
603 if (g_uVerbosity >= 4)
604 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: %RU32 bytes (%RU64ms) beacon data remaining\n",
605 idxTest, AudioTestBeaconGetRemaining(&Beacon),
606 PDMAudioPropsBytesToMilli(&pStream->pStream->Cfg.Props, AudioTestBeaconGetRemaining(&Beacon)));
607
608 bool fGoToNextStage = false;
609
610 if ( AudioTestBeaconGetSize(&Beacon)
611 && !AudioTestBeaconIsComplete(&Beacon))
612 {
613 bool const fStarted = AudioTestBeaconGetRemaining(&Beacon) == AudioTestBeaconGetSize(&Beacon);
614
615 uint32_t const cbBeaconRemaining = AudioTestBeaconGetRemaining(&Beacon);
616 AssertBreakStmt(cbBeaconRemaining, VERR_WRONG_ORDER);
617
618 /* Limit to exactly one beacon (pre or post). */
619 uint32_t const cbToWrite = RT_MIN(sizeof(abBuf), RT_MIN(cbCanWrite, cbBeaconRemaining));
620
621 rc = AudioTestBeaconWrite(&Beacon, abBuf, cbToWrite);
622 if (RT_SUCCESS(rc))
623 {
624 rc = AudioTestMixStreamPlay(&pStream->Mix, abBuf, cbToWrite, &cbWritten);
625 if ( RT_SUCCESS(rc)
626 && pTstEnv)
627 {
628 /* Also write the beacon data to the test object.
629 * Note: We use cbPlayed here instead of cbToPlay to know if the data actually was
630 * reported as being played by the audio stack. */
631 rc = AudioTestObjWrite(Obj, abBuf, cbWritten);
632 }
633 }
634
635 if ( fStarted
636 && g_uVerbosity >= 2)
637 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Writing %s beacon begin\n",
638 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
639 if (AudioTestBeaconIsComplete(&Beacon))
640 {
641 if (g_uVerbosity >= 2)
642 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Writing %s beacon end\n",
643 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
644 fGoToNextStage = true;
645 }
646 }
647 else
648 fGoToNextStage = true;
649
650 if (fGoToNextStage)
651 {
652 if (enmState == AUDIOTESTSTATE_PRE)
653 enmState = AUDIOTESTSTATE_RUN;
654 else if (enmState == AUDIOTESTSTATE_POST)
655 enmState = AUDIOTESTSTATE_DONE;
656 }
657 break;
658 }
659
660 case AUDIOTESTSTATE_RUN:
661 {
662 uint32_t cbToWrite = RT_MIN(sizeof(abBuf), cbCanWrite);
663 cbToWrite = RT_MIN(cbToWrite, cbToWriteTotal - cbWrittenTotal);
664 if (cbToWrite)
665 {
666 rc = AudioTestToneGenerate(&TstTone, abBuf, cbToWrite, &cbToWrite);
667 if (RT_SUCCESS(rc))
668 {
669 if (pTstEnv)
670 {
671 /* Write stuff to disk before trying to play it. Help analysis later. */
672 rc = AudioTestObjWrite(Obj, abBuf, cbToWrite);
673 }
674
675 if (RT_SUCCESS(rc))
676 {
677 rc = AudioTestMixStreamPlay(&pStream->Mix, abBuf, cbToWrite, &cbWritten);
678 if (RT_SUCCESS(rc))
679 {
680 AssertBreakStmt(cbWritten <= cbToWrite, rc = VERR_TOO_MUCH_DATA);
681
682 offStream += cbWritten;
683
684 if (cbWritten != cbToWrite)
685 RTTestFailed(g_hTest, "Test #%RU32: Only played %RU32/%RU32 bytes",
686 idxTest, cbWritten, cbToWrite);
687
688 if (cbWritten)
689 nsLastWrite = nsNow;
690
691 if (g_uVerbosity >= 4)
692 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
693 "Test #%RU32: Played back %RU32 bytes\n", idxTest, cbWritten);
694
695 cbWrittenTotal += cbWritten;
696 }
697 }
698 }
699 }
700
701 const bool fComplete = cbWrittenTotal >= cbToWriteTotal;
702 if (fComplete)
703 {
704 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing back audio data ended\n", idxTest);
705
706 enmState = AUDIOTESTSTATE_POST;
707
708 /* Re-use the beacon object, but this time it's the post beacon. */
709 AudioTestBeaconInit(&Beacon, (uint8_t)idxTest, AUDIOTESTTONEBEACONTYPE_PLAY_POST,
710 &pStream->Cfg.Props);
711 }
712 break;
713 }
714
715 case AUDIOTESTSTATE_DONE:
716 {
717 /* Handled below. */
718 break;
719 }
720
721 default:
722 AssertFailed();
723 break;
724 }
725
726 if (RT_FAILURE(rc))
727 break;
728
729 if (enmState == AUDIOTESTSTATE_DONE)
730 break;
731
732 nsLastMsgCantWrite = 0;
733 }
734 else if (AudioTestMixStreamIsOkay(&pStream->Mix))
735 {
736 RTMSINTERVAL const msSleep = RT_MIN(RT_MAX(1, pStream->Cfg.Device.cMsSchedulingHint), 256);
737
738 if ( g_uVerbosity >= 3
739 && ( !nsLastMsgCantWrite
740 || (nsNow - nsLastMsgCantWrite) > RT_NS_10SEC)) /* Don't spam the output too much. */
741 {
742 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Waiting %RU32ms for stream to be writable again (last write %RU64ns ago) ...\n",
743 idxTest, msSleep, nsNow - nsLastWrite);
744 nsLastMsgCantWrite = nsNow;
745 }
746
747 RTThreadSleep(msSleep);
748 }
749 else
750 AssertFailedBreakStmt(rc = VERR_AUDIO_STREAM_NOT_READY);
751
752 /* Fail-safe in case something screwed up while playing back. */
753 uint64_t const cNsElapsed = nsNow - nsStarted;
754 if (cNsElapsed > nsTimeout)
755 {
756 RTTestFailed(g_hTest, "Test #%RU32: Playback took too long (running %RU64 vs. timeout %RU64), aborting\n",
757 idxTest, cNsElapsed, nsTimeout);
758 rc = VERR_TIMEOUT;
759 }
760
761 if (RT_FAILURE(rc))
762 break;
763 } /* for */
764
765 if (cbWrittenTotal != cbToWriteTotal)
766 RTTestFailed(g_hTest, "Test #%RU32: Playback ended unexpectedly (%RU32/%RU32 played)\n",
767 idxTest, cbWrittenTotal, cbToWriteTotal);
768
769 if (RT_SUCCESS(rc))
770 {
771 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Draining stream ...\n", idxTest);
772 rc = AudioTestMixStreamDrain(&pStream->Mix, true /*fSync*/);
773 }
774 }
775 else
776 rc = VERR_AUDIO_STREAM_NOT_READY;
777
778 if (pTstEnv)
779 {
780 rc2 = AudioTestObjClose(Obj);
781 if (RT_SUCCESS(rc))
782 rc = rc2;
783 }
784
785 if (RT_FAILURE(rc))
786 RTTestFailed(g_hTest, "Test #%RU32: Playing tone failed with %Rrc\n", idxTest, rc);
787
788 return rc;
789}
790
791/**
792 * Records a test tone from a specific audio test stream.
793 *
794 * @returns VBox status code.
795 * @param pIoOpts I/O options to use.
796 * @param pTstEnv Test environment to use for running the test.
797 * @param pStream Stream to use for recording the tone.
798 * @param pParms Tone parameters to use.
799 *
800 * @note Blocking function.
801 */
802static int audioTestRecordTone(PAUDIOTESTIOOPTS pIoOpts, PAUDIOTESTENV pTstEnv, PAUDIOTESTSTREAM pStream, PAUDIOTESTTONEPARMS pParms)
803{
804 uint32_t const idxTest = (uint8_t)pParms->Hdr.idxTest;
805
806 const char *pcszPathOut = pTstEnv->Set.szPathAbs;
807
808 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Recording test tone (tone frequency is %RU16Hz, %RU32ms)\n",
809 idxTest, (uint16_t)pParms->dbFreqHz, pParms->msDuration);
810 RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Test #%RU32: Writing to '%s'\n", idxTest, pcszPathOut);
811
812 /** @todo Use .WAV here? */
813 AUDIOTESTOBJ Obj;
814 int rc = AudioTestSetObjCreateAndRegister(&pTstEnv->Set, "guest-tone-rec.pcm", &Obj);
815 AssertRCReturn(rc, rc);
816
817 PAUDIOTESTDRVMIXSTREAM pMix = &pStream->Mix;
818
819 rc = AudioTestMixStreamEnable(pMix);
820 if (RT_SUCCESS(rc))
821 {
822 uint32_t cbRecTotal = 0; /* Counts everything, including silence / whatever. */
823 uint32_t cbTestToRec = PDMAudioPropsMilliToBytes(&pStream->Cfg.Props, pParms->msDuration);
824 uint32_t cbTestRec = 0;
825
826 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Recording %RU32 bytes total\n", idxTest, cbTestToRec);
827
828 /* We expect a pre + post beacon before + after the actual test tone.
829 * We always start with the pre beacon. */
830 AUDIOTESTTONEBEACON Beacon;
831 AudioTestBeaconInit(&Beacon, (uint8_t)pParms->Hdr.idxTest, AUDIOTESTTONEBEACONTYPE_PLAY_PRE, &pStream->Cfg.Props);
832
833 uint32_t const cbBeacon = AudioTestBeaconGetSize(&Beacon);
834 if (cbBeacon)
835 {
836 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Expecting 2 x %RU32 bytes pre/post beacons\n",
837 idxTest, cbBeacon);
838 if (g_uVerbosity >= 2)
839 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Waiting for %s beacon ...\n",
840 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
841 }
842
843 AudioTestObjAddMetadataStr(Obj, "test_id=%04RU32\n", pParms->Hdr.idxTest);
844 AudioTestObjAddMetadataStr(Obj, "beacon_type=%RU32\n", (uint32_t)AudioTestBeaconGetType(&Beacon));
845 AudioTestObjAddMetadataStr(Obj, "beacon_pre_bytes=%RU32\n", cbBeacon);
846 AudioTestObjAddMetadataStr(Obj, "beacon_post_bytes=%RU32\n", cbBeacon);
847 AudioTestObjAddMetadataStr(Obj, "stream_to_record_bytes=%RU32\n", cbTestToRec);
848 AudioTestObjAddMetadataStr(Obj, "stream_buffer_size_ms=%RU32\n", pIoOpts->cMsBufferSize);
849 AudioTestObjAddMetadataStr(Obj, "stream_prebuf_size_ms=%RU32\n", pIoOpts->cMsPreBuffer);
850 /* Note: This mostly is provided by backend (e.g. PulseAudio / ALSA / ++) and
851 * has nothing to do with the device emulation scheduling hint. */
852 AudioTestObjAddMetadataStr(Obj, "device_scheduling_hint_ms=%RU32\n", pIoOpts->cMsSchedulingHint);
853
854 uint8_t abSamples[16384];
855 uint32_t const cbSamplesAligned = PDMAudioPropsFloorBytesToFrame(pMix->pProps, sizeof(abSamples));
856
857 uint64_t const nsStarted = RTTimeNanoTS();
858
859 uint64_t nsTimeout = RT_MS_5MIN_64 * RT_NS_1MS;
860 uint64_t nsLastMsgCantRead = 0; /* Timestamp (in ns) when the last message of an unreadable stream was shown. */
861
862 AUDIOTESTSTATE enmState = AUDIOTESTSTATE_PRE;
863
864 while (!g_fTerminate)
865 {
866 uint64_t const nsNow = RTTimeNanoTS();
867
868 /*
869 * Anything we can read?
870 */
871 uint32_t const cbCanRead = AudioTestMixStreamGetReadable(pMix);
872 if (cbCanRead)
873 {
874 if (g_uVerbosity >= 3)
875 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Stream is readable with %RU64ms (%RU32 bytes)\n",
876 idxTest, PDMAudioPropsBytesToMilli(pMix->pProps, cbCanRead), cbCanRead);
877
878 uint32_t const cbToRead = RT_MIN(cbCanRead, cbSamplesAligned);
879 uint32_t cbRecorded = 0;
880 rc = AudioTestMixStreamCapture(pMix, abSamples, cbToRead, &cbRecorded);
881 if (RT_SUCCESS(rc))
882 {
883 /* Flag indicating whether the whole block we're going to play is silence or not. */
884 bool const fIsAllSilence = PDMAudioPropsIsBufferSilence(&pStream->pStream->Cfg.Props, abSamples, cbRecorded);
885
886 cbRecTotal += cbRecorded; /* Do a bit of accounting. */
887
888 switch (enmState)
889 {
890 case AUDIOTESTSTATE_PRE:
891 RT_FALL_THROUGH();
892 case AUDIOTESTSTATE_POST:
893 {
894 bool fGoToNextStage = false;
895
896 if ( AudioTestBeaconGetSize(&Beacon)
897 && !AudioTestBeaconIsComplete(&Beacon))
898 {
899 bool const fStarted = AudioTestBeaconGetRemaining(&Beacon) == AudioTestBeaconGetSize(&Beacon);
900
901 size_t uOff;
902 rc = AudioTestBeaconAddConsecutive(&Beacon, abSamples, cbRecorded, &uOff);
903 if (RT_SUCCESS(rc))
904 {
905 /*
906 * When being in the AUDIOTESTSTATE_PRE state, we might get more audio data
907 * than we need for the pre-beacon to complete. In other words, that "more data"
908 * needs to be counted to the actual recorded test tone data then.
909 */
910 if (enmState == AUDIOTESTSTATE_PRE)
911 cbTestRec += cbRecorded - (uint32_t)uOff;
912 }
913
914 if ( fStarted
915 && g_uVerbosity >= 3)
916 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
917 "Test #%RU32: Detection of %s beacon started (%RU32ms recorded so far)\n",
918 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType),
919 PDMAudioPropsBytesToMilli(&pStream->pStream->Cfg.Props, cbRecTotal));
920
921 if (AudioTestBeaconIsComplete(&Beacon))
922 {
923 if (g_uVerbosity >= 2)
924 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Detected %s beacon\n",
925 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
926 fGoToNextStage = true;
927 }
928 }
929 else
930 fGoToNextStage = true;
931
932 if (fGoToNextStage)
933 {
934 if (enmState == AUDIOTESTSTATE_PRE)
935 enmState = AUDIOTESTSTATE_RUN;
936 else if (enmState == AUDIOTESTSTATE_POST)
937 enmState = AUDIOTESTSTATE_DONE;
938 }
939 break;
940 }
941
942 case AUDIOTESTSTATE_RUN:
943 {
944 /* Whether we count all silence as recorded data or not.
945 * Currently we don't, as otherwise consequtively played tones will be cut off in the end. */
946 if (!fIsAllSilence)
947 {
948 uint32_t const cbToAddMax = cbTestToRec - cbTestRec;
949
950 /* Don't read more than we're told to.
951 * After the actual test tone data there might come a post beacon which also
952 * needs to be handled in the AUDIOTESTSTATE_POST state then. */
953 if (cbRecorded > cbToAddMax)
954 cbRecorded = cbToAddMax;
955
956 cbTestRec += cbRecorded;
957 }
958
959 if (cbTestToRec - cbTestRec == 0) /* Done recording the test tone? */
960 {
961 enmState = AUDIOTESTSTATE_POST;
962
963 if (g_uVerbosity >= 2)
964 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Recording tone data done", idxTest);
965
966 if (AudioTestBeaconGetSize(&Beacon))
967 {
968 /* Re-use the beacon object, but this time it's the post beacon. */
969 AudioTestBeaconInit(&Beacon, (uint8_t)pParms->Hdr.idxTest, AUDIOTESTTONEBEACONTYPE_PLAY_POST,
970 &pStream->Cfg.Props);
971 if (g_uVerbosity >= 2)
972 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
973 "Test #%RU32: Waiting for %s beacon ...",
974 idxTest, AudioTestBeaconTypeGetName(Beacon.enmType));
975 }
976 }
977 break;
978 }
979
980 case AUDIOTESTSTATE_DONE:
981 {
982 /* Nothing to do here. */
983 break;
984 }
985
986 default:
987 AssertFailed();
988 break;
989 }
990 }
991
992 if (cbRecorded)
993 {
994 /* Always write (record) everything, no matter if the current audio contains complete silence or not.
995 * Might be also become handy later if we want to have a look at start/stop timings and so on. */
996 rc = AudioTestObjWrite(Obj, abSamples, cbRecorded);
997 AssertRCBreak(rc);
998 }
999
1000 if (enmState == AUDIOTESTSTATE_DONE) /* Bail out when in state "done". */
1001 break;
1002 }
1003 else if (AudioTestMixStreamIsOkay(pMix))
1004 {
1005 RTMSINTERVAL const msSleep = RT_MIN(RT_MAX(1, pStream->Cfg.Device.cMsSchedulingHint), 256);
1006
1007 if ( g_uVerbosity >= 3
1008 && ( !nsLastMsgCantRead
1009 || (nsNow - nsLastMsgCantRead) > RT_NS_10SEC)) /* Don't spam the output too much. */
1010 {
1011 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Waiting %RU32ms for stream to be readable again ...\n",
1012 idxTest, msSleep);
1013 nsLastMsgCantRead = nsNow;
1014 }
1015
1016 RTThreadSleep(msSleep);
1017 }
1018
1019 /* Fail-safe in case something screwed up while playing back. */
1020 uint64_t const cNsElapsed = nsNow - nsStarted;
1021 if (cNsElapsed > nsTimeout)
1022 {
1023 RTTestFailed(g_hTest, "Test #%RU32: Recording took too long (running %RU64 vs. timeout %RU64), aborting\n",
1024 idxTest, cNsElapsed, nsTimeout);
1025 rc = VERR_TIMEOUT;
1026 }
1027
1028 if (RT_FAILURE(rc))
1029 break;
1030 }
1031
1032 if (g_uVerbosity >= 2)
1033 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Recorded %RU32 bytes total\n", idxTest, cbRecTotal);
1034 if (cbTestRec != cbTestToRec)
1035 {
1036 RTTestFailed(g_hTest, "Test #%RU32: Recording ended unexpectedly (%RU32/%RU32 recorded)\n",
1037 idxTest, cbTestRec, cbTestToRec);
1038 rc = VERR_WRONG_ORDER; /** @todo Find a better rc. */
1039 }
1040
1041 if (RT_FAILURE(rc))
1042 RTTestFailed(g_hTest, "Test #%RU32: Recording failed (state is '%s')\n", idxTest, AudioTestStateToStr(enmState));
1043
1044 int rc2 = AudioTestMixStreamDisable(pMix);
1045 if (RT_SUCCESS(rc))
1046 rc = rc2;
1047 }
1048
1049 int rc2 = AudioTestObjClose(Obj);
1050 if (RT_SUCCESS(rc))
1051 rc = rc2;
1052
1053 if (RT_FAILURE(rc))
1054 RTTestFailed(g_hTest, "Test #%RU32: Recording tone done failed with %Rrc\n", idxTest, rc);
1055
1056 return rc;
1057}
1058
1059
1060/*********************************************************************************************************************************
1061* ATS Callback Implementations *
1062*********************************************************************************************************************************/
1063
1064/** @copydoc ATSCALLBACKS::pfnHowdy
1065 *
1066 * @note Runs as part of the guest ATS.
1067 */
1068static DECLCALLBACK(int) audioTestGstAtsHowdyCallback(void const *pvUser)
1069{
1070 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1071
1072 AssertReturn(pCtx->cClients <= UINT8_MAX - 1, VERR_BUFFER_OVERFLOW);
1073
1074 pCtx->cClients++;
1075
1076 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "New client connected, now %RU8 total\n", pCtx->cClients);
1077
1078 return VINF_SUCCESS;
1079}
1080
1081/** @copydoc ATSCALLBACKS::pfnBye
1082 *
1083 * @note Runs as part of the guest ATS.
1084 */
1085static DECLCALLBACK(int) audioTestGstAtsByeCallback(void const *pvUser)
1086{
1087 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1088
1089 AssertReturn(pCtx->cClients, VERR_WRONG_ORDER);
1090 pCtx->cClients--;
1091
1092 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Client wants to disconnect, %RU8 remaining\n", pCtx->cClients);
1093
1094 if (0 == pCtx->cClients) /* All clients disconnected? Tear things down. */
1095 {
1096 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Last client disconnected, terminating server ...\n");
1097 ASMAtomicWriteBool(&g_fTerminate, true);
1098 }
1099
1100 return VINF_SUCCESS;
1101}
1102
1103/** @copydoc ATSCALLBACKS::pfnTestSetBegin
1104 *
1105 * @note Runs as part of the guest ATS.
1106 */
1107static DECLCALLBACK(int) audioTestGstAtsTestSetBeginCallback(void const *pvUser, const char *pszTag)
1108{
1109 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1110 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
1111
1112 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for beginning test set '%s' in '%s'\n", pszTag, pTstEnv->szPathTemp);
1113
1114 return AudioTestSetCreate(&pTstEnv->Set, pTstEnv->szPathTemp, pszTag);
1115}
1116
1117/** @copydoc ATSCALLBACKS::pfnTestSetEnd
1118 *
1119 * @note Runs as part of the guest ATS.
1120 */
1121static DECLCALLBACK(int) audioTestGstAtsTestSetEndCallback(void const *pvUser, const char *pszTag)
1122{
1123 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1124 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
1125
1126 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for ending test set '%s'\n", pszTag);
1127
1128 /* Pack up everything to be ready for transmission. */
1129 return audioTestEnvPrologue(pTstEnv, true /* fPack */, pCtx->szTestSetArchive, sizeof(pCtx->szTestSetArchive));
1130}
1131
1132/** @copydoc ATSCALLBACKS::pfnTonePlay
1133 *
1134 * @note Runs as part of the guest ATS.
1135 */
1136static DECLCALLBACK(int) audioTestGstAtsTonePlayCallback(void const *pvUser, PAUDIOTESTTONEPARMS pToneParms)
1137{
1138 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1139 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
1140 PAUDIOTESTIOOPTS pIoOpts = &pTstEnv->IoOpts;
1141
1142 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for playing test tone #%RU32 (%RU16Hz, %RU32ms) ...\n",
1143 pToneParms->Hdr.idxTest, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
1144
1145 char szTimeCreated[RTTIME_STR_LEN];
1146 RTTimeToString(&pToneParms->Hdr.tsCreated, szTimeCreated, sizeof(szTimeCreated));
1147 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test created (caller UTC): %s\n", szTimeCreated);
1148
1149 const PAUDIOTESTSTREAM pTstStream = &pTstEnv->aStreams[0]; /** @todo Make this dynamic. */
1150
1151 int rc = audioTestStreamInit(pTstEnv->pDrvStack, pTstStream, PDMAUDIODIR_OUT, pIoOpts);
1152 if (RT_SUCCESS(rc))
1153 {
1154 AUDIOTESTPARMS TstParms;
1155 RT_ZERO(TstParms);
1156 TstParms.enmType = AUDIOTESTTYPE_TESTTONE_PLAY;
1157 TstParms.enmDir = PDMAUDIODIR_OUT;
1158 TstParms.TestTone = *pToneParms;
1159
1160 PAUDIOTESTENTRY pTst;
1161 rc = AudioTestSetTestBegin(&pTstEnv->Set, "Playing test tone", &TstParms, &pTst);
1162 if (RT_SUCCESS(rc))
1163 {
1164 rc = audioTestPlayTone(&pTstEnv->IoOpts, pTstEnv, pTstStream, pToneParms);
1165 if (RT_SUCCESS(rc))
1166 {
1167 AudioTestSetTestDone(pTst);
1168 }
1169 else
1170 AudioTestSetTestFailed(pTst, rc, "Playing tone failed");
1171 }
1172
1173 int rc2 = audioTestStreamDestroy(pTstEnv->pDrvStack, pTstStream);
1174 if (RT_SUCCESS(rc))
1175 rc = rc2;
1176 }
1177 else
1178 RTTestFailed(g_hTest, "Error creating output stream, rc=%Rrc\n", rc);
1179
1180 return rc;
1181}
1182
1183/** @copydoc ATSCALLBACKS::pfnToneRecord */
1184static DECLCALLBACK(int) audioTestGstAtsToneRecordCallback(void const *pvUser, PAUDIOTESTTONEPARMS pToneParms)
1185{
1186 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1187 PAUDIOTESTENV pTstEnv = pCtx->pTstEnv;
1188 PAUDIOTESTIOOPTS pIoOpts = &pTstEnv->IoOpts;
1189
1190 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Got request for recording test tone #%RU32 (%RU32ms) ...\n",
1191 pToneParms->Hdr.idxTest, pToneParms->msDuration);
1192
1193 char szTimeCreated[RTTIME_STR_LEN];
1194 RTTimeToString(&pToneParms->Hdr.tsCreated, szTimeCreated, sizeof(szTimeCreated));
1195 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test created (caller UTC): %s\n", szTimeCreated);
1196
1197 const PAUDIOTESTSTREAM pTstStream = &pTstEnv->aStreams[0]; /** @todo Make this dynamic. */
1198
1199 int rc = audioTestStreamInit(pTstEnv->pDrvStack, pTstStream, PDMAUDIODIR_IN, pIoOpts);
1200 if (RT_SUCCESS(rc))
1201 {
1202 AUDIOTESTPARMS TstParms;
1203 RT_ZERO(TstParms);
1204 TstParms.enmType = AUDIOTESTTYPE_TESTTONE_RECORD;
1205 TstParms.enmDir = PDMAUDIODIR_IN;
1206 TstParms.TestTone = *pToneParms;
1207
1208 PAUDIOTESTENTRY pTst;
1209 rc = AudioTestSetTestBegin(&pTstEnv->Set, "Recording test tone from host", &TstParms, &pTst);
1210 if (RT_SUCCESS(rc))
1211 {
1212 rc = audioTestRecordTone(pIoOpts, pTstEnv, pTstStream, pToneParms);
1213 if (RT_SUCCESS(rc))
1214 {
1215 AudioTestSetTestDone(pTst);
1216 }
1217 else
1218 AudioTestSetTestFailed(pTst, rc, "Recording tone failed");
1219 }
1220
1221 int rc2 = audioTestStreamDestroy(pTstEnv->pDrvStack, pTstStream);
1222 if (RT_SUCCESS(rc))
1223 rc = rc2;
1224 }
1225 else
1226 RTTestFailed(g_hTest, "Error creating input stream, rc=%Rrc\n", rc);
1227
1228 return rc;
1229}
1230
1231/** @copydoc ATSCALLBACKS::pfnTestSetSendBegin */
1232static DECLCALLBACK(int) audioTestGstAtsTestSetSendBeginCallback(void const *pvUser, const char *pszTag)
1233{
1234 RT_NOREF(pszTag);
1235
1236 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1237
1238 if (!RTFileExists(pCtx->szTestSetArchive)) /* Has the archive successfully been created yet? */
1239 return VERR_WRONG_ORDER;
1240
1241 int rc = RTFileOpen(&pCtx->hTestSetArchive, pCtx->szTestSetArchive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1242 if (RT_SUCCESS(rc))
1243 {
1244 uint64_t uSize;
1245 rc = RTFileQuerySize(pCtx->hTestSetArchive, &uSize);
1246 if (RT_SUCCESS(rc))
1247 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Sending test set '%s' (%zu bytes)\n", pCtx->szTestSetArchive, uSize);
1248 }
1249
1250 return rc;
1251}
1252
1253/** @copydoc ATSCALLBACKS::pfnTestSetSendRead */
1254static DECLCALLBACK(int) audioTestGstAtsTestSetSendReadCallback(void const *pvUser,
1255 const char *pszTag, void *pvBuf, size_t cbBuf, size_t *pcbRead)
1256{
1257 RT_NOREF(pszTag);
1258
1259 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1260
1261 return RTFileRead(pCtx->hTestSetArchive, pvBuf, cbBuf, pcbRead);
1262}
1263
1264/** @copydoc ATSCALLBACKS::pfnTestSetSendEnd */
1265static DECLCALLBACK(int) audioTestGstAtsTestSetSendEndCallback(void const *pvUser, const char *pszTag)
1266{
1267 RT_NOREF(pszTag);
1268
1269 PATSCALLBACKCTX pCtx = (PATSCALLBACKCTX)pvUser;
1270
1271 int rc = RTFileClose(pCtx->hTestSetArchive);
1272 if (RT_SUCCESS(rc))
1273 {
1274 pCtx->hTestSetArchive = NIL_RTFILE;
1275 }
1276
1277 return rc;
1278}
1279
1280
1281/*********************************************************************************************************************************
1282* Implementation of audio test environment handling *
1283*********************************************************************************************************************************/
1284
1285/**
1286 * Connects an ATS client via TCP/IP to a peer.
1287 *
1288 * @returns VBox status code.
1289 * @param pTstEnv Test environment to use.
1290 * @param pClient Client to connect.
1291 * @param pszWhat Hint of what to connect to where.
1292 * @param pTcpOpts Pointer to TCP options to use.
1293 */
1294int audioTestEnvConnectViaTcp(PAUDIOTESTENV pTstEnv, PATSCLIENT pClient, const char *pszWhat, PAUDIOTESTENVTCPOPTS pTcpOpts)
1295{
1296 RT_NOREF(pTstEnv);
1297
1298 RTGETOPTUNION Val;
1299 RT_ZERO(Val);
1300
1301 Val.u32 = pTcpOpts->enmConnMode;
1302 int rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONN_MODE, &Val);
1303 AssertRCReturn(rc, rc);
1304
1305 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
1306 || pTcpOpts->enmConnMode == ATSCONNMODE_SERVER)
1307 {
1308 Assert(pTcpOpts->uBindPort); /* Always set by the caller. */
1309 Val.u16 = pTcpOpts->uBindPort;
1310 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_BIND_PORT, &Val);
1311 AssertRCReturn(rc, rc);
1312
1313 if (pTcpOpts->szBindAddr[0])
1314 {
1315 Val.psz = pTcpOpts->szBindAddr;
1316 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_BIND_ADDRESS, &Val);
1317 AssertRCReturn(rc, rc);
1318 }
1319 else
1320 {
1321 RTTestFailed(g_hTest, "No bind address specified!\n");
1322 return VERR_INVALID_PARAMETER;
1323 }
1324
1325 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Connecting %s by listening as server at %s:%RU32 ...\n",
1326 pszWhat, pTcpOpts->szBindAddr, pTcpOpts->uBindPort);
1327 }
1328
1329
1330 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
1331 || pTcpOpts->enmConnMode == ATSCONNMODE_CLIENT)
1332 {
1333 Assert(pTcpOpts->uConnectPort); /* Always set by the caller. */
1334 Val.u16 = pTcpOpts->uConnectPort;
1335 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONNECT_PORT, &Val);
1336 AssertRCReturn(rc, rc);
1337
1338 if (pTcpOpts->szConnectAddr[0])
1339 {
1340 Val.psz = pTcpOpts->szConnectAddr;
1341 rc = AudioTestSvcClientHandleOption(pClient, ATSTCPOPT_CONNECT_ADDRESS, &Val);
1342 AssertRCReturn(rc, rc);
1343 }
1344 else
1345 {
1346 RTTestFailed(g_hTest, "No connect address specified!\n");
1347 return VERR_INVALID_PARAMETER;
1348 }
1349
1350 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Connecting %s by connecting as client to %s:%RU32 ...\n",
1351 pszWhat, pTcpOpts->szConnectAddr, pTcpOpts->uConnectPort);
1352 }
1353
1354 rc = AudioTestSvcClientConnect(pClient);
1355 if (RT_FAILURE(rc))
1356 {
1357 RTTestFailed(g_hTest, "Connecting %s failed with %Rrc\n", pszWhat, rc);
1358 return rc;
1359 }
1360
1361 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Successfully connected %s\n", pszWhat);
1362 return rc;
1363}
1364
1365/**
1366 * Configures and starts an ATS TCP/IP server.
1367 *
1368 * @returns VBox status code.
1369 * @param pSrv ATS server instance to configure and start.
1370 * @param pCallbacks ATS callback table to use.
1371 * @param pszDesc Hint of server type which is being started.
1372 * @param pTcpOpts TCP options to use.
1373 */
1374int audioTestEnvConfigureAndStartTcpServer(PATSSERVER pSrv, PCATSCALLBACKS pCallbacks, const char *pszDesc,
1375 PAUDIOTESTENVTCPOPTS pTcpOpts)
1376{
1377 RTGETOPTUNION Val;
1378 RT_ZERO(Val);
1379
1380 int rc = AudioTestSvcInit(pSrv, pCallbacks);
1381 if (RT_FAILURE(rc))
1382 return rc;
1383
1384 Val.u32 = pTcpOpts->enmConnMode;
1385 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONN_MODE, &Val);
1386 AssertRCReturn(rc, rc);
1387
1388 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
1389 || pTcpOpts->enmConnMode == ATSCONNMODE_SERVER)
1390 {
1391 Assert(pTcpOpts->uBindPort); /* Always set by the caller. */
1392 Val.u16 = pTcpOpts->uBindPort;
1393 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_BIND_PORT, &Val);
1394 AssertRCReturn(rc, rc);
1395
1396 if (pTcpOpts->szBindAddr[0])
1397 {
1398 Val.psz = pTcpOpts->szBindAddr;
1399 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_BIND_ADDRESS, &Val);
1400 AssertRCReturn(rc, rc);
1401 }
1402 else
1403 {
1404 RTTestFailed(g_hTest, "No bind address specified!\n");
1405 return VERR_INVALID_PARAMETER;
1406 }
1407
1408 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Starting server for %s at %s:%RU32 ...\n",
1409 pszDesc, pTcpOpts->szBindAddr, pTcpOpts->uBindPort);
1410 }
1411
1412
1413 if ( pTcpOpts->enmConnMode == ATSCONNMODE_BOTH
1414 || pTcpOpts->enmConnMode == ATSCONNMODE_CLIENT)
1415 {
1416 Assert(pTcpOpts->uConnectPort); /* Always set by the caller. */
1417 Val.u16 = pTcpOpts->uConnectPort;
1418 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONNECT_PORT, &Val);
1419 AssertRCReturn(rc, rc);
1420
1421 if (pTcpOpts->szConnectAddr[0])
1422 {
1423 Val.psz = pTcpOpts->szConnectAddr;
1424 rc = AudioTestSvcHandleOption(pSrv, ATSTCPOPT_CONNECT_ADDRESS, &Val);
1425 AssertRCReturn(rc, rc);
1426 }
1427 else
1428 {
1429 RTTestFailed(g_hTest, "No connect address specified!\n");
1430 return VERR_INVALID_PARAMETER;
1431 }
1432
1433 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Starting server for %s by connecting as client to %s:%RU32 ...\n",
1434 pszDesc, pTcpOpts->szConnectAddr, pTcpOpts->uConnectPort);
1435 }
1436
1437 if (RT_SUCCESS(rc))
1438 {
1439 rc = AudioTestSvcStart(pSrv);
1440 if (RT_FAILURE(rc))
1441 RTTestFailed(g_hTest, "Starting server for %s failed with %Rrc\n", pszDesc, rc);
1442 }
1443
1444 return rc;
1445}
1446
1447/**
1448 * Initializes an audio test environment.
1449 *
1450 * @param pTstEnv Audio test environment to initialize.
1451 */
1452void audioTestEnvInit(PAUDIOTESTENV pTstEnv)
1453{
1454 RT_BZERO(pTstEnv, sizeof(AUDIOTESTENV));
1455
1456 audioTestIoOptsInitDefaults(&pTstEnv->IoOpts);
1457 audioTestToneParmsInit(&pTstEnv->ToneParms);
1458}
1459
1460/**
1461 * Creates an audio test environment.
1462 *
1463 * @returns VBox status code.
1464 * @param pTstEnv Audio test environment to create.
1465 * @param pDrvStack Driver stack to use.
1466 */
1467int audioTestEnvCreate(PAUDIOTESTENV pTstEnv, PAUDIOTESTDRVSTACK pDrvStack)
1468{
1469 AssertReturn(PDMAudioPropsAreValid(&pTstEnv->IoOpts.Props), VERR_WRONG_ORDER);
1470
1471 int rc = VINF_SUCCESS;
1472
1473 pTstEnv->pDrvStack = pDrvStack;
1474
1475 /*
1476 * Set sane defaults if not already set.
1477 */
1478 if (!RTStrNLen(pTstEnv->szTag, sizeof(pTstEnv->szTag)))
1479 {
1480 rc = AudioTestGenTag(pTstEnv->szTag, sizeof(pTstEnv->szTag));
1481 AssertRCReturn(rc, rc);
1482 }
1483
1484 if (!RTStrNLen(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp)))
1485 {
1486 rc = AudioTestPathGetTemp(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp));
1487 AssertRCReturn(rc, rc);
1488 }
1489
1490 if (!RTStrNLen(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut)))
1491 {
1492 rc = RTPathJoin(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut), pTstEnv->szPathTemp, "vkat-temp");
1493 AssertRCReturn(rc, rc);
1494 }
1495
1496 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Initializing environment for mode '%s'\n", pTstEnv->enmMode == AUDIOTESTMODE_HOST ? "host" : "guest");
1497 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using tag '%s'\n", pTstEnv->szTag);
1498 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Output directory is '%s'\n", pTstEnv->szPathOut);
1499 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Temp directory is '%s'\n", pTstEnv->szPathTemp);
1500
1501 char szPathTemp[RTPATH_MAX];
1502 if ( !strlen(pTstEnv->szPathTemp)
1503 || !strlen(pTstEnv->szPathOut))
1504 rc = RTPathTemp(szPathTemp, sizeof(szPathTemp));
1505
1506 if ( RT_SUCCESS(rc)
1507 && !strlen(pTstEnv->szPathTemp))
1508 rc = RTPathJoin(pTstEnv->szPathTemp, sizeof(pTstEnv->szPathTemp), szPathTemp, "vkat-temp");
1509
1510 if (RT_SUCCESS(rc))
1511 {
1512 rc = RTDirCreate(pTstEnv->szPathTemp, RTFS_UNIX_IRWXU, 0 /* fFlags */);
1513 if (rc == VERR_ALREADY_EXISTS)
1514 rc = VINF_SUCCESS;
1515 }
1516
1517 if ( RT_SUCCESS(rc)
1518 && !strlen(pTstEnv->szPathOut))
1519 rc = RTPathJoin(pTstEnv->szPathOut, sizeof(pTstEnv->szPathOut), szPathTemp, "vkat");
1520
1521 if (RT_SUCCESS(rc))
1522 {
1523 rc = RTDirCreate(pTstEnv->szPathOut, RTFS_UNIX_IRWXU, 0 /* fFlags */);
1524 if (rc == VERR_ALREADY_EXISTS)
1525 rc = VINF_SUCCESS;
1526 }
1527
1528 if (RT_FAILURE(rc))
1529 return rc;
1530
1531 /**
1532 * For NAT'ed VMs we use (default):
1533 * - client mode (uConnectAddr / uConnectPort) on the guest.
1534 * - server mode (uBindAddr / uBindPort) on the host.
1535 */
1536 if ( !pTstEnv->TcpOpts.szConnectAddr[0]
1537 && !pTstEnv->TcpOpts.szBindAddr[0])
1538 RTStrCopy(pTstEnv->TcpOpts.szBindAddr, sizeof(pTstEnv->TcpOpts.szBindAddr), "0.0.0.0");
1539
1540 /*
1541 * Determine connection mode based on set variables.
1542 */
1543 if ( pTstEnv->TcpOpts.szBindAddr[0]
1544 && pTstEnv->TcpOpts.szConnectAddr[0])
1545 {
1546 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_BOTH;
1547 }
1548 else if (pTstEnv->TcpOpts.szBindAddr[0])
1549 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_SERVER;
1550 else /* "Reversed mode", i.e. used for NATed VMs. */
1551 pTstEnv->TcpOpts.enmConnMode = ATSCONNMODE_CLIENT;
1552
1553 /* Set a back reference to the test environment for the callback context. */
1554 pTstEnv->CallbackCtx.pTstEnv = pTstEnv;
1555
1556 ATSCALLBACKS Callbacks;
1557 RT_ZERO(Callbacks);
1558 Callbacks.pvUser = &pTstEnv->CallbackCtx;
1559
1560 if (pTstEnv->enmMode == AUDIOTESTMODE_GUEST)
1561 {
1562 Callbacks.pfnHowdy = audioTestGstAtsHowdyCallback;
1563 Callbacks.pfnBye = audioTestGstAtsByeCallback;
1564 Callbacks.pfnTestSetBegin = audioTestGstAtsTestSetBeginCallback;
1565 Callbacks.pfnTestSetEnd = audioTestGstAtsTestSetEndCallback;
1566 Callbacks.pfnTonePlay = audioTestGstAtsTonePlayCallback;
1567 Callbacks.pfnToneRecord = audioTestGstAtsToneRecordCallback;
1568 Callbacks.pfnTestSetSendBegin = audioTestGstAtsTestSetSendBeginCallback;
1569 Callbacks.pfnTestSetSendRead = audioTestGstAtsTestSetSendReadCallback;
1570 Callbacks.pfnTestSetSendEnd = audioTestGstAtsTestSetSendEndCallback;
1571
1572 if (!pTstEnv->TcpOpts.uBindPort)
1573 pTstEnv->TcpOpts.uBindPort = ATS_TCP_DEF_BIND_PORT_GUEST;
1574
1575 if (!pTstEnv->TcpOpts.uConnectPort)
1576 pTstEnv->TcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_GUEST;
1577
1578 pTstEnv->pSrv = (PATSSERVER)RTMemAlloc(sizeof(ATSSERVER));
1579 AssertPtrReturn(pTstEnv->pSrv, VERR_NO_MEMORY);
1580
1581 /*
1582 * Start the ATS (Audio Test Service) on the guest side.
1583 * That service then will perform playback and recording operations on the guest, triggered from the host.
1584 *
1585 * When running this in self-test mode, that service also can be run on the host if nothing else is specified.
1586 * Note that we have to bind to "0.0.0.0" by default so that the host can connect to it.
1587 */
1588 rc = audioTestEnvConfigureAndStartTcpServer(pTstEnv->pSrv, &Callbacks, "guest", &pTstEnv->TcpOpts);
1589 }
1590 else /* Host mode */
1591 {
1592 if (!pTstEnv->TcpOpts.uBindPort)
1593 pTstEnv->TcpOpts.uBindPort = ATS_TCP_DEF_BIND_PORT_HOST;
1594
1595 if (!pTstEnv->TcpOpts.uConnectPort)
1596 pTstEnv->TcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_HOST_PORT_FWD;
1597
1598 /**
1599 * Note: Don't set pTstEnv->TcpOpts.szTcpConnectAddr by default here, as this specifies what connection mode
1600 * (client / server / both) we use on the host.
1601 */
1602
1603 /* We need to start a server on the host so that VMs configured with NAT networking
1604 * can connect to it as well. */
1605 rc = AudioTestSvcClientCreate(&pTstEnv->u.Host.AtsClGuest);
1606 if (RT_SUCCESS(rc))
1607 rc = audioTestEnvConnectViaTcp(pTstEnv, &pTstEnv->u.Host.AtsClGuest,
1608 "host -> guest", &pTstEnv->TcpOpts);
1609 if (RT_SUCCESS(rc))
1610 {
1611 AUDIOTESTENVTCPOPTS ValKitTcpOpts;
1612 RT_ZERO(ValKitTcpOpts);
1613
1614 /* We only connect as client to the Validation Kit audio driver ATS. */
1615 ValKitTcpOpts.enmConnMode = ATSCONNMODE_CLIENT;
1616
1617 /* For now we ASSUME that the Validation Kit audio driver ATS runs on the same host as VKAT (this binary) runs on. */
1618 ValKitTcpOpts.uConnectPort = ATS_TCP_DEF_CONNECT_PORT_VALKIT; /** @todo Make this dynamic. */
1619 RTStrCopy(ValKitTcpOpts.szConnectAddr, sizeof(ValKitTcpOpts.szConnectAddr), ATS_TCP_DEF_CONNECT_HOST_ADDR_STR); /** @todo Ditto. */
1620
1621 rc = AudioTestSvcClientCreate(&pTstEnv->u.Host.AtsClValKit);
1622 if (RT_SUCCESS(rc))
1623 {
1624 rc = audioTestEnvConnectViaTcp(pTstEnv, &pTstEnv->u.Host.AtsClValKit,
1625 "host -> valkit", &ValKitTcpOpts);
1626 if (RT_FAILURE(rc))
1627 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Unable to connect to the Validation Kit audio driver!\n"
1628 "There could be multiple reasons:\n\n"
1629 " - Wrong host being used\n"
1630 " - VirtualBox host version is too old\n"
1631 " - Audio debug mode is not enabled\n"
1632 " - Support for Validation Kit audio driver is not included\n"
1633 " - Firewall / network configuration problem\n");
1634 }
1635 }
1636 }
1637
1638 return rc;
1639}
1640
1641/**
1642 * Destroys an audio test environment.
1643 *
1644 * @param pTstEnv Audio test environment to destroy.
1645 */
1646void audioTestEnvDestroy(PAUDIOTESTENV pTstEnv)
1647{
1648 if (!pTstEnv)
1649 return;
1650
1651 /* When in host mode, we need to destroy our ATS clients in order to also let
1652 * the ATS server(s) know we're going to quit. */
1653 if (pTstEnv->enmMode == AUDIOTESTMODE_HOST)
1654 {
1655 AudioTestSvcClientDestroy(&pTstEnv->u.Host.AtsClValKit);
1656 AudioTestSvcClientDestroy(&pTstEnv->u.Host.AtsClGuest);
1657 }
1658
1659 if (pTstEnv->pSrv)
1660 {
1661 int rc2 = AudioTestSvcDestroy(pTstEnv->pSrv);
1662 AssertRC(rc2);
1663
1664 RTMemFree(pTstEnv->pSrv);
1665 pTstEnv->pSrv = NULL;
1666 }
1667
1668 for (unsigned i = 0; i < RT_ELEMENTS(pTstEnv->aStreams); i++)
1669 {
1670 int rc2 = audioTestStreamDestroy(pTstEnv->pDrvStack, &pTstEnv->aStreams[i]);
1671 if (RT_FAILURE(rc2))
1672 RTTestFailed(g_hTest, "Stream destruction for stream #%u failed with %Rrc\n", i, rc2);
1673 }
1674
1675 /* Try cleaning up a bit. */
1676 RTDirRemove(pTstEnv->szPathTemp);
1677 RTDirRemove(pTstEnv->szPathOut);
1678
1679 pTstEnv->pDrvStack = NULL;
1680}
1681
1682/**
1683 * Closes, packs up and destroys a test environment.
1684 *
1685 * @returns VBox status code.
1686 * @param pTstEnv Test environment to handle.
1687 * @param fPack Whether to pack the test set up before destroying / wiping it.
1688 * @param pszPackFile Where to store the packed test set file on success. Can be NULL if \a fPack is \c false.
1689 * @param cbPackFile Size (in bytes) of \a pszPackFile. Can be 0 if \a fPack is \c false.
1690 */
1691int audioTestEnvPrologue(PAUDIOTESTENV pTstEnv, bool fPack, char *pszPackFile, size_t cbPackFile)
1692{
1693 /* Close the test set first. */
1694 AudioTestSetClose(&pTstEnv->Set);
1695
1696 int rc = VINF_SUCCESS;
1697
1698 if (fPack)
1699 {
1700 /* Before destroying the test environment, pack up the test set so
1701 * that it's ready for transmission. */
1702 rc = AudioTestSetPack(&pTstEnv->Set, pTstEnv->szPathOut, pszPackFile, cbPackFile);
1703 if (RT_SUCCESS(rc))
1704 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set packed up to '%s'\n", pszPackFile);
1705 }
1706
1707 if (!g_fDrvAudioDebug) /* Don't wipe stuff when debugging. Can be useful for introspecting data. */
1708 /* ignore rc */ AudioTestSetWipe(&pTstEnv->Set);
1709
1710 AudioTestSetDestroy(&pTstEnv->Set);
1711
1712 if (RT_FAILURE(rc))
1713 RTTestFailed(g_hTest, "Test set prologue failed with %Rrc\n", rc);
1714
1715 return rc;
1716}
1717
1718/**
1719 * Initializes an audio test parameters set.
1720 *
1721 * @param pTstParms Test parameters set to initialize.
1722 */
1723void audioTestParmsInit(PAUDIOTESTPARMS pTstParms)
1724{
1725 RT_ZERO(*pTstParms);
1726}
1727
1728/**
1729 * Destroys an audio test parameters set.
1730 *
1731 * @param pTstParms Test parameters set to destroy.
1732 */
1733void audioTestParmsDestroy(PAUDIOTESTPARMS pTstParms)
1734{
1735 if (!pTstParms)
1736 return;
1737
1738 return;
1739}
1740
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