2007-08-20 01:02:37 +00:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2013-02-15 08:47:44 -08:00
|
|
|
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
|
2011-04-08 13:03:26 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2007-08-20 01:02:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
The PulseAudio target for SDL 1.3 is based on the 1.3 arts target, with
|
|
|
|
the appropriate parts replaced with the 1.2 PulseAudio target code. This
|
|
|
|
was the cleanest way to move it to 1.3. The 1.2 target was written by
|
|
|
|
Stéphan Kochen: stephan .a.t. kochen.nl
|
|
|
|
*/
|
|
|
|
#include "SDL_config.h"
|
|
|
|
|
2011-03-12 13:21:57 -08:00
|
|
|
#if SDL_AUDIO_DRIVER_PULSEAUDIO
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
/* Allow access to a raw mixing buffer */
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
2010-01-17 06:14:04 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
2007-08-20 01:02:37 +00:00
|
|
|
#include <pulse/simple.h>
|
|
|
|
|
|
|
|
#include "SDL_timer.h"
|
|
|
|
#include "SDL_audio.h"
|
|
|
|
#include "../SDL_audiomem.h"
|
|
|
|
#include "../SDL_audio_c.h"
|
|
|
|
#include "SDL_pulseaudio.h"
|
|
|
|
#include "SDL_loadso.h"
|
|
|
|
|
2010-01-17 06:14:04 +00:00
|
|
|
#if (PA_API_VERSION < 12)
|
|
|
|
/** Return non-zero if the passed state is one of the connected states */
|
|
|
|
static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
|
|
|
|
return
|
|
|
|
x == PA_CONTEXT_CONNECTING ||
|
|
|
|
x == PA_CONTEXT_AUTHORIZING ||
|
|
|
|
x == PA_CONTEXT_SETTING_NAME ||
|
|
|
|
x == PA_CONTEXT_READY;
|
|
|
|
}
|
|
|
|
/** Return non-zero if the passed state is one of the connected states */
|
|
|
|
static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
|
|
|
|
return
|
|
|
|
x == PA_STREAM_CREATING ||
|
|
|
|
x == PA_STREAM_READY;
|
|
|
|
}
|
|
|
|
#endif /* pulseaudio <= 0.9.10 */
|
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
|
|
|
|
static pa_simple *(*PULSEAUDIO_pa_simple_new) (const char *, const char *,
|
|
|
|
pa_stream_direction_t, const char *, const char *, const pa_sample_spec *,
|
|
|
|
const pa_channel_map *, const pa_buffer_attr *, int *);
|
|
|
|
static void (*PULSEAUDIO_pa_simple_free) (pa_simple *);
|
|
|
|
static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
|
|
|
|
pa_channel_map *, unsigned, pa_channel_map_def_t);
|
|
|
|
static const char * (*PULSEAUDIO_pa_strerror) (int);
|
|
|
|
static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (void);
|
|
|
|
static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
|
|
|
|
static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int, int *);
|
|
|
|
static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
|
|
|
|
|
|
|
|
static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
|
|
|
|
pa_operation *);
|
|
|
|
static void (*PULSEAUDIO_pa_operation_cancel) (pa_operation *);
|
|
|
|
static void (*PULSEAUDIO_pa_operation_unref) (pa_operation *);
|
|
|
|
|
|
|
|
static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
|
|
|
|
const char *);
|
|
|
|
static int (*PULSEAUDIO_pa_context_connect) (pa_context *, const char *,
|
|
|
|
pa_context_flags_t, const pa_spawn_api *);
|
|
|
|
static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
|
|
|
|
static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
|
|
|
|
static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
|
|
|
|
|
|
|
|
static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *, const char *,
|
|
|
|
const pa_sample_spec *, const pa_channel_map *);
|
|
|
|
static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *, const char *,
|
|
|
|
const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
|
|
|
|
static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
|
|
|
|
static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
|
|
|
|
static int (*PULSEAUDIO_pa_stream_write) (pa_stream *, const void *, size_t,
|
|
|
|
pa_free_cb_t, int64_t, pa_seek_mode_t);
|
|
|
|
static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
|
|
|
|
pa_stream_success_cb_t, void *);
|
|
|
|
static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
|
|
|
|
static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
|
|
|
|
|
|
|
|
static int load_pulseaudio_syms(void);
|
|
|
|
|
|
|
|
|
2011-11-03 11:51:47 -04:00
|
|
|
#ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
|
2010-01-17 06:43:28 +00:00
|
|
|
|
|
|
|
static const char *pulseaudio_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
|
|
|
|
static void *pulseaudio_handle = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
load_pulseaudio_sym(const char *fn, void **addr)
|
2007-09-10 12:11:40 +00:00
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
*addr = SDL_LoadFunction(pulseaudio_handle, fn);
|
|
|
|
if (*addr == NULL) {
|
|
|
|
/* Don't call SDL_SetError(): SDL_LoadFunction already did. */
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-10 12:11:40 +00:00
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
|
|
|
|
#define SDL_PULSEAUDIO_SYM(x) \
|
|
|
|
if (!load_pulseaudio_sym(#x, (void **) (char *) &PULSEAUDIO_##x)) return -1
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
static void
|
2010-01-17 06:43:28 +00:00
|
|
|
UnloadPulseAudioLibrary(void)
|
2007-08-20 01:02:37 +00:00
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
if (pulseaudio_handle != NULL) {
|
|
|
|
SDL_UnloadObject(pulseaudio_handle);
|
|
|
|
pulseaudio_handle = NULL;
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-01-17 06:43:28 +00:00
|
|
|
LoadPulseAudioLibrary(void)
|
2007-08-20 01:02:37 +00:00
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
int retval = 0;
|
|
|
|
if (pulseaudio_handle == NULL) {
|
|
|
|
pulseaudio_handle = SDL_LoadObject(pulseaudio_library);
|
|
|
|
if (pulseaudio_handle == NULL) {
|
|
|
|
retval = -1;
|
|
|
|
/* Don't call SDL_SetError(): SDL_LoadObject already did. */
|
|
|
|
} else {
|
|
|
|
retval = load_pulseaudio_syms();
|
|
|
|
if (retval < 0) {
|
|
|
|
UnloadPulseAudioLibrary();
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
#define SDL_PULSEAUDIO_SYM(x) PULSEAUDIO_##x = x
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
static void
|
2010-01-17 06:43:28 +00:00
|
|
|
UnloadPulseAudioLibrary(void)
|
2007-08-20 01:02:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-01-17 06:43:28 +00:00
|
|
|
LoadPulseAudioLibrary(void)
|
2007-08-20 01:02:37 +00:00
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
load_pulseaudio_syms();
|
2007-08-20 01:02:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC */
|
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
load_pulseaudio_syms(void)
|
|
|
|
{
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_simple_new);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_simple_free);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_mainloop_new);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_mainloop_free);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_operation_get_state);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_operation_cancel);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_operation_unref);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_context_new);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_context_connect);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_context_get_state);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_context_disconnect);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_context_unref);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_new);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_get_state);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_write);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_drain);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_stream_unref);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
|
|
|
|
SDL_PULSEAUDIO_SYM(pa_strerror);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-26 16:31:52 -08:00
|
|
|
/* Check to see if we can connect to PulseAudio */
|
|
|
|
static SDL_bool
|
|
|
|
CheckPulseAudioAvailable()
|
|
|
|
{
|
|
|
|
pa_simple *s;
|
|
|
|
pa_sample_spec ss;
|
|
|
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
ss.channels = 1;
|
|
|
|
ss.rate = 22050;
|
|
|
|
|
|
|
|
s = PULSEAUDIO_pa_simple_new(NULL, "SDL", PA_STREAM_PLAYBACK, NULL,
|
|
|
|
"Test", &ss, NULL, NULL, NULL);
|
|
|
|
if (s) {
|
|
|
|
PULSEAUDIO_pa_simple_free(s);
|
|
|
|
return SDL_TRUE;
|
|
|
|
} else {
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
/* This function waits until it is possible to write a full sound buffer */
|
|
|
|
static void
|
|
|
|
PULSEAUDIO_WaitDevice(_THIS)
|
|
|
|
{
|
2010-01-17 06:51:31 +00:00
|
|
|
struct SDL_PrivateAudioData *h = this->hidden;
|
|
|
|
|
2010-01-17 06:14:04 +00:00
|
|
|
while(1) {
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
|
|
|
|
PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
|
|
|
|
PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
|
2010-01-17 06:14:04 +00:00
|
|
|
this->enabled = 0;
|
|
|
|
return;
|
|
|
|
}
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_stream_writable_size(h->stream) >= h->mixlen) {
|
2010-01-17 06:14:04 +00:00
|
|
|
return;
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PULSEAUDIO_PlayDevice(_THIS)
|
|
|
|
{
|
|
|
|
/* Write the audio data */
|
2010-01-17 06:51:31 +00:00
|
|
|
struct SDL_PrivateAudioData *h = this->hidden;
|
|
|
|
if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL,
|
2010-01-17 06:14:04 +00:00
|
|
|
PA_SEEK_RELATIVE) < 0) {
|
2007-08-20 01:02:37 +00:00
|
|
|
this->enabled = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-17 06:14:04 +00:00
|
|
|
static void
|
|
|
|
stream_drain_complete(pa_stream *s, int success, void *userdata)
|
|
|
|
{
|
|
|
|
/* no-op for pa_stream_drain() to use for callback. */
|
|
|
|
}
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
static void
|
|
|
|
PULSEAUDIO_WaitDone(_THIS)
|
|
|
|
{
|
2010-01-17 06:51:31 +00:00
|
|
|
struct SDL_PrivateAudioData *h = this->hidden;
|
2010-01-17 06:14:04 +00:00
|
|
|
pa_operation *o;
|
|
|
|
|
2010-01-17 06:51:31 +00:00
|
|
|
o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
|
2010-01-17 06:14:04 +00:00
|
|
|
if (!o) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
|
|
|
|
PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
|
|
|
|
PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_operation_cancel(o);
|
2010-01-17 06:14:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_operation_unref(o);
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-17 06:14:04 +00:00
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
static Uint8 *
|
|
|
|
PULSEAUDIO_GetDeviceBuf(_THIS)
|
|
|
|
{
|
|
|
|
return (this->hidden->mixbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
PULSEAUDIO_CloseDevice(_THIS)
|
|
|
|
{
|
|
|
|
if (this->hidden != NULL) {
|
|
|
|
if (this->hidden->mixbuf != NULL) {
|
|
|
|
SDL_FreeAudioMem(this->hidden->mixbuf);
|
|
|
|
this->hidden->mixbuf = NULL;
|
|
|
|
}
|
|
|
|
if (this->hidden->stream) {
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
|
|
|
|
PULSEAUDIO_pa_stream_unref(this->hidden->stream);
|
2007-08-20 01:02:37 +00:00
|
|
|
this->hidden->stream = NULL;
|
|
|
|
}
|
2010-01-17 06:14:04 +00:00
|
|
|
if (this->hidden->context != NULL) {
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_context_disconnect(this->hidden->context);
|
|
|
|
PULSEAUDIO_pa_context_unref(this->hidden->context);
|
2010-01-17 06:14:04 +00:00
|
|
|
this->hidden->context = NULL;
|
|
|
|
}
|
|
|
|
if (this->hidden->mainloop != NULL) {
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_mainloop_free(this->hidden->mainloop);
|
2010-01-17 06:14:04 +00:00
|
|
|
this->hidden->mainloop = NULL;
|
|
|
|
}
|
2007-08-20 01:02:37 +00:00
|
|
|
SDL_free(this->hidden);
|
|
|
|
this->hidden = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
|
|
|
|
{
|
2010-01-17 06:51:31 +00:00
|
|
|
struct SDL_PrivateAudioData *h = NULL;
|
2007-08-20 02:08:37 +00:00
|
|
|
Uint16 test_format = 0;
|
|
|
|
pa_sample_spec paspec;
|
|
|
|
pa_buffer_attr paattr;
|
|
|
|
pa_channel_map pacmap;
|
2010-01-17 06:14:04 +00:00
|
|
|
pa_stream_flags_t flags = 0;
|
|
|
|
int state = 0;
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
/* Initialize all variables that we clean on shutdown */
|
|
|
|
this->hidden = (struct SDL_PrivateAudioData *)
|
|
|
|
SDL_malloc((sizeof *this->hidden));
|
|
|
|
if (this->hidden == NULL) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
|
2010-01-17 06:51:31 +00:00
|
|
|
h = this->hidden;
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
paspec.format = PA_SAMPLE_INVALID;
|
|
|
|
|
|
|
|
/* Try for a closest match on audio format */
|
|
|
|
for (test_format = SDL_FirstAudioFormat(this->spec.format);
|
|
|
|
(paspec.format == PA_SAMPLE_INVALID) && test_format;) {
|
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
|
|
|
|
#endif
|
2007-09-10 12:11:40 +00:00
|
|
|
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;
|
|
|
|
default:
|
|
|
|
paspec.format = PA_SAMPLE_INVALID;
|
|
|
|
break;
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
if (paspec.format == PA_SAMPLE_INVALID) {
|
|
|
|
test_format = SDL_NextAudioFormat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (paspec.format == PA_SAMPLE_INVALID) {
|
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("Couldn't find any hardware audio formats");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
this->spec.format = test_format;
|
|
|
|
|
|
|
|
/* Calculate the final parameters for this audio specification */
|
2010-01-17 06:14:04 +00:00
|
|
|
#ifdef PA_STREAM_ADJUST_LATENCY
|
|
|
|
this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */
|
|
|
|
#endif
|
2007-08-20 01:02:37 +00:00
|
|
|
SDL_CalculateAudioSpec(&this->spec);
|
|
|
|
|
|
|
|
/* Allocate mixing buffer */
|
2010-01-17 06:51:31 +00:00
|
|
|
h->mixlen = this->spec.size;
|
|
|
|
h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen);
|
|
|
|
if (h->mixbuf == NULL) {
|
2007-08-20 01:02:37 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-17 06:51:31 +00:00
|
|
|
SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
paspec.channels = this->spec.channels;
|
|
|
|
paspec.rate = this->spec.freq;
|
|
|
|
|
|
|
|
/* Reduced prebuffering compared to the defaults. */
|
2010-01-17 06:14:04 +00:00
|
|
|
#ifdef PA_STREAM_ADJUST_LATENCY
|
2010-01-17 06:51:31 +00:00
|
|
|
/* 2x original requested bufsize */
|
|
|
|
paattr.tlength = h->mixlen * 4;
|
2010-01-17 06:14:04 +00:00
|
|
|
paattr.prebuf = -1;
|
|
|
|
paattr.maxlength = -1;
|
2010-01-17 06:51:31 +00:00
|
|
|
/* -1 can lead to pa_stream_writable_size() >= mixlen never being true */
|
|
|
|
paattr.minreq = h->mixlen;
|
2010-01-17 06:14:04 +00:00
|
|
|
flags = PA_STREAM_ADJUST_LATENCY;
|
|
|
|
#else
|
2010-01-17 06:51:31 +00:00
|
|
|
paattr.tlength = h->mixlen*2;
|
|
|
|
paattr.prebuf = h->mixlen*2;
|
|
|
|
paattr.maxlength = h->mixlen*2;
|
|
|
|
paattr.minreq = h->mixlen;
|
2010-01-17 06:14:04 +00:00
|
|
|
#endif
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
/* The SDL ALSA output hints us that we use Windows' channel mapping */
|
|
|
|
/* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
|
2010-01-17 06:43:28 +00:00
|
|
|
PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels,
|
2007-09-10 12:11:40 +00:00
|
|
|
PA_CHANNEL_MAP_WAVEEX);
|
2007-08-20 01:02:37 +00:00
|
|
|
|
2010-01-17 06:14:04 +00:00
|
|
|
/* Set up a new main loop */
|
2010-01-17 06:51:31 +00:00
|
|
|
if (!(h->mainloop = PULSEAUDIO_pa_mainloop_new())) {
|
2010-01-17 06:14:04 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("pa_mainloop_new() failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-17 06:51:31 +00:00
|
|
|
h->mainloop_api = PULSEAUDIO_pa_mainloop_get_api(h->mainloop);
|
Fixed bug #1011
Daniel Ellis 2010-06-25 15:20:31 PDT
SDL based applications sometimes display the wrong application name in the
Sound Preferences dialog when using pulseaudio.
I can see from the code that the SDL pulse module is initiating a new pulse
audio context and passing an application name using the function
get_progname().
The get_progname() function returns the name of the current process. However,
the process name is often not a suitable name to use. For example, the OpenShot
video editor is a python application, and so "python" is displayed in the Sound
Preferences window (see Bug #596504), when it should be displaying "OpenShot".
PulseAudio allows applications to specify the application name, either at the
time the context is created (as SDL does currently), or by special environment
variables (see http://www.pulseaudio.org/wiki/ApplicationProperties). If no
name is specified, then pulseaudio will determine the name based on the
process.
If you specify the application name when initiating the pulseaudio context,
then that will override any application name specified using an environment
variable.
As libsdl is a library, I believe the solution is for libsdl to not specify any
application name when initiating a pulseaudio context, which will enable
applications to specify the application name using environment variables. In
the case that the applications do not specify anything, pulseaudio will fall
back to using the process name anyway.
The attached patch removes the get_progname() function and passes NULL as the
application name when creating the pulseaudio context, which fixes the issue.
2011-01-23 21:55:04 -08:00
|
|
|
h->context = PULSEAUDIO_pa_context_new(h->mainloop_api, NULL);
|
2010-01-17 06:51:31 +00:00
|
|
|
if (!h->context) {
|
2010-01-17 06:14:04 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("pa_context_new() failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
/* Connect to the PulseAudio server */
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_context_connect(h->context, NULL, 0, NULL) < 0) {
|
2010-01-17 06:14:04 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("Could not setup connection to PulseAudio");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
|
2010-01-17 06:14:04 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("pa_mainloop_iterate() failed");
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-17 06:51:31 +00:00
|
|
|
state = PULSEAUDIO_pa_context_get_state(h->context);
|
2010-01-17 06:14:04 +00:00
|
|
|
if (!PA_CONTEXT_IS_GOOD(state)) {
|
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("Could not connect to PulseAudio");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (state != PA_CONTEXT_READY);
|
|
|
|
|
2010-01-17 06:51:31 +00:00
|
|
|
h->stream = PULSEAUDIO_pa_stream_new(
|
|
|
|
h->context,
|
2010-01-17 06:14:04 +00:00
|
|
|
"Simple DirectMedia Layer", /* stream description */
|
|
|
|
&paspec, /* sample format spec */
|
|
|
|
&pacmap /* channel map */
|
2007-09-10 12:11:40 +00:00
|
|
|
);
|
|
|
|
|
2010-01-17 06:51:31 +00:00
|
|
|
if (h->stream == NULL) {
|
2007-08-20 01:02:37 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
2010-01-17 06:14:04 +00:00
|
|
|
SDL_SetError("Could not set up PulseAudio stream");
|
2007-08-20 02:08:37 +00:00
|
|
|
return 0;
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_stream_connect_playback(h->stream, NULL, &paattr, flags,
|
2010-01-17 06:14:04 +00:00
|
|
|
NULL, NULL) < 0) {
|
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("Could not connect PulseAudio stream");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2010-01-17 06:51:31 +00:00
|
|
|
if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
|
2010-01-17 06:14:04 +00:00
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("pa_mainloop_iterate() failed");
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-17 06:51:31 +00:00
|
|
|
state = PULSEAUDIO_pa_stream_get_state(h->stream);
|
2010-01-17 06:14:04 +00:00
|
|
|
if (!PA_STREAM_IS_GOOD(state)) {
|
|
|
|
PULSEAUDIO_CloseDevice(this);
|
|
|
|
SDL_SetError("Could not create to PulseAudio stream");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (state != PA_STREAM_READY);
|
2007-08-20 01:02:37 +00:00
|
|
|
|
|
|
|
/* We're ready to rock and roll. :-) */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
PULSEAUDIO_Deinitialize(void)
|
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
UnloadPulseAudioLibrary();
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
|
|
|
|
{
|
2010-01-17 06:43:28 +00:00
|
|
|
if (LoadPulseAudioLibrary() < 0) {
|
2007-08-20 01:02:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-26 16:31:52 -08:00
|
|
|
if (!CheckPulseAudioAvailable()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
/* Set the function pointers */
|
|
|
|
impl->OpenDevice = PULSEAUDIO_OpenDevice;
|
|
|
|
impl->PlayDevice = PULSEAUDIO_PlayDevice;
|
|
|
|
impl->WaitDevice = PULSEAUDIO_WaitDevice;
|
|
|
|
impl->GetDeviceBuf = PULSEAUDIO_GetDeviceBuf;
|
|
|
|
impl->CloseDevice = PULSEAUDIO_CloseDevice;
|
|
|
|
impl->WaitDone = PULSEAUDIO_WaitDone;
|
|
|
|
impl->Deinitialize = PULSEAUDIO_Deinitialize;
|
|
|
|
impl->OnlyHasDefaultOutputDevice = 1;
|
|
|
|
|
2010-01-26 06:01:33 +00:00
|
|
|
return 1; /* this audio target is available. */
|
2007-08-20 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AudioBootStrap PULSEAUDIO_bootstrap = {
|
2011-07-26 14:18:00 -07:00
|
|
|
"pulseaudio", "PulseAudio", PULSEAUDIO_Init, 0
|
2007-08-20 01:02:37 +00:00
|
|
|
};
|
|
|
|
|
2011-03-12 13:21:57 -08:00
|
|
|
#endif /* SDL_AUDIO_DRIVER_PULSEAUDIO */
|
|
|
|
|
2007-08-20 01:02:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|