VirtualBox

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

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

Audio/Validation Kit: Perform a quick audio driver stack self-test on start of the actual testing (guest + host), plus in self-test mode.​ bugref:10008

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