VirtualBox

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

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

Audio/VKAT: A bit more error logging; useful for the test boxes. ​bugref:10008

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