SDL + SDL_Mixer + Theora

For weeks I’ve been trying to write Theora playback code using SDL for video
and SDL_Mixer for audio. So far I can’t get proper sync between audio and
video, and/or stuttering audio.

Does anyone know of an example player that uses SDL and SDL_Mixer? The ones
I can find don’t use SDL_Mixer for audio, and my conversion to this mixer
seems to introduce further problems.

Thanks,
–Gabriel

Den Thu, 3 Sep 2009 15:43:36 -0300
skrev Gabriel Gambetta :

For weeks I’ve been trying to write Theora playback code using SDL
for video and SDL_Mixer for audio. So far I can’t get proper sync
between audio and video, and/or stuttering audio.

Don’t know about the stuttering, but I think the desync happens
because the timer of the sound card and the cpu aren’t exactly synced,
so if you time the audio with the cpu you get drifting. At least
that’s what it seems like.

Anyway, when I was faced with this problem some time ago (though this
wasn’t for playing video, and I used SDL audio directly, not SDL_mixer,
though you might be able to do something similar to this with effects),
I solved it by keeping track of the update rate of the audio callback
vs the logic update (decoupled from video update), and adjusting the
logic rate to temporarily get a little faster/slower if they went too
far apart. In my case I had the audio callback running at 44100 hz
(per sample =)), and the logic at 100 hz. A counter in the audio
callback counted how many samples had been played (well, buffered), and
the logic update had its own count of how many it expected to have
been played (so, increased by 441 each tick). Then you just have to
compare the counts and act accordingly when they drift too far apart.
I also did some averaging of the differences to counter the buffering of
the audio and smooth it out a little.

I can show you the code if you’re interested, it’s a bit messy though =P

  • Gerry

Try increasing your buffer size. It sounds like you are underproviding
audio data.

In general, however, you can’t depend on requests to fill an audio
buffer to synchronize your audio and video. Synchronizing can be quite
a pain in the ass when you have to allow for asymmetrical hiccups in
system performance that hold back audio and video playback
differently.

As I frequently do, I recommend checking out GStreamer if your
interests are not purely in gaining experience with SDL :)On Thu, Sep 3, 2009 at 2:43 PM, Gabriel Gambetta wrote:

For weeks I’ve been trying to write Theora playback code using SDL for video
and SDL_Mixer for audio. So far I can’t get proper sync between audio and
video, and/or stuttering audio.


http://codebad.com/

Thanks, Gerry, apparently this did the trick. I was calculating my audio
decode target based on elapsed milliseconds. Now I calculate the playback
time as “bytes requested by the mixer callback” and the audio skipping is
gone :slight_smile:

Thanks!
–GabrielOn Sun, Sep 6, 2009 at 3:06 PM, Gerry JJ wrote:

Den Thu, 3 Sep 2009 15:43:36 -0300
skrev Gabriel Gambetta :

For weeks I’ve been trying to write Theora playback code using SDL
for video and SDL_Mixer for audio. So far I can’t get proper sync
between audio and video, and/or stuttering audio.

Don’t know about the stuttering, but I think the desync happens
because the timer of the sound card and the cpu aren’t exactly synced,
so if you time the audio with the cpu you get drifting. At least
that’s what it seems like.

Anyway, when I was faced with this problem some time ago (though this
wasn’t for playing video, and I used SDL audio directly, not SDL_mixer,
though you might be able to do something similar to this with effects),
I solved it by keeping track of the update rate of the audio callback
vs the logic update (decoupled from video update), and adjusting the
logic rate to temporarily get a little faster/slower if they went too
far apart. In my case I had the audio callback running at 44100 hz
(per sample =)), and the logic at 100 hz. A counter in the audio
callback counted how many samples had been played (well, buffered), and
the logic update had its own count of how many it expected to have
been played (so, increased by 441 each tick). Then you just have to
compare the counts and act accordingly when they drift too far apart.
I also did some averaging of the differences to counter the buffering of
the audio and smooth it out a little.

I can show you the code if you’re interested, it’s a bit messy though =P

  • Gerry

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

This helps a bit, but didn’t solve the issue (changing the time calculation
did - see my other reply).

My main issue with the buffer is that increasing the buffer automatically
increases audio lag. At 44 KHz 16 bit stereo the mixer eats 176400 kb/sec -
a 0.1 sec delay would mean a ~17 kb buffer, right?

From an user’s point of view, is a 0.1 sec lag acceptable? I have no idea
what’s the threshold.

Thanks,

–GabrielOn Sun, Sep 6, 2009 at 3:31 PM, Donny Viszneki <donny.viszneki at gmail.com>wrote:

On Thu, Sep 3, 2009 at 2:43 PM, Gabriel Gambetta wrote:

For weeks I’ve been trying to write Theora playback code using SDL for
video
and SDL_Mixer for audio. So far I can’t get proper sync between audio and
video, and/or stuttering audio.

Try increasing your buffer size. It sounds like you are underproviding
audio data.

In general, however, you can’t depend on requests to fill an audio
buffer to synchronize your audio and video. Synchronizing can be quite
a pain in the ass when you have to allow for asymmetrical hiccups in
system performance that hold back audio and video playback
differently.

As I frequently do, I recommend checking out GStreamer if your
interests are not purely in gaining experience with SDL :slight_smile:


http://codebad.com/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org