src/audio/SDL_audio.c
changeset 5593 ab22ca13c47f
parent 5592 2e88d0742f4d
child 5596 d4b2f6543074
equal deleted inserted replaced
5592:2e88d0742f4d 5593:ab22ca13c47f
   149     return open_devices[id];
   149     return open_devices[id];
   150 }
   150 }
   151 
   151 
   152 
   152 
   153 /* stubs for audio drivers that don't need a specific entry point... */
   153 /* stubs for audio drivers that don't need a specific entry point... */
   154 static int
   154 static void
   155 SDL_AudioDetectDevices_Default(int iscapture)
   155 SDL_AudioDetectDevices_Default(int iscapture, SDL_AddAudioDevice addfn)
   156 {
   156 {                               /* no-op. */
   157     return -1;
       
   158 }
   157 }
   159 
   158 
   160 static void
   159 static void
   161 SDL_AudioThreadInit_Default(_THIS)
   160 SDL_AudioThreadInit_Default(_THIS)
   162 {                               /* no-op. */
   161 {                               /* no-op. */
   195 
   194 
   196 static int
   195 static int
   197 SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
   196 SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
   198 {
   197 {
   199     return 0;
   198     return 0;
   200 }
       
   201 
       
   202 static const char *
       
   203 SDL_AudioGetDeviceName_Default(int index, int iscapture)
       
   204 {
       
   205     SDL_SetError("No such device");
       
   206     return NULL;
       
   207 }
   199 }
   208 
   200 
   209 static void
   201 static void
   210 SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
   202 SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
   211 {
   203 {
   236 #define FILL_STUB(x) \
   228 #define FILL_STUB(x) \
   237         if (current_audio.impl.x == NULL) { \
   229         if (current_audio.impl.x == NULL) { \
   238             current_audio.impl.x = SDL_Audio##x##_Default; \
   230             current_audio.impl.x = SDL_Audio##x##_Default; \
   239         }
   231         }
   240     FILL_STUB(DetectDevices);
   232     FILL_STUB(DetectDevices);
   241     FILL_STUB(GetDeviceName);
       
   242     FILL_STUB(OpenDevice);
   233     FILL_STUB(OpenDevice);
   243     FILL_STUB(ThreadInit);
   234     FILL_STUB(ThreadInit);
   244     FILL_STUB(WaitDevice);
   235     FILL_STUB(WaitDevice);
   245     FILL_STUB(PlayDevice);
   236     FILL_STUB(PlayDevice);
   246     FILL_STUB(GetDeviceBuf);
   237     FILL_STUB(GetDeviceBuf);
   639 SDL_GetCurrentAudioDriver()
   630 SDL_GetCurrentAudioDriver()
   640 {
   631 {
   641     return current_audio.name;
   632     return current_audio.name;
   642 }
   633 }
   643 
   634 
       
   635 static void
       
   636 free_device_list(char ***devices, int *devCount)
       
   637 {
       
   638     int i = *devCount;
       
   639     if ((i > 0) && (*devices != NULL)) {
       
   640         while (i--) {
       
   641             SDL_free((*devices)[i]);
       
   642         }
       
   643     }
       
   644 
       
   645     if (*devices != NULL) {
       
   646         SDL_free(*devices);
       
   647     }
       
   648 
       
   649     *devices = NULL;
       
   650     *devCount = 0;
       
   651 }
       
   652 
       
   653 static
       
   654 void SDL_AddCaptureAudioDevice(const char *_name)
       
   655 {
       
   656     char *name = NULL;
       
   657     void *ptr = SDL_realloc(current_audio.inputDevices,
       
   658                           (current_audio.inputDeviceCount+1) * sizeof(char*));
       
   659     if (ptr == NULL) {
       
   660         return;  /* oh well. */
       
   661     }
       
   662 
       
   663     current_audio.inputDevices = (char **) ptr;
       
   664     name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
       
   665     current_audio.inputDevices[current_audio.inputDeviceCount++] = name;
       
   666 }
       
   667 
       
   668 static
       
   669 void SDL_AddOutputAudioDevice(const char *_name)
       
   670 {
       
   671     char *name = NULL;
       
   672     void *ptr = SDL_realloc(current_audio.outputDevices,
       
   673                           (current_audio.outputDeviceCount+1) * sizeof(char*));
       
   674     if (ptr == NULL) {
       
   675         return;  /* oh well. */
       
   676     }
       
   677 
       
   678     current_audio.outputDevices = (char **) ptr;
       
   679     name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
       
   680     current_audio.outputDevices[current_audio.outputDeviceCount++] = name;
       
   681 }
       
   682 
   644 
   683 
   645 int
   684 int
   646 SDL_GetNumAudioDevices(int iscapture)
   685 SDL_GetNumAudioDevices(int iscapture)
   647 {
   686 {
       
   687     int retval = 0;
       
   688 
   648     if (!SDL_WasInit(SDL_INIT_AUDIO)) {
   689     if (!SDL_WasInit(SDL_INIT_AUDIO)) {
   649         return -1;
   690         return -1;
   650     }
   691     }
       
   692 
   651     if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
   693     if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
   652         return 0;
   694         return 0;
   653     }
   695     }
   654 
   696 
   655     if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
   697     if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
   658 
   700 
   659     if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
   701     if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
   660         return 1;
   702         return 1;
   661     }
   703     }
   662 
   704 
   663     return current_audio.impl.DetectDevices(iscapture);
   705     if (iscapture) {
       
   706         free_device_list(&current_audio.inputDevices,
       
   707                          &current_audio.inputDeviceCount);
       
   708         current_audio.impl.DetectDevices(iscapture, SDL_AddCaptureAudioDevice);
       
   709         retval = current_audio.inputDeviceCount;
       
   710     } else {
       
   711         free_device_list(&current_audio.outputDevices,
       
   712                          &current_audio.outputDeviceCount);
       
   713         current_audio.impl.DetectDevices(iscapture, SDL_AddOutputAudioDevice);
       
   714         retval = current_audio.outputDeviceCount;
       
   715     }
       
   716 
       
   717     return retval;
   664 }
   718 }
   665 
   719 
   666 
   720 
   667 const char *
   721 const char *
   668 SDL_GetAudioDeviceName(int index, int iscapture)
   722 SDL_GetAudioDeviceName(int index, int iscapture)
   676         SDL_SetError("No capture support");
   730         SDL_SetError("No capture support");
   677         return NULL;
   731         return NULL;
   678     }
   732     }
   679 
   733 
   680     if (index < 0) {
   734     if (index < 0) {
   681         SDL_SetError("No such device");
   735         goto no_such_device;
   682         return NULL;
       
   683     }
   736     }
   684 
   737 
   685     if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
   738     if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
   686         return DEFAULT_INPUT_DEVNAME;
   739         return DEFAULT_INPUT_DEVNAME;
   687     }
   740     }
   688 
   741 
   689     if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
   742     if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
   690         return DEFAULT_OUTPUT_DEVNAME;
   743         return DEFAULT_OUTPUT_DEVNAME;
   691     }
   744     }
   692 
   745 
   693     return current_audio.impl.GetDeviceName(index, iscapture);
   746     if (iscapture) {
       
   747         if (index >= current_audio.inputDeviceCount) {
       
   748             goto no_such_device;
       
   749         }
       
   750         return current_audio.inputDevices[index];
       
   751     } else {
       
   752         if (index >= current_audio.outputDeviceCount) {
       
   753             goto no_such_device;
       
   754         }
       
   755         return current_audio.outputDevices[index];
       
   756     }
       
   757 
       
   758 no_such_device:
       
   759     SDL_SetError("No such device");
       
   760     return NULL;
   694 }
   761 }
   695 
   762 
   696 
   763 
   697 static void
   764 static void
   698 close_audio_device(SDL_AudioDevice * device)
   765 close_audio_device(SDL_AudioDevice * device)
   878             SDL_SetError("Couldn't create mixer lock");
   945             SDL_SetError("Couldn't create mixer lock");
   879             return 0;
   946             return 0;
   880         }
   947         }
   881     }
   948     }
   882 
   949 
       
   950     /* force a device detection if we haven't done one yet. */
       
   951     if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
       
   952          ((!iscapture) && (current_audio.outputDevices == NULL)) )
       
   953         SDL_GetNumAudioDevices(iscapture);
       
   954 
   883     if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
   955     if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
   884         close_audio_device(device);
   956         close_audio_device(device);
   885         return 0;
   957         return 0;
   886     }
   958     }
   887     device->opened = 1;
   959     device->opened = 1;
  1119         SDL_CloseAudioDevice(i);
  1191         SDL_CloseAudioDevice(i);
  1120     }
  1192     }
  1121 
  1193 
  1122     /* Free the driver data */
  1194     /* Free the driver data */
  1123     current_audio.impl.Deinitialize();
  1195     current_audio.impl.Deinitialize();
       
  1196     free_device_list(&current_audio.outputDevices,
       
  1197                      &current_audio.outputDeviceCount);
       
  1198     free_device_list(&current_audio.inputDevices,
       
  1199                      &current_audio.inputDeviceCount);
  1124     SDL_memset(&current_audio, '\0', sizeof(current_audio));
  1200     SDL_memset(&current_audio, '\0', sizeof(current_audio));
  1125     SDL_memset(open_devices, '\0', sizeof(open_devices));
  1201     SDL_memset(open_devices, '\0', sizeof(open_devices));
  1126 }
  1202 }
  1127 
  1203 
  1128 #define NUM_FORMATS 10
  1204 #define NUM_FORMATS 10