VirtualBox

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

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

Audio/VKAT: Wait a bit to let guest and the audio stack process remaining data. ​bugref:10008

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

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