Skip to content

Commit 77f334d

Browse files
sdlout: Add support for SDL3 and require SDL2
See also: https://wiki.libsdl.org/SDL3/README/migration
1 parent 6fcde6b commit 77f334d

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed

.github/actions/install-dependencies/install-dependencies.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
# --- Dependency configuration ---
44
#
5-
# ubuntu-20.04: Qt 5 + GTK 2
65
# ubuntu-22.04: Qt 5 + GTK 3
76
# ubuntu-24.04: Qt 6 + GTK 3
87
# Windows: Qt 6 + GTK 2
@@ -32,7 +31,7 @@ ubuntu_qt5_packages='libqt5opengl5-dev libqt5svg5-dev libqt5x11extras5-dev
3231
ubuntu_qt6_packages='qt6-base-dev qt6-multimedia-dev qt6-svg-dev'
3332

3433
macos_packages='adplug faad2 ffmpeg libbs2b libcue libmms libmodplug libnotify
35-
libopenmpt libsamplerate libsoxr neon opusfile sdl2 wavpack'
34+
libopenmpt libsamplerate libsoxr neon opusfile sdl3 wavpack'
3635

3736
case "$os" in
3837
ubuntu-22.04)

configure.ac

+12-6
Original file line numberDiff line numberDiff line change
@@ -622,17 +622,23 @@ dnl SDL Output
622622
dnl ==========
623623

624624
AC_ARG_WITH(libsdl,
625-
AS_HELP_STRING([--with-libsdl=1,2], [select which SDL version to use (default=auto)]),
625+
AS_HELP_STRING([--with-libsdl=2,3], [select which SDL version to use (default=auto)]),
626626
[with_libsdl=$withval], [with_libsdl=auto])
627627

628628
test_sdlout () {
629-
if test "x$with_libsdl" = "x1"; then
630-
PKG_CHECK_MODULES(SDL, sdl >= 1.2.11, have_sdlout=yes, have_sdlout=no)
631-
elif test "x$with_libsdl" = "x2"; then
629+
if test "x$with_libsdl" = "x2"; then
632630
PKG_CHECK_MODULES(SDL, sdl2 >= 2.0, have_sdlout=yes, have_sdlout=no)
631+
elif test "x$with_libsdl" = "x3"; then
632+
PKG_CHECK_MODULES(SDL, sdl3 >= 3.2.0, [
633+
AC_DEFINE(HAVE_LIBSDL3, 1, [Define if libsdl version is >= 3.2.0])
634+
have_sdlout=yes
635+
], [have_sdlout=no])
633636
else
634-
PKG_CHECK_MODULES(SDL, sdl2 >= 2.0, have_sdlout=yes, [
635-
PKG_CHECK_MODULES(SDL, sdl >= 1.2.11, have_sdlout=yes, have_sdlout=no)
637+
PKG_CHECK_MODULES(SDL, sdl3 >= 3.2.0, [
638+
AC_DEFINE(HAVE_LIBSDL3, 1, [Define if libsdl version is >= 3.2.0])
639+
have_sdlout=yes
640+
], [
641+
PKG_CHECK_MODULES(SDL, sdl2 >= 2.0, have_sdlout=yes, have_sdlout=no)
636642
])
637643
fi
638644
}

src/config.h.meson

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#mesondefine HAVE_LIBCDDB
3232
#mesondefine HAVE_LIBCUE2
33+
#mesondefine HAVE_LIBSDL3
3334
#mesondefine HAVE_SNDIO_1_9
3435

3536
#mesondefine HAVE_ADPLUG_NEMUOPL_H

src/sdlout/meson.build

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
sdl_dep = dependency('sdl2', version: '>= 2.0', required: false)
1+
sdl_dep = dependency('sdl3', version: '>= 3.2.0', required: false)
22

33
if not sdl_dep.found()
4-
sdl_dep = dependency('sdl', version: '>= 1.2.11', required: false)
4+
sdl_dep = dependency('sdl2', version: '>= 2.0', required: false)
55
endif
66

7-
have_sdlout = sdl_dep.found()
7+
if sdl_dep.version().version_compare('>= 3.2.0')
8+
conf.set10('HAVE_LIBSDL3', true)
9+
endif
810

11+
have_sdlout = sdl_dep.found()
912

1013
if have_sdlout
1114
shared_module('sdlout',

src/sdlout/sdlout.cc

+62-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
#include <string.h>
2323
#include <sys/time.h>
2424

25+
#if HAVE_LIBSDL3
26+
#include <SDL3/SDL.h>
27+
#else
2528
#include <SDL.h>
26-
#include <SDL_audio.h>
29+
#endif
2730

2831
#include <libaudcore/audstrings.h>
2932
#include <libaudcore/i18n.h>
@@ -73,9 +76,10 @@ const char SDLOutput::about[] =
7376
"Copyright 2010 John Lindgren");
7477

7578
const char * const SDLOutput::defaults[] = {
76-
"vol_left", "100",
77-
"vol_right", "100",
78-
nullptr};
79+
"vol_left", "100",
80+
"vol_right", "100",
81+
nullptr
82+
};
7983

8084
static pthread_mutex_t sdlout_mutex = PTHREAD_MUTEX_INITIALIZER;
8185
static pthread_cond_t sdlout_cond = PTHREAD_COND_INITIALIZER;
@@ -84,6 +88,10 @@ static volatile int vol_left, vol_right;
8488

8589
static int sdlout_chan, sdlout_rate;
8690

91+
#if HAVE_LIBSDL3
92+
static SDL_AudioStream * sdlout_stream;
93+
#endif
94+
8795
static RingBuf<unsigned char> buffer;
8896

8997
static bool prebuffer_flag, paused_flag;
@@ -98,7 +106,11 @@ bool SDLOutput::init ()
98106
vol_left = aud_get_int ("sdlout", "vol_left");
99107
vol_right = aud_get_int ("sdlout", "vol_right");
100108

109+
#if HAVE_LIBSDL3
110+
if (! SDL_Init (SDL_INIT_AUDIO))
111+
#else
101112
if (SDL_Init (SDL_INIT_AUDIO) < 0)
113+
#endif
102114
{
103115
AUDERR ("Failed to init SDL: %s.\n", SDL_GetError ());
104116
return false;
@@ -186,6 +198,23 @@ static void callback (void * user, unsigned char * buf, int len)
186198
pthread_mutex_unlock (& sdlout_mutex);
187199
}
188200

201+
#if HAVE_LIBSDL3
202+
static void SDLCALL sdl3_callback (void * user, SDL_AudioStream * stream,
203+
int additional_amount, int total_amount)
204+
{
205+
if (additional_amount <= 0)
206+
return;
207+
208+
Uint8 * data = SDL_stack_alloc (Uint8, additional_amount);
209+
if (! data)
210+
return;
211+
212+
callback (user, data, additional_amount);
213+
SDL_PutAudioStreamData (stream, data, additional_amount);
214+
SDL_stack_free (data);
215+
}
216+
#endif
217+
189218
bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
190219
{
191220
if (format != FMT_S16_NE)
@@ -205,15 +234,22 @@ bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
205234
prebuffer_flag = true;
206235
paused_flag = false;
207236

208-
SDL_AudioSpec spec = {0};
237+
#if HAVE_LIBSDL3
238+
const SDL_AudioSpec spec = { SDL_AUDIO_S16, chan, rate };
239+
sdlout_stream = SDL_OpenAudioDeviceStream (
240+
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, & spec, sdl3_callback, nullptr);
209241

242+
if (! sdlout_stream)
243+
#else
244+
SDL_AudioSpec spec = {0};
210245
spec.freq = rate;
211246
spec.format = AUDIO_S16;
212247
spec.channels = chan;
213248
spec.samples = 4096;
214249
spec.callback = callback;
215250

216251
if (SDL_OpenAudio (& spec, nullptr) < 0)
252+
#endif
217253
{
218254
error = String (str_printf
219255
("SDL error: Failed to open audio stream: %s.", SDL_GetError ()));
@@ -227,7 +263,14 @@ bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
227263
void SDLOutput::close_audio ()
228264
{
229265
AUDDBG ("Closing audio.\n");
266+
267+
#if HAVE_LIBSDL3
268+
SDL_DestroyAudioStream (sdlout_stream);
269+
sdlout_stream = nullptr;
270+
#else
230271
SDL_CloseAudio ();
272+
#endif
273+
231274
buffer.destroy ();
232275
}
233276

@@ -239,7 +282,12 @@ static void check_started ()
239282
AUDDBG ("Starting playback.\n");
240283
prebuffer_flag = false;
241284
block_delay = 0;
285+
286+
#if HAVE_LIBSDL3
287+
SDL_ResumeAudioStreamDevice (sdlout_stream);
288+
#else
242289
SDL_PauseAudio (0);
290+
#endif
243291
}
244292

245293
void SDLOutput::period_wait ()
@@ -311,7 +359,16 @@ void SDLOutput::pause (bool pause)
311359
paused_flag = pause;
312360

313361
if (! prebuffer_flag)
362+
{
363+
#if HAVE_LIBSDL3
364+
if (pause)
365+
SDL_PauseAudioStreamDevice (sdlout_stream);
366+
else
367+
SDL_ResumeAudioStreamDevice (sdlout_stream);
368+
#else
314369
SDL_PauseAudio (pause);
370+
#endif
371+
}
315372

316373
pthread_cond_broadcast (& sdlout_cond); /* wake up period wait */
317374
pthread_mutex_unlock (& sdlout_mutex);

0 commit comments

Comments
 (0)