VirtualBox

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

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

Audio/Validation Kit: Added missing parameters to AudioTestSetVerifyOptsAreEqual(). ​bugref:10008

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 110.2 KB
 
1/* $Id: AudioTest.cpp 91534 2021-10-04 06:38:03Z 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.alldomusa.eu.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 uint32_t cDiffs = 0;
2242 uint64_t cbDiffs = 0;
2243
2244 RT_NOREF(pToneParms);
2245 uint32_t const cbChunkSize = PDMAudioPropsFrameSize(&pToneParms->Props); /* Use the audio frame size as chunk size. */
2246
2247 uint64_t offCur = 0;
2248 uint64_t offLastDiff = UINT64_MAX;
2249 uint64_t cbSize = RT_MIN(pCmpA->cbSize, pCmpB->cbSize);
2250 uint64_t cbToCompare = cbSize;
2251
2252 while (cbToCompare)
2253 {
2254 size_t cbReadA;
2255 rc = RTFileRead(pCmpA->hFile, auBufA, RT_MIN(cbToCompare, cbChunkSize), &cbReadA);
2256 AssertRCBreak(rc);
2257 size_t cbReadB;
2258 rc = RTFileRead(pCmpB->hFile, auBufB, RT_MIN(cbToCompare, cbChunkSize), &cbReadB);
2259 AssertRCBreak(rc);
2260 AssertBreakStmt(cbReadA == cbReadB, rc = VERR_INVALID_PARAMETER); /** @todo Find a better rc. */
2261
2262 if (memcmp(auBufA, auBufB, RT_MIN(cbReadA, cbReadB)) != 0)
2263 {
2264 if (offLastDiff == UINT64_MAX) /* No consequitive different chunk? Count as new then. */
2265 {
2266 cDiffs++;
2267 offLastDiff = offCur;
2268 }
2269 }
2270 else /* Reset and count next difference as new then. */
2271 {
2272 if (cDiffs)
2273 {
2274 uint32_t const cbDiff = offCur - offLastDiff;
2275 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Chunks differ: A @ %#x vs. B @ %#x [%08RU64-%08RU64] (%RU64 bytes, %RU64ms)",
2276 pCmpA->offStart, pCmpB->offStart, offLastDiff, offCur,
2277 cbDiff, PDMAudioPropsBytesToMilli(&pToneParms->Props, cbDiff));
2278 AssertRC(rc2);
2279
2280 cbDiffs += cbDiff;
2281 }
2282 offLastDiff = UINT64_MAX;
2283 }
2284
2285 AssertBreakStmt(cbToCompare >= cbReadA, VERR_INTERNAL_ERROR);
2286 cbToCompare -= cbReadA;
2287 offCur += cbReadA;
2288 }
2289
2290 /* If we didn't mention the last diff yet, do so now. */
2291 if ( offLastDiff != UINT64_MAX
2292 && cDiffs)
2293 {
2294 uint32_t const cbDiff = offCur - offLastDiff;
2295 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Chunks differ: A @ %#x vs. B @ %#x [%08RU64-%08RU64] (%RU64 bytes, %RU64ms)",
2296 pCmpA->offStart, pCmpB->offStart, offLastDiff, offCur,
2297 cbDiff, PDMAudioPropsBytesToMilli(&pToneParms->Props, cbDiff));
2298 AssertRC(rc2);
2299
2300 cbDiffs += cbDiff;
2301 }
2302
2303 if (cbDiffs)
2304 {
2305 uint8_t const uDiffPercent = (cbSize * 100) / cbDiffs;
2306 if (uDiffPercent > pVerJob->Opts.uMaxDiffPercent)
2307 {
2308 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Files binary-differ too much (expected maximum %RU8%%, got %RU8%%)",
2309 pVerJob->Opts.uMaxDiffPercent, uDiffPercent);
2310 AssertRC(rc2);
2311 }
2312 }
2313
2314 return cDiffs;
2315}
2316
2317#define CHECK_RC_MAYBE_RET(a_rc, a_pVerJob) \
2318 if (RT_FAILURE(a_rc)) \
2319 { \
2320 if (!a_pVerJob->Opts.fKeepGoing) \
2321 return VINF_SUCCESS; \
2322 }
2323
2324#define CHECK_RC_MSG_MAYBE_RET(a_rc, a_pVerJob, a_Msg) \
2325 if (RT_FAILURE(a_rc)) \
2326 { \
2327 int rc3 = audioTestErrorDescAddError(a_pVerJob->pErr, a_pVerJob->idxTest, a_Msg); \
2328 AssertRC(rc3); \
2329 if (!a_pVerJob->Opts.fKeepGoing) \
2330 return VINF_SUCCESS; \
2331 }
2332
2333#define CHECK_RC_MSG_VA_MAYBE_RET(a_rc, a_pVerJob, a_Msg, ...) \
2334 if (RT_FAILURE(a_rc)) \
2335 { \
2336 int rc3 = audioTestErrorDescAddError(a_pVerJob->pErr, a_pVerJob->idxTest, a_Msg, __VA_ARGS__); \
2337 AssertRC(rc3); \
2338 if (!a_pVerJob->Opts.fKeepGoing) \
2339 return VINF_SUCCESS; \
2340
2341/**
2342 * Does the actual PCM data verification of a test tone.
2343 *
2344 * @returns VBox status code.
2345 * @param pVerJob Verification job to verify PCM data for.
2346 * @param phTestA Test handle A of test to verify PCM data for.
2347 * @param phTestB Test handle B of test to verify PCM data for.
2348 */
2349static int audioTestVerifyTestToneData(PAUDIOTESTVERIFYJOB pVerJob, PAUDIOTESTOBJINT phTestA, PAUDIOTESTOBJINT phTestB)
2350{
2351 int rc;
2352
2353 /** @todo For now ASSUME that we only have one object per test. */
2354
2355 AUDIOTESTOBJINT ObjA;
2356 rc = audioTestObjGetChild(phTestA, 0 /* idxObj */, &ObjA);
2357 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to get object A");
2358
2359 rc = audioTestObjOpen(&ObjA);
2360 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to open object A");
2361
2362 AUDIOTESTOBJINT ObjB;
2363 rc = audioTestObjGetChild(phTestB, 0 /* idxObj */, &ObjB);
2364 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to get object B");
2365
2366 rc = audioTestObjOpen(&ObjB);
2367 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Unable to open object B");
2368
2369 /*
2370 * Start with most obvious methods first.
2371 */
2372 uint64_t cbSizeA, cbSizeB;
2373 rc = RTFileQuerySize(ObjA.File.hFile, &cbSizeA);
2374 AssertRCReturn(rc, rc);
2375 rc = RTFileQuerySize(ObjB.File.hFile, &cbSizeB);
2376 AssertRCReturn(rc, rc);
2377
2378 if (!cbSizeA)
2379 {
2380 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "File '%s' is empty", ObjA.szName);
2381 AssertRC(rc2);
2382 }
2383
2384 if (!cbSizeB)
2385 {
2386 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "File '%s' is empty", ObjB.szName);
2387 AssertRC(rc2);
2388 }
2389
2390 if (cbSizeA != cbSizeB)
2391 {
2392 size_t const cbDiffAbs = cbSizeA > cbSizeB ? cbSizeA - cbSizeB : cbSizeB - cbSizeA;
2393
2394 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File '%s' is %zu bytes (%RU64ms)",
2395 ObjA.szName, cbSizeA, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, cbSizeA));
2396 AssertRC(rc2);
2397 rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File '%s' is %zu bytes (%RU64ms)",
2398 ObjB.szName, cbSizeB, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, cbSizeB));
2399 AssertRC(rc2);
2400
2401 uint8_t const uSizeDiffPercentAbs
2402 = cbSizeA > cbSizeB ? 100 - ((cbSizeB * 100) / cbSizeA) : 100 - ((cbSizeA * 100) / cbSizeB);
2403
2404 if (uSizeDiffPercentAbs > pVerJob->Opts.uMaxSizePercent)
2405 {
2406 rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
2407 "File '%s' is %RU8%% (%zu bytes, %RU64ms) %s than '%s' (threshold is %RU8%%)",
2408 ObjA.szName,
2409 uSizeDiffPercentAbs,
2410 cbDiffAbs, PDMAudioPropsBytesToMilli(&pVerJob->PCMProps, (uint32_t)cbDiffAbs),
2411 cbSizeA > cbSizeB ? "bigger" : "smaller",
2412 ObjB.szName, pVerJob->Opts.uMaxSizePercent);
2413 AssertRC(rc2);
2414 }
2415 }
2416
2417 /* Do normalization first if enabled. */
2418 if (pVerJob->Opts.fNormalize)
2419 {
2420 rc = audioTestObjFileNormalize(pVerJob, &ObjA, &pVerJob->PCMProps);
2421 if (RT_SUCCESS(rc))
2422 rc = audioTestObjFileNormalize(pVerJob, &ObjB, &pVerJob->PCMProps);
2423 }
2424
2425 /** @todo For now we only support comparison of data which do have identical PCM properties! */
2426
2427 AUDIOTESTTONEPARMS ToneParmsA;
2428 RT_ZERO(ToneParmsA);
2429 ToneParmsA.Props = pVerJob->PCMProps;
2430
2431 size_t cbSearchWindow = PDMAudioPropsMilliToBytes(&ToneParmsA.Props, pVerJob->Opts.msSearchWindow);
2432
2433 AUDIOTESTFILECMPPARMS FileA;
2434 RT_ZERO(FileA);
2435 FileA.hFile = ObjA.File.hFile;
2436 FileA.offStart = audioTestToneFileFind(ObjA.File.hFile,
2437 true /* fFindSilence */, 0 /* uOff */, &ToneParmsA, cbSearchWindow);
2438 FileA.cbSize = RT_MIN(audioTestToneFileFind(ObjA.File.hFile,
2439 false /* fFindSilence */, FileA.offStart, &ToneParmsA, cbSearchWindow),
2440 cbSizeA);
2441
2442 AUDIOTESTTONEPARMS ToneParmsB;
2443 RT_ZERO(ToneParmsB);
2444 ToneParmsB.Props = pVerJob->PCMProps;
2445
2446 AUDIOTESTFILECMPPARMS FileB;
2447 RT_ZERO(FileB);
2448 FileB.hFile = ObjB.File.hFile;
2449 FileB.offStart = audioTestToneFileFind(ObjB.File.hFile,
2450 true /* fFindSilence */, 0 /* uOff */, &ToneParmsB, cbSearchWindow);
2451 FileB.cbSize = RT_MIN(audioTestToneFileFind(ObjB.File.hFile,
2452 false /* fFindSilence */, FileB.offStart, &ToneParmsB, cbSearchWindow),
2453 cbSizeB);
2454
2455 int rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File A ('%s'): uOff=%RU64 (%#x), cbSize=%RU64 (%#x), cbFileSize=%RU64\n",
2456 ObjA.szName, FileA.offStart, FileA.offStart, FileA.cbSize, FileA.cbSize, cbSizeA);
2457 AssertRC(rc2);
2458
2459 rc = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "File B ('%s'): uOff=%RU64 (%#x), cbSize=%RU64 (%#x), cbFileSize=%RU64\n",
2460 ObjB.szName, FileB.offStart, FileB.offStart, FileB.cbSize, FileB.cbSize, cbSizeB);
2461 AssertRC(rc2);
2462
2463 uint32_t const cDiffs = audioTestFilesFindDiffsBinary(pVerJob, &FileA, &FileB, &ToneParmsA);
2464
2465 if (cDiffs > pVerJob->Opts.cMaxDiff)
2466 {
2467 rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest,
2468 "Files '%s' and '%s' have too many different chunks (got %RU32, expected %RU32)",
2469 ObjA.szName, ObjB.szName, cDiffs, pVerJob->Opts.cMaxDiff);
2470 AssertRC(rc2);
2471 }
2472
2473 if (AudioTestErrorDescFailed(pVerJob->pErr))
2474 {
2475 rc2 = audioTestErrorDescAddInfo(pVerJob->pErr, pVerJob->idxTest, "Files '%s' and '%s' do not match",
2476 ObjA.szName, ObjB.szName);
2477 AssertRC(rc2);
2478 }
2479
2480 rc = audioTestObjClose(&ObjA);
2481 AssertRCReturn(rc, rc);
2482 rc = audioTestObjClose(&ObjB);
2483 AssertRCReturn(rc, rc);
2484
2485 return rc;
2486}
2487
2488/**
2489 * Verifies a test tone test.
2490 *
2491 * @returns VBox status code.
2492 * @returns Error if the verification failed and test verification job has fKeepGoing not set.
2493 * @retval VERR_
2494 * @param pVerJob Verification job to verify test tone for.
2495 * @param phTestA Test handle of test tone A to verify tone B with.
2496 * @param phTestB Test handle of test tone B to verify tone A with.*
2497 */
2498static int audioTestVerifyTestTone(PAUDIOTESTVERIFYJOB pVerJob, PAUDIOTESTOBJINT phTestA, PAUDIOTESTOBJINT phTestB)
2499{
2500 int rc;
2501
2502 /*
2503 * Verify test parameters.
2504 * More important items have precedence.
2505 */
2506 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "error_rc", "0", "Test was reported as failed");
2507 CHECK_RC_MAYBE_RET(rc, pVerJob);
2508 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "obj_count", NULL, "Object counts don't match");
2509 CHECK_RC_MAYBE_RET(rc, pVerJob);
2510 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_freq_hz", NULL, "Tone frequency doesn't match");
2511 CHECK_RC_MAYBE_RET(rc, pVerJob);
2512 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_prequel_ms", NULL, "Tone prequel (ms) doesn't match");
2513 CHECK_RC_MAYBE_RET(rc, pVerJob);
2514 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_duration_ms", NULL, "Tone duration (ms) doesn't match");
2515 CHECK_RC_MAYBE_RET(rc, pVerJob);
2516 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_sequel_ms", NULL, "Tone sequel (ms) doesn't match");
2517 CHECK_RC_MAYBE_RET(rc, pVerJob);
2518 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_volume_percent", NULL, "Tone volume (percent) doesn't match");
2519 CHECK_RC_MAYBE_RET(rc, pVerJob);
2520 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_hz", NULL, "Tone PCM Hz doesn't match");
2521 CHECK_RC_MAYBE_RET(rc, pVerJob);
2522 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_channels", NULL, "Tone PCM channels don't match");
2523 CHECK_RC_MAYBE_RET(rc, pVerJob);
2524 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_bits", NULL, "Tone PCM bits don't match");
2525 CHECK_RC_MAYBE_RET(rc, pVerJob);
2526 rc = audioTestVerifyValue(pVerJob, phTestA, phTestB, "tone_pcm_is_signed", NULL, "Tone PCM signed bit doesn't match");
2527 CHECK_RC_MAYBE_RET(rc, pVerJob);
2528
2529 rc = audioTestObjGetTonePcmProps(phTestA, &pVerJob->PCMProps);
2530 CHECK_RC_MAYBE_RET(rc, pVerJob);
2531
2532 /*
2533 * Now the fun stuff, PCM data analysis.
2534 */
2535 rc = audioTestVerifyTestToneData(pVerJob, phTestA, phTestB);
2536 if (RT_FAILURE(rc))
2537 {
2538 int rc2 = audioTestErrorDescAddError(pVerJob->pErr, pVerJob->idxTest, "Verififcation of test tone data failed\n");
2539 AssertRC(rc2);
2540 }
2541
2542 return VINF_SUCCESS;
2543}
2544
2545/**
2546 * Verifies an opened audio test set, extended version.
2547 *
2548 * @returns VBox status code.
2549 * @param pSetA Test set A to verify.
2550 * @param pSetB Test set to verify test set A with.
2551 * @param pOpts Verification options to use.
2552 * @param pErrDesc Where to return the test verification errors.
2553 *
2554 * @note Test verification errors have to be checked for errors, regardless of the
2555 * actual return code.
2556 * @note Uses the standard verification options. Use AudioTestSetVerifyEx() to specify
2557 * own options.
2558 */
2559int AudioTestSetVerifyEx(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB, PAUDIOTESTVERIFYOPTS pOpts, PAUDIOTESTERRORDESC pErrDesc)
2560{
2561 AssertPtrReturn(pSetA, VERR_INVALID_POINTER);
2562 AssertPtrReturn(pSetB, VERR_INVALID_POINTER);
2563 AssertReturn(audioTestManifestIsOpen(pSetA), VERR_WRONG_ORDER);
2564 AssertReturn(audioTestManifestIsOpen(pSetB), VERR_WRONG_ORDER);
2565 AssertPtrReturn(pOpts, VERR_INVALID_POINTER);
2566
2567 /* We ASSUME the caller has not init'd pErrDesc. */
2568 audioTestErrorDescInit(pErrDesc);
2569
2570 AUDIOTESTVERIFYJOB VerJob;
2571 RT_ZERO(VerJob);
2572 VerJob.pErr = pErrDesc;
2573 VerJob.pSetA = pSetA;
2574 VerJob.pSetB = pSetB;
2575
2576 memcpy(&VerJob.Opts, pOpts, sizeof(AUDIOTESTVERIFYOPTS));
2577
2578 PAUDIOTESTVERIFYJOB pVerJob = &VerJob;
2579
2580 int rc;
2581
2582 /*
2583 * Compare obvious values first.
2584 */
2585 AUDIOTESTOBJINT hHdrA;
2586 rc = audioTestSetGetSection(pVerJob->pSetA, AUDIOTEST_SEC_HDR_STR, &hHdrA);
2587 CHECK_RC_MAYBE_RET(rc, pVerJob);
2588
2589 AUDIOTESTOBJINT hHdrB;
2590 rc = audioTestSetGetSection(pVerJob->pSetB, AUDIOTEST_SEC_HDR_STR, &hHdrB);
2591 CHECK_RC_MAYBE_RET(rc, pVerJob);
2592
2593 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "magic", "vkat_ini", "Manifest magic wrong");
2594 CHECK_RC_MAYBE_RET(rc, pVerJob);
2595 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "ver", "1" , "Manifest version wrong");
2596 CHECK_RC_MAYBE_RET(rc, pVerJob);
2597 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "tag", NULL, "Manifest tags don't match");
2598 CHECK_RC_MAYBE_RET(rc, pVerJob);
2599 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "test_count", NULL, "Test counts don't match");
2600 CHECK_RC_MAYBE_RET(rc, pVerJob);
2601 rc = audioTestVerifyValue(&VerJob, &hHdrA, &hHdrB, "obj_count", NULL, "Object counts don't match");
2602 CHECK_RC_MAYBE_RET(rc, pVerJob);
2603
2604 /*
2605 * Compare ran tests.
2606 */
2607 uint32_t cTests;
2608 rc = audioTestObjGetUInt32(&hHdrA, "test_count", &cTests);
2609 AssertRCReturn(rc, rc);
2610
2611 for (uint32_t i = 0; i < cTests; i++)
2612 {
2613 VerJob.idxTest = i;
2614
2615 AUDIOTESTOBJINT hTestA;
2616 rc = audioTestSetGetTest(VerJob.pSetA, i, &hTestA);
2617 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test A not found");
2618
2619 AUDIOTESTOBJINT hTestB;
2620 rc = audioTestSetGetTest(VerJob.pSetB, i, &hTestB);
2621 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test B not found");
2622
2623 AUDIOTESTTYPE enmTestTypeA = AUDIOTESTTYPE_INVALID;
2624 rc = audioTestObjGetUInt32(&hTestA, "test_type", (uint32_t *)&enmTestTypeA);
2625 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test type A not found");
2626
2627 AUDIOTESTTYPE enmTestTypeB = AUDIOTESTTYPE_INVALID;
2628 rc = audioTestObjGetUInt32(&hTestB, "test_type", (uint32_t *)&enmTestTypeB);
2629 CHECK_RC_MSG_MAYBE_RET(rc, pVerJob, "Test type B not found");
2630
2631 switch (enmTestTypeA)
2632 {
2633 case AUDIOTESTTYPE_TESTTONE_PLAY:
2634 {
2635 if (enmTestTypeB == AUDIOTESTTYPE_TESTTONE_RECORD)
2636 rc = audioTestVerifyTestTone(&VerJob, &hTestA, &hTestB);
2637 else
2638 rc = audioTestErrorDescAddError(pErrDesc, i, "Playback test types don't match (set A=%#x, set B=%#x)",
2639 enmTestTypeA, enmTestTypeB);
2640 break;
2641 }
2642
2643 case AUDIOTESTTYPE_TESTTONE_RECORD:
2644 {
2645 if (enmTestTypeB == AUDIOTESTTYPE_TESTTONE_PLAY)
2646 rc = audioTestVerifyTestTone(&VerJob, &hTestB, &hTestA);
2647 else
2648 rc = audioTestErrorDescAddError(pErrDesc, i, "Recording test types don't match (set A=%#x, set B=%#x)",
2649 enmTestTypeA, enmTestTypeB);
2650 break;
2651 }
2652
2653 case AUDIOTESTTYPE_INVALID:
2654 rc = VERR_INVALID_PARAMETER;
2655 break;
2656
2657 default:
2658 rc = VERR_NOT_IMPLEMENTED;
2659 break;
2660 }
2661
2662 AssertRC(rc);
2663 }
2664
2665 /* Only return critical stuff not related to actual testing here. */
2666 return VINF_SUCCESS;
2667}
2668
2669/**
2670 * Initializes audio test verification options in a strict manner.
2671 *
2672 * @param pOpts Verification options to initialize.
2673 */
2674void AudioTestSetVerifyOptsInitStrict(PAUDIOTESTVERIFYOPTS pOpts)
2675{
2676 RT_BZERO(pOpts, sizeof(AUDIOTESTVERIFYOPTS));
2677
2678 pOpts->fKeepGoing = true;
2679 pOpts->fNormalize = true;
2680 pOpts->cMaxDiff = 0; /* By default we're very strict and consider any diff as being erroneous. */
2681 pOpts->uMaxSizePercent = 1; /* 1% is okay for us; might be due to any buffering / setup phase. */
2682
2683 /* We use a search window of 10ms by default for finding (non-)silent parts. */
2684 pOpts->msSearchWindow = 10;
2685
2686}
2687
2688/**
2689 * Initializes audio test verification options with default values (strict!).
2690 *
2691 * @param pOpts Verification options to initialize.
2692 */
2693void AudioTestSetVerifyOptsInit(PAUDIOTESTVERIFYOPTS pOpts)
2694{
2695 AudioTestSetVerifyOptsInitStrict(pOpts);
2696}
2697
2698/**
2699 * Returns whether two audio test verification options are equal.
2700 *
2701 * @returns \c true if equal, or \c false if not.
2702 * @param pOptsA Options A to compare.
2703 * @param pOptsB Options B to compare Options A with.
2704 */
2705bool AudioTestSetVerifyOptsAreEqual(PAUDIOTESTVERIFYOPTS pOptsA, PAUDIOTESTVERIFYOPTS pOptsB)
2706{
2707 if (pOptsA == pOptsB)
2708 return true;
2709
2710 return ( pOptsA->cMaxDiff == pOptsB->cMaxDiff
2711 && pOptsA->fKeepGoing == pOptsB->fKeepGoing
2712 && pOptsA->fNormalize == pOptsB->fNormalize
2713 && pOptsA->uMaxDiffPercent == pOptsB->uMaxDiffPercent
2714 && pOptsA->uMaxSizePercent == pOptsB->uMaxSizePercent
2715 && pOptsA->msSearchWindow == pOptsB->msSearchWindow);
2716}
2717
2718/**
2719 * Verifies an opened audio test set.
2720 *
2721 * @returns VBox status code.
2722 * @param pSetA Test set A to verify.
2723 * @param pSetB Test set to verify test set A with.
2724 * @param pErrDesc Where to return the test verification errors.
2725 *
2726 * @note Test verification errors have to be checked for errors, regardless of the
2727 * actual return code.
2728 * @note Uses the standard verification options (strict!).
2729 * Use AudioTestSetVerifyEx() to specify own options.
2730 */
2731int AudioTestSetVerify(PAUDIOTESTSET pSetA, PAUDIOTESTSET pSetB, PAUDIOTESTERRORDESC pErrDesc)
2732{
2733 AUDIOTESTVERIFYOPTS Opts;
2734 AudioTestSetVerifyOptsInitStrict(&Opts);
2735
2736 return AudioTestSetVerifyEx(pSetA,pSetB, &Opts, pErrDesc);
2737}
2738
2739#undef CHECK_RC_MAYBE_RET
2740#undef CHECK_RC_MSG_MAYBE_RET
2741
2742
2743/*********************************************************************************************************************************
2744* WAVE File Reader. *
2745*********************************************************************************************************************************/
2746
2747/**
2748 * Counts the number of set bits in @a fMask.
2749 */
2750static unsigned audioTestWaveCountBits(uint32_t fMask)
2751{
2752 unsigned cBits = 0;
2753 while (fMask)
2754 {
2755 if (fMask & 1)
2756 cBits++;
2757 fMask >>= 1;
2758 }
2759 return cBits;
2760}
2761
2762/**
2763 * Opens a wave (.WAV) file for reading.
2764 *
2765 * @returns VBox status code.
2766 * @param pszFile The file to open.
2767 * @param pWaveFile The open wave file structure to fill in on success.
2768 * @param pErrInfo Where to return addition error details on failure.
2769 */
2770int AudioTestWaveFileOpen(const char *pszFile, PAUDIOTESTWAVEFILE pWaveFile, PRTERRINFO pErrInfo)
2771{
2772 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
2773 RT_ZERO(pWaveFile->Props);
2774 pWaveFile->hFile = NIL_RTFILE;
2775 int rc = RTFileOpen(&pWaveFile->hFile, pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
2776 if (RT_FAILURE(rc))
2777 return RTErrInfoSet(pErrInfo, rc, "RTFileOpen failed");
2778 uint64_t cbFile = 0;
2779 rc = RTFileQuerySize(pWaveFile->hFile, &cbFile);
2780 if (RT_SUCCESS(rc))
2781 {
2782 union
2783 {
2784 uint8_t ab[512];
2785 struct
2786 {
2787 RTRIFFHDR Hdr;
2788 union
2789 {
2790 RTRIFFWAVEFMTCHUNK Fmt;
2791 RTRIFFWAVEFMTEXTCHUNK FmtExt;
2792 } u;
2793 } Wave;
2794 RTRIFFLIST List;
2795 RTRIFFCHUNK Chunk;
2796 RTRIFFWAVEDATACHUNK Data;
2797 } uBuf;
2798
2799 rc = RTFileRead(pWaveFile->hFile, &uBuf.Wave, sizeof(uBuf.Wave), NULL);
2800 if (RT_SUCCESS(rc))
2801 {
2802 rc = VERR_VFS_UNKNOWN_FORMAT;
2803 if ( uBuf.Wave.Hdr.uMagic == RTRIFFHDR_MAGIC
2804 && uBuf.Wave.Hdr.uFileType == RTRIFF_FILE_TYPE_WAVE
2805 && uBuf.Wave.u.Fmt.Chunk.uMagic == RTRIFFWAVEFMT_MAGIC
2806 && uBuf.Wave.u.Fmt.Chunk.cbChunk >= sizeof(uBuf.Wave.u.Fmt.Data))
2807 {
2808 if (uBuf.Wave.Hdr.cbFile != cbFile - sizeof(RTRIFFCHUNK))
2809 RTErrInfoSetF(pErrInfo, rc, "File size mismatch: %#x, actual %#RX64 (ignored)",
2810 uBuf.Wave.Hdr.cbFile, cbFile - sizeof(RTRIFFCHUNK));
2811 rc = VERR_VFS_BOGUS_FORMAT;
2812 if ( uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_PCM
2813 && uBuf.Wave.u.Fmt.Data.uFormatTag != RTRIFFWAVEFMT_TAG_EXTENSIBLE)
2814 RTErrInfoSetF(pErrInfo, rc, "Unsupported uFormatTag value: %#x (expected %#x or %#x)",
2815 uBuf.Wave.u.Fmt.Data.uFormatTag, RTRIFFWAVEFMT_TAG_PCM, RTRIFFWAVEFMT_TAG_EXTENSIBLE);
2816 else if ( uBuf.Wave.u.Fmt.Data.cBitsPerSample != 8
2817 && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 16
2818 /* && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 24 - not supported by our stack */
2819 && uBuf.Wave.u.Fmt.Data.cBitsPerSample != 32)
2820 RTErrInfoSetF(pErrInfo, rc, "Unsupported cBitsPerSample value: %u", uBuf.Wave.u.Fmt.Data.cBitsPerSample);
2821 else if ( uBuf.Wave.u.Fmt.Data.cChannels < 1
2822 || uBuf.Wave.u.Fmt.Data.cChannels >= 16)
2823 RTErrInfoSetF(pErrInfo, rc, "Unsupported cChannels value: %u (expected 1..15)", uBuf.Wave.u.Fmt.Data.cChannels);
2824 else if ( uBuf.Wave.u.Fmt.Data.uHz < 4096
2825 || uBuf.Wave.u.Fmt.Data.uHz > 768000)
2826 RTErrInfoSetF(pErrInfo, rc, "Unsupported uHz value: %u (expected 4096..768000)", uBuf.Wave.u.Fmt.Data.uHz);
2827 else if (uBuf.Wave.u.Fmt.Data.cbFrame != uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8)
2828 RTErrInfoSetF(pErrInfo, rc, "Invalid cbFrame value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbFrame,
2829 uBuf.Wave.u.Fmt.Data.cChannels * uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8);
2830 else if (uBuf.Wave.u.Fmt.Data.cbRate != uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz)
2831 RTErrInfoSetF(pErrInfo, rc, "Invalid cbRate value: %u (expected %u)", uBuf.Wave.u.Fmt.Data.cbRate,
2832 uBuf.Wave.u.Fmt.Data.cbFrame * uBuf.Wave.u.Fmt.Data.uHz);
2833 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2834 && uBuf.Wave.u.FmtExt.Data.cbExtra < RTRIFFWAVEFMTEXT_EXTRA_SIZE)
2835 RTErrInfoSetF(pErrInfo, rc, "Invalid cbExtra value: %#x (expected at least %#x)",
2836 uBuf.Wave.u.FmtExt.Data.cbExtra, RTRIFFWAVEFMTEXT_EXTRA_SIZE);
2837 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2838 && audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask) != uBuf.Wave.u.Fmt.Data.cChannels)
2839 RTErrInfoSetF(pErrInfo, rc, "fChannelMask does not match cChannels: %#x (%u bits set) vs %u channels",
2840 uBuf.Wave.u.FmtExt.Data.fChannelMask,
2841 audioTestWaveCountBits(uBuf.Wave.u.FmtExt.Data.fChannelMask), uBuf.Wave.u.Fmt.Data.cChannels);
2842 else if ( uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE
2843 && RTUuidCompareStr(&uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM) != 0)
2844 RTErrInfoSetF(pErrInfo, rc, "SubFormat is not PCM: %RTuuid (expected %s)",
2845 &uBuf.Wave.u.FmtExt.Data.SubFormat, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
2846 else
2847 {
2848 /*
2849 * Copy out the data we need from the file format structure.
2850 */
2851 PDMAudioPropsInit(&pWaveFile->Props, uBuf.Wave.u.Fmt.Data.cBitsPerSample / 8, true /*fSigned*/,
2852 uBuf.Wave.u.Fmt.Data.cChannels, uBuf.Wave.u.Fmt.Data.uHz);
2853 pWaveFile->offSamples = sizeof(RTRIFFHDR) + sizeof(RTRIFFCHUNK) + uBuf.Wave.u.Fmt.Chunk.cbChunk;
2854
2855 /*
2856 * Pick up channel assignments if present.
2857 */
2858 if (uBuf.Wave.u.Fmt.Data.uFormatTag == RTRIFFWAVEFMT_TAG_EXTENSIBLE)
2859 {
2860 static unsigned const s_cStdIds = (unsigned)PDMAUDIOCHANNELID_END_STANDARD
2861 - (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD;
2862 unsigned iCh = 0;
2863 for (unsigned idCh = 0; idCh < 32 && iCh < uBuf.Wave.u.Fmt.Data.cChannels; idCh++)
2864 if (uBuf.Wave.u.FmtExt.Data.fChannelMask & RT_BIT_32(idCh))
2865 {
2866 pWaveFile->Props.aidChannels[iCh] = idCh < s_cStdIds
2867 ? idCh + (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD
2868 : (unsigned)PDMAUDIOCHANNELID_UNKNOWN;
2869 iCh++;
2870 }
2871 }
2872
2873 /*
2874 * Find the 'data' chunk with the audio samples.
2875 *
2876 * There can be INFO lists both preceeding this and succeeding
2877 * it, containing IART and other things we can ignored. Thus
2878 * we read a list header here rather than just a chunk header,
2879 * since it doesn't matter if we read 4 bytes extra as
2880 * AudioTestWaveFileRead uses RTFileReadAt anyway.
2881 */
2882 rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
2883 for (uint32_t i = 0;
2884 i < 128
2885 && RT_SUCCESS(rc)
2886 && uBuf.Chunk.uMagic != RTRIFFWAVEDATACHUNK_MAGIC
2887 && (uint64_t)uBuf.Chunk.cbChunk + sizeof(RTRIFFCHUNK) * 2 <= cbFile - pWaveFile->offSamples;
2888 i++)
2889 {
2890 if ( uBuf.List.uMagic == RTRIFFLIST_MAGIC
2891 && uBuf.List.uListType == RTRIFFLIST_TYPE_INFO)
2892 { /*skip*/ }
2893 else if (uBuf.Chunk.uMagic == RTRIFFPADCHUNK_MAGIC)
2894 { /*skip*/ }
2895 else
2896 break;
2897 pWaveFile->offSamples += sizeof(RTRIFFCHUNK) + uBuf.Chunk.cbChunk;
2898 rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples, &uBuf, sizeof(uBuf.List), NULL);
2899 }
2900 if (RT_SUCCESS(rc))
2901 {
2902 pWaveFile->offSamples += sizeof(uBuf.Data.Chunk);
2903 pWaveFile->cbSamples = (uint32_t)cbFile - pWaveFile->offSamples;
2904
2905 rc = VERR_VFS_BOGUS_FORMAT;
2906 if ( uBuf.Data.Chunk.uMagic == RTRIFFWAVEDATACHUNK_MAGIC
2907 && uBuf.Data.Chunk.cbChunk <= pWaveFile->cbSamples
2908 && PDMAudioPropsIsSizeAligned(&pWaveFile->Props, uBuf.Data.Chunk.cbChunk))
2909 {
2910 pWaveFile->cbSamples = uBuf.Data.Chunk.cbChunk;
2911
2912 /*
2913 * We're good!
2914 */
2915 pWaveFile->offCur = 0;
2916 pWaveFile->fReadMode = true;
2917 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC;
2918 return VINF_SUCCESS;
2919 }
2920
2921 RTErrInfoSetF(pErrInfo, rc, "Bad data header: uMagic=%#x (expected %#x), cbChunk=%#x (max %#RX64, align %u)",
2922 uBuf.Data.Chunk.uMagic, RTRIFFWAVEDATACHUNK_MAGIC,
2923 uBuf.Data.Chunk.cbChunk, pWaveFile->cbSamples, PDMAudioPropsFrameSize(&pWaveFile->Props));
2924 }
2925 else
2926 RTErrInfoSet(pErrInfo, rc, "Failed to read data header");
2927 }
2928 }
2929 else
2930 RTErrInfoSetF(pErrInfo, rc, "Bad file header: uMagic=%#x (vs. %#x), uFileType=%#x (vs %#x), uFmtMagic=%#x (vs %#x) cbFmtChunk=%#x (min %#x)",
2931 uBuf.Wave.Hdr.uMagic, RTRIFFHDR_MAGIC, uBuf.Wave.Hdr.uFileType, RTRIFF_FILE_TYPE_WAVE,
2932 uBuf.Wave.u.Fmt.Chunk.uMagic, RTRIFFWAVEFMT_MAGIC,
2933 uBuf.Wave.u.Fmt.Chunk.cbChunk, sizeof(uBuf.Wave.u.Fmt.Data));
2934 }
2935 else
2936 rc = RTErrInfoSet(pErrInfo, rc, "Failed to read file header");
2937 }
2938 else
2939 rc = RTErrInfoSet(pErrInfo, rc, "Failed to query file size");
2940
2941 RTFileClose(pWaveFile->hFile);
2942 pWaveFile->hFile = NIL_RTFILE;
2943 return rc;
2944}
2945
2946
2947/**
2948 * Creates a new wave file.
2949 *
2950 * @returns VBox status code.
2951 * @param pszFile The filename.
2952 * @param pProps The audio format properties.
2953 * @param pWaveFile The wave file structure to fill in on success.
2954 * @param pErrInfo Where to return addition error details on failure.
2955 */
2956int AudioTestWaveFileCreate(const char *pszFile, PCPDMAUDIOPCMPROPS pProps, PAUDIOTESTWAVEFILE pWaveFile, PRTERRINFO pErrInfo)
2957{
2958 /*
2959 * Construct the file header first (we'll do some input validation
2960 * here, so better do it before creating the file).
2961 */
2962 struct
2963 {
2964 RTRIFFHDR Hdr;
2965 RTRIFFWAVEFMTEXTCHUNK FmtExt;
2966 RTRIFFCHUNK Data;
2967 } FileHdr;
2968
2969 FileHdr.Hdr.uMagic = RTRIFFHDR_MAGIC;
2970 FileHdr.Hdr.cbFile = 0; /* need to update this later */
2971 FileHdr.Hdr.uFileType = RTRIFF_FILE_TYPE_WAVE;
2972 FileHdr.FmtExt.Chunk.uMagic = RTRIFFWAVEFMT_MAGIC;
2973 FileHdr.FmtExt.Chunk.cbChunk = sizeof(RTRIFFWAVEFMTEXTCHUNK) - sizeof(RTRIFFCHUNK);
2974 FileHdr.FmtExt.Data.Core.uFormatTag = RTRIFFWAVEFMT_TAG_EXTENSIBLE;
2975 FileHdr.FmtExt.Data.Core.cChannels = PDMAudioPropsChannels(pProps);
2976 FileHdr.FmtExt.Data.Core.uHz = PDMAudioPropsHz(pProps);
2977 FileHdr.FmtExt.Data.Core.cbRate = PDMAudioPropsFramesToBytes(pProps, PDMAudioPropsHz(pProps));
2978 FileHdr.FmtExt.Data.Core.cbFrame = PDMAudioPropsFrameSize(pProps);
2979 FileHdr.FmtExt.Data.Core.cBitsPerSample = PDMAudioPropsSampleBits(pProps);
2980 FileHdr.FmtExt.Data.cbExtra = sizeof(FileHdr.FmtExt.Data) - sizeof(FileHdr.FmtExt.Data.Core);
2981 FileHdr.FmtExt.Data.cValidBitsPerSample = PDMAudioPropsSampleBits(pProps);
2982 FileHdr.FmtExt.Data.fChannelMask = 0;
2983 for (uintptr_t idxCh = 0; idxCh < FileHdr.FmtExt.Data.Core.cChannels; idxCh++)
2984 {
2985 PDMAUDIOCHANNELID const idCh = (PDMAUDIOCHANNELID)pProps->aidChannels[idxCh];
2986 if ( idCh >= PDMAUDIOCHANNELID_FIRST_STANDARD
2987 && idCh < PDMAUDIOCHANNELID_END_STANDARD)
2988 {
2989 if (!(FileHdr.FmtExt.Data.fChannelMask & RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD)))
2990 FileHdr.FmtExt.Data.fChannelMask |= RT_BIT_32(idCh - PDMAUDIOCHANNELID_FIRST_STANDARD);
2991 else
2992 return RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Channel #%u repeats channel ID %d", idxCh, idCh);
2993 }
2994 else
2995 return RTErrInfoSetF(pErrInfo, VERR_INVALID_PARAMETER, "Invalid channel ID %d for channel #%u", idCh, idxCh);
2996 }
2997
2998 RTUUID UuidTmp;
2999 int rc = RTUuidFromStr(&UuidTmp, RTRIFFWAVEFMTEXT_SUBTYPE_PCM);
3000 AssertRCReturn(rc, rc);
3001 FileHdr.FmtExt.Data.SubFormat = UuidTmp; /* (64-bit field maybe unaligned) */
3002
3003 FileHdr.Data.uMagic = RTRIFFWAVEDATACHUNK_MAGIC;
3004 FileHdr.Data.cbChunk = 0; /* need to update this later */
3005
3006 /*
3007 * Create the file and write the header.
3008 */
3009 pWaveFile->hFile = NIL_RTFILE;
3010 rc = RTFileOpen(&pWaveFile->hFile, pszFile, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
3011 if (RT_FAILURE(rc))
3012 return RTErrInfoSet(pErrInfo, rc, "RTFileOpen failed");
3013
3014 rc = RTFileWrite(pWaveFile->hFile, &FileHdr, sizeof(FileHdr), NULL);
3015 if (RT_SUCCESS(rc))
3016 {
3017 /*
3018 * Initialize the wave file structure.
3019 */
3020 pWaveFile->fReadMode = false;
3021 pWaveFile->offCur = 0;
3022 pWaveFile->offSamples = 0;
3023 pWaveFile->cbSamples = 0;
3024 pWaveFile->Props = *pProps;
3025 pWaveFile->offSamples = RTFileTell(pWaveFile->hFile);
3026 if (pWaveFile->offSamples != UINT32_MAX)
3027 {
3028 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC;
3029 return VINF_SUCCESS;
3030 }
3031 rc = RTErrInfoSet(pErrInfo, VERR_SEEK, "RTFileTell failed");
3032 }
3033 else
3034 RTErrInfoSet(pErrInfo, rc, "RTFileWrite failed writing header");
3035
3036 RTFileClose(pWaveFile->hFile);
3037 pWaveFile->hFile = NIL_RTFILE;
3038 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
3039
3040 RTFileDelete(pszFile);
3041 return rc;
3042}
3043
3044
3045/**
3046 * Closes a wave file.
3047 */
3048int AudioTestWaveFileClose(PAUDIOTESTWAVEFILE pWaveFile)
3049{
3050 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3051 int rcRet = VINF_SUCCESS;
3052 int rc;
3053
3054 /*
3055 * Update the size fields if writing.
3056 */
3057 if (!pWaveFile->fReadMode)
3058 {
3059 uint64_t cbFile = RTFileTell(pWaveFile->hFile);
3060 if (cbFile != UINT64_MAX)
3061 {
3062 uint32_t cbFile32 = cbFile - sizeof(RTRIFFCHUNK);
3063 rc = RTFileWriteAt(pWaveFile->hFile, RT_OFFSETOF(RTRIFFHDR, cbFile), &cbFile32, sizeof(cbFile32), NULL);
3064 AssertRCStmt(rc, rcRet = rc);
3065
3066 uint32_t cbSamples = cbFile - pWaveFile->offSamples;
3067 rc = RTFileWriteAt(pWaveFile->hFile, pWaveFile->offSamples - sizeof(uint32_t), &cbSamples, sizeof(cbSamples), NULL);
3068 AssertRCStmt(rc, rcRet = rc);
3069 }
3070 else
3071 rcRet = VERR_SEEK;
3072 }
3073
3074 /*
3075 * Close it.
3076 */
3077 rc = RTFileClose(pWaveFile->hFile);
3078 AssertRCStmt(rc, rcRet = rc);
3079
3080 pWaveFile->hFile = NIL_RTFILE;
3081 pWaveFile->u32Magic = AUDIOTESTWAVEFILE_MAGIC_DEAD;
3082 return rcRet;
3083}
3084
3085/**
3086 * Reads samples from a wave file.
3087 *
3088 * @returns VBox status code. See RTVfsFileRead for EOF status handling.
3089 * @param pWaveFile The file to read from.
3090 * @param pvBuf Where to put the samples.
3091 * @param cbBuf How much to read at most.
3092 * @param pcbRead Where to return the actual number of bytes read,
3093 * optional.
3094 */
3095int AudioTestWaveFileRead(PAUDIOTESTWAVEFILE pWaveFile, void *pvBuf, size_t cbBuf, size_t *pcbRead)
3096{
3097 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3098 AssertReturn(pWaveFile->fReadMode, VERR_ACCESS_DENIED);
3099
3100 bool fEofAdjusted;
3101 if (pWaveFile->offCur + cbBuf <= pWaveFile->cbSamples)
3102 fEofAdjusted = false;
3103 else if (pcbRead)
3104 {
3105 fEofAdjusted = true;
3106 cbBuf = pWaveFile->cbSamples - pWaveFile->offCur;
3107 }
3108 else
3109 return VERR_EOF;
3110
3111 int rc = RTFileReadAt(pWaveFile->hFile, pWaveFile->offSamples + pWaveFile->offCur, pvBuf, cbBuf, pcbRead);
3112 if (RT_SUCCESS(rc))
3113 {
3114 if (pcbRead)
3115 {
3116 pWaveFile->offCur += (uint32_t)*pcbRead;
3117 if (fEofAdjusted || cbBuf > *pcbRead)
3118 rc = VINF_EOF;
3119 else if (!cbBuf && pWaveFile->offCur == pWaveFile->cbSamples)
3120 rc = VINF_EOF;
3121 }
3122 else
3123 pWaveFile->offCur += (uint32_t)cbBuf;
3124 }
3125 return rc;
3126}
3127
3128
3129/**
3130 * Writes samples to a wave file.
3131 *
3132 * @returns VBox status code.
3133 * @param pWaveFile The file to write to.
3134 * @param pvBuf The samples to write.
3135 * @param cbBuf How many bytes of samples to write.
3136 */
3137int AudioTestWaveFileWrite(PAUDIOTESTWAVEFILE pWaveFile, const void *pvBuf, size_t cbBuf)
3138{
3139 AssertReturn(pWaveFile->u32Magic == AUDIOTESTWAVEFILE_MAGIC, VERR_INVALID_MAGIC);
3140 AssertReturn(!pWaveFile->fReadMode, VERR_ACCESS_DENIED);
3141
3142 pWaveFile->cbSamples += (uint32_t)cbBuf;
3143 return RTFileWrite(pWaveFile->hFile, pvBuf, cbBuf, NULL);
3144}
3145
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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