audio: Allow audio streams to be created when the subsystem isn't initialized.

You are on your own to destroy them, though!
This commit is contained in:
Ryan C. Gordon
2023-10-11 22:42:52 -04:00
parent 1ae33f6751
commit 2bca4671a6

View File

@@ -139,9 +139,12 @@ static int GetDefaultSampleFramesFromFreq(const int freq)
void OnAudioStreamCreated(SDL_AudioStream *stream) void OnAudioStreamCreated(SDL_AudioStream *stream)
{ {
SDL_assert(SDL_GetCurrentAudioDriver() != NULL);
SDL_assert(stream != NULL); SDL_assert(stream != NULL);
// NOTE that you can create an audio stream without initializing the audio subsystem,
// but it will not be automatically destroyed during a later call to SDL_Quit!
// You must explicitly destroy it yourself!
if (current_audio.device_list_lock) {
// this isn't really part of the "device list" but it's a convenient lock to use here. // this isn't really part of the "device list" but it's a convenient lock to use here.
SDL_LockRWLockForWriting(current_audio.device_list_lock); SDL_LockRWLockForWriting(current_audio.device_list_lock);
if (current_audio.existing_streams) { if (current_audio.existing_streams) {
@@ -152,12 +155,16 @@ void OnAudioStreamCreated(SDL_AudioStream *stream)
current_audio.existing_streams = stream; current_audio.existing_streams = stream;
SDL_UnlockRWLock(current_audio.device_list_lock); SDL_UnlockRWLock(current_audio.device_list_lock);
} }
}
void OnAudioStreamDestroy(SDL_AudioStream *stream) void OnAudioStreamDestroy(SDL_AudioStream *stream)
{ {
SDL_assert(SDL_GetCurrentAudioDriver() != NULL);
SDL_assert(stream != NULL); SDL_assert(stream != NULL);
// NOTE that you can create an audio stream without initializing the audio subsystem,
// but it will not be automatically destroyed during a later call to SDL_Quit!
// You must explicitly destroy it yourself!
if (current_audio.device_list_lock) {
// this isn't really part of the "device list" but it's a convenient lock to use here. // this isn't really part of the "device list" but it's a convenient lock to use here.
SDL_LockRWLockForWriting(current_audio.device_list_lock); SDL_LockRWLockForWriting(current_audio.device_list_lock);
if (stream->prev) { if (stream->prev) {
@@ -171,7 +178,7 @@ void OnAudioStreamDestroy(SDL_AudioStream *stream)
} }
SDL_UnlockRWLock(current_audio.device_list_lock); SDL_UnlockRWLock(current_audio.device_list_lock);
} }
}
// device should be locked when calling this. // device should be locked when calling this.
static SDL_bool AudioDeviceCanUseSimpleCopy(SDL_AudioDevice *device) static SDL_bool AudioDeviceCanUseSimpleCopy(SDL_AudioDevice *device)