src/audio/wasapi/SDL_wasapi_win32.c
changeset 12345 50e1cca28b39
parent 12222 09e314140a28
child 12503 806492103856
equal deleted inserted replaced
12344:9dd351b26dcb 12345:50e1cca28b39
   349 
   349 
   350     return 0;  /* good to go. */
   350     return 0;  /* good to go. */
   351 }
   351 }
   352 
   352 
   353 
   353 
       
   354 typedef struct
       
   355 {
       
   356     LPWSTR devid;
       
   357     char *devname;
       
   358 } EndpointItem;
       
   359 
       
   360 static int sort_endpoints(const void *_a, const void *_b)
       
   361 {
       
   362     LPWSTR a = ((const EndpointItem *) _a)->devid;
       
   363     LPWSTR b = ((const EndpointItem *) _b)->devid;
       
   364     if (!a && b) {
       
   365         return -1;
       
   366     } else if (a && !b) {
       
   367         return 1;
       
   368     }
       
   369 
       
   370     while (SDL_TRUE) {
       
   371         if (*a < *b) {
       
   372             return -1;
       
   373         } else if (*a > *b) {
       
   374             return 1;
       
   375         } else if (*a == 0) {
       
   376             break;
       
   377         }
       
   378         a++;
       
   379         b++;
       
   380     }
       
   381 
       
   382     return 0;
       
   383 }
       
   384 
   354 static void
   385 static void
   355 WASAPI_EnumerateEndpointsForFlow(const SDL_bool iscapture)
   386 WASAPI_EnumerateEndpointsForFlow(const SDL_bool iscapture)
   356 {
   387 {
   357     IMMDeviceCollection *collection = NULL;
   388     IMMDeviceCollection *collection = NULL;
       
   389     EndpointItem *items;
   358     UINT i, total;
   390     UINT i, total;
   359 
   391 
   360     /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
   392     /* Note that WASAPI separates "adapter devices" from "audio endpoint devices"
   361        ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
   393        ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */
   362 
   394 
   367     if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
   399     if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) {
   368         IMMDeviceCollection_Release(collection);
   400         IMMDeviceCollection_Release(collection);
   369         return;
   401         return;
   370     }
   402     }
   371 
   403 
       
   404     items = (EndpointItem *) SDL_calloc(total, sizeof (EndpointItem));
       
   405     if (!items) {
       
   406         return;  /* oh well. */
       
   407     }
       
   408 
   372     for (i = 0; i < total; i++) {
   409     for (i = 0; i < total; i++) {
       
   410         EndpointItem *item = items + i;
   373         IMMDevice *device = NULL;
   411         IMMDevice *device = NULL;
   374         if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
   412         if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) {
   375             LPWSTR devid = NULL;
   413             if (SUCCEEDED(IMMDevice_GetId(device, &item->devid))) {
   376             if (SUCCEEDED(IMMDevice_GetId(device, &devid))) {
   414                 item->devname = GetWasapiDeviceName(device);
   377                 char *devname = GetWasapiDeviceName(device);
       
   378                 if (devname) {
       
   379                     WASAPI_AddDevice(iscapture, devname, devid);
       
   380                     SDL_free(devname);
       
   381                 }
       
   382                 CoTaskMemFree(devid);
       
   383             }
   415             }
   384             IMMDevice_Release(device);
   416             IMMDevice_Release(device);
   385         }
   417         }
   386     }
   418     }
   387 
   419 
       
   420     /* sort the list of devices by their guid so list is consistent between runs */
       
   421     SDL_qsort(items, total, sizeof (*items), sort_endpoints);
       
   422 
       
   423     /* Send the sorted list on to the SDL's higher level. */
       
   424     for (i = 0; i < total; i++) {
       
   425         EndpointItem *item = items + i;
       
   426         if ((item->devid) && (item->devname)) {
       
   427             WASAPI_AddDevice(iscapture, item->devname, item->devid);
       
   428         }
       
   429         SDL_free(item->devname);
       
   430         CoTaskMemFree(item->devid);
       
   431     }
       
   432 
       
   433     SDL_free(items);
   388     IMMDeviceCollection_Release(collection);
   434     IMMDeviceCollection_Release(collection);
   389 }
   435 }
   390 
   436 
   391 void
   437 void
   392 WASAPI_EnumerateEndpoints(void)
   438 WASAPI_EnumerateEndpoints(void)