Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
355 lines (302 loc) · 9.1 KB

SDL_esdaudio.c

File metadata and controls

355 lines (302 loc) · 9.1 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
/* Allow access to an ESD network stream mixing buffer */
Mar 31, 2006
Mar 31, 2006
26
27
28
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
Apr 26, 2001
Apr 26, 2001
29
30
31
#include <errno.h>
#include <esd.h>
Feb 10, 2006
Feb 10, 2006
32
#include "SDL_timer.h"
Apr 26, 2001
Apr 26, 2001
33
#include "SDL_audio.h"
Feb 16, 2006
Feb 16, 2006
34
35
#include "../SDL_audiomem.h"
#include "../SDL_audio_c.h"
Apr 26, 2001
Apr 26, 2001
36
37
#include "SDL_esdaudio.h"
Feb 16, 2006
Feb 16, 2006
38
#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
Mar 6, 2002
Mar 6, 2002
39
40
41
42
43
44
#include "SDL_name.h"
#include "SDL_loadso.h"
#else
#define SDL_NAME(X) X
#endif
Apr 26, 2001
Apr 26, 2001
45
46
47
/* The tag name used by ESD audio */
#define ESD_DRIVER_NAME "esd"
Feb 16, 2006
Feb 16, 2006
48
#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
Mar 6, 2002
Mar 6, 2002
49
Feb 16, 2006
Feb 16, 2006
50
static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
Mar 6, 2002
Mar 6, 2002
51
52
static void *esd_handle = NULL;
Jul 10, 2006
Jul 10, 2006
53
54
55
56
static int (*SDL_NAME(esd_open_sound)) (const char *host);
static int (*SDL_NAME(esd_close)) (int esd);
static int (*SDL_NAME(esd_play_stream)) (esd_format_t format, int rate,
const char *host, const char *name);
Oct 6, 2006
Oct 6, 2006
57
58
#define SDL_ESD_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
Jul 10, 2006
Jul 10, 2006
59
60
61
62
static struct
{
const char *name;
void **func;
Mar 6, 2002
Mar 6, 2002
63
} esd_functions[] = {
Oct 6, 2006
Oct 6, 2006
64
65
66
67
68
SDL_ESD_SYM(esd_open_sound),
SDL_ESD_SYM(esd_close),
SDL_ESD_SYM(esd_play_stream),
};
#undef SDL_ESD_SYM
Mar 6, 2002
Mar 6, 2002
69
Jul 10, 2006
Jul 10, 2006
70
71
static void
UnloadESDLibrary()
Mar 6, 2002
Mar 6, 2002
72
{
Oct 6, 2006
Oct 6, 2006
73
if (esd_handle != NULL) {
Jul 10, 2006
Jul 10, 2006
74
75
76
SDL_UnloadObject(esd_handle);
esd_handle = NULL;
}
Mar 6, 2002
Mar 6, 2002
77
78
}
Jul 10, 2006
Jul 10, 2006
79
80
static int
LoadESDLibrary(void)
Mar 6, 2002
Mar 6, 2002
81
{
Jul 10, 2006
Jul 10, 2006
82
83
int i, retval = -1;
Oct 6, 2006
Oct 6, 2006
84
85
86
87
88
89
90
91
92
93
94
95
if (esd_handle == NULL) {
esd_handle = SDL_LoadObject(esd_library);
if (esd_handle) {
retval = 0;
for (i = 0; i < SDL_arraysize(esd_functions); ++i) {
*esd_functions[i].func =
SDL_LoadFunction(esd_handle, esd_functions[i].name);
if (!*esd_functions[i].func) {
retval = -1;
UnloadESDLibrary();
break;
}
Jul 10, 2006
Jul 10, 2006
96
97
98
99
}
}
}
return retval;
Mar 6, 2002
Mar 6, 2002
100
101
102
103
}
#else
Jul 10, 2006
Jul 10, 2006
104
105
static void
UnloadESDLibrary()
Mar 6, 2002
Mar 6, 2002
106
{
Jul 10, 2006
Jul 10, 2006
107
return;
Mar 6, 2002
Mar 6, 2002
108
109
}
Jul 10, 2006
Jul 10, 2006
110
111
static int
LoadESDLibrary(void)
Mar 6, 2002
Mar 6, 2002
112
{
Jul 10, 2006
Jul 10, 2006
113
return 0;
Mar 6, 2002
Mar 6, 2002
114
115
}
Feb 16, 2006
Feb 16, 2006
116
#endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
Mar 6, 2002
Mar 6, 2002
117
Jul 10, 2006
Jul 10, 2006
118
static int
Oct 6, 2006
Oct 6, 2006
119
ESD_Available(void)
Apr 26, 2001
Apr 26, 2001
120
{
Oct 4, 2006
Oct 4, 2006
121
int available = 0;
Oct 6, 2006
Oct 6, 2006
122
123
124
125
126
127
128
129
130
131
132
if (LoadESDLibrary() == 0) {
int connection;
if (SDL_getenv("ESD_NO_SPAWN") == NULL) {
SDL_putenv("ESD_NO_SPAWN=1"); /* Don't start ESD if it's not running */
}
connection = SDL_NAME(esd_open_sound) (NULL);
if (connection >= 0) {
available = 1;
SDL_NAME(esd_close) (connection);
}
UnloadESDLibrary();
Jul 10, 2006
Jul 10, 2006
133
}
Oct 4, 2006
Oct 4, 2006
134
return available;
Apr 26, 2001
Apr 26, 2001
135
136
137
138
}
/* This function waits until it is possible to write a full sound buffer */
Jul 10, 2006
Jul 10, 2006
139
static void
Oct 6, 2006
Oct 6, 2006
140
ESD_WaitDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
141
{
Jul 10, 2006
Jul 10, 2006
142
143
144
145
146
147
148
149
Sint32 ticks;
/* Check to see if the thread-parent process is still alive */
{
static int cnt = 0;
/* Note that this only works with thread implementations
that use a different process id for each thread.
*/
Oct 6, 2006
Oct 6, 2006
150
151
152
/* Check every 10 loops */
if (this->hidden->parent && (((++cnt) % 10) == 0)) {
if (kill(this->hidden->parent, 0) < 0) {
Jul 10, 2006
Jul 10, 2006
153
154
155
156
157
158
this->enabled = 0;
}
}
}
/* Use timer for general audio synchronization */
Oct 6, 2006
Oct 6, 2006
159
ticks = ((Sint32) (this->hidden->next_frame-SDL_GetTicks())) - FUDGE_TICKS;
Jul 10, 2006
Jul 10, 2006
160
161
162
if (ticks > 0) {
SDL_Delay(ticks);
}
Apr 26, 2001
Apr 26, 2001
163
164
}
Jul 10, 2006
Jul 10, 2006
165
static void
Oct 6, 2006
Oct 6, 2006
166
ESD_PlayDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
167
{
Oct 6, 2006
Oct 6, 2006
168
int written = 0;
Jul 10, 2006
Jul 10, 2006
169
170
171
/* Write the audio data, checking for EAGAIN on broken audio drivers */
do {
Oct 6, 2006
Oct 6, 2006
172
173
174
written = write(this->hidden->audio_fd,
this->hidden->mixbuf,
this->hidden->mixlen);
Jul 10, 2006
Jul 10, 2006
175
176
177
178
179
180
181
182
if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) {
SDL_Delay(1); /* Let a little CPU time go by */
}
}
while ((written < 0) &&
((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
/* Set the next write frame */
Oct 6, 2006
Oct 6, 2006
183
this->hidden->next_frame += this->hidden->frame_ticks;
Jul 10, 2006
Jul 10, 2006
184
185
186
187
188
/* If we couldn't write, assume fatal error for now */
if (written < 0) {
this->enabled = 0;
}
Apr 26, 2001
Apr 26, 2001
189
190
}
Jul 10, 2006
Jul 10, 2006
191
static Uint8 *
Oct 6, 2006
Oct 6, 2006
192
ESD_GetDeviceBuf(_THIS)
Apr 26, 2001
Apr 26, 2001
193
{
Oct 6, 2006
Oct 6, 2006
194
return (this->hidden->mixbuf);
Apr 26, 2001
Apr 26, 2001
195
196
}
Jul 10, 2006
Jul 10, 2006
197
static void
Oct 6, 2006
Oct 6, 2006
198
ESD_CloseDevice(_THIS)
Apr 26, 2001
Apr 26, 2001
199
{
Oct 6, 2006
Oct 6, 2006
200
201
202
203
204
205
206
207
208
209
210
211
if (this->hidden != NULL) {
if (this->hidden->mixbuf != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
if (this->hidden->audio_fd >= 0) {
SDL_NAME(esd_close) (this->hidden->audio_fd);
this->hidden->audio_fd = -1;
}
SDL_free(this->hidden);
this->hidden = NULL;
Jul 10, 2006
Jul 10, 2006
212
}
Oct 6, 2006
Oct 6, 2006
213
UnloadESDLibrary();
Apr 26, 2001
Apr 26, 2001
214
215
216
}
/* Try to get the name of the program */
Jul 10, 2006
Jul 10, 2006
217
218
static char *
get_progname(void)
Apr 26, 2001
Apr 26, 2001
219
{
Jul 10, 2006
Jul 10, 2006
220
char *progname = NULL;
Feb 21, 2006
Feb 21, 2006
221
#ifdef __LINUX__
Jul 10, 2006
Jul 10, 2006
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
FILE *fp;
static char temp[BUFSIZ];
SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
fp = fopen(temp, "r");
if (fp != NULL) {
if (fgets(temp, sizeof(temp) - 1, fp)) {
progname = SDL_strrchr(temp, '/');
if (progname == NULL) {
progname = temp;
} else {
progname = progname + 1;
}
}
fclose(fp);
}
Apr 26, 2001
Apr 26, 2001
238
#endif
Jul 10, 2006
Jul 10, 2006
239
return (progname);
Apr 26, 2001
Apr 26, 2001
240
241
}
Oct 6, 2006
Oct 6, 2006
242
Jul 10, 2006
Jul 10, 2006
243
static int
Oct 6, 2006
Oct 6, 2006
244
ESD_OpenDevice(_THIS, const char *devname, int iscapture)
Apr 26, 2001
Apr 26, 2001
245
{
Oct 6, 2006
Oct 6, 2006
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
esd_format_t format = (ESD_STREAM | ESD_PLAY);
SDL_AudioFormat test_format = 0;
int found = 0;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return 0;
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
this->hidden->audio_fd = -1;
if (LoadESDLibrary() < 0) {
ESD_CloseDevice(this);
SDL_SetError("ESD: failed to load library: %s", SDL_GetError());
return 0;
}
Jul 10, 2006
Jul 10, 2006
265
266
/* Convert audio spec to the ESD audio format */
Oct 6, 2006
Oct 6, 2006
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Try for a closest match on audio format */
for (test_format = SDL_FirstAudioFormat(this->spec.format);
!found && test_format; test_format = SDL_NextAudioFormat()) {
#ifdef DEBUG_AUDIO
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
#endif
found = 1;
switch (test_format) {
case AUDIO_U8:
format |= ESD_BITS8;
break;
case AUDIO_S16SYS:
format |= ESD_BITS16;
break;
default:
found = 0;
break;
}
}
if (!found) {
ESD_CloseDevice(this);
SDL_SetError("Couldn't find any hardware audio formats");
return 0;
Jul 10, 2006
Jul 10, 2006
291
}
Oct 6, 2006
Oct 6, 2006
292
293
if (this->spec.channels == 1) {
Jul 10, 2006
Jul 10, 2006
294
295
296
297
format |= ESD_MONO;
} else {
format |= ESD_STEREO;
}
Apr 26, 2001
Apr 26, 2001
298
#if 0
Oct 6, 2006
Oct 6, 2006
299
this->spec.samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */
Apr 26, 2001
Apr 26, 2001
300
301
#endif
Jul 10, 2006
Jul 10, 2006
302
/* Open a connection to the ESD audio server */
Oct 6, 2006
Oct 6, 2006
303
304
305
306
307
this->hidden->audio_fd =
SDL_NAME(esd_play_stream)(format,this->spec.freq,NULL,get_progname());
if (this->hidden->audio_fd < 0) {
ESD_CloseDevice(this);
Jul 10, 2006
Jul 10, 2006
308
SDL_SetError("Couldn't open ESD connection");
Oct 6, 2006
Oct 6, 2006
309
return 0;
Jul 10, 2006
Jul 10, 2006
310
311
312
}
/* Calculate the final parameters for this audio specification */
Oct 6, 2006
Oct 6, 2006
313
314
315
SDL_CalculateAudioSpec(&this->spec);
this->hidden->frame_ticks = (float) (this->spec.samples*1000) / this->spec.freq;
this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks;
Jul 10, 2006
Jul 10, 2006
316
317
/* Allocate mixing buffer */
Oct 6, 2006
Oct 6, 2006
318
319
320
321
322
323
this->hidden->mixlen = this->spec.size;
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
ESD_CloseDevice(this);
SDL_OutOfMemory();
return 0;
Jul 10, 2006
Jul 10, 2006
324
}
Oct 6, 2006
Oct 6, 2006
325
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
Jul 10, 2006
Jul 10, 2006
326
327
/* Get the parent process id (we're the parent of the audio thread) */
Oct 6, 2006
Oct 6, 2006
328
this->hidden->parent = getpid();
Jul 10, 2006
Jul 10, 2006
329
330
/* We're ready to rock and roll. :-) */
Oct 6, 2006
Oct 6, 2006
331
return 1;
Apr 26, 2001
Apr 26, 2001
332
}
Jul 10, 2006
Jul 10, 2006
333
Oct 6, 2006
Oct 6, 2006
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
static int
ESD_Init(SDL_AudioDriverImpl *impl)
{
/* Set the function pointers */
impl->OpenDevice = ESD_OpenDevice;
impl->PlayDevice = ESD_PlayDevice;
impl->WaitDevice = ESD_WaitDevice;
impl->GetDeviceBuf = ESD_GetDeviceBuf;
impl->CloseDevice = ESD_CloseDevice;
impl->OnlyHasDefaultOutputDevice = 1;
return 1;
}
AudioBootStrap ESD_bootstrap = {
ESD_DRIVER_NAME, "Enlightened Sound Daemon",
ESD_Available, ESD_Init, 0
};
Jul 10, 2006
Jul 10, 2006
355
/* vi: set ts=4 sw=4 expandtab: */