VirtualBox

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

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

Audio/ValKit: Show VKAT logo before daemonizing. ​bugref:10008

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