VirtualBox

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

最後變更 在這個檔案從96407是 96407,由 vboxsync 提交於 3 年 前

scm copyright and license note update

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

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