VirtualBox

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

最後變更 在這個檔案從92019是 92019,由 vboxsync 提交於 3 年 前

Audio/Validation Kit: Added "--no-audio-ok" switch to VKAT to enable running on hosts which do not have any audio hardware (e.g. servers). ​bugref:10008

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 59.2 KB
 
1/* $Id: vkat.cpp 92019 2021-10-25 10:10:32Z 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.alldomusa.eu.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 pTstParmsAcq->cIterations = pTstEnv->cIterations == 0 ? RTRandU32Ex(1, 10) : pTstEnv->cIterations;
285 pTstParmsAcq->idxCurrent = 0;
286
287 pTstParmsAcq->TestTone = pTstEnv->ToneParms;
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 for (uint32_t i = 0; i < pTstParms->cIterations; i++)
302 {
303 PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone;
304
305 pToneParms->Hdr.idxSeq = i;
306 RTTIMESPEC NowTimeSpec;
307 RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec));
308
309 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32/%RU16: Playing test tone (%RU16Hz, %RU32ms)\n",
310 pTstParms->idxCurrent, i, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
311
312 /*
313 * 1. Arm the (host) ValKit ATS with the recording parameters.
314 */
315 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to record new tone ...\n");
316 rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClValKit, pToneParms);
317 if (RT_SUCCESS(rc))
318 {
319 /* Give the Validaiton Kit audio driver on the host a bit of time to register / arming the new test. */
320 RTThreadSleep(5000); /* Fudge factor. */
321
322 /*
323 * 2. Tell VKAT on guest to start playback.
324 */
325 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to play tone ...\n");
326 rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClGuest, pToneParms);
327 if (RT_FAILURE(rc))
328 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientTonePlay() failed with %Rrc\n",
329 pTstParms->idxCurrent, i, rc);
330 }
331 else
332 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientToneRecord() failed with %Rrc\n",
333 pTstParms->idxCurrent, i, rc);
334
335 if (RT_SUCCESS(rc))
336 {
337 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Playing tone done\n");
338
339 /* Give the audio stack a random amount of time for draining data before the next iteration. */
340 if (pTstParms->cIterations > 1)
341 RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */
342 }
343
344 if (RT_FAILURE(rc))
345 {
346 RTTestFailed(g_hTest, "Test #%RU32/%RU16: Playing test tone failed with %Rrc\n", pTstParms->idxCurrent, i, rc);
347 break; /* Not worth retrying, bail out. */
348 }
349 }
350
351 return rc;
352}
353
354/**
355 * @copydoc FNAUDIOTESTDESTROY
356 */
357static DECLCALLBACK(int) audioTestPlayToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx)
358{
359 RT_NOREF(pTstEnv, pvCtx);
360
361 return VINF_SUCCESS;
362}
363
364/**
365 * @copydoc FNAUDIOTESTSETUP
366 */
367static DECLCALLBACK(int) audioTestRecordToneSetup(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, PAUDIOTESTPARMS pTstParmsAcq, void **ppvCtx)
368{
369 RT_NOREF(pTstDesc, ppvCtx);
370
371 int rc = VINF_SUCCESS;
372
373 if (strlen(pTstEnv->szDev))
374 {
375 rc = audioTestDriverStackSetDevice(pTstEnv->pDrvStack, PDMAUDIODIR_IN, pTstEnv->szDev);
376 if (RT_FAILURE(rc))
377 return rc;
378 }
379
380 pTstParmsAcq->enmType = AUDIOTESTTYPE_TESTTONE_RECORD;
381 pTstParmsAcq->enmDir = PDMAUDIODIR_IN;
382 pTstParmsAcq->cIterations = pTstEnv->cIterations == 0 ? RTRandU32Ex(1, 10) : pTstEnv->cIterations;
383 pTstParmsAcq->idxCurrent = 0;
384
385 pTstParmsAcq->TestTone = pTstEnv->ToneParms;
386
387 return rc;
388}
389
390/**
391 * @copydoc FNAUDIOTESTEXEC
392 */
393static DECLCALLBACK(int) audioTestRecordToneExec(PAUDIOTESTENV pTstEnv, void *pvCtx, PAUDIOTESTPARMS pTstParms)
394{
395 RT_NOREF(pvCtx);
396
397 int rc = VINF_SUCCESS;
398
399 for (uint32_t i = 0; i < pTstParms->cIterations; i++)
400 {
401 PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone;
402
403 pToneParms->Hdr.idxSeq = i;
404 RTTIMESPEC NowTimeSpec;
405 RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec));
406
407 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32/%RU16: Recording test tone (%RU16Hz, %RU32ms)\n",
408 pTstParms->idxCurrent, i, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration);
409
410 /*
411 * 1. Arm the (host) ValKit ATS with the playback parameters.
412 */
413 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to inject recording data ...\n");
414 rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClValKit, &pTstParms->TestTone);
415 if (RT_SUCCESS(rc))
416 {
417 /*
418 * 2. Tell the guest ATS to start recording.
419 */
420 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to record audio ...\n");
421 rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClGuest, &pTstParms->TestTone);
422 if (RT_FAILURE(rc))
423 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientToneRecord() failed with %Rrc\n",
424 pTstParms->idxCurrent, i, rc);
425 }
426 else
427 RTTestFailed(g_hTest, "Test #%RU32/%RU16: AudioTestSvcClientTonePlay() failed with %Rrc\n",
428 pTstParms->idxCurrent, i, rc);
429
430 if (RT_SUCCESS(rc))
431 {
432 /* Wait a bit to let the left over audio bits being processed. */
433 if (pTstParms->cIterations > 1)
434 RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */
435 }
436
437 if (RT_FAILURE(rc))
438 {
439 RTTestFailed(g_hTest, "Test #%RU32/%RU16: Recording test tone failed with %Rrc\n", pTstParms->idxCurrent, i, rc);
440 break; /* Not worth retrying, bail out. */
441 }
442 }
443
444 return rc;
445}
446
447/**
448 * @copydoc FNAUDIOTESTDESTROY
449 */
450static DECLCALLBACK(int) audioTestRecordToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx)
451{
452 RT_NOREF(pTstEnv, pvCtx);
453
454 return VINF_SUCCESS;
455}
456
457
458/*********************************************************************************************************************************
459* Test execution *
460*********************************************************************************************************************************/
461
462/** Test definition table. */
463AUDIOTESTDESC g_aTests[] =
464{
465 /* pszTest fExcluded pfnSetup */
466 { "PlayTone", false, audioTestPlayToneSetup, audioTestPlayToneExec, audioTestPlayToneDestroy },
467 { "RecordTone", false, audioTestRecordToneSetup, audioTestRecordToneExec, audioTestRecordToneDestroy }
468};
469/** Number of tests defined. */
470unsigned g_cTests = RT_ELEMENTS(g_aTests);
471
472/**
473 * Runs one specific audio test.
474 *
475 * @returns VBox status code.
476 * @param pTstEnv Test environment to use for running the test.
477 * @param pTstDesc Test to run.
478 * @param uSeq Test sequence # in case there are more tests.
479 */
480static int audioTestOne(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, unsigned uSeq)
481{
482 RT_NOREF(uSeq);
483
484 int rc;
485
486 AUDIOTESTPARMS TstParms;
487 audioTestParmsInit(&TstParms);
488
489 RTTestSub(g_hTest, pTstDesc->pszName);
490
491 if (pTstDesc->fExcluded)
492 {
493 RTTestSkipped(g_hTest, "Test #%u is excluded from list, skipping", uSeq);
494 return VINF_SUCCESS;
495 }
496
497 void *pvCtx = NULL; /* Test-specific opaque context. Optional and can be NULL. */
498
499 if (pTstDesc->pfnSetup)
500 {
501 rc = pTstDesc->pfnSetup(pTstEnv, pTstDesc, &TstParms, &pvCtx);
502 if (RT_FAILURE(rc))
503 {
504 RTTestFailed(g_hTest, "Test #%u setup failed with %Rrc\n", uSeq, rc);
505 return rc;
506 }
507 }
508
509 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%u (%RU32 iterations total)\n", uSeq, TstParms.cIterations);
510
511 AssertPtr(pTstDesc->pfnExec);
512 rc = pTstDesc->pfnExec(pTstEnv, pvCtx, &TstParms);
513 if (RT_FAILURE(rc))
514 RTTestFailed(g_hTest, "Test #%u failed with %Rrc\n", uSeq, rc);
515
516 RTTestSubDone(g_hTest);
517
518 if (pTstDesc->pfnDestroy)
519 {
520 int rc2 = pTstDesc->pfnDestroy(pTstEnv, pvCtx);
521 if (RT_SUCCESS(rc))
522 rc = rc2;
523
524 if (RT_FAILURE(rc2))
525 RTTestFailed(g_hTest, "Test #%u destruction failed with %Rrc\n", uSeq, rc2);
526 }
527
528 audioTestParmsDestroy(&TstParms);
529
530 return rc;
531}
532
533/**
534 * Runs all specified tests in a row.
535 *
536 * @returns VBox status code.
537 * @param pTstEnv Test environment to use for running all tests.
538 */
539int audioTestWorker(PAUDIOTESTENV pTstEnv)
540{
541 int rc = VINF_SUCCESS;
542
543 if (pTstEnv->enmMode == AUDIOTESTMODE_GUEST)
544 {
545 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS running\n");
546
547 while (!g_fTerminate)
548 RTThreadSleep(100);
549
550 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down guest ATS ...\n");
551
552 int rc2 = AudioTestSvcStop(pTstEnv->pSrv);
553 if (RT_SUCCESS(rc))
554 rc = rc2;
555
556 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS shutdown complete\n");
557 }
558 else if (pTstEnv->enmMode == AUDIOTESTMODE_HOST)
559 {
560 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using tag '%s'\n", pTstEnv->szTag);
561
562 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to begin a new test set ...\n");
563 rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag);
564 if (RT_SUCCESS(rc))
565 {
566 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to begin a new test set ...\n");
567 rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag);
568 if (RT_FAILURE(rc))
569 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
570 "Beginning test set on guest failed with %Rrc\n", rc);
571 }
572 else
573 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
574 "Beginning test set on host (Validation Kit audio driver) failed with %Rrc\n", rc);
575
576 if (RT_SUCCESS(rc))
577 {
578 unsigned uSeq = 0;
579 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
580 {
581 int rc2 = audioTestOne(pTstEnv, &g_aTests[i], uSeq);
582 if (RT_SUCCESS(rc))
583 rc = rc2;
584
585 if (!g_aTests[i].fExcluded)
586 uSeq++;
587
588 if (g_fTerminate)
589 break;
590 }
591
592 if (RT_SUCCESS(rc))
593 {
594 /** @todo Fudge! */
595 RTMSINTERVAL const msWait = RTRandU32Ex(RT_MS_1SEC, RT_MS_5SEC);
596 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
597 "Waiting %RU32ms to let guest and the audio stack process remaining data ...\n", msWait);
598 RTThreadSleep(msWait);
599 }
600
601 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest ...\n");
602 int rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag);
603 if (RT_FAILURE(rc2))
604 {
605 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest failed with %Rrc\n", rc2);
606 if (RT_SUCCESS(rc))
607 rc = rc2;
608 }
609
610 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on host (Validation Kit audio driver) ...\n");
611 rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag);
612 if (RT_FAILURE(rc2))
613 {
614 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
615 "Ending test set on host (Validation Kit audio driver) failed with %Rrc\n", rc2);
616 if (RT_SUCCESS(rc))
617 rc = rc2;
618 }
619
620 if ( !g_fTerminate
621 && RT_SUCCESS(rc))
622 {
623 /*
624 * Download guest + Validation Kit audio driver test sets to our output directory.
625 */
626 char szFileName[RTPATH_MAX];
627 if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-guest.tar.gz", pTstEnv->szTag))
628 {
629 rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetGuest, sizeof(pTstEnv->u.Host.szPathTestSetGuest),
630 pTstEnv->szPathOut, szFileName);
631 if (RT_SUCCESS(rc))
632 {
633 if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-host.tar.gz", pTstEnv->szTag))
634 {
635 rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetValKit, sizeof(pTstEnv->u.Host.szPathTestSetValKit),
636 pTstEnv->szPathOut, szFileName);
637 }
638 else
639 rc = VERR_BUFFER_OVERFLOW;
640 }
641 else
642 rc = VERR_BUFFER_OVERFLOW;
643
644 if (RT_SUCCESS(rc))
645 {
646 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading guest test set to '%s'\n",
647 pTstEnv->u.Host.szPathTestSetGuest);
648 rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClGuest,
649 pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetGuest);
650 }
651
652 if (RT_SUCCESS(rc))
653 {
654 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading host test set to '%s'\n",
655 pTstEnv->u.Host.szPathTestSetValKit);
656 rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClValKit,
657 pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetValKit);
658 }
659 }
660 else
661 rc = VERR_BUFFER_OVERFLOW;
662
663 if ( RT_SUCCESS(rc)
664 && !pTstEnv->fSkipVerify)
665 {
666 rc = audioVerifyOne(pTstEnv->u.Host.szPathTestSetGuest, pTstEnv->u.Host.szPathTestSetValKit, NULL /* pOpts */);
667 }
668 else
669 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification skipped\n");
670
671 if (!pTstEnv->fSkipVerify)
672 {
673 RTFileDelete(pTstEnv->u.Host.szPathTestSetGuest);
674 RTFileDelete(pTstEnv->u.Host.szPathTestSetValKit);
675 }
676 else
677 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Leaving test set files behind\n");
678 }
679 }
680 }
681 else
682 rc = VERR_NOT_IMPLEMENTED;
683
684 /* Clean up. */
685 RTDirRemove(pTstEnv->szPathTemp);
686 RTDirRemove(pTstEnv->szPathOut);
687
688 if (RT_FAILURE(rc))
689 RTTestFailed(g_hTest, "Test worker failed with %Rrc", rc);
690
691 return rc;
692}
693
694/** Option help for the 'test' command. */
695static DECLCALLBACK(const char *) audioTestCmdTestHelp(PCRTGETOPTDEF pOpt)
696{
697 switch (pOpt->iShort)
698 {
699 case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)";
700 case 'b': return "The audio backend to use";
701 case 'd': return "Go via DrvAudio instead of directly interfacing with the backend";
702 case 'e': return "Exclude the given test id from the list";
703 case 'i': return "Include the given test id in the list";
704 case VKAT_TEST_OPT_COUNT: return "Number of test iterations to perform for selected tests\n"
705 " Default: random number";
706 case VKAT_TEST_OPT_DEV: return "Name of the input/output device to use\n"
707 " Default: default device";
708 case VKAT_TEST_OPT_TONE_DURATION_MS: return "Test tone duration to play / record (ms)\n"
709 " Default: random duration";
710 case VKAT_TEST_OPT_TONE_VOL_PERCENT: return "Test tone volume (percent)\n"
711 " Default: 100";
712 case VKAT_TEST_OPT_GUEST_ATS_ADDR: return "Address of guest ATS to connect to\n"
713 " Default: " ATS_TCP_DEF_CONNECT_GUEST_STR;
714 case VKAT_TEST_OPT_GUEST_ATS_PORT: return "Port of guest ATS to connect to (needs NAT port forwarding)\n"
715 " Default: 6042"; /* ATS_TCP_DEF_CONNECT_PORT_GUEST */
716 case VKAT_TEST_OPT_HOST_ATS_ADDR: return "Address of host ATS to connect to\n"
717 " Default: " ATS_TCP_DEF_CONNECT_HOST_ADDR_STR;
718 case VKAT_TEST_OPT_HOST_ATS_PORT: return "Port of host ATS to connect to\n"
719 " Default: 6052"; /* ATS_TCP_DEF_BIND_PORT_VALKIT */
720 case VKAT_TEST_OPT_MODE: return "Test mode to use when running the tests";
721 case VKAT_TEST_OPT_NO_AUDIO_OK: return "Enables running without any found audio hardware (e.g. servers)";
722 case VKAT_TEST_OPT_NO_VERIFY: return "Skips the verification step";
723 case VKAT_TEST_OPT_OUTDIR: return "Output directory to use";
724 case VKAT_TEST_OPT_PAUSE: return "Not yet implemented";
725 case VKAT_TEST_OPT_PCM_HZ: return "PCM Hertz (Hz) rate to use\n"
726 " Default: 44100";
727 case VKAT_TEST_OPT_PCM_BIT: return "PCM sample bits (i.e. 16) to use\n"
728 " Default: 16";
729 case VKAT_TEST_OPT_PCM_CHAN: return "PCM channels to use\n"
730 " Default: 2";
731 case VKAT_TEST_OPT_PCM_SIGNED: return "PCM samples to use (signed = true, unsigned = false)\n"
732 " Default: true";
733 case VKAT_TEST_OPT_PROBE_BACKENDS: return "Probes all (available) backends until a working one is found\n";
734 case VKAT_TEST_OPT_TAG: return "Test set tag to use";
735 case VKAT_TEST_OPT_TEMPDIR: return "Temporary directory to use";
736 case VKAT_TEST_OPT_VOL: return "Audio volume (percent) to use";
737 case VKAT_TEST_OPT_TCP_BIND_ADDRESS: return "TCP address listening to (server mode)";
738 case VKAT_TEST_OPT_TCP_BIND_PORT: return "TCP port listening to (server mode)";
739 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS: return "TCP address to connect to (client mode)";
740 case VKAT_TEST_OPT_TCP_CONNECT_PORT: return "TCP port to connect to (client mode)";
741 default:
742 break;
743 }
744 return NULL;
745}
746
747/**
748 * Main (entry) function for the testing functionality of VKAT.
749 *
750 * @returns Program exit code.
751 * @param pGetState RTGetOpt state.
752 */
753static DECLCALLBACK(RTEXITCODE) audioTestMain(PRTGETOPTSTATE pGetState)
754{
755 AUDIOTESTENV TstEnv;
756 audioTestEnvInit(&TstEnv);
757
758 int rc;
759
760 PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend();
761 uint8_t cPcmSampleBit = 0;
762 uint8_t cPcmChannels = 0;
763 uint32_t uPcmHz = 0;
764 bool fPcmSigned = true;
765 bool fProbeBackends = false;
766 bool fNoAudioOk = false;
767
768 const char *pszGuestTcpAddr = NULL;
769 uint16_t uGuestTcpPort = ATS_TCP_DEF_BIND_PORT_GUEST;
770 const char *pszValKitTcpAddr = NULL;
771 uint16_t uValKitTcpPort = ATS_TCP_DEF_BIND_PORT_VALKIT;
772
773 int ch;
774 RTGETOPTUNION ValueUnion;
775 while ((ch = RTGetOpt(pGetState, &ValueUnion)))
776 {
777 switch (ch)
778 {
779 case 'a':
780 for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
781 g_aTests[i].fExcluded = true;
782 break;
783
784 case 'b':
785 pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
786 if (pDrvReg == NULL)
787 return RTEXITCODE_SYNTAX;
788 break;
789
790 case 'd':
791 TstEnv.IoOpts.fWithDrvAudio = true;
792 break;
793
794 case 'e':
795 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
796 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
797 g_aTests[ValueUnion.u32].fExcluded = true;
798 break;
799
800 case VKAT_TEST_OPT_GUEST_ATS_ADDR:
801 pszGuestTcpAddr = ValueUnion.psz;
802 break;
803
804 case VKAT_TEST_OPT_GUEST_ATS_PORT:
805 uGuestTcpPort = ValueUnion.u32;
806 break;
807
808 case VKAT_TEST_OPT_HOST_ATS_ADDR:
809 pszValKitTcpAddr = ValueUnion.psz;
810 break;
811
812 case VKAT_TEST_OPT_HOST_ATS_PORT:
813 uValKitTcpPort = ValueUnion.u32;
814 break;
815
816 case VKAT_TEST_OPT_MODE:
817 if (TstEnv.enmMode != AUDIOTESTMODE_UNKNOWN)
818 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Test mode (guest / host) already specified");
819 TstEnv.enmMode = RTStrICmp(ValueUnion.psz, "guest") == 0 ? AUDIOTESTMODE_GUEST : AUDIOTESTMODE_HOST;
820 break;
821
822 case VKAT_TEST_OPT_NO_AUDIO_OK:
823 fNoAudioOk = true;
824 break;
825
826 case VKAT_TEST_OPT_NO_VERIFY:
827 TstEnv.fSkipVerify = true;
828 break;
829
830 case 'i':
831 if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests))
832 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
833 g_aTests[ValueUnion.u32].fExcluded = false;
834 break;
835
836 case VKAT_TEST_OPT_COUNT:
837 TstEnv.cIterations = ValueUnion.u32;
838 break;
839
840 case VKAT_TEST_OPT_DEV:
841 rc = RTStrCopy(TstEnv.szDev, sizeof(TstEnv.szDev), ValueUnion.psz);
842 if (RT_FAILURE(rc))
843 return RTMsgErrorExitFailure("Failed to copy out device: %Rrc", rc);
844 break;
845
846 case VKAT_TEST_OPT_TONE_DURATION_MS:
847 TstEnv.ToneParms.msDuration = ValueUnion.u32;
848 break;
849
850 case VKAT_TEST_OPT_TONE_VOL_PERCENT:
851 TstEnv.ToneParms.uVolumePercent = ValueUnion.u8;
852 break;
853
854 case VKAT_TEST_OPT_PAUSE:
855 return RTMsgErrorExitFailure("Not yet implemented!");
856
857 case VKAT_TEST_OPT_OUTDIR:
858 rc = RTStrCopy(TstEnv.szPathOut, sizeof(TstEnv.szPathOut), ValueUnion.psz);
859 if (RT_FAILURE(rc))
860 return RTMsgErrorExitFailure("Failed to copy out directory: %Rrc", rc);
861 break;
862
863 case VKAT_TEST_OPT_PCM_BIT:
864 cPcmSampleBit = ValueUnion.u8;
865 break;
866
867 case VKAT_TEST_OPT_PCM_CHAN:
868 cPcmChannels = ValueUnion.u8;
869 break;
870
871 case VKAT_TEST_OPT_PCM_HZ:
872 uPcmHz = ValueUnion.u32;
873 break;
874
875 case VKAT_TEST_OPT_PCM_SIGNED:
876 fPcmSigned = ValueUnion.f;
877 break;
878
879 case VKAT_TEST_OPT_PROBE_BACKENDS:
880 fProbeBackends = true;
881 break;
882
883 case VKAT_TEST_OPT_TAG:
884 rc = RTStrCopy(TstEnv.szTag, sizeof(TstEnv.szTag), ValueUnion.psz);
885 if (RT_FAILURE(rc))
886 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Tag invalid, rc=%Rrc", rc);
887 break;
888
889 case VKAT_TEST_OPT_TEMPDIR:
890 rc = RTStrCopy(TstEnv.szPathTemp, sizeof(TstEnv.szPathTemp), ValueUnion.psz);
891 if (RT_FAILURE(rc))
892 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Temp dir invalid, rc=%Rrc", rc);
893 break;
894
895 case VKAT_TEST_OPT_VOL:
896 TstEnv.IoOpts.uVolumePercent = ValueUnion.u8;
897 break;
898
899 case VKAT_TEST_OPT_TCP_BIND_ADDRESS:
900 rc = RTStrCopy(TstEnv.TcpOpts.szBindAddr, sizeof(TstEnv.TcpOpts.szBindAddr), ValueUnion.psz);
901 if (RT_FAILURE(rc))
902 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Bind address invalid, rc=%Rrc", rc);
903 break;
904
905 case VKAT_TEST_OPT_TCP_BIND_PORT:
906 TstEnv.TcpOpts.uBindPort = ValueUnion.u16;
907 break;
908
909 case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS:
910 rc = RTStrCopy(TstEnv.TcpOpts.szConnectAddr, sizeof(TstEnv.TcpOpts.szConnectAddr), ValueUnion.psz);
911 if (RT_FAILURE(rc))
912 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Connect address invalid, rc=%Rrc", rc);
913 break;
914
915 case VKAT_TEST_OPT_TCP_CONNECT_PORT:
916 TstEnv.TcpOpts.uConnectPort = ValueUnion.u16;
917 break;
918
919 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
920
921 default:
922 return RTGetOptPrintError(ch, &ValueUnion);
923 }
924 }
925
926 /*
927 * Start testing.
928 */
929 RTTestBanner(g_hTest);
930
931 if (TstEnv.enmMode == AUDIOTESTMODE_UNKNOWN)
932 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No test mode (--mode) specified!\n");
933
934 /* Validate TCP options. */
935 if ( TstEnv.TcpOpts.szBindAddr[0]
936 && TstEnv.TcpOpts.szConnectAddr[0])
937 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Only one TCP connection mode (connect as client *or* bind as server) can be specified) at a time!\n");
938
939 /* Set new (override standard) I/O PCM properties if set by the user. */
940 if ( cPcmSampleBit
941 || cPcmChannels
942 || uPcmHz)
943 {
944 PDMAudioPropsInit(&TstEnv.IoOpts.Props,
945 cPcmSampleBit ? cPcmSampleBit / 2 : 2 /* 16-bit */, fPcmSigned /* fSigned */,
946 cPcmChannels ? cPcmChannels : 2 /* Stereo */, uPcmHz ? uPcmHz : 44100);
947 }
948
949 /* Do this first before everything else below. */
950 rc = AudioTestDriverStackPerformSelftest();
951 if (RT_FAILURE(rc))
952 {
953 if (!fNoAudioOk)
954 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testing driver stack failed: %Rrc\n", rc);
955 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Warning: Testing driver stack not possible (%Rrc), but --no-audio-ok was specified. Running on a server without audio hardware?\n");
956 }
957
958 AUDIOTESTDRVSTACK DrvStack;
959 if (fProbeBackends)
960 rc = audioTestDriverStackProbe(&DrvStack, pDrvReg,
961 true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */
962 else
963 rc = audioTestDriverStackInitEx(&DrvStack, pDrvReg,
964 true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */
965 if (RT_FAILURE(rc))
966 {
967 if (!fNoAudioOk)
968 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to init driver stack: %Rrc\n", rc);
969 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Warning: Initializing driver stack not possible (%Rrc), but --no-audio-ok was specified. Running on a server without audio hardware?\n");
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 case 'v':
1407 g_uVerbosity++;
1408 break;
1409
1410 default:
1411 break;
1412 }
1413 }
1414
1415 audioTestShowLogo(g_pStdOut);
1416
1417 if (fDaemonize)
1418 {
1419 if (!fDaemonized)
1420 {
1421 rc = RTProcDaemonize(argv, "--daemonized");
1422 if (RT_FAILURE(rc))
1423 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize() failed with %Rrc\n", rc);
1424
1425 RTMsgInfo("Starting in background (daemonizing) ...");
1426 return RTEXITCODE_SUCCESS;
1427 }
1428 /* else continue running in background. */
1429 }
1430
1431 /*
1432 * Init test and globals.
1433 * Note: Needs to be done *after* daemonizing, otherwise the child will fail!
1434 */
1435 rc = RTTestCreate("AudioTest", &g_hTest);
1436 if (RT_FAILURE(rc))
1437 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTTestCreate() failed with %Rrc\n", rc);
1438
1439#ifdef RT_OS_WINDOWS
1440 HRESULT hrc = CoInitializeEx(NULL /*pReserved*/, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE);
1441 if (FAILED(hrc))
1442 RTMsgWarning("CoInitializeEx failed: %#x", hrc);
1443#endif
1444
1445 /*
1446 * Configure release logging to go to stderr.
1447 */
1448 RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
1449#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1450 fFlags |= RTLOGFLAGS_USECRLF;
1451#endif
1452 static const char * const g_apszLogGroups[] = VBOX_LOGGROUP_NAMES;
1453 rc = RTLogCreate(&g_pRelLogger, fFlags, "all.e.l", "VKAT_RELEASE_LOG",
1454 RT_ELEMENTS(g_apszLogGroups), g_apszLogGroups, RTLOGDEST_STDOUT, NULL /*"vkat-release.log"*/);
1455 if (RT_SUCCESS(rc))
1456 {
1457 RTLogRelSetDefaultInstance(g_pRelLogger);
1458 if (g_uVerbosity)
1459 {
1460 RTMsgInfo("Setting verbosity logging to level %u\n", g_uVerbosity);
1461 switch (g_uVerbosity) /* Not very elegant, but has to do it for now. */
1462 {
1463 case 1:
1464 rc = RTLogGroupSettings(g_pRelLogger,
1465 "drv_audio.e.l+audio_mixer.e.l+audio_test.e.l");
1466 break;
1467
1468 case 2:
1469 rc = RTLogGroupSettings(g_pRelLogger,
1470 "drv_audio.e.l.l2+audio_mixer.e.l.l2+audio_test.e.l.l2");
1471 break;
1472
1473 case 3:
1474 rc = RTLogGroupSettings(g_pRelLogger,
1475 "drv_audio.e.l.l2.l3+audio_mixer.e.l.l2.l3+audio_test.e.l.l2.l3");
1476 break;
1477
1478 case 4:
1479 RT_FALL_THROUGH();
1480 default:
1481 rc = RTLogGroupSettings(g_pRelLogger,
1482 "drv_audio.e.l.l2.l3.f+audio_mixer.e.l.l2.l3.f+audio_test.e.l.l2.l3.f");
1483 break;
1484 }
1485 if (RT_FAILURE(rc))
1486 RTMsgError("Setting debug logging failed, rc=%Rrc\n", rc);
1487 }
1488 }
1489 else
1490 RTMsgWarning("Failed to create release logger: %Rrc", rc);
1491
1492 /*
1493 * Install a Ctrl-C signal handler.
1494 */
1495#ifdef RT_OS_WINDOWS
1496 signal(SIGINT, audioTestSignalHandler);
1497#else
1498 struct sigaction sa;
1499 RT_ZERO(sa);
1500 sa.sa_handler = audioTestSignalHandler;
1501 sigaction(SIGINT, &sa, NULL);
1502#endif
1503
1504 /*
1505 * Process common options.
1506 */
1507 RT_ZERO(GetState);
1508 rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions,
1509 RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */);
1510 AssertRCReturn(rc, RTEXITCODE_INIT);
1511
1512 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
1513 {
1514 switch (ch)
1515 {
1516 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
1517
1518 case VINF_GETOPT_NOT_OPTION:
1519 {
1520 for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++)
1521 {
1522 PCVKATCMD const pCmd = g_apCommands[iCmd];
1523 if (strcmp(ValueUnion.psz, pCmd->pszCommand) == 0)
1524 {
1525 size_t cCombinedOptions = pCmd->cOptions + RT_ELEMENTS(g_aCmdCommonOptions);
1526 if (pCmd->fNeedsTransport)
1527 {
1528 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1529 cCombinedOptions += g_apTransports[iTx]->cOpts;
1530 }
1531 PRTGETOPTDEF paCombinedOptions = (PRTGETOPTDEF)RTMemAlloc(cCombinedOptions * sizeof(RTGETOPTDEF));
1532 if (paCombinedOptions)
1533 {
1534 uint32_t idxOpts = 0;
1535 memcpy(paCombinedOptions, g_aCmdCommonOptions, sizeof(g_aCmdCommonOptions));
1536 idxOpts += RT_ELEMENTS(g_aCmdCommonOptions);
1537 memcpy(&paCombinedOptions[idxOpts], pCmd->paOptions, pCmd->cOptions * sizeof(RTGETOPTDEF));
1538 idxOpts += (uint32_t)pCmd->cOptions;
1539 if (pCmd->fNeedsTransport)
1540 {
1541 for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++)
1542 {
1543 memcpy(&paCombinedOptions[idxOpts],
1544 g_apTransports[iTx]->paOpts, g_apTransports[iTx]->cOpts * sizeof(RTGETOPTDEF));
1545 idxOpts += (uint32_t)g_apTransports[iTx]->cOpts;
1546 }
1547 }
1548
1549 rc = RTGetOptInit(&GetState, argc, argv, paCombinedOptions, cCombinedOptions,
1550 GetState.iNext /*idxFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1551 if (RT_SUCCESS(rc))
1552 {
1553 RTEXITCODE rcExit = pCmd->pfnHandler(&GetState);
1554 RTMemFree(paCombinedOptions);
1555 return rcExit;
1556 }
1557 return RTMsgErrorExitFailure("RTGetOptInit failed for '%s': %Rrc", ValueUnion.psz, rc);
1558 }
1559 return RTMsgErrorExitFailure("Out of memory!");
1560 }
1561 }
1562 RTMsgError("Unknown command '%s'!\n", ValueUnion.psz);
1563 audioTestUsage(g_pStdErr);
1564 return RTEXITCODE_SYNTAX;
1565 }
1566
1567 default:
1568 return RTGetOptPrintError(ch, &ValueUnion);
1569 }
1570 }
1571
1572 RTMsgError("No command specified!\n");
1573 audioTestUsage(g_pStdErr);
1574 return RTEXITCODE_SYNTAX;
1575}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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