VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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