We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3710

Summary: SDL_OpenAudio(desired, obtained) doesn't update desired's size when obtained is NULL
Product: SDL Reporter: David Ludwig <dll>
Component: audioAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: dll, ny00
Version: HG 2.0Keywords: target-2.0.6
Hardware: All   
OS: Other   
Attachments: fix #1 (rejected) for SDL Bug 3710
fix #2,A, adjust both 'size' and 'silence'
fix #2,B, adjust only 'size'

Description David Ludwig 2017-07-23 19:01:48 UTC
The SDL wiki's documentation for SDL_OpenAudio(desired, obtained) states that, if and when 'obtained' is NULL, that 'desired' will be modified, as appropriate:

Via: https://wiki.libsdl.org/SDL_OpenAudio
alternatively: https://web.archive.org/web/20161112172633/https://wiki.libsdl.org/SDL_OpenAudio

"If obtained is NULL, the audio data passed to the callback function will be guaranteed to be in the requested format, and will be automatically converted to the actual hardware audio format if necessary. If obtained is NULL, desired will have fields modified."

SDL_OpenAudio, via both SDL 2.0.5, and SDL2 as of https://hg.libsdl.org/SDL/rev/4094632213a2, does not update the 'desired' structure's 'size' parameter, if and when 'obtained' is NULL.

This bug manifested via an unofficial port of Basilisk II to SDL2 (via https://github.com/davidludwig/macemu), and was reported by Clang's Address Sanitizer in Xcode 8.3.3.
Comment 1 David Ludwig 2017-07-23 19:04:28 UTC
Created attachment 2807 [details]
fix #1 (rejected) for SDL Bug 3710

This change modifies SDL_OpenAudio(desired, obtained), if and when called with 'obtained == NULL', to record the SDL_AudioSpec changes to a temporary buffer, then, on successful creation of an audio device, copy those values back into 'desired'.
Comment 2 Ryan C. Gordon 2017-08-09 05:25:35 UTC
(Sorry if you get a lot of copies of this email, we're touching dozens of bug reports right now.)

Tagging a bunch of bugs as target-2.0.6.

This means we're in the final stretch for an official SDL 2.0.6 release! These are the bugs we really want to fix before shipping if humanly possible.

That being said, we don't promise to fix them because of this tag, we just want to make sure we don't forget to deal with them before we bless a final 2.0.6 release, and generally be organized about what we're aiming to ship. After some debate, we might just remove this tag again and deal with it for a later release.

Hopefully you'll hear more about this bug soon. If you have more information (including "this got fixed at some point, nevermind"), we would love to have you come add more information to the bug report when you have a moment.

Thanks!
--ryan.
Comment 3 Sam Lantinga 2017-08-11 17:14:21 UTC
This is the wrong fix. The actual behavior should be that the application should get called back with exactly what it requested and SDL should convert it to the correct format for output.
Comment 4 ny00 2017-08-13 20:19:45 UTC
*** Bug 3749 has been marked as a duplicate of this bug. ***
Comment 5 David Ludwig 2017-08-21 22:10:18 UTC
If and when 'obtained' is NULL, should SDL modify the 'desired' field's 'size' parameter?  For example:

SDL_AudioSpec want;
SDL_memset(&want, 0, sizeof(want));
want.freq = 48000;
want.format = AUDIO_F32;
want.channels = 2;
want.samples = 4096;
want.callback = MyAudioCallback;
if (SDL_OpenAudio(&want, NULL) != 0) {
    exit(1);
}

//
// Assuming we get to this point in the code, should 'want.size' be zero, or non-zero?
// Currently, it'll be zero.
//
Comment 6 Sam Lantinga 2017-08-21 23:28:14 UTC
size is the only field that should be written, and it should be calculated from the other fields in the desired audio spec.
Comment 7 David Ludwig 2017-08-22 13:09:57 UTC
I've created a new set of patches.  I am happy to create more, if it would help.

One version only copies 'size'.

A second version copies both 'size' and 'silence'.  When looking over the documentation for SDL_OpenAudio in SDL_audio.h, it mentioned that both 'size' and 'silence' were things that SDL_OpenAudio would calculate.


Regarding *both* patches, I did notice that SDL 1.2 appears to have always modified desired's size and silence fields.  The SDL wiki, at https://wiki.libsdl.org/SDL_OpenAudio#Remarks , does note:

"This function remains for compatibility with SDL 1.2, but also because it's slightly easier to use than the new functions in SDL 2.0."

Should SDL_OpenAudio, in SDL 2.x, always write size and/or silence field(s) in 'desired', regardless of whether 'obtained' is passed in?
Comment 8 David Ludwig 2017-08-22 13:12:15 UTC
Created attachment 2874 [details]
fix #2,A, adjust both 'size' and 'silence'
Comment 9 David Ludwig 2017-08-22 13:12:50 UTC
Created attachment 2875 [details]
fix #2,B, adjust only 'size'
Comment 10 David Ludwig 2017-08-22 13:20:38 UTC
For reference, here is a link to SDL_audio.c on SDL 1.2.x, which contains its implementation of SDL_OpenAudio: https://hg.libsdl.org/SDL/file/SDL-1.2/src/audio/SDL_audio.c
Comment 11 Sam Lantinga 2017-08-28 02:10:55 UTC
Fix #2,A is in, thanks!
https://hg.libsdl.org/SDL/rev/ec27c4fd6880