VirtualBox

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

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

Audio/Validation Kit: Factored out general I/O options into AUDIOTESTIOOPTS to also support setting the master volume besides the test tone volume, as function parameter lists already were too big. Also uniformed the command line volume arguments a little ("--vol vs. --volume" everywhere). ​bugref:10008

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette