1 | /* $Id: AudioTest.cpp 89389 2021-05-31 10:06:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Audio testing routines.
|
---|
4 | * Common code which is being used by the ValidationKit and the debug / ValdikationKit audio driver(s).
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2021 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /*********************************************************************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *********************************************************************************************************************************/
|
---|
23 |
|
---|
24 | #include <package-generated.h>
|
---|
25 | #include "product-generated.h"
|
---|
26 |
|
---|
27 | #include <iprt/buildconfig.h>
|
---|
28 | #include <iprt/dir.h>
|
---|
29 | #include <iprt/env.h>
|
---|
30 | #include <iprt/file.h>
|
---|
31 | #include <iprt/formats/riff.h>
|
---|
32 | #include <iprt/inifile.h>
|
---|
33 | #include <iprt/list.h>
|
---|
34 | #include <iprt/message.h> /** @todo Get rid of this once we have own log hooks. */
|
---|
35 | #include <iprt/rand.h>
|
---|
36 | #include <iprt/system.h>
|
---|
37 | #include <iprt/uuid.h>
|
---|
38 | #include <iprt/vfs.h>
|
---|
39 | #include <iprt/zip.h>
|
---|
40 |
|
---|
41 | #define _USE_MATH_DEFINES
|
---|
42 | #include <math.h> /* sin, M_PI */
|
---|
43 |
|
---|
44 | #include <VBox/version.h>
|
---|
45 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
46 | #include <VBox/vmm/pdmaudioinline.h>
|
---|
47 |
|
---|
48 | #include "AudioTest.h"
|
---|
49 |
|
---|
50 |
|
---|
51 | /*********************************************************************************************************************************
|
---|
52 | * Defines *
|
---|
53 | *********************************************************************************************************************************/
|
---|
54 | /** The test manifest file name. */
|
---|
55 | #define AUDIOTEST_MANIFEST_FILE_STR "vkat_manifest.ini"
|
---|
56 | /** The current test manifest version. */
|
---|
57 | #define AUDIOTEST_MANIFEST_VER 1
|
---|
58 | /** Audio test archive default suffix.
|
---|
59 | * According to IPRT terminology this always contains the dot. */
|
---|
60 | #define AUDIOTEST_ARCHIVE_SUFF_STR ".tar.gz"
|
---|
61 |
|
---|
62 | /** Test manifest header name. */
|
---|
63 | #define AUDIOTEST_INI_SEC_HDR_STR "header"
|
---|
64 |
|
---|
65 |
|
---|
66 | /*********************************************************************************************************************************
|
---|
67 | * Structures and Typedefs *
|
---|
68 | *********************************************************************************************************************************/
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Global Variables *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 | /** Well-known frequency selection test tones. */
|
---|
75 | static const double s_aAudioTestToneFreqsHz[] =
|
---|
76 | {
|
---|
77 | 349.2282 /*F4*/,
|
---|
78 | 440.0000 /*A4*/,
|
---|
79 | 523.2511 /*C5*/,
|
---|
80 | 698.4565 /*F5*/,
|
---|
81 | 880.0000 /*A5*/,
|
---|
82 | 1046.502 /*C6*/,
|
---|
83 | 1174.659 /*D6*/,
|
---|
84 | 1396.913 /*F6*/,
|
---|
85 | 1760.0000 /*A6*/
|
---|
86 | };
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns a random test tone frequency.
|
---|
90 | */
|
---|
91 | DECLINLINE(double) audioTestToneGetRandomFreq(void)
|
---|
92 | {
|
---|
93 | return s_aAudioTestToneFreqsHz[RTRandU32Ex(0, RT_ELEMENTS(s_aAudioTestToneFreqsHz) - 1)];
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Initializes a test tone with a specific frequency (in Hz).
|
---|
98 | *
|
---|
99 | * @returns Used tone frequency (in Hz).
|
---|
100 | * @param pTone Pointer to test tone to initialize.
|
---|
101 | * @param pProps PCM properties to use for the test tone.
|
---|
102 | * @param dbFreq Frequency (in Hz) to initialize tone with.
|
---|
103 | * When set to 0.0, a random frequency will be chosen.
|
---|
104 | */
|
---|
105 | double AudioTestToneInit(PAUDIOTESTTONE pTone, PPDMAUDIOPCMPROPS pProps, double dbFreq)
|
---|
106 | {
|
---|
107 | if (dbFreq == 0.0)
|
---|
108 | dbFreq = audioTestToneGetRandomFreq();
|
---|
109 |
|
---|
110 | pTone->rdFreqHz = dbFreq;
|
---|
111 | pTone->rdFixed = 2.0 * M_PI * pTone->rdFreqHz / PDMAudioPropsHz(pProps);
|
---|
112 | pTone->uSample = 0;
|
---|
113 |
|
---|
114 | memcpy(&pTone->Props, pProps, sizeof(PDMAUDIOPCMPROPS));
|
---|
115 |
|
---|
116 | pTone->enmType = AUDIOTESTTONETYPE_SINE; /* Only type implemented so far. */
|
---|
117 |
|
---|
118 | return dbFreq;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Initializes a test tone by picking a random but well-known frequency (in Hz).
|
---|
123 | *
|
---|
124 | * @returns Randomly picked tone frequency (in Hz).
|
---|
125 | * @param pTone Pointer to test tone to initialize.
|
---|
126 | * @param pProps PCM properties to use for the test tone.
|
---|
127 | */
|
---|
128 | double AudioTestToneInitRandom(PAUDIOTESTTONE pTone, PPDMAUDIOPCMPROPS pProps)
|
---|
129 | {
|
---|
130 | return AudioTestToneInit(pTone, pProps,
|
---|
131 | /* Pick a frequency from our selection, so that every time a recording starts
|
---|
132 | * we'll hopfully generate a different note. */
|
---|
133 | 0.0);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Writes (and iterates) a given test tone to an output buffer.
|
---|
138 | *
|
---|
139 | * @returns VBox status code.
|
---|
140 | * @param pTone Pointer to test tone to write.
|
---|
141 | * @param pvBuf Pointer to output buffer to write test tone to.
|
---|
142 | * @param cbBuf Size (in bytes) of output buffer.
|
---|
143 | * @param pcbWritten How many bytes were written on success.
|
---|
144 | */
|
---|
145 | int AudioTestToneGenerate(PAUDIOTESTTONE pTone, void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Clear the buffer first so we don't need to think about additional channels.
|
---|
149 | */
|
---|
150 | uint32_t cFrames = PDMAudioPropsBytesToFrames(&pTone->Props, cbBuf);
|
---|
151 |
|
---|
152 | /* Input cbBuf not necessarily is aligned to the frames, so re-calculate it. */
|
---|
153 | const uint32_t cbToWrite = PDMAudioPropsFramesToBytes(&pTone->Props, cFrames);
|
---|
154 |
|
---|
155 | PDMAudioPropsClearBuffer(&pTone->Props, pvBuf, cbBuf, cFrames);
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Generate the select sin wave in the first channel:
|
---|
159 | */
|
---|
160 | uint32_t const cbFrame = PDMAudioPropsFrameSize(&pTone->Props);
|
---|
161 | double const rdFixed = pTone->rdFixed;
|
---|
162 | uint64_t iSrcFrame = pTone->uSample;
|
---|
163 | switch (PDMAudioPropsSampleSize(&pTone->Props))
|
---|
164 | {
|
---|
165 | case 1:
|
---|
166 | /* untested */
|
---|
167 | if (PDMAudioPropsIsSigned(&pTone->Props))
|
---|
168 | {
|
---|
169 | int8_t *piSample = (int8_t *)pvBuf;
|
---|
170 | while (cFrames-- > 0)
|
---|
171 | {
|
---|
172 | *piSample = (int8_t)(126 /*Amplitude*/ * sin(rdFixed * iSrcFrame));
|
---|
173 | iSrcFrame++;
|
---|
174 | piSample += cbFrame;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | /* untested */
|
---|
180 | uint8_t *pbSample = (uint8_t *)pvBuf;
|
---|
181 | while (cFrames-- > 0)
|
---|
182 | {
|
---|
183 | *pbSample = (uint8_t)(126 /*Amplitude*/ * sin(rdFixed * iSrcFrame) + 0x80);
|
---|
184 | iSrcFrame++;
|
---|
185 | pbSample += cbFrame;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | break;
|
---|
189 |
|
---|
190 | case 2:
|
---|
191 | if (PDMAudioPropsIsSigned(&pTone->Props))
|
---|
192 | {
|
---|
193 | int16_t *piSample = (int16_t *)pvBuf;
|
---|
194 | while (cFrames-- > 0)
|
---|
195 | {
|
---|
196 | *piSample = (int16_t)(32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame));
|
---|
197 | iSrcFrame++;
|
---|
198 | piSample = (int16_t *)((uint8_t *)piSample + cbFrame);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | /* untested */
|
---|
204 | uint16_t *puSample = (uint16_t *)pvBuf;
|
---|
205 | while (cFrames-- > 0)
|
---|
206 | {
|
---|
207 | *puSample = (uint16_t)(32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame) + 0x8000);
|
---|
208 | iSrcFrame++;
|
---|
209 | puSample = (uint16_t *)((uint8_t *)puSample + cbFrame);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case 4:
|
---|
215 | /* untested */
|
---|
216 | if (PDMAudioPropsIsSigned(&pTone->Props))
|
---|
217 | {
|
---|
218 | int32_t *piSample = (int32_t *)pvBuf;
|
---|
219 | while (cFrames-- > 0)
|
---|
220 | {
|
---|
221 | *piSample = (int32_t)((32760 << 16) /*Amplitude*/ * sin(rdFixed * iSrcFrame));
|
---|
222 | iSrcFrame++;
|
---|
223 | piSample = (int32_t *)((uint8_t *)piSample + cbFrame);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | uint32_t *puSample = (uint32_t *)pvBuf;
|
---|
229 | while (cFrames-- > 0)
|
---|
230 | {
|
---|
231 | *puSample = (uint32_t)((32760 << 16) /*Amplitude*/ * sin(rdFixed * iSrcFrame) + UINT32_C(0x80000000));
|
---|
232 | iSrcFrame++;
|
---|
233 | puSample = (uint32_t *)((uint8_t *)puSample + cbFrame);
|
---|
234 | }
|
---|
235 | }
|
---|
236 | break;
|
---|
237 |
|
---|
238 | default:
|
---|
239 | AssertFailedReturn(VERR_NOT_SUPPORTED);
|
---|
240 | }
|
---|
241 |
|
---|
242 | pTone->uSample = iSrcFrame;
|
---|
243 |
|
---|
244 | if (pcbWritten)
|
---|
245 | *pcbWritten = cbToWrite;
|
---|
246 |
|
---|
247 | return VINF_SUCCESS;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Initializes an audio test tone parameters struct with random values.
|
---|
252 | * @param pToneParams Test tone parameters to initialize.
|
---|
253 | * @param pProps PCM properties to use for the test tone.
|
---|
254 | */
|
---|
255 | int AudioTestToneParamsInitRandom(PAUDIOTESTTONEPARMS pToneParams, PPDMAUDIOPCMPROPS pProps)
|
---|
256 | {
|
---|
257 | AssertReturn(PDMAudioPropsAreValid(pProps), VERR_INVALID_PARAMETER);
|
---|
258 |
|
---|
259 | memcpy(&pToneParams->Props, pProps, sizeof(PDMAUDIOPCMPROPS));
|
---|
260 |
|
---|
261 | /** @todo Make this a bit more sophisticated later, e.g. muting and prequel/sequel are not very balanced. */
|
---|
262 |
|
---|
263 | pToneParams->dbFreqHz = audioTestToneGetRandomFreq();
|
---|
264 | pToneParams->msPrequel = RTRandU32Ex(0, RT_MS_5SEC);
|
---|
265 | #ifdef DEBUG_andy
|
---|
266 | pToneParams->msDuration = RTRandU32Ex(0, RT_MS_1SEC);
|
---|
267 | #else
|
---|
268 | pToneParams->msDuration = RTRandU32Ex(0, RT_MS_10SEC); /** @todo Probably a bit too long, but let's see. */
|
---|
269 | #endif
|
---|
270 | pToneParams->msSequel = RTRandU32Ex(0, RT_MS_5SEC);
|
---|
271 | pToneParams->uVolumePercent = RTRandU32Ex(0, 100);
|
---|
272 |
|
---|
273 | return VINF_SUCCESS;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Generates a tag.
|
---|
278 | *
|
---|
279 | * @returns VBox status code.
|
---|
280 | * @param pszTag The output buffer.
|
---|
281 | * @param cbTag The size of the output buffer.
|
---|
282 | * AUDIOTEST_TAG_MAX is a good size.
|
---|
283 | */
|
---|
284 | int AudioTestGenTag(char *pszTag, size_t cbTag)
|
---|
285 | {
|
---|
286 | RTUUID UUID;
|
---|
287 | int rc = RTUuidCreate(&UUID);
|
---|
288 | AssertRCReturn(rc, rc);
|
---|
289 | rc = RTUuidToStr(&UUID, pszTag, cbTag);
|
---|
290 | AssertRCReturn(rc, rc);
|
---|
291 | return rc;
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Return the tag to use in the given buffer, generating one if needed.
|
---|
297 | *
|
---|
298 | * @returns VBox status code.
|
---|
299 | * @param pszTag The output buffer.
|
---|
300 | * @param cbTag The size of the output buffer.
|
---|
301 | * AUDIOTEST_TAG_MAX is a good size.
|
---|
302 | * @param pszTagUser User specified tag, optional.
|
---|
303 | */
|
---|
304 | int AudioTestCopyOrGenTag(char *pszTag, size_t cbTag, const char *pszTagUser)
|
---|
305 | {
|
---|
306 | if (pszTagUser && *pszTagUser)
|
---|
307 | return RTStrCopy(pszTag, cbTag, pszTagUser);
|
---|
308 | return AudioTestGenTag(pszTag, cbTag);
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Creates a new path (directory) for a specific audio test set tag.
|
---|
314 | *
|
---|
315 | * @returns VBox status code.
|
---|
316 | * @param pszPath On input, specifies the absolute base path where to create the test set path.
|
---|
317 | * On output this specifies the absolute path created.
|
---|
318 | * @param cbPath Size (in bytes) of \a pszPath.
|
---|
319 | * @param pszTag Tag to use for path creation.
|
---|
320 | *
|
---|
321 | * @note Can be used multiple times with the same tag; a sub directory with an ISO time string will be used
|
---|
322 | * on each call.
|
---|
323 | */
|
---|
324 | int AudioTestPathCreate(char *pszPath, size_t cbPath, const char *pszTag)
|
---|
325 | {
|
---|
326 | char szTag[AUDIOTEST_TAG_MAX];
|
---|
327 | int rc = AudioTestCopyOrGenTag(szTag, sizeof(szTag), pszTag);
|
---|
328 | AssertRCReturn(rc, rc);
|
---|
329 |
|
---|
330 | char szName[RT_ELEMENTS(AUDIOTEST_PATH_PREFIX_STR) + AUDIOTEST_TAG_MAX + 4];
|
---|
331 | if (RTStrPrintf2(szName, sizeof(szName), "%s-%s", AUDIOTEST_PATH_PREFIX_STR, szTag) < 0)
|
---|
332 | AssertFailedReturn(VERR_BUFFER_OVERFLOW);
|
---|
333 |
|
---|
334 | rc = RTPathAppend(pszPath, cbPath, szName);
|
---|
335 | AssertRCReturn(rc, rc);
|
---|
336 |
|
---|
337 | #ifndef DEBUG /* Makes debugging easier to have a deterministic directory. */
|
---|
338 | char szTime[64];
|
---|
339 | RTTIMESPEC time;
|
---|
340 | if (!RTTimeSpecToString(RTTimeNow(&time), szTime, sizeof(szTime)))
|
---|
341 | return VERR_BUFFER_UNDERFLOW;
|
---|
342 |
|
---|
343 | /* Colons aren't allowed in windows filenames, so change to dashes. */
|
---|
344 | char *pszColon;
|
---|
345 | while ((pszColon = strchr(szTime, ':')) != NULL)
|
---|
346 | *pszColon = '-';
|
---|
347 |
|
---|
348 | rc = RTPathAppend(pszPath, cbPath, szTime);
|
---|
349 | AssertRCReturn(rc, rc);
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | return RTDirCreateFullPath(pszPath, RTFS_UNIX_IRWXU);
|
---|
353 | }
|
---|
354 |
|
---|
355 | DECLINLINE(int) audioTestManifestWriteData(PAUDIOTESTSET pSet, const void *pvData, size_t cbData)
|
---|
356 | {
|
---|
357 | /** @todo Use RTIniFileWrite once its implemented. */
|
---|
358 | return RTFileWrite(pSet->f.hFile, pvData, cbData, NULL);
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Writes string data to a test set manifest.
|
---|
363 | *
|
---|
364 | * @returns VBox status code.
|
---|
365 | * @param pSet Test set to write manifest for.
|
---|
366 | * @param pszFormat Format string to write.
|
---|
367 | * @param args Variable arguments for \a pszFormat.
|
---|
368 | */
|
---|
369 | static int audioTestManifestWriteV(PAUDIOTESTSET pSet, const char *pszFormat, va_list args)
|
---|
370 | {
|
---|
371 | /** @todo r=bird: Use RTStrmOpen + RTStrmPrintf instead of this slow
|
---|
372 | * do-it-all-yourself stuff. */
|
---|
373 | char *psz = NULL;
|
---|
374 | if (RTStrAPrintfV(&psz, pszFormat, args) == -1)
|
---|
375 | return VERR_NO_MEMORY;
|
---|
376 | AssertPtrReturn(psz, VERR_NO_MEMORY);
|
---|
377 |
|
---|
378 | int rc = audioTestManifestWriteData(pSet, psz, strlen(psz));
|
---|
379 | AssertRC(rc);
|
---|
380 |
|
---|
381 | RTStrFree(psz);
|
---|
382 |
|
---|
383 | return rc;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Writes a string to a test set manifest.
|
---|
388 | * Convenience function.
|
---|
389 | *
|
---|
390 | * @returns VBox status code.
|
---|
391 | * @param pSet Test set to write manifest for.
|
---|
392 | * @param pszFormat Format string to write.
|
---|
393 | * @param ... Variable arguments for \a pszFormat. Optional.
|
---|
394 | */
|
---|
395 | static int audioTestManifestWrite(PAUDIOTESTSET pSet, const char *pszFormat, ...)
|
---|
396 | {
|
---|
397 | va_list va;
|
---|
398 | va_start(va, pszFormat);
|
---|
399 |
|
---|
400 | int rc = audioTestManifestWriteV(pSet, pszFormat, va);
|
---|
401 | AssertRC(rc);
|
---|
402 |
|
---|
403 | va_end(va);
|
---|
404 |
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Returns the current read/write offset (in bytes) of the opened manifest file.
|
---|
410 | *
|
---|
411 | * @returns Current read/write offset (in bytes).
|
---|
412 | * @param pSet Set to return offset for.
|
---|
413 | * Must have an opened manifest file.
|
---|
414 | */
|
---|
415 | DECLINLINE(uint64_t) audioTestManifestGetOffsetAbs(PAUDIOTESTSET pSet)
|
---|
416 | {
|
---|
417 | AssertReturn(RTFileIsValid(pSet->f.hFile), 0);
|
---|
418 | return RTFileTell(pSet->f.hFile);
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Writes a section header to a test set manifest.
|
---|
423 | *
|
---|
424 | * @returns VBox status code.
|
---|
425 | * @param pSet Test set to write manifest for.
|
---|
426 | * @param pszSection Format string of section to write.
|
---|
427 | * @param ... Variable arguments for \a pszSection. Optional.
|
---|
428 | */
|
---|
429 | static int audioTestManifestWriteSectionHdr(PAUDIOTESTSET pSet, const char *pszSection, ...)
|
---|
430 | {
|
---|
431 | va_list va;
|
---|
432 | va_start(va, pszSection);
|
---|
433 |
|
---|
434 | /** @todo Keep it as simple as possible for now. Improve this later. */
|
---|
435 | int rc = audioTestManifestWrite(pSet, "[%N]\n", pszSection, &va);
|
---|
436 |
|
---|
437 | va_end(va);
|
---|
438 |
|
---|
439 | return rc;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Initializes an audio test set, internal function.
|
---|
444 | *
|
---|
445 | * @param pSet Test set to initialize.
|
---|
446 | */
|
---|
447 | static void audioTestSetInitInternal(PAUDIOTESTSET pSet)
|
---|
448 | {
|
---|
449 | pSet->f.hFile = NIL_RTFILE;
|
---|
450 |
|
---|
451 | RTListInit(&pSet->lstObj);
|
---|
452 | pSet->cObj = 0;
|
---|
453 |
|
---|
454 | RTListInit(&pSet->lstTest);
|
---|
455 | pSet->cTests = 0;
|
---|
456 | pSet->cTestsRunning = 0;
|
---|
457 | pSet->offTestCount = 0;
|
---|
458 | pSet->pTestCur = NULL;
|
---|
459 | pSet->cObj = 0;
|
---|
460 | pSet->offObjCount = 0;
|
---|
461 | pSet->cTotalFailures = 0;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Returns whether a test set's manifest file is open (and thus ready) or not.
|
---|
466 | *
|
---|
467 | * @returns \c true if open (and ready), or \c false if not.
|
---|
468 | * @retval VERR_
|
---|
469 | * @param pSet Test set to return open status for.
|
---|
470 | */
|
---|
471 | static bool audioTestManifestIsOpen(PAUDIOTESTSET pSet)
|
---|
472 | {
|
---|
473 | if ( pSet->enmMode == AUDIOTESTSETMODE_TEST
|
---|
474 | && pSet->f.hFile != NIL_RTFILE)
|
---|
475 | return true;
|
---|
476 | else if ( pSet->enmMode == AUDIOTESTSETMODE_VERIFY
|
---|
477 | && pSet->f.hIniFile != NIL_RTINIFILE)
|
---|
478 | return true;
|
---|
479 |
|
---|
480 | return false;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Initializes an audio test error description.
|
---|
485 | *
|
---|
486 | * @param pErr Test error description to initialize.
|
---|
487 | */
|
---|
488 | static void audioTestErrorDescInit(PAUDIOTESTERRORDESC pErr)
|
---|
489 | {
|
---|
490 | RTListInit(&pErr->List);
|
---|
491 | pErr->cErrors = 0;
|
---|
492 | }
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Destroys an audio test error description.
|
---|
496 | *
|
---|
497 | * @param pErr Test error description to destroy.
|
---|
498 | */
|
---|
499 | void AudioTestErrorDescDestroy(PAUDIOTESTERRORDESC pErr)
|
---|
500 | {
|
---|
501 | if (!pErr)
|
---|
502 | return;
|
---|
503 |
|
---|
504 | PAUDIOTESTERRORENTRY pErrEntry, pErrEntryNext;
|
---|
505 | RTListForEachSafe(&pErr->List, pErrEntry, pErrEntryNext, AUDIOTESTERRORENTRY, Node)
|
---|
506 | {
|
---|
507 | RTListNodeRemove(&pErrEntry->Node);
|
---|
508 |
|
---|
509 | RTMemFree(pErrEntry);
|
---|
510 |
|
---|
511 | Assert(pErr->cErrors);
|
---|
512 | pErr->cErrors--;
|
---|
513 | }
|
---|
514 |
|
---|
515 | Assert(pErr->cErrors == 0);
|
---|
516 | }
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Returns if an audio test error description contains any errors or not.
|
---|
520 | *
|
---|
521 | * @returns \c true if it contains errors, or \c false if not.
|
---|
522 | *
|
---|
523 | * @param pErr Test error description to return error status for.
|
---|
524 | */
|
---|
525 | bool AudioTestErrorDescFailed(PAUDIOTESTERRORDESC pErr)
|
---|
526 | {
|
---|
527 | if (pErr->cErrors)
|
---|
528 | {
|
---|
529 | Assert(!RTListIsEmpty(&pErr->List));
|
---|
530 | return true;
|
---|
531 | }
|
---|
532 |
|
---|
533 | return false;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Adds a single error entry to an audio test error description, va_list version.
|
---|
538 | *
|
---|
539 | * @returns VBox status code.
|
---|
540 | * @param pErr Test error description to add entry for.
|
---|
541 | * @param rc Result code of entry to add.
|
---|
542 | * @param pszDesc Error description format string to add.
|
---|
543 | * @param args Optional format arguments of \a pszDesc to add.
|
---|
544 | */
|
---|
545 | static int audioTestErrorDescAddV(PAUDIOTESTERRORDESC pErr, int rc, const char *pszDesc, va_list args)
|
---|
546 | {
|
---|
547 | PAUDIOTESTERRORENTRY pEntry = (PAUDIOTESTERRORENTRY)RTMemAlloc(sizeof(AUDIOTESTERRORENTRY));
|
---|
548 | AssertReturn(pEntry, VERR_NO_MEMORY);
|
---|
549 |
|
---|
550 | if (RTStrPrintf2V(pEntry->szDesc, sizeof(pEntry->szDesc), pszDesc, args) < 0)
|
---|
551 | AssertFailedReturn(VERR_BUFFER_OVERFLOW);
|
---|
552 |
|
---|
553 | pEntry->rc = rc;
|
---|
554 |
|
---|
555 | RTListAppend(&pErr->List, &pEntry->Node);
|
---|
556 |
|
---|
557 | pErr->cErrors++;
|
---|
558 |
|
---|
559 | return VINF_SUCCESS;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Adds a single error entry to an audio test error description, va_list version.
|
---|
564 | *
|
---|
565 | * @returns VBox status code.
|
---|
566 | * @param pErr Test error description to add entry for.
|
---|
567 | * @param pszDesc Error description format string to add.
|
---|
568 | * @param ... Optional format arguments of \a pszDesc to add.
|
---|
569 | */
|
---|
570 | static int audioTestErrorDescAdd(PAUDIOTESTERRORDESC pErr, const char *pszDesc, ...)
|
---|
571 | {
|
---|
572 | va_list va;
|
---|
573 | va_start(va, pszDesc);
|
---|
574 |
|
---|
575 | int rc = audioTestErrorDescAddV(pErr, VERR_GENERAL_FAILURE /** @todo Fudge! */, pszDesc, va);
|
---|
576 |
|
---|
577 | va_end(va);
|
---|
578 | return rc;
|
---|
579 | }
|
---|
580 |
|
---|
581 | #if 0
|
---|
582 | static int audioTestErrorDescAddRc(PAUDIOTESTERRORDESC pErr, int rc, const char *pszFormat, ...)
|
---|
583 | {
|
---|
584 | va_list va;
|
---|
585 | va_start(va, pszFormat);
|
---|
586 |
|
---|
587 | int rc2 = audioTestErrorDescAddV(pErr, rc, pszFormat, va);
|
---|
588 |
|
---|
589 | va_end(va);
|
---|
590 | return rc2;
|
---|
591 | }
|
---|
592 | #endif
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Creates a new temporary directory with a specific (test) tag.
|
---|
596 | *
|
---|
597 | * @returns VBox status code.
|
---|
598 | * @param pszPath Where to return the absolute path of the created directory on success.
|
---|
599 | * @param cbPath Size (in bytes) of \a pszPath.
|
---|
600 | * @param pszTag Tag name to use for directory creation.
|
---|
601 | *
|
---|
602 | * @note Can be used multiple times with the same tag; a sub directory with an ISO time string will be used
|
---|
603 | * on each call.
|
---|
604 | */
|
---|
605 | int AudioTestPathCreateTemp(char *pszPath, size_t cbPath, const char *pszTag)
|
---|
606 | {
|
---|
607 | AssertReturn(pszTag && strlen(pszTag) <= AUDIOTEST_TAG_MAX, VERR_INVALID_PARAMETER);
|
---|
608 |
|
---|
609 | char szTemp[RTPATH_MAX];
|
---|
610 | int rc = RTEnvGetEx(RTENV_DEFAULT, "TESTBOX_PATH_SCRATCH", szTemp, sizeof(szTemp), NULL);
|
---|
611 | if (RT_FAILURE(rc))
|
---|
612 | {
|
---|
613 | rc = RTPathTemp(szTemp, sizeof(szTemp));
|
---|
614 | AssertRCReturn(rc, rc);
|
---|
615 | }
|
---|
616 |
|
---|
617 | rc = AudioTestPathCreate(szTemp, sizeof(szTemp), pszTag);
|
---|
618 | AssertRCReturn(rc, rc);
|
---|
619 |
|
---|
620 | return RTStrCopy(pszPath, cbPath, szTemp);
|
---|
621 | }
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Returns the absolute path of a given audio test set object.
|
---|
625 | *
|
---|
626 | * @returns VBox status code.
|
---|
627 | * @param pSet Test set the object contains.
|
---|
628 | * @param pszPathAbs Where to return the absolute path on success.
|
---|
629 | * @param cbPathAbs Size (in bytes) of \a pszPathAbs.
|
---|
630 | * @param pszObjName Name of the object to create absolute path for.
|
---|
631 | */
|
---|
632 | DECLINLINE(int) audioTestSetGetObjPath(PAUDIOTESTSET pSet, char *pszPathAbs, size_t cbPathAbs, const char *pszObjName)
|
---|
633 | {
|
---|
634 | return RTPathJoin(pszPathAbs, cbPathAbs, pSet->szPathAbs, pszObjName);
|
---|
635 | }
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Creates a new audio test set.
|
---|
639 | *
|
---|
640 | * @returns VBox status code.
|
---|
641 | * @param pSet Test set to create.
|
---|
642 | * @param pszPath Where to store the set set data. If NULL, the
|
---|
643 | * temporary directory will be used.
|
---|
644 | * @param pszTag Tag name to use for this test set.
|
---|
645 | */
|
---|
646 | int AudioTestSetCreate(PAUDIOTESTSET pSet, const char *pszPath, const char *pszTag)
|
---|
647 | {
|
---|
648 | audioTestSetInitInternal(pSet);
|
---|
649 |
|
---|
650 | int rc = AudioTestCopyOrGenTag(pSet->szTag, sizeof(pSet->szTag), pszTag);
|
---|
651 | AssertRCReturn(rc, rc);
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Test set directory.
|
---|
655 | */
|
---|
656 | if (pszPath)
|
---|
657 | {
|
---|
658 | rc = RTPathAbs(pszPath, pSet->szPathAbs, sizeof(pSet->szPathAbs));
|
---|
659 | AssertRCReturn(rc, rc);
|
---|
660 |
|
---|
661 | rc = AudioTestPathCreate(pSet->szPathAbs, sizeof(pSet->szPathAbs), pSet->szTag);
|
---|
662 | }
|
---|
663 | else
|
---|
664 | rc = AudioTestPathCreateTemp(pSet->szPathAbs, sizeof(pSet->szPathAbs), pSet->szTag);
|
---|
665 | AssertRCReturn(rc, rc);
|
---|
666 |
|
---|
667 | /*
|
---|
668 | * Create the manifest file.
|
---|
669 | */
|
---|
670 | char szTmp[RTPATH_MAX];
|
---|
671 | rc = RTPathJoin(szTmp, sizeof(szTmp), pSet->szPathAbs, AUDIOTEST_MANIFEST_FILE_STR);
|
---|
672 | AssertRCReturn(rc, rc);
|
---|
673 |
|
---|
674 | rc = RTFileOpen(&pSet->f.hFile, szTmp, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
675 | AssertRCReturn(rc, rc);
|
---|
676 |
|
---|
677 | rc = audioTestManifestWriteSectionHdr(pSet, "header");
|
---|
678 | AssertRCReturn(rc, rc);
|
---|
679 |
|
---|
680 | rc = audioTestManifestWrite(pSet, "magic=vkat_ini\n"); /* VKAT Manifest, .INI-style. */
|
---|
681 | AssertRCReturn(rc, rc);
|
---|
682 | rc = audioTestManifestWrite(pSet, "ver=%d\n", AUDIOTEST_MANIFEST_VER);
|
---|
683 | AssertRCReturn(rc, rc);
|
---|
684 | rc = audioTestManifestWrite(pSet, "tag=%s\n", pSet->szTag);
|
---|
685 | AssertRCReturn(rc, rc);
|
---|
686 |
|
---|
687 | AssertCompile(sizeof(szTmp) > RTTIME_STR_LEN);
|
---|
688 | RTTIMESPEC Now;
|
---|
689 | rc = audioTestManifestWrite(pSet, "date_created=%s\n", RTTimeSpecToString(RTTimeNow(&Now), szTmp, sizeof(szTmp)));
|
---|
690 | AssertRCReturn(rc, rc);
|
---|
691 |
|
---|
692 | RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
|
---|
693 | rc = audioTestManifestWrite(pSet, "os_product=%s\n", szTmp);
|
---|
694 | AssertRCReturn(rc, rc);
|
---|
695 |
|
---|
696 | RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
|
---|
697 | rc = audioTestManifestWrite(pSet, "os_rel=%s\n", szTmp);
|
---|
698 | AssertRCReturn(rc, rc);
|
---|
699 |
|
---|
700 | RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
|
---|
701 | rc = audioTestManifestWrite(pSet, "os_ver=%s\n", szTmp);
|
---|
702 | AssertRCReturn(rc, rc);
|
---|
703 |
|
---|
704 | rc = audioTestManifestWrite(pSet, "vbox_ver=%s r%u %s (%s %s)\n",
|
---|
705 | VBOX_VERSION_STRING, RTBldCfgRevision(), RTBldCfgTargetDotArch(), __DATE__, __TIME__);
|
---|
706 | AssertRCReturn(rc, rc);
|
---|
707 |
|
---|
708 | rc = audioTestManifestWrite(pSet, "test_count=");
|
---|
709 | AssertRCReturn(rc, rc);
|
---|
710 | pSet->offTestCount = audioTestManifestGetOffsetAbs(pSet);
|
---|
711 | rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
|
---|
712 | AssertRCReturn(rc, rc);
|
---|
713 |
|
---|
714 | rc = audioTestManifestWrite(pSet, "obj_count=");
|
---|
715 | AssertRCReturn(rc, rc);
|
---|
716 | pSet->offObjCount = audioTestManifestGetOffsetAbs(pSet);
|
---|
717 | rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
|
---|
718 | AssertRCReturn(rc, rc);
|
---|
719 |
|
---|
720 | pSet->enmMode = AUDIOTESTSETMODE_TEST;
|
---|
721 |
|
---|
722 | return rc;
|
---|
723 | }
|
---|
724 |
|
---|
725 | /**
|
---|
726 | * Destroys a test set.
|
---|
727 | *
|
---|
728 | * @returns VBox status code.
|
---|
729 | * @param pSet Test set to destroy.
|
---|
730 | */
|
---|
731 | int AudioTestSetDestroy(PAUDIOTESTSET pSet)
|
---|
732 | {
|
---|
733 | if (!pSet)
|
---|
734 | return VINF_SUCCESS;
|
---|
735 |
|
---|
736 | AssertReturn(pSet->cTestsRunning == 0, VERR_WRONG_ORDER); /* Make sure no tests sill are running. */
|
---|
737 |
|
---|
738 | int rc = AudioTestSetClose(pSet);
|
---|
739 | if (RT_FAILURE(rc))
|
---|
740 | return rc;
|
---|
741 |
|
---|
742 | PAUDIOTESTOBJ pObj, pObjNext;
|
---|
743 | RTListForEachSafe(&pSet->lstObj, pObj, pObjNext, AUDIOTESTOBJ, Node)
|
---|
744 | {
|
---|
745 | rc = AudioTestSetObjClose(pObj);
|
---|
746 | if (RT_SUCCESS(rc))
|
---|
747 | {
|
---|
748 | RTListNodeRemove(&pObj->Node);
|
---|
749 | RTMemFree(pObj);
|
---|
750 |
|
---|
751 | Assert(pSet->cObj);
|
---|
752 | pSet->cObj--;
|
---|
753 | }
|
---|
754 | else
|
---|
755 | break;
|
---|
756 | }
|
---|
757 |
|
---|
758 | if (RT_FAILURE(rc))
|
---|
759 | return rc;
|
---|
760 |
|
---|
761 | Assert(pSet->cObj == 0);
|
---|
762 |
|
---|
763 | PAUDIOTESTENTRY pEntry, pEntryNext;
|
---|
764 | RTListForEachSafe(&pSet->lstTest, pEntry, pEntryNext, AUDIOTESTENTRY, Node)
|
---|
765 | {
|
---|
766 | RTListNodeRemove(&pEntry->Node);
|
---|
767 | RTMemFree(pEntry);
|
---|
768 |
|
---|
769 | Assert(pSet->cTests);
|
---|
770 | pSet->cTests--;
|
---|
771 | }
|
---|
772 |
|
---|
773 | if (RT_FAILURE(rc))
|
---|
774 | return rc;
|
---|
775 |
|
---|
776 | Assert(pSet->cTests == 0);
|
---|
777 |
|
---|
778 | return rc;
|
---|
779 | }
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Opens an existing audio test set.
|
---|
783 | *
|
---|
784 | * @returns VBox status code.
|
---|
785 | * @param pSet Test set to open.
|
---|
786 | * @param pszPath Absolute path of the test set to open.
|
---|
787 | */
|
---|
788 | int AudioTestSetOpen(PAUDIOTESTSET pSet, const char *pszPath)
|
---|
789 | {
|
---|
790 | audioTestSetInitInternal(pSet);
|
---|
791 |
|
---|
792 | char szManifest[RTPATH_MAX];
|
---|
793 | int rc = RTPathJoin(szManifest, sizeof(szManifest), pszPath, AUDIOTEST_MANIFEST_FILE_STR);
|
---|
794 | AssertRCReturn(rc, rc);
|
---|
795 |
|
---|
796 | RTVFSFILE hVfsFile;
|
---|
797 | rc = RTVfsFileOpenNormal(szManifest, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE, &hVfsFile);
|
---|
798 | if (RT_FAILURE(rc))
|
---|
799 | return rc;
|
---|
800 |
|
---|
801 | rc = RTIniFileCreateFromVfsFile(&pSet->f.hIniFile, hVfsFile, RTINIFILE_F_READONLY);
|
---|
802 | RTVfsFileRelease(hVfsFile);
|
---|
803 | AssertRCReturn(rc, rc);
|
---|
804 |
|
---|
805 | pSet->enmMode = AUDIOTESTSETMODE_VERIFY;
|
---|
806 |
|
---|
807 | return rc;
|
---|
808 | }
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * Closes an opened audio test set.
|
---|
812 | *
|
---|
813 | * @returns VBox status code.
|
---|
814 | * @param pSet Test set to close.
|
---|
815 | */
|
---|
816 | int AudioTestSetClose(PAUDIOTESTSET pSet)
|
---|
817 | {
|
---|
818 | if (!pSet)
|
---|
819 | return VINF_SUCCESS;
|
---|
820 |
|
---|
821 | if (!RTFileIsValid(pSet->f.hFile))
|
---|
822 | return VINF_SUCCESS;
|
---|
823 |
|
---|
824 | int rc;
|
---|
825 |
|
---|
826 | /* Update number of bound test objects. */
|
---|
827 | PAUDIOTESTENTRY pTest;
|
---|
828 | RTListForEach(&pSet->lstTest, pTest, AUDIOTESTENTRY, Node)
|
---|
829 | {
|
---|
830 | rc = RTFileSeek(pSet->f.hFile, pTest->offObjCount, RTFILE_SEEK_BEGIN, NULL);
|
---|
831 | AssertRCReturn(rc, rc);
|
---|
832 | rc = audioTestManifestWrite(pSet, "%04RU32", pTest->cObj);
|
---|
833 | AssertRCReturn(rc, rc);
|
---|
834 | }
|
---|
835 |
|
---|
836 | /*
|
---|
837 | * Update number of ran tests.
|
---|
838 | */
|
---|
839 | rc = RTFileSeek(pSet->f.hFile, pSet->offObjCount, RTFILE_SEEK_BEGIN, NULL);
|
---|
840 | AssertRCReturn(rc, rc);
|
---|
841 | rc = audioTestManifestWrite(pSet, "%04RU32", pSet->cObj);
|
---|
842 | AssertRCReturn(rc, rc);
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * Update number of ran tests.
|
---|
846 | */
|
---|
847 | rc = RTFileSeek(pSet->f.hFile, pSet->offTestCount, RTFILE_SEEK_BEGIN, NULL);
|
---|
848 | AssertRCReturn(rc, rc);
|
---|
849 | rc = audioTestManifestWrite(pSet, "%04RU32", pSet->cTests);
|
---|
850 | AssertRCReturn(rc, rc);
|
---|
851 |
|
---|
852 | /*
|
---|
853 | * Serialize all registered test objects.
|
---|
854 | */
|
---|
855 | rc = RTFileSeek(pSet->f.hFile, 0, RTFILE_SEEK_END, NULL);
|
---|
856 | AssertRCReturn(rc, rc);
|
---|
857 |
|
---|
858 | PAUDIOTESTOBJ pObj;
|
---|
859 | RTListForEach(&pSet->lstObj, pObj, AUDIOTESTOBJ, Node)
|
---|
860 | {
|
---|
861 | rc = audioTestManifestWrite(pSet, "\n");
|
---|
862 | AssertRCReturn(rc, rc);
|
---|
863 | char szUuid[64];
|
---|
864 | rc = RTUuidToStr(&pObj->Uuid, szUuid, sizeof(szUuid));
|
---|
865 | AssertRCReturn(rc, rc);
|
---|
866 | rc = audioTestManifestWriteSectionHdr(pSet, "obj_%s", szUuid);
|
---|
867 | AssertRCReturn(rc, rc);
|
---|
868 | rc = audioTestManifestWrite(pSet, "obj_type=%RU32\n", pObj->enmType);
|
---|
869 | AssertRCReturn(rc, rc);
|
---|
870 | rc = audioTestManifestWrite(pSet, "obj_name=%s\n", pObj->szName);
|
---|
871 | AssertRCReturn(rc, rc);
|
---|
872 | }
|
---|
873 |
|
---|
874 | RTFileClose(pSet->f.hFile);
|
---|
875 | pSet->f.hFile = NIL_RTFILE;
|
---|
876 |
|
---|
877 | return rc;
|
---|
878 | }
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Physically wipes all related test set files off the disk.
|
---|
882 | *
|
---|
883 | * @returns VBox status code.
|
---|
884 | * @param pSet Test set to wipe.
|
---|
885 | */
|
---|
886 | int AudioTestSetWipe(PAUDIOTESTSET pSet)
|
---|
887 | {
|
---|
888 | AssertPtrReturn(pSet, VERR_INVALID_POINTER);
|
---|
889 |
|
---|
890 | int rc = VINF_SUCCESS;
|
---|
891 | char szFilePath[RTPATH_MAX];
|
---|
892 |
|
---|
893 | PAUDIOTESTOBJ pObj;
|
---|
894 | RTListForEach(&pSet->lstObj, pObj, AUDIOTESTOBJ, Node)
|
---|
895 | {
|
---|
896 | int rc2 = AudioTestSetObjClose(pObj);
|
---|
897 | if (RT_SUCCESS(rc2))
|
---|
898 | {
|
---|
899 | rc2 = audioTestSetGetObjPath(pSet, szFilePath, sizeof(szFilePath), pObj->szName);
|
---|
900 | if (RT_SUCCESS(rc2))
|
---|
901 | rc2 = RTFileDelete(szFilePath);
|
---|
902 | }
|
---|
903 |
|
---|
904 | if (RT_SUCCESS(rc))
|
---|
905 | rc = rc2;
|
---|
906 | /* Keep going. */
|
---|
907 | }
|
---|
908 |
|
---|
909 | if (RT_SUCCESS(rc))
|
---|
910 | {
|
---|
911 | rc = RTPathJoin(szFilePath, sizeof(szFilePath), pSet->szPathAbs, AUDIOTEST_MANIFEST_FILE_STR);
|
---|
912 | if (RT_SUCCESS(rc))
|
---|
913 | rc = RTFileDelete(szFilePath);
|
---|
914 | }
|
---|
915 |
|
---|
916 | /* Remove the (hopefully now empty) directory. Otherwise let this fail. */
|
---|
917 | if (RT_SUCCESS(rc))
|
---|
918 | rc = RTDirRemove(pSet->szPathAbs);
|
---|
919 |
|
---|
920 | return rc;
|
---|
921 | }
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * Creates and registers a new audio test object to the current running test.
|
---|
925 | *
|
---|
926 | * @returns VBox status code.
|
---|
927 | * @param pSet Test set to create and register new object for.
|
---|
928 | * @param pszName Name of new object to create.
|
---|
929 | * @param ppObj Where to return the pointer to the newly created object on success.
|
---|
930 | */
|
---|
931 | int AudioTestSetObjCreateAndRegister(PAUDIOTESTSET pSet, const char *pszName, PAUDIOTESTOBJ *ppObj)
|
---|
932 | {
|
---|
933 | AssertReturn(pSet->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
|
---|
934 |
|
---|
935 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
936 |
|
---|
937 | PAUDIOTESTOBJ pObj = (PAUDIOTESTOBJ)RTMemAlloc(sizeof(AUDIOTESTOBJ));
|
---|
938 | AssertPtrReturn(pObj, VERR_NO_MEMORY);
|
---|
939 |
|
---|
940 | if (RTStrPrintf2(pObj->szName, sizeof(pObj->szName), "%04RU32-%s", pSet->cObj, pszName) <= 0)
|
---|
941 | AssertFailedReturn(VERR_BUFFER_OVERFLOW);
|
---|
942 |
|
---|
943 | /** @todo Generalize this function more once we have more object types. */
|
---|
944 |
|
---|
945 | char szObjPathAbs[RTPATH_MAX];
|
---|
946 | int rc = audioTestSetGetObjPath(pSet, szObjPathAbs, sizeof(szObjPathAbs), pObj->szName);
|
---|
947 | if (RT_SUCCESS(rc))
|
---|
948 | {
|
---|
949 | rc = RTFileOpen(&pObj->File.hFile, szObjPathAbs, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
950 | if (RT_SUCCESS(rc))
|
---|
951 | {
|
---|
952 | pObj->enmType = AUDIOTESTOBJTYPE_FILE;
|
---|
953 | pObj->cRefs = 1; /* Currently only 1:1 mapping. */
|
---|
954 |
|
---|
955 | RTListAppend(&pSet->lstObj, &pObj->Node);
|
---|
956 | pSet->cObj++;
|
---|
957 |
|
---|
958 | /* Generate + set an UUID for the object and assign it to the current test. */
|
---|
959 | rc = RTUuidCreate(&pObj->Uuid);
|
---|
960 | AssertRCReturn(rc, rc);
|
---|
961 | char szUuid[64];
|
---|
962 | rc = RTUuidToStr(&pObj->Uuid, szUuid, sizeof(szUuid));
|
---|
963 | AssertRCReturn(rc, rc);
|
---|
964 |
|
---|
965 | rc = audioTestManifestWrite(pSet, "obj%RU32_uuid=%s\n", pSet->pTestCur->cObj, szUuid);
|
---|
966 | AssertRCReturn(rc, rc);
|
---|
967 |
|
---|
968 | AssertPtr(pSet->pTestCur);
|
---|
969 | pSet->pTestCur->cObj++;
|
---|
970 |
|
---|
971 | *ppObj = pObj;
|
---|
972 | }
|
---|
973 | }
|
---|
974 |
|
---|
975 | if (RT_FAILURE(rc))
|
---|
976 | RTMemFree(pObj);
|
---|
977 |
|
---|
978 | return rc;
|
---|
979 | }
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * Writes to a created audio test object.
|
---|
983 | *
|
---|
984 | * @returns VBox status code.
|
---|
985 | * @param pObj Audio test object to write to.
|
---|
986 | * @param pvBuf Pointer to data to write.
|
---|
987 | * @param cbBuf Size (in bytes) of \a pvBuf to write.
|
---|
988 | */
|
---|
989 | int AudioTestSetObjWrite(PAUDIOTESTOBJ pObj, void *pvBuf, size_t cbBuf)
|
---|
990 | {
|
---|
991 | /** @todo Generalize this function more once we have more object types. */
|
---|
992 | AssertReturn(pObj->enmType == AUDIOTESTOBJTYPE_FILE, VERR_INVALID_PARAMETER);
|
---|
993 |
|
---|
994 | return RTFileWrite(pObj->File.hFile, pvBuf, cbBuf, NULL);
|
---|
995 | }
|
---|
996 |
|
---|
997 | /**
|
---|
998 | * Closes an opened audio test object.
|
---|
999 | *
|
---|
1000 | * @returns VBox status code.
|
---|
1001 | * @param pObj Audio test object to close.
|
---|
1002 | */
|
---|
1003 | int AudioTestSetObjClose(PAUDIOTESTOBJ pObj)
|
---|
1004 | {
|
---|
1005 | if (!pObj)
|
---|
1006 | return VINF_SUCCESS;
|
---|
1007 |
|
---|
1008 | /** @todo Generalize this function more once we have more object types. */
|
---|
1009 | AssertReturn(pObj->enmType == AUDIOTESTOBJTYPE_FILE, VERR_INVALID_PARAMETER);
|
---|
1010 |
|
---|
1011 | int rc = VINF_SUCCESS;
|
---|
1012 |
|
---|
1013 | if (RTFileIsValid(pObj->File.hFile))
|
---|
1014 | {
|
---|
1015 | rc = RTFileClose(pObj->File.hFile);
|
---|
1016 | pObj->File.hFile = NIL_RTFILE;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | return rc;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | int AudioTestSetTestBegin(PAUDIOTESTSET pSet, const char *pszDesc, PAUDIOTESTPARMS pParms, PAUDIOTESTENTRY *ppEntry)
|
---|
1023 | {
|
---|
1024 | AssertReturn(pSet->cTestsRunning == 0, VERR_WRONG_ORDER); /* No test nesting allowed. */
|
---|
1025 |
|
---|
1026 | PAUDIOTESTENTRY pEntry = (PAUDIOTESTENTRY)RTMemAllocZ(sizeof(AUDIOTESTENTRY));
|
---|
1027 | AssertPtrReturn(pEntry, VERR_NO_MEMORY);
|
---|
1028 |
|
---|
1029 | int rc = RTStrCopy(pEntry->szDesc, sizeof(pEntry->szDesc), pszDesc);
|
---|
1030 | AssertRCReturn(rc, rc);
|
---|
1031 |
|
---|
1032 | memcpy(&pEntry->Parms, pParms, sizeof(AUDIOTESTPARMS));
|
---|
1033 | pEntry->pParent = pSet;
|
---|
1034 |
|
---|
1035 | rc = audioTestManifestWrite(pSet, "\n");
|
---|
1036 | AssertRCReturn(rc, rc);
|
---|
1037 |
|
---|
1038 | rc = audioTestManifestWriteSectionHdr(pSet, "test_%04RU32", pSet->cTests);
|
---|
1039 | AssertRCReturn(rc, rc);
|
---|
1040 | rc = audioTestManifestWrite(pSet, "test_desc=%s\n", pszDesc);
|
---|
1041 | AssertRCReturn(rc, rc);
|
---|
1042 | rc = audioTestManifestWrite(pSet, "test_type=%RU32\n", pParms->enmType);
|
---|
1043 | AssertRCReturn(rc, rc);
|
---|
1044 | rc = audioTestManifestWrite(pSet, "test_delay_ms=%RU32\n", pParms->msDelay);
|
---|
1045 | AssertRCReturn(rc, rc);
|
---|
1046 | rc = audioTestManifestWrite(pSet, "audio_direction=%s\n", PDMAudioDirGetName(pParms->enmDir));
|
---|
1047 | AssertRCReturn(rc, rc);
|
---|
1048 |
|
---|
1049 | rc = audioTestManifestWrite(pSet, "obj_count=");
|
---|
1050 | AssertRCReturn(rc, rc);
|
---|
1051 | pEntry->offObjCount = audioTestManifestGetOffsetAbs(pSet);
|
---|
1052 | rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
|
---|
1053 | AssertRCReturn(rc, rc);
|
---|
1054 |
|
---|
1055 | switch (pParms->enmType)
|
---|
1056 | {
|
---|
1057 | case AUDIOTESTTYPE_TESTTONE_PLAY:
|
---|
1058 | RT_FALL_THROUGH();
|
---|
1059 | case AUDIOTESTTYPE_TESTTONE_RECORD:
|
---|
1060 | {
|
---|
1061 | rc = audioTestManifestWrite(pSet, "tone_freq_hz=%RU16\n", (uint16_t)pParms->TestTone.dbFreqHz);
|
---|
1062 | AssertRCReturn(rc, rc);
|
---|
1063 | rc = audioTestManifestWrite(pSet, "tone_prequel_ms=%RU32\n", pParms->TestTone.msPrequel);
|
---|
1064 | AssertRCReturn(rc, rc);
|
---|
1065 | rc = audioTestManifestWrite(pSet, "tone_duration_ms=%RU32\n", pParms->TestTone.msDuration);
|
---|
1066 | AssertRCReturn(rc, rc);
|
---|
1067 | rc = audioTestManifestWrite(pSet, "tone_sequel_ms=%RU32\n", pParms->TestTone.msSequel);
|
---|
1068 | AssertRCReturn(rc, rc);
|
---|
1069 | rc = audioTestManifestWrite(pSet, "tone_volume_percent=%RU32\n", pParms->TestTone.uVolumePercent);
|
---|
1070 | AssertRCReturn(rc, rc);
|
---|
1071 | rc = audioTestManifestWrite(pSet, "tone_pcm_hz=%RU32\n", PDMAudioPropsHz(&pParms->TestTone.Props));
|
---|
1072 | AssertRCReturn(rc, rc);
|
---|
1073 | rc = audioTestManifestWrite(pSet, "tone_pcm_channels=%RU8\n", PDMAudioPropsChannels(&pParms->TestTone.Props));
|
---|
1074 | AssertRCReturn(rc, rc);
|
---|
1075 | rc = audioTestManifestWrite(pSet, "tone_pcm_bits=%RU8\n", PDMAudioPropsSampleBits(&pParms->TestTone.Props));
|
---|
1076 | AssertRCReturn(rc, rc);
|
---|
1077 | rc = audioTestManifestWrite(pSet, "tone_pcm_is_signed=%RTbool\n", PDMAudioPropsIsSigned(&pParms->TestTone.Props));
|
---|
1078 | AssertRCReturn(rc, rc);
|
---|
1079 | break;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | default:
|
---|
1083 | AssertFailed();
|
---|
1084 | break;
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | RTListAppend(&pSet->lstTest, &pEntry->Node);
|
---|
1088 | pSet->cTests++;
|
---|
1089 | pSet->cTestsRunning++;
|
---|
1090 | pSet->pTestCur = pEntry;
|
---|
1091 |
|
---|
1092 | *ppEntry = pEntry;
|
---|
1093 |
|
---|
1094 | return rc;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | int AudioTestSetTestFailed(PAUDIOTESTENTRY pEntry, int rc, const char *pszErr)
|
---|
1098 | {
|
---|
1099 | AssertReturn(pEntry->pParent->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
|
---|
1100 | AssertReturn(pEntry->rc == VINF_SUCCESS, VERR_WRONG_ORDER);
|
---|
1101 |
|
---|
1102 | pEntry->rc = rc;
|
---|
1103 |
|
---|
1104 | int rc2 = audioTestManifestWrite(pEntry->pParent, "error_rc=%RI32\n", rc);
|
---|
1105 | AssertRCReturn(rc2, rc2);
|
---|
1106 | rc2 = audioTestManifestWrite(pEntry->pParent, "error_desc=%s\n", pszErr);
|
---|
1107 | AssertRCReturn(rc2, rc2);
|
---|
1108 |
|
---|
1109 | pEntry->pParent->cTestsRunning--;
|
---|
1110 | pEntry->pParent->pTestCur = NULL;
|
---|
1111 |
|
---|
1112 | return rc2;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | int AudioTestSetTestDone(PAUDIOTESTENTRY pEntry)
|
---|
1116 | {
|
---|
1117 | AssertReturn(pEntry->pParent->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
|
---|
1118 | AssertReturn(pEntry->rc == VINF_SUCCESS, VERR_WRONG_ORDER);
|
---|
1119 |
|
---|
1120 | int rc2 = audioTestManifestWrite(pEntry->pParent, "error_rc=%RI32\n", VINF_SUCCESS);
|
---|
1121 | AssertRCReturn(rc2, rc2);
|
---|
1122 |
|
---|
1123 | pEntry->pParent->cTestsRunning--;
|
---|
1124 | pEntry->pParent->pTestCur = NULL;
|
---|
1125 |
|
---|
1126 | return rc2;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | /**
|
---|
1130 | * Packs an audio test so that it's ready for transmission.
|
---|
1131 | *
|
---|
1132 | * @returns VBox status code.
|
---|
1133 | * @param pSet Test set to pack.
|
---|
1134 | * @param pszOutDir Directory where to store the packed test set.
|
---|
1135 | * @param pszFileName Where to return the final name of the packed test set. Optional and can be NULL.
|
---|
1136 | * @param cbFileName Size (in bytes) of \a pszFileName.
|
---|
1137 | */
|
---|
1138 | int AudioTestSetPack(PAUDIOTESTSET pSet, const char *pszOutDir, char *pszFileName, size_t cbFileName)
|
---|
1139 | {
|
---|
1140 | AssertReturn(!pszFileName || cbFileName, VERR_INVALID_PARAMETER);
|
---|
1141 | AssertReturn(audioTestManifestIsOpen(pSet), VERR_WRONG_ORDER);
|
---|
1142 |
|
---|
1143 | /** @todo Check and deny if \a pszOutDir is part of the set's path. */
|
---|
1144 |
|
---|
1145 | char szOutName[RT_ELEMENTS(AUDIOTEST_PATH_PREFIX_STR) + AUDIOTEST_TAG_MAX + 16];
|
---|
1146 | if (RTStrPrintf2(szOutName, sizeof(szOutName), "%s-%s%s",
|
---|
1147 | AUDIOTEST_PATH_PREFIX_STR, pSet->szTag, AUDIOTEST_ARCHIVE_SUFF_STR) <= 0)
|
---|
1148 | AssertFailedReturn(VERR_BUFFER_OVERFLOW);
|
---|
1149 |
|
---|
1150 | char szOutPath[RTPATH_MAX];
|
---|
1151 | int rc = RTPathJoin(szOutPath, sizeof(szOutPath), pszOutDir, szOutName);
|
---|
1152 | AssertRCReturn(rc, rc);
|
---|
1153 |
|
---|
1154 | const char *apszArgs[10];
|
---|
1155 | unsigned cArgs = 0;
|
---|
1156 |
|
---|
1157 | apszArgs[cArgs++] = "AudioTest";
|
---|
1158 | apszArgs[cArgs++] = "--create";
|
---|
1159 | apszArgs[cArgs++] = "--gzip";
|
---|
1160 | apszArgs[cArgs++] = "--directory";
|
---|
1161 | apszArgs[cArgs++] = pSet->szPathAbs;
|
---|
1162 | apszArgs[cArgs++] = "--file";
|
---|
1163 | apszArgs[cArgs++] = szOutPath;
|
---|
1164 | apszArgs[cArgs++] = ".";
|
---|
1165 |
|
---|
1166 | RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
|
---|
1167 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
1168 | rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */
|
---|
1169 |
|
---|
1170 | if (RT_SUCCESS(rc))
|
---|
1171 | {
|
---|
1172 | if (pszFileName)
|
---|
1173 | rc = RTStrCopy(pszFileName, cbFileName, szOutPath);
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | return rc;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /**
|
---|
1180 | * Returns whether a test set archive is packed (as .tar.gz by default) or
|
---|
1181 | * a plain directory.
|
---|
1182 | *
|
---|
1183 | * @returns \c true if packed (as .tar.gz), or \c false if not (directory).
|
---|
1184 | * @param pszPath Path to return packed staus for.
|
---|
1185 | */
|
---|
1186 | bool AudioTestSetIsPacked(const char *pszPath)
|
---|
1187 | {
|
---|
1188 | /** @todo Improve this, good enough for now. */
|
---|
1189 | return (RTStrIStr(pszPath, AUDIOTEST_ARCHIVE_SUFF_STR) != NULL);
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | /**
|
---|
1193 | * Unpacks a formerly packed audio test set.
|
---|
1194 | *
|
---|
1195 | * @returns VBox status code.
|
---|
1196 | * @param pszFile Test set file to unpack. Must contain the absolute path.
|
---|
1197 | * @param pszOutDir Directory where to unpack the test set into.
|
---|
1198 | * If the directory does not exist it will be created.
|
---|
1199 | */
|
---|
1200 | int AudioTestSetUnpack(const char *pszFile, const char *pszOutDir)
|
---|
1201 | {
|
---|
1202 | AssertReturn(pszFile && pszOutDir, VERR_INVALID_PARAMETER);
|
---|
1203 |
|
---|
1204 | int rc = VINF_SUCCESS;
|
---|
1205 |
|
---|
1206 | if (!RTDirExists(pszOutDir))
|
---|
1207 | {
|
---|
1208 | rc = RTDirCreateFullPath(pszOutDir, 0755);
|
---|
1209 | if (RT_FAILURE(rc))
|
---|
1210 | return rc;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | const char *apszArgs[8];
|
---|
1214 | unsigned cArgs = 0;
|
---|
1215 |
|
---|
1216 | apszArgs[cArgs++] = "AudioTest";
|
---|
1217 | apszArgs[cArgs++] = "--extract";
|
---|
1218 | apszArgs[cArgs++] = "--gunzip";
|
---|
1219 | apszArgs[cArgs++] = "--directory";
|
---|
1220 | apszArgs[cArgs++] = pszOutDir;
|
---|
1221 | apszArgs[cArgs++] = "--file";
|
---|
1222 | apszArgs[cArgs++] = pszFile;
|
---|
1223 |
|
---|
1224 | RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
|
---|
1225 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
1226 | rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */
|
---|
1227 |
|
---|
1228 | return rc;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | static int audioTestVerifyIniValue(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB,
|
---|
1232 | const char *pszSec, const char *pszKey, const char *pszVal)
|
---|
1233 | {
|
---|
1234 | char szValA[_1K];
|
---|
1235 | int rc = RTIniFileQueryValue(pSetA->f.hIniFile, pszSec, pszKey, szValA, sizeof(szValA), NULL);
|
---|
1236 | if (RT_FAILURE(rc))
|
---|
1237 | return rc;
|
---|
1238 | char szValB[_1K];
|
---|
1239 | rc = RTIniFileQueryValue(pSetB->f.hIniFile, pszSec, pszKey, szValB, sizeof(szValB), NULL);
|
---|
1240 | if (RT_FAILURE(rc))
|
---|
1241 | return rc;
|
---|
1242 |
|
---|
1243 | if (RTStrCmp(szValA, szValB))
|
---|
1244 | return VERR_WRONG_TYPE; /** @todo Fudge! */
|
---|
1245 |
|
---|
1246 | if (pszVal)
|
---|
1247 | {
|
---|
1248 | if (RTStrCmp(szValA, pszVal))
|
---|
1249 | return VERR_WRONG_TYPE; /** @todo Fudge! */
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | return rc;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /**
|
---|
1256 | * Verifies an opened audio test set.
|
---|
1257 | *
|
---|
1258 | * @returns VBox status code.
|
---|
1259 | * @param pSetA Test set A to verify.
|
---|
1260 | * @param pSetB Test set to verify test set A with.
|
---|
1261 | * @param pErrDesc Where to return the test verification errors.
|
---|
1262 | *
|
---|
1263 | * @note Test verification errors have to be checked for errors, regardless of the
|
---|
1264 | * actual return code.
|
---|
1265 | */
|
---|
1266 | int AudioTestSetVerify(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB, PAUDIOTESTERRORDESC pErrDesc)
|
---|
1267 | {
|
---|
1268 | AssertReturn(audioTestManifestIsOpen(pSetA), VERR_WRONG_ORDER);
|
---|
1269 | AssertReturn(audioTestManifestIsOpen(pSetB), VERR_WRONG_ORDER);
|
---|
1270 |
|
---|
1271 | /* We ASSUME the caller has not init'd pErrDesc. */
|
---|
1272 | audioTestErrorDescInit(pErrDesc);
|
---|
1273 |
|
---|
1274 | int rc;
|
---|
1275 |
|
---|
1276 | #define VERIFY_VALUE(a_Sec, a_Key, a_Val, ...) \
|
---|
1277 | rc = audioTestVerifyIniValue(pSetA, pSetB, a_Sec, a_Key, a_Val); \
|
---|
1278 | if (RT_FAILURE(rc)) \
|
---|
1279 | return audioTestErrorDescAdd(pErrDesc, (__VA_ARGS__));
|
---|
1280 |
|
---|
1281 | /*
|
---|
1282 | * Compare obvious values first.
|
---|
1283 | */
|
---|
1284 | VERIFY_VALUE("header", "magic", "vkat_ini", "Manifest magic wrong");
|
---|
1285 | VERIFY_VALUE("header", "ver", "1" , "Manifest version wrong");
|
---|
1286 | VERIFY_VALUE("header", "tag", NULL, "Manifest tags don't match");
|
---|
1287 | VERIFY_VALUE("header", "test_count", NULL, "Test counts don't match");
|
---|
1288 | VERIFY_VALUE("header", "obj_count", NULL, "Object counts don't match");
|
---|
1289 |
|
---|
1290 | #undef VERIFY_VALUE
|
---|
1291 |
|
---|
1292 | /* Only return critical stuff not related to actual testing here. */
|
---|
1293 | return VINF_SUCCESS;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 |
|
---|
1297 | /*********************************************************************************************************************************
|
---|
1298 | * WAVE File Reader. *
|
---|
1299 | *********************************************************************************************************************************/
|
---|
1300 |
|
---|
1301 | /**
|
---|
1302 | * Counts the number of set bits in @a fMask.
|
---|
1303 | */
|
---|
1304 | static unsigned audioTestWaveCountBits(uint32_t fMask)
|
---|
1305 | {
|
---|
1306 | unsigned cBits = 0;
|
---|
1307 | while (fMask)
|
---|
1308 | {
|
---|
1309 | if (fMask & 1)
|
---|
1310 | cBits++;
|
---|
1311 | fMask >>= 1;
|
---|
1312 | }
|
---|
1313 | return cBits;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | /**
|
---|
1317 | * Opens a wave (.WAV) file for reading.
|
---|
1318 | *
|
---|
1319 | * @returns VBox status code.
|
---|
1320 | * @param pszFile The file to open.
|
---|
1321 | * @param pWaveFile The open wave file structure to fill in on success.
|
---|
1322 | * @param pErrInfo Where to return addition error details on failure.
|
---|
1323 | */
|
---|
1324 | int AudioTestWaveFileOpen(const char *pszFile, PAUDIOTESTWAVEFILE pWaveFile, PRTERRINFO pErrInfo)
|
---|
1325 | {
|
---|
1326 | RT_ZERO(pWaveFile->Props);
|
---|
1327 | pWaveFile->hFile = NIL_RTFILE;
|
---|
1328 | int rc = RTFileOpen(&pWaveFile->hFile, pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
1329 | if (RT_FAILURE(rc))
|
---|
1330 | return RTErrInfoSet(pErrInfo, rc, "RTFileOpen failed");
|
---|
1331 | uint64_t cbFile = 0;
|
---|
1332 | rc = RTFileQuerySize(pWaveFile->hFile, &cbFile);
|
---|
1333 | if (RT_SUCCESS(rc))
|
---|
1334 | {
|
---|
1335 | union
|
---|
1336 | {
|
---|
1337 | uint8_t ab[512];
|
---|
1338 | struct
|
---|
1339 | {
|
---|
1340 | RTRIFFHDR Hdr;
|
---|
1341 | union
|
---|
1342 | {
|
---|
1343 | RTRIFFWAVEFMTCHUNK Fmt;
|
---|
1344 | RTRIFFWAVEFMTEXTCHUNK FmtExt;
|
---|
1345 | } u;
|
---|
1346 | } Wave;
|
---|
1347 | RTRIFFLIST List;
|
---|
1348 | RTRIFFCHUNK Chunk;
|
---|
1349 | RTRIFFWAVEDATACHUNK Data;
|
---|
1350 | } uBuf;
|
---|
1351 |
|
---|
1352 | rc = RTFileRead(pWaveFile->hFile, &uBuf.Wave, sizeof(uBuf.Wave), NULL);
|
---|
1353 | if (RT_SUCCESS(rc))
|
---|
1354 | {
|
---|
1355 | rc = VERR_VFS_UNKNOWN_FORMAT;
|
---|
1356 | if ( uBuf.Wave.Hdr.uMagic == RTRIFFHDR_MAGIC
|
---|
1357 | && uBuf.Wave.Hdr.uFileType == RTRIFF_FILE_TYPE_WAVE
|
---|
1358 | && uBuf.Wave.u.Fmt.Chunk.uMagic == RTRIFFWAVEFMT_MAGIC
|
---|
1359 | && uBuf.Wave.u.Fmt.Chunk.cbChunk >= sizeof(uBuf.Wave.u.Fmt.Data))
|
---|
1360 | {
|
---|
1361 | if (uBuf.Wave.Hdr.cbFile != cbFile - sizeof(RTRIFFCHUNK))
|
---|
1362 | RTErrInfoSetF(pErrInfo, rc, "File size mismatch: %#x, actual %#RX64 (ignored)",
|
---|
1363 | uBuf.Wave.Hdr.cbFile, cbFile - sizeof(RTRIFFCHUNK));
|
---|
1364 | rc = VERR_VFS_BOGUS_FORMAT;
|
---|
1365 | if ( uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_PCM
|
---|
1366 | && uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_EXTENSIBLE)
|
---|
1367 | RTErrInfoSetF(pErrInfo, rc, "Unsupported uFormatTag value: %#x (expected %#x or %#x)",
|
---|
1368 | uBuf.Wave.u.Fmt.Data.uFormatTag, RTRIFFWAVEFMT_TAG_PCM, RTRIFFWAVEFMT_TAG_EXTENSIBLE);
|
---|
1369 | else if ( uBuf.Wave.u.Fmt.Data.cBitsPerSample != 8
|
---|
1370 | && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 16
|
---|
1371 | /* && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 24 - not supported by our stack */
|
---|
1372 | && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 32)
|
---|
1373 | RTErrInfoSetF(pErrInfo, rc, "Unsupported cBitsPerSample value: %u", uBuf.Wave.u.Fmt.Data.cBitsPerSample);
|
---|
1374 | else if ( uBuf.Wave.u.Fmt.Data.cChannels < 1
|
---|
1375 | || uBuf.Wave.u.Fmt.Data.cChannels >= 16)
|
---|
1376 | RTErrInfoSetF(pErrInfo, rc, "Unsupported cChannels value: %u (expected 1..15)", uBuf.Wave.u.Fmt.Data.cChannels);
|
---|
1377 | else if ( uBuf.Wave.u.Fmt.Data.uHz < 4096
|
---|
1378 | || uBuf.Wave.u.Fmt.Data.uHz > 768000)
|
---|
1379 | RTErrInfoSetF(pErrInfo, rc, "Unsupported uHz value: %u (expected 4096..768000)", uBuf.Wave.u.Fmt.Data.uHz);
|
---|
1380 | else if (uBuf.Wave.u.Fmt.Data.cbFrame != uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8)
|
---|
1381 | RTErrInfoSetF(pErrInfo, rc, "Invalid cbFrame value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbFrame,
|
---|
1382 | uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8);
|
---|
1383 | else if (uBuf.Wave.u.Fmt.Data.cbRate != uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz)
|
---|
1384 | RTErrInfoSetF(pErrInfo, rc, "Invalid cbRate value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbRate,
|
---|
1385 | uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz);
|
---|
1386 | else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
|
---|
1387 | && uBuf.Wave.u.FmtExt.Data.cbExtra < RTRIFFWAVEFMTEXT_EXTRA_SIZE)
|
---|
1388 | RTErrInfoSetF(pErrInfo, rc, "Invalid cbExtra value: %#x (expected at least %#x)",
|
---|
1389 | uBuf.Wave.u.FmtExt.Data.cbExtra, RTRIFFWAVEFMTEXT_EXTRA_SIZE);
|
---|
1390 | else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
|
---|
1391 | && audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask) != uBuf.Wave.u.Fmt.Data.cChannels)
|
---|
1392 | RTErrInfoSetF(pErrInfo, rc, "fChannelMask does not match cChannels: %#x (%u bits set) vs %u channels",
|
---|
1393 | uBuf.Wave.u.FmtExt.Data.fChannelMask,
|
---|
1394 | audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask), uBuf.Wave.u.Fmt.Data.cChannels);
|
---|
1395 | else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
|
---|
1396 | && RTUuidCompareStr(&uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM) != 0)
|
---|
1397 | RTErrInfoSetF(pErrInfo, rc, "SubFormat is not PCM: %RTuuid (expected %s)",
|
---|
1398 | &uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
|
---|
1399 | else
|
---|
1400 | {
|
---|
1401 | /*
|
---|
1402 | * Copy out the data we need from the file format structure.
|
---|
1403 | */
|
---|
1404 | PDMAudioPropsInit(&pWaveFile->Props, uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8, true /*fSigned*/,
|
---|
1405 | uBuf.Wave.u.Fmt.Data.cChannels, uBuf.Wave.u.Fmt.Data.uHz);
|
---|
1406 | pWaveFile->offSamples = sizeof(RTRIFFHDR) + sizeof(RTRIFFCHUNK) + uBuf.Wave.u.Fmt.Chunk.cbChunk;
|
---|
1407 |
|
---|
1408 | /*
|
---|
1409 | * Pick up channel assignments if present.
|
---|
1410 | */
|
---|
1411 | if (uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE)
|
---|
1412 | {
|
---|
1413 | static unsigned const s_cStdIds = (unsigned)PDMAUDIOCHANNELID_END_STANDARD
|
---|
1414 | - (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD;
|
---|
1415 | unsigned iCh = 0;
|
---|
1416 | for (unsigned idCh = 0; idCh < 32 && iCh < uBuf.Wave.u.Fmt.Data.cChannels; idCh++)
|
---|
1417 | if (uBuf.Wave.u.FmtExt.Data.fChannelMask & RT_BIT_32(idCh))
|
---|
1418 | {
|
---|
1419 | pWaveFile->Props.aidChannels[iCh] = idCh < s_cStdIds
|
---|
1420 | ? idCh + (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD
|
---|
1421 | : (unsigned)PDMAUDIOCHANNELID_UNKNOWN;
|
---|
1422 | iCh++;
|
---|
1423 | }
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | /*
|
---|
1427 | * Find the 'data' chunk with the audio samples.
|
---|
1428 | *
|
---|
1429 | * There can be INFO lists both preceeding this and succeeding
|
---|
1430 | * it, containing IART and other things we can ignored. Thus
|
---|
1431 | * we read a list header here rather than just a chunk header,
|
---|
1432 | * since it doesn't matter if we read 4 bytes extra as
|
---|
1433 | * AudioTestWaveFileRead uses RTFileReadAt anyway.
|
---|
1434 | */
|
---|
1435 | rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
|
---|
1436 | for (uint32_t i = 0;
|
---|
1437 | i < 128
|
---|
1438 | && RT_SUCCESS(rc)
|
---|
1439 | && uBuf.Chunk.uMagic != RTRIFFWAVEDATACHUNK_MAGIC
|
---|
1440 | && (uint64_t)uBuf.Chunk.cbChunk + sizeof(RTRIFFCHUNK) * 2 <= cbFile - pWaveFile->offSamples;
|
---|
1441 | i++)
|
---|
1442 | {
|
---|
1443 | if ( uBuf.List.uMagic == RTRIFFLIST_MAGIC
|
---|
1444 | && uBuf.List.uListType == RTRIFFLIST_TYPE_INFO)
|
---|
1445 | { /*skip*/ }
|
---|
1446 | else if (uBuf.Chunk.uMagic == RTRIFFPADCHUNK_MAGIC)
|
---|
1447 | { /*skip*/ }
|
---|
1448 | else
|
---|
1449 | break;
|
---|
1450 | pWaveFile->offSamples += sizeof(RTRIFFCHUNK) + uBuf.Chunk.cbChunk;
|
---|
1451 | rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
|
---|
1452 | }
|
---|
1453 | if (RT_SUCCESS(rc))
|
---|
1454 | {
|
---|
1455 | pWaveFile->offSamples += sizeof(uBuf.Data.Chunk);
|
---|
1456 | pWaveFile->cbSamples = (uint32_t)cbFile - pWaveFile->offSamples;
|
---|
1457 |
|
---|
1458 | rc = VERR_VFS_BOGUS_FORMAT;
|
---|
1459 | if ( uBuf.Data.Chunk.uMagic == RTRIFFWAVEDATACHUNK_MAGIC
|
---|
1460 | && uBuf.Data.Chunk.cbChunk <= pWaveFile->cbSamples
|
---|
1461 | && PDMAudioPropsIsSizeAligned(&pWaveFile->Props, uBuf.Data.Chunk.cbChunk))
|
---|
1462 | {
|
---|
1463 | pWaveFile->cbSamples = uBuf.Data.Chunk.cbChunk;
|
---|
1464 |
|
---|
1465 | /*
|
---|
1466 | * We're good!
|
---|
1467 | */
|
---|
1468 | pWaveFile->offCur = 0;
|
---|
1469 | return VINF_SUCCESS;
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | RTErrInfoSetF(pErrInfo, rc, "Bad data header: uMagic=%#x (expected %#x), cbChunk=%#x (max %#RX64, align %u)",
|
---|
1473 | uBuf.Data.Chunk.uMagic, RTRIFFWAVEDATACHUNK_MAGIC,
|
---|
1474 | uBuf.Data.Chunk.cbChunk, pWaveFile->cbSamples, PDMAudioPropsFrameSize(&pWaveFile->Props));
|
---|
1475 | }
|
---|
1476 | else
|
---|
1477 | RTErrInfoSet(pErrInfo, rc, "Failed to read data header");
|
---|
1478 | }
|
---|
1479 | }
|
---|
1480 | else
|
---|
1481 | RTErrInfoSetF(pErrInfo, rc, "Bad file header: uMagic=%#x (vs. %#x), uFileType=%#x (vs %#x), uFmtMagic=%#x (vs %#x) cbFmtChunk=%#x (min %#x)",
|
---|
1482 | uBuf.Wave.Hdr.uMagic, RTRIFFHDR_MAGIC, uBuf.Wave.Hdr.uFileType, RTRIFF_FILE_TYPE_WAVE,
|
---|
1483 | uBuf.Wave.u.Fmt.Chunk.uMagic, RTRIFFWAVEFMT_MAGIC,
|
---|
1484 | uBuf.Wave.u.Fmt.Chunk.cbChunk, sizeof(uBuf.Wave.u.Fmt.Data));
|
---|
1485 | }
|
---|
1486 | else
|
---|
1487 | rc = RTErrInfoSet(pErrInfo, rc, "Failed to read file header");
|
---|
1488 | }
|
---|
1489 | else
|
---|
1490 | rc = RTErrInfoSet(pErrInfo, rc, "Failed to query file size");
|
---|
1491 |
|
---|
1492 | RTFileClose(pWaveFile->hFile);
|
---|
1493 | pWaveFile->hFile = NIL_RTFILE;
|
---|
1494 | return rc;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | /**
|
---|
1498 | * Closes a wave file.
|
---|
1499 | */
|
---|
1500 | void AudioTestWaveFileClose(PAUDIOTESTWAVEFILE pWaveFile)
|
---|
1501 | {
|
---|
1502 | RTFileClose(pWaveFile->hFile);
|
---|
1503 | pWaveFile->hFile = NIL_RTFILE;
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | /**
|
---|
1507 | * Reads samples from a wave file.
|
---|
1508 | *
|
---|
1509 | * @returns VBox status code. See RTVfsFileRead for EOF status handling.
|
---|
1510 | * @param pWaveFile The file to read from.
|
---|
1511 | * @param pvBuf Where to put the samples.
|
---|
1512 | * @param cbBuf How much to read at most.
|
---|
1513 | * @param pcbRead Where to return the actual number of bytes read,
|
---|
1514 | * optional.
|
---|
1515 | */
|
---|
1516 | int AudioTestWaveFileRead(PAUDIOTESTWAVEFILE pWaveFile, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
1517 | {
|
---|
1518 | bool fEofAdjusted;
|
---|
1519 | if (pWaveFile->offCur + cbBuf <= pWaveFile->cbSamples)
|
---|
1520 | fEofAdjusted = false;
|
---|
1521 | else if (pcbRead)
|
---|
1522 | {
|
---|
1523 | fEofAdjusted = true;
|
---|
1524 | cbBuf = pWaveFile->cbSamples - pWaveFile->offCur;
|
---|
1525 | }
|
---|
1526 | else
|
---|
1527 | return VERR_EOF;
|
---|
1528 |
|
---|
1529 | int rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples + pWaveFile->offCur, pvBuf, cbBuf, pcbRead);
|
---|
1530 | if (RT_SUCCESS(rc))
|
---|
1531 | {
|
---|
1532 | if (pcbRead)
|
---|
1533 | {
|
---|
1534 | pWaveFile->offCur += (uint32_t)*pcbRead;
|
---|
1535 | if (fEofAdjusted || cbBuf > *pcbRead)
|
---|
1536 | rc = VINF_EOF;
|
---|
1537 | else if (!cbBuf && pWaveFile->offCur == pWaveFile->cbSamples)
|
---|
1538 | rc = VINF_EOF;
|
---|
1539 | }
|
---|
1540 | else
|
---|
1541 | pWaveFile->offCur += (uint32_t)cbBuf;
|
---|
1542 | }
|
---|
1543 | return rc;
|
---|
1544 | }
|
---|
1545 |
|
---|