src/audio/pulseaudio/SDL_pulseaudio.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 22:01:24 -0700
changeset 7397 08e4eb5cea46
parent 7191 75360622e65f
child 7398 31ec14665b79
permissions -rw-r--r--
Fixed bug 1949 - Pulseaudio 32 bit audio formats support Matt Scheirer Pulse has supported (since version 0.8, at least) 32 bit audio formats that are now becoming available in SDL2. This patch adds those format conversions to the switch clause in the pulseaudio backend.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     7
  arising from the use of this software.
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    11
  freely, subject to the following restrictions:
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
*/
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
/*
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
  The PulseAudio target for SDL 1.3 is based on the 1.3 arts target, with
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
   the appropriate parts replaced with the 1.2 PulseAudio target code. This
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
   was the cleanest way to move it to 1.3. The 1.2 target was written by
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
   St├ęphan Kochen: stephan .a.t. kochen.nl
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
*/
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    28
#include "SDL_config.h"
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    30
#if SDL_AUDIO_DRIVER_PULSEAUDIO
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
/* Allow access to a raw mixing buffer */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#ifdef HAVE_SIGNAL_H
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#include <signal.h>
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#endif
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#include <unistd.h>
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#include <sys/types.h>
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#include <errno.h>
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    40
#include <pulse/pulseaudio.h>
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#include <pulse/simple.h>
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#include "SDL_timer.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#include "SDL_audio.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
#include "../SDL_audiomem.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#include "../SDL_audio_c.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#include "SDL_pulseaudio.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
#include "SDL_loadso.h"
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    50
#if (PA_API_VERSION < 12)
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    51
/** Return non-zero if the passed state is one of the connected states */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    52
static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    53
    return
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    54
        x == PA_CONTEXT_CONNECTING ||
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    55
        x == PA_CONTEXT_AUTHORIZING ||
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    56
        x == PA_CONTEXT_SETTING_NAME ||
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    57
        x == PA_CONTEXT_READY;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    58
}
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    59
/** Return non-zero if the passed state is one of the connected states */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    60
static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    61
    return
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    62
        x == PA_STREAM_CREATING ||
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    63
        x == PA_STREAM_READY;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    64
}
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    65
#endif /* pulseaudio <= 0.9.10 */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    66
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    68
static pa_simple *(*PULSEAUDIO_pa_simple_new) (const char *, const char *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    69
    pa_stream_direction_t, const char *, const char *, const pa_sample_spec *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    70
    const pa_channel_map *, const pa_buffer_attr *, int *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    71
static void (*PULSEAUDIO_pa_simple_free) (pa_simple *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    72
static pa_channel_map *(*PULSEAUDIO_pa_channel_map_init_auto) (
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    73
    pa_channel_map *, unsigned, pa_channel_map_def_t);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    74
static const char * (*PULSEAUDIO_pa_strerror) (int);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    75
static pa_mainloop * (*PULSEAUDIO_pa_mainloop_new) (void);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    76
static pa_mainloop_api * (*PULSEAUDIO_pa_mainloop_get_api) (pa_mainloop *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    77
static int (*PULSEAUDIO_pa_mainloop_iterate) (pa_mainloop *, int, int *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    78
static void (*PULSEAUDIO_pa_mainloop_free) (pa_mainloop *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    79
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    80
static pa_operation_state_t (*PULSEAUDIO_pa_operation_get_state) (
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    81
    pa_operation *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    82
static void (*PULSEAUDIO_pa_operation_cancel) (pa_operation *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    83
static void (*PULSEAUDIO_pa_operation_unref) (pa_operation *);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    84
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    85
static pa_context * (*PULSEAUDIO_pa_context_new) (pa_mainloop_api *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    86
    const char *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    87
static int (*PULSEAUDIO_pa_context_connect) (pa_context *, const char *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    88
    pa_context_flags_t, const pa_spawn_api *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    89
static pa_context_state_t (*PULSEAUDIO_pa_context_get_state) (pa_context *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    90
static void (*PULSEAUDIO_pa_context_disconnect) (pa_context *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    91
static void (*PULSEAUDIO_pa_context_unref) (pa_context *);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    92
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    93
static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *, const char *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    94
    const pa_sample_spec *, const pa_channel_map *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    95
static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *, const char *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    96
    const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    97
static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    98
static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
    99
static int (*PULSEAUDIO_pa_stream_write) (pa_stream *, const void *, size_t,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   100
    pa_free_cb_t, int64_t, pa_seek_mode_t);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   101
static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   102
    pa_stream_success_cb_t, void *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   103
static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   104
static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   105
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   106
static int load_pulseaudio_syms(void);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
   109
#ifdef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   110
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   111
static const char *pulseaudio_library = SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   112
static void *pulseaudio_handle = NULL;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   113
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   114
static int
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   115
load_pulseaudio_sym(const char *fn, void **addr)
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   116
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   117
    *addr = SDL_LoadFunction(pulseaudio_handle, fn);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   118
    if (*addr == NULL) {
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   119
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   120
        return 0;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   121
    }
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   122
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   123
    return 1;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   124
}
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   125
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   126
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   127
#define SDL_PULSEAUDIO_SYM(x) \
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   128
    if (!load_pulseaudio_sym(#x, (void **) (char *) &PULSEAUDIO_##x)) return -1
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
static void
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   131
UnloadPulseAudioLibrary(void)
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   133
    if (pulseaudio_handle != NULL) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   134
        SDL_UnloadObject(pulseaudio_handle);
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   135
        pulseaudio_handle = NULL;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
static int
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   140
LoadPulseAudioLibrary(void)
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   142
    int retval = 0;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   143
    if (pulseaudio_handle == NULL) {
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   144
        pulseaudio_handle = SDL_LoadObject(pulseaudio_library);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   145
        if (pulseaudio_handle == NULL) {
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   146
            retval = -1;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   147
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   148
        } else {
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   149
            retval = load_pulseaudio_syms();
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   150
            if (retval < 0) {
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   151
                UnloadPulseAudioLibrary();
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
            }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    return retval;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
#else
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   160
#define SDL_PULSEAUDIO_SYM(x) PULSEAUDIO_##x = x
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   161
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
static void
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   163
UnloadPulseAudioLibrary(void)
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
{
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
static int
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   168
LoadPulseAudioLibrary(void)
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   170
    load_pulseaudio_syms();
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    return 0;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
#endif /* SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   176
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   177
static int
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   178
load_pulseaudio_syms(void)
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   179
{
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   180
    SDL_PULSEAUDIO_SYM(pa_simple_new);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   181
    SDL_PULSEAUDIO_SYM(pa_simple_free);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   182
    SDL_PULSEAUDIO_SYM(pa_mainloop_new);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   183
    SDL_PULSEAUDIO_SYM(pa_mainloop_get_api);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   184
    SDL_PULSEAUDIO_SYM(pa_mainloop_iterate);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   185
    SDL_PULSEAUDIO_SYM(pa_mainloop_free);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   186
    SDL_PULSEAUDIO_SYM(pa_operation_get_state);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   187
    SDL_PULSEAUDIO_SYM(pa_operation_cancel);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   188
    SDL_PULSEAUDIO_SYM(pa_operation_unref);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   189
    SDL_PULSEAUDIO_SYM(pa_context_new);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   190
    SDL_PULSEAUDIO_SYM(pa_context_connect);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   191
    SDL_PULSEAUDIO_SYM(pa_context_get_state);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   192
    SDL_PULSEAUDIO_SYM(pa_context_disconnect);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   193
    SDL_PULSEAUDIO_SYM(pa_context_unref);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   194
    SDL_PULSEAUDIO_SYM(pa_stream_new);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   195
    SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   196
    SDL_PULSEAUDIO_SYM(pa_stream_get_state);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   197
    SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   198
    SDL_PULSEAUDIO_SYM(pa_stream_write);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   199
    SDL_PULSEAUDIO_SYM(pa_stream_drain);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   200
    SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   201
    SDL_PULSEAUDIO_SYM(pa_stream_unref);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   202
    SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   203
    SDL_PULSEAUDIO_SYM(pa_strerror);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   204
    return 0;
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   205
}
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   206
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   207
6933
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   208
/* Check to see if we can connect to PulseAudio */
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   209
static SDL_bool
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   210
CheckPulseAudioAvailable()
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   211
{
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   212
    pa_simple *s;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   213
    pa_sample_spec ss;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   214
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   215
    ss.format = PA_SAMPLE_S16NE;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   216
    ss.channels = 1;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   217
    ss.rate = 22050;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   218
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   219
    s = PULSEAUDIO_pa_simple_new(NULL, "SDL", PA_STREAM_PLAYBACK, NULL,
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   220
                                 "Test", &ss, NULL, NULL, NULL);
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   221
    if (s) {
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   222
        PULSEAUDIO_pa_simple_free(s);
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   223
        return SDL_TRUE;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   224
    } else {
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   225
        return SDL_FALSE;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   226
    }
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   227
}
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   228
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
/* This function waits until it is possible to write a full sound buffer */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
static void
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
PULSEAUDIO_WaitDevice(_THIS)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
{
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   233
    struct SDL_PrivateAudioData *h = this->hidden;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   234
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   235
    while(1) {
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   236
        if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   237
            PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   238
            PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   239
            this->enabled = 0;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   240
            return;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
        }
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   242
        if (PULSEAUDIO_pa_stream_writable_size(h->stream) >= h->mixlen) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   243
            return;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   244
        }
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
static void
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
PULSEAUDIO_PlayDevice(_THIS)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
{
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    /* Write the audio data */
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   252
    struct SDL_PrivateAudioData *h = this->hidden;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   253
    if (PULSEAUDIO_pa_stream_write(h->stream, h->mixbuf, h->mixlen, NULL, 0LL,
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   254
                                   PA_SEEK_RELATIVE) < 0) {
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        this->enabled = 0;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
static void
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   260
stream_drain_complete(pa_stream *s, int success, void *userdata)
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   261
{
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   262
    /* no-op for pa_stream_drain() to use for callback. */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   263
}
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   264
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   265
static void
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
PULSEAUDIO_WaitDone(_THIS)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
{
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   268
    struct SDL_PrivateAudioData *h = this->hidden;
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   269
    pa_operation *o;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   270
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   271
    o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   272
    if (!o) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   273
        return;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   274
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   275
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   276
    while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   277
        if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   278
            PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   279
            PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   280
            PULSEAUDIO_pa_operation_cancel(o);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   281
            break;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   282
        }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   283
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   284
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   285
    PULSEAUDIO_pa_operation_unref(o);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   289
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
static Uint8 *
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
PULSEAUDIO_GetDeviceBuf(_THIS)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
{
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    return (this->hidden->mixbuf);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
static void
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
PULSEAUDIO_CloseDevice(_THIS)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
{
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    if (this->hidden != NULL) {
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        if (this->hidden->mixbuf != NULL) {
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
            SDL_FreeAudioMem(this->hidden->mixbuf);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
            this->hidden->mixbuf = NULL;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
        }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        if (this->hidden->stream) {
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   306
            PULSEAUDIO_pa_stream_disconnect(this->hidden->stream);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   307
            PULSEAUDIO_pa_stream_unref(this->hidden->stream);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
            this->hidden->stream = NULL;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        }
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   310
        if (this->hidden->context != NULL) {
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   311
            PULSEAUDIO_pa_context_disconnect(this->hidden->context);
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   312
            PULSEAUDIO_pa_context_unref(this->hidden->context);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   313
            this->hidden->context = NULL;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   314
        }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   315
        if (this->hidden->mainloop != NULL) {
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   316
            PULSEAUDIO_pa_mainloop_free(this->hidden->mainloop);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   317
            this->hidden->mainloop = NULL;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   318
        }
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        SDL_free(this->hidden);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        this->hidden = NULL;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
static int
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
PULSEAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
{
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   328
    struct SDL_PrivateAudioData *h = NULL;
2272
25a87553a59d Minor PulseAudio fixes: corrected OpenDevice return code, human-readable
Ryan C. Gordon <icculus@icculus.org>
parents: 2271
diff changeset
   329
    Uint16 test_format = 0;
25a87553a59d Minor PulseAudio fixes: corrected OpenDevice return code, human-readable
Ryan C. Gordon <icculus@icculus.org>
parents: 2271
diff changeset
   330
    pa_sample_spec paspec;
25a87553a59d Minor PulseAudio fixes: corrected OpenDevice return code, human-readable
Ryan C. Gordon <icculus@icculus.org>
parents: 2271
diff changeset
   331
    pa_buffer_attr paattr;
25a87553a59d Minor PulseAudio fixes: corrected OpenDevice return code, human-readable
Ryan C. Gordon <icculus@icculus.org>
parents: 2271
diff changeset
   332
    pa_channel_map pacmap;
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   333
    pa_stream_flags_t flags = 0;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   334
    int state = 0;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    /* Initialize all variables that we clean on shutdown */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    this->hidden = (struct SDL_PrivateAudioData *)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        SDL_malloc((sizeof *this->hidden));
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   340
        return SDL_OutOfMemory();
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   343
    h = this->hidden;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    paspec.format = PA_SAMPLE_INVALID;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    /* Try for a closest match on audio format */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
         (paspec.format == PA_SAMPLE_INVALID) && test_format;) {
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
#ifdef DEBUG_AUDIO
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
#endif
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   353
        switch (test_format) {
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   354
        case AUDIO_U8:
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   355
            paspec.format = PA_SAMPLE_U8;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   356
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   357
        case AUDIO_S16LSB:
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   358
            paspec.format = PA_SAMPLE_S16LE;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   359
            break;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   360
        case AUDIO_S16MSB:
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   361
            paspec.format = PA_SAMPLE_S16BE;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   362
            break;
7397
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   363
        case AUDIO_S32LSB:
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   364
            paspec.format = PA_SAMPLE_S32LE;
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   365
            break;
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   366
        case AUDIO_S32MSB:
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   367
            paspec.format = PA_SAMPLE_S32BE;
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   368
            break;
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   369
        case AUDIO_F32LSB:
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   370
            paspec.format = PA_SAMPLE_FLOAT32LE
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   371
            break;
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   372
        case AUDIO_F32MSB:
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   373
            paspec.format = PA_SAMPLE_FLOAT32BE
08e4eb5cea46 Fixed bug 1949 - Pulseaudio 32 bit audio formats support
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   374
            break;
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   375
        default:
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   376
            paspec.format = PA_SAMPLE_INVALID;
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   377
            break;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
        }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
        if (paspec.format == PA_SAMPLE_INVALID) {
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
            test_format = SDL_NextAudioFormat();
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
        }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
    if (paspec.format == PA_SAMPLE_INVALID) {
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   385
        return SDL_SetError("Couldn't find any hardware audio formats");
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    this->spec.format = test_format;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    /* Calculate the final parameters for this audio specification */
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   390
#ifdef PA_STREAM_ADJUST_LATENCY
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   391
    this->spec.samples /= 2; /* Mix in smaller chunck to avoid underruns */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   392
#endif
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    SDL_CalculateAudioSpec(&this->spec);
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
    /* Allocate mixing buffer */
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   396
    h->mixlen = this->spec.size;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   397
    h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen);
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   398
    if (h->mixbuf == NULL) {
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   400
        return SDL_OutOfMemory();
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
    }
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   402
    SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
    paspec.channels = this->spec.channels;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    paspec.rate = this->spec.freq;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
    /* Reduced prebuffering compared to the defaults. */
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   408
#ifdef PA_STREAM_ADJUST_LATENCY
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   409
    /* 2x original requested bufsize */
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   410
    paattr.tlength = h->mixlen * 4;
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   411
    paattr.prebuf = -1;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   412
    paattr.maxlength = -1;
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   413
    /* -1 can lead to pa_stream_writable_size() >= mixlen never being true */
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   414
    paattr.minreq = h->mixlen;
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   415
    flags = PA_STREAM_ADJUST_LATENCY;
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   416
#else
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   417
    paattr.tlength = h->mixlen*2;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   418
    paattr.prebuf = h->mixlen*2;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   419
    paattr.maxlength = h->mixlen*2;
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   420
    paattr.minreq = h->mixlen;
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   421
#endif
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
    /* The SDL ALSA output hints us that we use Windows' channel mapping */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
    /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   425
    PULSEAUDIO_pa_channel_map_init_auto(&pacmap, this->spec.channels,
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   426
                                        PA_CHANNEL_MAP_WAVEEX);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   428
    /* Set up a new main loop */
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   429
    if (!(h->mainloop = PULSEAUDIO_pa_mainloop_new())) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   430
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   431
        return SDL_SetError("pa_mainloop_new() failed");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   432
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   433
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   434
    h->mainloop_api = PULSEAUDIO_pa_mainloop_get_api(h->mainloop);
5080
de59e0218aa2 Fixed bug #1011
Sam Lantinga <slouken@libsdl.org>
parents: 3699
diff changeset
   435
    h->context = PULSEAUDIO_pa_context_new(h->mainloop_api, NULL);
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   436
    if (!h->context) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   437
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   438
        return SDL_SetError("pa_context_new() failed");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   439
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   440
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
    /* Connect to the PulseAudio server */
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   442
    if (PULSEAUDIO_pa_context_connect(h->context, NULL, 0, NULL) < 0) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   443
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   444
        return SDL_SetError("Could not setup connection to PulseAudio");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   445
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   446
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   447
    do {
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   448
        if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   449
            PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   450
            return SDL_SetError("pa_mainloop_iterate() failed");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   451
        }
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   452
        state = PULSEAUDIO_pa_context_get_state(h->context);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   453
        if (!PA_CONTEXT_IS_GOOD(state)) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   454
            PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   455
            return SDL_SetError("Could not connect to PulseAudio");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   456
        }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   457
    } while (state != PA_CONTEXT_READY);
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   458
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   459
    h->stream = PULSEAUDIO_pa_stream_new(
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   460
        h->context,
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   461
        "Simple DirectMedia Layer", /* stream description */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   462
        &paspec,    /* sample format spec */
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   463
        &pacmap     /* channel map */
2274
Sam Lantinga <slouken@libsdl.org>
parents: 2272
diff changeset
   464
        );
2272
25a87553a59d Minor PulseAudio fixes: corrected OpenDevice return code, human-readable
Ryan C. Gordon <icculus@icculus.org>
parents: 2271
diff changeset
   465
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   466
    if (h->stream == NULL) {
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   468
        return SDL_SetError("Could not set up PulseAudio stream");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   469
    }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   470
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   471
    if (PULSEAUDIO_pa_stream_connect_playback(h->stream, NULL, &paattr, flags,
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   472
            NULL, NULL) < 0) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   473
        PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   474
        return SDL_SetError("Could not connect PulseAudio stream");
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   477
    do {
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   478
        if (PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   479
            PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   480
            return SDL_SetError("pa_mainloop_iterate() failed");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   481
        }
3674
7b597182d06b Minor cleanups in PulseAudio code, to keep this 80-column-friendly.
Ryan C. Gordon <icculus@icculus.org>
parents: 3673
diff changeset
   482
        state = PULSEAUDIO_pa_stream_get_state(h->stream);
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   483
        if (!PA_STREAM_IS_GOOD(state)) {
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   484
            PULSEAUDIO_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   485
            return SDL_SetError("Could not create to PulseAudio stream");
3672
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   486
        }
878d8adf2fde Merged r4210:5510 from branches/SDL-1.2/src/audio/pulse: PulseAudio updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   487
    } while (state != PA_STREAM_READY);
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
    /* We're ready to rock and roll. :-) */
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6934
diff changeset
   490
    return 0;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
static void
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
PULSEAUDIO_Deinitialize(void)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   497
    UnloadPulseAudioLibrary();
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
static int
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
{
3673
c9d5bd1b4e18 Attempt to clean up PulseAudio dynamic loading support.
Ryan C. Gordon <icculus@icculus.org>
parents: 3672
diff changeset
   503
    if (LoadPulseAudioLibrary() < 0) {
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
        return 0;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
    }
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
6933
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   507
    if (!CheckPulseAudioAvailable()) {
6934
3ce238619067 Unload the PulseAudio library if the pulseaudio connection isn't available.
Sam Lantinga <slouken@libsdl.org>
parents: 6933
diff changeset
   508
        UnloadPulseAudioLibrary();
6933
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   509
        return 0;
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   510
    }
e56ed2eb139e Check to make sure we can connect to PulseAudio before we use it.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   511
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    /* Set the function pointers */
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
    impl->OpenDevice = PULSEAUDIO_OpenDevice;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
    impl->PlayDevice = PULSEAUDIO_PlayDevice;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
    impl->WaitDevice = PULSEAUDIO_WaitDevice;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
    impl->GetDeviceBuf = PULSEAUDIO_GetDeviceBuf;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    impl->CloseDevice = PULSEAUDIO_CloseDevice;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    impl->WaitDone = PULSEAUDIO_WaitDone;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    impl->Deinitialize = PULSEAUDIO_Deinitialize;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    impl->OnlyHasDefaultOutputDevice = 1;
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   522
    return 1;   /* this audio target is available. */
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
}
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
AudioBootStrap PULSEAUDIO_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   527
    "pulseaudio", "PulseAudio", PULSEAUDIO_Init, 0
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
};
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   530
#endif /* SDL_AUDIO_DRIVER_PULSEAUDIO */
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   531
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
/* vi: set ts=4 sw=4 expandtab: */