VirtualBox

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

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

Audio/Validation Kit: Perform a quick audio driver stack self-test on start of the actual testing (guest + host), plus in self-test mode [SCM fix].​ bugref:10008

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