VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioTest.cpp@ 91531

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

Audio/Validation Kit: Initial implementation of two-pass audio data normalization, which is done before working on the data. Also, a search window has been added when treating / classifying audio data [build fix]. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 109.4 KB
Line 
1/* $Id: AudioTest.cpp 91531 2021-10-01 20:48:53Z vboxsync $ */
2/** @file
3 * Audio testing routines.
4 *
5 * Common code which is being used by the ValidationKit and the
6 * debug / ValdikationKit audio driver(s).
7 */
8
9/*
10 * Copyright (C) 2021 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21
22/*********************************************************************************************************************************
23* Header Files *
24*********************************************************************************************************************************/
25#include <package-generated.h>
26#include "product-generated.h"
27
28#include <iprt/buildconfig.h>
29#include <iprt/cdefs.h>
30#include <iprt/dir.h>
31#include <iprt/env.h>
32#include <iprt/file.h>
33#include <iprt/formats/riff.h>
34#include <iprt/inifile.h>
35#include <iprt/list.h>
36#include <iprt/message.h> /** @todo Get rid of this once we have own log hooks. */
37#include <iprt/rand.h>
38#include <iprt/stream.h>
39#include <iprt/system.h>
40#include <iprt/uuid.h>
41#include <iprt/vfs.h>
42#include <iprt/zip.h>
43
44#define _USE_MATH_DEFINES
45#include <math.h> /* sin, M_PI */
46
47#define LOG_GROUP LOG_GROUP_AUDIO_TEST
48#include <VBox/log.h>
49
50#include <VBox/version.h>
51#include <VBox/vmm/pdmaudioifs.h>
52#include <VBox/vmm/pdmaudioinline.h>
53
54#include "AudioTest.h"
55
56
57/*********************************************************************************************************************************
58* Defines *
59*********************************************************************************************************************************/
60/** The test manifest file name. */
61#define AUDIOTEST_MANIFEST_FILE_STR "vkat_manifest.ini"
62/** The current test manifest version. */
63#define AUDIOTEST_MANIFEST_VER 1
64/** Audio test archive default suffix.
65 * According to IPRT terminology this always contains the dot. */
66#define AUDIOTEST_ARCHIVE_SUFF_STR ".tar.gz"
67
68/** Test manifest header name. */
69#define AUDIOTEST_SEC_HDR_STR "header"
70/** Maximum section name length (in UTF-8 characters). */
71#define AUDIOTEST_MAX_SEC_LEN 128
72/** Maximum object name length (in UTF-8 characters). */
73#define AUDIOTEST_MAX_OBJ_LEN 128
74
75
76/*********************************************************************************************************************************
77* Structures and Typedefs *
78*********************************************************************************************************************************/
79/**
80 * Enumeration for an audio test object type.
81 */
82typedef enum AUDIOTESTOBJTYPE
83{
84 /** Unknown / invalid, do not use. */
85 AUDIOTESTOBJTYPE_UNKNOWN = 0,
86 /** The test object is a file. */
87 AUDIOTESTOBJTYPE_FILE,
88 /** The usual 32-bit hack. */
89 AUDIOTESTOBJTYPE_32BIT_HACK = 0x7fffffff
90} AUDIOTESTOBJTYPE;
91
92/**
93 * Structure for keeping an audio test object file.
94 */
95typedef struct AUDIOTESTOBJFILE
96{
97 /** File handle. */
98 RTFILE hFile;
99 /** Total size (in bytes). */
100 size_t cbSize;
101} AUDIOTESTOBJFILE;
102/** Pointer to an audio test object file. */
103typedef AUDIOTESTOBJFILE *PAUDIOTESTOBJFILE;
104
105/**
106 * Enumeration for an audio test object meta data type.
107 */
108typedef enum AUDIOTESTOBJMETADATATYPE
109{
110 /** Unknown / invalid, do not use. */
111 AUDIOTESTOBJMETADATATYPE_INVALID = 0,
112 /** Meta data is an UTF-8 string. */
113 AUDIOTESTOBJMETADATATYPE_STRING,
114 /** The usual 32-bit hack. */
115 AUDIOTESTOBJMETADATATYPE_32BIT_HACK = 0x7fffffff
116} AUDIOTESTOBJMETADATATYPE;
117
118/**
119 * Structure for keeping a meta data block.
120 */
121typedef struct AUDIOTESTOBJMETA
122{
123 /** List node. */
124 RTLISTNODE Node;
125 /** Meta data type. */
126 AUDIOTESTOBJMETADATATYPE enmType;
127 /** Meta data block. */
128 void *pvMeta;
129 /** Size (in bytes) of \a pvMeta. */
130 size_t cbMeta;
131} AUDIOTESTOBJMETA;
132/** Pointer to an audio test object file. */
133typedef AUDIOTESTOBJMETA *PAUDIOTESTOBJMETA;
134
135/**
136 * Structure for keeping a single audio test object.
137 *
138 * A test object is data which is needed in order to perform and verify one or
139 * more audio test case(s).
140 */
141typedef struct AUDIOTESTOBJINT
142{
143 /** List node. */
144 RTLISTNODE Node;
145 /** Pointer to test set this handle is bound to. */
146 PAUDIOTESTSET pSet;
147 /** As we only support .INI-style files for now, this only has the object's section name in it. */
148 /** @todo Make this more generic (union, ++). */
149 char szSec[AUDIOTEST_MAX_SEC_LEN];
150 /** The UUID of the object.
151 * Used to identify an object within a test set. */
152 RTUUID Uuid;
153 /** Number of references to this test object. */
154 uint32_t cRefs;
155 /** Name of the test object.
156 * Must not contain a path and has to be able to serialize to disk. */
157 char szName[64];
158 /** The object type. */
159 AUDIOTESTOBJTYPE enmType;
160 /** Meta data list. */
161 RTLISTANCHOR lstMeta;
162 /** Union for holding the object type-specific data. */
163 union
164 {
165 AUDIOTESTOBJFILE File;
166 };
167} AUDIOTESTOBJINT;
168/** Pointer to an audio test object. */
169typedef AUDIOTESTOBJINT *PAUDIOTESTOBJINT;
170
171/**
172 * Structure for keeping an audio test verification job.
173 */
174typedef struct AUDIOTESTVERIFYJOB
175{
176 /** Pointer to set A. */
177 PAUDIOTESTSET pSetA;
178 /** Pointer to set B. */
179 PAUDIOTESTSET pSetB;
180 /** Pointer to the error description to use. */
181 PAUDIOTESTERRORDESC pErr;
182 /** Zero-based index of current test being verified. */
183 uint32_t idxTest;
184 /** The verification options to use. */
185 AUDIOTESTVERIFYOPTS Opts;
186 /** PCM properties to use for verification. */
187 PDMAUDIOPCMPROPS PCMProps;
188} AUDIOTESTVERIFYJOB;
189/** Pointer to an audio test verification job. */
190typedef AUDIOTESTVERIFYJOB *PAUDIOTESTVERIFYJOB;
191
192
193/*********************************************************************************************************************************
194* Global Variables *
195*********************************************************************************************************************************/
196/** Well-known frequency selection test tones. */
197static const double s_aAudioTestToneFreqsHz[] =
198{
199 349.2282 /*F4*/,
200 440.0000 /*A4*/,
201 523.2511 /*C5*/,
202 698.4565 /*F5*/,
203 880.0000 /*A5*/,
204 1046.502 /*C6*/,
205 1174.659 /*D6*/,
206 1396.913 /*F6*/,
207 1760.0000 /*A6*/
208};
209
210
211/*********************************************************************************************************************************
212* Internal Functions *
213*********************************************************************************************************************************/
214static int audioTestObjClose(PAUDIOTESTOBJINT pObj);
215static void audioTestObjFinalize(PAUDIOTESTOBJINT pObj);
216static void audioTestObjInit(PAUDIOTESTOBJINT pObj);
217static bool audioTestObjIsOpen(PAUDIOTESTOBJINT pObj);
218
219
220/**
221 * Initializes a test tone with a specific frequency (in Hz).
222 *
223 * @returns Used tone frequency (in Hz).
224 * @param pTone Pointer to test tone to initialize.
225 * @param pProps PCM properties to use for the test tone.
226 * @param dbFreq Frequency (in Hz) to initialize tone with.
227 * When set to 0.0, a random frequency will be chosen.
228 */
229double AudioTestToneInit(PAUDIOTESTTONE pTone, PPDMAUDIOPCMPROPS pProps, double dbFreq)
230{
231 if (dbFreq == 0.0)
232 dbFreq = AudioTestToneGetRandomFreq();
233
234 pTone->rdFreqHz = dbFreq;
235 pTone->rdFixed = 2.0 * M_PI * pTone->rdFreqHz / PDMAudioPropsHz(pProps);
236 pTone->uSample = 0;
237
238 memcpy(&pTone->Props, pProps, sizeof(PDMAUDIOPCMPROPS));
239
240 pTone->enmType = AUDIOTESTTONETYPE_SINE; /* Only type implemented so far. */
241
242 return dbFreq;
243}
244
245/**
246 * Initializes a test tone by picking a random but well-known frequency (in Hz).
247 *
248 * @returns Randomly picked tone frequency (in Hz).
249 * @param pTone Pointer to test tone to initialize.
250 * @param pProps PCM properties to use for the test tone.
251 */
252double AudioTestToneInitRandom(PAUDIOTESTTONE pTone, PPDMAUDIOPCMPROPS pProps)
253{
254 return AudioTestToneInit(pTone, pProps,
255 /* Pick a frequency from our selection, so that every time a recording starts
256 * we'll hopfully generate a different note. */
257 0.0);
258}
259
260/**
261 * Writes (and iterates) a given test tone to an output buffer.
262 *
263 * @returns VBox status code.
264 * @param pTone Pointer to test tone to write.
265 * @param pvBuf Pointer to output buffer to write test tone to.
266 * @param cbBuf Size (in bytes) of output buffer.
267 * @param pcbWritten How many bytes were written on success.
268 */
269int AudioTestToneGenerate(PAUDIOTESTTONE pTone, void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
270{
271 /*
272 * Clear the buffer first so we don't need to think about additional channels.
273 */
274 uint32_t cFrames = PDMAudioPropsBytesToFrames(&pTone->Props, cbBuf);
275
276 /* Input cbBuf not necessarily is aligned to the frames, so re-calculate it. */
277 const uint32_t cbToWrite = PDMAudioPropsFramesToBytes(&pTone->Props, cFrames);
278
279 PDMAudioPropsClearBuffer(&pTone->Props, pvBuf, cbBuf, cFrames);
280
281 /*
282 * Generate the select sin wave in the first channel:
283 */
284 uint32_t const cbFrame = PDMAudioPropsFrameSize(&pTone->Props);
285 double const rdFixed = pTone->rdFixed;
286 uint64_t iSrcFrame = pTone->uSample;
287 switch (PDMAudioPropsSampleSize(&pTone->Props))
288 {
289 case 1:
290 /* untested */
291 if (PDMAudioPropsIsSigned(&pTone->Props))
292 {
293 int8_t *piSample = (int8_t *)pvBuf;
294 while (cFrames-- > 0)
295 {
296 *piSample = (int8_t)(126 /*Amplitude*/ * sin(rdFixed * iSrcFrame));
297 iSrcFrame++;
298 piSample += cbFrame;
299 }
300 }
301 else
302 {
303 /* untested */
304 uint8_t *pbSample = (uint8_t *)pvBuf;
305 while (cFrames-- > 0)
306 {
307 *pbSample = (uint8_t)(126 /*Amplitude*/ * sin(rdFixed * iSrcFrame) + 0x80);
308 iSrcFrame++;
309 pbSample += cbFrame;
310 }
311 }
312 break;
313
314 case 2:
315 if (PDMAudioPropsIsSigned(&pTone->Props))
316 {
317 int16_t *piSample = (int16_t *)pvBuf;
318 while (cFrames-- > 0)
319 {
320 *piSample = (int16_t)(32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame));
321 iSrcFrame++;
322 piSample = (int16_t *)((uint8_t *)piSample + cbFrame);
323 }
324 }
325 else
326 {
327 /* untested */
328 uint16_t *puSample = (uint16_t *)pvBuf;
329 while (cFrames-- > 0)
330 {
331 *puSample = (uint16_t)(32760 /*Amplitude*/ * sin(rdFixed * iSrcFrame) + 0x8000);
332 iSrcFrame++;
333 puSample = (uint16_t *)((uint8_t *)puSample + cbFrame);
334 }
335 }
336 break;
337
338 case 4:
339 /* untested */
340 if (PDMAudioPropsIsSigned(&pTone->Props))
341 {
342 int32_t *piSample = (int32_t *)pvBuf;
343 while (cFrames-- > 0)
344 {
345 *piSample = (int32_t)((32760 << 16) /*Amplitude*/ * sin(rdFixed * iSrcFrame));
346 iSrcFrame++;
347 piSample = (int32_t *)((uint8_t *)piSample + cbFrame);
348 }
349 }
350 else
351 {
352 uint32_t *puSample = (uint32_t *)pvBuf;
353 while (cFrames-- > 0)
354 {
355 *puSample = (uint32_t)((32760 << 16) /*Amplitude*/ * sin(rdFixed * iSrcFrame) + UINT32_C(0x80000000));
356 iSrcFrame++;
357 puSample = (uint32_t *)((uint8_t *)puSample + cbFrame);
358 }
359 }
360 break;
361
362 default:
363 AssertFailedReturn(VERR_NOT_SUPPORTED);
364 }
365
366 pTone->uSample = iSrcFrame;
367
368 if (pcbWritten)
369 *pcbWritten = cbToWrite;
370
371 return VINF_SUCCESS;
372}
373
374/**
375 * Returns a random test tone frequency.
376 */
377double AudioTestToneGetRandomFreq(void)
378{
379 return s_aAudioTestToneFreqsHz[RTRandU32Ex(0, RT_ELEMENTS(s_aAudioTestToneFreqsHz) - 1)];
380}
381
382/**
383 * Finds the next audible *or* silent audio sample and returns its offset.
384 *
385 * @returns Offset (in bytes) of the next found sample, or UINT64_MAX if not found / invalid parameters.
386 * @param hFile File handle of file to search in.
387 * @param fFindSilence Whether to search for a silent sample or not (i.e. audible).
388 * What a silent sample is depends on \a pToneParms PCM parameters.
389 * @param uOff Absolute offset (in bytes) to start searching from.
390 * @param pToneParms Tone parameters to use.
391 * @param cbWindow Search window size (in bytes).
392 */
393static uint64_t audioTestToneFileFind(RTFILE hFile, bool fFindSilence, uint64_t uOff, PAUDIOTESTTONEPARMS pToneParms,
394 size_t cbWindow)
395{
396 int rc = RTFileSeek(hFile, uOff, RTFILE_SEEK_BEGIN, NULL);
397 AssertRCReturn(rc, UINT64_MAX);
398
399 uint64_t offFound = 0;
400 uint8_t abBuf[_64K];
401
402 size_t const cbFrame = PDMAudioPropsFrameSize(&pToneParms->Props);
403 AssertReturn(cbFrame, UINT64_MAX);
404
405 AssertReturn(PDMAudioPropsIsSizeAligned(&pToneParms->Props, (uint32_t)cbWindow), UINT64_MAX);
406
407 size_t cbRead;
408 for (;;)
409 {
410 rc = RTFileRead(hFile, &abBuf, RT_MIN(cbWindow, sizeof(abBuf)), &cbRead);
411 if ( RT_FAILURE(rc)
412 || !cbRead)
413 break;
414
415 AssertReturn(PDMAudioPropsIsSizeAligned(&pToneParms->Props, (uint32_t)cbRead), UINT64_MAX);
416 AssertReturn(cbRead % cbFrame == 0, UINT64_MAX);
417
418 /** @todo Do we need to have a sliding window here? */
419
420 for (size_t i = 0; i < cbRead; i += cbWindow) /** @todo Slow as heck, but works for now. */
421 {
422 bool const fIsSilence = PDMAudioPropsIsBufferSilence(&pToneParms->Props, (const uint8_t *)abBuf + i, cbWindow);
423 if (fIsSilence != fFindSilence)
424 return offFound;
425 offFound += cbWindow;
426 }
427 }
428
429 AssertReturn(PDMAudioPropsIsSizeAligned(&pToneParms->Props, offFound), 0);
430 return UINT64_MAX;
431}
432
433/**
434 * Generates a tag.
435 *
436 * @returns VBox status code.
437 * @param pszTag The output buffer.
438 * @param cbTag The size of the output buffer.
439 * AUDIOTEST_TAG_MAX is a good size.
440 */
441int AudioTestGenTag(char *pszTag, size_t cbTag)
442{
443 RTUUID UUID;
444 int rc = RTUuidCreate(&UUID);
445 AssertRCReturn(rc, rc);
446 rc = RTUuidToStr(&UUID, pszTag, cbTag);
447 AssertRCReturn(rc, rc);
448 return rc;
449}
450
451/**
452 * Return the tag to use in the given buffer, generating one if needed.
453 *
454 * @returns VBox status code.
455 * @param pszTag The output buffer.
456 * @param cbTag The size of the output buffer.
457 * AUDIOTEST_TAG_MAX is a good size.
458 * @param pszTagUser User specified tag, optional.
459 */
460static int audioTestCopyOrGenTag(char *pszTag, size_t cbTag, const char *pszTagUser)
461{
462 if (pszTagUser && *pszTagUser)
463 return RTStrCopy(pszTag, cbTag, pszTagUser);
464 return AudioTestGenTag(pszTag, cbTag);
465}
466
467
468/**
469 * Creates a new path (directory) for a specific audio test set tag.
470 *
471 * @returns VBox status code.
472 * @param pszPath On input, specifies the absolute base path where to create the test set path.
473 * On output this specifies the absolute path created.
474 * @param cbPath Size (in bytes) of \a pszPath.
475 * @param pszTag Tag to use for path creation.
476 *
477 * @note Can be used multiple times with the same tag; a sub directory with an ISO time string will be used
478 * on each call.
479 */
480int AudioTestPathCreate(char *pszPath, size_t cbPath, const char *pszTag)
481{
482 char szTag[AUDIOTEST_TAG_MAX];
483 int rc = audioTestCopyOrGenTag(szTag, sizeof(szTag), pszTag);
484 AssertRCReturn(rc, rc);
485
486 char szName[RT_ELEMENTS(AUDIOTEST_PATH_PREFIX_STR) + AUDIOTEST_TAG_MAX + 4];
487 if (RTStrPrintf2(szName, sizeof(szName), "%s-%s", AUDIOTEST_PATH_PREFIX_STR, szTag) < 0)
488 AssertFailedReturn(VERR_BUFFER_OVERFLOW);
489
490 rc = RTPathAppend(pszPath, cbPath, szName);
491 AssertRCReturn(rc, rc);
492
493#ifndef DEBUG /* Makes debugging easier to have a deterministic directory. */
494 char szTime[64];
495 RTTIMESPEC time;
496 if (!RTTimeSpecToString(RTTimeNow(&time), szTime, sizeof(szTime)))
497 return VERR_BUFFER_UNDERFLOW;
498
499 /* Colons aren't allowed in windows filenames, so change to dashes. */
500 char *pszColon;
501 while ((pszColon = strchr(szTime, ':')) != NULL)
502 *pszColon = '-';
503
504 rc = RTPathAppend(pszPath, cbPath, szTime);
505 AssertRCReturn(rc, rc);
506#endif
507
508 return RTDirCreateFullPath(pszPath, RTFS_UNIX_IRWXU);
509}
510
511DECLINLINE(int) audioTestManifestWriteData(PAUDIOTESTSET pSet, const void *pvData, size_t cbData)
512{
513 /** @todo Use RTIniFileWrite once its implemented. */
514 return RTFileWrite(pSet->f.hFile, pvData, cbData, NULL);
515}
516
517/**
518 * Writes string data to a test set manifest.
519 *
520 * @returns VBox status code.
521 * @param pSet Test set to write manifest for.
522 * @param pszFormat Format string to write.
523 * @param args Variable arguments for \a pszFormat.
524 */
525static int audioTestManifestWriteV(PAUDIOTESTSET pSet, const char *pszFormat, va_list args)
526{
527 /** @todo r=bird: Use RTStrmOpen + RTStrmPrintf instead of this slow
528 * do-it-all-yourself stuff. */
529 char *psz = NULL;
530 if (RTStrAPrintfV(&psz, pszFormat, args) == -1)
531 return VERR_NO_MEMORY;
532 AssertPtrReturn(psz, VERR_NO_MEMORY);
533
534 int rc = audioTestManifestWriteData(pSet, psz, strlen(psz));
535 AssertRC(rc);
536
537 RTStrFree(psz);
538
539 return rc;
540}
541
542/**
543 * Writes a string to a test set manifest.
544 * Convenience function.
545 *
546 * @returns VBox status code.
547 * @param pSet Test set to write manifest for.
548 * @param pszFormat Format string to write.
549 * @param ... Variable arguments for \a pszFormat. Optional.
550 */
551static int audioTestManifestWrite(PAUDIOTESTSET pSet, const char *pszFormat, ...)
552{
553 va_list va;
554 va_start(va, pszFormat);
555
556 int rc = audioTestManifestWriteV(pSet, pszFormat, va);
557 AssertRC(rc);
558
559 va_end(va);
560
561 return rc;
562}
563
564/**
565 * Returns the current read/write offset (in bytes) of the opened manifest file.
566 *
567 * @returns Current read/write offset (in bytes).
568 * @param pSet Set to return offset for.
569 * Must have an opened manifest file.
570 */
571DECLINLINE(uint64_t) audioTestManifestGetOffsetAbs(PAUDIOTESTSET pSet)
572{
573 AssertReturn(RTFileIsValid(pSet->f.hFile), 0);
574 return RTFileTell(pSet->f.hFile);
575}
576
577/**
578 * Writes a section header to a test set manifest.
579 *
580 * @returns VBox status code.
581 * @param pSet Test set to write manifest for.
582 * @param pszSection Format string of section to write.
583 * @param ... Variable arguments for \a pszSection. Optional.
584 */
585static int audioTestManifestWriteSectionHdr(PAUDIOTESTSET pSet, const char *pszSection, ...)
586{
587 va_list va;
588 va_start(va, pszSection);
589
590 /** @todo Keep it as simple as possible for now. Improve this later. */
591 int rc = audioTestManifestWrite(pSet, "[%N]\n", pszSection, &va);
592
593 va_end(va);
594
595 return rc;
596}
597
598/**
599 * Initializes an audio test set, internal function.
600 *
601 * @param pSet Test set to initialize.
602 */
603static void audioTestSetInitInternal(PAUDIOTESTSET pSet)
604{
605 pSet->f.hFile = NIL_RTFILE;
606
607 RTListInit(&pSet->lstObj);
608 pSet->cObj = 0;
609
610 RTListInit(&pSet->lstTest);
611 pSet->cTests = 0;
612 pSet->cTestsRunning = 0;
613 pSet->offTestCount = 0;
614 pSet->pTestCur = NULL;
615 pSet->cObj = 0;
616 pSet->offObjCount = 0;
617 pSet->cTotalFailures = 0;
618}
619
620/**
621 * Returns whether a test set's manifest file is open (and thus ready) or not.
622 *
623 * @returns \c true if open (and ready), or \c false if not.
624 * @retval VERR_
625 * @param pSet Test set to return open status for.
626 */
627static bool audioTestManifestIsOpen(PAUDIOTESTSET pSet)
628{
629 if ( pSet->enmMode == AUDIOTESTSETMODE_TEST
630 && pSet->f.hFile != NIL_RTFILE)
631 return true;
632 else if ( pSet->enmMode == AUDIOTESTSETMODE_VERIFY
633 && pSet->f.hIniFile != NIL_RTINIFILE)
634 return true;
635
636 return false;
637}
638
639/**
640 * Initializes an audio test error description.
641 *
642 * @param pErr Test error description to initialize.
643 */
644static void audioTestErrorDescInit(PAUDIOTESTERRORDESC pErr)
645{
646 RTListInit(&pErr->List);
647 pErr->cErrors = 0;
648}
649
650/**
651 * Destroys an audio test error description.
652 *
653 * @param pErr Test error description to destroy.
654 */
655void AudioTestErrorDescDestroy(PAUDIOTESTERRORDESC pErr)
656{
657 if (!pErr)
658 return;
659
660 PAUDIOTESTERRORENTRY pErrEntry, pErrEntryNext;
661 RTListForEachSafe(&pErr->List, pErrEntry, pErrEntryNext, AUDIOTESTERRORENTRY, Node)
662 {
663 RTListNodeRemove(&pErrEntry->Node);
664
665 RTMemFree(pErrEntry);
666 }
667
668 pErr->cErrors = 0;
669}
670
671/**
672 * Returns the the number of errors of an audio test error description.
673 *
674 * @returns Error count.
675 * @param pErr Test error description to return error count for.
676 */
677uint32_t AudioTestErrorDescCount(PCAUDIOTESTERRORDESC pErr)
678{
679 return pErr->cErrors;
680}
681
682/**
683 * Returns if an audio test error description contains any errors or not.
684 *
685 * @returns \c true if it contains errors, or \c false if not.
686 * @param pErr Test error description to return error status for.
687 */
688bool AudioTestErrorDescFailed(PCAUDIOTESTERRORDESC pErr)
689{
690 if (pErr->cErrors)
691 {
692 Assert(!RTListIsEmpty(&pErr->List));
693 return true;
694 }
695
696 return false;
697}
698
699/**
700 * Adds a single error entry to an audio test error description, va_list version.
701 *
702 * @returns VBox status code.
703 * @param pErr Test error description to add entry for.
704 * @param idxTest Index of failing test (zero-based).
705 * @param rc Result code of entry to add.
706 * @param pszFormat Error description format string to add.
707 * @param va Optional format arguments of \a pszDesc to add.
708 */
709static int audioTestErrorDescAddV(PAUDIOTESTERRORDESC pErr, uint32_t idxTest, int rc, const char *pszFormat, va_list va)
710{
711 PAUDIOTESTERRORENTRY pEntry = (PAUDIOTESTERRORENTRY)RTMemAlloc(sizeof(AUDIOTESTERRORENTRY));
712 AssertPtrReturn(pEntry, VERR_NO_MEMORY);
713
714 char *pszDescTmp;
715 if (RTStrAPrintfV(&pszDescTmp, pszFormat, va) < 0)
716 AssertFailedReturn(VERR_NO_MEMORY);
717
718 const ssize_t cch = RTStrPrintf2(pEntry->szDesc, sizeof(pEntry->szDesc), "Test #%RU32 %s: %s",
719 idxTest, RT_FAILURE(rc) ? "failed" : "info", pszDescTmp);
720 RTStrFree(pszDescTmp);
721 AssertReturn(cch > 0, VERR_BUFFER_OVERFLOW);
722
723 pEntry->rc = rc;
724
725 RTListAppend(&pErr->List, &pEntry->Node);
726
727 if (RT_FAILURE(rc))
728 pErr->cErrors++;
729
730 return VINF_SUCCESS;
731}
732
733/**
734 * Adds a single error entry to an audio test error description.
735 *
736 * @returns VBox status code.
737 * @param pErr Test error description to add entry for.
738 * @param idxTest Index of failing test (zero-based).
739 * @param pszFormat Error description format string to add.
740 * @param ... Optional format arguments of \a pszDesc to add.
741 */
742static int audioTestErrorDescAddError(PAUDIOTESTERRORDESC pErr, uint32_t idxTest, const char *pszFormat, ...)
743{
744 va_list va;
745 va_start(va, pszFormat);
746
747 int rc = audioTestErrorDescAddV(pErr, idxTest, VERR_GENERAL_FAILURE /** @todo Fudge! */, pszFormat, va);
748
749 va_end(va);
750 return rc;
751}
752
753/**
754 * Adds a single info entry to an audio test error description, va_list version.
755 *
756 * @returns VBox status code.
757 * @param pErr Test error description to add entry for.
758 * @param idxTest Index of failing test (zero-based).
759 * @param pszFormat Error description format string to add.
760 * @param ... Optional format arguments of \a pszDesc to add.
761 */
762static int audioTestErrorDescAddInfo(PAUDIOTESTERRORDESC pErr, uint32_t idxTest, const char *pszFormat, ...)
763{
764 va_list va;
765 va_start(va, pszFormat);
766
767 int rc = audioTestErrorDescAddV(pErr, idxTest, VINF_SUCCESS, pszFormat, va);
768
769 va_end(va);
770 return rc;
771}
772
773#if 0
774static int audioTestErrorDescAddRc(PAUDIOTESTERRORDESC pErr, int rc, const char *pszFormat, ...)
775{
776 va_list va;
777 va_start(va, pszFormat);
778
779 int rc2 = audioTestErrorDescAddV(pErr, rc, pszFormat, va);
780
781 va_end(va);
782 return rc2;
783}
784#endif
785
786/**
787 * Retrieves the temporary directory.
788 *
789 * @returns VBox status code.
790 * @param pszPath Where to return the absolute path of the created directory on success.
791 * @param cbPath Size (in bytes) of \a pszPath.
792 */
793int AudioTestPathGetTemp(char *pszPath, size_t cbPath)
794{
795 int rc = RTEnvGetEx(RTENV_DEFAULT, "TESTBOX_PATH_SCRATCH", pszPath, cbPath, NULL);
796 if (RT_FAILURE(rc))
797 {
798 rc = RTPathTemp(pszPath, cbPath);
799 AssertRCReturn(rc, rc);
800 }
801
802 return rc;
803}
804
805/**
806 * Creates a new temporary directory with a specific (test) tag.
807 *
808 * @returns VBox status code.
809 * @param pszPath Where to return the absolute path of the created directory on success.
810 * @param cbPath Size (in bytes) of \a pszPath.
811 * @param pszTag Tag name to use for directory creation.
812 *
813 * @note Can be used multiple times with the same tag; a sub directory with an ISO time string will be used
814 * on each call.
815 */
816int AudioTestPathCreateTemp(char *pszPath, size_t cbPath, const char *pszTag)
817{
818 AssertReturn(pszTag && strlen(pszTag) <= AUDIOTEST_TAG_MAX, VERR_INVALID_PARAMETER);
819
820 char szTemp[RTPATH_MAX];
821 int rc = AudioTestPathGetTemp(szTemp, sizeof(szTemp));
822 AssertRCReturn(rc, rc);
823
824 rc = AudioTestPathCreate(szTemp, sizeof(szTemp), pszTag);
825 AssertRCReturn(rc, rc);
826
827 return RTStrCopy(pszPath, cbPath, szTemp);
828}
829
830/**
831 * Gets a value as string.
832 *
833 * @returns VBox status code.
834 * @param pObj Object handle to get value for.
835 * @param pszKey Key to get value from.
836 * @param pszVal Where to return the value on success.
837 * @param cbVal Size (in bytes) of \a pszVal.
838 */
839static int audioTestObjGetStr(PAUDIOTESTOBJINT pObj, const char *pszKey, char *pszVal, size_t cbVal)
840{
841 /** @todo For now we only support .INI-style files. */
842 AssertPtrReturn(pObj->pSet, VERR_WRONG_ORDER);
843 return RTIniFileQueryValue(pObj->pSet->f.hIniFile, pObj->szSec, pszKey, pszVal, cbVal, NULL);
844}
845
846/**
847 * Gets a value as boolean.
848 *
849 * @returns VBox status code.
850 * @param pObj Object handle to get value for.
851 * @param pszKey Key to get value from.
852 * @param pbVal Where to return the value on success.
853 */
854static int audioTestObjGetBool(PAUDIOTESTOBJINT pObj, const char *pszKey, bool *pbVal)
855{
856 char szVal[_1K];
857 int rc = audioTestObjGetStr(pObj, pszKey, szVal, sizeof(szVal));
858 if (RT_SUCCESS(rc))
859 *pbVal = (RTStrICmp(szVal, "true") == 0)
860 || (RTStrICmp(szVal, "1") == 0) ? true : false;
861
862 return rc;
863}
864
865/**
866 * Gets a value as uint8_t.
867 *
868 * @returns VBox status code.
869 * @param pObj Object handle to get value for.
870 * @param pszKey Key to get value from.
871 * @param puVal Where to return the value on success.
872 */
873static int audioTestObjGetUInt8(PAUDIOTESTOBJINT pObj, const char *pszKey, uint8_t *puVal)
874{
875 char szVal[_1K];
876 int rc = audioTestObjGetStr(pObj, pszKey, szVal, sizeof(szVal));
877 if (RT_SUCCESS(rc))
878 *puVal = RTStrToUInt8(szVal);
879
880 return rc;
881}
882
883/**
884 * Gets a value as uint32_t.
885 *
886 * @returns VBox status code.
887 * @param pObj Object handle to get value for.
888 * @param pszKey Key to get value from.
889 * @param puVal Where to return the value on success.
890 */
891static int audioTestObjGetUInt32(PAUDIOTESTOBJINT pObj, const char *pszKey, uint32_t *puVal)
892{
893 char szVal[_1K];
894 int rc = audioTestObjGetStr(pObj, pszKey, szVal, sizeof(szVal));
895 if (RT_SUCCESS(rc))
896 *puVal = RTStrToUInt32(szVal);
897
898 return rc;
899}
900
901/**
902 * Returns the absolute path of a given audio test set object.
903 *
904 * @returns VBox status code.
905 * @param pSet Test set the object contains.
906 * @param pszPathAbs Where to return the absolute path on success.
907 * @param cbPathAbs Size (in bytes) of \a pszPathAbs.
908 * @param pszObjName Name of the object to create absolute path for.
909 */
910DECLINLINE(int) audioTestSetGetObjPath(PAUDIOTESTSET pSet, char *pszPathAbs, size_t cbPathAbs, const char *pszObjName)
911{
912 return RTPathJoin(pszPathAbs, cbPathAbs, pSet->szPathAbs, pszObjName);
913}
914
915/**
916 * Returns the tag of a test set.
917 *
918 * @returns Test set tag.
919 * @param pSet Test set to return tag for.
920 */
921const char *AudioTestSetGetTag(PAUDIOTESTSET pSet)
922{
923 return pSet->szTag;
924}
925
926/**
927 * Returns the total number of registered tests.
928 *
929 * @returns Total number of registered tests.
930 * @param pSet Test set to return value for.
931 */
932uint32_t AudioTestSetGetTestsTotal(PAUDIOTESTSET pSet)
933{
934 return pSet->cTests;
935}
936
937/**
938 * Returns the total number of (still) running tests.
939 *
940 * @returns Total number of (still) running tests.
941 * @param pSet Test set to return value for.
942 */
943uint32_t AudioTestSetGetTestsRunning(PAUDIOTESTSET pSet)
944{
945 return pSet->cTestsRunning;
946}
947
948/**
949 * Returns the total number of test failures occurred.
950 *
951 * @returns Total number of test failures occurred.
952 * @param pSet Test set to return value for.
953 */
954uint32_t AudioTestSetGetTotalFailures(PAUDIOTESTSET pSet)
955{
956 return pSet->cTotalFailures;
957}
958
959/**
960 * Creates a new audio test set.
961 *
962 * @returns VBox status code.
963 * @param pSet Test set to create.
964 * @param pszPath Where to store the set set data. If NULL, the
965 * temporary directory will be used.
966 * @param pszTag Tag name to use for this test set.
967 */
968int AudioTestSetCreate(PAUDIOTESTSET pSet, const char *pszPath, const char *pszTag)
969{
970 audioTestSetInitInternal(pSet);
971
972 int rc = audioTestCopyOrGenTag(pSet->szTag, sizeof(pSet->szTag), pszTag);
973 AssertRCReturn(rc, rc);
974
975 /*
976 * Test set directory.
977 */
978 if (pszPath)
979 {
980 rc = RTPathAbs(pszPath, pSet->szPathAbs, sizeof(pSet->szPathAbs));
981 AssertRCReturn(rc, rc);
982
983 rc = AudioTestPathCreate(pSet->szPathAbs, sizeof(pSet->szPathAbs), pSet->szTag);
984 }
985 else
986 rc = AudioTestPathCreateTemp(pSet->szPathAbs, sizeof(pSet->szPathAbs), pSet->szTag);
987 AssertRCReturn(rc, rc);
988
989 /*
990 * Create the manifest file.
991 */
992 char szTmp[RTPATH_MAX];
993 rc = RTPathJoin(szTmp, sizeof(szTmp), pSet->szPathAbs, AUDIOTEST_MANIFEST_FILE_STR);
994 AssertRCReturn(rc, rc);
995
996 rc = RTFileOpen(&pSet->f.hFile, szTmp, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
997 AssertRCReturn(rc, rc);
998
999 rc = audioTestManifestWriteSectionHdr(pSet, "header");
1000 AssertRCReturn(rc, rc);
1001
1002 rc = audioTestManifestWrite(pSet, "magic=vkat_ini\n"); /* VKAT Manifest, .INI-style. */
1003 AssertRCReturn(rc, rc);
1004 rc = audioTestManifestWrite(pSet, "ver=%d\n", AUDIOTEST_MANIFEST_VER);
1005 AssertRCReturn(rc, rc);
1006 rc = audioTestManifestWrite(pSet, "tag=%s\n", pSet->szTag);
1007 AssertRCReturn(rc, rc);
1008
1009 AssertCompile(sizeof(szTmp) > RTTIME_STR_LEN);
1010 RTTIMESPEC Now;
1011 rc = audioTestManifestWrite(pSet, "date_created=%s\n", RTTimeSpecToString(RTTimeNow(&Now), szTmp, sizeof(szTmp)));
1012 AssertRCReturn(rc, rc);
1013
1014 RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
1015 rc = audioTestManifestWrite(pSet, "os_product=%s\n", szTmp);
1016 AssertRCReturn(rc, rc);
1017
1018 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
1019 rc = audioTestManifestWrite(pSet, "os_rel=%s\n", szTmp);
1020 AssertRCReturn(rc, rc);
1021
1022 RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp)); /* do NOT return on failure. */
1023 rc = audioTestManifestWrite(pSet, "os_ver=%s\n", szTmp);
1024 AssertRCReturn(rc, rc);
1025
1026 rc = audioTestManifestWrite(pSet, "vbox_ver=%s r%u %s (%s %s)\n",
1027 VBOX_VERSION_STRING, RTBldCfgRevision(), RTBldCfgTargetDotArch(), __DATE__, __TIME__);
1028 AssertRCReturn(rc, rc);
1029
1030 rc = audioTestManifestWrite(pSet, "test_count=");
1031 AssertRCReturn(rc, rc);
1032 pSet->offTestCount = audioTestManifestGetOffsetAbs(pSet);
1033 rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
1034 AssertRCReturn(rc, rc);
1035
1036 rc = audioTestManifestWrite(pSet, "obj_count=");
1037 AssertRCReturn(rc, rc);
1038 pSet->offObjCount = audioTestManifestGetOffsetAbs(pSet);
1039 rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
1040 AssertRCReturn(rc, rc);
1041
1042 pSet->enmMode = AUDIOTESTSETMODE_TEST;
1043
1044 return rc;
1045}
1046
1047/**
1048 * Destroys a test set.
1049 *
1050 * @returns VBox status code.
1051 * @param pSet Test set to destroy.
1052 */
1053int AudioTestSetDestroy(PAUDIOTESTSET pSet)
1054{
1055 if (!pSet)
1056 return VINF_SUCCESS;
1057
1058 AssertReturn(pSet->cTestsRunning == 0, VERR_WRONG_ORDER); /* Make sure no tests sill are running. */
1059
1060 int rc = AudioTestSetClose(pSet);
1061 if (RT_FAILURE(rc))
1062 return rc;
1063
1064 PAUDIOTESTOBJINT pObj, pObjNext;
1065 RTListForEachSafe(&pSet->lstObj, pObj, pObjNext, AUDIOTESTOBJINT, Node)
1066 {
1067 rc = audioTestObjClose(pObj);
1068 if (RT_SUCCESS(rc))
1069 {
1070 PAUDIOTESTOBJMETA pMeta, pMetaNext;
1071 RTListForEachSafe(&pObj->lstMeta, pMeta, pMetaNext, AUDIOTESTOBJMETA, Node)
1072 {
1073 switch (pMeta->enmType)
1074 {
1075 case AUDIOTESTOBJMETADATATYPE_STRING:
1076 {
1077 RTStrFree((char *)pMeta->pvMeta);
1078 break;
1079 }
1080
1081 default:
1082 AssertFailed();
1083 break;
1084 }
1085
1086 RTListNodeRemove(&pMeta->Node);
1087 RTMemFree(pMeta);
1088 }
1089
1090 RTListNodeRemove(&pObj->Node);
1091 RTMemFree(pObj);
1092
1093 Assert(pSet->cObj);
1094 pSet->cObj--;
1095 }
1096 else
1097 break;
1098 }
1099
1100 if (RT_FAILURE(rc))
1101 return rc;
1102
1103 Assert(pSet->cObj == 0);
1104
1105 PAUDIOTESTENTRY pEntry, pEntryNext;
1106 RTListForEachSafe(&pSet->lstTest, pEntry, pEntryNext, AUDIOTESTENTRY, Node)
1107 {
1108 RTListNodeRemove(&pEntry->Node);
1109 RTMemFree(pEntry);
1110
1111 Assert(pSet->cTests);
1112 pSet->cTests--;
1113 }
1114
1115 if (RT_FAILURE(rc))
1116 return rc;
1117
1118 Assert(pSet->cTests == 0);
1119
1120 return rc;
1121}
1122
1123/**
1124 * Opens an existing audio test set.
1125 *
1126 * @returns VBox status code.
1127 * @param pSet Test set to open.
1128 * @param pszPath Absolute path of the test set to open.
1129 */
1130int AudioTestSetOpen(PAUDIOTESTSET pSet, const char *pszPath)
1131{
1132 audioTestSetInitInternal(pSet);
1133
1134 char szManifest[RTPATH_MAX];
1135 int rc = RTPathJoin(szManifest, sizeof(szManifest), pszPath, AUDIOTEST_MANIFEST_FILE_STR);
1136 AssertRCReturn(rc, rc);
1137
1138 RTVFSFILE hVfsFile;
1139 rc = RTVfsFileOpenNormal(szManifest, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE, &hVfsFile);
1140 if (RT_FAILURE(rc))
1141 return rc;
1142
1143 rc = RTIniFileCreateFromVfsFile(&pSet->f.hIniFile, hVfsFile, RTINIFILE_F_READONLY);
1144 RTVfsFileRelease(hVfsFile);
1145 AssertRCReturn(rc, rc);
1146
1147 rc = RTStrCopy(pSet->szPathAbs, sizeof(pSet->szPathAbs), pszPath);
1148 AssertRCReturn(rc, rc);
1149
1150 pSet->enmMode = AUDIOTESTSETMODE_VERIFY;
1151
1152 return rc;
1153}
1154
1155/**
1156 * Closes an opened audio test set.
1157 *
1158 * @returns VBox status code.
1159 * @param pSet Test set to close.
1160 */
1161int AudioTestSetClose(PAUDIOTESTSET pSet)
1162{
1163 AssertPtrReturn(pSet, VERR_INVALID_POINTER);
1164
1165 if (!audioTestManifestIsOpen(pSet))
1166 return VINF_SUCCESS;
1167
1168 int rc;
1169
1170 if (pSet->enmMode == AUDIOTESTSETMODE_TEST)
1171 {
1172 /* Update number of bound test objects. */
1173 PAUDIOTESTENTRY pTest;
1174 RTListForEach(&pSet->lstTest, pTest, AUDIOTESTENTRY, Node)
1175 {
1176 rc = RTFileSeek(pSet->f.hFile, pTest->offObjCount, RTFILE_SEEK_BEGIN, NULL);
1177 AssertRCReturn(rc, rc);
1178 rc = audioTestManifestWrite(pSet, "%04RU32", pTest->cObj);
1179 AssertRCReturn(rc, rc);
1180 }
1181
1182 /*
1183 * Update number of ran tests.
1184 */
1185 rc = RTFileSeek(pSet->f.hFile, pSet->offObjCount, RTFILE_SEEK_BEGIN, NULL);
1186 AssertRCReturn(rc, rc);
1187 rc = audioTestManifestWrite(pSet, "%04RU32", pSet->cObj);
1188 AssertRCReturn(rc, rc);
1189
1190 /*
1191 * Update number of ran tests.
1192 */
1193 rc = RTFileSeek(pSet->f.hFile, pSet->offTestCount, RTFILE_SEEK_BEGIN, NULL);
1194 AssertRCReturn(rc, rc);
1195 rc = audioTestManifestWrite(pSet, "%04RU32", pSet->cTests);
1196 AssertRCReturn(rc, rc);
1197
1198 /*
1199 * Serialize all registered test objects.
1200 */
1201 rc = RTFileSeek(pSet->f.hFile, 0, RTFILE_SEEK_END, NULL);
1202 AssertRCReturn(rc, rc);
1203
1204 PAUDIOTESTOBJINT pObj;
1205 RTListForEach(&pSet->lstObj, pObj, AUDIOTESTOBJINT, Node)
1206 {
1207 rc = audioTestManifestWrite(pSet, "\n");
1208 AssertRCReturn(rc, rc);
1209 char szUuid[AUDIOTEST_MAX_SEC_LEN];
1210 rc = RTUuidToStr(&pObj->Uuid, szUuid, sizeof(szUuid));
1211 AssertRCReturn(rc, rc);
1212 rc = audioTestManifestWriteSectionHdr(pSet, "obj_%s", szUuid);
1213 AssertRCReturn(rc, rc);
1214 rc = audioTestManifestWrite(pSet, "obj_type=%RU32\n", pObj->enmType);
1215 AssertRCReturn(rc, rc);
1216 rc = audioTestManifestWrite(pSet, "obj_name=%s\n", pObj->szName);
1217 AssertRCReturn(rc, rc);
1218
1219 switch (pObj->enmType)
1220 {
1221 case AUDIOTESTOBJTYPE_FILE:
1222 {
1223 rc = audioTestManifestWrite(pSet, "obj_size=%RU64\n", pObj->File.cbSize);
1224 AssertRCReturn(rc, rc);
1225 break;
1226 }
1227
1228 default:
1229 AssertFailed();
1230 break;
1231 }
1232
1233 /*
1234 * Write all meta data.
1235 */
1236 PAUDIOTESTOBJMETA pMeta;
1237 RTListForEach(&pObj->lstMeta, pMeta, AUDIOTESTOBJMETA, Node)
1238 {
1239 switch (pMeta->enmType)
1240 {
1241 case AUDIOTESTOBJMETADATATYPE_STRING:
1242 {
1243 rc = audioTestManifestWrite(pSet, (const char *)pMeta->pvMeta);
1244 AssertRCReturn(rc, rc);
1245 break;
1246 }
1247
1248 default:
1249 AssertFailed();
1250 break;
1251 }
1252 }
1253 }
1254
1255 rc = RTFileClose(pSet->f.hFile);
1256 if (RT_SUCCESS(rc))
1257 pSet->f.hFile = NIL_RTFILE;
1258 }
1259 else if (pSet->enmMode == AUDIOTESTSETMODE_VERIFY)
1260 {
1261 RTIniFileRelease(pSet->f.hIniFile);
1262 pSet->f.hIniFile = NIL_RTINIFILE;
1263
1264 rc = VINF_SUCCESS;
1265 }
1266 else
1267 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
1268
1269 return rc;
1270}
1271
1272/**
1273 * Physically wipes all related test set files off the disk.
1274 *
1275 * @returns VBox status code.
1276 * @param pSet Test set to wipe.
1277 */
1278int AudioTestSetWipe(PAUDIOTESTSET pSet)
1279{
1280 AssertPtrReturn(pSet, VERR_INVALID_POINTER);
1281
1282 int rc = VINF_SUCCESS;
1283 char szFilePath[RTPATH_MAX];
1284
1285 PAUDIOTESTOBJINT pObj;
1286 RTListForEach(&pSet->lstObj, pObj, AUDIOTESTOBJINT, Node)
1287 {
1288 int rc2 = audioTestObjClose(pObj);
1289 if (RT_SUCCESS(rc2))
1290 {
1291 rc2 = audioTestSetGetObjPath(pSet, szFilePath, sizeof(szFilePath), pObj->szName);
1292 if (RT_SUCCESS(rc2))
1293 rc2 = RTFileDelete(szFilePath);
1294 }
1295
1296 if (RT_SUCCESS(rc))
1297 rc = rc2;
1298 /* Keep going. */
1299 }
1300
1301 if (RT_SUCCESS(rc))
1302 {
1303 rc = RTPathJoin(szFilePath, sizeof(szFilePath), pSet->szPathAbs, AUDIOTEST_MANIFEST_FILE_STR);
1304 if (RT_SUCCESS(rc))
1305 rc = RTFileDelete(szFilePath);
1306 }
1307
1308 /* Remove the (hopefully now empty) directory. Otherwise let this fail. */
1309 if (RT_SUCCESS(rc))
1310 rc = RTDirRemove(pSet->szPathAbs);
1311
1312 return rc;
1313}
1314
1315/**
1316 * Creates and registers a new audio test object to the current running test.
1317 *
1318 * @returns VBox status code.
1319 * @param pSet Test set to create and register new object for.
1320 * @param pszName Name of new object to create.
1321 * @param pObj Where to return the pointer to the newly created object on success.
1322 */
1323int AudioTestSetObjCreateAndRegister(PAUDIOTESTSET pSet, const char *pszName, PAUDIOTESTOBJ pObj)
1324{
1325 AssertReturn(pSet->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
1326
1327 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1328
1329 PAUDIOTESTOBJINT pThis = (PAUDIOTESTOBJINT)RTMemAlloc(sizeof(AUDIOTESTOBJINT));
1330 AssertPtrReturn(pThis, VERR_NO_MEMORY);
1331
1332 audioTestObjInit(pThis);
1333
1334 if (RTStrPrintf2(pThis->szName, sizeof(pThis->szName), "%04RU32-%s", pSet->cObj, pszName) <= 0)
1335 AssertFailedReturn(VERR_BUFFER_OVERFLOW);
1336
1337 /** @todo Generalize this function more once we have more object types. */
1338
1339 char szObjPathAbs[RTPATH_MAX];
1340 int rc = audioTestSetGetObjPath(pSet, szObjPathAbs, sizeof(szObjPathAbs), pThis->szName);
1341 if (RT_SUCCESS(rc))
1342 {
1343 rc = RTFileOpen(&pThis->File.hFile, szObjPathAbs, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
1344 if (RT_SUCCESS(rc))
1345 {
1346 pThis->enmType = AUDIOTESTOBJTYPE_FILE;
1347 pThis->cRefs = 1; /* Currently only 1:1 mapping. */
1348
1349 RTListAppend(&pSet->lstObj, &pThis->Node);
1350 pSet->cObj++;
1351
1352 /* Generate + set an UUID for the object and assign it to the current test. */
1353 rc = RTUuidCreate(&pThis->Uuid);
1354 AssertRCReturn(rc, rc);
1355 char szUuid[AUDIOTEST_MAX_OBJ_LEN];
1356 rc = RTUuidToStr(&pThis->Uuid, szUuid, sizeof(szUuid));
1357 AssertRCReturn(rc, rc);
1358
1359 rc = audioTestManifestWrite(pSet, "obj%RU32_uuid=%s\n", pSet->pTestCur->cObj, szUuid);
1360 AssertRCReturn(rc, rc);
1361
1362 AssertPtr(pSet->pTestCur);
1363 pSet->pTestCur->cObj++;
1364
1365 *pObj = pThis;
1366 }
1367 }
1368
1369 if (RT_FAILURE(rc))
1370 RTMemFree(pThis);
1371
1372 return rc;
1373}
1374
1375/**
1376 * Writes to a created audio test object.
1377 *
1378 * @returns VBox status code.
1379 * @param hObj Handle to the audio test object to write to.
1380 * @param pvBuf Pointer to data to write.
1381 * @param cbBuf Size (in bytes) of \a pvBuf to write.
1382 */
1383int AudioTestObjWrite(AUDIOTESTOBJ hObj, const void *pvBuf, size_t cbBuf)
1384{
1385 AUDIOTESTOBJINT *pThis = hObj;
1386
1387 /** @todo Generalize this function more once we have more object types. */
1388 AssertReturn(pThis->enmType == AUDIOTESTOBJTYPE_FILE, VERR_INVALID_PARAMETER);
1389
1390 return RTFileWrite(pThis->File.hFile, pvBuf, cbBuf, NULL);
1391}
1392
1393/**
1394 * Adds meta data to a test object as a string, va_list version.
1395 *
1396 * @returns VBox status code.
1397 * @param pObj Test object to add meta data for.
1398 * @param pszFormat Format string to add.
1399 * @param va Variable arguments list to use for the format string.
1400 */
1401static int audioTestObjAddMetadataStrV(PAUDIOTESTOBJINT pObj, const char *pszFormat, va_list va)
1402{
1403 PAUDIOTESTOBJMETA pMeta = (PAUDIOTESTOBJMETA)RTMemAlloc(sizeof(AUDIOTESTOBJMETA));
1404 AssertPtrReturn(pMeta, VERR_NO_MEMORY);
1405
1406 pMeta->pvMeta = RTStrAPrintf2V(pszFormat, va);
1407 AssertPtrReturn(pMeta->pvMeta, VERR_BUFFER_OVERFLOW);
1408 pMeta->cbMeta = RTStrNLen((const char *)pMeta->pvMeta, RTSTR_MAX);
1409
1410 pMeta->enmType = AUDIOTESTOBJMETADATATYPE_STRING;
1411
1412 RTListAppend(&pObj->lstMeta, &pMeta->Node);
1413
1414 return VINF_SUCCESS;
1415}
1416
1417/**
1418 * Adds meta data to a test object as a string.
1419 *
1420 * @returns VBox status code.
1421 * @param hObj Handle to the test object to add meta data for.
1422 * @param pszFormat Format string to add.
1423 * @param ... Variable arguments for the format string.
1424 */
1425int AudioTestObjAddMetadataStr(AUDIOTESTOBJ hObj, const char *pszFormat, ...)
1426{
1427 AUDIOTESTOBJINT *pThis = hObj;
1428
1429 va_list va;
1430
1431 va_start(va, pszFormat);
1432 int rc = audioTestObjAddMetadataStrV(pThis, pszFormat, va);
1433 va_end(va);
1434
1435 return rc;
1436}
1437
1438/**
1439 * Closes an opened audio test object.
1440 *
1441 * @returns VBox status code.
1442 * @param hObj Handle to the audio test object to close.
1443 */
1444int AudioTestObjClose(AUDIOTESTOBJ hObj)
1445{
1446 AUDIOTESTOBJINT *pThis = hObj;
1447
1448 if (!pThis)
1449 return VINF_SUCCESS;
1450
1451 audioTestObjFinalize(pThis);
1452
1453 return audioTestObjClose(pThis);
1454}
1455
1456/**
1457 * Begins a new test of a test set.
1458 *
1459 * @returns VBox status code.
1460 * @param pSet Test set to begin new test for.
1461 * @param pszDesc Test description.
1462 * @param pParms Test parameters to use.
1463 * @param ppEntry Where to return the new test
1464 */
1465int AudioTestSetTestBegin(PAUDIOTESTSET pSet, const char *pszDesc, PAUDIOTESTPARMS pParms, PAUDIOTESTENTRY *ppEntry)
1466{
1467 AssertReturn(pSet->cTestsRunning == 0, VERR_WRONG_ORDER); /* No test nesting allowed. */
1468
1469 PAUDIOTESTENTRY pEntry = (PAUDIOTESTENTRY)RTMemAllocZ(sizeof(AUDIOTESTENTRY));
1470 AssertPtrReturn(pEntry, VERR_NO_MEMORY);
1471
1472 int rc = RTStrCopy(pEntry->szDesc, sizeof(pEntry->szDesc), pszDesc);
1473 AssertRCReturn(rc, rc);
1474
1475 memcpy(&pEntry->Parms, pParms, sizeof(AUDIOTESTPARMS));
1476
1477 pEntry->pParent = pSet;
1478 pEntry->rc = VERR_IPE_UNINITIALIZED_STATUS;
1479
1480 rc = audioTestManifestWrite(pSet, "\n");
1481 AssertRCReturn(rc, rc);
1482
1483 rc = audioTestManifestWriteSectionHdr(pSet, "test_%04RU32", pSet->cTests);
1484 AssertRCReturn(rc, rc);
1485 rc = audioTestManifestWrite(pSet, "test_desc=%s\n", pszDesc);
1486 AssertRCReturn(rc, rc);
1487 rc = audioTestManifestWrite(pSet, "test_type=%RU32\n", pParms->enmType);
1488 AssertRCReturn(rc, rc);
1489 rc = audioTestManifestWrite(pSet, "test_delay_ms=%RU32\n", pParms->msDelay);
1490 AssertRCReturn(rc, rc);
1491 rc = audioTestManifestWrite(pSet, "audio_direction=%s\n", PDMAudioDirGetName(pParms->enmDir));
1492 AssertRCReturn(rc, rc);
1493
1494 rc = audioTestManifestWrite(pSet, "obj_count=");
1495 AssertRCReturn(rc, rc);
1496 pEntry->offObjCount = audioTestManifestGetOffsetAbs(pSet);
1497 rc = audioTestManifestWrite(pSet, "0000\n"); /* A bit messy, but does the trick for now. */
1498 AssertRCReturn(rc, rc);
1499
1500 switch (pParms->enmType)
1501 {
1502 case AUDIOTESTTYPE_TESTTONE_PLAY:
1503 RT_FALL_THROUGH();
1504 case AUDIOTESTTYPE_TESTTONE_RECORD:
1505 {
1506 rc = audioTestManifestWrite(pSet, "tone_freq_hz=%RU16\n", (uint16_t)pParms->TestTone.dbFreqHz);
1507 AssertRCReturn(rc, rc);
1508 rc = audioTestManifestWrite(pSet, "tone_prequel_ms=%RU32\n", pParms->TestTone.msPrequel);
1509 AssertRCReturn(rc, rc);
1510 rc = audioTestManifestWrite(pSet, "tone_duration_ms=%RU32\n", pParms->TestTone.msDuration);
1511 AssertRCReturn(rc, rc);
1512 rc = audioTestManifestWrite(pSet, "tone_sequel_ms=%RU32\n", pParms->TestTone.msSequel);
1513 AssertRCReturn(rc, rc);
1514 rc = audioTestManifestWrite(pSet, "tone_volume_percent=%RU32\n", pParms->TestTone.uVolumePercent);
1515 AssertRCReturn(rc, rc);
1516 rc = audioTestManifestWrite(pSet, "tone_pcm_hz=%RU32\n", PDMAudioPropsHz(&pParms->TestTone.Props));
1517 AssertRCReturn(rc, rc);
1518 rc = audioTestManifestWrite(pSet, "tone_pcm_channels=%RU8\n", PDMAudioPropsChannels(&pParms->TestTone.Props));
1519 AssertRCReturn(rc, rc);
1520 rc = audioTestManifestWrite(pSet, "tone_pcm_bits=%RU8\n", PDMAudioPropsSampleBits(&pParms->TestTone.Props));
1521 AssertRCReturn(rc, rc);
1522 rc = audioTestManifestWrite(pSet, "tone_pcm_is_signed=%RTbool\n", PDMAudioPropsIsSigned(&pParms->TestTone.Props));
1523 AssertRCReturn(rc, rc);
1524 break;
1525 }
1526
1527 default:
1528 AssertFailed();
1529 break;
1530 }
1531
1532 RTListAppend(&pSet->lstTest, &pEntry->Node);
1533
1534 pSet->cTests++;
1535 pSet->cTestsRunning++;
1536 pSet->pTestCur = pEntry;
1537
1538 *ppEntry = pEntry;
1539
1540 return rc;
1541}
1542
1543/**
1544 * Marks a running test as failed.
1545 *
1546 * @returns VBox status code.
1547 * @param pEntry Test to mark.
1548 * @param rc Error code.
1549 * @param pszErr Error description.
1550 */
1551int AudioTestSetTestFailed(PAUDIOTESTENTRY pEntry, int rc, const char *pszErr)
1552{
1553 AssertReturn(pEntry->pParent->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
1554 AssertReturn(pEntry->rc == VERR_IPE_UNINITIALIZED_STATUS, VERR_WRONG_ORDER);
1555
1556 pEntry->rc = rc;
1557
1558 int rc2 = audioTestManifestWrite(pEntry->pParent, "error_rc=%RI32\n", rc);
1559 AssertRCReturn(rc2, rc2);
1560 rc2 = audioTestManifestWrite(pEntry->pParent, "error_desc=%s\n", pszErr);
1561 AssertRCReturn(rc2, rc2);
1562
1563 pEntry->pParent->cTestsRunning--;
1564 pEntry->pParent->pTestCur = NULL;
1565
1566 return rc2;
1567}
1568
1569/**
1570 * Marks a running test as successfully done.
1571 *
1572 * @returns VBox status code.
1573 * @param pEntry Test to mark.
1574 */
1575int AudioTestSetTestDone(PAUDIOTESTENTRY pEntry)
1576{
1577 AssertReturn(pEntry->pParent->cTestsRunning == 1, VERR_WRONG_ORDER); /* No test nesting allowed. */
1578 AssertReturn(pEntry->rc == VERR_IPE_UNINITIALIZED_STATUS, VERR_WRONG_ORDER);
1579
1580 pEntry->rc = VINF_SUCCESS;
1581
1582 int rc2 = audioTestManifestWrite(pEntry->pParent, "error_rc=%RI32\n", VINF_SUCCESS);
1583 AssertRCReturn(rc2, rc2);
1584
1585 pEntry->pParent->cTestsRunning--;
1586 pEntry->pParent->pTestCur = NULL;
1587
1588 return rc2;
1589}
1590
1591/**
1592 * Returns whether a test is still running or not.
1593 *
1594 * @returns \c true if test is still running, or \c false if not.
1595 * @param pEntry Test to get running status for.
1596 */
1597bool AudioTestSetTestIsRunning(PAUDIOTESTENTRY pEntry)
1598{
1599 return (pEntry->rc == VERR_IPE_UNINITIALIZED_STATUS);
1600}
1601
1602/**
1603 * Packs a closed audio test so that it's ready for transmission.
1604 *
1605 * @returns VBox status code.
1606 * @param pSet Test set to pack.
1607 * @param pszOutDir Directory where to store the packed test set.
1608 * @param pszFileName Where to return the final name of the packed test set. Optional and can be NULL.
1609 * @param cbFileName Size (in bytes) of \a pszFileName.
1610 */
1611int AudioTestSetPack(PAUDIOTESTSET pSet, const char *pszOutDir, char *pszFileName, size_t cbFileName)
1612{
1613 AssertPtrReturn(pSet, VERR_INVALID_POINTER);
1614 AssertReturn(!pszFileName || cbFileName, VERR_INVALID_PARAMETER);
1615 AssertReturn(!audioTestManifestIsOpen(pSet), VERR_WRONG_ORDER);
1616
1617 AssertMsgReturn(pSet->cTests, ("No tests run yet"), VERR_INVALID_STATE);
1618 AssertMsgReturn(pSet->cTestsRunning == 0 , ("Some tests are still running"), VERR_INVALID_STATE);
1619
1620 /** @todo Check and deny if \a pszOutDir is part of the set's path. */
1621
1622 int rc = RTDirCreateFullPath(pszOutDir, 0755);
1623 if (RT_FAILURE(rc))
1624 return rc;
1625
1626 char szOutName[RT_ELEMENTS(AUDIOTEST_PATH_PREFIX_STR) + AUDIOTEST_TAG_MAX + 16];
1627 if (RTStrPrintf2(szOutName, sizeof(szOutName), "%s-%s%s",
1628 AUDIOTEST_PATH_PREFIX_STR, pSet->szTag, AUDIOTEST_ARCHIVE_SUFF_STR) <= 0)
1629 AssertFailedReturn(VERR_BUFFER_OVERFLOW);
1630
1631 char szOutPath[RTPATH_MAX];
1632 rc = RTPathJoin(szOutPath, sizeof(szOutPath), pszOutDir, szOutName);
1633 AssertRCReturn(rc, rc);
1634
1635 const char *apszArgs[10];
1636 unsigned cArgs = 0;
1637
1638 apszArgs[cArgs++] = "vkat";
1639 apszArgs[cArgs++] = "--create";
1640 apszArgs[cArgs++] = "--gzip";
1641 apszArgs[cArgs++] = "--directory";
1642 apszArgs[cArgs++] = pSet->szPathAbs;
1643 apszArgs[cArgs++] = "--file";
1644 apszArgs[cArgs++] = szOutPath;
1645 apszArgs[cArgs++] = ".";
1646
1647 RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
1648 if (rcExit != RTEXITCODE_SUCCESS)
1649 rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */
1650
1651 if (RT_SUCCESS(rc))
1652 {
1653 if (pszFileName)
1654 rc = RTStrCopy(pszFileName, cbFileName, szOutPath);
1655 }
1656
1657 return rc;
1658}
1659
1660/**
1661 * Returns whether a test set archive is packed (as .tar.gz by default) or
1662 * a plain directory.
1663 *
1664 * @returns \c true if packed (as .tar.gz), or \c false if not (directory).
1665 * @param pszPath Path to return packed staus for.
1666 */
1667bool AudioTestSetIsPacked(const char *pszPath)
1668{
1669 /** @todo Improve this, good enough for now. */
1670 return (RTStrIStr(pszPath, AUDIOTEST_ARCHIVE_SUFF_STR) != NULL);
1671}
1672
1673/**
1674 * Returns whether a test set has running (active) tests or not.
1675 *
1676 * @returns \c true if it has running tests, or \c false if not.
1677 * @param pSet Test set to return status for.
1678 */
1679bool AudioTestSetIsRunning(PAUDIOTESTSET pSet)
1680{
1681 return (pSet->cTestsRunning > 0);
1682}
1683
1684/**
1685 * Unpacks a formerly packed audio test set.
1686 *
1687 * @returns VBox status code.
1688 * @param pszFile Test set file to unpack. Must contain the absolute path.
1689 * @param pszOutDir Directory where to unpack the test set into.
1690 * If the directory does not exist it will be created.
1691 */
1692int AudioTestSetUnpack(const char *pszFile, const char *pszOutDir)
1693{
1694 AssertReturn(pszFile && pszOutDir, VERR_INVALID_PARAMETER);
1695
1696 int rc = VINF_SUCCESS;
1697
1698 if (!RTDirExists(pszOutDir))
1699 {
1700 rc = RTDirCreateFullPath(pszOutDir, 0755);
1701 if (RT_FAILURE(rc))
1702 return rc;
1703 }
1704
1705 const char *apszArgs[8];
1706 unsigned cArgs = 0;
1707
1708 apszArgs[cArgs++] = "vkat";
1709 apszArgs[cArgs++] = "--extract";
1710 apszArgs[cArgs++] = "--gunzip";
1711 apszArgs[cArgs++] = "--directory";
1712 apszArgs[cArgs++] = pszOutDir;
1713 apszArgs[cArgs++] = "--file";
1714 apszArgs[cArgs++] = pszFile;
1715
1716 RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs);
1717 if (rcExit != RTEXITCODE_SUCCESS)
1718 rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */
1719
1720 return rc;
1721}
1722
1723/**
1724 * Retrieves an object handle of a specific test set section.
1725 *
1726 * @returns VBox status code.
1727 * @param pSet Test set the section contains.
1728 * @param pszSec Name of section to retrieve object handle for.
1729 * @param phSec Where to store the object handle on success.
1730 */
1731static int audioTestSetGetSection(PAUDIOTESTSET pSet, const char *pszSec, PAUDIOTESTOBJINT phSec)
1732{
1733 int rc = RTStrCopy(phSec->szSec, sizeof(phSec->szSec), pszSec);
1734 if (RT_FAILURE(rc))
1735 return rc;
1736
1737 phSec->pSet = pSet;
1738
1739 /** @todo Check for section existence. */
1740 RT_NOREF(pSet);
1741
1742 return VINF_SUCCESS;
1743}
1744
1745/**
1746 * Retrieves an object handle of a specific test.
1747 *
1748 * @returns VBox status code.
1749 * @param pSet Test set the test contains.
1750 * @param idxTst Index of test to retrieve the object handle for.
1751 * @param phTst Where to store the object handle on success.
1752 */
1753static int audioTestSetGetTest(PAUDIOTESTSET pSet, uint32_t idxTst, PAUDIOTESTOBJINT phTst)
1754{
1755 char szSec[AUDIOTEST_MAX_SEC_LEN];
1756 if (RTStrPrintf2(szSec, sizeof(szSec), "test_%04RU32", idxTst) <= 0)
1757 return VERR_BUFFER_OVERFLOW;
1758
1759 return audioTestSetGetSection(pSet, szSec, phTst);
1760}
1761
1762/**
1763 * Initializes a test object.
1764 *
1765 * @param pObj Object to initialize.
1766 */
1767static void audioTestObjInit(PAUDIOTESTOBJINT pObj)
1768{
1769 RT_BZERO(pObj, sizeof(AUDIOTESTOBJINT));
1770
1771 pObj->cRefs = 1;
1772
1773 RTListInit(&pObj->lstMeta);
1774}
1775
1776/**
1777 * Retrieves a child object of a specific parent object.
1778 *
1779 * @returns VBox status code.
1780 * @param pParent Parent object the child object contains.
1781 * @param idxObj Index of object to retrieve the object handle for.
1782 * @param pObj Where to store the object handle on success.
1783 */
1784static int audioTestObjGetChild(PAUDIOTESTOBJINT pParent, uint32_t idxObj, PAUDIOTESTOBJINT pObj)
1785{
1786 char szObj[AUDIOTEST_MAX_SEC_LEN];
1787 if (RTStrPrintf2(szObj, sizeof(szObj), "obj%RU32_uuid", idxObj) <= 0)
1788 AssertFailedReturn(VERR_BUFFER_OVERFLOW);
1789
1790 char szUuid[AUDIOTEST_MAX_SEC_LEN];
1791 int rc = audioTestObjGetStr(pParent, szObj, szUuid, sizeof(szUuid));
1792 if (RT_SUCCESS(rc))
1793 {
1794 audioTestObjInit(pObj);
1795
1796 AssertReturn(RTStrPrintf2(pObj->szSec, sizeof(pObj->szSec), "obj_%s", szUuid) > 0, VERR_BUFFER_OVERFLOW);
1797
1798 /** @todo Check test section existence. */
1799
1800 pObj->pSet = pParent->pSet;
1801 }
1802
1803 return rc;
1804}
1805
1806/**
1807 * Verifies a value of a test verification job.
1808 *
1809 * @returns VBox status code.
1810 * @returns Error if the verification failed and test verification job has fKeepGoing not set.
1811 * @param pVerJob Verification job to verify value for.
1812 * @param pObjA Object handle A to verify value for.
1813 * @param pObjB Object handle B to verify value for.
1814 * @param pszKey Key to verify.
1815 * @param pszVal Value to verify.
1816 * @param pszErrFmt Error format string in case the verification failed.
1817 * @param ... Variable aruments for error format string.
1818 */
1819static int audioTestVerifyValue(PAUDIOTESTVERIFYJOB pVerJob,
1820 PAUDIOTESTOBJINT pObjA, PAUDIOTESTOBJINT pObjB, const char *pszKey, const char *pszVal, const char *pszErrFmt, ...)
1821{
1822 va_list va;
1823 va_start(va, pszErrFmt);
1824
1825 char szValA[_1K];
1826 int rc = audioTestObjGetStr(pObjA, pszKey, szValA, sizeof(szValA));
1827 if (RT_SUCCESS(rc))
1828 {
1829 char szValB[_1K];
1830 rc = audioTestObjGetStr(pObjB, pszKey, szValB, sizeof(szValB));
1831 if (RT_SUCCESS(rc))
1832 {
1833 if (RTStrCmp(szValA, szValB))
1834 {
1835 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
1836 "Values are not equal ('%s' vs. '%s')", szValA, szValB);
1837 AssertRC(rc2);
1838 rc = VERR_WRONG_TYPE; /** @todo Fudge! */
1839 }
1840
1841 if (pszVal)
1842 {
1843 if (RTStrCmp(szValA, pszVal))
1844 {
1845 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
1846 "Values don't match expected value (got '%s', expected '%s')", szValA, pszVal);
1847 AssertRC(rc2);
1848 rc = VERR_WRONG_TYPE; /** @todo Fudge! */
1849 }
1850 }
1851 }
1852 }
1853
1854 if (RT_FAILURE(rc))
1855 {
1856 int rc2 = audioTestErrorDescAddV(pVerJob->pErr, pVerJob->idxTest, rc, pszErrFmt, va);
1857 AssertRC(rc2);
1858 }
1859
1860 va_end(va);
1861
1862 return pVerJob->Opts.fKeepGoing ? VINF_SUCCESS : rc;
1863}
1864
1865/**
1866 * Opens a test object which is a regular file.
1867 *
1868 * @returns VBox status code.
1869 * @param pObj Test object to open.
1870 * @param pszFile Absolute file path of file to open.
1871 */
1872static int audioTestObjOpenFile(PAUDIOTESTOBJINT pObj, const char *pszFile)
1873{
1874 int rc = RTFileOpen(&pObj->File.hFile, pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
1875 if (RT_SUCCESS(rc))
1876 {
1877 int rc2 = RTStrCopy(pObj->szName, sizeof(pObj->szName), pszFile);
1878 AssertRC(rc2);
1879
1880 pObj->enmType = AUDIOTESTOBJTYPE_FILE;
1881 }
1882
1883 return rc;
1884}
1885
1886/**
1887 * Opens an existing audio test object.
1888 *
1889 * @returns VBox status code.
1890 * @param pObj Object to open.
1891 */
1892static int audioTestObjOpen(PAUDIOTESTOBJINT pObj)
1893{
1894 AssertReturn(pObj->enmType == AUDIOTESTOBJTYPE_UNKNOWN, VERR_WRONG_ORDER);
1895
1896 char szFileName[AUDIOTEST_MAX_SEC_LEN];
1897 int rc = audioTestObjGetStr(pObj, "obj_name", szFileName, sizeof(szFileName));
1898 if (RT_SUCCESS(rc))
1899 {
1900 char szFilePath[RTPATH_MAX];
1901 rc = RTPathJoin(szFilePath, sizeof(szFilePath), pObj->pSet->szPathAbs, szFileName);
1902 if (RT_SUCCESS(rc))
1903 {
1904 /** @todo Check "obj_type". */
1905 rc = audioTestObjOpenFile(pObj, szFilePath);
1906 }
1907 }
1908 return rc;
1909}
1910
1911/**
1912 * Closes an audio test set object.
1913 *
1914 * @returns VBox status code.
1915 * @param pObj Object to close.
1916 */
1917static int audioTestObjClose(PAUDIOTESTOBJINT pObj)
1918{
1919 if (!audioTestObjIsOpen(pObj))
1920 return VINF_SUCCESS;
1921
1922 int rc;
1923
1924 /** @todo Generalize this function more once we have more object types. */
1925
1926 if (RTFileIsValid(pObj->File.hFile))
1927 {
1928 rc = RTFileClose(pObj->File.hFile);
1929 if (RT_SUCCESS(rc))
1930 pObj->File.hFile = NIL_RTFILE;
1931 }
1932 else
1933 rc = VINF_SUCCESS;
1934
1935 return rc;
1936}
1937
1938/**
1939 * Returns whether a test set object is in opened state or not.
1940 *
1941 * @returns \c true if open, or \c false if not.
1942 * @param pObj Object to return status for.
1943 */
1944static bool audioTestObjIsOpen(PAUDIOTESTOBJINT pObj)
1945{
1946 return pObj->enmType != AUDIOTESTOBJTYPE_UNKNOWN;
1947}
1948
1949/**
1950 * Finalizes an audio test set object.
1951 *
1952 * @param pObj Test object to finalize.
1953 */
1954static void audioTestObjFinalize(PAUDIOTESTOBJINT pObj)
1955{
1956 /** @todo Generalize this function more once we have more object types. */
1957 AssertReturnVoid(pObj->enmType == AUDIOTESTOBJTYPE_FILE);
1958
1959 if (RTFileIsValid(pObj->File.hFile))
1960 pObj->File.cbSize = RTFileTell(pObj->File.hFile);
1961}
1962
1963/**
1964 * Retrieves tone PCM properties of an object.
1965 *
1966 * @returns VBox status code.
1967 * @param pObj Object to retrieve PCM properties for.
1968 * @param pProps Where to store the PCM properties on success.
1969 */
1970static int audioTestObjGetTonePcmProps(PAUDIOTESTOBJINT pObj, PPDMAUDIOPCMPROPS pProps)
1971{
1972 int rc;
1973 uint32_t uHz;
1974 rc = audioTestObjGetUInt32(pObj, "tone_pcm_hz", &uHz);
1975 AssertRCReturn(rc, rc);
1976 uint8_t cBits;
1977 rc = audioTestObjGetUInt8(pObj, "tone_pcm_bits", &cBits);
1978 AssertRCReturn(rc, rc);
1979 uint8_t cChan;
1980 rc = audioTestObjGetUInt8(pObj, "tone_pcm_channels", &cChan);
1981 AssertRCReturn(rc, rc);
1982 bool fSigned;
1983 rc = audioTestObjGetBool(pObj, "tone_pcm_is_signed", &fSigned);
1984 AssertRCReturn(rc, rc);
1985
1986 PDMAudioPropsInit(pProps, (cBits / 8), fSigned, cChan, uHz);
1987
1988 return VINF_SUCCESS;
1989}
1990
1991/**
1992 * Normalizes PCM audio data.
1993 * Only supports 16 bit stereo PCM data for now.
1994 *
1995 * @returns VBox status code.
1996 * @param hFileSrc Source file handle of audio data to normalize.
1997 * @param pProps PCM properties to use for normalization.
1998 * @param cbSize Size (in bytes) of audio data to normalize.
1999 * @param dbNormalizePercent Normalization (percent) to achieve.
2000 * @param hFileDst Destiation file handle (must be open) where to write the normalized audio data to.
2001 * @param pdbRatio Where to store the normalization ratio used on success. Optional and can be NULL.
2002 * A ration of exactly 1 means no normalization.
2003 *
2004 * @note The source file handle must point at the beginning of the PCM audio data to normalize.
2005 */
2006static int audioTestFileNormalizePCM(RTFILE hFileSrc, PCPDMAUDIOPCMPROPS pProps, uint64_t cbSize,
2007 double dbNormalizePercent, RTFILE hFileDst, double *pdbRatio)
2008{
2009 if ( !pProps->fSigned
2010 || pProps->cbSampleX != 2) /* Fend-off non-supported stuff first. */
2011 return VERR_NOT_SUPPORTED;
2012
2013 int rc = VINF_SUCCESS; /* Shut up MSVC. */
2014
2015 if (!cbSize)
2016 {
2017 rc = RTFileQuerySize(hFileSrc, &cbSize);
2018 AssertRCReturn(rc, rc);
2019 }
2020 else
2021 AssertReturn(PDMAudioPropsIsSizeAligned(pProps, (uint32_t)cbSize), VERR_INVALID_PARAMETER);
2022
2023 uint64_t offStart = RTFileTell(hFileSrc);
2024 size_t cbToRead = cbSize;
2025
2026 /* Find minimum and maximum peaks. */
2027 int16_t iMin = 0;
2028 int16_t iMax = 0;
2029 double dbRatio = 0.0;
2030
2031 uint8_t auBuf[_64K];
2032 while (cbToRead)
2033 {
2034 size_t const cbChunk = RT_MIN(cbToRead, sizeof(auBuf));
2035 size_t cbRead = 0;
2036 rc = RTFileRead(hFileSrc, auBuf, cbChunk, &cbRead);
2037 if (rc == VERR_EOF)
2038 break;
2039 AssertRCBreak(rc);
2040
2041 AssertBreak(PDMAudioPropsIsSizeAligned(pProps, (uint32_t)cbRead));
2042
2043 switch (pProps->cbSampleX)
2044 {
2045 case 2: /* 16 bit signed */
2046 {
2047 int16_t *pi16Src = (int16_t *)auBuf;
2048 for (size_t i = 0; i < cbRead / pProps->cbSampleX; i += pProps->cbSampleX)
2049 {
2050 if (*pi16Src < iMin)
2051 iMin = *pi16Src;
2052 if (*pi16Src > iMax)
2053 iMax = *pi16Src;
2054 pi16Src++;
2055 }
2056 break;
2057 }
2058
2059 default:
2060 AssertMsgFailedBreakStmt(("Invalid bytes per sample: %RU8\n", pProps->cbSampleX), rc = VERR_NOT_SUPPORTED);
2061 }
2062
2063 Assert(cbToRead >= cbRead);
2064 cbToRead -= cbRead;
2065 }
2066
2067 if (RT_FAILURE(rc))
2068 return rc;
2069
2070 /* Now rewind and do the actual gain / attenuation. */
2071 rc = RTFileSeek(hFileSrc, offStart, RTFILE_SEEK_BEGIN, NULL /* poffActual */);
2072 AssertRCReturn(rc, rc);
2073 cbToRead = cbSize;
2074
2075 switch (pProps->cbSampleX)
2076 {
2077 case 2: /* 16 bit signed */
2078 {
2079 if (iMin == INT16_MIN)
2080 iMin = INT16_MIN + 1;
2081 if ((-iMin) > iMax)
2082 iMax = -iMin;
2083
2084 dbRatio = iMax == 0 ? 1.0 : ((double)INT16_MAX * dbNormalizePercent) / ((double)iMax * 100.0);
2085
2086 while (cbToRead)
2087 {
2088 size_t const cbChunk = RT_MIN(cbToRead, sizeof(auBuf));
2089 size_t cbRead;
2090 rc = RTFileRead(hFileSrc, auBuf, cbChunk, &cbRead);
2091 if (rc == VERR_EOF)
2092 break;
2093 AssertRCBreak(rc);
2094
2095 int16_t *pi16Src = (int16_t *)auBuf;
2096 for (size_t i = 0; i < cbRead / pProps->cbSampleX; i += pProps->cbSampleX)
2097 {
2098 /** @todo Optimize this -- use a lookup table for sample indices? */
2099 if ((*pi16Src * dbRatio) > INT16_MAX)
2100 *pi16Src = INT16_MAX;
2101 else if ((*pi16Src * dbRatio) < INT16_MIN)
2102 *pi16Src = INT16_MIN;
2103 else
2104 *pi16Src = (int16_t)(*pi16Src * dbRatio);
2105 pi16Src++;
2106 }
2107
2108 size_t cbWritten;
2109 rc = RTFileWrite(hFileDst, auBuf, cbChunk, &cbWritten);
2110 AssertRCBreak(rc);
2111 Assert(cbWritten == cbChunk);
2112
2113 Assert(cbToRead >= cbRead);
2114 cbToRead -= cbRead;
2115 }
2116 break;
2117 }
2118
2119 default:
2120 AssertMsgFailedBreakStmt(("Invalid bytes per sample: %RU8\n", pProps->cbSampleX), rc = VERR_NOT_SUPPORTED);
2121 }
2122
2123 if (RT_SUCCESS(rc))
2124 {
2125 if (pdbRatio)
2126 *pdbRatio = dbRatio;
2127 }
2128
2129 return rc;
2130}
2131
2132/**
2133 * Normalizes a test set audio object's audio data, extended version.
2134 *
2135 * @returns VBox status code. On success the test set object will point to the (temporary) normalized file data.
2136 * @param pVerJob Verification job that contains \a pObj.
2137 * @param pObj Test set object to normalize.
2138 * @param pProps PCM properties to use for normalization.
2139 * @param cbSize Size (in bytes) of audio data to normalize.
2140 * @param dbNormalizePercent Normalization to achieve (in percent).
2141 *
2142 * @note The test set's file pointer must point to beginning of PCM data to normalize.
2143 */
2144static int audioTestObjFileNormalizeEx(PAUDIOTESTVERIFYJOB pVerJob,
2145 PAUDIOTESTOBJINT pObj, PPDMAUDIOPCMPROPS pProps, uint64_t cbSize, double dbNormalizePercent)
2146{
2147 /* Store normalized file into a temporary file. */
2148 char szFileDst[RTPATH_MAX];
2149 int rc = RTPathTemp(szFileDst, sizeof(szFileDst));
2150 AssertRCReturn(rc, rc);
2151
2152 rc = RTPathAppend(szFileDst, sizeof(szFileDst), "VBoxAudioTest-normalized-XXXXX.pcm");
2153 AssertRCReturn(rc, rc);
2154
2155 rc = RTFileCreateTemp(szFileDst, 0600);
2156 AssertRCReturn(rc, rc);
2157
2158 RTFILE hFileDst;
2159 rc = RTFileOpen(&hFileDst, szFileDst, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
2160 AssertRCReturn(rc, rc);
2161
2162 double dbRatio = 0.0;
2163 rc = audioTestFileNormalizePCM(pObj->File.hFile, pProps, cbSize, dbNormalizePercent, hFileDst, &dbRatio);
2164 if (RT_SUCCESS(rc))
2165 {
2166 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Normalized '%s' (ratio is %u.%02u%%)\n",
2167 pObj->szName, (unsigned)dbRatio, (unsigned)(dbRatio * 100) % 100);
2168 AssertRC(rc2);
2169 }
2170
2171 int rc2 = RTFileClose(hFileDst);
2172 if (RT_SUCCESS(rc))
2173 rc = rc2;
2174
2175 if (RT_SUCCESS(rc))
2176 {
2177 /* Close the original test set object and use the (temporary) normalized file instead now. */
2178 rc = audioTestObjClose(pObj);
2179 if (RT_SUCCESS(rc))
2180 rc = audioTestObjOpenFile(pObj, szFileDst);
2181 }
2182
2183 return rc;
2184}
2185
2186/**
2187 * Normalizes a test set audio object's audio data.
2188 *
2189 * @returns VBox status code.
2190 * @param pVerJob Verification job that contains \a pObj.
2191 * @param pObj Test set object to normalize.
2192 * @param pProps PCM properties to use for normalization.
2193 *
2194 * @note The test set's file pointer must point to beginning of PCM data to normalize.
2195 */
2196static int audioTestObjFileNormalize(PAUDIOTESTVERIFYJOB pVerJob, PAUDIOTESTOBJINT pObj, PPDMAUDIOPCMPROPS pProps)
2197{
2198 return audioTestObjFileNormalizeEx(pVerJob,
2199 pObj, pProps, 0 /* cbSize, 0 means all */, 100.0 /* dbNormalizePercent */);
2200}
2201
2202/**
2203 * Structure for keeping file comparison parameters for one file.
2204 */
2205typedef struct AUDIOTESTFILECMPPARMS
2206{
2207 /** File handle to file to compare. */
2208 RTFILE hFile;
2209 /** Absolute offset (in bytes) to start comparing.
2210 * Ignored when set to 0. */
2211 uint64_t offStart;
2212 /** Size (in bytes) of area to compare.
2213 * Starts at \a offStart. */
2214 uint64_t cbSize;
2215} AUDIOTESTFILECMPPARMS;
2216/** Pointer to file comparison parameters for one file. */
2217typedef AUDIOTESTFILECMPPARMS *PAUDIOTESTFILECMPPARMS;
2218
2219/**
2220 * Finds differences in two audio test files by binary comparing chunks.
2221 *
2222 * @returns Number of differences. 0 means they are equal (but not necessarily identical).
2223 * @param pVerJob Verification job to verify PCM data for.
2224 * @param pCmpA File comparison parameters to file A to compare file B with.
2225 * @param pCmpB File comparison parameters to file B to compare file A with.
2226 * @param pToneParms Tone parameters to use for comparison.
2227 */
2228static uint32_t audioTestFilesFindDiffsBinary(PAUDIOTESTVERIFYJOB pVerJob,
2229 PAUDIOTESTFILECMPPARMS pCmpA, PAUDIOTESTFILECMPPARMS pCmpB,
2230 PAUDIOTESTTONEPARMS pToneParms)
2231{
2232 uint8_t auBufA[_4K];
2233 uint8_t auBufB[_4K];
2234
2235 int rc = RTFileSeek(pCmpA->hFile, pCmpA->offStart, RTFILE_SEEK_BEGIN, NULL);
2236 AssertRC(rc);
2237
2238 rc = RTFileSeek(pCmpB->hFile, pCmpB->offStart, RTFILE_SEEK_BEGIN, NULL);
2239 AssertRC(rc);
2240
2241 RT_NOREF(pToneParms);
2242 uint32_t const cbChunkSize = PDMAudioPropsFrameSize(&pToneParms->Props); /* Use the audio frame size as chunk size. */
2243
2244 uint64_t offCur = 0;
2245 uint64_t offLastDiff = UINT64_MAX;
2246 uint32_t cDiffs = 0;
2247 uint64_t cbToCompare = RT_MIN(pCmpA->cbSize, pCmpB->cbSize);
2248
2249 while (cbToCompare)
2250 {
2251 size_t cbReadA;
2252 rc = RTFileRead(pCmpA->hFile, auBufA, RT_MIN(cbToCompare, cbChunkSize), &cbReadA);
2253 AssertRCBreak(rc);
2254 size_t cbReadB;
2255 rc = RTFileRead(pCmpB->hFile, auBufB, RT_MIN(cbToCompare, cbChunkSize), &cbReadB);
2256 AssertRCBreak(rc);
2257 AssertBreakStmt(cbReadA == cbReadB, rc = VERR_INVALID_PARAMETER); /** @todo Find a better rc. */
2258
2259 if (memcmp(auBufA, auBufB, RT_MIN(cbReadA, cbReadB)) != 0)
2260 {
2261 if (offLastDiff == UINT64_MAX) /* No consequitive different chunk? Count as new then. */
2262 {
2263 cDiffs++;
2264 offLastDiff = offCur;
2265 }
2266 }
2267 else /* Reset and count next difference as new then. */
2268 {
2269 if (cDiffs)
2270 {
2271 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Chunks differ: A @ %#x vs. B @ %#x [%08RU64-%08RU64] (%RU64 bytes, %RU64ms)",
2272 pCmpA->offStart, pCmpB->offStart, offLastDiff, offCur,
2273 offCur - offLastDiff, PDMAudioPropsBytesToMilli(&pToneParms->Props, offCur - offLastDiff));
2274 AssertRC(rc2);
2275 }
2276 offLastDiff = UINT64_MAX;
2277 }
2278
2279 AssertBreakStmt(cbToCompare >= cbReadA, VERR_INTERNAL_ERROR);
2280 cbToCompare -= cbReadA;
2281 offCur += cbReadA;
2282 }
2283
2284 /* If we didn't mention the last diff yet, do so now. */
2285 if ( offLastDiff != UINT64_MAX
2286 && cDiffs)
2287 {
2288 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Chunks differ: A @ %#x vs. B @ %#x [%08RU64-%08RU64] (%RU64 bytes, %RU64ms)",
2289 pCmpA->offStart, pCmpB->offStart, offLastDiff, offCur,
2290 offCur - offLastDiff, PDMAudioPropsBytesToMilli(&pToneParms->Props, offCur - offLastDiff));
2291 AssertRC(rc2);
2292 }
2293
2294 return cDiffs;
2295}
2296
2297#define CHECK_RC_MAYBE_RET(a_rc, a_pVerJob) \
2298 if (RT_FAILURE(a_rc)) \
2299 { \
2300 if (!a_pVerJob->Opts.fKeepGoing) \
2301 return VINF_SUCCESS; \
2302 }
2303
2304#define CHECK_RC_MSG_MAYBE_RET(a_rc, a_pVerJob, a_Msg) \
2305 if (RT_FAILURE(a_rc)) \
2306 { \
2307 int rc3 = audioTestErrorDescAddError(a_pVerJob->pErr, a_pVerJob->idxTest, a_Msg); \
2308 AssertRC(rc3); \
2309 if (!a_pVerJob->Opts.fKeepGoing) \
2310 return VINF_SUCCESS; \
2311 }
2312
2313#define CHECK_RC_MSG_VA_MAYBE_RET(a_rc, a_pVerJob, a_Msg, ...) \
2314 if (RT_FAILURE(a_rc)) \
2315 { \
2316 int rc3 = audioTestErrorDescAddError(a_pVerJob->pErr, a_pVerJob->idxTest, a_Msg, __VA_ARGS__); \
2317 AssertRC(rc3); \
2318 if (!a_pVerJob->Opts.fKeepGoing) \
2319 return VINF_SUCCESS; \
2320
2321/**
2322 * Does the actual PCM data verification of a test tone.
2323 *
2324 * @returns VBox status code.
2325 * @param pVerJob Verification job to verify PCM data for.
2326 * @param phTestA Test handle A of test to verify PCM data for.
2327 * @param phTestB Test handle B of test to verify PCM data for.
2328 */
2329static int audioTestVerifyTestToneData(PAUDIOTESTVERIFYJOB pVerJob, PAUDIOTESTOBJINT phTestA, PAUDIOTESTOBJINT phTestB)
2330{
2331 int rc;
2332
2333 /** @todo For now ASSUME that we only have one object per test. */
2334
2335 AUDIOTESTOBJINT ObjA;
2336 rc = audioTestObjGetChild(phTestA, 0 /* idxObj */, &ObjA);
2337 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to get object A");
2338
2339 rc = audioTestObjOpen(&ObjA);
2340 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to open object A");
2341
2342 AUDIOTESTOBJINT ObjB;
2343 rc = audioTestObjGetChild(phTestB, 0 /* idxObj */, &ObjB);
2344 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to get object B");
2345
2346 rc = audioTestObjOpen(&ObjB);
2347 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to open object B");
2348
2349 /*
2350 * Start with most obvious methods first.
2351 */
2352 uint64_t cbSizeA, cbSizeB;
2353 rc = RTFileQuerySize(ObjA.File.hFile, &cbSizeA);
2354 AssertRCReturn(rc, rc);
2355 rc = RTFileQuerySize(ObjB.File.hFile, &cbSizeB);
2356 AssertRCReturn(rc, rc);
2357
2358 if (!cbSizeA)
2359 {
2360 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "File '%s' is empty", ObjA.szName);
2361 AssertRC(rc2);
2362 }
2363
2364 if (!cbSizeB)
2365 {
2366 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "File '%s' is empty", ObjB.szName);
2367 AssertRC(rc2);
2368 }
2369
2370 if (cbSizeA != cbSizeB)
2371 {
2372 size_t const cbDiffAbs = cbSizeA > cbSizeB ? cbSizeA - cbSizeB : cbSizeB - cbSizeA;
2373
2374 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File '%s' is %zu bytes (%RU64ms)",
2375 ObjA.szName, cbSizeA, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, cbSizeA));
2376 AssertRC(rc2);
2377 rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File '%s' is %zu bytes (%RU64ms)",
2378 ObjB.szName, cbSizeB, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, cbSizeB));
2379 AssertRC(rc2);
2380
2381 uint8_t const uSizeDiffPercentAbs
2382 = cbSizeA > cbSizeB ? 100 - ((cbSizeB * 100) / cbSizeA) : 100 - ((cbSizeA * 100) / cbSizeB);
2383
2384 if (uSizeDiffPercentAbs > pVerJob->Opts.uMaxSizePercent)
2385 {
2386 rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
2387 "File '%s' is %RU8%% (%zu bytes, %RU64ms) %s than '%s' (threshold is %RU8%%)",
2388 ObjA.szName,
2389 uSizeDiffPercentAbs,
2390 cbDiffAbs, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, (uint32_t)cbDiffAbs),
2391 cbSizeA > cbSizeB ? "bigger" : "smaller",
2392 ObjB.szName, pVerJob->Opts.uMaxSizePercent);
2393 AssertRC(rc2);
2394 }
2395 }
2396
2397 /* Do normalization first if enabled. */
2398 if (pVerJob->Opts.fNormalize)
2399 {
2400 rc = audioTestObjFileNormalize(pVerJob, &ObjA, &pVerJob->PCMProps);
2401 if (RT_SUCCESS(rc))
2402 rc = audioTestObjFileNormalize(pVerJob, &ObjB, &pVerJob->PCMProps);
2403 }
2404
2405 /** @todo For now we only support comparison of data which do have identical PCM properties! */
2406
2407 AUDIOTESTTONEPARMS ToneParmsA;
2408 RT_ZERO(ToneParmsA);
2409 ToneParmsA.Props = pVerJob->PCMProps;
2410
2411 size_t cbSearchWindow = PDMAudioPropsMilliToBytes(&ToneParmsA.Props, pVerJob->Opts.msSearchWindow);
2412
2413 AUDIOTESTFILECMPPARMS FileA;
2414 RT_ZERO(FileA);
2415 FileA.hFile = ObjA.File.hFile;
2416 FileA.offStart = audioTestToneFileFind(ObjA.File.hFile,
2417 true /* fFindSilence */, 0 /* uOff */, &ToneParmsA, cbSearchWindow);
2418 FileA.cbSize = RT_MIN(audioTestToneFileFind(ObjA.File.hFile,
2419 false /* fFindSilence */, FileA.offStart, &ToneParmsA, cbSearchWindow),
2420 cbSizeA);
2421
2422 AUDIOTESTTONEPARMS ToneParmsB;
2423 RT_ZERO(ToneParmsB);
2424 ToneParmsB.Props = pVerJob->PCMProps;
2425
2426 AUDIOTESTFILECMPPARMS FileB;
2427 RT_ZERO(FileB);
2428 FileB.hFile = ObjB.File.hFile;
2429 FileB.offStart = audioTestToneFileFind(ObjB.File.hFile,
2430 true /* fFindSilence */, 0 /* uOff */, &ToneParmsB, cbSearchWindow);
2431 FileB.cbSize = RT_MIN(audioTestToneFileFind(ObjB.File.hFile,
2432 false /* fFindSilence */, FileB.offStart, &ToneParmsB, cbSearchWindow),
2433 cbSizeB);
2434
2435 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File A ('%s'): uOff=%RU64 (%#x), cbSize=%RU64 (%#x), cbFileSize=%RU64\n",
2436 ObjA.szName, FileA.offStart, FileA.offStart, FileA.cbSize, FileA.cbSize, cbSizeA);
2437 AssertRC(rc2);
2438
2439 rc = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File B ('%s'): uOff=%RU64 (%#x), cbSize=%RU64 (%#x), cbFileSize=%RU64\n",
2440 ObjB.szName, FileB.offStart, FileB.offStart, FileB.cbSize, FileB.cbSize, cbSizeB);
2441 AssertRC(rc2);
2442
2443 uint32_t const cDiffs = audioTestFilesFindDiffsBinary(pVerJob, &FileA, &FileB, &ToneParmsA);
2444
2445 if (cDiffs > pVerJob->Opts.cMaxDiff)
2446 {
2447 rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
2448 "Files '%s' and '%s' have too many different chunks (got %RU32, expected %RU32)",
2449 ObjA.szName, ObjB.szName, cDiffs, pVerJob->Opts.cMaxDiff);
2450 AssertRC(rc2);
2451 }
2452
2453 if (AudioTestErrorDescFailed(pVerJob->pErr))
2454 {
2455 rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Files '%s' and '%s' do not match",
2456 ObjA.szName, ObjB.szName);
2457 AssertRC(rc2);
2458 }
2459
2460 rc = audioTestObjClose(&ObjA);
2461 AssertRCReturn(rc, rc);
2462 rc = audioTestObjClose(&ObjB);
2463 AssertRCReturn(rc, rc);
2464
2465 return rc;
2466}
2467
2468/**
2469 * Verifies a test tone test.
2470 *
2471 * @returns VBox status code.
2472 * @returns Error if the verification failed and test verification job has fKeepGoing not set.
2473 * @retval VERR_
2474 * @param pVerJob Verification job to verify test tone for.
2475 * @param phTestA Test handle of test tone A to verify tone B with.
2476 * @param phTestB Test handle of test tone B to verify tone A with.*
2477 */
2478static int audioTestVerifyTestTone(PAUDIOTESTVERIFYJOB pVerJob, PAUDIOTESTOBJINT phTestA, PAUDIOTESTOBJINT phTestB)
2479{
2480 int rc;
2481
2482 /*
2483 * Verify test parameters.
2484 * More important items have precedence.
2485 */
2486 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "error_rc", "0", "Test was reported as failed");
2487 CHECK_RC_MAYBE_RET(rc, pVerJob);
2488 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "obj_count", NULL, "Object counts don't match");
2489 CHECK_RC_MAYBE_RET(rc, pVerJob);
2490 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_freq_hz", NULL, "Tone frequency doesn't match");
2491 CHECK_RC_MAYBE_RET(rc, pVerJob);
2492 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_prequel_ms", NULL, "Tone prequel (ms) doesn't match");
2493 CHECK_RC_MAYBE_RET(rc, pVerJob);
2494 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_duration_ms", NULL, "Tone duration (ms) doesn't match");
2495 CHECK_RC_MAYBE_RET(rc, pVerJob);
2496 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_sequel_ms", NULL, "Tone sequel (ms) doesn't match");
2497 CHECK_RC_MAYBE_RET(rc, pVerJob);
2498 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_volume_percent", NULL, "Tone volume (percent) doesn't match");
2499 CHECK_RC_MAYBE_RET(rc, pVerJob);
2500 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_hz", NULL, "Tone PCM Hz doesn't match");
2501 CHECK_RC_MAYBE_RET(rc, pVerJob);
2502 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_channels", NULL, "Tone PCM channels don't match");
2503 CHECK_RC_MAYBE_RET(rc, pVerJob);
2504 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_bits", NULL, "Tone PCM bits don't match");
2505 CHECK_RC_MAYBE_RET(rc, pVerJob);
2506 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_is_signed", NULL, "Tone PCM signed bit doesn't match");
2507 CHECK_RC_MAYBE_RET(rc, pVerJob);
2508
2509 rc = audioTestObjGetTonePcmProps(phTestA, &pVerJob->PCMProps);
2510 CHECK_RC_MAYBE_RET(rc, pVerJob);
2511
2512 /*
2513 * Now the fun stuff, PCM data analysis.
2514 */
2515 rc = audioTestVerifyTestToneData(pVerJob, phTestA, phTestB);
2516 if (RT_FAILURE(rc))
2517 {
2518 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "Verififcation of test tone data failed\n");
2519 AssertRC(rc2);
2520 }
2521
2522 return VINF_SUCCESS;
2523}
2524
2525/**
2526 * Verifies an opened audio test set, extended version.
2527 *
2528 * @returns VBox status code.
2529 * @param pSetA Test set A to verify.
2530 * @param pSetB Test set to verify test set A with.
2531 * @param pOpts Verification options to use.
2532 * @param pErrDesc Where to return the test verification errors.
2533 *
2534 * @note Test verification errors have to be checked for errors, regardless of the
2535 * actual return code.
2536 * @note Uses the standard verification options. Use AudioTestSetVerifyEx() to specify
2537 * own options.
2538 */
2539int AudioTestSetVerifyEx(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB, PAUDIOTESTVERIFYOPTS pOpts, PAUDIOTESTERRORDESC pErrDesc)
2540{
2541 AssertPtrReturn(pSetA, VERR_INVALID_POINTER);
2542 AssertPtrReturn(pSetB, VERR_INVALID_POINTER);
2543 AssertReturn(audioTestManifestIsOpen(pSetA), VERR_WRONG_ORDER);
2544 AssertReturn(audioTestManifestIsOpen(pSetB), VERR_WRONG_ORDER);
2545 AssertPtrReturn(pOpts, VERR_INVALID_POINTER);
2546
2547 /* We ASSUME the caller has not init'd pErrDesc. */
2548 audioTestErrorDescInit(pErrDesc);
2549
2550 AUDIOTESTVERIFYJOB VerJob;
2551 RT_ZERO(VerJob);
2552 VerJob.pErr = pErrDesc;
2553 VerJob.pSetA = pSetA;
2554 VerJob.pSetB = pSetB;
2555
2556 memcpy(&VerJob.Opts, pOpts, sizeof(AUDIOTESTVERIFYOPTS));
2557
2558 PAUDIOTESTVERIFYJOB pVerJob = &VerJob;
2559
2560 int rc;
2561
2562 /*
2563 * Compare obvious values first.
2564 */
2565 AUDIOTESTOBJINT hHdrA;
2566 rc = audioTestSetGetSection(pVerJob->pSetA, AUDIOTEST_SEC_HDR_STR, &hHdrA);
2567 CHECK_RC_MAYBE_RET(rc, pVerJob);
2568
2569 AUDIOTESTOBJINT hHdrB;
2570 rc = audioTestSetGetSection(pVerJob->pSetB, AUDIOTEST_SEC_HDR_STR, &hHdrB);
2571 CHECK_RC_MAYBE_RET(rc, pVerJob);
2572
2573 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "magic", "vkat_ini", "Manifest magic wrong");
2574 CHECK_RC_MAYBE_RET(rc, pVerJob);
2575 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "ver", "1" , "Manifest version wrong");
2576 CHECK_RC_MAYBE_RET(rc, pVerJob);
2577 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "tag", NULL, "Manifest tags don't match");
2578 CHECK_RC_MAYBE_RET(rc, pVerJob);
2579 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "test_count", NULL, "Test counts don't match");
2580 CHECK_RC_MAYBE_RET(rc, pVerJob);
2581 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "obj_count", NULL, "Object counts don't match");
2582 CHECK_RC_MAYBE_RET(rc, pVerJob);
2583
2584 /*
2585 * Compare ran tests.
2586 */
2587 uint32_t cTests;
2588 rc = audioTestObjGetUInt32(&hHdrA, "test_count", &cTests);
2589 AssertRCReturn(rc, rc);
2590
2591 for (uint32_t i = 0; i < cTests; i++)
2592 {
2593 VerJob.idxTest = i;
2594
2595 AUDIOTESTOBJINT hTestA;
2596 rc = audioTestSetGetTest(VerJob.pSetA, i, &hTestA);
2597 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test A not found");
2598
2599 AUDIOTESTOBJINT hTestB;
2600 rc = audioTestSetGetTest(VerJob.pSetB, i, &hTestB);
2601 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test B not found");
2602
2603 AUDIOTESTTYPE enmTestTypeA = AUDIOTESTTYPE_INVALID;
2604 rc = audioTestObjGetUInt32(&hTestA, "test_type", (uint32_t *)&enmTestTypeA);
2605 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test type A not found");
2606
2607 AUDIOTESTTYPE enmTestTypeB = AUDIOTESTTYPE_INVALID;
2608 rc = audioTestObjGetUInt32(&hTestB, "test_type", (uint32_t *)&enmTestTypeB);
2609 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test type B not found");
2610
2611 switch (enmTestTypeA)
2612 {
2613 case AUDIOTESTTYPE_TESTTONE_PLAY:
2614 {
2615 if (enmTestTypeB == AUDIOTESTTYPE_TESTTONE_RECORD)
2616 rc = audioTestVerifyTestTone(&VerJob, &hTestA, &hTestB);
2617 else
2618 rc = audioTestErrorDescAddError(pErrDesc, i, "Playback test types don't match (set A=%#x, set B=%#x)",
2619 enmTestTypeA, enmTestTypeB);
2620 break;
2621 }
2622
2623 case AUDIOTESTTYPE_TESTTONE_RECORD:
2624 {
2625 if (enmTestTypeB == AUDIOTESTTYPE_TESTTONE_PLAY)
2626 rc = audioTestVerifyTestTone(&VerJob, &hTestB, &hTestA);
2627 else
2628 rc = audioTestErrorDescAddError(pErrDesc, i, "Recording test types don't match (set A=%#x, set B=%#x)",
2629 enmTestTypeA, enmTestTypeB);
2630 break;
2631 }
2632
2633 case AUDIOTESTTYPE_INVALID:
2634 rc = VERR_INVALID_PARAMETER;
2635 break;
2636
2637 default:
2638 rc = VERR_NOT_IMPLEMENTED;
2639 break;
2640 }
2641
2642 AssertRC(rc);
2643 }
2644
2645 /* Only return critical stuff not related to actual testing here. */
2646 return VINF_SUCCESS;
2647}
2648
2649/**
2650 * Initializes audio test verification options in a strict manner.
2651 *
2652 * @param pOpts Verification options to initialize.
2653 */
2654void AudioTestSetVerifyOptsInitStrict(PAUDIOTESTVERIFYOPTS pOpts)
2655{
2656 RT_BZERO(pOpts, sizeof(AUDIOTESTVERIFYOPTS));
2657
2658 pOpts->fKeepGoing = true;
2659 pOpts->fNormalize = true;
2660 pOpts->cMaxDiff = 0; /* By default we're very strict and consider any diff as being erroneous. */
2661 pOpts->uMaxSizePercent = 1; /* 1% is okay for us; might be due to any buffering / setup phase. */
2662
2663 /* We use a search window of 10ms by default for finding (non-)silent parts. */
2664 pOpts->msSearchWindow = 10;
2665
2666}
2667
2668/**
2669 * Initializes audio test verification options with default values (strict!).
2670 *
2671 * @param pOpts Verification options to initialize.
2672 */
2673void AudioTestSetVerifyOptsInit(PAUDIOTESTVERIFYOPTS pOpts)
2674{
2675 AudioTestSetVerifyOptsInitStrict(pOpts);
2676}
2677
2678/**
2679 * Returns whether two audio test verification options are equal.
2680 *
2681 * @returns \c true if equal, or \c false if not.
2682 * @param pOptsA Options A to compare.
2683 * @param pOptsB Options B to compare Options A with.
2684 */
2685bool AudioTestSetVerifyOptsAreEqual(PAUDIOTESTVERIFYOPTS pOptsA, PAUDIOTESTVERIFYOPTS pOptsB)
2686{
2687 if (pOptsA == pOptsB)
2688 return true;
2689
2690 return ( pOptsA->cMaxDiff == pOptsB->cMaxDiff
2691 && pOptsA->fKeepGoing == pOptsB->fKeepGoing
2692 && pOptsA->uMaxDiffPercent == pOptsB->uMaxDiffPercent
2693 && pOptsA->uMaxSizePercent == pOptsB->uMaxSizePercent);
2694}
2695
2696/**
2697 * Verifies an opened audio test set.
2698 *
2699 * @returns VBox status code.
2700 * @param pSetA Test set A to verify.
2701 * @param pSetB Test set to verify test set A with.
2702 * @param pErrDesc Where to return the test verification errors.
2703 *
2704 * @note Test verification errors have to be checked for errors, regardless of the
2705 * actual return code.
2706 * @note Uses the standard verification options (strict!).
2707 * Use AudioTestSetVerifyEx() to specify own options.
2708 */
2709int AudioTestSetVerify(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB, PAUDIOTESTERRORDESC pErrDesc)
2710{
2711 AUDIOTESTVERIFYOPTS Opts;
2712 AudioTestSetVerifyOptsInitStrict(&Opts);
2713
2714 return AudioTestSetVerifyEx(pSetA,pSetB, &Opts, pErrDesc);
2715}
2716
2717#undef CHECK_RC_MAYBE_RET
2718#undef CHECK_RC_MSG_MAYBE_RET
2719
2720
2721/*********************************************************************************************************************************
2722* WAVE File Reader. *
2723*********************************************************************************************************************************/
2724
2725/**
2726 * Counts the number of set bits in @a fMask.
2727 */
2728static unsigned audioTestWaveCountBits(uint32_t fMask)
2729{
2730 unsigned cBits = 0;
2731 while (fMask)
2732 {
2733 if (fMask & 1)
2734 cBits++;
2735 fMask >>= 1;
2736 }
2737 return cBits;
2738}
2739
2740/**
2741 * Opens a wave (.WAV) file for reading.
2742 *
2743 * @returns VBox status code.
2744 * @param pszFile The file to open.
2745 * @param pWaveFile The open wave file structure to fill in on success.
2746 * @param pErrInfo Where to return addition error details on failure.
2747 */
2748int AudioTestWaveFileOpen(const char *pszFile, PAUDIOTESTWAVEFILE pWaveFile, PRTERRINFO pErrInfo)
2749{
2750 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
2751 RT_ZERO(pWaveFile->Props);
2752 pWaveFile->hFile = NIL_RTFILE;
2753 int rc = RTFileOpen(&pWaveFile->hFile, pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
2754 if (RT_FAILURE(rc))
2755 return RTErrInfoSet(pErrInfo, rc, "RTFileOpen failed");
2756 uint64_t cbFile = 0;
2757 rc = RTFileQuerySize(pWaveFile->hFile, &cbFile);
2758 if (RT_SUCCESS(rc))
2759 {
2760 union
2761 {
2762 uint8_t ab[512];
2763 struct
2764 {
2765 RTRIFFHDR Hdr;
2766 union
2767 {
2768 RTRIFFWAVEFMTCHUNK Fmt;
2769 RTRIFFWAVEFMTEXTCHUNK FmtExt;
2770 } u;
2771 } Wave;
2772 RTRIFFLIST List;
2773 RTRIFFCHUNK Chunk;
2774 RTRIFFWAVEDATACHUNK Data;
2775 } uBuf;
2776
2777 rc = RTFileRead(pWaveFile->hFile, &uBuf.Wave, sizeof(uBuf.Wave), NULL);
2778 if (RT_SUCCESS(rc))
2779 {
2780 rc = VERR_VFS_UNKNOWN_FORMAT;
2781 if ( uBuf.Wave.Hdr.uMagic == RTRIFFHDR_MAGIC
2782 && uBuf.Wave.Hdr.uFileType == RTRIFF_FILE_TYPE_WAVE
2783 && uBuf.Wave.u.Fmt.Chunk.uMagic == RTRIFFWAVEFMT_MAGIC
2784 && uBuf.Wave.u.Fmt.Chunk.cbChunk >= sizeof(uBuf.Wave.u.Fmt.Data))
2785 {
2786 if (uBuf.Wave.Hdr.cbFile != cbFile - sizeof(RTRIFFCHUNK))
2787 RTErrInfoSetF(pErrInfo, rc, "File size mismatch: %#x, actual %#RX64 (ignored)",
2788 uBuf.Wave.Hdr.cbFile, cbFile - sizeof(RTRIFFCHUNK));
2789 rc = VERR_VFS_BOGUS_FORMAT;
2790 if ( uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_PCM
2791 && uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_EXTENSIBLE)
2792 RTErrInfoSetF(pErrInfo, rc, "Unsupported uFormatTag value: %#x (expected %#x or %#x)",
2793 uBuf.Wave.u.Fmt.Data.uFormatTag, RTRIFFWAVEFMT_TAG_PCM, RTRIFFWAVEFMT_TAG_EXTENSIBLE);
2794 else if ( uBuf.Wave.u.Fmt.Data.cBitsPerSample != 8
2795 && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 16
2796 /* && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 24 - not supported by our stack */
2797 && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 32)
2798 RTErrInfoSetF(pErrInfo, rc, "Unsupported cBitsPerSample value: %u", uBuf.Wave.u.Fmt.Data.cBitsPerSample);
2799 else if ( uBuf.Wave.u.Fmt.Data.cChannels < 1
2800 || uBuf.Wave.u.Fmt.Data.cChannels >= 16)
2801 RTErrInfoSetF(pErrInfo, rc, "Unsupported cChannels value: %u (expected 1..15)", uBuf.Wave.u.Fmt.Data.cChannels);
2802 else if ( uBuf.Wave.u.Fmt.Data.uHz < 4096
2803 || uBuf.Wave.u.Fmt.Data.uHz > 768000)
2804 RTErrInfoSetF(pErrInfo, rc, "Unsupported uHz value: %u (expected 4096..768000)", uBuf.Wave.u.Fmt.Data.uHz);
2805 else if (uBuf.Wave.u.Fmt.Data.cbFrame != uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8)
2806 RTErrInfoSetF(pErrInfo, rc, "Invalid cbFrame value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbFrame,
2807 uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8);
2808 else if (uBuf.Wave.u.Fmt.Data.cbRate != uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz)
2809 RTErrInfoSetF(pErrInfo, rc, "Invalid cbRate value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbRate,
2810 uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz);
2811 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2812 && uBuf.Wave.u.FmtExt.Data.cbExtra < RTRIFFWAVEFMTEXT_EXTRA_SIZE)
2813 RTErrInfoSetF(pErrInfo, rc, "Invalid cbExtra value: %#x (expected at least %#x)",
2814 uBuf.Wave.u.FmtExt.Data.cbExtra, RTRIFFWAVEFMTEXT_EXTRA_SIZE);
2815 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2816 && audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask) != uBuf.Wave.u.Fmt.Data.cChannels)
2817 RTErrInfoSetF(pErrInfo, rc, "fChannelMask does not match cChannels: %#x (%u bits set) vs %u channels",
2818 uBuf.Wave.u.FmtExt.Data.fChannelMask,
2819 audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask), uBuf.Wave.u.Fmt.Data.cChannels);
2820 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2821 && RTUuidCompareStr(&uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM) != 0)
2822 RTErrInfoSetF(pErrInfo, rc, "SubFormat is not PCM: %RTuuid (expected %s)",
2823 &uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
2824 else
2825 {
2826 /*
2827 * Copy out the data we need from the file format structure.
2828 */
2829 PDMAudioPropsInit(&pWaveFile->Props, uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8, true /*fSigned*/,
2830 uBuf.Wave.u.Fmt.Data.cChannels, uBuf.Wave.u.Fmt.Data.uHz);
2831 pWaveFile->offSamples = sizeof(RTRIFFHDR) + sizeof(RTRIFFCHUNK) + uBuf.Wave.u.Fmt.Chunk.cbChunk;
2832
2833 /*
2834 * Pick up channel assignments if present.
2835 */
2836 if (uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE)
2837 {
2838 static unsigned const s_cStdIds = (unsigned)PDMAUDIOCHANNELID_END_STANDARD
2839 - (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD;
2840 unsigned iCh = 0;
2841 for (unsigned idCh = 0; idCh < 32 && iCh < uBuf.Wave.u.Fmt.Data.cChannels; idCh++)
2842 if (uBuf.Wave.u.FmtExt.Data.fChannelMask & RT_BIT_32(idCh))
2843 {
2844 pWaveFile->Props.aidChannels[iCh] = idCh < s_cStdIds
2845 ? idCh + (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD
2846 : (unsigned)PDMAUDIOCHANNELID_UNKNOWN;
2847 iCh++;
2848 }
2849 }
2850
2851 /*
2852 * Find the 'data' chunk with the audio samples.
2853 *
2854 * There can be INFO lists both preceeding this and succeeding
2855 * it, containing IART and other things we can ignored. Thus
2856 * we read a list header here rather than just a chunk header,
2857 * since it doesn't matter if we read 4 bytes extra as
2858 * AudioTestWaveFileRead uses RTFileReadAt anyway.
2859 */
2860 rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
2861 for (uint32_t i = 0;
2862 i < 128
2863 && RT_SUCCESS(rc)
2864 && uBuf.Chunk.uMagic != RTRIFFWAVEDATACHUNK_MAGIC
2865 && (uint64_t)uBuf.Chunk.cbChunk + sizeof(RTRIFFCHUNK) * 2 <= cbFile - pWaveFile->offSamples;
2866 i++)
2867 {
2868 if ( uBuf.List.uMagic == RTRIFFLIST_MAGIC
2869 && uBuf.List.uListType == RTRIFFLIST_TYPE_INFO)
2870 { /*skip*/ }
2871 else if (uBuf.Chunk.uMagic == RTRIFFPADCHUNK_MAGIC)
2872 { /*skip*/ }
2873 else
2874 break;
2875 pWaveFile->offSamples += sizeof(RTRIFFCHUNK) + uBuf.Chunk.cbChunk;
2876 rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
2877 }
2878 if (RT_SUCCESS(rc))
2879 {
2880 pWaveFile->offSamples += sizeof(uBuf.Data.Chunk);
2881 pWaveFile->cbSamples = (uint32_t)cbFile - pWaveFile->offSamples;
2882
2883 rc = VERR_VFS_BOGUS_FORMAT;
2884 if ( uBuf.Data.Chunk.uMagic == RTRIFFWAVEDATACHUNK_MAGIC
2885 && uBuf.Data.Chunk.cbChunk <= pWaveFile->cbSamples
2886 && PDMAudioPropsIsSizeAligned(&pWaveFile->Props, uBuf.Data.Chunk.cbChunk))
2887 {
2888 pWaveFile->cbSamples = uBuf.Data.Chunk.cbChunk;
2889
2890 /*
2891 * We're good!
2892 */
2893 pWaveFile->offCur = 0;
2894 pWaveFile->fReadMode = true;
2895 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC;
2896 return VINF_SUCCESS;
2897 }
2898
2899 RTErrInfoSetF(pErrInfo, rc, "Bad data header: uMagic=%#x (expected %#x), cbChunk=%#x (max %#RX64, align %u)",
2900 uBuf.Data.Chunk.uMagic, RTRIFFWAVEDATACHUNK_MAGIC,
2901 uBuf.Data.Chunk.cbChunk, pWaveFile->cbSamples, PDMAudioPropsFrameSize(&pWaveFile->Props));
2902 }
2903 else
2904 RTErrInfoSet(pErrInfo, rc, "Failed to read data header");
2905 }
2906 }
2907 else
2908 RTErrInfoSetF(pErrInfo, rc, "Bad file header: uMagic=%#x (vs. %#x), uFileType=%#x (vs %#x), uFmtMagic=%#x (vs %#x) cbFmtChunk=%#x (min %#x)",
2909 uBuf.Wave.Hdr.uMagic, RTRIFFHDR_MAGIC, uBuf.Wave.Hdr.uFileType, RTRIFF_FILE_TYPE_WAVE,
2910 uBuf.Wave.u.Fmt.Chunk.uMagic, RTRIFFWAVEFMT_MAGIC,
2911 uBuf.Wave.u.Fmt.Chunk.cbChunk, sizeof(uBuf.Wave.u.Fmt.Data));
2912 }
2913 else
2914 rc = RTErrInfoSet(pErrInfo, rc, "Failed to read file header");
2915 }
2916 else
2917 rc = RTErrInfoSet(pErrInfo, rc, "Failed to query file size");
2918
2919 RTFileClose(pWaveFile->hFile);
2920 pWaveFile->hFile = NIL_RTFILE;
2921 return rc;
2922}
2923
2924
2925/**
2926 * Creates a new wave file.
2927 *
2928 * @returns VBox status code.
2929 * @param pszFile The filename.
2930 * @param pProps The audio format properties.
2931 * @param pWaveFile The wave file structure to fill in on success.
2932 * @param pErrInfo Where to return addition error details on failure.
2933 */
2934int AudioTestWaveFileCreate(const char *pszFile, PCPDMAUDIOPCMPROPS pProps, PAUDIOTESTWAVEFILE pWaveFile, PRTERRINFO pErrInfo)
2935{
2936 /*
2937 * Construct the file header first (we'll do some input validation
2938 * here, so better do it before creating the file).
2939 */
2940 struct
2941 {
2942 RTRIFFHDR Hdr;
2943 RTRIFFWAVEFMTEXTCHUNK FmtExt;
2944 RTRIFFCHUNK Data;
2945 } FileHdr;
2946
2947 FileHdr.Hdr.uMagic = RTRIFFHDR_MAGIC;
2948 FileHdr.Hdr.cbFile = 0; /* need to update this later */
2949 FileHdr.Hdr.uFileType = RTRIFF_FILE_TYPE_WAVE;
2950 FileHdr.FmtExt.Chunk.uMagic = RTRIFFWAVEFMT_MAGIC;
2951 FileHdr.FmtExt.Chunk.cbChunk = sizeof(RTRIFFWAVEFMTEXTCHUNK) - sizeof(RTRIFFCHUNK);
2952 FileHdr.FmtExt.Data.Core.uFormatTag = RTRIFFWAVEFMT_TAG_EXTENSIBLE;
2953 FileHdr.FmtExt.Data.Core.cChannels = PDMAudioPropsChannels(pProps);
2954 FileHdr.FmtExt.Data.Core.uHz = PDMAudioPropsHz(pProps);
2955 FileHdr.FmtExt.Data.Core.cbRate = PDMAudioPropsFramesToBytes(pProps, PDMAudioPropsHz(pProps));
2956 FileHdr.FmtExt.Data.Core.cbFrame = PDMAudioPropsFrameSize(pProps);
2957 FileHdr.FmtExt.Data.Core.cBitsPerSample = PDMAudioPropsSampleBits(pProps);
2958 FileHdr.FmtExt.Data.cbExtra = sizeof(FileHdr.FmtExt.Data) - sizeof(FileHdr.FmtExt.Data.Core);
2959 FileHdr.FmtExt.Data.cValidBitsPerSample = PDMAudioPropsSampleBits(pProps);
2960 FileHdr.FmtExt.Data.fChannelMask = 0;
2961 for (uintptr_t idxCh = 0; idxCh < FileHdr.FmtExt.Data.Core.cChannels; idxCh++)
2962 {
2963 PDMAUDIOCHANNELID const idCh = (PDMAUDIOCHANNELID)pProps->aidChannels[idxCh];
2964 if ( idCh >= PDMAUDIOCHANNELID_FIRST_STANDARD
2965 && idCh < PDMAUDIOCHANNELID_END_STANDARD)
2966 {
2967 if (!(FileHdr.FmtExt.Data.fChannelMask & RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD)))
2968 FileHdr.FmtExt.Data.fChannelMask |= RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD);
2969 else
2970 return RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Channel #%u repeats channel ID %d", idxCh, idCh);
2971 }
2972 else
2973 return RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Invalid channel ID %d for channel #%u", idCh, idxCh);
2974 }
2975
2976 RTUUID UuidTmp;
2977 int rc = RTUuidFromStr(&UuidTmp, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
2978 AssertRCReturn(rc, rc);
2979 FileHdr.FmtExt.Data.SubFormat = UuidTmp; /* (64-bit field maybe unaligned) */
2980
2981 FileHdr.Data.uMagic = RTRIFFWAVEDATACHUNK_MAGIC;
2982 FileHdr.Data.cbChunk = 0; /* need to update this later */
2983
2984 /*
2985 * Create the file and write the header.
2986 */
2987 pWaveFile->hFile = NIL_RTFILE;
2988 rc = RTFileOpen(&pWaveFile->hFile, pszFile, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
2989 if (RT_FAILURE(rc))
2990 return RTErrInfoSet(pErrInfo, rc, "RTFileOpen failed");
2991
2992 rc = RTFileWrite(pWaveFile->hFile, &FileHdr, sizeof(FileHdr), NULL);
2993 if (RT_SUCCESS(rc))
2994 {
2995 /*
2996 * Initialize the wave file structure.
2997 */
2998 pWaveFile->fReadMode = false;
2999 pWaveFile->offCur = 0;
3000 pWaveFile->offSamples = 0;
3001 pWaveFile->cbSamples = 0;
3002 pWaveFile->Props = *pProps;
3003 pWaveFile->offSamples = RTFileTell(pWaveFile->hFile);
3004 if (pWaveFile->offSamples != UINT32_MAX)
3005 {
3006 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC;
3007 return VINF_SUCCESS;
3008 }
3009 rc = RTErrInfoSet(pErrInfo, VERR_SEEK, "RTFileTell failed");
3010 }
3011 else
3012 RTErrInfoSet(pErrInfo, rc, "RTFileWrite failed writing header");
3013
3014 RTFileClose(pWaveFile->hFile);
3015 pWaveFile->hFile = NIL_RTFILE;
3016 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
3017
3018 RTFileDelete(pszFile);
3019 return rc;
3020}
3021
3022
3023/**
3024 * Closes a wave file.
3025 */
3026int AudioTestWaveFileClose(PAUDIOTESTWAVEFILE pWaveFile)
3027{
3028 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3029 int rcRet = VINF_SUCCESS;
3030 int rc;
3031
3032 /*
3033 * Update the size fields if writing.
3034 */
3035 if (!pWaveFile->fReadMode)
3036 {
3037 uint64_t cbFile = RTFileTell(pWaveFile->hFile);
3038 if (cbFile != UINT64_MAX)
3039 {
3040 uint32_t cbFile32 = cbFile - sizeof(RTRIFFCHUNK);
3041 rc = RTFileWriteAt(pWaveFile->hFile, RT_OFFSETOF(RTRIFFHDR, cbFile), &cbFile32, sizeof(cbFile32), NULL);
3042 AssertRCStmt(rc, rcRet = rc);
3043
3044 uint32_t cbSamples = cbFile - pWaveFile->offSamples;
3045 rc = RTFileWriteAt(pWaveFile->hFile, pWaveFile->offSamples - sizeof(uint32_t), &cbSamples, sizeof(cbSamples), NULL);
3046 AssertRCStmt(rc, rcRet = rc);
3047 }
3048 else
3049 rcRet = VERR_SEEK;
3050 }
3051
3052 /*
3053 * Close it.
3054 */
3055 rc = RTFileClose(pWaveFile->hFile);
3056 AssertRCStmt(rc, rcRet = rc);
3057
3058 pWaveFile->hFile = NIL_RTFILE;
3059 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
3060 return rcRet;
3061}
3062
3063/**
3064 * Reads samples from a wave file.
3065 *
3066 * @returns VBox status code. See RTVfsFileRead for EOF status handling.
3067 * @param pWaveFile The file to read from.
3068 * @param pvBuf Where to put the samples.
3069 * @param cbBuf How much to read at most.
3070 * @param pcbRead Where to return the actual number of bytes read,
3071 * optional.
3072 */
3073int AudioTestWaveFileRead(PAUDIOTESTWAVEFILE pWaveFile, void *pvBuf, size_t cbBuf, size_t *pcbRead)
3074{
3075 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3076 AssertReturn(pWaveFile->fReadMode, VERR_ACCESS_DENIED);
3077
3078 bool fEofAdjusted;
3079 if (pWaveFile->offCur + cbBuf <= pWaveFile->cbSamples)
3080 fEofAdjusted = false;
3081 else if (pcbRead)
3082 {
3083 fEofAdjusted = true;
3084 cbBuf = pWaveFile->cbSamples - pWaveFile->offCur;
3085 }
3086 else
3087 return VERR_EOF;
3088
3089 int rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples + pWaveFile->offCur, pvBuf, cbBuf, pcbRead);
3090 if (RT_SUCCESS(rc))
3091 {
3092 if (pcbRead)
3093 {
3094 pWaveFile->offCur += (uint32_t)*pcbRead;
3095 if (fEofAdjusted || cbBuf > *pcbRead)
3096 rc = VINF_EOF;
3097 else if (!cbBuf && pWaveFile->offCur == pWaveFile->cbSamples)
3098 rc = VINF_EOF;
3099 }
3100 else
3101 pWaveFile->offCur += (uint32_t)cbBuf;
3102 }
3103 return rc;
3104}
3105
3106
3107/**
3108 * Writes samples to a wave file.
3109 *
3110 * @returns VBox status code.
3111 * @param pWaveFile The file to write to.
3112 * @param pvBuf The samples to write.
3113 * @param cbBuf How many bytes of samples to write.
3114 */
3115int AudioTestWaveFileWrite(PAUDIOTESTWAVEFILE pWaveFile, const void *pvBuf, size_t cbBuf)
3116{
3117 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3118 AssertReturn(!pWaveFile->fReadMode, VERR_ACCESS_DENIED);
3119
3120 pWaveFile->cbSamples += (uint32_t)cbBuf;
3121 return RTFileWrite(pWaveFile->hFile, pvBuf, cbBuf, NULL);
3122}
3123
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