c23a149
diff -up SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.c.pa-rewrite SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.c
c23a149
--- SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.c.pa-rewrite	2007-12-31 05:47:58.000000000 +0100
c23a149
+++ SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.c	2009-02-13 10:20:26.000000000 +0100
c23a149
@@ -29,6 +30,7 @@
c23a149
 #include <unistd.h>
c23a149
 #include <signal.h>
c23a149
 #include <errno.h>
c23a149
+#include <pulse/pulseaudio.h>
c23a149
 #include <pulse/simple.h>
c23a149
 
c23a149
 #include "SDL_timer.h"
c23a149
@@ -54,6 +56,7 @@ static void PULSE_WaitAudio(_THIS);
c23a149
 static void PULSE_PlayAudio(_THIS);
c23a149
 static Uint8 *PULSE_GetAudioBuf(_THIS);
c23a149
 static void PULSE_CloseAudio(_THIS);
c23a149
+static void PULSE_WaitDone(_THIS);
c23a149
 
c23a149
 #ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
c23a149
 
c23a149
@@ -73,19 +76,44 @@ static pa_simple* (*SDL_NAME(pa_simple_n
c23a149
 	int *error
c23a149
 );
c23a149
 static void (*SDL_NAME(pa_simple_free))(pa_simple *s);
c23a149
-static int (*SDL_NAME(pa_simple_drain))(pa_simple *s, int *error);
c23a149
-static int (*SDL_NAME(pa_simple_write))(
c23a149
-	pa_simple *s,
c23a149
-	const void *data,
c23a149
-	size_t length,
c23a149
-	int *error 
c23a149
-);
c23a149
+
c23a149
 static pa_channel_map* (*SDL_NAME(pa_channel_map_init_auto))(
c23a149
 	pa_channel_map *m,
c23a149
 	unsigned channels,
c23a149
 	pa_channel_map_def_t def
c23a149
 );
c23a149
-	
c23a149
+
c23a149
+pa_mainloop * (*SDL_NAME(pa_mainloop_new))(void);
c23a149
+pa_mainloop_api * (*SDL_NAME(pa_mainloop_get_api))(pa_mainloop *m);
c23a149
+int (*SDL_NAME(pa_mainloop_iterate))(pa_mainloop *m, int block, int *retval);
c23a149
+void (*SDL_NAME(pa_mainloop_free))(pa_mainloop *m);
c23a149
+
c23a149
+pa_operation_state_t (*SDL_NAME(pa_operation_get_state))(pa_operation *o);
c23a149
+void (*SDL_NAME(pa_operation_cancel))(pa_operation *o);
c23a149
+void (*SDL_NAME(pa_operation_unref))(pa_operation *o);
c23a149
+
c23a149
+pa_context * (*SDL_NAME(pa_context_new))(
c23a149
+	pa_mainloop_api *m, const char *name);
c23a149
+int (*SDL_NAME(pa_context_connect))(
c23a149
+	pa_context *c, const char *server,
c23a149
+	pa_context_flags_t flags, const pa_spawn_api *api);
c23a149
+pa_context_state_t (*SDL_NAME(pa_context_get_state))(pa_context *c);
c23a149
+void (*SDL_NAME(pa_context_disconnect))(pa_context *c);
c23a149
+void (*SDL_NAME(pa_context_unref))(pa_context *c);
c23a149
+
c23a149
+pa_stream * (*SDL_NAME(pa_stream_new))(pa_context *c,
c23a149
+	const char *name, const pa_sample_spec *ss, const pa_channel_map *map);
c23a149
+int (*SDL_NAME(pa_stream_connect_playback))(pa_stream *s, const char *dev,
c23a149
+	const pa_buffer_attr *attr, pa_stream_flags_t flags,
c23a149
+	pa_cvolume *volume, pa_stream *sync_stream);
c23a149
+pa_stream_state_t (*SDL_NAME(pa_stream_get_state))(pa_stream *s);
c23a149
+size_t (*SDL_NAME(pa_stream_writable_size))(pa_stream *s);
c23a149
+int (*SDL_NAME(pa_stream_write))(pa_stream *s, const void *data, size_t nbytes,
c23a149
+	pa_free_cb_t free_cb, int64_t offset, pa_seek_mode_t seek);
c23a149
+pa_operation * (*SDL_NAME(pa_stream_drain))(pa_stream *s,
c23a149
+	pa_stream_success_cb_t cb, void *userdata);
c23a149
+int (*SDL_NAME(pa_stream_disconnect))(pa_stream *s);
c23a149
+void (*SDL_NAME(pa_stream_unref))(pa_stream *s);
c23a149
 
c23a149
 static struct {
c23a149
 	const char *name;
c23a149
@@ -95,12 +123,48 @@ static struct {
c23a149
 		(void **)&SDL_NAME(pa_simple_new)		},
c23a149
 	{ "pa_simple_free",
c23a149
 		(void **)&SDL_NAME(pa_simple_free)		},
c23a149
-	{ "pa_simple_drain",
c23a149
-		(void **)&SDL_NAME(pa_simple_drain)		},
c23a149
-	{ "pa_simple_write",
c23a149
-		(void **)&SDL_NAME(pa_simple_write)		},
c23a149
 	{ "pa_channel_map_init_auto",
c23a149
 		(void **)&SDL_NAME(pa_channel_map_init_auto)	},
c23a149
+	{ "pa_mainloop_new",
c23a149
+		(void **)&SDL_NAME(pa_mainloop_new)		},
c23a149
+	{ "pa_mainloop_get_api",
c23a149
+		(void **)&SDL_NAME(pa_mainloop_get_api)		},
c23a149
+	{ "pa_mainloop_iterate",
c23a149
+		(void **)&SDL_NAME(pa_mainloop_iterate)		},
c23a149
+	{ "pa_mainloop_free",
c23a149
+		(void **)&SDL_NAME(pa_mainloop_free)		},
c23a149
+	{ "pa_operation_get_state",
c23a149
+		(void **)&SDL_NAME(pa_operation_get_state)	},
c23a149
+	{ "pa_operation_cancel",
c23a149
+		(void **)&SDL_NAME(pa_operation_cancel)		},
c23a149
+	{ "pa_operation_unref",
c23a149
+		(void **)&SDL_NAME(pa_operation_unref)		},
c23a149
+	{ "pa_context_new",
c23a149
+		(void **)&SDL_NAME(pa_context_new)		},
c23a149
+	{ "pa_context_connect",
c23a149
+		(void **)&SDL_NAME(pa_context_connect)		},
c23a149
+	{ "pa_context_get_state",
c23a149
+		(void **)&SDL_NAME(pa_context_get_state)	},
c23a149
+	{ "pa_context_disconnect",
c23a149
+		(void **)&SDL_NAME(pa_context_disconnect)	},
c23a149
+	{ "pa_context_unref",
c23a149
+		(void **)&SDL_NAME(pa_context_unref)		},
c23a149
+	{ "pa_stream_new",
c23a149
+		(void **)&SDL_NAME(pa_stream_new)		},
c23a149
+	{ "pa_stream_connect_playback",
c23a149
+		(void **)&SDL_NAME(pa_stream_connect_playback)	},
c23a149
+	{ "pa_stream_get_state",
c23a149
+		(void **)&SDL_NAME(pa_stream_get_state)		},
c23a149
+	{ "pa_stream_writable_size",
c23a149
+		(void **)&SDL_NAME(pa_stream_writable_size)	},
c23a149
+	{ "pa_stream_write",
c23a149
+		(void **)&SDL_NAME(pa_stream_write)		},
c23a149
+	{ "pa_stream_drain",
c23a149
+		(void **)&SDL_NAME(pa_stream_drain)		},
c23a149
+	{ "pa_stream_disconnect",
c23a149
+		(void **)&SDL_NAME(pa_stream_disconnect)	},
c23a149
+	{ "pa_stream_unref",
c23a149
+		(void **)&SDL_NAME(pa_stream_unref)		},
c23a149
 };
c23a149
 
c23a149
 static void UnloadPulseLibrary()
c23a149
@@ -158,16 +222,16 @@ static int Audio_Available(void)
c23a149
 	if ( LoadPulseLibrary() < 0 ) {
c23a149
 		return available;
c23a149
 	}
c23a149
-	
c23a149
+
c23a149
 	/* Connect with a dummy format. */
c23a149
 	paspec.format = PA_SAMPLE_U8;
c23a149
 	paspec.rate = 11025;
c23a149
 	paspec.channels = 1;
c23a149
 	connection = SDL_NAME(pa_simple_new)(
c23a149
-		SDL_getenv("PASERVER"),      /* server */
c23a149
+		NULL,                        /* server */
c23a149
 		"Test stream",               /* application name */
c23a149
 		PA_STREAM_PLAYBACK,          /* playback mode */
c23a149
-		SDL_getenv("PADEVICE"),      /* device on the server */
c23a149
+		NULL,                        /* device on the server */
c23a149
 		"Simple DirectMedia Layer",  /* stream description */
c23a149
 		&paspec,                     /* sample format spec */
c23a149
 		NULL,                        /* channel map */
c23a149
@@ -217,6 +281,7 @@ static SDL_AudioDevice *Audio_CreateDevi
c23a149
 	this->PlayAudio = PULSE_PlayAudio;
c23a149
 	this->GetAudioBuf = PULSE_GetAudioBuf;
c23a149
 	this->CloseAudio = PULSE_CloseAudio;
c23a149
+	this->WaitDone = PULSE_WaitDone;
c23a149
 
c23a149
 	this->free = Audio_DeleteDevice;
c23a149
 
c23a149
@@ -231,26 +296,25 @@ AudioBootStrap PULSE_bootstrap = {
c23a149
 /* This function waits until it is possible to write a full sound buffer */
c23a149
 static void PULSE_WaitAudio(_THIS)
c23a149
 {
c23a149
-	/* Check to see if the thread-parent process is still alive */
c23a149
-	{ static int cnt = 0;
c23a149
-		/* Note that this only works with thread implementations 
c23a149
-		   that use a different process id for each thread.
c23a149
-		*/
c23a149
-		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
c23a149
-			if ( kill(parent, 0) < 0 ) {
c23a149
-				this->enabled = 0;
c23a149
-			}
c23a149
+	int size;
c23a149
+	while(1) {
c23a149
+		if (SDL_NAME(pa_context_get_state)(context) != PA_CONTEXT_READY ||
c23a149
+		    SDL_NAME(pa_stream_get_state)(stream) != PA_STREAM_READY ||
c23a149
+		    SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
c23a149
+			this->enabled = 0;
c23a149
+			return;
c23a149
 		}
c23a149
+		size = SDL_NAME(pa_stream_writable_size)(stream);
c23a149
+		if (size >= mixlen)
c23a149
+			return;
c23a149
 	}
c23a149
 }
c23a149
 
c23a149
 static void PULSE_PlayAudio(_THIS)
c23a149
 {
c23a149
 	/* Write the audio data */
c23a149
-	if ( SDL_NAME(pa_simple_write)(stream, mixbuf, mixlen, NULL) != 0 )
c23a149
-	{
c23a149
+	if (SDL_NAME(pa_stream_write)(stream, mixbuf, mixlen, NULL, 0LL, PA_SEEK_RELATIVE) < 0)
c23a149
 		this->enabled = 0;
c23a149
-	}
c23a149
 }
c23a149
 
c23a149
 static Uint8 *PULSE_GetAudioBuf(_THIS)
c23a149
@@ -265,10 +329,19 @@ static void PULSE_CloseAudio(_THIS)
c23a149
 		mixbuf = NULL;
c23a149
 	}
c23a149
 	if ( stream != NULL ) {
c23a149
-		SDL_NAME(pa_simple_drain)(stream, NULL);
c23a149
-		SDL_NAME(pa_simple_free)(stream);
c23a149
+		SDL_NAME(pa_stream_disconnect)(stream);
c23a149
+		SDL_NAME(pa_stream_unref)(stream);
c23a149
 		stream = NULL;
c23a149
 	}
c23a149
+	if (context != NULL) {
c23a149
+		SDL_NAME(pa_context_disconnect)(context);
c23a149
+		SDL_NAME(pa_context_unref)(context);
c23a149
+		context = NULL;
c23a149
+	}
c23a149
+	if (mainloop != NULL) {
c23a149
+		SDL_NAME(pa_mainloop_free)(mainloop);
c23a149
+		mainloop = NULL;
c23a149
+	}
c23a149
 }
c23a149
 
c23a149
 /* Try to get the name of the program */
c23a149
@@ -296,13 +369,37 @@ static char *get_progname(void)
c23a149
 	return(progname);
c23a149
 }
c23a149
 
c23a149
+static void stream_drain_complete(pa_stream *s, int success, void *userdata) {
c23a149
+}
c23a149
+
c23a149
+static void PULSE_WaitDone(_THIS)
c23a149
+{
c23a149
+	pa_operation *o;
c23a149
+
c23a149
+	o = SDL_NAME(pa_stream_drain)(stream, stream_drain_complete, NULL);
c23a149
+	if (!o)
c23a149
+		return;
c23a149
+
c23a149
+	while (SDL_NAME(pa_operation_get_state)(o) != PA_OPERATION_DONE) {
c23a149
+		if (SDL_NAME(pa_context_get_state)(context) != PA_CONTEXT_READY ||
c23a149
+		    SDL_NAME(pa_stream_get_state)(stream) != PA_STREAM_READY ||
c23a149
+		    SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
c23a149
+			SDL_NAME(pa_operation_cancel)(o);
c23a149
+			break;
c23a149
+		}
c23a149
+	}
c23a149
+	SDL_NAME(pa_operation_unref)(o);
c23a149
+}
c23a149
+
c23a149
 static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec)
c23a149
 {
c23a149
+	int             state;
c23a149
 	Uint16          test_format;
c23a149
 	pa_sample_spec  paspec;
c23a149
 	pa_buffer_attr  paattr;
c23a149
 	pa_channel_map  pacmap;
c23a149
-	
c23a149
+	pa_stream_flags_t flags = 0;
c23a149
+
c23a149
 	paspec.format = PA_SAMPLE_INVALID;
c23a149
 	for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) {
c23a149
 		switch ( test_format ) {
c23a149
@@ -324,11 +421,14 @@ static int PULSE_OpenAudio(_THIS, SDL_Au
c23a149
 		return(-1);
c23a149
 	}
c23a149
 	spec->format = test_format;
c23a149
-	
c23a149
+
c23a149
 	paspec.channels = spec->channels;
c23a149
 	paspec.rate = spec->freq;
c23a149
 
c23a149
 	/* Calculate the final parameters for this audio specification */
c23a149
+#ifdef PA_STREAM_ADJUST_LATENCY
c23a149
+	spec->samples /= 2; /* Mix in smaller chunck to avoid underruns */
c23a149
+#endif
c23a149
 	SDL_CalculateAudioSpec(spec);
c23a149
 
c23a149
 	/* Allocate mixing buffer */
c23a149
@@ -338,40 +438,94 @@ static int PULSE_OpenAudio(_THIS, SDL_Au
c23a149
 		return(-1);
c23a149
 	}
c23a149
 	SDL_memset(mixbuf, spec->silence, spec->size);
c23a149
-	
c23a149
+
c23a149
 	/* Reduced prebuffering compared to the defaults. */
c23a149
-	paattr.tlength = mixlen;
c23a149
+#ifdef PA_STREAM_ADJUST_LATENCY
c23a149
+	paattr.tlength = mixlen * 4; /* 2x original requested bufsize */
c23a149
+	paattr.prebuf = -1;
c23a149
+	paattr.maxlength = -1;
c23a149
+	paattr.minreq = mixlen; /* -1 can lead to pa_stream_writable_size()
c23a149
+				   >= mixlen never becoming true */
c23a149
+	flags = PA_STREAM_ADJUST_LATENCY;
c23a149
+#else
c23a149
+	paattr.tlength = mixlen*2;
c23a149
+	paattr.prebuf = mixlen*2;
c23a149
+	paattr.maxlength = mixlen*2;
c23a149
 	paattr.minreq = mixlen;
c23a149
-	paattr.fragsize = mixlen;
c23a149
-	paattr.prebuf = mixlen;
c23a149
-	paattr.maxlength = mixlen * 4;
c23a149
-	
c23a149
+#endif
c23a149
+
c23a149
 	/* The SDL ALSA output hints us that we use Windows' channel mapping */
c23a149
 	/* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
c23a149
 	SDL_NAME(pa_channel_map_init_auto)(
c23a149
 		&pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX);
c23a149
-	
c23a149
+
c23a149
+	/* Set up a new main loop */
c23a149
+	if (!(mainloop = SDL_NAME(pa_mainloop_new)())) {
c23a149
+		PULSE_CloseAudio(this);
c23a149
+		SDL_SetError("pa_mainloop_new() failed");
c23a149
+		return(-1);
c23a149
+	}
c23a149
+
c23a149
+	mainloop_api = SDL_NAME(pa_mainloop_get_api)(mainloop);
c23a149
+	if (!(context = SDL_NAME(pa_context_new)(mainloop_api, get_progname()))) {
c23a149
+		PULSE_CloseAudio(this);
c23a149
+		SDL_SetError("pa_context_new() failed");
c23a149
+		return(-1);
c23a149
+	}
c23a149
+
c23a149
 	/* Connect to the PulseAudio server */
c23a149
-	stream = SDL_NAME(pa_simple_new)(
c23a149
-		SDL_getenv("PASERVER"),      /* server */
c23a149
-		get_progname(),              /* application name */
c23a149
-		PA_STREAM_PLAYBACK,          /* playback mode */
c23a149
-		SDL_getenv("PADEVICE"),      /* device on the server */
c23a149
+	if (SDL_NAME(pa_context_connect)(context, NULL, 0, NULL) < 0) {
c23a149
+		PULSE_CloseAudio(this);
c23a149
+	        SDL_SetError("Could not setup connection to PulseAudio");
c23a149
+		return(-1);
c23a149
+	}
c23a149
+
c23a149
+	do {
c23a149
+		if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
c23a149
+			PULSE_CloseAudio(this);
c23a149
+			SDL_SetError("pa_mainloop_iterate() failed");
c23a149
+			return(-1);
c23a149
+		}
c23a149
+		state = SDL_NAME(pa_context_get_state)(context);
c23a149
+		if (!PA_CONTEXT_IS_GOOD(state)) {
c23a149
+			PULSE_CloseAudio(this);
c23a149
+			SDL_SetError("Could not connect to PulseAudio");
c23a149
+			return(-1);
c23a149
+		}
c23a149
+	} while (state != PA_CONTEXT_READY);
c23a149
+
c23a149
+	stream = SDL_NAME(pa_stream_new)(
c23a149
+		context,
c23a149
 		"Simple DirectMedia Layer",  /* stream description */
c23a149
 		&paspec,                     /* sample format spec */
c23a149
-		&pacmap,                     /* channel map */
c23a149
-		&paattr,                     /* buffering attributes */
c23a149
-		NULL                         /* error code */
c23a149
+		&pacmap                      /* channel map */
c23a149
 	);
c23a149
 	if ( stream == NULL ) {
c23a149
 		PULSE_CloseAudio(this);
c23a149
-		SDL_SetError("Could not connect to PulseAudio");
c23a149
+		SDL_SetError("Could not setup PulseAudio stream");
c23a149
 		return(-1);
c23a149
 	}
c23a149
 
c23a149
-	/* Get the parent process id (we're the parent of the audio thread) */
c23a149
-	parent = getpid();
c23a149
-	
c23a149
+	if (SDL_NAME(pa_stream_connect_playback)(stream, NULL, &paattr, flags,
c23a149
+			NULL, NULL) < 0) {
c23a149
+		PULSE_CloseAudio(this);
c23a149
+		SDL_SetError("Could not connect PulseAudio stream");
c23a149
+		return(-1);
c23a149
+	}
c23a149
+
c23a149
+	do {
c23a149
+		if (SDL_NAME(pa_mainloop_iterate)(mainloop, 1, NULL) < 0) {
c23a149
+			PULSE_CloseAudio(this);
c23a149
+			SDL_SetError("pa_mainloop_iterate() failed");
c23a149
+			return(-1);
c23a149
+		}
c23a149
+		state = SDL_NAME(pa_stream_get_state)(stream);
c23a149
+		if (!PA_STREAM_IS_GOOD(state)) {
c23a149
+			PULSE_CloseAudio(this);
c23a149
+			SDL_SetError("Could not create to PulseAudio stream");
c23a149
+			return(-1);
c23a149
+		}
c23a149
+	} while (state != PA_STREAM_READY);
c23a149
+
c23a149
 	return(0);
c23a149
 }
c23a149
-
c23a149
diff -up SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.h.pa-rewrite SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.h
c23a149
--- SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.h.pa-rewrite	2007-12-31 05:47:58.000000000 +0100
c23a149
+++ SDL-1.2.13/src/audio/pulse/SDL_pulseaudio.h	2009-02-13 10:20:26.000000000 +0100
c23a149
@@ -32,11 +32,10 @@
c23a149
 #define _THIS	SDL_AudioDevice *this
c23a149
 
c23a149
 struct SDL_PrivateAudioData {
c23a149
-	/* The audio stream handle */
c23a149
-	pa_simple * stream;
c23a149
-
c23a149
-	/* The parent process id, to detect when application quits */
c23a149
-	pid_t parent;
c23a149
+	pa_mainloop *mainloop;
c23a149
+	pa_mainloop_api *mainloop_api;
c23a149
+	pa_context *context;
c23a149
+	pa_stream *stream;
c23a149
 
c23a149
 	/* Raw mixing buffer */
c23a149
 	Uint8 *mixbuf;
c23a149
@@ -44,8 +43,10 @@ struct SDL_PrivateAudioData {
c23a149
 };
c23a149
 
c23a149
 /* Old variable names */
c23a149
+#define mainloop		(this->hidden->mainloop)
c23a149
+#define mainloop_api		(this->hidden->mainloop_api)
c23a149
+#define context			(this->hidden->context)
c23a149
 #define stream			(this->hidden->stream)
c23a149
-#define parent			(this->hidden->parent)
c23a149
 #define mixbuf			(this->hidden->mixbuf)
c23a149
 #define mixlen			(this->hidden->mixlen)
c23a149