view src/audio/pulse/SDL_pulseaudio.c @ 4139:568c9b3c0167 SDL-1.2

* Added configure option --enable-screensaver, to allow enabling the screensaver by default. * Use XResetScreenSaver() instead of disabling screensaver entirely. Full discussion summary from Erik on the SDL mailing list: Current behaviour ================= SDL changes the user's display power management settings without permission from the user and without telling the user. The interface that it uses to do so is DPMSDisable/DPMSEnable, which should only ever be used by configuration utilities like KControl, never by normal application programs, let alone by the libraries that they use. Using an interface that is not at all intended for what SDL tries to achieve means that it will not work as it should. Firstly, the power management is completely disabled during the whole lifetime of the SDL program, not only when it should be. Secondly, it makes SDL non-reentrant, meaning that things will break when multiple SDL programs are clients of the same X server simultaneously. Thirdly, no cleanup mechanism ensures that the setting is restored if the client does not do that (for example if it crashes). In addition to that, this interface is broken on xorg, [http://bugs.freedesktop.org/show_bug.cgi?id=13962], so what SDL tries to do does not work at all on that implementation of the X Window System. (The reason that the DPMSEnable works in KControl is that it calls DPMSSetTimeout immediately after, [http://websvn.kde.org/tags/KDE/3.5.9/kdebase/kcontrol/energy/energy.cpp?annotate=774532#l343]). The problems that the current behaviour causes ============================================== 1. Information leak. When the user is away, someone might see what the user has on the display when the user counts on the screensaver preventing this. This does not even require physical access to the workstation, it is enough to see it from a distance. 2. Draining battery. An SDL program that runs on a laptop will quickly drain the battery while the user is away. The system will soon shut down and require recharging before being usable again, while it should in fact have consumed very little energy if the user's settings would have been obeyed. 3. Wasting energy. Even if battery issues are not considered, energy as such is wasted. 4. Display wear. The display may be worn out. The problems that the current behaviour tries to solve ====================================================== 1. Preventing screensaver while playing movies. Many SDL applications are media players. They have reasons to prevent screensavers from being activated while a movie is being played. When a user clicks on the play button it can be interpreted as saying "play this movie, but do not turn off the display while playing it, because I will watch it even though I do not interact with the system". 2. Preventing screensaver when some input bypasses X. Sometimes SDL uses input from another source than the X server, so that the X server is bypassed. This obviously breaks the screensaver handling. SDL tries to work around that. 3. Preventing screensaver when all input bypasses X. There is something called Direct Graphics Access mode, where a program takes control of both the display and the input devices from the X server. This obviously means that the X server can not handle the screensaver alone, since screensaver handling depends on input handling. SDL does not do what it should to help the X server to handle the screensaver. Nor does SDL take care of screeensaver handling itself. SDL simply disables the screensaver completely. How the problems should be solved ================================= The correct way for an application program to prevent the screensaver under X is to call XResetScreenSaver. This was recently discovered and implemented by the mplayer developers, [http://svn.mplayerhq.hu/mplayer?view=rev&revision=25637]. SDL needs to wrap this in an API call (SDL_ResetScreenSaver) and implement it for the other video targets (if they do not have a corresponding call, SDL should do what it takes on that particular target, for example sending fake key events). 1. When a movie is played, the player should reset the screensaver when the animation is advanced to a new frame. The same applies to anything similar, like slideshows. 2. When the X server is handling input, it must handle all input (keyboards, mice, gamepads, ...). This is necessary, not only to be able to handle the screensaver, but also so that it can send the events to the correct (the currently active) client. If there is an input device that the X server can not handle for some reason (such as lack of Plug and Play capability), the program that handles the device as a workaround must simulate what would happen if the X server would have handled the device, by calling XResetScreenSaver when input is received from the device. 3. When the X server is not handling the input, it depends on the program that does to call XResetScreenSaver whenever an input event occurs. Alternatively the program must handle the screensaver countdown internally and call XActivateScreenSaver.
author Sam Lantinga <slouken@libsdl.org>
date Fri, 29 Feb 2008 13:55:44 +0000
parents 42e83d81224b
children a1b03ba2fcd0
line wrap: on
line source

/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2007 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Stéphan Kochen
    stephan@kochen.nl
    
    Based on parts of the ALSA and ESounD output drivers.
*/
#include "SDL_config.h"

/* Allow access to an PulseAudio network stream mixing buffer */

#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <pulse/simple.h>

#include "SDL_timer.h"
#include "SDL_audio.h"
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
#include "SDL_pulseaudio.h"

#ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
#include "SDL_name.h"
#include "SDL_loadso.h"
#else
#define SDL_NAME(X)	X
#endif

/* The tag name used by the driver */
#define PULSE_DRIVER_NAME	"pulse"

/* Audio driver functions */
static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec);
static void PULSE_WaitAudio(_THIS);
static void PULSE_PlayAudio(_THIS);
static Uint8 *PULSE_GetAudioBuf(_THIS);
static void PULSE_CloseAudio(_THIS);

#ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC

static const char *pulse_library = SDL_AUDIO_DRIVER_PULSE_DYNAMIC;
static void *pulse_handle = NULL;
static int pulse_loaded = 0;

static pa_simple* (*SDL_NAME(pa_simple_new))(
	const char *server,
	const char *name,
	pa_stream_direction_t dir,
	const char *dev,
	const char *stream_name,
	const pa_sample_spec *ss,
	const pa_channel_map *map,
	const pa_buffer_attr *attr,
	int *error
);
static void (*SDL_NAME(pa_simple_free))(pa_simple *s);
static int (*SDL_NAME(pa_simple_drain))(pa_simple *s, int *error);
static int (*SDL_NAME(pa_simple_write))(
	pa_simple *s,
	const void *data,
	size_t length,
	int *error 
);
static pa_channel_map* (*SDL_NAME(pa_channel_map_init_auto))(
	pa_channel_map *m,
	unsigned channels,
	pa_channel_map_def_t def
);
	

static struct {
	const char *name;
	void **func;
} pulse_functions[] = {
	{ "pa_simple_new",
		(void **)&SDL_NAME(pa_simple_new)		},
	{ "pa_simple_free",
		(void **)&SDL_NAME(pa_simple_free)		},
	{ "pa_simple_drain",
		(void **)&SDL_NAME(pa_simple_drain)		},
	{ "pa_simple_write",
		(void **)&SDL_NAME(pa_simple_write)		},
	{ "pa_channel_map_init_auto",
		(void **)&SDL_NAME(pa_channel_map_init_auto)	},
};

static void UnloadPulseLibrary()
{
	if ( pulse_loaded ) {
		SDL_UnloadObject(pulse_handle);
		pulse_handle = NULL;
		pulse_loaded = 0;
	}
}

static int LoadPulseLibrary(void)
{
	int i, retval = -1;

	pulse_handle = SDL_LoadObject(pulse_library);
	if ( pulse_handle ) {
		pulse_loaded = 1;
		retval = 0;
		for ( i=0; i<SDL_arraysize(pulse_functions); ++i ) {
			*pulse_functions[i].func = SDL_LoadFunction(pulse_handle, pulse_functions[i].name);
			if ( !*pulse_functions[i].func ) {
				retval = -1;
				UnloadPulseLibrary();
				break;
			}
		}
	}
	return retval;
}

#else

static void UnloadPulseLibrary()
{
	return;
}

static int LoadPulseLibrary(void)
{
	return 0;
}

#endif /* SDL_AUDIO_DRIVER_PULSE_DYNAMIC */

/* Audio driver bootstrap functions */

static int Audio_Available(void)
{
	pa_sample_spec paspec;
	pa_simple *connection;
	int available;

	available = 0;
	if ( LoadPulseLibrary() < 0 ) {
		return available;
	}
	
	/* Connect with a dummy format. */
	paspec.format = PA_SAMPLE_U8;
	paspec.rate = 11025;
	paspec.channels = 1;
	connection = SDL_NAME(pa_simple_new)(
		SDL_getenv("PASERVER"),      /* server */
		"Test stream",               /* application name */
		PA_STREAM_PLAYBACK,          /* playback mode */
		SDL_getenv("PADEVICE"),      /* device on the server */
		"Simple DirectMedia Layer",  /* stream description */
		&paspec,                     /* sample format spec */
		NULL,                        /* channel map */
		NULL,                        /* buffering attributes */
		NULL                         /* error code */
	);
	if ( connection != NULL ) {
		available = 1;
		SDL_NAME(pa_simple_free)(connection);
	}
	
	UnloadPulseLibrary();
	return(available);
}

static void Audio_DeleteDevice(SDL_AudioDevice *device)
{
	SDL_free(device->hidden);
	SDL_free(device);
	UnloadPulseLibrary();
}

static SDL_AudioDevice *Audio_CreateDevice(int devindex)
{
	SDL_AudioDevice *this;

	/* Initialize all variables that we clean on shutdown */
	LoadPulseLibrary();
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
	if ( this ) {
		SDL_memset(this, 0, (sizeof *this));
		this->hidden = (struct SDL_PrivateAudioData *)
				SDL_malloc((sizeof *this->hidden));
	}
	if ( (this == NULL) || (this->hidden == NULL) ) {
		SDL_OutOfMemory();
		if ( this ) {
			SDL_free(this);
		}
		return(0);
	}
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));

	/* Set the function pointers */
	this->OpenAudio = PULSE_OpenAudio;
	this->WaitAudio = PULSE_WaitAudio;
	this->PlayAudio = PULSE_PlayAudio;
	this->GetAudioBuf = PULSE_GetAudioBuf;
	this->CloseAudio = PULSE_CloseAudio;

	this->free = Audio_DeleteDevice;

	return this;
}

AudioBootStrap PULSE_bootstrap = {
	PULSE_DRIVER_NAME, "PulseAudio",
	Audio_Available, Audio_CreateDevice
};

/* This function waits until it is possible to write a full sound buffer */
static void PULSE_WaitAudio(_THIS)
{
	/* Check to see if the thread-parent process is still alive */
	{ static int cnt = 0;
		/* Note that this only works with thread implementations 
		   that use a different process id for each thread.
		*/
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
			if ( kill(parent, 0) < 0 ) {
				this->enabled = 0;
			}
		}
	}
}

static void PULSE_PlayAudio(_THIS)
{
	/* Write the audio data */
	if ( SDL_NAME(pa_simple_write)(stream, mixbuf, mixlen, NULL) != 0 )
	{
		this->enabled = 0;
	}
}

static Uint8 *PULSE_GetAudioBuf(_THIS)
{
	return(mixbuf);
}

static void PULSE_CloseAudio(_THIS)
{
	if ( mixbuf != NULL ) {
		SDL_FreeAudioMem(mixbuf);
		mixbuf = NULL;
	}
	if ( stream != NULL ) {
		SDL_NAME(pa_simple_drain)(stream, NULL);
		SDL_NAME(pa_simple_free)(stream);
		stream = NULL;
	}
}

/* Try to get the name of the program */
static char *get_progname(void)
{
	char *progname = NULL;
#ifdef __LINUX__
	FILE *fp;
	static char temp[BUFSIZ];

	SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
	fp = fopen(temp, "r");
	if ( fp != NULL ) {
		if ( fgets(temp, sizeof(temp)-1, fp) ) {
			progname = SDL_strrchr(temp, '/');
			if ( progname == NULL ) {
				progname = temp;
			} else {
				progname = progname+1;
			}
		}
		fclose(fp);
	}
#endif
	return(progname);
}

static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec)
{
	Uint16          test_format;
	pa_sample_spec  paspec;
	pa_buffer_attr  paattr;
	pa_channel_map  pacmap;
	
	paspec.format = PA_SAMPLE_INVALID;
	for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) {
		switch ( test_format ) {
			case AUDIO_U8:
				paspec.format = PA_SAMPLE_U8;
				break;
			case AUDIO_S16LSB:
				paspec.format = PA_SAMPLE_S16LE;
				break;
			case AUDIO_S16MSB:
				paspec.format = PA_SAMPLE_S16BE;
				break;
		}
		if ( paspec.format != PA_SAMPLE_INVALID )
			break;
	}
	if (paspec.format == PA_SAMPLE_INVALID ) {
		SDL_SetError("Couldn't find any suitable audio formats");
		return(-1);
	}
	spec->format = test_format;
	
	paspec.channels = spec->channels;
	paspec.rate = spec->freq;

	/* Calculate the final parameters for this audio specification */
	SDL_CalculateAudioSpec(spec);

	/* Allocate mixing buffer */
	mixlen = spec->size;
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
	if ( mixbuf == NULL ) {
		return(-1);
	}
	SDL_memset(mixbuf, spec->silence, spec->size);
	
	/* Reduced prebuffering compared to the defaults. */
	paattr.tlength = mixlen;
	paattr.minreq = mixlen;
	paattr.fragsize = mixlen;
	paattr.prebuf = mixlen;
	paattr.maxlength = mixlen * 4;
	
	/* The SDL ALSA output hints us that we use Windows' channel mapping */
	/* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
	SDL_NAME(pa_channel_map_init_auto)(
		&pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX);
	
	/* Connect to the PulseAudio server */
	stream = SDL_NAME(pa_simple_new)(
		SDL_getenv("PASERVER"),      /* server */
		get_progname(),              /* application name */
		PA_STREAM_PLAYBACK,          /* playback mode */
		SDL_getenv("PADEVICE"),      /* device on the server */
		"Simple DirectMedia Layer",  /* stream description */
		&paspec,                     /* sample format spec */
		&pacmap,                     /* channel map */
		&paattr,                     /* buffering attributes */
		NULL                         /* error code */
	);
	if ( stream == NULL ) {
		PULSE_CloseAudio(this);
		SDL_SetError("Could not connect to PulseAudio");
		return(-1);
	}

	/* Get the parent process id (we're the parent of the audio thread) */
	parent = getpid();
	
	return(0);
}