VirtualBox

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

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

Audio/ValKit: More work on ATS client destruction handling. bugref:10008

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