2 * Original code: automated SDL audio test written by Edgar Simo "bobbens"
3 * New/updated tests: aschiffler at ferzkopp dot net
6 /* quiet windows compiler warnings */
7 #define _CRT_SECURE_NO_WARNINGS
15 /* ================= Test Case Implementation ================== */
20 _audioSetUp(void *arg)
22 /* Start SDL audio subsystem */
23 int ret = SDL_InitSubSystem( SDL_INIT_AUDIO );
24 SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_AUDIO)");
25 SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_AUDIO)");
27 SDLTest_LogError("%s", SDL_GetError());
32 _audioTearDown(void *arg)
34 /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
35 remove("sdlaudio.raw");
37 SDLTest_AssertPass("Cleanup of test files completed");
41 /* Global counter for callback invocation */
42 int _audio_testCallbackCounter;
44 /* Global accumulator for total callback length */
45 int _audio_testCallbackLength;
48 /* Test callback function */
49 void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
51 /* track that callback was called */
52 _audio_testCallbackCounter++;
53 _audio_testCallbackLength += len;
57 /* Test case functions */
60 * \brief Stop and restart audio subsystem
62 * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
63 * \sa https://wiki.libsdl.org/SDL_InitSubSystem
65 int audio_quitInitAudioSubSystem()
67 /* Stop SDL audio subsystem */
68 SDL_QuitSubSystem( SDL_INIT_AUDIO );
69 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
71 /* Restart audio again */
74 return TEST_COMPLETED;
78 * \brief Start and stop audio directly
80 * \sa https://wiki.libsdl.org/SDL_InitAudio
81 * \sa https://wiki.libsdl.org/SDL_QuitAudio
83 int audio_initQuitAudio()
87 const char* audioDriver;
89 /* Stop SDL audio subsystem */
90 SDL_QuitSubSystem( SDL_INIT_AUDIO );
91 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
93 /* Loop over all available audio drivers */
94 iMax = SDL_GetNumAudioDrivers();
95 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
96 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
97 for (i = 0; i < iMax; i++) {
98 audioDriver = SDL_GetAudioDriver(i);
99 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
100 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
101 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
104 result = SDL_AudioInit(audioDriver);
105 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
106 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
110 SDLTest_AssertPass("Call to SDL_AudioQuit()");
113 /* NULL driver specification */
117 result = SDL_AudioInit(audioDriver);
118 SDLTest_AssertPass("Call to SDL_AudioInit(NULL)");
119 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
123 SDLTest_AssertPass("Call to SDL_AudioQuit()");
125 /* Restart audio again */
128 return TEST_COMPLETED;
132 * \brief Start, open, close and stop audio
134 * \sa https://wiki.libsdl.org/SDL_InitAudio
135 * \sa https://wiki.libsdl.org/SDL_OpenAudio
136 * \sa https://wiki.libsdl.org/SDL_CloseAudio
137 * \sa https://wiki.libsdl.org/SDL_QuitAudio
139 int audio_initOpenCloseQuitAudio()
141 int result, expectedResult;
143 const char* audioDriver;
144 SDL_AudioSpec desired;
146 /* Stop SDL audio subsystem */
147 SDL_QuitSubSystem( SDL_INIT_AUDIO );
148 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
150 /* Loop over all available audio drivers */
151 iMax = SDL_GetNumAudioDrivers();
152 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
153 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
154 for (i = 0; i < iMax; i++) {
155 audioDriver = SDL_GetAudioDriver(i);
156 //dsp open failed, maybe dsp is not supported in tizen.
157 if(SDL_strcmp(audioDriver ,"dsp")==0) continue;
158 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
159 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
160 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
163 for (j = 0; j < 2; j++) {
166 result = SDL_AudioInit(audioDriver);
167 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
168 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
171 SDL_memset(&desired, 0, sizeof(desired));
174 /* Set standard desired spec */
175 desired.freq = 22050;
176 desired.format = AUDIO_S16SYS;
177 desired.channels = 2;
178 desired.samples = 4096;
179 desired.callback = _audio_testCallback;
180 desired.userdata = NULL;
183 /* Set custom desired spec */
184 desired.freq = 48000;
185 desired.format = AUDIO_F32SYS;
186 desired.channels = 2;
187 desired.samples = 2048;
188 desired.callback = _audio_testCallback;
189 desired.userdata = NULL;
193 /* Call Open (maybe multiple times) */
194 for (k=0; k <= j; k++) {
195 result = SDL_OpenAudio(&desired, NULL);
196 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k+1);
197 expectedResult = (k==0) ? 0 : -1;
198 SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
201 /* Call Close (maybe multiple times) */
202 for (k=0; k <= j; k++) {
204 SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k+1);
207 /* Call Quit (maybe multiple times) */
208 for (k=0; k <= j; k++) {
210 SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k+1);
216 /* Restart audio again */
219 return TEST_COMPLETED;
223 * \brief Pause and unpause audio
225 * \sa https://wiki.libsdl.org/SDL_PauseAudio
227 int audio_pauseUnpauseAudio()
230 int i, iMax, j, k, l;
234 const char* audioDriver;
235 SDL_AudioSpec desired;
237 /* Stop SDL audio subsystem */
238 SDL_QuitSubSystem( SDL_INIT_AUDIO );
239 SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
241 /* Loop over all available audio drivers */
242 iMax = SDL_GetNumAudioDrivers();
243 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
244 SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
245 for (i = 0; i < iMax; i++) {
246 audioDriver = SDL_GetAudioDriver(i);
247 //dsp open failed, maybe dsp is not supported in tizen.
248 if(SDL_strcmp(audioDriver ,"dsp")==0) continue;
249 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
250 SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
251 SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
254 for (j = 0; j < 2; j++) {
257 result = SDL_AudioInit(audioDriver);
258 SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
259 SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
262 SDL_memset(&desired, 0, sizeof(desired));
265 /* Set standard desired spec */
266 desired.freq = 22050;
267 desired.format = AUDIO_S16SYS;
268 desired.channels = 2;
269 desired.samples = 4096;
270 desired.callback = _audio_testCallback;
271 desired.userdata = NULL;
274 /* Set custom desired spec */
275 desired.freq = 48000;
276 desired.format = AUDIO_F32SYS;
277 desired.channels = 2;
278 desired.samples = 2048;
279 desired.callback = _audio_testCallback;
280 desired.userdata = NULL;
285 result = SDL_OpenAudio(&desired, NULL);
286 SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
287 SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
289 /* Start and stop audio multiple times */
290 for (l=0; l<3; l++) {
291 SDLTest_Log("Pause/Unpause iteration: %d", l+1);
293 /* Reset callback counters */
294 _audio_testCallbackCounter = 0;
295 _audio_testCallbackLength = 0;
297 /* Un-pause audio to start playing (maybe multiple times) */
299 for (k=0; k <= j; k++) {
300 SDL_PauseAudio(pause_on);
301 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
304 /* Wait for callback */
310 while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
311 SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
312 SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
314 /* Pause audio to stop playing (maybe multiple times) */
315 for (k=0; k <= j; k++) {
316 pause_on = (k==0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
317 SDL_PauseAudio(pause_on);
318 SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
321 /* Ensure callback is not called again */
322 originalCounter = _audio_testCallbackCounter;
323 SDL_Delay(totalDelay + 10);
324 SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
329 SDLTest_AssertPass("Call to SDL_CloseAudio()");
333 SDLTest_AssertPass("Call to SDL_AudioQuit()");
338 /* Restart audio again */
341 return TEST_COMPLETED;
345 * \brief Enumerate and name available audio devices (output and capture).
347 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
348 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
350 int audio_enumerateAndNameAudioDevices()
354 const char *name, *nameAgain;
356 /* Iterate over types: t=0 output device, t=1 input/capture device */
357 for (t=0; t<2; t++) {
359 /* Get number of devices. */
360 n = SDL_GetNumAudioDevices(t);
361 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(%i)", t);
362 SDLTest_Log("Number of %s devices < 0, reported as %i", (t) ? "capture" : "output", n);
363 SDLTest_AssertCheck(n >= 0, "Validate result is >= 0, got: %i", n);
365 /* Variation of non-zero type */
367 tt = t + SDLTest_RandomIntegerInRange(1,10);
368 nn = SDL_GetNumAudioDevices(tt);
369 SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", tt, n, nn);
370 nn = SDL_GetNumAudioDevices(-tt);
371 SDLTest_AssertCheck(n==nn, "Verify result from SDL_GetNumAudioDevices(%i), expected same number of audio devices %i, got %i", -tt, n, nn);
376 for (i=0; i<n; i++) {
377 name = SDL_GetAudioDeviceName(i, t);
378 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
379 SDLTest_AssertCheck(name != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, t);
381 SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
383 /* Also try non-zero type */
384 tt = t + SDLTest_RandomIntegerInRange(1,10);
385 nameAgain = SDL_GetAudioDeviceName(i, tt);
386 SDLTest_AssertCheck(nameAgain != NULL, "Verify result from SDL_GetAudioDeviceName(%i, %i) is not NULL", i, tt);
387 if (nameAgain != NULL) {
388 SDLTest_AssertCheck(nameAgain[0] != '\0', "Verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, tt, nameAgain);
389 SDLTest_AssertCheck(SDL_strcmp(name, nameAgain)==0,
390 "Verify SDL_GetAudioDeviceName(%i, %i) and SDL_GetAudioDeviceName(%i %i) return the same string",
399 return TEST_COMPLETED;
403 * \brief Negative tests around enumeration and naming of audio devices.
405 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
406 * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
408 int audio_enumerateAndNameAudioDevicesNegativeTests()
414 /* Get number of devices. */
415 no = SDL_GetNumAudioDevices(0);
416 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
417 nc = SDL_GetNumAudioDevices(1);
418 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(1)");
420 /* Invalid device index when getting name */
421 for (t=0; t<2; t++) {
422 /* Negative device index */
423 i = SDLTest_RandomIntegerInRange(-10,-1);
424 name = SDL_GetAudioDeviceName(i, t);
425 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
426 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result NULL, expected NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
428 /* Device index past range */
429 for (j=0; j<3; j++) {
430 i = (t) ? nc+j : no+j;
431 name = SDL_GetAudioDeviceName(i, t);
432 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
433 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
436 /* Capture index past capture range but within output range */
437 if ((no>0) && (no>nc) && (t==1)) {
439 name = SDL_GetAudioDeviceName(i, t);
440 SDLTest_AssertPass("Call to SDL_GetAudioDeviceName(%i, %i)", i, t);
441 SDLTest_AssertCheck(name == NULL, "Check SDL_GetAudioDeviceName(%i, %i) result, expected: NULL, got: %s", i, t, (name == NULL) ? "NULL" : name);
445 return TEST_COMPLETED;
450 * \brief Checks available audio driver names.
452 * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
453 * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
455 int audio_printAudioDrivers()
460 /* Get number of drivers */
461 n = SDL_GetNumAudioDrivers();
462 SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers()");
463 SDLTest_AssertCheck(n>=0, "Verify number of audio drivers >= 0, got: %i", n);
468 for (i=0; i<n; i++) {
469 name = SDL_GetAudioDriver(i);
470 SDLTest_AssertPass("Call to SDL_GetAudioDriver(%i)", i);
471 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
473 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
478 return TEST_COMPLETED;
483 * \brief Checks current audio driver name with initialized audio.
485 * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
487 int audio_printCurrentAudioDriver()
489 /* Check current audio driver */
490 const char *name = SDL_GetCurrentAudioDriver();
491 SDLTest_AssertPass("Call to SDL_GetCurrentAudioDriver()");
492 SDLTest_AssertCheck(name != NULL, "Verify returned name is not NULL");
494 SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
497 return TEST_COMPLETED;
500 /* Definition of all formats, channels, and frequencies used to test audio conversions */
501 const int _numAudioFormats = 18;
502 SDL_AudioFormat _audioFormats[] = { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16SYS, AUDIO_S16, AUDIO_U16LSB,
503 AUDIO_U16MSB, AUDIO_U16SYS, AUDIO_U16, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32SYS, AUDIO_S32,
504 AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32SYS, AUDIO_F32 };
505 char *_audioFormatsVerbose[] = { "AUDIO_S8", "AUDIO_U8", "AUDIO_S16LSB", "AUDIO_S16MSB", "AUDIO_S16SYS", "AUDIO_S16", "AUDIO_U16LSB",
506 "AUDIO_U16MSB", "AUDIO_U16SYS", "AUDIO_U16", "AUDIO_S32LSB", "AUDIO_S32MSB", "AUDIO_S32SYS", "AUDIO_S32",
507 "AUDIO_F32LSB", "AUDIO_F32MSB", "AUDIO_F32SYS", "AUDIO_F32" };
508 const int _numAudioChannels = 4;
509 Uint8 _audioChannels[] = { 1, 2, 4, 6 };
510 const int _numAudioFrequencies = 4;
511 int _audioFrequencies[] = { 11025, 22050, 44100, 48000 };
515 * \brief Builds various audio conversion structures
517 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
519 int audio_buildAudioCVT()
525 int i, ii, j, jj, k, kk;
527 /* No conversion needed */
528 spec1.format = AUDIO_S16LSB;
531 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
532 spec1.format, spec1.channels, spec1.freq);
533 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec1)");
534 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0, got: %i", result);
536 /* Typical conversion */
537 spec1.format = AUDIO_S8;
540 spec2.format = AUDIO_S16LSB;
543 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
544 spec2.format, spec2.channels, spec2.freq);
545 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
546 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
548 /* All source conversions with random conversion targets, allow 'null' conversions */
549 for (i = 0; i < _numAudioFormats; i++) {
550 for (j = 0; j < _numAudioChannels; j++) {
551 for (k = 0; k < _numAudioFrequencies; k++) {
552 spec1.format = _audioFormats[i];
553 spec1.channels = _audioChannels[j];
554 spec1.freq = _audioFrequencies[k];
555 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
556 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
557 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
558 spec2.format = _audioFormats[ii];
559 spec2.channels = _audioChannels[jj];
560 spec2.freq = _audioFrequencies[kk];
561 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
562 spec2.format, spec2.channels, spec2.freq);
563 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
564 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
565 SDLTest_AssertCheck(result == 0 || result == 1, "Verify result value; expected: 0 or 1, got: %i", result);
567 SDLTest_LogError("%s", SDL_GetError());
569 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
575 return TEST_COMPLETED;
579 * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
581 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
583 int audio_buildAudioCVTNegative()
585 const char *expectedError = "Parameter 'cvt' is invalid";
595 spec1.format = AUDIO_S8;
598 spec2.format = AUDIO_S16LSB;
603 SDLTest_AssertPass("Call to SDL_ClearError()");
605 /* NULL input for CVT buffer */
606 result = SDL_BuildAudioCVT((SDL_AudioCVT *)NULL, spec1.format, spec1.channels, spec1.freq,
607 spec2.format, spec2.channels, spec2.freq);
608 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(NULL,...)");
609 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
610 error = SDL_GetError();
611 SDLTest_AssertPass("Call to SDL_GetError()");
612 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
614 SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
615 "Validate error message, expected: '%s', got: '%s'", expectedError, error);
618 /* Invalid conversions */
619 for (i = 1; i < 64; i++) {
620 /* Valid format to start with */
621 spec1.format = AUDIO_S8;
624 spec2.format = AUDIO_S16LSB;
629 SDLTest_AssertPass("Call to SDL_ClearError()");
631 /* Set various invalid format inputs */
632 SDL_strlcpy(message, "Invalid: ", 256);
634 SDL_strlcat(message, " spec1.format", 256);
638 SDL_strlcat(message, " spec1.channels", 256);
642 SDL_strlcat(message, " spec1.freq", 256);
646 SDL_strlcat(message, " spec2.format", 256);
650 SDL_strlcat(message, " spec2.channels", 256);
654 SDL_strlcat(message, " spec2.freq", 256);
657 SDLTest_Log("%s", message);
658 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
659 spec2.format, spec2.channels, spec2.freq);
660 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(spec1 ==> spec2)");
661 SDLTest_AssertCheck(result == -1, "Verify result value; expected: -1, got: %i", result);
662 error = SDL_GetError();
663 SDLTest_AssertPass("Call to SDL_GetError()");
664 SDLTest_AssertCheck(error != NULL && error[0] != '\0', "Validate that error message was not NULL or empty");
668 SDLTest_AssertPass("Call to SDL_ClearError()");
670 return TEST_COMPLETED;
674 * \brief Checks current audio status.
676 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
678 int audio_getAudioStatus()
680 SDL_AudioStatus result;
682 /* Check current audio status */
683 result = SDL_GetAudioStatus();
684 SDLTest_AssertPass("Call to SDL_GetAudioStatus()");
685 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
686 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
687 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
689 return TEST_COMPLETED;
695 * \brief Opens, checks current audio status, and closes a device.
697 * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
699 int audio_openCloseAndGetAudioStatus()
701 SDL_AudioStatus result;
705 SDL_AudioDeviceID id;
706 SDL_AudioSpec desired, obtained;
708 /* Get number of devices. */
709 count = SDL_GetNumAudioDevices(0);
710 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
712 for (i = 0; i < count; i++) {
713 /* Get device name */
714 device = (char *)SDL_GetAudioDeviceName(i, 0);
716 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
717 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
718 if (device == NULL) return TEST_ABORTED;
720 /* Set standard desired spec */
722 desired.format=AUDIO_S16SYS;
724 desired.samples=4096;
725 desired.callback=_audio_testCallback;
726 desired.userdata=NULL;
729 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
730 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
731 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
734 /* Check device audio status */
735 result = SDL_GetAudioDeviceStatus(id);
736 SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
737 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
738 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
739 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
741 /* Close device again */
742 SDL_CloseAudioDevice(id);
743 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
747 SDLTest_Log("No devices to test with");
750 return TEST_COMPLETED;
754 * \brief Locks and unlocks open audio device.
756 * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
757 * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
759 int audio_lockUnlockOpenAudioDevice()
764 SDL_AudioDeviceID id;
765 SDL_AudioSpec desired, obtained;
767 /* Get number of devices. */
768 count = SDL_GetNumAudioDevices(0);
769 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
771 for (i = 0; i < count; i++) {
772 /* Get device name */
773 device = (char *)SDL_GetAudioDeviceName(i, 0);
775 /*add by majunqing for skip the device 1*/
776 const char* ignoreDevice = "VIRTUAL AUDIO W";
777 if(SDL_strcmp(device, ignoreDevice) == 0)
782 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
783 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
784 if (device == NULL) return TEST_ABORTED;
786 /* Set standard desired spec */
788 desired.format=AUDIO_S16SYS;
790 desired.samples=4096;
791 desired.callback=_audio_testCallback;
792 desired.userdata=NULL;
795 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
796 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
797 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
799 /* Lock to protect callback */
800 SDL_LockAudioDevice(id);
801 SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
803 /* Simulate callback processing */
805 SDLTest_Log("Simulate callback processing - delay");
808 SDL_UnlockAudioDevice(id);
809 SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);
811 /* Close device again */
812 SDL_CloseAudioDevice(id);
813 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
817 SDLTest_Log("No devices to test with");
820 return TEST_COMPLETED;
825 * \brief Convert audio using various conversion structures
827 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
828 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
830 int audio_convertAudio()
838 int i, ii, j, jj, k, kk, l, ll;
840 /* Iterate over bitmask that determines which parameters are modified in the conversion */
841 for (c = 1; c < 8; c++) {
842 SDL_strlcpy(message, "Changing:", 128);
844 SDL_strlcat(message, " Format", 128);
847 SDL_strlcat(message, " Channels", 128);
850 SDL_strlcat(message, " Frequencies", 128);
852 SDLTest_Log("%s", message);
853 /* All source conversions with random conversion targets */
854 for (i = 0; i < _numAudioFormats; i++) {
855 for (j = 0; j < _numAudioChannels; j++) {
856 for (k = 0; k < _numAudioFrequencies; k++) {
857 spec1.format = _audioFormats[i];
858 spec1.channels = _audioChannels[j];
859 spec1.freq = _audioFrequencies[k];
861 /* Ensure we have a different target format */
864 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
869 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
874 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
878 } while ((i == ii) && (j == jj) && (k == kk));
879 spec2.format = _audioFormats[ii];
880 spec2.channels = _audioChannels[jj];
881 spec2.freq = _audioFrequencies[kk];
883 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
884 spec2.format, spec2.channels, spec2.freq);
885 SDLTest_AssertPass("Call to SDL_BuildAudioCVT(format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i ==> format[%i]=%s(%i),channels[%i]=%i,freq[%i]=%i)",
886 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
887 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
889 SDLTest_LogError("%s", SDL_GetError());
891 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
892 if (cvt.len_mult < 1) return TEST_ABORTED;
894 /* Create some random data to convert */
896 ll = l * cvt.len_mult;
897 SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
899 cvt.buf = (Uint8 *)SDL_malloc(ll);
900 SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
901 if (cvt.buf == NULL) return TEST_ABORTED;
903 /* Convert the data */
904 result = SDL_ConvertAudio(&cvt);
905 SDLTest_AssertPass("Call to SDL_ConvertAudio()");
906 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
907 SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
908 SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
910 /* Free converted buffer */
919 return TEST_COMPLETED;
924 * \brief Opens, checks current connected status, and closes a device.
926 * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
928 int audio_openCloseAudioDeviceConnected()
934 SDL_AudioDeviceID id;
935 SDL_AudioSpec desired, obtained;
937 /* Get number of devices. */
938 count = SDL_GetNumAudioDevices(0);
939 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
941 for (i = 0; i < count; i++) {
942 /* Get device name */
943 device = (char *)SDL_GetAudioDeviceName(i, 0);
944 /*add by majunqing for skip the device 1*/
945 const char* ignoreDevice = "VIRTUAL AUDIO W";
946 if(SDL_strcmp(device, ignoreDevice) == 0)
951 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
952 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
953 if (device == NULL) return TEST_ABORTED;
955 /* Set standard desired spec */
957 desired.format=AUDIO_S16SYS;
959 desired.samples=4096;
960 desired.callback=_audio_testCallback;
961 desired.userdata=NULL;
964 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
965 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
966 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %i", id);
969 /* TODO: enable test code when function is available in SDL2 */
971 #ifdef AUDIODEVICECONNECTED_DEFINED
972 /* Get connected status */
973 result = SDL_AudioDeviceConnected(id);
974 SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
976 SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
978 /* Close device again */
979 SDL_CloseAudioDevice(id);
980 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
984 SDLTest_Log("No devices to test with");
987 return TEST_COMPLETED;
992 /* ================= Test Case References ================== */
994 /* Audio test cases */
995 static const SDLTest_TestCaseReference audioTest1 =
996 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED };
998 static const SDLTest_TestCaseReference audioTest2 =
999 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
1001 static const SDLTest_TestCaseReference audioTest3 =
1002 { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
1004 static const SDLTest_TestCaseReference audioTest4 =
1005 { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
1007 static const SDLTest_TestCaseReference audioTest5 =
1008 { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
1010 static const SDLTest_TestCaseReference audioTest6 =
1011 { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
1013 static const SDLTest_TestCaseReference audioTest7 =
1014 { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
1016 static const SDLTest_TestCaseReference audioTest8 =
1017 { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
1019 static const SDLTest_TestCaseReference audioTest9 =
1020 { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
1022 /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
1023 /* For debugging, test case can be run manually using --filter audio_convertAudio */
1025 static const SDLTest_TestCaseReference audioTest10 =
1026 { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
1028 /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
1030 static const SDLTest_TestCaseReference audioTest11 =
1031 { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
1033 static const SDLTest_TestCaseReference audioTest12 =
1034 { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
1036 static const SDLTest_TestCaseReference audioTest13 =
1037 { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
1039 static const SDLTest_TestCaseReference audioTest14 =
1040 { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
1042 static const SDLTest_TestCaseReference audioTest15 =
1043 { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
1045 /* Sequence of Audio test cases */
1046 static const SDLTest_TestCaseReference *audioTests[] = {
1047 &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
1048 &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
1049 &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
1052 /* Audio test suite (global) */
1053 SDLTest_TestSuiteReference audioTestSuite = {