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 _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);
715 /*add by majunqing for skip the device 1*/
716 const char* ignoreDevice = "VIRTUAL AUDIO W";
717 if(SDL_strcmp(device, ignoreDevice) == 0)
721 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
722 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
723 if (device == NULL) return TEST_ABORTED;
725 /* Set standard desired spec */
727 desired.format=AUDIO_S16SYS;
729 desired.samples=4096;
730 desired.callback=_audio_testCallback;
731 desired.userdata=NULL;
734 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
735 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
736 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
739 /* Check device audio status */
740 result = SDL_GetAudioDeviceStatus(id);
741 SDLTest_AssertPass("Call to SDL_GetAudioDeviceStatus()");
742 SDLTest_AssertCheck(result == SDL_AUDIO_STOPPED || result == SDL_AUDIO_PLAYING || result == SDL_AUDIO_PAUSED,
743 "Verify returned value; expected: STOPPED (%i) | PLAYING (%i) | PAUSED (%i), got: %i",
744 SDL_AUDIO_STOPPED, SDL_AUDIO_PLAYING, SDL_AUDIO_PAUSED, result);
746 /* Close device again */
747 SDL_CloseAudioDevice(id);
748 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
752 SDLTest_Log("No devices to test with");
755 return TEST_COMPLETED;
759 * \brief Locks and unlocks open audio device.
761 * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
762 * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
764 int audio_lockUnlockOpenAudioDevice()
769 SDL_AudioDeviceID id;
770 SDL_AudioSpec desired, obtained;
772 /* Get number of devices. */
773 count = SDL_GetNumAudioDevices(0);
774 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
776 for (i = 0; i < count; i++) {
777 /* Get device name */
778 device = (char *)SDL_GetAudioDeviceName(i, 0);
780 /*add by majunqing for skip the device 1*/
781 const char* ignoreDevice = "VIRTUAL AUDIO W";
782 if(SDL_strcmp(device, ignoreDevice) == 0)
787 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
788 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
789 if (device == NULL) return TEST_ABORTED;
791 /* Set standard desired spec */
793 desired.format=AUDIO_S16SYS;
795 desired.samples=4096;
796 desired.callback=_audio_testCallback;
797 desired.userdata=NULL;
800 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
801 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
802 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >=2, got: %i", id);
804 /* Lock to protect callback */
805 SDL_LockAudioDevice(id);
806 SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
808 /* Simulate callback processing */
810 SDLTest_Log("Simulate callback processing - delay");
813 SDL_UnlockAudioDevice(id);
814 SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);
816 /* Close device again */
817 SDL_CloseAudioDevice(id);
818 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
822 SDLTest_Log("No devices to test with");
825 return TEST_COMPLETED;
830 * \brief Convert audio using various conversion structures
832 * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
833 * \sa https://wiki.libsdl.org/SDL_ConvertAudio
835 int audio_convertAudio()
843 int i, ii, j, jj, k, kk, l, ll;
845 /* Iterate over bitmask that determines which parameters are modified in the conversion */
846 for (c = 1; c < 8; c++) {
847 SDL_strlcpy(message, "Changing:", 128);
849 SDL_strlcat(message, " Format", 128);
852 SDL_strlcat(message, " Channels", 128);
855 SDL_strlcat(message, " Frequencies", 128);
857 SDLTest_Log("%s", message);
858 /* All source conversions with random conversion targets */
859 for (i = 0; i < _numAudioFormats; i++) {
860 for (j = 0; j < _numAudioChannels; j++) {
861 for (k = 0; k < _numAudioFrequencies; k++) {
862 spec1.format = _audioFormats[i];
863 spec1.channels = _audioChannels[j];
864 spec1.freq = _audioFrequencies[k];
866 /* Ensure we have a different target format */
869 ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
874 jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
879 kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
883 } while ((i == ii) && (j == jj) && (k == kk));
884 spec2.format = _audioFormats[ii];
885 spec2.channels = _audioChannels[jj];
886 spec2.freq = _audioFrequencies[kk];
888 result = SDL_BuildAudioCVT(&cvt, spec1.format, spec1.channels, spec1.freq,
889 spec2.format, spec2.channels, spec2.freq);
890 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)",
891 i, _audioFormatsVerbose[i], spec1.format, j, spec1.channels, k, spec1.freq, ii, _audioFormatsVerbose[ii], spec2.format, jj, spec2.channels, kk, spec2.freq);
892 SDLTest_AssertCheck(result == 1, "Verify result value; expected: 1, got: %i", result);
894 SDLTest_LogError("%s", SDL_GetError());
896 SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
897 if (cvt.len_mult < 1) return TEST_ABORTED;
899 /* Create some random data to convert */
901 ll = l * cvt.len_mult;
902 SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
904 cvt.buf = (Uint8 *)SDL_malloc(ll);
905 SDLTest_AssertCheck(cvt.buf != NULL, "Check data buffer to convert is not NULL");
906 if (cvt.buf == NULL) return TEST_ABORTED;
908 /* Convert the data */
909 result = SDL_ConvertAudio(&cvt);
910 SDLTest_AssertPass("Call to SDL_ConvertAudio()");
911 SDLTest_AssertCheck(result == 0, "Verify result value; expected: 0; got: %i", result);
912 SDLTest_AssertCheck(cvt.buf != NULL, "Verify conversion buffer is not NULL");
913 SDLTest_AssertCheck(cvt.len_ratio > 0.0, "Verify conversion length ratio; expected: >0; got: %f", cvt.len_ratio);
915 /* Free converted buffer */
924 return TEST_COMPLETED;
929 * \brief Opens, checks current connected status, and closes a device.
931 * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
933 int audio_openCloseAudioDeviceConnected()
939 SDL_AudioDeviceID id;
940 SDL_AudioSpec desired, obtained;
942 /* Get number of devices. */
943 count = SDL_GetNumAudioDevices(0);
944 SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
946 for (i = 0; i < count; i++) {
947 /* Get device name */
948 device = (char *)SDL_GetAudioDeviceName(i, 0);
949 /*add by majunqing for skip the device 1*/
950 const char* ignoreDevice = "VIRTUAL AUDIO W";
951 if(SDL_strcmp(device, ignoreDevice) == 0)
956 SDLTest_AssertPass("SDL_GetAudioDeviceName(%i,0)", i);
957 SDLTest_AssertCheck(device != NULL, "Validate device name is not NULL; got: %s", (device != NULL) ? device : "NULL");
958 if (device == NULL) return TEST_ABORTED;
960 /* Set standard desired spec */
962 desired.format=AUDIO_S16SYS;
964 desired.samples=4096;
965 desired.callback=_audio_testCallback;
966 desired.userdata=NULL;
969 id = SDL_OpenAudioDevice((const char *)device, 0, &desired, &obtained, SDL_AUDIO_ALLOW_ANY_CHANGE);
970 SDLTest_AssertPass("SDL_OpenAudioDevice('%s',...)", device);
971 SDLTest_AssertCheck(id > 1, "Validate device ID; expected: >1, got: %i", id);
974 /* TODO: enable test code when function is available in SDL2 */
976 #ifdef AUDIODEVICECONNECTED_DEFINED
977 /* Get connected status */
978 result = SDL_AudioDeviceConnected(id);
979 SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
981 SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
983 /* Close device again */
984 SDL_CloseAudioDevice(id);
985 SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
989 SDLTest_Log("No devices to test with");
992 return TEST_COMPLETED;
997 /* ================= Test Case References ================== */
999 /* Audio test cases */
1000 static const SDLTest_TestCaseReference audioTest1 =
1001 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevices, "audio_enumerateAndNameAudioDevices", "Enumerate and name available audio devices (output and capture)", TEST_ENABLED };
1003 static const SDLTest_TestCaseReference audioTest2 =
1004 { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
1006 static const SDLTest_TestCaseReference audioTest3 =
1007 { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
1009 static const SDLTest_TestCaseReference audioTest4 =
1010 { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
1012 static const SDLTest_TestCaseReference audioTest5 =
1013 { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
1015 static const SDLTest_TestCaseReference audioTest6 =
1016 { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
1018 static const SDLTest_TestCaseReference audioTest7 =
1019 { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
1021 static const SDLTest_TestCaseReference audioTest8 =
1022 { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
1024 static const SDLTest_TestCaseReference audioTest9 =
1025 { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
1027 /* TODO: enable test when SDL_ConvertAudio segfaults on cygwin have been fixed. */
1028 /* For debugging, test case can be run manually using --filter audio_convertAudio */
1030 static const SDLTest_TestCaseReference audioTest10 =
1031 { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
1033 /* TODO: enable test when SDL_AudioDeviceConnected has been implemented. */
1035 static const SDLTest_TestCaseReference audioTest11 =
1036 { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
1038 static const SDLTest_TestCaseReference audioTest12 =
1039 { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
1041 static const SDLTest_TestCaseReference audioTest13 =
1042 { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
1044 static const SDLTest_TestCaseReference audioTest14 =
1045 { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
1047 static const SDLTest_TestCaseReference audioTest15 =
1048 { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
1050 /* Sequence of Audio test cases */
1051 static const SDLTest_TestCaseReference *audioTests[] = {
1052 &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
1053 &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
1054 &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
1057 /* Audio test suite (global) */
1058 SDLTest_TestSuiteReference audioTestSuite = {