Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 17 Jan 2010 06:14:04 +0000
changeset 3672878d8adf2fde
parent 3671 0d6f520c0eb9
child 3673 c9d5bd1b4e18
Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.

This pulls all the PulseAudio reworking that was done for 1.2.14 into 1.3.

Fixes Bugzilla #801.
src/audio/pulseaudio/SDL_pulseaudio.c
src/audio/pulseaudio/SDL_pulseaudio.h
     1.1 --- a/src/audio/pulseaudio/SDL_pulseaudio.c	Fri Jan 15 20:05:49 2010 +0000
     1.2 +++ b/src/audio/pulseaudio/SDL_pulseaudio.c	Sun Jan 17 06:14:04 2010 +0000
     1.3 @@ -37,6 +37,7 @@
     1.4  #include <unistd.h>
     1.5  #include <sys/types.h>
     1.6  #include <errno.h>
     1.7 +#include <pulse/pulseaudio.h>
     1.8  #include <pulse/simple.h>
     1.9  
    1.10  #include "SDL_timer.h"
    1.11 @@ -57,6 +58,23 @@
    1.12  
    1.13  #ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
    1.14  
    1.15 +#if (PA_API_VERSION < 12)
    1.16 +/** Return non-zero if the passed state is one of the connected states */
    1.17 +static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
    1.18 +    return
    1.19 +        x == PA_CONTEXT_CONNECTING ||
    1.20 +        x == PA_CONTEXT_AUTHORIZING ||
    1.21 +        x == PA_CONTEXT_SETTING_NAME ||
    1.22 +        x == PA_CONTEXT_READY;
    1.23 +}
    1.24 +/** Return non-zero if the passed state is one of the connected states */
    1.25 +static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
    1.26 +    return
    1.27 +        x == PA_STREAM_CREATING ||
    1.28 +        x == PA_STREAM_READY;
    1.29 +}
    1.30 +#endif /* pulseaudio <= 0.9.10 */
    1.31 +
    1.32  static const char *pulse_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
    1.33  static void *pulse_handle = NULL;
    1.34  
    1.35 @@ -71,10 +89,6 @@
    1.36                                                const pa_buffer_attr * attr,
    1.37                                                int *error);
    1.38  static void (*SDL_NAME(pa_simple_free)) (pa_simple * s);
    1.39 -static int (*SDL_NAME(pa_simple_drain)) (pa_simple * s, int *error);
    1.40 -static int (*SDL_NAME(pa_simple_write)) (pa_simple * s,
    1.41 -                                         const void *data,
    1.42 -                                         size_t length, int *error);
    1.43  static pa_channel_map *(*SDL_NAME(pa_channel_map_init_auto)) (pa_channel_map *
    1.44                                                                m,
    1.45                                                                unsigned
    1.46 @@ -82,6 +96,37 @@
    1.47                                                                pa_channel_map_def_t
    1.48                                                                def);
    1.49  static const char *(*SDL_NAME(pa_strerror)) (int error);
    1.50 +static pa_mainloop * (*SDL_NAME(pa_mainloop_new))(void);
    1.51 +static pa_mainloop_api * (*SDL_NAME(pa_mainloop_get_api))(pa_mainloop *m);
    1.52 +static int (*SDL_NAME(pa_mainloop_iterate))(pa_mainloop *m, int block, int *retval);
    1.53 +static void (*SDL_NAME(pa_mainloop_free))(pa_mainloop *m);
    1.54 +
    1.55 +static pa_operation_state_t (*SDL_NAME(pa_operation_get_state))(pa_operation *o);
    1.56 +static void (*SDL_NAME(pa_operation_cancel))(pa_operation *o);
    1.57 +static void (*SDL_NAME(pa_operation_unref))(pa_operation *o);
    1.58 +
    1.59 +static pa_context * (*SDL_NAME(pa_context_new))(
    1.60 +    pa_mainloop_api *m, const char *name);
    1.61 +static int (*SDL_NAME(pa_context_connect))(
    1.62 +    pa_context *c, const char *server,
    1.63 +    pa_context_flags_t flags, const pa_spawn_api *api);
    1.64 +static pa_context_state_t (*SDL_NAME(pa_context_get_state))(pa_context *c);
    1.65 +static void (*SDL_NAME(pa_context_disconnect))(pa_context *c);
    1.66 +static void (*SDL_NAME(pa_context_unref))(pa_context *c);
    1.67 +
    1.68 +static pa_stream * (*SDL_NAME(pa_stream_new))(pa_context *c,
    1.69 +    const char *name, const pa_sample_spec *ss, const pa_channel_map *map);
    1.70 +static int (*SDL_NAME(pa_stream_connect_playback))(pa_stream *s, const char *dev,
    1.71 +    const pa_buffer_attr *attr, pa_stream_flags_t flags,
    1.72 +    pa_cvolume *volume, pa_stream *sync_stream);
    1.73 +static pa_stream_state_t (*SDL_NAME(pa_stream_get_state))(pa_stream *s);
    1.74 +static size_t (*SDL_NAME(pa_stream_writable_size))(pa_stream *s);
    1.75 +static int (*SDL_NAME(pa_stream_write))(pa_stream *s, const void *data, size_t nbytes,
    1.76 +    pa_free_cb_t free_cb, int64_t offset, pa_seek_mode_t seek);
    1.77 +static pa_operation * (*SDL_NAME(pa_stream_drain))(pa_stream *s,
    1.78 +    pa_stream_success_cb_t cb, void *userdata);
    1.79 +static int (*SDL_NAME(pa_stream_disconnect))(pa_stream *s);
    1.80 +static void (*SDL_NAME(pa_stream_unref))(pa_stream *s);
    1.81  
    1.82  
    1.83  #define SDL_PULSEAUDIO_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
    1.84 @@ -93,8 +138,26 @@
    1.85  /* *INDENT-OFF* */
    1.86      SDL_PULSEAUDIO_SYM(pa_simple_new),
    1.87      SDL_PULSEAUDIO_SYM(pa_simple_free),
    1.88 -    SDL_PULSEAUDIO_SYM(pa_simple_drain),
    1.89 -    SDL_PULSEAUDIO_SYM(pa_simple_write),
    1.90 +    SDL_PULSEAUDIO_SYM(pa_mainloop_new),
    1.91 +    SDL_PULSEAUDIO_SYM(pa_mainloop_get_api),
    1.92 +    SDL_PULSEAUDIO_SYM(pa_mainloop_iterate),
    1.93 +    SDL_PULSEAUDIO_SYM(pa_mainloop_free),
    1.94 +    SDL_PULSEAUDIO_SYM(pa_operation_get_state),
    1.95 +    SDL_PULSEAUDIO_SYM(pa_operation_cancel),
    1.96 +    SDL_PULSEAUDIO_SYM(pa_operation_unref),
    1.97 +    SDL_PULSEAUDIO_SYM(pa_context_new),
    1.98 +    SDL_PULSEAUDIO_SYM(pa_context_connect),
    1.99 +    SDL_PULSEAUDIO_SYM(pa_context_get_state),
   1.100 +    SDL_PULSEAUDIO_SYM(pa_context_disconnect),
   1.101 +    SDL_PULSEAUDIO_SYM(pa_context_unref),
   1.102 +    SDL_PULSEAUDIO_SYM(pa_stream_new),
   1.103 +    SDL_PULSEAUDIO_SYM(pa_stream_connect_playback),
   1.104 +    SDL_PULSEAUDIO_SYM(pa_stream_get_state),
   1.105 +    SDL_PULSEAUDIO_SYM(pa_stream_writable_size),
   1.106 +    SDL_PULSEAUDIO_SYM(pa_stream_write),
   1.107 +    SDL_PULSEAUDIO_SYM(pa_stream_drain),
   1.108 +    SDL_PULSEAUDIO_SYM(pa_stream_disconnect),
   1.109 +    SDL_PULSEAUDIO_SYM(pa_stream_unref),
   1.110      SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto),
   1.111      SDL_PULSEAUDIO_SYM(pa_strerror),
   1.112  /* *INDENT-ON* */
   1.113 @@ -155,27 +218,16 @@
   1.114  static void
   1.115  PULSEAUDIO_WaitDevice(_THIS)
   1.116  {
   1.117 -    Sint32 ticks;
   1.118 -
   1.119 -    /* Check to see if the thread-parent process is still alive */
   1.120 -    {
   1.121 -        static int cnt = 0;
   1.122 -        /* Note that this only works with thread implementations
   1.123 -           that use a different process id for each thread.
   1.124 -         */
   1.125 -        /* Check every 10 loops */
   1.126 -        if (this->hidden->parent && (((++cnt) % 10) == 0)) {
   1.127 -            if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) {
   1.128 -                this->enabled = 0;
   1.129 -            }
   1.130 +    while(1) {
   1.131 +        if (SDL_NAME(pa_context_get_state)(this->hidden->context) != PA_CONTEXT_READY ||
   1.132 +            SDL_NAME(pa_stream_get_state)(this->hidden->stream) != PA_STREAM_READY ||
   1.133 +            SDL_NAME(pa_mainloop_iterate)(this->hidden->mainloop, 1, NULL) < 0) {
   1.134 +            this->enabled = 0;
   1.135 +            return;
   1.136          }
   1.137 -    }
   1.138 -
   1.139 -    /* Use timer for general audio synchronization */
   1.140 -    ticks =
   1.141 -        ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
   1.142 -    if (ticks > 0) {
   1.143 -        SDL_Delay(ticks);
   1.144 +        if (SDL_NAME(pa_stream_writable_size)(this->hidden->stream) >= this->hidden->mixlen) {
   1.145 +            return;
   1.146 +        }
   1.147      }
   1.148  }
   1.149  
   1.150 @@ -183,19 +235,43 @@
   1.151  PULSEAUDIO_PlayDevice(_THIS)
   1.152  {
   1.153      /* Write the audio data */
   1.154 -    if (SDL_NAME(pa_simple_write) (this->hidden->stream, this->hidden->mixbuf,
   1.155 -                                   this->hidden->mixlen, NULL) != 0) {
   1.156 +    if (SDL_NAME(pa_stream_write) (this->hidden->stream, this->hidden->mixbuf,
   1.157 +                                   this->hidden->mixlen, NULL, 0LL,
   1.158 +                                   PA_SEEK_RELATIVE) < 0) {
   1.159          this->enabled = 0;
   1.160      }
   1.161  }
   1.162  
   1.163  static void
   1.164 +stream_drain_complete(pa_stream *s, int success, void *userdata)
   1.165 +{
   1.166 +    /* no-op for pa_stream_drain() to use for callback. */
   1.167 +}
   1.168 +
   1.169 +static void
   1.170  PULSEAUDIO_WaitDone(_THIS)
   1.171  {
   1.172 -    SDL_NAME(pa_simple_drain) (this->hidden->stream, NULL);
   1.173 +    pa_operation *o;
   1.174 +
   1.175 +    o = SDL_NAME(pa_stream_drain)(this->hidden->stream, stream_drain_complete, NULL);
   1.176 +    if (!o) {
   1.177 +        return;
   1.178 +    }
   1.179 +
   1.180 +    while (SDL_NAME(pa_operation_get_state)(o) != PA_OPERATION_DONE) {
   1.181 +        if (SDL_NAME(pa_context_get_state)(this->hidden->context) != PA_CONTEXT_READY ||
   1.182 +            SDL_NAME(pa_stream_get_state)(this->hidden->stream) != PA_STREAM_READY ||
   1.183 +            SDL_NAME(pa_mainloop_iterate)(this->hidden->mainloop, 1, NULL) < 0) {
   1.184 +            SDL_NAME(pa_operation_cancel)(o);
   1.185 +            break;
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +    SDL_NAME(pa_operation_unref)(o);
   1.190  }
   1.191  
   1.192  
   1.193 +
   1.194  static Uint8 *
   1.195  PULSEAUDIO_GetDeviceBuf(_THIS)
   1.196  {
   1.197 @@ -212,10 +288,19 @@
   1.198              this->hidden->mixbuf = NULL;
   1.199          }
   1.200          if (this->hidden->stream) {
   1.201 -            SDL_NAME(pa_simple_drain) (this->hidden->stream, NULL);
   1.202 -            SDL_NAME(pa_simple_free) (this->hidden->stream);
   1.203 +            SDL_NAME(pa_stream_disconnect)(this->hidden->stream);
   1.204 +            SDL_NAME(pa_stream_unref)(this->hidden->stream);
   1.205              this->hidden->stream = NULL;
   1.206          }
   1.207 +        if (this->hidden->context != NULL) {
   1.208 +            SDL_NAME(pa_context_disconnect)(this->hidden->context);
   1.209 +            SDL_NAME(pa_context_unref)(this->hidden->context);
   1.210 +            this->hidden->context = NULL;
   1.211 +        }
   1.212 +        if (this->hidden->mainloop != NULL) {
   1.213 +            SDL_NAME(pa_mainloop_free)(this->hidden->mainloop);
   1.214 +            this->hidden->mainloop = NULL;
   1.215 +        }
   1.216          SDL_free(this->hidden);
   1.217          this->hidden = NULL;
   1.218      }
   1.219 @@ -227,8 +312,8 @@
   1.220  static char *
   1.221  get_progname(void)
   1.222  {
   1.223 +#ifdef __LINUX__
   1.224      char *progname = NULL;
   1.225 -#ifdef __LINUX__
   1.226      FILE *fp;
   1.227      static char temp[BUFSIZ];
   1.228  
   1.229 @@ -245,8 +330,12 @@
   1.230          }
   1.231          fclose(fp);
   1.232      }
   1.233 +    return(progname);
   1.234 +#elif defined(__NetBSD__)
   1.235 +    return getprogname();
   1.236 +#else
   1.237 +    return("unknown");
   1.238  #endif
   1.239 -    return (progname);
   1.240  }
   1.241  
   1.242  
   1.243 @@ -257,7 +346,8 @@
   1.244      pa_sample_spec paspec;
   1.245      pa_buffer_attr paattr;
   1.246      pa_channel_map pacmap;
   1.247 -    int err = 0;
   1.248 +    pa_stream_flags_t flags = 0;
   1.249 +    int state = 0;
   1.250  
   1.251      /* Initialize all variables that we clean on shutdown */
   1.252      this->hidden = (struct SDL_PrivateAudioData *)
   1.253 @@ -302,6 +392,9 @@
   1.254      this->spec.format = test_format;
   1.255  
   1.256      /* Calculate the final parameters for this audio specification */
   1.257 +#ifdef PA_STREAM_ADJUST_LATENCY
   1.258 +    this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */
   1.259 +#endif
   1.260      SDL_CalculateAudioSpec(&this->spec);
   1.261  
   1.262      /* Allocate mixing buffer */
   1.263 @@ -318,38 +411,93 @@
   1.264      paspec.rate = this->spec.freq;
   1.265  
   1.266      /* Reduced prebuffering compared to the defaults. */
   1.267 -    paattr.tlength = this->hidden->mixlen;
   1.268 +#ifdef PA_STREAM_ADJUST_LATENCY
   1.269 +    paattr.tlength = this->hidden->mixlen * 4; /* 2x original requested bufsize */
   1.270 +    paattr.prebuf = -1;
   1.271 +    paattr.maxlength = -1;
   1.272 +    /* -1 can lead to pa_stream_writable_size() >= this->hidden->mixlen never being true */
   1.273      paattr.minreq = this->hidden->mixlen;
   1.274 -    paattr.fragsize = this->hidden->mixlen;
   1.275 -    paattr.prebuf = this->hidden->mixlen;
   1.276 -    paattr.maxlength = this->hidden->mixlen * 4;
   1.277 +    flags = PA_STREAM_ADJUST_LATENCY;
   1.278 +#else
   1.279 +    paattr.tlength = this->hidden->mixlen*2;
   1.280 +    paattr.prebuf = this->hidden->mixlen*2;
   1.281 +    paattr.maxlength = this->hidden->mixlen*2;
   1.282 +    paattr.minreq = this->hidden->mixlen;
   1.283 +#endif
   1.284  
   1.285      /* The SDL ALSA output hints us that we use Windows' channel mapping */
   1.286      /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
   1.287      SDL_NAME(pa_channel_map_init_auto) (&pacmap, this->spec.channels,
   1.288                                          PA_CHANNEL_MAP_WAVEEX);
   1.289  
   1.290 +    /* Set up a new main loop */
   1.291 +    if (!(this->hidden->mainloop = SDL_NAME(pa_mainloop_new)())) {
   1.292 +        PULSEAUDIO_CloseDevice(this);
   1.293 +        SDL_SetError("pa_mainloop_new() failed");
   1.294 +        return 0;
   1.295 +    }
   1.296 +
   1.297 +    this->hidden->mainloop_api = SDL_NAME(pa_mainloop_get_api)(this->hidden->mainloop);
   1.298 +    if (!(this->hidden->context = SDL_NAME(pa_context_new)(this->hidden->mainloop_api, get_progname()))) {
   1.299 +        PULSEAUDIO_CloseDevice(this);
   1.300 +        SDL_SetError("pa_context_new() failed");
   1.301 +        return 0;
   1.302 +    }
   1.303 +
   1.304      /* Connect to the PulseAudio server */
   1.305 -    this->hidden->stream = SDL_NAME(pa_simple_new) (SDL_getenv("PASERVER"),     /* server */
   1.306 -                                                    get_progname(),     /* application name */
   1.307 -                                                    PA_STREAM_PLAYBACK, /* playback mode */
   1.308 -                                                    SDL_getenv("PADEVICE"),     /* device on the server */
   1.309 -                                                    "Simple DirectMedia Layer", /* stream description */
   1.310 -                                                    &paspec,    /* sample format spec */
   1.311 -                                                    &pacmap,    /* channel map */
   1.312 -                                                    &paattr,    /* buffering attributes */
   1.313 -                                                    &err        /* error code */
   1.314 +    if (SDL_NAME(pa_context_connect)(this->hidden->context, NULL, 0, NULL) < 0) {
   1.315 +        PULSEAUDIO_CloseDevice(this);
   1.316 +        SDL_SetError("Could not setup connection to PulseAudio");
   1.317 +        return 0;
   1.318 +    }
   1.319 +
   1.320 +    do {
   1.321 +        if (SDL_NAME(pa_mainloop_iterate)(this->hidden->mainloop, 1, NULL) < 0) {
   1.322 +            PULSEAUDIO_CloseDevice(this);
   1.323 +            SDL_SetError("pa_mainloop_iterate() failed");
   1.324 +            return 0;
   1.325 +        }
   1.326 +        state = SDL_NAME(pa_context_get_state)(this->hidden->context);
   1.327 +        if (!PA_CONTEXT_IS_GOOD(state)) {
   1.328 +            PULSEAUDIO_CloseDevice(this);
   1.329 +            SDL_SetError("Could not connect to PulseAudio");
   1.330 +            return 0;
   1.331 +        }
   1.332 +    } while (state != PA_CONTEXT_READY);
   1.333 +
   1.334 +    this->hidden->stream = SDL_NAME(pa_stream_new)(
   1.335 +        this->hidden->context,
   1.336 +        "Simple DirectMedia Layer", /* stream description */
   1.337 +        &paspec,    /* sample format spec */
   1.338 +        &pacmap     /* channel map */
   1.339          );
   1.340  
   1.341      if (this->hidden->stream == NULL) {
   1.342          PULSEAUDIO_CloseDevice(this);
   1.343 -        SDL_SetError("Could not connect to PulseAudio: %s",
   1.344 -                     SDL_NAME(pa_strerror(err)));
   1.345 +        SDL_SetError("Could not set up PulseAudio stream");
   1.346          return 0;
   1.347      }
   1.348  
   1.349 -    /* Get the parent process id (we're the parent of the audio thread) */
   1.350 -    this->hidden->parent = getpid();
   1.351 +    if (SDL_NAME(pa_stream_connect_playback)(this->hidden->stream, NULL, &paattr, flags,
   1.352 +            NULL, NULL) < 0) {
   1.353 +        PULSEAUDIO_CloseDevice(this);
   1.354 +        SDL_SetError("Could not connect PulseAudio stream");
   1.355 +        return 0;
   1.356 +    }
   1.357 +
   1.358 +    do {
   1.359 +        if (SDL_NAME(pa_mainloop_iterate)(this->hidden->mainloop, 1, NULL) < 0) {
   1.360 +            PULSEAUDIO_CloseDevice(this);
   1.361 +            SDL_SetError("pa_mainloop_iterate() failed");
   1.362 +            return 0;
   1.363 +        }
   1.364 +        state = SDL_NAME(pa_stream_get_state)(this->hidden->stream);
   1.365 +        if (!PA_STREAM_IS_GOOD(state)) {
   1.366 +            PULSEAUDIO_CloseDevice(this);
   1.367 +            SDL_SetError("Could not create to PulseAudio stream");
   1.368 +            return 0;
   1.369 +        }
   1.370 +    } while (state != PA_STREAM_READY);
   1.371  
   1.372      /* We're ready to rock and roll. :-) */
   1.373      return 1;
     2.1 --- a/src/audio/pulseaudio/SDL_pulseaudio.h	Fri Jan 15 20:05:49 2010 +0000
     2.2 +++ b/src/audio/pulseaudio/SDL_pulseaudio.h	Sun Jan 17 06:14:04 2010 +0000
     2.3 @@ -29,25 +29,20 @@
     2.4  #include "../SDL_sysaudio.h"
     2.5  
     2.6  /* Hidden "this" pointer for the audio functions */
     2.7 -#define _THIS	SDL_AudioDevice *this
     2.8 +#define _THIS SDL_AudioDevice *this
     2.9  
    2.10  struct SDL_PrivateAudioData
    2.11  {
    2.12 -    /* The audio stream handle */
    2.13 -    pa_simple *stream;
    2.14 -
    2.15 -    /* The parent process id, to detect when application quits */
    2.16 -    pid_t parent;
    2.17 +    /* pulseaudio structures */
    2.18 +    pa_mainloop *mainloop;
    2.19 +    pa_mainloop_api *mainloop_api;
    2.20 +    pa_context *context;
    2.21 +    pa_stream *stream;
    2.22  
    2.23      /* Raw mixing buffer */
    2.24      Uint8 *mixbuf;
    2.25      int mixlen;
    2.26 -
    2.27 -    /* Support for audio timing using a timer, in addition to select() */
    2.28 -    float frame_ticks;
    2.29 -    float next_frame;
    2.30  };
    2.31 -#define FUDGE_TICKS	10      /* The scheduler overhead ticks per frame */
    2.32  
    2.33  #endif /* _SDL_pulseaudio_h */
    2.34