src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Nov 2016 12:41:06 -0800
changeset 10603 d2c8ae6ceac8
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed bug 3484 - DSP driver does not detect /dev/dsp0

Tobias Kortkamp

using SDL 2.0.5 (and a repository checkout) on FreeBSD 11.0 I get this output
from testaudioinfo with SDL_AUDIODRIVER=dsp:

INFO: Found 8 output devices:
INFO: 0: /dev/dsp
INFO: 1: /dev/dsp1
INFO: 2: /dev/dsp2
INFO: 3: /dev/dsp3
INFO: 4: /dev/dsp4
INFO: 5: /dev/dsp5
INFO: 6: /dev/dsp6
INFO: 7: /dev/dsp7
INFO:
INFO: Found 3 capture devices:
INFO: 0: /dev/dsp
INFO: 1: /dev/dsp4
INFO: 2: /dev/dsp5
INFO:

This is /dev/sndstat:

Installed devices:
pcm0: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm1: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm2: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm3: <NVIDIA (0x0040) (HDMI/DP 8ch)> (play)
pcm4: <Realtek ALC887 (Rear Analog 7.1/2.0)> (play/rec)
pcm5: <Realtek ALC887 (Front Analog)> (play/rec) default
pcm6: <Realtek ALC887 (Rear Digital)> (play)
pcm7: <Realtek ALC887 (Onboard Digital)> (play)
No devices installed from userspace.

I'd expect to find /dev/dsp0 in the output device list. It's not detected
because of a a small logic error in SDL_audiodev.c (see attached patch).

With the patch applied I get this which is what I'd expect:

INFO: Found 9 output devices:
INFO: 0: /dev/dsp
INFO: 1: /dev/dsp0
INFO: 2: /dev/dsp1
INFO: 3: /dev/dsp2
INFO: 4: /dev/dsp3
INFO: 5: /dev/dsp4
INFO: 6: /dev/dsp5
INFO: 7: /dev/dsp6
INFO: 8: /dev/dsp7
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 /* Get the name of the audio device we use for output */
    24 
    25 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    26 
    27 #include <fcntl.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include <unistd.h> /* For close() */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_audiodev_c.h"
    34 
    35 #ifndef _PATH_DEV_DSP
    36 #if defined(__NETBSD__) || defined(__OPENBSD__)
    37 #define _PATH_DEV_DSP  "/dev/audio"
    38 #else
    39 #define _PATH_DEV_DSP  "/dev/dsp"
    40 #endif
    41 #endif
    42 #ifndef _PATH_DEV_DSP24
    43 #define _PATH_DEV_DSP24 "/dev/sound/dsp"
    44 #endif
    45 #ifndef _PATH_DEV_AUDIO
    46 #define _PATH_DEV_AUDIO "/dev/audio"
    47 #endif
    48 
    49 static void
    50 test_device(const int iscapture, const char *fname, int flags, int (*test) (int fd))
    51 {
    52     struct stat sb;
    53     if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    54         const int audio_fd = open(fname, flags, 0);
    55         if (audio_fd >= 0) {
    56             const int okay = test(audio_fd);
    57             close(audio_fd);
    58             if (okay) {
    59                 static size_t dummyhandle = 0;
    60                 dummyhandle++;
    61                 SDL_assert(dummyhandle != 0);
    62                 SDL_AddAudioDevice(iscapture, fname, (void *) dummyhandle);
    63             }
    64         }
    65     }
    66 }
    67 
    68 static int
    69 test_stub(int fd)
    70 {
    71     return 1;
    72 }
    73 
    74 static void
    75 SDL_EnumUnixAudioDevices_Internal(const int iscapture, const int classic, int (*test)(int))
    76 {
    77     const int flags = iscapture ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT;
    78     const char *audiodev;
    79     char audiopath[1024];
    80 
    81     if (test == NULL)
    82         test = test_stub;
    83 
    84     /* Figure out what our audio device is */
    85     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    86         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    87         if (classic) {
    88             audiodev = _PATH_DEV_AUDIO;
    89         } else {
    90             struct stat sb;
    91 
    92             /* Added support for /dev/sound/\* in Linux 2.4 */
    93             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    94                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    95                     && S_ISCHR(sb.st_mode))) {
    96                 audiodev = _PATH_DEV_DSP24;
    97             } else {
    98                 audiodev = _PATH_DEV_DSP;
    99             }
   100         }
   101     }
   102     test_device(iscapture, audiodev, flags, test);
   103 
   104     if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
   105         int instance = 0;
   106         while (instance <= 64) {
   107             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   108                          "%s%d", audiodev, instance);
   109             instance++;
   110             test_device(iscapture, audiopath, flags, test);
   111         }
   112     }
   113 }
   114 
   115 void
   116 SDL_EnumUnixAudioDevices(const int classic, int (*test)(int))
   117 {
   118     SDL_EnumUnixAudioDevices_Internal(SDL_TRUE, classic, test);
   119     SDL_EnumUnixAudioDevices_Internal(SDL_FALSE, classic, test);
   120 }
   121 
   122 #endif /* Audio driver selection */
   123 
   124 /* vi: set ts=4 sw=4 expandtab: */