|
| 1 | +#include "sound.h" |
| 2 | +#include "common/io/io.h" |
| 3 | + |
| 4 | +#include <fcntl.h> |
| 5 | +#include <stdint.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/audioio.h> |
| 8 | +#include <sys/ioctl.h> |
| 9 | + |
| 10 | +const char* ffDetectSound(FFlist* devices) |
| 11 | +{ |
| 12 | + int defaultDev; |
| 13 | + { |
| 14 | + char audiop[12]; |
| 15 | + ssize_t plen = readlink("/dev/audio", audiop, ARRAY_SIZE(audiop)); |
| 16 | + if (plen < (ssize_t) strlen("audioN")) |
| 17 | + return "readlink(/dev/audio) failed"; |
| 18 | + defaultDev = audiop[plen - 1] - '0'; |
| 19 | + if (defaultDev < 0 || defaultDev > 9) |
| 20 | + return "Invalid audio device"; |
| 21 | + } |
| 22 | + |
| 23 | + char path[] = "/dev/audio0"; |
| 24 | + |
| 25 | + for (int idev = 0; idev < 9; ++idev) |
| 26 | + { |
| 27 | + path[strlen("/dev/audio")] = (char) ('0' + idev); |
| 28 | + FF_AUTO_CLOSE_FD int fd = open(path, O_RDWR); |
| 29 | + if (fd < 0) break; |
| 30 | + |
| 31 | + audio_device_t ad; |
| 32 | + if (ioctl(fd, AUDIO_GETDEV, &ad) < 0) |
| 33 | + continue; |
| 34 | + |
| 35 | + audio_info_t ai; |
| 36 | + if (ioctl(fd, AUDIO_GETINFO, &ai) < 0) |
| 37 | + continue; |
| 38 | + |
| 39 | + FFSoundDevice* device = ffListAdd(devices); |
| 40 | + ffStrbufInitS(&device->identifier, path); |
| 41 | + ffStrbufInitS(&device->name, ad.name); |
| 42 | + ffStrbufTrimRightSpace(&device->name); |
| 43 | + ffStrbufInitF(&device->platformApi, "%s", "SunAudio"); |
| 44 | + device->volume = (uint8_t) (ai.play.gain * 100 / AUDIO_MAX_GAIN); |
| 45 | + device->active = true; |
| 46 | + device->main = defaultDev == idev; |
| 47 | + } |
| 48 | + |
| 49 | + return NULL; |
| 50 | +} |
0 commit comments