VirtualBox

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

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

Audio/Validation Kit: One more fix for r147627 (Correctly set the probe backends flags (don't use the value union there)). ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.3 KB
Line 
1/* $Id: vkat.cpp 92002 2021-10-22 12:54:42Z 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 "Probes all (available) backends until a working one is found\n"
731 case VKAT_TEST_OPT_TAG: return "Test set tag to use";
732 case VKAT_TEST_OPT_TEMPDIR: return "Temporary directory to use";
733 case VKAT_TEST_OPT_VOL: return "Audio volume (percent) to use";
734 case VKAT_TEST_OPT_TCP_BIND_ADDRESS: return "TCP address listening to (server mode)";
735 case VKAT_TEST_OPT_TCP_BIND_PORT: return "TCP port listening to (server mode)";
736 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS: return "TCP address to connect to (client mode)";
737 case VKAT_TEST_OPT_TCP_CONNECT_PORT: return "TCP port to connect to (client mode)";
738 default:
739 break;
740 }
741 return NULL;
742}
743
744/**
745 * Main (entry) function for the testing functionality of VKAT.
746 *
747 * @returns Program exit code.
748 * @param pGetState RTGetOpt state.
749 */
750static DECLCALLBACK(RTEXITCODE) audioTestMain(PRTGETOPTSTATE pGetState)
751{
752 AUDIOTESTENV TstEnv;
753 audioTestEnvInit(&TstEnv);
754
755 int rc;
756
757 PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend();
758 uint8_t cPcmSampleBit = 0;
759 uint8_t cPcmChannels = 0;
760 uint32_t uPcmHz = 0;
761 bool fPcmSigned = true;
762 bool fProbeBackends = false;
763
764 const char *pszGuestTcpAddr = NULL;
765 uint16_t uGuestTcpPort = ATS_TCP_DEF_BIND_PORT_GUEST;
766 const char *pszValKitTcpAddr = NULL;
767 uint16_t uValKitTcpPort = ATS_TCP_DEF_BIND_PORT_VALKIT;
768
769 int ch;
770 RTGETOPTUNION ValueUnion;
771 while ((ch = RTGetOpt(pGetState, &ValueUnion)))
772 {
773 switch (ch)
774 {
775 case 'a':
776 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
777 g_aTests[i].fExcluded = true;
778 break;
779
780 case 'b':
781 pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
782 if (pDrvReg == NULL)
783 return RTEXITCODE_SYNTAX;
784 break;
785
786 case 'd':
787 TstEnv.IoOpts.fWithDrvAudio = true;
788 break;
789
790 case 'e':
791 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
792 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
793 g_aTests[ValueUnion.u32].fExcluded = true;
794 break;
795
796 case VKAT_TEST_OPT_GUEST_ATS_ADDR:
797 pszGuestTcpAddr = ValueUnion.psz;
798 break;
799
800 case VKAT_TEST_OPT_GUEST_ATS_PORT:
801 uGuestTcpPort = ValueUnion.u32;
802 break;
803
804 case VKAT_TEST_OPT_HOST_ATS_ADDR:
805 pszValKitTcpAddr = ValueUnion.psz;
806 break;
807
808 case VKAT_TEST_OPT_HOST_ATS_PORT:
809 uValKitTcpPort = ValueUnion.u32;
810 break;
811
812 case VKAT_TEST_OPT_MODE:
813 if (TstEnv.enmMode != AUDIOTESTMODE_UNKNOWN)
814 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Test mode (guest / host) already specified");
815 TstEnv.enmMode = RTStrICmp(ValueUnion.psz, "guest") == 0 ? AUDIOTESTMODE_GUEST : AUDIOTESTMODE_HOST;
816 break;
817
818 case VKAT_TEST_OPT_NO_VERIFY:
819 TstEnv.fSkipVerify = true;
820 break;
821
822 case 'i':
823 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
824 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
825 g_aTests[ValueUnion.u32].fExcluded = false;
826 break;
827
828 case VKAT_TEST_OPT_COUNT:
829 TstEnv.cIterations = ValueUnion.u32;
830 break;
831
832 case VKAT_TEST_OPT_DEV:
833 rc = RTStrCopy(TstEnv.szDev, sizeof(TstEnv.szDev), ValueUnion.psz);
834 if (RT_FAILURE(rc))
835 return RTMsgErrorExitFailure("Failed to copy out device: %Rrc", rc);
836 break;
837
838 case VKAT_TEST_OPT_TONE_DURATION_MS:
839 TstEnv.ToneParms.msDuration = ValueUnion.u32;
840 break;
841
842 case VKAT_TEST_OPT_TONE_VOL_PERCENT:
843 TstEnv.ToneParms.uVolumePercent = ValueUnion.u8;
844 break;
845
846 case VKAT_TEST_OPT_PAUSE:
847 return RTMsgErrorExitFailure("Not yet implemented!");
848
849 case VKAT_TEST_OPT_OUTDIR:
850 rc = RTStrCopy(TstEnv.szPathOut, sizeof(TstEnv.szPathOut), ValueUnion.psz);
851 if (RT_FAILURE(rc))
852 return RTMsgErrorExitFailure("Failed to copy out directory: %Rrc", rc);
853 break;
854
855 case VKAT_TEST_OPT_PCM_BIT:
856 cPcmSampleBit = ValueUnion.u8;
857 break;
858
859 case VKAT_TEST_OPT_PCM_CHAN:
860 cPcmChannels = ValueUnion.u8;
861 break;
862
863 case VKAT_TEST_OPT_PCM_HZ:
864 uPcmHz = ValueUnion.u32;
865 break;
866
867 case VKAT_TEST_OPT_PCM_SIGNED:
868 fPcmSigned = ValueUnion.f;
869 break;
870
871 case VKAT_TEST_OPT_PROBE_BACKENDS:
872 fProbeBackends = true;
873 break;
874
875 case VKAT_TEST_OPT_TAG:
876 rc = RTStrCopy(TstEnv.szTag, sizeof(TstEnv.szTag), ValueUnion.psz);
877 if (RT_FAILURE(rc))
878 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Tag invalid, rc=%Rrc", rc);
879 break;
880
881 case VKAT_TEST_OPT_TEMPDIR:
882 rc = RTStrCopy(TstEnv.szPathTemp, sizeof(TstEnv.szPathTemp), ValueUnion.psz);
883 if (RT_FAILURE(rc))
884 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Temp dir invalid, rc=%Rrc", rc);
885 break;
886
887 case VKAT_TEST_OPT_VOL:
888 TstEnv.IoOpts.uVolumePercent = ValueUnion.u8;
889 break;
890
891 case VKAT_TEST_OPT_TCP_BIND_ADDRESS:
892 rc = RTStrCopy(TstEnv.TcpOpts.szBindAddr, sizeof(TstEnv.TcpOpts.szBindAddr), ValueUnion.psz);
893 if (RT_FAILURE(rc))
894 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Bind address invalid, rc=%Rrc", rc);
895 break;
896
897 case VKAT_TEST_OPT_TCP_BIND_PORT:
898 TstEnv.TcpOpts.uBindPort = ValueUnion.u16;
899 break;
900
901 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS:
902 rc = RTStrCopy(TstEnv.TcpOpts.szConnectAddr, sizeof(TstEnv.TcpOpts.szConnectAddr), ValueUnion.psz);
903 if (RT_FAILURE(rc))
904 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Connect address invalid, rc=%Rrc", rc);
905 break;
906
907 case VKAT_TEST_OPT_TCP_CONNECT_PORT:
908 TstEnv.TcpOpts.uConnectPort = ValueUnion.u16;
909 break;
910
911 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
912
913 default:
914 return RTGetOptPrintError(ch, &ValueUnion);
915 }
916 }
917
918 /*
919 * Start testing.
920 */
921 RTTestBanner(g_hTest);
922
923 if (TstEnv.enmMode == AUDIOTESTMODE_UNKNOWN)
924 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No test mode (--mode) specified!\n");
925
926 /* Validate TCP options. */
927 if ( TstEnv.TcpOpts.szBindAddr[0]
928 && TstEnv.TcpOpts.szConnectAddr[0])
929 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Only one TCP connection mode (connect as client *or* bind as server) can be specified) at a time!\n");
930
931 /* Set new (override standard) I/O PCM properties if set by the user. */
932 if ( cPcmSampleBit
933 || cPcmChannels
934 || uPcmHz)
935 {
936 PDMAudioPropsInit(&TstEnv.IoOpts.Props,
937 cPcmSampleBit ? cPcmSampleBit / 2 : 2 /* 16-bit */, fPcmSigned /* fSigned */,
938 cPcmChannels ? cPcmChannels : 2 /* Stereo */, uPcmHz ? uPcmHz : 44100);
939 }
940
941 /* Do this first before everything else below. */
942 rc = AudioTestDriverStackPerformSelftest();
943 if (RT_FAILURE(rc))
944 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testing driver stack failed: %Rrc\n", rc);
945
946 AUDIOTESTDRVSTACK DrvStack;
947 if (fProbeBackends)
948 rc = audioTestDriverStackProbe(&DrvStack, pDrvReg,
949 true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */
950 else
951 rc = audioTestDriverStackInitEx(&DrvStack, pDrvReg,
952 true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */
953 if (RT_FAILURE(rc))
954 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to init driver stack: %Rrc\n", rc);
955
956 PPDMAUDIOHOSTDEV pDev;
957 rc = audioTestDevicesEnumerateAndCheck(&DrvStack, TstEnv.szDev, &pDev);
958 if (RT_FAILURE(rc))
959 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Enumerating device(s) failed: %Rrc\n", rc);
960
961 /* For now all tests have the same test environment and driver stack. */
962 rc = audioTestEnvCreate(&TstEnv, &DrvStack);
963 if (RT_SUCCESS(rc))
964 rc = audioTestWorker(&TstEnv);
965
966 audioTestEnvDestroy(&TstEnv);
967 audioTestDriverStackDelete(&DrvStack);
968
969 if (RT_FAILURE(rc)) /* Let us know that something went wrong in case we forgot to mention it. */
970 RTTestFailed(g_hTest, "Testing failed with %Rrc\n", rc);
971
972 /*
973 * Print summary and exit.
974 */
975 return RTTestSummaryAndDestroy(g_hTest);
976}
977
978
979const VKATCMD g_CmdTest =
980{
981 "test",
982 audioTestMain,
983 "Runs audio tests and creates an audio test set.",
984 g_aCmdTestOptions,
985 RT_ELEMENTS(g_aCmdTestOptions),
986 audioTestCmdTestHelp,
987 true /* fNeedsTransport */
988};
989
990
991/*********************************************************************************************************************************
992* Command: verify *
993*********************************************************************************************************************************/
994
995static int audioVerifyOpenTestSet(const char *pszPathSet, PAUDIOTESTSET pSet)
996{
997 int rc;
998
999 char szPathExtracted[RTPATH_MAX];
1000
1001 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Opening test set '%s'\n", pszPathSet);
1002
1003 const bool fPacked = AudioTestSetIsPacked(pszPathSet);
1004
1005 if (fPacked)
1006 {
1007 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set is an archive and needs to be unpacked\n");
1008
1009 if (!RTFileExists(pszPathSet))
1010 {
1011 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set '%s' does not exist\n", pszPathSet);
1012 rc = VERR_FILE_NOT_FOUND;
1013 }
1014 else
1015 rc = VINF_SUCCESS;
1016
1017 if (RT_SUCCESS(rc))
1018 {
1019 char szPathTemp[RTPATH_MAX];
1020 rc = RTPathTemp(szPathTemp, sizeof(szPathTemp));
1021 if (RT_SUCCESS(rc))
1022 {
1023 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using temporary directory '%s'\n", szPathTemp);
1024
1025 rc = RTPathJoin(szPathExtracted, sizeof(szPathExtracted), szPathTemp, "vkat-testset-XXXX");
1026 if (RT_SUCCESS(rc))
1027 {
1028 rc = RTDirCreateTemp(szPathExtracted, 0755);
1029 if (RT_SUCCESS(rc))
1030 {
1031 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Unpacking archive to '%s'\n", szPathExtracted);
1032 rc = AudioTestSetUnpack(pszPathSet, szPathExtracted);
1033 if (RT_SUCCESS(rc))
1034 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Archive successfully unpacked\n");
1035 }
1036 }
1037 }
1038 }
1039 }
1040 else
1041 rc = VINF_SUCCESS;
1042
1043 if (RT_SUCCESS(rc))
1044 rc = AudioTestSetOpen(pSet, fPacked ? szPathExtracted : pszPathSet);
1045
1046 if (RT_FAILURE(rc))
1047 RTTestFailed(g_hTest, "Unable to open / unpack test set archive: %Rrc", rc);
1048
1049 return rc;
1050}
1051
1052/**
1053 * Verifies one test set pair.
1054 *
1055 * @returns VBox status code.
1056 * @param pszPathSetA Absolute path to test set A.
1057 * @param pszPathSetB Absolute path to test set B.
1058 * @param pOpts Verification options to use. Optional.
1059 * When NULL, the (very strict) defaults will be used.
1060 */
1061static int audioVerifyOne(const char *pszPathSetA, const char *pszPathSetB, PAUDIOTESTVERIFYOPTS pOpts)
1062{
1063 RTTestSubF(g_hTest, "Verifying");
1064 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verifying test set '%s' with test set '%s'\n", pszPathSetA, pszPathSetB);
1065
1066 AUDIOTESTSET SetA, SetB;
1067 int rc = audioVerifyOpenTestSet(pszPathSetA, &SetA);
1068 if (RT_SUCCESS(rc))
1069 {
1070 rc = audioVerifyOpenTestSet(pszPathSetB, &SetB);
1071 if (RT_SUCCESS(rc))
1072 {
1073 AUDIOTESTERRORDESC errDesc;
1074 if (pOpts)
1075 rc = AudioTestSetVerifyEx(&SetA, &SetB, pOpts, &errDesc);
1076 else
1077 rc = AudioTestSetVerify(&SetA, &SetB, &errDesc);
1078 if (RT_SUCCESS(rc))
1079 {
1080 uint32_t const cErr = AudioTestErrorDescCount(&errDesc);
1081 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%RU32 errors occurred while verifying\n", cErr);
1082
1083 /** @todo Use some AudioTestErrorXXX API for enumeration here later. */
1084 PAUDIOTESTERRORENTRY pErrEntry;
1085 RTListForEach(&errDesc.List, pErrEntry, AUDIOTESTERRORENTRY, Node)
1086 {
1087 if (RT_FAILURE(pErrEntry->rc))
1088 RTTestFailed(g_hTest, "%s\n", pErrEntry->szDesc);
1089 else
1090 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%s\n", pErrEntry->szDesc);
1091 }
1092
1093 if (cErr == 0)
1094 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification successful\n");
1095
1096 AudioTestErrorDescDestroy(&errDesc);
1097 }
1098 else
1099 RTTestFailed(g_hTest, "Verification failed with %Rrc", rc);
1100
1101#ifdef DEBUG
1102 if (g_fDrvAudioDebug)
1103 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
1104 "\n"
1105 "Use the following command line to re-run verification in the debugger:\n"
1106 "gdb --args ./VBoxAudioTest -vvvv --debug-audio verify \"%s\" \"%s\"\n",
1107 SetA.szPathAbs, SetB.szPathAbs);
1108#endif
1109 if (!g_fDrvAudioDebug) /* Don't wipe stuff when debugging. Can be useful for introspecting data. */
1110 AudioTestSetWipe(&SetB);
1111 AudioTestSetClose(&SetB);
1112 }
1113
1114 if (!g_fDrvAudioDebug) /* Ditto. */
1115 AudioTestSetWipe(&SetA);
1116 AudioTestSetClose(&SetA);
1117 }
1118
1119 RTTestSubDone(g_hTest);
1120
1121 return rc;
1122}
1123
1124/** Option help for the 'verify' command. */
1125static DECLCALLBACK(const char *) audioTestCmdVerifyHelp(PCRTGETOPTDEF pOpt)
1126{
1127 switch (pOpt->iShort)
1128 {
1129 case VKAT_VERIFY_OPT_MAX_DIFF_COUNT: return "Specifies the maximum number of differences\n"
1130 " Default: 0 (strict)";
1131 case VKAT_VERIFY_OPT_MAX_DIFF_PERCENT: return "Specifies the maximum difference (percent)\n"
1132 " Default: 0 (strict)";
1133 case VKAT_VERIFY_OPT_MAX_SIZE_PERCENT: return "Specifies the maximum size difference (percent)\n"
1134 " Default: 1 (strict)";
1135 case VKAT_VERIFY_OPT_NORMALIZE: return "Enables / disables audio data normalization\n"
1136 " Default: false";
1137 default:
1138 break;
1139 }
1140 return NULL;
1141}
1142
1143/**
1144 * Main (entry) function for the verification functionality of VKAT.
1145 *
1146 * @returns Program exit code.
1147 * @param pGetState RTGetOpt state.
1148 */
1149static DECLCALLBACK(RTEXITCODE) audioVerifyMain(PRTGETOPTSTATE pGetState)
1150{
1151 /*
1152 * Parse options and process arguments.
1153 */
1154 const char *apszSets[2] = { NULL, NULL };
1155 unsigned iTestSet = 0;
1156
1157 AUDIOTESTVERIFYOPTS Opts;
1158 AudioTestSetVerifyOptsInit(&Opts);
1159
1160 int ch;
1161 RTGETOPTUNION ValueUnion;
1162 while ((ch = RTGetOpt(pGetState, &ValueUnion)))
1163 {
1164 switch (ch)
1165 {
1166 case VKAT_VERIFY_OPT_MAX_DIFF_COUNT:
1167 Opts.cMaxDiff = ValueUnion.u32;
1168 break;
1169
1170 case VKAT_VERIFY_OPT_MAX_DIFF_PERCENT:
1171 Opts.uMaxDiffPercent = ValueUnion.u8;
1172 break;
1173
1174 case VKAT_VERIFY_OPT_MAX_SIZE_PERCENT:
1175 Opts.uMaxSizePercent = ValueUnion.u8;
1176 break;
1177
1178 case VKAT_VERIFY_OPT_NORMALIZE:
1179 Opts.fNormalize = ValueUnion.f;
1180 break;
1181
1182 case VINF_GETOPT_NOT_OPTION:
1183 if (iTestSet == 0)
1184 RTTestBanner(g_hTest);
1185 if (iTestSet >= RT_ELEMENTS(apszSets))
1186 return RTMsgErrorExitFailure("Only two test sets can be verified at one time");
1187 apszSets[iTestSet++] = ValueUnion.psz;
1188 break;
1189
1190 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
1191
1192 default:
1193 return RTGetOptPrintError(ch, &ValueUnion);
1194 }
1195 }
1196
1197 if (!iTestSet)
1198 return RTMsgErrorExitFailure("At least one test set must be specified");
1199
1200 int rc = VINF_SUCCESS;
1201
1202 /*
1203 * If only test set A is given, default to the current directory
1204 * for test set B.
1205 */
1206 char szDirCur[RTPATH_MAX];
1207 if (iTestSet == 1)
1208 {
1209 rc = RTPathGetCurrent(szDirCur, sizeof(szDirCur));
1210 if (RT_SUCCESS(rc))
1211 apszSets[1] = szDirCur;
1212 else
1213 RTTestFailed(g_hTest, "Failed to retrieve current directory: %Rrc", rc);
1214 }
1215
1216 if (RT_SUCCESS(rc))
1217 audioVerifyOne(apszSets[0], apszSets[1], &Opts);
1218
1219 /*
1220 * Print summary and exit.
1221 */
1222 return RTTestSummaryAndDestroy(g_hTest);
1223}
1224
1225
1226const VKATCMD g_CmdVerify =
1227{
1228 "verify",
1229 audioVerifyMain,
1230 "Verifies a formerly created audio test set.",
1231 g_aCmdVerifyOptions,
1232 RT_ELEMENTS(g_aCmdVerifyOptions),
1233 audioTestCmdVerifyHelp,
1234 false /* fNeedsTransport */
1235};
1236
1237
1238/*********************************************************************************************************************************
1239* Main *
1240*********************************************************************************************************************************/
1241
1242/**
1243 * Ctrl-C signal handler.
1244 *
1245 * This just sets g_fTerminate and hope it will be noticed soon. It restores
1246 * the SIGINT action to default, so that a second Ctrl-C will have the normal
1247 * effect (just in case the code doesn't respond to g_fTerminate).
1248 */
1249static void audioTestSignalHandler(int iSig) RT_NOEXCEPT
1250{
1251 Assert(iSig == SIGINT); RT_NOREF(iSig);
1252 RTPrintf("Ctrl-C!\n");
1253 ASMAtomicWriteBool(&g_fTerminate, true);
1254 signal(SIGINT, SIG_DFL);
1255}
1256
1257/**
1258 * Commands.
1259 */
1260const VKATCMD *g_apCommands[] =
1261{
1262 &g_CmdTest,
1263 &g_CmdVerify,
1264 &g_CmdEnum,
1265 &g_CmdPlay,
1266 &g_CmdRec,
1267 &g_CmdSelfTest
1268};
1269
1270/**
1271 * Shows tool usage text.
1272 */
1273RTEXITCODE audioTestUsage(PRTSTREAM pStrm)
1274{
1275 RTStrmPrintf(pStrm, "usage: %s [global options] <command> [command-options]\n",
1276 RTPathFilename(RTProcExecutablePath()));
1277 RTStrmPrintf(pStrm,
1278 "\n"
1279 "Global Options:\n"
1280 " --debug-audio\n"
1281 " Enables (DrvAudio) debugging.\n"
1282 " --debug-audio-path=<path>\n"
1283 " Tells DrvAudio where to put its debug output (wav-files).\n"
1284 " -q, --quiet\n"
1285 " Sets verbosity to zero.\n"
1286 " -v, --verbose\n"
1287 " Increase verbosity.\n"
1288 " -V, --version\n"
1289 " Displays version.\n"
1290 " -h, -?, --help\n"
1291 " Displays help.\n"
1292 );
1293
1294 for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++)
1295 {
1296 PCVKATCMD const pCmd = g_apCommands[iCmd];
1297 RTStrmPrintf(pStrm,
1298 "\n"
1299 "Command '%s':\n"
1300 " %s\n"
1301 "Options for '%s':\n",
1302 pCmd->pszCommand, pCmd->pszDesc, pCmd->pszCommand);
1303 PCRTGETOPTDEF const paOptions = pCmd->paOptions;
1304 for (unsigned i = 0; i < pCmd->cOptions; i++)
1305 {
1306 if (RT_C_IS_PRINT(paOptions[i].iShort))
1307 RTStrmPrintf(pStrm, " -%c, %s\n", paOptions[i].iShort, paOptions[i].pszLong);
1308 else
1309 RTStrmPrintf(pStrm, " %s\n", paOptions[i].pszLong);
1310
1311 const char *pszHelp = NULL;
1312 if (pCmd->pfnOptionHelp)
1313 pszHelp = pCmd->pfnOptionHelp(&paOptions[i]);
1314 if (pszHelp)
1315 RTStrmPrintf(pStrm, " %s\n", pszHelp);
1316 }
1317
1318 if (pCmd->fNeedsTransport)
1319 {
1320 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1321 g_apTransports[iTx]->pfnUsage(pStrm);
1322 }
1323 }
1324
1325 return RTEXITCODE_SUCCESS;
1326}
1327
1328/**
1329 * Shows tool version.
1330 */
1331RTEXITCODE audioTestVersion(void)
1332{
1333 RTPrintf("%s\n", RTBldCfgRevisionStr());
1334 return RTEXITCODE_SUCCESS;
1335}
1336
1337/**
1338 * Shows the logo.
1339 *
1340 * @param pStream Output stream to show logo on.
1341 */
1342void audioTestShowLogo(PRTSTREAM pStream)
1343{
1344 RTStrmPrintf(pStream, VBOX_PRODUCT " VKAT (Validation Kit Audio Test) Version " VBOX_VERSION_STRING " - r%s\n"
1345 "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
1346 "All rights reserved.\n\n", RTBldCfgRevisionStr());
1347}
1348
1349int main(int argc, char **argv)
1350{
1351 /*
1352 * Init IPRT.
1353 */
1354 int rc = RTR3InitExe(argc, &argv, 0);
1355 if (RT_FAILURE(rc))
1356 {
1357 RTPrintf("RTR3InitExe() failed with %Rrc\n", rc);
1358 return RTMsgInitFailure(rc);
1359 }
1360
1361 /*
1362 * Handle special command line options which need parsing before
1363 * everything else.
1364 */
1365 bool fDaemonize = false;
1366 bool fDaemonized = false;
1367
1368 RTGETOPTSTATE GetState;
1369 rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions,
1370 RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
1371 AssertRCReturn(rc, RTEXITCODE_INIT);
1372
1373 int ch;
1374 RTGETOPTUNION ValueUnion;
1375 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1376 {
1377 switch (ch)
1378 {
1379 case AUDIO_TEST_OPT_CMN_DAEMONIZE:
1380 fDaemonize = true;
1381 break;
1382
1383 case AUDIO_TEST_OPT_CMN_DAEMONIZED:
1384 fDaemonized = true;
1385 break;
1386
1387 case 'v':
1388 g_uVerbosity++;
1389 break;
1390
1391 default:
1392 break;
1393 }
1394 }
1395
1396 audioTestShowLogo(g_pStdOut);
1397
1398 if (fDaemonize)
1399 {
1400 if (!fDaemonized)
1401 {
1402 rc = RTProcDaemonize(argv, "--daemonized");
1403 if (RT_FAILURE(rc))
1404 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize() failed with %Rrc\n", rc);
1405
1406 RTMsgInfo("Starting in background (daemonizing) ...");
1407 return RTEXITCODE_SUCCESS;
1408 }
1409 /* else continue running in background. */
1410 }
1411
1412 /*
1413 * Init test and globals.
1414 * Note: Needs to be done *after* daemonizing, otherwise the child will fail!
1415 */
1416 rc = RTTestCreate("AudioTest", &g_hTest);
1417 if (RT_FAILURE(rc))
1418 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTTestCreate() failed with %Rrc\n", rc);
1419
1420#ifdef RT_OS_WINDOWS
1421 HRESULT hrc = CoInitializeEx(NULL /*pReserved*/, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE);
1422 if (FAILED(hrc))
1423 RTMsgWarning("CoInitializeEx failed: %#x", hrc);
1424#endif
1425
1426 /*
1427 * Configure release logging to go to stderr.
1428 */
1429 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
1430#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1431 fFlags |= RTLOGFLAGS_USECRLF;
1432#endif
1433 static const char * const g_apszLogGroups[] = VBOX_LOGGROUP_NAMES;
1434 rc = RTLogCreate(&g_pRelLogger, fFlags, "all.e.l", "VKAT_RELEASE_LOG",
1435 RT_ELEMENTS(g_apszLogGroups), g_apszLogGroups, RTLOGDEST_STDOUT, NULL /*"vkat-release.log"*/);
1436 if (RT_SUCCESS(rc))
1437 {
1438 RTLogRelSetDefaultInstance(g_pRelLogger);
1439 if (g_uVerbosity)
1440 {
1441 RTMsgInfo("Setting verbosity logging to level %u\n", g_uVerbosity);
1442 switch (g_uVerbosity) /* Not very elegant, but has to do it for now. */
1443 {
1444 case 1:
1445 rc = RTLogGroupSettings(g_pRelLogger,
1446 "drv_audio.e.l+audio_mixer.e.l+audio_test.e.l");
1447 break;
1448
1449 case 2:
1450 rc = RTLogGroupSettings(g_pRelLogger,
1451 "drv_audio.e.l.l2+audio_mixer.e.l.l2+audio_test.e.l.l2");
1452 break;
1453
1454 case 3:
1455 rc = RTLogGroupSettings(g_pRelLogger,
1456 "drv_audio.e.l.l2.l3+audio_mixer.e.l.l2.l3+audio_test.e.l.l2.l3");
1457 break;
1458
1459 case 4:
1460 RT_FALL_THROUGH();
1461 default:
1462 rc = RTLogGroupSettings(g_pRelLogger,
1463 "drv_audio.e.l.l2.l3.f+audio_mixer.e.l.l2.l3.f+audio_test.e.l.l2.l3.f");
1464 break;
1465 }
1466 if (RT_FAILURE(rc))
1467 RTMsgError("Setting debug logging failed, rc=%Rrc\n", rc);
1468 }
1469 }
1470 else
1471 RTMsgWarning("Failed to create release logger: %Rrc", rc);
1472
1473 /*
1474 * Install a Ctrl-C signal handler.
1475 */
1476#ifdef RT_OS_WINDOWS
1477 signal(SIGINT, audioTestSignalHandler);
1478#else
1479 struct sigaction sa;
1480 RT_ZERO(sa);
1481 sa.sa_handler = audioTestSignalHandler;
1482 sigaction(SIGINT, &sa, NULL);
1483#endif
1484
1485 /*
1486 * Process common options.
1487 */
1488 RT_ZERO(GetState);
1489 rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions,
1490 RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
1491 AssertRCReturn(rc, RTEXITCODE_INIT);
1492
1493 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1494 {
1495 switch (ch)
1496 {
1497 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
1498
1499 case VINF_GETOPT_NOT_OPTION:
1500 {
1501 for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++)
1502 {
1503 PCVKATCMD const pCmd = g_apCommands[iCmd];
1504 if (strcmp(ValueUnion.psz, pCmd->pszCommand) == 0)
1505 {
1506 size_t cCombinedOptions = pCmd->cOptions + RT_ELEMENTS(g_aCmdCommonOptions);
1507 if (pCmd->fNeedsTransport)
1508 {
1509 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1510 cCombinedOptions += g_apTransports[iTx]->cOpts;
1511 }
1512 PRTGETOPTDEF paCombinedOptions = (PRTGETOPTDEF)RTMemAlloc(cCombinedOptions * sizeof(RTGETOPTDEF));
1513 if (paCombinedOptions)
1514 {
1515 uint32_t idxOpts = 0;
1516 memcpy(paCombinedOptions, g_aCmdCommonOptions, sizeof(g_aCmdCommonOptions));
1517 idxOpts += RT_ELEMENTS(g_aCmdCommonOptions);
1518 memcpy(&paCombinedOptions[idxOpts], pCmd->paOptions, pCmd->cOptions * sizeof(RTGETOPTDEF));
1519 idxOpts += (uint32_t)pCmd->cOptions;
1520 if (pCmd->fNeedsTransport)
1521 {
1522 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1523 {
1524 memcpy(&paCombinedOptions[idxOpts],
1525 g_apTransports[iTx]->paOpts, g_apTransports[iTx]->cOpts * sizeof(RTGETOPTDEF));
1526 idxOpts += (uint32_t)g_apTransports[iTx]->cOpts;
1527 }
1528 }
1529
1530 rc = RTGetOptInit(&GetState, argc, argv, paCombinedOptions, cCombinedOptions,
1531 GetState.iNext /*idxFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1532 if (RT_SUCCESS(rc))
1533 {
1534 RTEXITCODE rcExit = pCmd->pfnHandler(&GetState);
1535 RTMemFree(paCombinedOptions);
1536 return rcExit;
1537 }
1538 return RTMsgErrorExitFailure("RTGetOptInit failed for '%s': %Rrc", ValueUnion.psz, rc);
1539 }
1540 return RTMsgErrorExitFailure("Out of memory!");
1541 }
1542 }
1543 RTMsgError("Unknown command '%s'!\n", ValueUnion.psz);
1544 audioTestUsage(g_pStdErr);
1545 return RTEXITCODE_SYNTAX;
1546 }
1547
1548 default:
1549 return RTGetOptPrintError(ch, &ValueUnion);
1550 }
1551 }
1552
1553 RTMsgError("No command specified!\n");
1554 audioTestUsage(g_pStdErr);
1555 return RTEXITCODE_SYNTAX;
1556}
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