audio: Fixed some "is_*" variables to be cleaner and/or more specific.

Requested at https://github.com/libsdl-org/SDL/pull/8165#discussion_r1306700881_
This commit is contained in:
Ryan C. Gordon 2023-08-29 10:46:14 -04:00
parent 2471d8cc2a
commit 3699b12ed0
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
3 changed files with 30 additions and 30 deletions

View File

@ -336,7 +336,7 @@ void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device)
SDL_LogicalAudioDevice *next = NULL; SDL_LogicalAudioDevice *next = NULL;
for (SDL_LogicalAudioDevice *logdev = device->logical_devices; logdev != NULL; logdev = next) { for (SDL_LogicalAudioDevice *logdev = device->logical_devices; logdev != NULL; logdev = next) {
next = logdev->next; next = logdev->next;
if (!logdev->is_default) { // if opened as a default, leave it on the zombie device for later migration. if (!logdev->opened_as_default) { // if opened as a default, leave it on the zombie device for later migration.
DisconnectLogicalAudioDevice(logdev); DisconnectLogicalAudioDevice(logdev);
} }
} }
@ -1043,16 +1043,16 @@ int SDL_GetAudioDeviceFormat(SDL_AudioDeviceID devid, SDL_AudioSpec *spec)
return SDL_InvalidParamError("spec"); return SDL_InvalidParamError("spec");
} }
SDL_bool is_default = SDL_FALSE; SDL_bool wants_default = SDL_FALSE;
if (devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) { if (devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
devid = current_audio.default_output_device_id; devid = current_audio.default_output_device_id;
is_default = SDL_TRUE; wants_default = SDL_TRUE;
} else if (devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) { } else if (devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
devid = current_audio.default_capture_device_id; devid = current_audio.default_capture_device_id;
is_default = SDL_TRUE; wants_default = SDL_TRUE;
} }
if ((devid == 0) && is_default) { if ((devid == 0) && wants_default) {
return SDL_SetError("No default audio device available"); return SDL_SetError("No default audio device available");
} }
@ -1081,9 +1081,9 @@ static void ClosePhysicalAudioDevice(SDL_AudioDevice *device)
SDL_AtomicSet(&device->thread_alive, 0); SDL_AtomicSet(&device->thread_alive, 0);
} }
if (device->is_opened) { if (device->currently_opened) {
current_audio.impl.CloseDevice(device); // if ProvidesOwnCallbackThread, this must join on any existing device thread before returning! current_audio.impl.CloseDevice(device); // if ProvidesOwnCallbackThread, this must join on any existing device thread before returning!
device->is_opened = SDL_FALSE; device->currently_opened = SDL_FALSE;
device->hidden = NULL; // just in case. device->hidden = NULL; // just in case.
} }
@ -1190,7 +1190,7 @@ char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen)
// this expects the device lock to be held. // this expects the device lock to be held.
static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec *inspec) static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec *inspec)
{ {
SDL_assert(!device->is_opened); SDL_assert(!device->currently_opened);
SDL_assert(device->logical_devices == NULL); SDL_assert(device->logical_devices == NULL);
// Just pretend to open a zombie device. It can still collect logical devices on the assumption they will all migrate when the default device is officially changed. // Just pretend to open a zombie device. It can still collect logical devices on the assumption they will all migrate when the default device is officially changed.
@ -1212,7 +1212,7 @@ static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
device->sample_frames = GetDefaultSampleFramesFromFreq(device->spec.freq); device->sample_frames = GetDefaultSampleFramesFromFreq(device->spec.freq);
SDL_UpdatedAudioDeviceFormat(device); // start this off sane. SDL_UpdatedAudioDeviceFormat(device); // start this off sane.
device->is_opened = SDL_TRUE; // mark this true even if impl.OpenDevice fails, so we know to clean up. device->currently_opened = SDL_TRUE; // mark this true even if impl.OpenDevice fails, so we know to clean up.
if (current_audio.impl.OpenDevice(device) < 0) { if (current_audio.impl.OpenDevice(device) < 0) {
ClosePhysicalAudioDevice(device); // clean up anything the backend left half-initialized. ClosePhysicalAudioDevice(device); // clean up anything the backend left half-initialized.
return -1; return -1;
@ -1252,16 +1252,16 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
return 0; return 0;
} }
SDL_bool is_default = SDL_FALSE; SDL_bool wants_default = SDL_FALSE;
if (devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) { if (devid == SDL_AUDIO_DEVICE_DEFAULT_OUTPUT) {
devid = current_audio.default_output_device_id; devid = current_audio.default_output_device_id;
is_default = SDL_TRUE; wants_default = SDL_TRUE;
} else if (devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) { } else if (devid == SDL_AUDIO_DEVICE_DEFAULT_CAPTURE) {
devid = current_audio.default_capture_device_id; devid = current_audio.default_capture_device_id;
is_default = SDL_TRUE; wants_default = SDL_TRUE;
} }
if ((devid == 0) && is_default) { if ((devid == 0) && wants_default) {
SDL_SetError("No default audio device available"); SDL_SetError("No default audio device available");
return 0; return 0;
} }
@ -1274,7 +1274,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
} else { } else {
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid); // this locks the physical device, too. SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid); // this locks the physical device, too.
if (logdev) { if (logdev) {
is_default = logdev->is_default; // was the original logical device meant to be a default? Make this one, too. wants_default = logdev->opened_as_default; // was the original logical device meant to be a default? Make this one, too.
device = logdev->physical_device; device = logdev->physical_device;
} }
} }
@ -1283,18 +1283,18 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
if (device) { if (device) {
SDL_LogicalAudioDevice *logdev = NULL; SDL_LogicalAudioDevice *logdev = NULL;
if (!is_default && SDL_AtomicGet(&device->zombie)) { if (!wants_default && SDL_AtomicGet(&device->zombie)) {
// uhoh, this device is undead, and just waiting for a new default device to be declared so it can hand off to it. Refuse explicit opens. // uhoh, this device is undead, and just waiting for a new default device to be declared so it can hand off to it. Refuse explicit opens.
SDL_SetError("Device was already lost and can't accept new opens"); SDL_SetError("Device was already lost and can't accept new opens");
} else if ((logdev = (SDL_LogicalAudioDevice *) SDL_calloc(1, sizeof (SDL_LogicalAudioDevice))) == NULL) { } else if ((logdev = (SDL_LogicalAudioDevice *) SDL_calloc(1, sizeof (SDL_LogicalAudioDevice))) == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
} else if (!device->is_opened && OpenPhysicalAudioDevice(device, spec) == -1) { // first thing using this physical device? Open at the OS level... } else if (!device->currently_opened && OpenPhysicalAudioDevice(device, spec) == -1) { // first thing using this physical device? Open at the OS level...
SDL_free(logdev); SDL_free(logdev);
} else { } else {
SDL_AtomicSet(&logdev->paused, 0); SDL_AtomicSet(&logdev->paused, 0);
retval = logdev->instance_id = assign_audio_device_instance_id(device->iscapture, /*islogical=*/SDL_TRUE); retval = logdev->instance_id = assign_audio_device_instance_id(device->iscapture, /*islogical=*/SDL_TRUE);
logdev->physical_device = device; logdev->physical_device = device;
logdev->is_default = is_default; logdev->opened_as_default = wants_default;
logdev->next = device->logical_devices; logdev->next = device->logical_devices;
if (device->logical_devices) { if (device->logical_devices) {
device->logical_devices->prev = logdev; device->logical_devices->prev = logdev;
@ -1357,7 +1357,7 @@ int SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream **streams, int
return SDL_SetError("Audio streams are bound to device ids from SDL_OpenAudioDevice, not raw physical devices"); return SDL_SetError("Audio streams are bound to device ids from SDL_OpenAudioDevice, not raw physical devices");
} else if ((logdev = ObtainLogicalAudioDevice(devid)) == NULL) { } else if ((logdev = ObtainLogicalAudioDevice(devid)) == NULL) {
return -1; // ObtainLogicalAudioDevice set the error message. return -1; // ObtainLogicalAudioDevice set the error message.
} else if (logdev->is_simplified) { } else if (logdev->simplified) {
SDL_UnlockMutex(logdev->physical_device->lock); SDL_UnlockMutex(logdev->physical_device->lock);
return SDL_SetError("Cannot change stream bindings on device opened with SDL_OpenAudioDeviceStream"); return SDL_SetError("Cannot change stream bindings on device opened with SDL_OpenAudioDeviceStream");
} }
@ -1464,7 +1464,7 @@ void SDL_UnbindAudioStreams(SDL_AudioStream **streams, int num_streams)
for (int i = 0; i < num_streams; i++) { for (int i = 0; i < num_streams; i++) {
SDL_AudioStream *stream = streams[i]; SDL_AudioStream *stream = streams[i];
// don't allow unbinding from "simplified" devices (opened with SDL_OpenAudioDeviceStream). Just ignore them. // don't allow unbinding from "simplified" devices (opened with SDL_OpenAudioDeviceStream). Just ignore them.
if (stream && stream->bound_device && !stream->bound_device->is_simplified) { if (stream && stream->bound_device && !stream->bound_device->simplified) {
if (stream->bound_device->bound_streams == stream) { if (stream->bound_device->bound_streams == stream) {
SDL_assert(stream->prev_binding == NULL); SDL_assert(stream->prev_binding == NULL);
stream->bound_device->bound_streams = stream->next_binding; stream->bound_device->bound_streams = stream->next_binding;
@ -1546,8 +1546,8 @@ SDL_AudioStream *SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_Au
stream = NULL; stream = NULL;
} }
logdev->is_simplified = SDL_TRUE; // forbid further binding changes on this logical device. logdev->simplified = SDL_TRUE; // forbid further binding changes on this logical device.
stream->is_simplified = SDL_TRUE; // so we know to close the audio device when this is destroyed. stream->simplified = SDL_TRUE; // so we know to close the audio device when this is destroyed.
if (callback) { if (callback) {
int rc; int rc;
@ -1625,7 +1625,7 @@ void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device)
SDL_bool needs_migration = SDL_FALSE; SDL_bool needs_migration = SDL_FALSE;
SDL_zero(spec); SDL_zero(spec);
for (SDL_LogicalAudioDevice *logdev = current_default_device->logical_devices; logdev != NULL; logdev = logdev->next) { for (SDL_LogicalAudioDevice *logdev = current_default_device->logical_devices; logdev != NULL; logdev = logdev->next) {
if (logdev->is_default) { if (logdev->opened_as_default) {
needs_migration = SDL_TRUE; needs_migration = SDL_TRUE;
for (SDL_AudioStream *stream = logdev->bound_streams; stream != NULL; stream = stream->next_binding) { for (SDL_AudioStream *stream = logdev->bound_streams; stream != NULL; stream = stream->next_binding) {
const SDL_AudioSpec *streamspec = iscapture ? &stream->dst_spec : &stream->src_spec; const SDL_AudioSpec *streamspec = iscapture ? &stream->dst_spec : &stream->src_spec;
@ -1655,7 +1655,7 @@ void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device)
for (SDL_LogicalAudioDevice *logdev = current_default_device->logical_devices; logdev != NULL; logdev = next) { for (SDL_LogicalAudioDevice *logdev = current_default_device->logical_devices; logdev != NULL; logdev = next) {
next = logdev->next; next = logdev->next;
if (!logdev->is_default) { if (!logdev->opened_as_default) {
continue; // not opened as a default, leave it on the current physical device. continue; // not opened as a default, leave it on the current physical device.
} }

View File

@ -1281,9 +1281,9 @@ int SDL_ClearAudioStream(SDL_AudioStream *stream)
void SDL_DestroyAudioStream(SDL_AudioStream *stream) void SDL_DestroyAudioStream(SDL_AudioStream *stream)
{ {
if (stream) { if (stream) {
const SDL_bool is_simplified = stream->is_simplified; const SDL_bool simplified = stream->simplified;
if (is_simplified) { if (simplified) {
SDL_assert(stream->bound_device->is_simplified); SDL_assert(stream->bound_device->simplified);
SDL_CloseAudioDevice(stream->bound_device->instance_id); // this will unbind the stream. SDL_CloseAudioDevice(stream->bound_device->instance_id); // this will unbind the stream.
} else { } else {
SDL_UnbindAudioStream(stream); SDL_UnbindAudioStream(stream);

View File

@ -187,7 +187,7 @@ struct SDL_AudioStream
int packetlen; int packetlen;
SDL_bool is_simplified; // SDL_TRUE if created via SDL_OpenAudioDeviceStream SDL_bool simplified; // SDL_TRUE if created via SDL_OpenAudioDeviceStream
SDL_LogicalAudioDevice *bound_device; SDL_LogicalAudioDevice *bound_device;
SDL_AudioStream *next_binding; SDL_AudioStream *next_binding;
@ -213,10 +213,10 @@ struct SDL_LogicalAudioDevice
SDL_AudioStream *bound_streams; SDL_AudioStream *bound_streams;
// SDL_TRUE if this was opened as a default device. // SDL_TRUE if this was opened as a default device.
SDL_bool is_default; SDL_bool opened_as_default;
// SDL_TRUE if device was opened with SDL_OpenAudioDeviceStream (so it forbids binding changes, etc). // SDL_TRUE if device was opened with SDL_OpenAudioDeviceStream (so it forbids binding changes, etc).
SDL_bool is_simplified; SDL_bool simplified;
// double-linked list of opened devices on the same physical device. // double-linked list of opened devices on the same physical device.
SDL_LogicalAudioDevice *next; SDL_LogicalAudioDevice *next;
@ -272,7 +272,7 @@ struct SDL_AudioDevice
SDL_Thread *thread; SDL_Thread *thread;
// SDL_TRUE if this physical device is currently opened by the backend. // SDL_TRUE if this physical device is currently opened by the backend.
SDL_bool is_opened; SDL_bool currently_opened;
// Data private to this driver // Data private to this driver
struct SDL_PrivateAudioData *hidden; struct SDL_PrivateAudioData *hidden;