VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/vkat.cpp@ 91198

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

Audio/Validation Kit: Also send the test index and test creation time (caller UTC) when starting tests. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.0 KB
Line 
1/* $Id: vkat.cpp 91194 2021-09-10 10:11:03Z vboxsync $ */
2/** @file
3 * Validation Kit Audio Test (VKAT) utility for testing and validating the audio stack.
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
33#include <iprt/buildconfig.h>
34#include <iprt/ctype.h>
35#include <iprt/dir.h>
36#include <iprt/errcore.h>
37#include <iprt/file.h>
38#include <iprt/initterm.h>
39#include <iprt/getopt.h>
40#include <iprt/message.h>
41#include <iprt/path.h>
42#include <iprt/process.h>
43#include <iprt/rand.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/test.h>
47
48#include <package-generated.h>
49#include "product-generated.h"
50
51#include <VBox/version.h>
52#include <VBox/log.h>
53
54#ifdef RT_OS_WINDOWS
55# include <iprt/win/windows.h> /* for CoInitializeEx */
56#endif
57#include <signal.h>
58
59#include "vkatInternal.h"
60
61
62/*********************************************************************************************************************************
63* Internal Functions *
64*********************************************************************************************************************************/
65static int audioVerifyOne(const char *pszPathSetA, const char *pszPathSetB, PAUDIOTESTVERIFYOPTS pOpts);
66
67
68/*********************************************************************************************************************************
69* Global Variables *
70*********************************************************************************************************************************/
71/**
72 * Backends description table.
73 *
74 * @note The first backend in the array is the default one for the platform.
75 */
76AUDIOTESTBACKENDDESC const g_aBackends[] =
77{
78#ifdef VBOX_WITH_AUDIO_PULSE
79 { &g_DrvHostPulseAudio, "pulseaudio" },
80 { &g_DrvHostPulseAudio, "pulse" },
81 { &g_DrvHostPulseAudio, "pa" },
82#endif
83/*
84 * Note: ALSA has to come second so that PulseAudio above always is the default on Linux-y OSes
85 * -- most distros are using an ALSA plugin for PulseAudio nowadays.
86 * However, some of these configurations do not seem to work by default (can't create audio streams).
87 *
88 * If PulseAudio is not available, the (optional) probing ("--probe") will choose the "pure" ALSA stack instead then.
89 */
90#if defined(VBOX_WITH_AUDIO_ALSA) && defined(RT_OS_LINUX)
91 { &g_DrvHostALSAAudio, "alsa" },
92#endif
93#ifdef VBOX_WITH_AUDIO_OSS
94 { &g_DrvHostOSSAudio, "oss" },
95#endif
96#if defined(RT_OS_DARWIN)
97 { &g_DrvHostCoreAudio, "coreaudio" },
98 { &g_DrvHostCoreAudio, "core" },
99 { &g_DrvHostCoreAudio, "ca" },
100#endif
101#if defined(RT_OS_WINDOWS)
102 { &g_DrvHostAudioWas, "wasapi" },
103 { &g_DrvHostAudioWas, "was" },
104 { &g_DrvHostDSound, "directsound" },
105 { &g_DrvHostDSound, "dsound" },
106 { &g_DrvHostDSound, "ds" },
107#endif
108 { &g_DrvHostValidationKitAudio, "valkit" }
109};
110AssertCompile(sizeof(g_aBackends) > 0 /* port me */);
111/** Number of backends defined. */
112unsigned g_cBackends = RT_ELEMENTS(g_aBackends);
113
114/**
115 * Long option values for the 'test' command.
116 */
117enum
118{
119 VKAT_TEST_OPT_COUNT = 900,
120 VKAT_TEST_OPT_DEV,
121 VKAT_TEST_OPT_GUEST_ATS_ADDR,
122 VKAT_TEST_OPT_GUEST_ATS_PORT,
123 VKAT_TEST_OPT_HOST_ATS_ADDR,
124 VKAT_TEST_OPT_HOST_ATS_PORT,
125 VKAT_TEST_OPT_MODE,
126 VKAT_TEST_OPT_NO_VERIFY,
127 VKAT_TEST_OPT_OUTDIR,
128 VKAT_TEST_OPT_PAUSE,
129 VKAT_TEST_OPT_PCM_HZ,
130 VKAT_TEST_OPT_PCM_BIT,
131 VKAT_TEST_OPT_PCM_CHAN,
132 VKAT_TEST_OPT_PCM_SIGNED,
133 VKAT_TEST_OPT_PROBE_BACKENDS,
134 VKAT_TEST_OPT_TAG,
135 VKAT_TEST_OPT_TEMPDIR,
136 VKAT_TEST_OPT_VOL,
137 VKAT_TEST_OPT_TCP_BIND_ADDRESS,
138 VKAT_TEST_OPT_TCP_BIND_PORT,
139 VKAT_TEST_OPT_TCP_CONNECT_ADDRESS,
140 VKAT_TEST_OPT_TCP_CONNECT_PORT
141};
142
143/**
144 * Long option values for the 'verify' command.
145 */
146enum
147{
148 VKAT_VERIFY_OPT_MAX_DIFF_COUNT = 900,
149 VKAT_VERIFY_OPT_MAX_DIFF_PERCENT,
150 VKAT_VERIFY_OPT_MAX_SIZE_PERCENT
151};
152
153/**
154 * Common command line parameters.
155 */
156static const RTGETOPTDEF g_aCmdCommonOptions[] =
157{
158 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
159 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
160 { "--daemonize", AUDIO_TEST_OPT_CMN_DAEMONIZE, RTGETOPT_REQ_NOTHING },
161 { "--daemonized", AUDIO_TEST_OPT_CMN_DAEMONIZED, RTGETOPT_REQ_NOTHING },
162 { "--debug-audio", AUDIO_TEST_OPT_CMN_DEBUG_AUDIO_ENABLE, RTGETOPT_REQ_NOTHING },
163 { "--debug-audio-path", AUDIO_TEST_OPT_CMN_DEBUG_AUDIO_PATH, RTGETOPT_REQ_STRING },
164};
165
166/**
167 * Command line parameters for test mode.
168 */
169static const RTGETOPTDEF g_aCmdTestOptions[] =
170{
171 { "--backend", 'b', RTGETOPT_REQ_STRING },
172 { "--drvaudio", 'd', RTGETOPT_REQ_NOTHING },
173 { "--exclude", 'e', RTGETOPT_REQ_UINT32 },
174 { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING },
175 { "--guest-ats-addr", VKAT_TEST_OPT_GUEST_ATS_ADDR, RTGETOPT_REQ_STRING },
176 { "--guest-ats-port", VKAT_TEST_OPT_GUEST_ATS_PORT, RTGETOPT_REQ_UINT32 },
177 { "--host-ats-address", VKAT_TEST_OPT_HOST_ATS_ADDR, RTGETOPT_REQ_STRING },
178 { "--host-ats-port", VKAT_TEST_OPT_HOST_ATS_PORT, RTGETOPT_REQ_UINT32 },
179 { "--include", 'i', RTGETOPT_REQ_UINT32 },
180 { "--outdir", VKAT_TEST_OPT_OUTDIR, RTGETOPT_REQ_STRING },
181 { "--count", VKAT_TEST_OPT_COUNT, RTGETOPT_REQ_UINT32 },
182 { "--device", VKAT_TEST_OPT_DEV, RTGETOPT_REQ_STRING },
183 { "--pause", VKAT_TEST_OPT_PAUSE, RTGETOPT_REQ_UINT32 },
184 { "--pcm-bit", VKAT_TEST_OPT_PCM_BIT, RTGETOPT_REQ_UINT8 },
185 { "--pcm-chan", VKAT_TEST_OPT_PCM_CHAN, RTGETOPT_REQ_UINT8 },
186 { "--pcm-hz", VKAT_TEST_OPT_PCM_HZ, RTGETOPT_REQ_UINT16 },
187 { "--pcm-signed", VKAT_TEST_OPT_PCM_SIGNED, RTGETOPT_REQ_BOOL },
188 { "--probe-backends", VKAT_TEST_OPT_PROBE_BACKENDS, RTGETOPT_REQ_NOTHING },
189 { "--mode", VKAT_TEST_OPT_MODE, RTGETOPT_REQ_STRING },
190 { "--no-verify", VKAT_TEST_OPT_NO_VERIFY, RTGETOPT_REQ_NOTHING },
191 { "--tag", VKAT_TEST_OPT_TAG, RTGETOPT_REQ_STRING },
192 { "--tempdir", VKAT_TEST_OPT_TEMPDIR, RTGETOPT_REQ_STRING },
193 { "--volume", VKAT_TEST_OPT_VOL, RTGETOPT_REQ_UINT8 },
194 { "--tcp-bind-addr", VKAT_TEST_OPT_TCP_BIND_ADDRESS, RTGETOPT_REQ_STRING },
195 { "--tcp-bind-port", VKAT_TEST_OPT_TCP_BIND_PORT, RTGETOPT_REQ_UINT16 },
196 { "--tcp-connect-addr", VKAT_TEST_OPT_TCP_CONNECT_ADDRESS, RTGETOPT_REQ_STRING },
197 { "--tcp-connect-port", VKAT_TEST_OPT_TCP_CONNECT_PORT, RTGETOPT_REQ_UINT16 }
198};
199
200/**
201 * Command line parameters for verification mode.
202 */
203static const RTGETOPTDEF g_aCmdVerifyOptions[] =
204{
205 { "--max-diff-count", VKAT_VERIFY_OPT_MAX_DIFF_COUNT, RTGETOPT_REQ_UINT32 },
206 { "--max-diff-percent", VKAT_VERIFY_OPT_MAX_DIFF_PERCENT, RTGETOPT_REQ_UINT8 },
207 { "--max-size-percent", VKAT_VERIFY_OPT_MAX_SIZE_PERCENT, RTGETOPT_REQ_UINT8 }
208};
209
210/** Terminate ASAP if set. Set on Ctrl-C. */
211bool volatile g_fTerminate = false;
212/** The release logger. */
213PRTLOGGER g_pRelLogger = NULL;
214/** The test handle. */
215RTTEST g_hTest;
216/** The current verbosity level. */
217unsigned g_uVerbosity = 0;
218/** DrvAudio: Enable debug (or not). */
219bool g_fDrvAudioDebug = false;
220/** DrvAudio: The debug output path. */
221const char *g_pszDrvAudioDebug = NULL;
222
223
224/**
225 * Get default backend.
226 */
227PCPDMDRVREG AudioTestGetDefaultBackend(void)
228{
229 return g_aBackends[0].pDrvReg;
230}
231
232
233/**
234 * Helper for handling --backend options.
235 *
236 * @returns Pointer to the specified backend, NULL if not found (error
237 * displayed).
238 * @param pszBackend The backend option value.
239 */
240PCPDMDRVREG AudioTestFindBackendOpt(const char *pszBackend)
241{
242 for (uintptr_t i = 0; i < RT_ELEMENTS(g_aBackends); i++)
243 if ( strcmp(pszBackend, g_aBackends[i].pszName) == 0
244 || strcmp(pszBackend, g_aBackends[i].pDrvReg->szName) == 0)
245 return g_aBackends[i].pDrvReg;
246 RTMsgError("Unknown backend: '%s'", pszBackend);
247 return NULL;
248}
249
250
251/*********************************************************************************************************************************
252* Test callbacks *
253*********************************************************************************************************************************/
254
255/**
256 * @copydoc FNAUDIOTESTSETUP
257 */
258static DECLCALLBACK(int) audioTestPlayToneSetup(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, PAUDIOTESTPARMS pTstParmsAcq, void **ppvCtx)
259{
260 RT_NOREF(pTstDesc, ppvCtx);
261
262 int rc = VINF_SUCCESS;
263
264 if (strlen(pTstEnv->szDev))
265 {
266 rc = audioTestDriverStackSetDevice(pTstEnv->pDrvStack, PDMAUDIODIR_OUT, pTstEnv->szDev);
267 if (RT_FAILURE(rc))
268 return rc;
269 }
270
271 pTstParmsAcq->enmType = AUDIOTESTTYPE_TESTTONE_PLAY;
272 pTstParmsAcq->Props = pTstEnv->Props;
273 pTstParmsAcq->enmDir = PDMAUDIODIR_OUT;
274#ifdef DEBUG
275 pTstParmsAcq->cIterations = 1;
276#else
277 pTstParmsAcq->cIterations = RTRandU32Ex(1, 10);
278#endif
279 pTstParmsAcq->idxCurrent = 0;
280
281 PAUDIOTESTTONEPARMS pToneParms = &pTstParmsAcq->TestTone;
282
283 pToneParms->Props = pTstParmsAcq->Props;
284 pToneParms->dbFreqHz = AudioTestToneGetRandomFreq();
285 pToneParms->msPrequel = 0; /** @todo Implement analyzing this first! */
286#ifdef DEBUG
287 pToneParms->msDuration = RTRandU32Ex(50, 2500);
288#else
289 pToneParms->msDuration = RTRandU32Ex(0, RT_MS_10SEC); /** @todo Probably a bit too long, but let's see. */
290#endif
291 pToneParms->msSequel = 0; /** @todo Implement analyzing this first! */
292 pToneParms->uVolumePercent = 100; /** @todo Implement analyzing this first! */
293
294 return rc;
295}
296
297/**
298 * @copydoc FNAUDIOTESTEXEC
299 */
300static DECLCALLBACK(int) audioTestPlayToneExec(PAUDIOTESTENV pTstEnv, void *pvCtx, PAUDIOTESTPARMS pTstParms)
301{
302 RT_NOREF(pvCtx);
303
304 int rc = VINF_SUCCESS;
305
306 for (uint32_t i = 0; i < pTstParms->cIterations; i++)
307 {
308 PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone;
309
310 pToneParms->Hdr.idxSeq = i;
311 RTTIMESPEC NowTimeSpec;
312 RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec));
313
314 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32/%RU16: Playing test tone (%RU16Hz, %RU32ms)\n",
315 pTstParms->idxCurrent, i, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
316
317 /*
318 * 1. Arm the (host) ValKit ATS with the recording parameters.
319 */
320 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to record new tone ...\n");
321 rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClValKit, pToneParms);
322 if (RT_SUCCESS(rc))
323 {
324 /* Give the Validaiton Kit audio driver on the host a bit of time to register / arming the new test. */
325 RTThreadSleep(5000); /* Fudge factor. */
326
327 /*
328 * 2. Tell VKAT on guest to start playback.
329 */
330 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to play tone ...\n");
331 rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClGuest, pToneParms);
332 if (RT_FAILURE(rc))
333 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientTonePlay() failed with %Rrc\n",
334 pTstParms->idxCurrent, i, rc);
335 }
336 else
337 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientToneRecord() failed with %Rrc\n",
338 pTstParms->idxCurrent, i, rc);
339
340 if (RT_SUCCESS(rc))
341 {
342 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Playing tone done\n");
343
344 /* Give the audio stack a random amount of time for draining data before the next iteration. */
345 if (pTstParms->cIterations > 1)
346 RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */
347 }
348
349 if (RT_FAILURE(rc))
350 {
351 RTTestFailed(g_hTest, "Test #%RU32/%RU16: Playing test tone failed with %Rrc\n", pTstParms->idxCurrent, i, rc);
352 break; /* Not worth retrying, bail out. */
353 }
354 }
355
356 return rc;
357}
358
359/**
360 * @copydoc FNAUDIOTESTDESTROY
361 */
362static DECLCALLBACK(int) audioTestPlayToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx)
363{
364 RT_NOREF(pTstEnv, pvCtx);
365
366 return VINF_SUCCESS;
367}
368
369/**
370 * @copydoc FNAUDIOTESTSETUP
371 */
372static DECLCALLBACK(int) audioTestRecordToneSetup(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, PAUDIOTESTPARMS pTstParmsAcq, void **ppvCtx)
373{
374 RT_NOREF(pTstDesc, ppvCtx);
375
376 int rc = VINF_SUCCESS;
377
378 if (strlen(pTstEnv->szDev))
379 {
380 rc = audioTestDriverStackSetDevice(pTstEnv->pDrvStack, PDMAUDIODIR_IN, pTstEnv->szDev);
381 if (RT_FAILURE(rc))
382 return rc;
383 }
384
385 pTstParmsAcq->enmType = AUDIOTESTTYPE_TESTTONE_RECORD;
386 pTstParmsAcq->Props = pTstEnv->Props;
387 pTstParmsAcq->enmDir = PDMAUDIODIR_IN;
388#ifdef DEBUG
389 pTstParmsAcq->cIterations = 1;
390#else
391 pTstParmsAcq->cIterations = RTRandU32Ex(1, 10);
392#endif
393 pTstParmsAcq->idxCurrent = 0;
394
395 PAUDIOTESTTONEPARMS pToneParms = &pTstParmsAcq->TestTone;
396
397 pToneParms->Props = pTstParmsAcq->Props;
398 pToneParms->dbFreqHz = AudioTestToneGetRandomFreq();
399 pToneParms->msPrequel = 0; /** @todo Implement analyzing this first! */
400 pToneParms->Props = pTstParmsAcq->Props;
401#ifdef DEBUG
402 pToneParms->msDuration = RTRandU32Ex(50 /* ms */, 2500);
403#else
404 pToneParms->msDuration = RTRandU32Ex(50 /* ms */, RT_MS_30SEC); /** @todo Record even longer? */
405#endif
406 pToneParms->msSequel = 0; /** @todo Implement analyzing this first! */
407 pToneParms->uVolumePercent = 100; /** @todo Implement analyzing this first! */
408
409 return rc;
410}
411
412/**
413 * @copydoc FNAUDIOTESTEXEC
414 */
415static DECLCALLBACK(int) audioTestRecordToneExec(PAUDIOTESTENV pTstEnv, void *pvCtx, PAUDIOTESTPARMS pTstParms)
416{
417 RT_NOREF(pvCtx);
418
419 int rc = VINF_SUCCESS;
420
421 for (uint32_t i = 0; i < pTstParms->cIterations; i++)
422 {
423 PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone;
424
425 pToneParms->Hdr.idxSeq = i;
426 RTTIMESPEC NowTimeSpec;
427 RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec));
428
429 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32/%RU16: Recording test tone (%RU16Hz, %RU32ms)\n",
430 pTstParms->idxCurrent, i, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
431
432 /*
433 * 1. Arm the (host) ValKit ATS with the playback parameters.
434 */
435 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to inject recording data ...\n");
436 rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClValKit, &pTstParms->TestTone);
437 if (RT_SUCCESS(rc))
438 {
439 /*
440 * 2. Tell the guest ATS to start recording.
441 */
442 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to record audio ...\n");
443 rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClGuest, &pTstParms->TestTone);
444 if (RT_FAILURE(rc))
445 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientToneRecord() failed with %Rrc\n",
446 pTstParms->idxCurrent, i, rc);
447 }
448 else
449 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientTonePlay() failed with %Rrc\n",
450 pTstParms->idxCurrent, i, rc);
451
452 if (RT_SUCCESS(rc))
453 {
454 /* Wait a bit to let the left over audio bits being processed. */
455 if (pTstParms->cIterations > 1)
456 RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */
457 }
458
459 if (RT_FAILURE(rc))
460 {
461 RTTestFailed(g_hTest, "Test #%RU32/%RU16: Recording test tone failed with %Rrc\n", pTstParms->idxCurrent, i, rc);
462 break; /* Not worth retrying, bail out. */
463 }
464 }
465
466 return rc;
467}
468
469/**
470 * @copydoc FNAUDIOTESTDESTROY
471 */
472static DECLCALLBACK(int) audioTestRecordToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx)
473{
474 RT_NOREF(pTstEnv, pvCtx);
475
476 return VINF_SUCCESS;
477}
478
479
480/*********************************************************************************************************************************
481* Test execution *
482*********************************************************************************************************************************/
483
484/** Test definition table. */
485AUDIOTESTDESC g_aTests[] =
486{
487 /* pszTest fExcluded pfnSetup */
488 { "PlayTone", false, audioTestPlayToneSetup, audioTestPlayToneExec, audioTestPlayToneDestroy },
489 { "RecordTone", false, audioTestRecordToneSetup, audioTestRecordToneExec, audioTestRecordToneDestroy }
490};
491/** Number of tests defined. */
492unsigned g_cTests = RT_ELEMENTS(g_aTests);
493
494/**
495 * Runs one specific audio test.
496 *
497 * @returns VBox status code.
498 * @param pTstEnv Test environment to use for running the test.
499 * @param pTstDesc Test to run.
500 * @param uSeq Test sequence # in case there are more tests.
501 */
502static int audioTestOne(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, unsigned uSeq)
503{
504 RT_NOREF(uSeq);
505
506 int rc;
507
508 AUDIOTESTPARMS TstParms;
509 audioTestParmsInit(&TstParms);
510
511 RTTestSub(g_hTest, pTstDesc->pszName);
512
513 if (pTstDesc->fExcluded)
514 {
515 RTTestSkipped(g_hTest, "Test #%u is excluded from list, skipping", uSeq);
516 return VINF_SUCCESS;
517 }
518
519 void *pvCtx = NULL; /* Test-specific opaque context. Optional and can be NULL. */
520
521 if (pTstDesc->pfnSetup)
522 {
523 rc = pTstDesc->pfnSetup(pTstEnv, pTstDesc, &TstParms, &pvCtx);
524 if (RT_FAILURE(rc))
525 {
526 RTTestFailed(g_hTest, "Test #%u setup failed with %Rrc\n", uSeq, rc);
527 return rc;
528 }
529 }
530
531 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%u (%RU32 iterations total)\n", uSeq, TstParms.cIterations);
532
533 AssertPtr(pTstDesc->pfnExec);
534 rc = pTstDesc->pfnExec(pTstEnv, pvCtx, &TstParms);
535 if (RT_FAILURE(rc))
536 RTTestFailed(g_hTest, "Test #%u failed with %Rrc\n", uSeq, rc);
537
538 RTTestSubDone(g_hTest);
539
540 if (pTstDesc->pfnDestroy)
541 {
542 int rc2 = pTstDesc->pfnDestroy(pTstEnv, pvCtx);
543 if (RT_SUCCESS(rc))
544 rc = rc2;
545
546 if (RT_FAILURE(rc2))
547 RTTestFailed(g_hTest, "Test #%u destruction failed with %Rrc\n", uSeq, rc2);
548 }
549
550 audioTestParmsDestroy(&TstParms);
551
552 return rc;
553}
554
555/**
556 * Runs all specified tests in a row.
557 *
558 * @returns VBox status code.
559 * @param pTstEnv Test environment to use for running all tests.
560 */
561int audioTestWorker(PAUDIOTESTENV pTstEnv)
562{
563 int rc = VINF_SUCCESS;
564
565 if (pTstEnv->enmMode == AUDIOTESTMODE_GUEST)
566 {
567 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS running\n");
568
569 while (!g_fTerminate)
570 RTThreadSleep(100);
571
572 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down guest ATS ...\n");
573
574 int rc2 = AudioTestSvcStop(pTstEnv->pSrv);
575 if (RT_SUCCESS(rc))
576 rc = rc2;
577
578 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS shutdown complete\n");
579 }
580 else if (pTstEnv->enmMode == AUDIOTESTMODE_HOST)
581 {
582 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using tag '%s'\n", pTstEnv->szTag);
583
584 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to begin a new test set ...\n");
585 rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag);
586 if (RT_SUCCESS(rc))
587 {
588 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to begin a new test set ...\n");
589 rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag);
590 if (RT_FAILURE(rc))
591 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
592 "Beginning test set on guest failed with %Rrc\n", rc);
593 }
594 else
595 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
596 "Beginning test set on host (Validation Kit audio driver) failed with %Rrc\n", rc);
597
598 if (RT_SUCCESS(rc))
599 {
600 unsigned uSeq = 0;
601 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
602 {
603 int rc2 = audioTestOne(pTstEnv, &g_aTests[i], uSeq);
604 if (RT_SUCCESS(rc))
605 rc = rc2;
606
607 if (!g_aTests[i].fExcluded)
608 uSeq++;
609
610 if (g_fTerminate)
611 break;
612 }
613
614 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest ...\n");
615 int rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag);
616 if (RT_FAILURE(rc2))
617 {
618 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest failed with %Rrc\n", rc2);
619 if (RT_SUCCESS(rc))
620 rc = rc2;
621 }
622
623 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on host (Validation Kit audio driver) ...\n");
624 rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag);
625 if (RT_FAILURE(rc2))
626 {
627 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
628 "Ending test set on host (Validation Kit audio driver) failed with %Rrc\n", rc2);
629 if (RT_SUCCESS(rc))
630 rc = rc2;
631 }
632
633 if ( !g_fTerminate
634 && RT_SUCCESS(rc))
635 {
636 /*
637 * Download guest + Validation Kit audio driver test sets to our output directory.
638 */
639 char szFileName[RTPATH_MAX];
640 if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-guest.tar.gz", pTstEnv->szTag))
641 {
642 rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetGuest, sizeof(pTstEnv->u.Host.szPathTestSetGuest),
643 pTstEnv->szPathOut, szFileName);
644 if (RT_SUCCESS(rc))
645 {
646 if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-host.tar.gz", pTstEnv->szTag))
647 {
648 rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetValKit, sizeof(pTstEnv->u.Host.szPathTestSetValKit),
649 pTstEnv->szPathOut, szFileName);
650 }
651 else
652 rc = VERR_BUFFER_OVERFLOW;
653 }
654 else
655 rc = VERR_BUFFER_OVERFLOW;
656
657 if (RT_SUCCESS(rc))
658 {
659 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading guest test set to '%s'\n",
660 pTstEnv->u.Host.szPathTestSetGuest);
661 rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClGuest,
662 pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetGuest);
663 }
664
665 if (RT_SUCCESS(rc))
666 {
667 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading host test set to '%s'\n",
668 pTstEnv->u.Host.szPathTestSetValKit);
669 rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClValKit,
670 pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetValKit);
671 }
672 }
673 else
674 rc = VERR_BUFFER_OVERFLOW;
675
676 if ( RT_SUCCESS(rc)
677 && !pTstEnv->fSkipVerify)
678 {
679 rc = audioVerifyOne(pTstEnv->u.Host.szPathTestSetGuest, pTstEnv->u.Host.szPathTestSetValKit, NULL /* pOpts */);
680 }
681 else
682 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification skipped\n");
683
684 if (!pTstEnv->fSkipVerify)
685 {
686 RTFileDelete(pTstEnv->u.Host.szPathTestSetGuest);
687 RTFileDelete(pTstEnv->u.Host.szPathTestSetValKit);
688 }
689 else
690 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Leaving test set files behind\n");
691 }
692 }
693 }
694 else
695 rc = VERR_NOT_IMPLEMENTED;
696
697 /* Clean up. */
698 RTDirRemove(pTstEnv->szPathTemp);
699 RTDirRemove(pTstEnv->szPathOut);
700
701 if (RT_FAILURE(rc))
702 RTTestFailed(g_hTest, "Test worker failed with %Rrc", rc);
703
704 return rc;
705}
706
707/** Option help for the 'test' command. */
708static DECLCALLBACK(const char *) audioTestCmdTestHelp(PCRTGETOPTDEF pOpt)
709{
710 switch (pOpt->iShort)
711 {
712 case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)";
713 case 'b': return "The audio backend to use";
714 case 'd': return "Go via DrvAudio instead of directly interfacing with the backend";
715 case 'e': return "Exclude the given test id from the list";
716 case 'i': return "Include the given test id in the list";
717 case VKAT_TEST_OPT_COUNT: return "Number of test iterations to perform for selected tests\n"
718 " Default: random number";
719 case VKAT_TEST_OPT_DEV: return "Name of the input/output device to use\n"
720 " Default: default device";
721 case VKAT_TEST_OPT_GUEST_ATS_ADDR: return "Address of guest ATS to connect to\n"
722 " Default: " ATS_TCP_DEF_CONNECT_GUEST_STR;
723 case VKAT_TEST_OPT_GUEST_ATS_PORT: return "Port of guest ATS to connect to (needs NAT port forwarding)\n"
724 " Default: 6042"; /* ATS_TCP_DEF_CONNECT_PORT_GUEST */
725 case VKAT_TEST_OPT_HOST_ATS_ADDR: return "Address of host ATS to connect to\n"
726 " Default: " ATS_TCP_DEF_CONNECT_HOST_ADDR_STR;
727 case VKAT_TEST_OPT_HOST_ATS_PORT: return "Port of host ATS to connect to\n"
728 " Default: 6052"; /* ATS_TCP_DEF_BIND_PORT_VALKIT */
729 case VKAT_TEST_OPT_MODE: return "Specifies the test mode to use when running the tests";
730 case VKAT_TEST_OPT_NO_VERIFY: return "Skips the verification step";
731 case VKAT_TEST_OPT_OUTDIR: return "Specifies the output directory to use";
732 case VKAT_TEST_OPT_PAUSE: return "Not yet implemented";
733 case VKAT_TEST_OPT_PCM_HZ: return "Specifies the PCM Hetz (Hz) rate to use\n"
734 " Default: 44100";
735 case VKAT_TEST_OPT_PCM_BIT: return "Specifies the PCM sample bits (i.e. 16) to use\n"
736 " Default: 16";
737 case VKAT_TEST_OPT_PCM_CHAN: return "Specifies the number of PCM channels to use\n"
738 " Default: 2";
739 case VKAT_TEST_OPT_PCM_SIGNED: return "Specifies whether to use signed (true) or unsigned (false) samples\n"
740 " Default: true";
741 case VKAT_TEST_OPT_PROBE_BACKENDS: return "Specifies whether to probe all (available) backends until a working one is found\n"
742 " Default: false";
743 case VKAT_TEST_OPT_TAG: return "Specifies the test set tag to use";
744 case VKAT_TEST_OPT_TEMPDIR: return "Specifies the temporary directory to use";
745 case VKAT_TEST_OPT_VOL: return "Specifies the audio volume (in percent, 0-100) to use";
746 case VKAT_TEST_OPT_TCP_BIND_ADDRESS: return "Specifies the TCP address listening to (server mode)";
747 case VKAT_TEST_OPT_TCP_BIND_PORT: return "Specifies the TCP port listening to (server mode)";
748 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS: return "Specifies the TCP address to connect to (client mode)";
749 case VKAT_TEST_OPT_TCP_CONNECT_PORT: return "Specifies the TCP port to connect to (client mode)";
750 default:
751 break;
752 }
753 return NULL;
754}
755
756/**
757 * Main (entry) function for the testing functionality of VKAT.
758 *
759 * @returns Program exit code.
760 * @param pGetState RTGetOpt state.
761 */
762static DECLCALLBACK(RTEXITCODE) audioTestMain(PRTGETOPTSTATE pGetState)
763{
764 AUDIOTESTENV TstEnv;
765 RT_ZERO(TstEnv);
766
767 int rc;
768
769 PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend();
770 bool fWithDrvAudio = false;
771 uint8_t cPcmSampleBit = 0;
772 uint8_t cPcmChannels = 0;
773 uint32_t uPcmHz = 0;
774 bool fPcmSigned = true;
775 bool fProbeBackends = false;
776
777 const char *pszGuestTcpAddr = NULL;
778 uint16_t uGuestTcpPort = ATS_TCP_DEF_BIND_PORT_GUEST;
779 const char *pszValKitTcpAddr = NULL;
780 uint16_t uValKitTcpPort = ATS_TCP_DEF_BIND_PORT_VALKIT;
781
782 int ch;
783 RTGETOPTUNION ValueUnion;
784 while ((ch = RTGetOpt(pGetState, &ValueUnion)))
785 {
786 switch (ch)
787 {
788 case 'a':
789 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
790 g_aTests[i].fExcluded = true;
791 break;
792
793 case 'b':
794 pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
795 if (pDrvReg == NULL)
796 return RTEXITCODE_SYNTAX;
797 break;
798
799 case 'd':
800 fWithDrvAudio = true;
801 break;
802
803 case 'e':
804 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
805 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
806 g_aTests[ValueUnion.u32].fExcluded = true;
807 break;
808
809 case VKAT_TEST_OPT_GUEST_ATS_ADDR:
810 pszGuestTcpAddr = ValueUnion.psz;
811 break;
812
813 case VKAT_TEST_OPT_GUEST_ATS_PORT:
814 uGuestTcpPort = ValueUnion.u32;
815 break;
816
817 case VKAT_TEST_OPT_HOST_ATS_ADDR:
818 pszValKitTcpAddr = ValueUnion.psz;
819 break;
820
821 case VKAT_TEST_OPT_HOST_ATS_PORT:
822 uValKitTcpPort = ValueUnion.u32;
823 break;
824
825 case VKAT_TEST_OPT_MODE:
826 if (TstEnv.enmMode != AUDIOTESTMODE_UNKNOWN)
827 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Test mode (guest / host) already specified");
828 TstEnv.enmMode = RTStrICmp(ValueUnion.psz, "guest") == 0 ? AUDIOTESTMODE_GUEST : AUDIOTESTMODE_HOST;
829 break;
830
831 case VKAT_TEST_OPT_NO_VERIFY:
832 TstEnv.fSkipVerify = true;
833 break;
834
835 case 'i':
836 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
837 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
838 g_aTests[ValueUnion.u32].fExcluded = false;
839 break;
840
841 case VKAT_TEST_OPT_COUNT:
842 return RTMsgErrorExitFailure("Not yet implemented!");
843
844 case VKAT_TEST_OPT_DEV:
845 rc = RTStrCopy(TstEnv.szDev, sizeof(TstEnv.szDev), ValueUnion.psz);
846 if (RT_FAILURE(rc))
847 return RTMsgErrorExitFailure("Failed to copy out device: %Rrc", rc);
848 break;
849
850 case VKAT_TEST_OPT_PAUSE:
851 return RTMsgErrorExitFailure("Not yet implemented!");
852
853 case VKAT_TEST_OPT_OUTDIR:
854 rc = RTStrCopy(TstEnv.szPathOut, sizeof(TstEnv.szPathOut), ValueUnion.psz);
855 if (RT_FAILURE(rc))
856 return RTMsgErrorExitFailure("Failed to copy out directory: %Rrc", rc);
857 break;
858
859 case VKAT_TEST_OPT_PCM_BIT:
860 cPcmSampleBit = ValueUnion.u8;
861 break;
862
863 case VKAT_TEST_OPT_PCM_CHAN:
864 cPcmChannels = ValueUnion.u8;
865 break;
866
867 case VKAT_TEST_OPT_PCM_HZ:
868 uPcmHz = ValueUnion.u32;
869 break;
870
871 case VKAT_TEST_OPT_PCM_SIGNED:
872 fPcmSigned = ValueUnion.f;
873 break;
874
875 case VKAT_TEST_OPT_PROBE_BACKENDS:
876 fProbeBackends = ValueUnion.f;
877 break;
878
879 case VKAT_TEST_OPT_TAG:
880 rc = RTStrCopy(TstEnv.szTag, sizeof(TstEnv.szTag), ValueUnion.psz);
881 if (RT_FAILURE(rc))
882 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Tag invalid, rc=%Rrc", rc);
883 break;
884
885 case VKAT_TEST_OPT_TEMPDIR:
886 rc = RTStrCopy(TstEnv.szPathTemp, sizeof(TstEnv.szPathTemp), ValueUnion.psz);
887 if (RT_FAILURE(rc))
888 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Temp dir invalid, rc=%Rrc", rc);
889 break;
890
891 case VKAT_TEST_OPT_VOL:
892 TstEnv.uVolumePercent = ValueUnion.u8;
893 break;
894
895 case VKAT_TEST_OPT_TCP_BIND_ADDRESS:
896 rc = RTStrCopy(TstEnv.TcpOpts.szBindAddr, sizeof(TstEnv.TcpOpts.szBindAddr), ValueUnion.psz);
897 if (RT_FAILURE(rc))
898 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Bind address invalid, rc=%Rrc", rc);
899 break;
900
901 case VKAT_TEST_OPT_TCP_BIND_PORT:
902 TstEnv.TcpOpts.uBindPort = ValueUnion.u16;
903 break;
904
905 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS:
906 rc = RTStrCopy(TstEnv.TcpOpts.szConnectAddr, sizeof(TstEnv.TcpOpts.szConnectAddr), ValueUnion.psz);
907 if (RT_FAILURE(rc))
908 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Connect address invalid, rc=%Rrc", rc);
909 break;
910
911 case VKAT_TEST_OPT_TCP_CONNECT_PORT:
912 TstEnv.TcpOpts.uConnectPort = ValueUnion.u16;
913 break;
914
915 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
916
917 default:
918 return RTGetOptPrintError(ch, &ValueUnion);
919 }
920 }
921
922 /*
923 * Start testing.
924 */
925 RTTestBanner(g_hTest);
926
927 /* Initialize the custom test parameters with sensible defaults if nothing else is given. */
928 PDMAudioPropsInit(&TstEnv.Props,
929 cPcmSampleBit ? cPcmSampleBit / 8 : 2 /* 16-bit */, fPcmSigned, cPcmChannels ? cPcmChannels : 2,
930 uPcmHz ? uPcmHz : 44100);
931
932 if (TstEnv.enmMode == AUDIOTESTMODE_UNKNOWN)
933 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No test mode (--mode) specified!\n");
934
935 /* Validate TCP options. */
936 if ( TstEnv.TcpOpts.szBindAddr[0]
937 && TstEnv.TcpOpts.szConnectAddr[0])
938 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Only one TCP connection mode (connect as client *or* bind as server) can be specified) at a time!\n");
939
940 AUDIOTESTDRVSTACK DrvStack;
941 if (fProbeBackends)
942 rc = audioTestDriverStackProbe(&DrvStack, pDrvReg,
943 true /* fEnabledIn */, true /* fEnabledOut */, fWithDrvAudio); /** @todo Make in/out configurable, too. */
944 else
945 rc = audioTestDriverStackInitEx(&DrvStack, pDrvReg,
946 true /* fEnabledIn */, true /* fEnabledOut */, fWithDrvAudio); /** @todo Make in/out configurable, too. */
947 if (RT_FAILURE(rc))
948 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to init driver stack: %Rrc\n", rc);
949
950 PPDMAUDIOHOSTDEV pDev;
951 rc = audioTestDevicesEnumerateAndCheck(&DrvStack, TstEnv.szDev, &pDev);
952 if (RT_FAILURE(rc))
953 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Enumerating device(s) failed: %Rrc\n", rc);
954
955 /* For now all tests have the same test environment and driver stack. */
956 rc = audioTestEnvInit(&TstEnv, &DrvStack);
957 if (RT_SUCCESS(rc))
958 rc = audioTestWorker(&TstEnv);
959
960 audioTestEnvDestroy(&TstEnv);
961 audioTestDriverStackDelete(&DrvStack);
962
963 if (RT_FAILURE(rc)) /* Let us know that something went wrong in case we forgot to mention it. */
964 RTTestFailed(g_hTest, "Testing failed with %Rrc\n", rc);
965
966 /*
967 * Print summary and exit.
968 */
969 return RTTestSummaryAndDestroy(g_hTest);
970}
971
972
973const VKATCMD g_CmdTest =
974{
975 "test",
976 audioTestMain,
977 "Runs audio tests and creates an audio test set.",
978 g_aCmdTestOptions,
979 RT_ELEMENTS(g_aCmdTestOptions),
980 audioTestCmdTestHelp,
981 true /* fNeedsTransport */
982};
983
984
985/*********************************************************************************************************************************
986* Command: verify *
987*********************************************************************************************************************************/
988
989static int audioVerifyOpenTestSet(const char *pszPathSet, PAUDIOTESTSET pSet)
990{
991 int rc;
992
993 char szPathExtracted[RTPATH_MAX];
994
995 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Opening test set '%s'\n", pszPathSet);
996
997 const bool fPacked = AudioTestSetIsPacked(pszPathSet);
998
999 if (fPacked)
1000 {
1001 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set is an archive and needs to be unpacked\n");
1002
1003 if (!RTFileExists(pszPathSet))
1004 {
1005 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set '%s' does not exist\n", pszPathSet);
1006 rc = VERR_FILE_NOT_FOUND;
1007 }
1008 else
1009 rc = VINF_SUCCESS;
1010
1011 if (RT_SUCCESS(rc))
1012 {
1013 char szPathTemp[RTPATH_MAX];
1014 rc = RTPathTemp(szPathTemp, sizeof(szPathTemp));
1015 if (RT_SUCCESS(rc))
1016 {
1017 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using temporary directory '%s'\n", szPathTemp);
1018
1019 rc = RTPathJoin(szPathExtracted, sizeof(szPathExtracted), szPathTemp, "vkat-testset-XXXX");
1020 if (RT_SUCCESS(rc))
1021 {
1022 rc = RTDirCreateTemp(szPathExtracted, 0755);
1023 if (RT_SUCCESS(rc))
1024 {
1025 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Unpacking archive to '%s'\n", szPathExtracted);
1026 rc = AudioTestSetUnpack(pszPathSet, szPathExtracted);
1027 if (RT_SUCCESS(rc))
1028 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Archive successfully unpacked\n");
1029 }
1030 }
1031 }
1032 }
1033 }
1034 else
1035 rc = VINF_SUCCESS;
1036
1037 if (RT_SUCCESS(rc))
1038 rc = AudioTestSetOpen(pSet, fPacked ? szPathExtracted : pszPathSet);
1039
1040 if (RT_FAILURE(rc))
1041 RTTestFailed(g_hTest, "Unable to open / unpack test set archive: %Rrc", rc);
1042
1043 return rc;
1044}
1045
1046/**
1047 * Verifies one test set pair.
1048 *
1049 * @returns VBox status code.
1050 * @param pszPathSetA Absolute path to test set A.
1051 * @param pszPathSetB Absolute path to test set B.
1052 * @param pOpts Verification options to use. Optional.
1053 * When NULL, the (very strict) defaults will be used.
1054 */
1055static int audioVerifyOne(const char *pszPathSetA, const char *pszPathSetB, PAUDIOTESTVERIFYOPTS pOpts)
1056{
1057 RTTestSubF(g_hTest, "Verifying");
1058 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verifying test set '%s' with test set '%s'\n", pszPathSetA, pszPathSetB);
1059
1060 AUDIOTESTSET SetA, SetB;
1061 int rc = audioVerifyOpenTestSet(pszPathSetA, &SetA);
1062 if (RT_SUCCESS(rc))
1063 {
1064 rc = audioVerifyOpenTestSet(pszPathSetB, &SetB);
1065 if (RT_SUCCESS(rc))
1066 {
1067 AUDIOTESTERRORDESC errDesc;
1068 if (pOpts)
1069 rc = AudioTestSetVerifyEx(&SetA, &SetB, pOpts, &errDesc);
1070 else
1071 rc = AudioTestSetVerify(&SetA, &SetB, &errDesc);
1072 if (RT_SUCCESS(rc))
1073 {
1074 uint32_t const cErr = AudioTestErrorDescCount(&errDesc);
1075 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%RU32 errors occurred while verifying\n", cErr);
1076
1077 /** @todo Use some AudioTestErrorXXX API for enumeration here later. */
1078 PAUDIOTESTERRORENTRY pErrEntry;
1079 RTListForEach(&errDesc.List, pErrEntry, AUDIOTESTERRORENTRY, Node)
1080 {
1081 if (RT_FAILURE(pErrEntry->rc))
1082 RTTestFailed(g_hTest, "%s\n", pErrEntry->szDesc);
1083 else
1084 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%s\n", pErrEntry->szDesc);
1085 }
1086
1087 if (cErr == 0)
1088 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification successful\n");
1089
1090 AudioTestErrorDescDestroy(&errDesc);
1091 }
1092 else
1093 RTTestFailed(g_hTest, "Verification failed with %Rrc", rc);
1094
1095#ifdef DEBUG
1096 if (g_fDrvAudioDebug)
1097 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
1098 "\n"
1099 "Use the following command line to re-run verification in the debugger:\n"
1100 "gdb --args ./VBoxAudioTest -vvvv --debug-audio verify \"%s\" \"%s\"\n",
1101 SetA.szPathAbs, SetB.szPathAbs);
1102#endif
1103 if (!g_fDrvAudioDebug) /* Don't wipe stuff when debugging. Can be useful for introspecting data. */
1104 AudioTestSetWipe(&SetB);
1105 AudioTestSetClose(&SetB);
1106 }
1107
1108 if (!g_fDrvAudioDebug) /* Ditto. */
1109 AudioTestSetWipe(&SetA);
1110 AudioTestSetClose(&SetA);
1111 }
1112
1113 RTTestSubDone(g_hTest);
1114
1115 return rc;
1116}
1117
1118/**
1119 * Main (entry) function for the verification functionality of VKAT.
1120 *
1121 * @returns Program exit code.
1122 * @param pGetState RTGetOpt state.
1123 */
1124static DECLCALLBACK(RTEXITCODE) audioVerifyMain(PRTGETOPTSTATE pGetState)
1125{
1126 /*
1127 * Parse options and process arguments.
1128 */
1129 const char *apszSets[2] = { NULL, NULL };
1130 unsigned iTestSet = 0;
1131
1132 AUDIOTESTVERIFYOPTS Opts;
1133 AudioTestSetVerifyOptsInit(&Opts);
1134
1135 int ch;
1136 RTGETOPTUNION ValueUnion;
1137 while ((ch = RTGetOpt(pGetState, &ValueUnion)))
1138 {
1139 switch (ch)
1140 {
1141 case VKAT_VERIFY_OPT_MAX_DIFF_COUNT:
1142 Opts.cMaxDiff = ValueUnion.u32;
1143 break;
1144
1145 case VKAT_VERIFY_OPT_MAX_DIFF_PERCENT:
1146 Opts.uMaxDiffPercent = ValueUnion.u8;
1147 break;
1148
1149 case VKAT_VERIFY_OPT_MAX_SIZE_PERCENT:
1150 Opts.uMaxSizePercent = ValueUnion.u8;
1151 break;
1152
1153 case VINF_GETOPT_NOT_OPTION:
1154 if (iTestSet == 0)
1155 RTTestBanner(g_hTest);
1156 if (iTestSet >= RT_ELEMENTS(apszSets))
1157 return RTMsgErrorExitFailure("Only two test sets can be verified at one time");
1158 apszSets[iTestSet++] = ValueUnion.psz;
1159 break;
1160
1161 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
1162
1163 default:
1164 return RTGetOptPrintError(ch, &ValueUnion);
1165 }
1166 }
1167
1168 if (!iTestSet)
1169 return RTMsgErrorExitFailure("At least one test set must be specified");
1170
1171 int rc = VINF_SUCCESS;
1172
1173 /*
1174 * If only test set A is given, default to the current directory
1175 * for test set B.
1176 */
1177 char szDirCur[RTPATH_MAX];
1178 if (iTestSet == 1)
1179 {
1180 rc = RTPathGetCurrent(szDirCur, sizeof(szDirCur));
1181 if (RT_SUCCESS(rc))
1182 apszSets[1] = szDirCur;
1183 else
1184 RTTestFailed(g_hTest, "Failed to retrieve current directory: %Rrc", rc);
1185 }
1186
1187 if (RT_SUCCESS(rc))
1188 audioVerifyOne(apszSets[0], apszSets[1], &Opts);
1189
1190 /*
1191 * Print summary and exit.
1192 */
1193 return RTTestSummaryAndDestroy(g_hTest);
1194}
1195
1196
1197const VKATCMD g_CmdVerify =
1198{
1199 "verify",
1200 audioVerifyMain,
1201 "Verifies a formerly created audio test set.",
1202 g_aCmdVerifyOptions,
1203 RT_ELEMENTS(g_aCmdVerifyOptions),
1204 NULL,
1205 false /* fNeedsTransport */
1206};
1207
1208
1209/*********************************************************************************************************************************
1210* Main *
1211*********************************************************************************************************************************/
1212
1213/**
1214 * Ctrl-C signal handler.
1215 *
1216 * This just sets g_fTerminate and hope it will be noticed soon. It restores
1217 * the SIGINT action to default, so that a second Ctrl-C will have the normal
1218 * effect (just in case the code doesn't respond to g_fTerminate).
1219 */
1220static void audioTestSignalHandler(int iSig) RT_NOEXCEPT
1221{
1222 Assert(iSig == SIGINT); RT_NOREF(iSig);
1223 RTPrintf("Ctrl-C!\n");
1224 ASMAtomicWriteBool(&g_fTerminate, true);
1225 signal(SIGINT, SIG_DFL);
1226}
1227
1228/**
1229 * Commands.
1230 */
1231const VKATCMD *g_apCommands[] =
1232{
1233 &g_CmdTest,
1234 &g_CmdVerify,
1235 &g_CmdEnum,
1236 &g_CmdPlay,
1237 &g_CmdRec,
1238 &g_CmdSelfTest
1239};
1240
1241/**
1242 * Shows tool usage text.
1243 */
1244RTEXITCODE audioTestUsage(PRTSTREAM pStrm)
1245{
1246 RTStrmPrintf(pStrm, "usage: %s [global options] <command> [command-options]\n",
1247 RTPathFilename(RTProcExecutablePath()));
1248 RTStrmPrintf(pStrm,
1249 "\n"
1250 "Global Options:\n"
1251 " --debug-audio\n"
1252 " Enables (DrvAudio) debugging.\n"
1253 " --debug-audio-path=<path>\n"
1254 " Tells DrvAudio where to put its debug output (wav-files).\n"
1255 " -q, --quiet\n"
1256 " Sets verbosity to zero.\n"
1257 " -v, --verbose\n"
1258 " Increase verbosity.\n"
1259 " -V, --version\n"
1260 " Displays version.\n"
1261 " -h, -?, --help\n"
1262 " Displays help.\n"
1263 );
1264
1265 for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++)
1266 {
1267 PCVKATCMD const pCmd = g_apCommands[iCmd];
1268 RTStrmPrintf(pStrm,
1269 "\n"
1270 "Command '%s':\n"
1271 " %s\n"
1272 "Options for '%s':\n",
1273 pCmd->pszCommand, pCmd->pszDesc, pCmd->pszCommand);
1274 PCRTGETOPTDEF const paOptions = pCmd->paOptions;
1275 for (unsigned i = 0; i < pCmd->cOptions; i++)
1276 {
1277 if (RT_C_IS_PRINT(paOptions[i].iShort))
1278 RTStrmPrintf(pStrm, " -%c, %s\n", paOptions[i].iShort, paOptions[i].pszLong);
1279 else
1280 RTStrmPrintf(pStrm, " %s\n", paOptions[i].pszLong);
1281
1282 const char *pszHelp = NULL;
1283 if (pCmd->pfnOptionHelp)
1284 pszHelp = pCmd->pfnOptionHelp(&paOptions[i]);
1285 if (pszHelp)
1286 RTStrmPrintf(pStrm, " %s\n", pszHelp);
1287 }
1288
1289 if (pCmd->fNeedsTransport)
1290 {
1291 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1292 g_apTransports[iTx]->pfnUsage(pStrm);
1293 }
1294 }
1295
1296 return RTEXITCODE_SUCCESS;
1297}
1298
1299/**
1300 * Shows tool version.
1301 */
1302RTEXITCODE audioTestVersion(void)
1303{
1304 RTPrintf("%s\n", RTBldCfgRevisionStr());
1305 return RTEXITCODE_SUCCESS;
1306}
1307
1308/**
1309 * Shows the logo.
1310 *
1311 * @param pStream Output stream to show logo on.
1312 */
1313void audioTestShowLogo(PRTSTREAM pStream)
1314{
1315 RTStrmPrintf(pStream, VBOX_PRODUCT " VKAT (Validation Kit Audio Test) Version " VBOX_VERSION_STRING " - r%s\n"
1316 "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
1317 "All rights reserved.\n\n", RTBldCfgRevisionStr());
1318}
1319
1320int main(int argc, char **argv)
1321{
1322 /*
1323 * Init IPRT.
1324 */
1325 int rc = RTR3InitExe(argc, &argv, 0);
1326 if (RT_FAILURE(rc))
1327 {
1328 RTPrintf("RTR3InitExe() failed with %Rrc\n", rc);
1329 return RTMsgInitFailure(rc);
1330 }
1331
1332 /*
1333 * Handle special command line options which need parsing before
1334 * everything else.
1335 */
1336 bool fDaemonize = false;
1337 bool fDaemonized = false;
1338
1339 RTGETOPTSTATE GetState;
1340 rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions,
1341 RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
1342 AssertRCReturn(rc, RTEXITCODE_INIT);
1343
1344 int ch;
1345 RTGETOPTUNION ValueUnion;
1346 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1347 {
1348 switch (ch)
1349 {
1350 case AUDIO_TEST_OPT_CMN_DAEMONIZE:
1351 fDaemonize = true;
1352 break;
1353
1354 case AUDIO_TEST_OPT_CMN_DAEMONIZED:
1355 fDaemonized = true;
1356 break;
1357
1358 case 'v':
1359 g_uVerbosity++;
1360 break;
1361
1362 default:
1363 break;
1364 }
1365 }
1366
1367 audioTestShowLogo(g_pStdOut);
1368
1369 if (fDaemonize)
1370 {
1371 if (!fDaemonized)
1372 {
1373 rc = RTProcDaemonize(argv, "--daemonized");
1374 if (RT_FAILURE(rc))
1375 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize() failed with %Rrc\n", rc);
1376
1377 RTMsgInfo("Starting in background (daemonizing) ...");
1378 return RTEXITCODE_SUCCESS;
1379 }
1380 /* else continue running in background. */
1381 }
1382
1383 /*
1384 * Init test and globals.
1385 * Note: Needs to be done *after* daemonizing, otherwise the child will fail!
1386 */
1387 rc = RTTestCreate("AudioTest", &g_hTest);
1388 if (RT_FAILURE(rc))
1389 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTTestCreate() failed with %Rrc\n", rc);
1390
1391#ifdef RT_OS_WINDOWS
1392 HRESULT hrc = CoInitializeEx(NULL /*pReserved*/, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE);
1393 if (FAILED(hrc))
1394 RTMsgWarning("CoInitializeEx failed: %#x", hrc);
1395#endif
1396
1397 /*
1398 * Configure release logging to go to stderr.
1399 */
1400 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
1401#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1402 fFlags |= RTLOGFLAGS_USECRLF;
1403#endif
1404 static const char * const g_apszLogGroups[] = VBOX_LOGGROUP_NAMES;
1405 rc = RTLogCreate(&g_pRelLogger, fFlags, "all.e.l", "VKAT_RELEASE_LOG",
1406 RT_ELEMENTS(g_apszLogGroups), g_apszLogGroups, RTLOGDEST_STDOUT, NULL /*"vkat-release.log"*/);
1407 if (RT_SUCCESS(rc))
1408 {
1409 RTLogRelSetDefaultInstance(g_pRelLogger);
1410 if (g_uVerbosity)
1411 {
1412 RTMsgInfo("Setting verbosity logging to level %u\n", g_uVerbosity);
1413 switch (g_uVerbosity) /* Not very elegant, but has to do it for now. */
1414 {
1415 case 1:
1416 rc = RTLogGroupSettings(g_pRelLogger,
1417 "drv_audio.e.l+audio_mixer.e.l+audio_test.e.l");
1418 break;
1419
1420 case 2:
1421 rc = RTLogGroupSettings(g_pRelLogger,
1422 "drv_audio.e.l.l2+audio_mixer.e.l.l2+audio_test.e.l.l2");
1423 break;
1424
1425 case 3:
1426 rc = RTLogGroupSettings(g_pRelLogger,
1427 "drv_audio.e.l.l2.l3+audio_mixer.e.l.l2.l3+audio_test.e.l.l2.l3");
1428 break;
1429
1430 case 4:
1431 RT_FALL_THROUGH();
1432 default:
1433 rc = RTLogGroupSettings(g_pRelLogger,
1434 "drv_audio.e.l.l2.l3.f+audio_mixer.e.l.l2.l3.f+audio_test.e.l.l2.l3.f");
1435 break;
1436 }
1437 if (RT_FAILURE(rc))
1438 RTMsgError("Setting debug logging failed, rc=%Rrc\n", rc);
1439 }
1440 }
1441 else
1442 RTMsgWarning("Failed to create release logger: %Rrc", rc);
1443
1444 /*
1445 * Install a Ctrl-C signal handler.
1446 */
1447#ifdef RT_OS_WINDOWS
1448 signal(SIGINT, audioTestSignalHandler);
1449#else
1450 struct sigaction sa;
1451 RT_ZERO(sa);
1452 sa.sa_handler = audioTestSignalHandler;
1453 sigaction(SIGINT, &sa, NULL);
1454#endif
1455
1456 /*
1457 * Process common options.
1458 */
1459 RT_ZERO(GetState);
1460 rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions,
1461 RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
1462 AssertRCReturn(rc, RTEXITCODE_INIT);
1463
1464 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1465 {
1466 switch (ch)
1467 {
1468 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
1469
1470 case VINF_GETOPT_NOT_OPTION:
1471 {
1472 for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++)
1473 {
1474 PCVKATCMD const pCmd = g_apCommands[iCmd];
1475 if (strcmp(ValueUnion.psz, pCmd->pszCommand) == 0)
1476 {
1477 size_t cCombinedOptions = pCmd->cOptions + RT_ELEMENTS(g_aCmdCommonOptions);
1478 if (pCmd->fNeedsTransport)
1479 {
1480 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1481 cCombinedOptions += g_apTransports[iTx]->cOpts;
1482 }
1483 PRTGETOPTDEF paCombinedOptions = (PRTGETOPTDEF)RTMemAlloc(cCombinedOptions * sizeof(RTGETOPTDEF));
1484 if (paCombinedOptions)
1485 {
1486 uint32_t idxOpts = 0;
1487 memcpy(paCombinedOptions, g_aCmdCommonOptions, sizeof(g_aCmdCommonOptions));
1488 idxOpts += RT_ELEMENTS(g_aCmdCommonOptions);
1489 memcpy(&paCombinedOptions[idxOpts], pCmd->paOptions, pCmd->cOptions * sizeof(RTGETOPTDEF));
1490 idxOpts += (uint32_t)pCmd->cOptions;
1491 if (pCmd->fNeedsTransport)
1492 {
1493 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1494 {
1495 memcpy(&paCombinedOptions[idxOpts],
1496 g_apTransports[iTx]->paOpts, g_apTransports[iTx]->cOpts * sizeof(RTGETOPTDEF));
1497 idxOpts += (uint32_t)g_apTransports[iTx]->cOpts;
1498 }
1499 }
1500
1501 rc = RTGetOptInit(&GetState, argc, argv, paCombinedOptions, cCombinedOptions,
1502 GetState.iNext /*idxFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1503 if (RT_SUCCESS(rc))
1504 {
1505 RTEXITCODE rcExit = pCmd->pfnHandler(&GetState);
1506 RTMemFree(paCombinedOptions);
1507 return rcExit;
1508 }
1509 return RTMsgErrorExitFailure("RTGetOptInit failed for '%s': %Rrc", ValueUnion.psz, rc);
1510 }
1511 return RTMsgErrorExitFailure("Out of memory!");
1512 }
1513 }
1514 RTMsgError("Unknown command '%s'!\n", ValueUnion.psz);
1515 audioTestUsage(g_pStdErr);
1516 return RTEXITCODE_SYNTAX;
1517 }
1518
1519 default:
1520 return RTGetOptPrintError(ch, &ValueUnion);
1521 }
1522 }
1523
1524 RTMsgError("No command specified!\n");
1525 audioTestUsage(g_pStdErr);
1526 return RTEXITCODE_SYNTAX;
1527}
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