[Tizen] Support Autoconf 2.71
[platform/upstream/SDL.git] / test / testautomation_audio.c
1 /**
2  * Original code: automated SDL audio test written by Edgar Simo "bobbens"
3  * New/updated tests: aschiffler at ferzkopp dot net
4  */
5
6 /* quiet windows compiler warnings */
7 #define _CRT_SECURE_NO_WARNINGS
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include "SDL.h"
13 #include "SDL_test.h"
14
15 /* ================= Test Case Implementation ================== */
16
17 /* Fixture */
18
19 void
20 _audioSetUp(void *arg)
21 {
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)");
26     if (ret != 0) {
27            SDLTest_LogError("%s", SDL_GetError());
28         }
29 }
30
31 void
32 _audioTearDown(void *arg)
33 {
34     /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
35     remove("sdlaudio.raw");
36
37     SDLTest_AssertPass("Cleanup of test files completed");
38 }
39
40
41 /* Global counter for callback invocation */
42 int _audio_testCallbackCounter;
43
44 /* Global accumulator for total callback length */
45 int _audio_testCallbackLength;
46
47
48 /* Test callback function */
49 void SDLCALL _audio_testCallback(void *userdata, Uint8 *stream, int len)
50 {
51    /* track that callback was called */
52    _audio_testCallbackCounter++;
53    _audio_testCallbackLength += len;
54 }
55
56
57 /* Test case functions */
58
59 /**
60  * \brief Stop and restart audio subsystem
61  *
62  * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
63  * \sa https://wiki.libsdl.org/SDL_InitSubSystem
64  */
65 int audio_quitInitAudioSubSystem()
66 {
67     /* Stop SDL audio subsystem */
68     SDL_QuitSubSystem( SDL_INIT_AUDIO );
69         SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
70
71         /* Restart audio again */
72         _audioSetUp(NULL);
73
74     return TEST_COMPLETED;
75 }
76
77 /**
78  * \brief Start and stop audio directly
79  *
80  * \sa https://wiki.libsdl.org/SDL_InitAudio
81  * \sa https://wiki.libsdl.org/SDL_QuitAudio
82  */
83 int audio_initQuitAudio()
84 {
85         int result;
86     int i, iMax;
87     const char* audioDriver;
88
89     /* Stop SDL audio subsystem */
90     SDL_QuitSubSystem( SDL_INIT_AUDIO );
91         SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
92
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);
102
103             /* Call Init */
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);
107
108             /* Call Quit */
109             SDL_AudioQuit();
110             SDLTest_AssertPass("Call to SDL_AudioQuit()");
111     }
112
113     /* NULL driver specification */
114     audioDriver = NULL;
115
116     /* Call Init */
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);
120
121     /* Call Quit */
122     SDL_AudioQuit();
123     SDLTest_AssertPass("Call to SDL_AudioQuit()");
124
125         /* Restart audio again */
126         _audioSetUp(NULL);
127
128     return TEST_COMPLETED;
129 }
130
131 /**
132  * \brief Start, open, close and stop audio
133  *
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
138  */
139 int audio_initOpenCloseQuitAudio()
140 {
141     int result, expectedResult;
142     int i, iMax, j, k;
143     const char* audioDriver;
144     SDL_AudioSpec desired;
145
146     /* Stop SDL audio subsystem */
147     SDL_QuitSubSystem( SDL_INIT_AUDIO );
148         SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
149
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);
161
162             /* Change specs */
163             for (j = 0; j < 2; j++) {
164
165                 /* Call Init */
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);
169
170                 /* Set spec */
171                 SDL_memset(&desired, 0, sizeof(desired));
172                 switch (j) {
173                     case 0:
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;
181
182                     case 1:
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;
190                     break;
191             }
192
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);
199             }
200
201             /* Call Close (maybe multiple times) */
202             for (k=0; k <= j; k++) {
203                 SDL_CloseAudio();
204                 SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k+1);
205             }
206
207             /* Call Quit (maybe multiple times) */
208             for (k=0; k <= j; k++) {
209                 SDL_AudioQuit();
210                 SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k+1);
211             }
212
213         } /* spec loop */
214     } /* driver loop */
215
216         /* Restart audio again */
217         _audioSetUp(NULL);
218
219     return TEST_COMPLETED;
220 }
221
222 /**
223  * \brief Pause and unpause audio
224  *
225  * \sa https://wiki.libsdl.org/SDL_PauseAudio
226  */
227 int audio_pauseUnpauseAudio()
228 {
229     int result;
230     int i, iMax, j, k, l;
231     int totalDelay;
232     int pause_on;
233     int originalCounter;
234     const char* audioDriver;
235     SDL_AudioSpec desired;
236
237     /* Stop SDL audio subsystem */
238     SDL_QuitSubSystem( SDL_INIT_AUDIO );
239         SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
240
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);
252
253             /* Change specs */
254             for (j = 0; j < 2; j++) {
255
256                 /* Call Init */
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);
260
261                 /* Set spec */
262                 SDL_memset(&desired, 0, sizeof(desired));
263                 switch (j) {
264                     case 0:
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;
272
273                     case 1:
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;
281                     break;
282             }
283
284             /* Call Open */
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);
288
289             /* Start and stop audio multiple times */
290             for (l=0; l<3; l++) {
291                 SDLTest_Log("Pause/Unpause iteration: %d", l+1);
292             
293                 /* Reset callback counters */
294                 _audio_testCallbackCounter = 0;
295                 _audio_testCallbackLength = 0;
296
297                 /* Un-pause audio to start playing (maybe multiple times) */
298                 pause_on = 0;
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);
302                 }
303             
304                 /* Wait for callback */
305                 totalDelay = 0;
306                 do {
307                     SDL_Delay(10);
308                     totalDelay += 10;
309                 } 
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);
313
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);
319                 }
320             
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);
325             }
326
327             /* Call Close */
328             SDL_CloseAudio();
329             SDLTest_AssertPass("Call to SDL_CloseAudio()");
330
331             /* Call Quit */
332             SDL_AudioQuit();
333             SDLTest_AssertPass("Call to SDL_AudioQuit()");
334
335         } /* spec loop */
336     } /* driver loop */
337
338     /* Restart audio again */
339     _audioSetUp(NULL);
340
341     return TEST_COMPLETED;
342 }
343
344 /**
345  * \brief Enumerate and name available audio devices (output and capture).
346  *
347  * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
348  * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
349  */
350 int audio_enumerateAndNameAudioDevices()
351 {
352    int t, tt;
353    int i, n, nn;
354    const char *name, *nameAgain;
355
356    /* Iterate over types: t=0 output device, t=1 input/capture device */
357    for (t=0; t<2; t++) {
358
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);
364
365       /* Variation of non-zero type */
366       if (t==1) {
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);
372       }
373
374       /* List devices. */
375       if (n>0) {
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);
380             if (name != NULL) {
381               SDLTest_AssertCheck(name[0] != '\0', "verify result from SDL_GetAudioDeviceName(%i, %i) is not empty, got: '%s'", i, t, name);
382               if (t==1) {
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",
391                       i, t, i, tt);
392                   }
393                }
394             }
395          }
396       }
397    }
398
399    return TEST_COMPLETED;
400 }
401
402 /**
403  * \brief Negative tests around enumeration and naming of audio devices.
404  *
405  * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
406  * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
407  */
408 int audio_enumerateAndNameAudioDevicesNegativeTests()
409 {
410    int t;
411    int i, j, no, nc;
412    const char *name;
413
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)");
419
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);
427
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);
434       }
435
436       /* Capture index past capture range but within output range */
437       if ((no>0) && (no>nc) && (t==1)) {
438          i = no-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);
442       }
443    }
444
445    return TEST_COMPLETED;
446 }
447
448
449 /**
450  * \brief Checks available audio driver names.
451  *
452  * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
453  * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
454  */
455 int audio_printAudioDrivers()
456 {
457    int i, n;
458    const char *name;
459
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);
464
465    /* List drivers. */
466    if (n>0)
467    {
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");
472          if (name != NULL) {
473             SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
474          }
475       }
476    }
477
478    return TEST_COMPLETED;
479 }
480
481
482 /**
483  * \brief Checks current audio driver name with initialized audio.
484  *
485  * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
486  */
487 int audio_printCurrentAudioDriver()
488 {
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");
493    if (name != NULL) {
494       SDLTest_AssertCheck(name[0] != '\0', "Verify returned name is not empty, got: '%s'", name);
495    }
496
497    return TEST_COMPLETED;
498 }
499
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 };
512
513
514 /**
515  * \brief Builds various audio conversion structures
516  *
517  * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
518  */
519 int audio_buildAudioCVT()
520 {
521   int result;
522   SDL_AudioCVT  cvt;
523   SDL_AudioSpec spec1;
524   SDL_AudioSpec spec2;
525   int i, ii, j, jj, k, kk;
526
527   /* No conversion needed */
528   spec1.format = AUDIO_S16LSB;
529   spec1.channels = 2;
530   spec1.freq = 22050;
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);
535
536   /* Typical conversion */
537   spec1.format = AUDIO_S8;
538   spec1.channels = 1;
539   spec1.freq = 22050;
540   spec2.format = AUDIO_S16LSB;
541   spec2.channels = 2;
542   spec2.freq = 44100;
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);
547
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);
566         if (result<0) {
567           SDLTest_LogError("%s", SDL_GetError());
568         } else {
569           SDLTest_AssertCheck(cvt.len_mult > 0, "Verify that cvt.len_mult value; expected: >0, got: %i", cvt.len_mult);
570         }
571       }
572     }
573   }
574
575    return TEST_COMPLETED;
576 }
577
578 /**
579  * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
580  *
581  * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
582  */
583 int audio_buildAudioCVTNegative()
584 {
585   const char *expectedError = "Parameter 'cvt' is invalid";
586   const char *error;
587   int result;
588   SDL_AudioCVT  cvt;
589   SDL_AudioSpec spec1;
590   SDL_AudioSpec spec2;
591   int i;
592   char message[256];
593
594   /* Valid format */
595   spec1.format = AUDIO_S8;
596   spec1.channels = 1;
597   spec1.freq = 22050;
598   spec2.format = AUDIO_S16LSB;
599   spec2.channels = 2;
600   spec2.freq = 44100;
601
602   SDL_ClearError();
603   SDLTest_AssertPass("Call to SDL_ClearError()");
604
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");
613   if (error != NULL) {
614       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
615           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
616   }
617
618   /* Invalid conversions */
619   for (i = 1; i < 64; i++) {
620     /* Valid format to start with */
621     spec1.format = AUDIO_S8;
622     spec1.channels = 1;
623     spec1.freq = 22050;
624     spec2.format = AUDIO_S16LSB;
625     spec2.channels = 2;
626     spec2.freq = 44100;
627
628     SDL_ClearError();
629     SDLTest_AssertPass("Call to SDL_ClearError()");
630
631     /* Set various invalid format inputs */
632     SDL_strlcpy(message, "Invalid: ", 256);
633     if (i & 1) {
634         SDL_strlcat(message, " spec1.format", 256);
635         spec1.format = 0;
636     }
637     if (i & 2) {
638         SDL_strlcat(message, " spec1.channels", 256);
639         spec1.channels = 0;
640     }
641     if (i & 4) {
642         SDL_strlcat(message, " spec1.freq", 256);
643         spec1.freq = 0;
644     }
645     if (i & 8) {
646         SDL_strlcat(message, " spec2.format", 256);
647         spec2.format = 0;
648     }
649     if (i & 16) {
650         SDL_strlcat(message, " spec2.channels", 256);
651         spec2.channels = 0;
652     }
653     if (i & 32) {
654         SDL_strlcat(message, " spec2.freq", 256);
655         spec2.freq = 0;
656     }
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");
665   }
666
667   SDL_ClearError();
668   SDLTest_AssertPass("Call to SDL_ClearError()");
669
670   return TEST_COMPLETED;
671 }
672
673 /**
674  * \brief Checks current audio status.
675  *
676  * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
677  */
678 int audio_getAudioStatus()
679 {
680    SDL_AudioStatus result;
681
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);
688
689    return TEST_COMPLETED;
690 }
691
692
693
694 /**
695  * \brief Opens, checks current audio status, and closes a device.
696  *
697  * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
698  */
699 int audio_openCloseAndGetAudioStatus()
700 {
701    SDL_AudioStatus result;
702    int i;
703    int count;
704    char *device;
705    SDL_AudioDeviceID id;
706    SDL_AudioSpec desired, obtained;
707
708    /* Get number of devices. */
709    count = SDL_GetNumAudioDevices(0);
710    SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
711    if (count > 0) {
712      for (i = 0; i < count; i++) {
713        /* Get device name */
714        device = (char *)SDL_GetAudioDeviceName(i, 0);
715
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;
719
720        /* Set standard desired spec */
721        desired.freq=22050;
722        desired.format=AUDIO_S16SYS;
723        desired.channels=2;
724        desired.samples=4096;
725        desired.callback=_audio_testCallback;
726        desired.userdata=NULL;
727
728        /* Open device */
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);
732        if (id > 1) {
733
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);
740
741          /* Close device again */
742          SDL_CloseAudioDevice(id);
743          SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
744        }
745      }
746    } else {
747      SDLTest_Log("No devices to test with");
748    }
749
750    return TEST_COMPLETED;
751 }
752
753 /**
754  * \brief Locks and unlocks open audio device.
755  *
756  * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
757  * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
758  */
759 int audio_lockUnlockOpenAudioDevice()
760 {
761    int i;
762    int count;
763    char *device;
764    SDL_AudioDeviceID id;
765    SDL_AudioSpec desired, obtained;
766
767    /* Get number of devices. */
768    count = SDL_GetNumAudioDevices(0);
769    SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
770    if (count > 0) {
771      for (i = 0; i < count; i++) {
772        /* Get device name */
773        device = (char *)SDL_GetAudioDeviceName(i, 0);
774            
775        /*add by majunqing for skip the device 1*/
776        const char* ignoreDevice = "VIRTUAL AUDIO W";
777        if(SDL_strcmp(device, ignoreDevice) == 0)
778        {
779                 continue;
780        }
781            
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;
785
786        /* Set standard desired spec */
787        desired.freq=22050;
788        desired.format=AUDIO_S16SYS;
789        desired.channels=2;
790        desired.samples=4096;
791        desired.callback=_audio_testCallback;
792        desired.userdata=NULL;
793
794        /* Open device */
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);
798        if (id > 1) {
799          /* Lock to protect callback */
800          SDL_LockAudioDevice(id);
801          SDLTest_AssertPass("SDL_LockAudioDevice(%i)", id);
802
803          /* Simulate callback processing */
804          SDL_Delay(10);
805          SDLTest_Log("Simulate callback processing - delay");
806
807          /* Unlock again */
808          SDL_UnlockAudioDevice(id);
809          SDLTest_AssertPass("SDL_UnlockAudioDevice(%i)", id);
810
811          /* Close device again */
812          SDL_CloseAudioDevice(id);
813          SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
814        }
815      }
816    } else {
817      SDLTest_Log("No devices to test with");
818    }
819
820    return TEST_COMPLETED;
821 }
822
823
824 /**
825  * \brief Convert audio using various conversion structures
826  *
827  * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
828  * \sa https://wiki.libsdl.org/SDL_ConvertAudio
829  */
830 int audio_convertAudio()
831 {
832   int result;
833   SDL_AudioCVT  cvt;
834   SDL_AudioSpec spec1;
835   SDL_AudioSpec spec2;
836   int c;
837   char message[128];
838   int i, ii, j, jj, k, kk, l, ll;
839
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);
843     if (c & 1) {
844       SDL_strlcat(message, " Format", 128);
845     }
846     if (c & 2) {
847       SDL_strlcat(message, " Channels", 128);
848     }
849     if (c & 4) {
850       SDL_strlcat(message, " Frequencies", 128);
851     }
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];
860
861           /* Ensure we have a different target format */
862           do {
863             if (c & 1) {
864               ii = SDLTest_RandomIntegerInRange(0, _numAudioFormats - 1);
865             } else {
866               ii = 1;
867             }
868             if (c & 2) {
869               jj = SDLTest_RandomIntegerInRange(0, _numAudioChannels - 1);
870             } else {
871               jj= j;
872             }
873             if (c & 4) {
874               kk = SDLTest_RandomIntegerInRange(0, _numAudioFrequencies - 1);
875             } else {
876               kk = k;
877             }
878           } while ((i == ii) && (j == jj) && (k == kk));
879           spec2.format = _audioFormats[ii];
880           spec2.channels = _audioChannels[jj];
881           spec2.freq = _audioFrequencies[kk];
882
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);
888           if (result != 1) {
889             SDLTest_LogError("%s", SDL_GetError());
890           } else {
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;
893
894             /* Create some random data to convert */
895             l = 64;
896             ll = l * cvt.len_mult;
897             SDLTest_Log("Creating dummy sample buffer of %i length (%i bytes)", l, ll);
898             cvt.len = l;
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;
902
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);
909
910             /* Free converted buffer */
911             SDL_free(cvt.buf);
912             cvt.buf = NULL;
913           }
914         }
915       }
916     }
917   }
918
919    return TEST_COMPLETED;
920 }
921
922
923 /**
924  * \brief Opens, checks current connected status, and closes a device.
925  *
926  * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
927  */
928 int audio_openCloseAudioDeviceConnected()
929 {
930    int result = -1;
931    int i;
932    int count;
933    char *device;
934    SDL_AudioDeviceID id;
935    SDL_AudioSpec desired, obtained;
936
937    /* Get number of devices. */
938    count = SDL_GetNumAudioDevices(0);
939    SDLTest_AssertPass("Call to SDL_GetNumAudioDevices(0)");
940    if (count > 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)
947        {
948                 continue;
949        }
950            
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;
954
955        /* Set standard desired spec */
956        desired.freq=22050;
957        desired.format=AUDIO_S16SYS;
958        desired.channels=2;
959        desired.samples=4096;
960        desired.callback=_audio_testCallback;
961        desired.userdata=NULL;
962
963        /* Open device */
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);
967        if (id > 1) {
968
969 /* TODO: enable test code when function is available in SDL2 */
970
971 #ifdef AUDIODEVICECONNECTED_DEFINED
972          /* Get connected status */
973          result = SDL_AudioDeviceConnected(id);
974          SDLTest_AssertPass("Call to SDL_AudioDeviceConnected()");
975 #endif
976          SDLTest_AssertCheck(result == 1, "Verify returned value; expected: 1; got: %i", result);
977
978          /* Close device again */
979          SDL_CloseAudioDevice(id);
980          SDLTest_AssertPass("Call to SDL_CloseAudioDevice()");
981        }
982      }
983    } else {
984      SDLTest_Log("No devices to test with");
985    }
986
987    return TEST_COMPLETED;
988 }
989
990
991
992 /* ================= Test Case References ================== */
993
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 };
997
998 static const SDLTest_TestCaseReference audioTest2 =
999         { (SDLTest_TestCaseFp)audio_enumerateAndNameAudioDevicesNegativeTests, "audio_enumerateAndNameAudioDevicesNegativeTests", "Negative tests around enumeration and naming of audio devices.", TEST_ENABLED };
1000
1001 static const SDLTest_TestCaseReference audioTest3 =
1002         { (SDLTest_TestCaseFp)audio_printAudioDrivers, "audio_printAudioDrivers", "Checks available audio driver names.", TEST_ENABLED };
1003
1004 static const SDLTest_TestCaseReference audioTest4 =
1005         { (SDLTest_TestCaseFp)audio_printCurrentAudioDriver, "audio_printCurrentAudioDriver", "Checks current audio driver name with initialized audio.", TEST_ENABLED };
1006
1007 static const SDLTest_TestCaseReference audioTest5 =
1008         { (SDLTest_TestCaseFp)audio_buildAudioCVT, "audio_buildAudioCVT", "Builds various audio conversion structures.", TEST_ENABLED };
1009
1010 static const SDLTest_TestCaseReference audioTest6 =
1011         { (SDLTest_TestCaseFp)audio_buildAudioCVTNegative, "audio_buildAudioCVTNegative", "Checks calls with invalid input to SDL_BuildAudioCVT", TEST_ENABLED };
1012
1013 static const SDLTest_TestCaseReference audioTest7 =
1014         { (SDLTest_TestCaseFp)audio_getAudioStatus, "audio_getAudioStatus", "Checks current audio status.", TEST_ENABLED };
1015
1016 static const SDLTest_TestCaseReference audioTest8 =
1017         { (SDLTest_TestCaseFp)audio_openCloseAndGetAudioStatus, "audio_openCloseAndGetAudioStatus", "Opens and closes audio device and get audio status.", TEST_ENABLED };
1018
1019 static const SDLTest_TestCaseReference audioTest9 =
1020         { (SDLTest_TestCaseFp)audio_lockUnlockOpenAudioDevice, "audio_lockUnlockOpenAudioDevice", "Locks and unlocks an open audio device.", TEST_ENABLED };
1021
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  */
1024
1025 static const SDLTest_TestCaseReference audioTest10 =
1026         { (SDLTest_TestCaseFp)audio_convertAudio, "audio_convertAudio", "Convert audio using available formats.", TEST_DISABLED };
1027
1028 /* TODO: enable test when SDL_AudioDeviceConnected has been implemented.           */
1029
1030 static const SDLTest_TestCaseReference audioTest11 =
1031         { (SDLTest_TestCaseFp)audio_openCloseAudioDeviceConnected, "audio_openCloseAudioDeviceConnected", "Opens and closes audio device and get connected status.", TEST_DISABLED };
1032
1033 static const SDLTest_TestCaseReference audioTest12 =
1034         { (SDLTest_TestCaseFp)audio_quitInitAudioSubSystem, "audio_quitInitAudioSubSystem", "Quit and re-init audio subsystem.", TEST_ENABLED };
1035
1036 static const SDLTest_TestCaseReference audioTest13 =
1037         { (SDLTest_TestCaseFp)audio_initQuitAudio, "audio_initQuitAudio", "Init and quit audio drivers directly.", TEST_ENABLED };
1038
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 };
1041
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 };
1044
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
1050 };
1051
1052 /* Audio test suite (global) */
1053 SDLTest_TestSuiteReference audioTestSuite = {
1054     "Audio",
1055     _audioSetUp,
1056     audioTests,
1057     _audioTearDown
1058 };