VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp

Last change on this file was 106061, checked in by vboxsync, 4 days ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.7 KB
Line 
1/* $Id: vkatCmdSelfTest.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * Validation Kit Audio Test (VKAT) - Self test.
4 *
5 * Self-test which does a complete audio testing framework run without the need
6 * of a VM or other infrastructure, i.e. all required parts are running locally
7 * on the same machine.
8 *
9 * This self-test does the following:
10 * - 1. Creates a separate thread for the guest side VKAT and connects to the ATS instance on
11 * the host side at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST).
12 * - 2. Uses the Validation Kit audio backend, which in turn creates an ATS instance
13 * listening at port 6062 (ATS_TCP_DEF_BIND_PORT_VALKIT).
14 * - 3. Uses the host test environment which creates an ATS instance
15 * listening at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST).
16 * - 4. Executes a complete test run locally (e.g. without any guest (VM) involved).
17 */
18
19/*
20 * Copyright (C) 2021-2024 Oracle and/or its affiliates.
21 *
22 * This file is part of VirtualBox base platform packages, as
23 * available from https://www.virtualbox.org.
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation, in version 3 of the
28 * License.
29 *
30 * This program is distributed in the hope that it will be useful, but
31 * WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, see <https://www.gnu.org/licenses>.
37 *
38 * The contents of this file may alternatively be used under the terms
39 * of the Common Development and Distribution License Version 1.0
40 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
41 * in the VirtualBox distribution, in which case the provisions of the
42 * CDDL are applicable instead of those of the GPL.
43 *
44 * You may elect to license modified versions of this file under the
45 * terms and conditions of either the GPL or the CDDL or both.
46 *
47 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
48 */
49
50
51/*********************************************************************************************************************************
52* Header Files *
53*********************************************************************************************************************************/
54
55#include <iprt/ctype.h>
56#include <iprt/errcore.h>
57#include <iprt/getopt.h>
58#include <iprt/message.h>
59#include <iprt/rand.h>
60#include <iprt/test.h>
61
62#include "Audio/AudioHlp.h"
63#include "Audio/AudioTest.h"
64#include "Audio/AudioTestService.h"
65#include "Audio/AudioTestServiceClient.h"
66
67#include "vkatInternal.h"
68
69
70/*********************************************************************************************************************************
71* Internal structures *
72*********************************************************************************************************************************/
73
74/**
75 * Structure for keeping a VKAT self test context.
76 */
77typedef struct SELFTESTCTX
78{
79 /** Common tag for guest and host side. */
80 char szTag[AUDIOTEST_TAG_MAX];
81 /** The driver stack in use. */
82 AUDIOTESTDRVSTACK DrvStack;
83 /** Audio driver to use.
84 * Defaults to the platform's default driver. */
85 PCPDMDRVREG pDrvReg;
86 struct
87 {
88 AUDIOTESTENV TstEnv;
89 /** Where to bind the address of the guest ATS instance to.
90 * Defaults to localhost (127.0.0.1) if empty. */
91 char szAtsAddr[64];
92 /** Port of the guest ATS instance.
93 * Defaults to ATS_ALT_PORT if not set. */
94 uint32_t uAtsPort;
95 } Guest;
96 struct
97 {
98 AUDIOTESTENV TstEnv;
99 /** Address of the guest ATS instance.
100 * Defaults to localhost (127.0.0.1) if not set. */
101 char szGuestAtsAddr[64];
102 /** Port of the guest ATS instance.
103 * Defaults to ATS_DEFAULT_PORT if not set. */
104 uint32_t uGuestAtsPort;
105 /** Address of the Validation Kit audio driver ATS instance.
106 * Defaults to localhost (127.0.0.1) if not set. */
107 char szValKitAtsAddr[64];
108 /** Port of the Validation Kit audio driver ATS instance.
109 * Defaults to ATS_ALT_PORT if not set. */
110 uint32_t uValKitAtsPort;
111 } Host;
112} SELFTESTCTX;
113/** Pointer to a VKAT self test context. */
114typedef SELFTESTCTX *PSELFTESTCTX;
115
116
117/*********************************************************************************************************************************
118* Global Variables *
119*********************************************************************************************************************************/
120
121/** The global self-text context. */
122static SELFTESTCTX g_Ctx;
123
124
125/*********************************************************************************************************************************
126* Driver stack self-test implementation *
127*********************************************************************************************************************************/
128
129/**
130 * Performs a (quick) audio driver stack self test.
131 *
132 * Local only, no guest/host communication involved.
133 *
134 * @returns VBox status code.
135 */
136int AudioTestDriverStackPerformSelftest(void)
137{
138 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack started\n");
139
140 AUDIOTESTDRVSTACK DrvStack;
141 int rc = audioTestDriverStackProbe(&DrvStack,
142 true /* fEnabledIn */, true /* fEnabledOut */, false /* fWithDrvAudio */);
143 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
144
145 AUDIOTESTIOOPTS IoOpts;
146 audioTestIoOptsInitDefaults(&IoOpts);
147
148 PPDMAUDIOSTREAM pStream;
149 PDMAUDIOSTREAMCFG CfgAcq;
150 rc = audioTestDriverStackStreamCreateOutput(&DrvStack, &IoOpts.Props,
151 IoOpts.cMsBufferSize, IoOpts.cMsPreBuffer, IoOpts.cMsSchedulingHint,
152 &pStream, &CfgAcq);
153 AssertRCReturn(rc, rc);
154
155 rc = audioTestDriverStackStreamEnable(&DrvStack, pStream);
156 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
157
158 RTTEST_CHECK_RET(g_hTest, audioTestDriverStackStreamIsOkay(&DrvStack, pStream), VERR_AUDIO_STREAM_NOT_READY);
159
160 uint8_t abBuf[_4K];
161 memset(abBuf, 0x42, sizeof(abBuf));
162
163 uint32_t cbWritten;
164 rc = audioTestDriverStackStreamPlay(&DrvStack, pStream, abBuf, sizeof(abBuf), &cbWritten);
165 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
166 RTTEST_CHECK_RET(g_hTest, cbWritten == sizeof(abBuf), VERR_AUDIO_STREAM_NOT_READY);
167
168 audioTestDriverStackStreamDrain(&DrvStack, pStream, true /* fSync */);
169 audioTestDriverStackStreamDestroy(&DrvStack, pStream);
170
171 audioTestDriverStackDelete(&DrvStack);
172
173 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack ended with %Rrc\n", rc);
174 return rc;
175}
176
177
178/*********************************************************************************************************************************
179* Self-test implementation *
180*********************************************************************************************************************************/
181
182/**
183 * Thread callback for mocking the guest (VM) side of things.
184 *
185 * @returns VBox status code.
186 * @param hThread Thread handle.
187 * @param pvUser Pointer to user-supplied data.
188 */
189static DECLCALLBACK(int) audioTestSelftestGuestAtsThread(RTTHREAD hThread, void *pvUser)
190{
191 RT_NOREF(hThread);
192 PSELFTESTCTX pCtx = (PSELFTESTCTX)pvUser;
193
194 PAUDIOTESTENV pTstEnvGst = &pCtx->Guest.TstEnv;
195
196 audioTestEnvInit(pTstEnvGst);
197
198 /* Flag the environment for self test mode. */
199 pTstEnvGst->fSelftest = true;
200
201 /* Tweak the address the guest ATS is trying to connect to the host if anything else is specified.
202 * Note: The host also runs on the same host (this self-test is completely self-contained and does not need a VM). */
203 if (!pTstEnvGst->TcpOpts.szConnectAddr[0])
204 RTStrCopy(pTstEnvGst->TcpOpts.szConnectAddr, sizeof(pTstEnvGst->TcpOpts.szConnectAddr), "127.0.0.1");
205
206 /* Generate tag for guest side. */
207 int rc = RTStrCopy(pTstEnvGst->szTag, sizeof(pTstEnvGst->szTag), pCtx->szTag);
208 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
209
210 rc = AudioTestPathCreateTemp(pTstEnvGst->szPathTemp, sizeof(pTstEnvGst->szPathTemp), "selftest-guest");
211 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
212
213 rc = AudioTestPathCreateTemp(pTstEnvGst->szPathOut, sizeof(pTstEnvGst->szPathOut), "selftest-out");
214 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
215
216 pTstEnvGst->enmMode = AUDIOTESTMODE_GUEST;
217
218 rc = audioTestEnvCreate(pTstEnvGst, &pCtx->DrvStack);
219 if (RT_SUCCESS(rc))
220 {
221 RTThreadUserSignal(hThread);
222
223 rc = audioTestWorker(pTstEnvGst);
224 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
225
226 audioTestEnvDestroy(pTstEnvGst);
227 }
228
229 return rc;
230}
231
232/**
233 * Main function for performing the self test.
234 *
235 * @returns RTEXITCODE
236 * @param pCtx Self test context to use.
237 */
238static RTEXITCODE audioTestDoSelftest(PSELFTESTCTX pCtx)
239{
240 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Running self test ...\n");
241
242 /* Generate a common tag for guest and host side. */
243 int rc = AudioTestGenTag(pCtx->szTag, sizeof(pCtx->szTag));
244 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
245
246 PAUDIOTESTENV pTstEnvHst = &pCtx->Host.TstEnv;
247
248 audioTestEnvInit(pTstEnvHst);
249
250 /* Flag the environment for self test mode. */
251 pTstEnvHst->fSelftest = true;
252
253 /* One test iteration with a 5s maximum test tone is enough for a (quick) self test. */
254 pTstEnvHst->cIterations = 1;
255 pTstEnvHst->ToneParms.msDuration = RTRandU32Ex(500, RT_MS_5SEC);
256
257 /* Generate tag for host side. */
258 rc = RTStrCopy(pTstEnvHst->szTag, sizeof(pTstEnvHst->szTag), pCtx->szTag);
259 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
260
261 rc = AudioTestPathCreateTemp(pTstEnvHst->szPathTemp, sizeof(pTstEnvHst->szPathTemp), "selftest-tmp");
262 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
263
264 rc = AudioTestPathCreateTemp(pTstEnvHst->szPathOut, sizeof(pTstEnvHst->szPathOut), "selftest-out");
265 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
266
267 /*
268 * Step 1.
269 */
270 RTTHREAD hThreadGstAts = NIL_RTTHREAD;
271
272 bool const fStartGuestAts = RTStrNLen(pCtx->Host.szGuestAtsAddr, sizeof(pCtx->Host.szGuestAtsAddr)) == 0;
273 if (fStartGuestAts)
274 {
275 /* Step 1b. */
276 rc = RTThreadCreate(&hThreadGstAts, audioTestSelftestGuestAtsThread, pCtx, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
277 "VKATGstAts");
278 if (RT_SUCCESS(rc))
279 rc = RTThreadUserWait(hThreadGstAts, RT_MS_30SEC);
280 }
281
282 RTThreadSleep(2000); /* Fudge: Wait until guest ATS is up. 2 seconds should be enough (tm). */
283
284 if (RT_SUCCESS(rc))
285 {
286 /*
287 * Steps 2 + 3.
288 */
289 pTstEnvHst->enmMode = AUDIOTESTMODE_HOST;
290
291 rc = audioTestEnvCreate(pTstEnvHst, &pCtx->DrvStack);
292 if (RT_SUCCESS(rc))
293 {
294 /*
295 * Step 4.
296 */
297 rc = audioTestWorker(pTstEnvHst);
298 RTTEST_CHECK_RC_OK(g_hTest, rc);
299
300 audioTestEnvDestroy(pTstEnvHst);
301 }
302 }
303
304 /*
305 * Shutting down.
306 */
307 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down self test\n");
308
309 /* If we started the guest ATS ourselves, wait for it to terminate properly. */
310 if (fStartGuestAts)
311 {
312 int rcThread;
313 int rc2 = RTThreadWait(hThreadGstAts, RT_MS_30SEC, &rcThread);
314 if (RT_SUCCESS(rc2))
315 rc2 = rcThread;
316 if (RT_FAILURE(rc2))
317 RTTestFailed(g_hTest, "Shutting down guest ATS failed with %Rrc\n", rc2);
318 if (RT_SUCCESS(rc))
319 rc = rc2;
320 }
321
322 if (RT_FAILURE(rc))
323 RTTestFailed(g_hTest, "Self test failed with %Rrc\n", rc);
324
325 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
326}
327
328
329/*********************************************************************************************************************************
330* Command: selftest *
331*********************************************************************************************************************************/
332
333/**
334 * Command line parameters for self-test mode.
335 */
336static const RTGETOPTDEF s_aCmdSelftestOptions[] =
337{
338 { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING },
339 { "--backend", 'b', RTGETOPT_REQ_STRING },
340 { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING },
341 { "--with-mixer", 'm', RTGETOPT_REQ_NOTHING },
342 { "--exclude", 'e', RTGETOPT_REQ_UINT32 },
343 { "--include", 'i', RTGETOPT_REQ_UINT32 }
344};
345
346/** the 'selftest' command option help. */
347static DECLCALLBACK(const char *) audioTestCmdSelftestHelp(PCRTGETOPTDEF pOpt)
348{
349 switch (pOpt->iShort)
350 {
351 case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)";
352 case 'b': return "The audio backend to use";
353 case 'd': return "Go via DrvAudio instead of directly interfacing with the backend";
354 case 'e': return "Exclude the given test id from the list";
355 case 'i': return "Include the given test id in the list";
356 case 'm': return "Use the internal mixing engine explicitly";
357 default: return NULL;
358 }
359}
360
361/**
362 * The 'selftest' command handler.
363 *
364 * @returns Program exit code.
365 * @param pGetState RTGetOpt state.
366 */
367static DECLCALLBACK(RTEXITCODE) audioTestCmdSelftestHandler(PRTGETOPTSTATE pGetState)
368{
369 RT_ZERO(g_Ctx);
370
371 audioTestEnvInit(&g_Ctx.Guest.TstEnv);
372 audioTestEnvInit(&g_Ctx.Host.TstEnv);
373
374 AUDIOTESTIOOPTS IoOpts;
375 audioTestIoOptsInitDefaults(&IoOpts);
376
377 /* Argument processing loop: */
378 int rc;
379 RTGETOPTUNION ValueUnion;
380 while ((rc = RTGetOpt(pGetState, &ValueUnion)) != 0)
381 {
382 switch (rc)
383 {
384 case 'a':
385 for (unsigned i = 0; i < g_cTests; i++)
386 g_aTests[i].fExcluded = true;
387 break;
388
389 case 'b':
390 g_Ctx.pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
391 if (g_Ctx.pDrvReg == NULL)
392 return RTEXITCODE_SYNTAX;
393 break;
394
395 case 'd':
396 IoOpts.fWithDrvAudio = true;
397 break;
398
399 case 'e':
400 if (ValueUnion.u32 >= g_cTests)
401 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
402 g_aTests[ValueUnion.u32].fExcluded = true;
403 break;
404
405 case 'i':
406 if (ValueUnion.u32 >= g_cTests)
407 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
408 g_aTests[ValueUnion.u32].fExcluded = false;
409 break;
410
411 case 'm':
412 IoOpts.fWithMixer = true;
413 break;
414
415 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdSelfTest);
416
417 default:
418 return RTGetOptPrintError(rc, &ValueUnion);
419 }
420 }
421
422 /* For simplicity both test environments, guest and host, will have the same I/O options.
423 ** @todo Make this indepedent by a prefix, "--[guest|host]-<option>" -> e.g. "--guest-with-drv-audio". */
424 memcpy(&g_Ctx.Guest.TstEnv.IoOpts, &IoOpts, sizeof(AUDIOTESTIOOPTS));
425 memcpy(&g_Ctx.Host.TstEnv.IoOpts, &IoOpts, sizeof(AUDIOTESTIOOPTS));
426
427 rc = AudioTestDriverStackPerformSelftest();
428 if (RT_FAILURE(rc))
429 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testing driver stack failed: %Rrc\n", rc);
430
431 /* Go with the Validation Kit audio backend if nothing else is specified. */
432 if (g_Ctx.pDrvReg == NULL)
433 g_Ctx.pDrvReg = AudioTestFindBackendOpt("valkit");
434
435 /*
436 * In self-test mode the guest and the host side have to share the same driver stack,
437 * as we don't have any device emulation between the two sides.
438 *
439 * This is necessary to actually get the played/recorded audio to from/to the guest
440 * and host respectively.
441 *
442 * Choosing any other backend than the Validation Kit above *will* break this self-test!
443 */
444 rc = audioTestDriverStackInitEx(&g_Ctx.DrvStack, g_Ctx.pDrvReg,
445 true /* fEnabledIn */, true /* fEnabledOut */, g_Ctx.Host.TstEnv.IoOpts.fWithDrvAudio);
446 if (RT_FAILURE(rc))
447 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to init driver stack: %Rrc\n", rc);
448
449 /*
450 * Start testing.
451 */
452 RTTestBanner(g_hTest);
453
454 int rc2 = audioTestDoSelftest(&g_Ctx);
455 if (RT_FAILURE(rc2))
456 RTTestFailed(g_hTest, "Self test failed with rc=%Rrc", rc2);
457
458 audioTestDriverStackDelete(&g_Ctx.DrvStack);
459
460 /*
461 * Print summary and exit.
462 */
463 return RTTestSummaryAndDestroy(g_hTest);
464}
465
466/**
467 * Command table entry for 'selftest'.
468 */
469const VKATCMD g_CmdSelfTest =
470{
471 "selftest",
472 audioTestCmdSelftestHandler,
473 "Performs self-tests.",
474 s_aCmdSelftestOptions,
475 RT_ELEMENTS(s_aCmdSelftestOptions),
476 audioTestCmdSelftestHelp,
477 true /* fNeedsTransport */
478};
479
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