VirtualBox

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

Last change on this file since 89467 was 89465, checked in by vboxsync, 4 years ago

AudioTest: Added support for writing wave files. bugref:10008

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

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