netbsdaudio: Updated for SDL3 audio API.

This commit is contained in:
Ryan C. Gordon
2023-07-06 00:05:23 -04:00
parent 0f6e59312b
commit 6bc85577d7

View File

@@ -38,26 +38,26 @@
#include "../SDL_audiodev_c.h" #include "../SDL_audiodev_c.h"
#include "SDL_netbsdaudio.h" #include "SDL_netbsdaudio.h"
/* #define DEBUG_AUDIO */ //#define DEBUG_AUDIO
static void NETBSDAUDIO_DetectDevices(void) static void NETBSDAUDIO_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioDevice **default_capture)
{ {
SDL_EnumUnixAudioDevices(0, NULL); SDL_EnumUnixAudioDevices(SDL_FALSE, NULL);
} }
static void NETBSDAUDIO_Status(SDL_AudioDevice *_this) static void NETBSDAUDIO_Status(SDL_AudioDevice *device)
{ {
#ifdef DEBUG_AUDIO #ifdef DEBUG_AUDIO
/* *INDENT-OFF* */ /* clang-format off */ /* *INDENT-OFF* */ /* clang-format off */
audio_info_t info; audio_info_t info;
const struct audio_prinfo *prinfo; const struct audio_prinfo *prinfo;
if (ioctl(_this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) { if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
fprintf(stderr, "AUDIO_GETINFO failed.\n"); fprintf(stderr, "AUDIO_GETINFO failed.\n");
return; return;
} }
prinfo = _this->iscapture ? &info.record : &info.play; prinfo = device->iscapture ? &info.record : &info.play;
fprintf(stderr, "\n" fprintf(stderr, "\n"
"[%s info]\n" "[%s info]\n"
@@ -74,7 +74,7 @@ static void NETBSDAUDIO_Status(SDL_AudioDevice *_this)
"waiting : %s\n" "waiting : %s\n"
"active : %s\n" "active : %s\n"
"", "",
_this->iscapture ? "record" : "play", device->iscapture ? "record" : "play",
prinfo->buffer_size, prinfo->buffer_size,
prinfo->sample_rate, prinfo->sample_rate,
prinfo->channels, prinfo->channels,
@@ -100,7 +100,7 @@ static void NETBSDAUDIO_Status(SDL_AudioDevice *_this)
info.blocksize, info.blocksize,
info.hiwat, info.lowat, info.hiwat, info.lowat,
(info.mode == AUMODE_PLAY) ? "PLAY" (info.mode == AUMODE_PLAY) ? "PLAY"
: (info.mode = AUMODE_RECORD) ? "RECORD" : (info.mode == AUMODE_RECORD) ? "RECORD"
: (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL" : "?")); : (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL" : "?"));
fprintf(stderr, "\n" fprintf(stderr, "\n"
@@ -108,23 +108,46 @@ static void NETBSDAUDIO_Status(SDL_AudioDevice *_this)
"format : 0x%x\n" "format : 0x%x\n"
"size : %u\n" "size : %u\n"
"", "",
_this->spec.format, device->spec.format,
_this->spec.size); device->buffer_size);
/* *INDENT-ON* */ /* clang-format on */ /* *INDENT-ON* */ /* clang-format on */
#endif /* DEBUG_AUDIO */ #endif // DEBUG_AUDIO
} }
static void NETBSDAUDIO_PlayDevice(SDL_AudioDevice *_this) static void NETBSDAUDIO_WaitDevice(SDL_AudioDevice *device)
{ {
struct SDL_PrivateAudioData *h = _this->hidden; const SDL_bool iscapture = device->iscapture;
int written; while (!SDL_AtomicGet(&device->shutdown)) {
audio_info_t info;
const int rc = ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info);
if (rc < 0) {
if (errno == EAGAIN) {
continue;
}
// Hmm, not much we can do - abort
fprintf(stderr, "netbsdaudio WaitDevice ioctl failed (unrecoverable): %s\n", strerror(errno));
SDL_AudioDeviceDisconnected(device);
return;
}
const size_t remain = (size_t)((iscapture ? info.record.seek : info.play.seek) * (SDL_AUDIO_BITSIZE(device->spec.format) / 8));
if (!iscapture && (remain >= device->buffer_size)) {
SDL_Delay(10);
} else if (iscapture && (remain < device->buffer_size)) {
SDL_Delay(10);
} else {
break; /* ready to go! */
}
}
}
/* Write the audio data */ static void NETBSDAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buflen)
written = write(h->audio_fd, h->mixbuf, h->mixlen); {
struct SDL_PrivateAudioData *h = device->hidden;
const int written = write(h->audio_fd, buffer, buflen);
if (written == -1) { if (written == -1) {
/* Non recoverable error has occurred. It should be reported!!! */ // Non recoverable error has occurred. It should be reported!!!
SDL_OpenedAudioDeviceDisconnected(_this); SDL_AudioDeviceDisconnected(device);
perror("audio"); perror("audio");
return; return;
} }
@@ -134,19 +157,17 @@ static void NETBSDAUDIO_PlayDevice(SDL_AudioDevice *_this)
#endif #endif
} }
static Uint8 *NETBSDAUDIO_GetDeviceBuf(SDL_AudioDevice *_this) static Uint8 *NETBSDAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
{ {
return _this->hidden->mixbuf; return device->hidden->mixbuf;
} }
static int NETBSDAUDIO_CaptureFromDevice(SDL_AudioDevice *_this, void *_buffer, int buflen) static int NETBSDAUDIO_CaptureFromDevice(SDL_AudioDevice *device, void *vbuffer, int buflen)
{ {
Uint8 *buffer = (Uint8 *)_buffer; Uint8 *buffer = (Uint8 *)vbuffer;
int br; const int br = read(device->hidden->audio_fd, buffer, buflen);
br = read(_this->hidden->audio_fd, buffer, buflen);
if (br == -1) { if (br == -1) {
/* Non recoverable error has occurred. It should be reported!!! */ // Non recoverable error has occurred. It should be reported!!!
perror("audio"); perror("audio");
return -1; return -1;
} }
@@ -154,86 +175,72 @@ static int NETBSDAUDIO_CaptureFromDevice(SDL_AudioDevice *_this, void *_buffer,
#ifdef DEBUG_AUDIO #ifdef DEBUG_AUDIO
fprintf(stderr, "Captured %d bytes of audio data\n", br); fprintf(stderr, "Captured %d bytes of audio data\n", br);
#endif #endif
return 0; return br;
} }
static void NETBSDAUDIO_FlushCapture(SDL_AudioDevice *_this) static void NETBSDAUDIO_FlushCapture(SDL_AudioDevice *device)
{ {
struct SDL_PrivateAudioData *h = device->hidden;
audio_info_t info; audio_info_t info;
size_t remain; if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) == 0) {
Uint8 buf[512]; size_t remain = (size_t)(info.record.seek * (SDL_AUDIO_BITSIZE(device->spec.format) / 8));
while (remain > 0) {
if (ioctl(_this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) { char buf[512];
return; /* oh well. */ const size_t len = SDL_min(sizeof(buf), remain);
} const ssize_t br = read(h->audio_fd, buf, len);
if (br <= 0) {
remain = (size_t)(info.record.samples * (SDL_AUDIO_BITSIZE(_this->spec.format) / 8)); break;
while (remain > 0) { }
const size_t len = SDL_min(sizeof(buf), remain); remain -= br;
const int br = read(_this->hidden->audio_fd, buf, len);
if (br <= 0) {
return; /* oh well. */
} }
remain -= br;
} }
} }
static void NETBSDAUDIO_CloseDevice(SDL_AudioDevice *_this) static void NETBSDAUDIO_CloseDevice(SDL_AudioDevice *device)
{ {
if (_this->hidden->audio_fd >= 0) { if (device->hidden) {
close(_this->hidden->audio_fd); if (device->hidden->audio_fd >= 0) {
close(device->hidden->audio_fd);
}
SDL_free(device->hidden->mixbuf);
SDL_free(device->hidden);
} }
SDL_free(_this->hidden->mixbuf);
SDL_free(_this->hidden);
} }
static int NETBSDAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname) static int NETBSDAUDIO_OpenDevice(SDL_AudioDevice *device)
{ {
SDL_bool iscapture = _this->iscapture; const SDL_bool iscapture = device->iscapture;
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts;
int encoding = AUDIO_ENCODING_NONE; int encoding = AUDIO_ENCODING_NONE;
audio_info_t info, hwinfo; audio_info_t info, hwinfo;
struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play; struct audio_prinfo *prinfo = iscapture ? &info.record : &info.play;
/* We don't care what the devname is...we'll try to open anything. */ // Initialize all variables that we clean on shutdown
/* ...but default to first name in the list... */ device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden));
if (devname == NULL) { if (device->hidden == NULL) {
devname = SDL_GetAudioDeviceName(0, iscapture);
if (devname == NULL) {
return SDL_SetError("No such audio device");
}
}
/* Initialize all variables that we clean on shutdown */
_this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*_this->hidden));
if (_this->hidden == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
SDL_zerop(_this->hidden);
/* Open the audio device */ // Open the audio device; we hardcode the device path in `device->name` for lack of better info, so use that.
_this->hidden->audio_fd = open(devname, (iscapture ? O_RDONLY : O_WRONLY) | O_CLOEXEC); const int flags = ((device->iscapture) ? O_RDONLY : O_WRONLY);
if (_this->hidden->audio_fd < 0) { device->hidden->audio_fd = open(device->name, flags | O_CLOEXEC);
return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno)); if (device->hidden->audio_fd < 0) {
return SDL_SetError("Couldn't open %s: %s", device->name, strerror(errno));
} }
AUDIO_INITINFO(&info); AUDIO_INITINFO(&info);
#ifdef AUDIO_GETFORMAT /* Introduced in NetBSD 9.0 */ #ifdef AUDIO_GETFORMAT // Introduced in NetBSD 9.0
if (ioctl(_this->hidden->audio_fd, AUDIO_GETFORMAT, &hwinfo) != -1) { if (ioctl(device->hidden->audio_fd, AUDIO_GETFORMAT, &hwinfo) != -1) {
/* // Use the device's native sample rate so the kernel doesn't have to resample.
* Use the device's native sample rate so the kernel doesn't have to device->spec.freq = iscapture ? hwinfo.record.sample_rate : hwinfo.play.sample_rate;
* resample.
*/
_this->spec.freq = iscapture ? hwinfo.record.sample_rate : hwinfo.play.sample_rate;
} }
#endif #endif
prinfo->sample_rate = _this->spec.freq; prinfo->sample_rate = device->spec.freq;
prinfo->channels = _this->spec.channels; prinfo->channels = device->spec.channels;
closefmts = SDL_ClosestAudioFormats(_this->spec.format); SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format);
while ((test_format = *(closefmts++)) != 0) { while ((test_format = *(closefmts++)) != 0) {
switch (test_format) { switch (test_format) {
case SDL_AUDIO_U8: case SDL_AUDIO_U8:
@@ -268,56 +275,56 @@ static int NETBSDAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
info.hiwat = 5; info.hiwat = 5;
info.lowat = 3; info.lowat = 3;
if (ioctl(_this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) { if (ioctl(device->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
return SDL_SetError("AUDIO_SETINFO failed for %s: %s", devname, strerror(errno)); return SDL_SetError("AUDIO_SETINFO failed for %s: %s", device->name, strerror(errno));
} }
if (ioctl(_this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) { if (ioctl(device->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
return SDL_SetError("AUDIO_GETINFO failed for %s: %s", devname, strerror(errno)); return SDL_SetError("AUDIO_GETINFO failed for %s: %s", device->name, strerror(errno));
} }
/* Final spec used for the device. */ // Final spec used for the device.
_this->spec.format = test_format; device->spec.format = test_format;
_this->spec.freq = prinfo->sample_rate; device->spec.freq = prinfo->sample_rate;
_this->spec.channels = prinfo->channels; device->spec.channels = prinfo->channels;
SDL_CalculateAudioSpec(&_this->spec); SDL_UpdatedAudioDeviceFormat(device);
if (!iscapture) { if (!iscapture) {
/* Allocate mixing buffer */ // Allocate mixing buffer
_this->hidden->mixlen = _this->spec.size; device->hidden->mixlen = device->buffer_size;
_this->hidden->mixbuf = (Uint8 *)SDL_malloc(_this->hidden->mixlen); device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->hidden->mixlen);
if (_this->hidden->mixbuf == NULL) { if (device->hidden->mixbuf == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
SDL_memset(_this->hidden->mixbuf, _this->spec.silence, _this->spec.size); SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
} }
NETBSDAUDIO_Status(_this); NETBSDAUDIO_Status(device);
/* We're ready to rock and roll. :-) */ return 0; // We're ready to rock and roll. :-)
return 0;
} }
static SDL_bool NETBSDAUDIO_Init(SDL_AudioDriverImpl *impl) static SDL_bool NETBSDAUDIO_Init(SDL_AudioDriverImpl *impl)
{ {
/* Set the function pointers */
impl->DetectDevices = NETBSDAUDIO_DetectDevices; impl->DetectDevices = NETBSDAUDIO_DetectDevices;
impl->OpenDevice = NETBSDAUDIO_OpenDevice; impl->OpenDevice = NETBSDAUDIO_OpenDevice;
impl->WaitDevice = NETBSDAUDIO_WaitDevice;
impl->PlayDevice = NETBSDAUDIO_PlayDevice; impl->PlayDevice = NETBSDAUDIO_PlayDevice;
impl->GetDeviceBuf = NETBSDAUDIO_GetDeviceBuf; impl->GetDeviceBuf = NETBSDAUDIO_GetDeviceBuf;
impl->CloseDevice = NETBSDAUDIO_CloseDevice; impl->CloseDevice = NETBSDAUDIO_CloseDevice;
impl->WaitCaptureDevice = NETBSDAUDIO_WaitDevice;
impl->CaptureFromDevice = NETBSDAUDIO_CaptureFromDevice; impl->CaptureFromDevice = NETBSDAUDIO_CaptureFromDevice;
impl->FlushCapture = NETBSDAUDIO_FlushCapture; impl->FlushCapture = NETBSDAUDIO_FlushCapture;
impl->HasCaptureSupport = SDL_TRUE; impl->HasCaptureSupport = SDL_TRUE;
impl->AllowsArbitraryDeviceNames = SDL_TRUE; impl->AllowsArbitraryDeviceNames = SDL_TRUE;
return SDL_TRUE; /* this audio target is available. */ return SDL_TRUE;
} }
AudioBootStrap NETBSDAUDIO_bootstrap = { AudioBootStrap NETBSDAUDIO_bootstrap = {
"netbsd", "NetBSD audio", NETBSDAUDIO_Init, SDL_FALSE "netbsd", "NetBSD audio", NETBSDAUDIO_Init, SDL_FALSE
}; };
#endif /* SDL_AUDIO_DRIVER_NETBSD */ #endif // SDL_AUDIO_DRIVER_NETBSD