VirtualBox

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

Last change on this file since 91413 was 91413, checked in by vboxsync, 4 years ago

Audio/Validation Kit: Added "--tone-duration <ms>" option to specify test tone fixed durations being recorded / played back. ​bugref:10008

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

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