annotate src/audio/pulse/SDL_pulseaudio.c @ 4015:7e878cc4250a SDL-1.2

Fixed compiler warning (gcc w/ -Wall) for using getpid() and kill() without their proper headers included.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 08 Jul 2007 01:36:16 +0000
parents 42e83d81224b
children a1b03ba2fcd0
rev   line source
3939
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 Copyright (C) 1997-2007 Sam Lantinga
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 Lesser General Public License for more details.
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19 Stéphan Kochen
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 stephan@kochen.nl
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 Based on parts of the ALSA and ESounD output drivers.
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 #include "SDL_config.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 /* Allow access to an PulseAudio network stream mixing buffer */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 #include <sys/types.h>
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 #include <unistd.h>
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 #include <signal.h>
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 #include <errno.h>
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 #include <pulse/simple.h>
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 #include "SDL_timer.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 #include "SDL_audio.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #include "../SDL_audiomem.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 #include "../SDL_audio_c.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 #include "../SDL_audiodev_c.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include "SDL_pulseaudio.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 #include "SDL_name.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 #include "SDL_loadso.h"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 #else
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 #define SDL_NAME(X) X
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #endif
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 /* The tag name used by the driver */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 #define PULSE_DRIVER_NAME "pulse"
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 /* Audio driver functions */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 static void PULSE_WaitAudio(_THIS);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 static void PULSE_PlayAudio(_THIS);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 static Uint8 *PULSE_GetAudioBuf(_THIS);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 static void PULSE_CloseAudio(_THIS);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 #ifdef SDL_AUDIO_DRIVER_PULSE_DYNAMIC
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 static const char *pulse_library = SDL_AUDIO_DRIVER_PULSE_DYNAMIC;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 static void *pulse_handle = NULL;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 static int pulse_loaded = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 static pa_simple* (*SDL_NAME(pa_simple_new))(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 const char *server,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 const char *name,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 pa_stream_direction_t dir,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 const char *dev,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 const char *stream_name,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 const pa_sample_spec *ss,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 const pa_channel_map *map,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 const pa_buffer_attr *attr,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 int *error
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 );
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 static void (*SDL_NAME(pa_simple_free))(pa_simple *s);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 static int (*SDL_NAME(pa_simple_drain))(pa_simple *s, int *error);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 static int (*SDL_NAME(pa_simple_write))(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 pa_simple *s,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 const void *data,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 size_t length,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 int *error
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 );
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 static pa_channel_map* (*SDL_NAME(pa_channel_map_init_auto))(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 pa_channel_map *m,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 unsigned channels,
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 pa_channel_map_def_t def
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 );
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 static struct {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 const char *name;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 void **func;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 } pulse_functions[] = {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 { "pa_simple_new",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 (void **)&SDL_NAME(pa_simple_new) },
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 { "pa_simple_free",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 (void **)&SDL_NAME(pa_simple_free) },
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 { "pa_simple_drain",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 (void **)&SDL_NAME(pa_simple_drain) },
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 { "pa_simple_write",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 (void **)&SDL_NAME(pa_simple_write) },
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 { "pa_channel_map_init_auto",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 (void **)&SDL_NAME(pa_channel_map_init_auto) },
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 };
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 static void UnloadPulseLibrary()
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 if ( pulse_loaded ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 SDL_UnloadObject(pulse_handle);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 pulse_handle = NULL;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 pulse_loaded = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 static int LoadPulseLibrary(void)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 int i, retval = -1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 pulse_handle = SDL_LoadObject(pulse_library);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 if ( pulse_handle ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 pulse_loaded = 1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 retval = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 for ( i=0; i<SDL_arraysize(pulse_functions); ++i ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 *pulse_functions[i].func = SDL_LoadFunction(pulse_handle, pulse_functions[i].name);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 if ( !*pulse_functions[i].func ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 retval = -1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 UnloadPulseLibrary();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 break;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 return retval;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 #else
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 static void UnloadPulseLibrary()
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 return;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 static int LoadPulseLibrary(void)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 return 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 #endif /* SDL_AUDIO_DRIVER_PULSE_DYNAMIC */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 /* Audio driver bootstrap functions */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 static int Audio_Available(void)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 pa_sample_spec paspec;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 pa_simple *connection;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 int available;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 available = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 if ( LoadPulseLibrary() < 0 ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 return available;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 /* Connect with a dummy format. */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 paspec.format = PA_SAMPLE_U8;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 paspec.rate = 11025;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 paspec.channels = 1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 connection = SDL_NAME(pa_simple_new)(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 SDL_getenv("PASERVER"), /* server */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 "Test stream", /* application name */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 PA_STREAM_PLAYBACK, /* playback mode */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 SDL_getenv("PADEVICE"), /* device on the server */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 "Simple DirectMedia Layer", /* stream description */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 &paspec, /* sample format spec */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 NULL, /* channel map */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 NULL, /* buffering attributes */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 NULL /* error code */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 );
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 if ( connection != NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 available = 1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 SDL_NAME(pa_simple_free)(connection);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 UnloadPulseLibrary();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 return(available);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 static void Audio_DeleteDevice(SDL_AudioDevice *device)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 SDL_free(device->hidden);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 SDL_free(device);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 UnloadPulseLibrary();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 SDL_AudioDevice *this;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 /* Initialize all variables that we clean on shutdown */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 LoadPulseLibrary();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 if ( this ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 SDL_memset(this, 0, (sizeof *this));
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 this->hidden = (struct SDL_PrivateAudioData *)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 SDL_malloc((sizeof *this->hidden));
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 if ( (this == NULL) || (this->hidden == NULL) ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 SDL_OutOfMemory();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 if ( this ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 SDL_free(this);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 return(0);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 /* Set the function pointers */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 this->OpenAudio = PULSE_OpenAudio;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 this->WaitAudio = PULSE_WaitAudio;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 this->PlayAudio = PULSE_PlayAudio;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 this->GetAudioBuf = PULSE_GetAudioBuf;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 this->CloseAudio = PULSE_CloseAudio;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 this->free = Audio_DeleteDevice;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 return this;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 AudioBootStrap PULSE_bootstrap = {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 PULSE_DRIVER_NAME, "PulseAudio",
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 Audio_Available, Audio_CreateDevice
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 };
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 /* This function waits until it is possible to write a full sound buffer */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 static void PULSE_WaitAudio(_THIS)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 /* Check to see if the thread-parent process is still alive */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 { static int cnt = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 /* Note that this only works with thread implementations
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 that use a different process id for each thread.
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 if ( kill(parent, 0) < 0 ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 this->enabled = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 static void PULSE_PlayAudio(_THIS)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 /* Write the audio data */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 if ( SDL_NAME(pa_simple_write)(stream, mixbuf, mixlen, NULL) != 0 )
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 this->enabled = 0;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 static Uint8 *PULSE_GetAudioBuf(_THIS)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 return(mixbuf);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 static void PULSE_CloseAudio(_THIS)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 if ( mixbuf != NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 SDL_FreeAudioMem(mixbuf);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 mixbuf = NULL;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267 if ( stream != NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268 SDL_NAME(pa_simple_drain)(stream, NULL);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 SDL_NAME(pa_simple_free)(stream);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 stream = NULL;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 /* Try to get the name of the program */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 static char *get_progname(void)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 char *progname = NULL;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 #ifdef __LINUX__
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 FILE *fp;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 static char temp[BUFSIZ];
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 fp = fopen(temp, "r");
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 if ( fp != NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 if ( fgets(temp, sizeof(temp)-1, fp) ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 progname = SDL_strrchr(temp, '/');
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 if ( progname == NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 progname = temp;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 } else {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 progname = progname+1;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 fclose(fp);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 #endif
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 return(progname);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299 static int PULSE_OpenAudio(_THIS, SDL_AudioSpec *spec)
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 Uint16 test_format;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 pa_sample_spec paspec;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 pa_buffer_attr paattr;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 pa_channel_map pacmap;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 paspec.format = PA_SAMPLE_INVALID;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 for ( test_format = SDL_FirstAudioFormat(spec->format); test_format; ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 switch ( test_format ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 case AUDIO_U8:
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 paspec.format = PA_SAMPLE_U8;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 break;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 case AUDIO_S16LSB:
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 paspec.format = PA_SAMPLE_S16LE;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 break;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 case AUDIO_S16MSB:
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 paspec.format = PA_SAMPLE_S16BE;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 break;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 if ( paspec.format != PA_SAMPLE_INVALID )
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 break;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322 if (paspec.format == PA_SAMPLE_INVALID ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 SDL_SetError("Couldn't find any suitable audio formats");
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 return(-1);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 spec->format = test_format;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 paspec.channels = spec->channels;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 paspec.rate = spec->freq;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331 /* Calculate the final parameters for this audio specification */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 SDL_CalculateAudioSpec(spec);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 /* Allocate mixing buffer */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 mixlen = spec->size;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 if ( mixbuf == NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 return(-1);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 SDL_memset(mixbuf, spec->silence, spec->size);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 /* Reduced prebuffering compared to the defaults. */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 paattr.tlength = mixlen;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 paattr.minreq = mixlen;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 paattr.fragsize = mixlen;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 paattr.prebuf = mixlen;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 paattr.maxlength = mixlen * 4;
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 /* The SDL ALSA output hints us that we use Windows' channel mapping */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 /* http://bugzilla.libsdl.org/show_bug.cgi?id=110 */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 SDL_NAME(pa_channel_map_init_auto)(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 &pacmap, spec->channels, PA_CHANNEL_MAP_WAVEEX);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354 /* Connect to the PulseAudio server */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355 stream = SDL_NAME(pa_simple_new)(
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 SDL_getenv("PASERVER"), /* server */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 get_progname(), /* application name */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358 PA_STREAM_PLAYBACK, /* playback mode */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 SDL_getenv("PADEVICE"), /* device on the server */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 "Simple DirectMedia Layer", /* stream description */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 &paspec, /* sample format spec */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 &pacmap, /* channel map */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 &paattr, /* buffering attributes */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364 NULL /* error code */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 );
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 if ( stream == NULL ) {
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 PULSE_CloseAudio(this);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 SDL_SetError("Could not connect to PulseAudio");
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 return(-1);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 /* Get the parent process id (we're the parent of the audio thread) */
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 parent = getpid();
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375 return(0);
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 }
42e83d81224b Committed PulseAudio driver. Thanks, Stephan!
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377