VirtualBox

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

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

Audio/Validation Kit: Syntax help massaging. bugref:10008

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