2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Allow access to an ESD network stream mixing buffer */
26 #include <sys/types.h>
32 #include "SDL_timer.h"
33 #include "SDL_audio.h"
34 #include "../SDL_audiomem.h"
35 #include "../SDL_audio_c.h"
36 #include "../SDL_audiodev_c.h"
37 #include "SDL_esdaudio.h"
39 #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
41 #include "SDL_loadso.h"
46 /* The tag name used by ESD audio */
47 #define ESD_DRIVER_NAME "esd"
49 /* Audio driver functions */
50 static int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec);
51 static void ESD_WaitAudio(_THIS);
52 static void ESD_PlayAudio(_THIS);
53 static Uint8 *ESD_GetAudioBuf(_THIS);
54 static void ESD_CloseAudio(_THIS);
56 #ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
58 static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
59 static void *esd_handle = NULL;
60 static int esd_loaded = 0;
62 static int (*SDL_NAME(esd_open_sound))( const char *host );
63 static int (*SDL_NAME(esd_close))( int esd );
64 static int (*SDL_NAME(esd_play_stream))( esd_format_t format, int rate,
65 const char *host, const char *name );
70 { "esd_open_sound", (void **)&SDL_NAME(esd_open_sound) },
71 { "esd_close", (void **)&SDL_NAME(esd_close) },
72 { "esd_play_stream", (void **)&SDL_NAME(esd_play_stream) },
75 static void UnloadESDLibrary()
78 SDL_UnloadObject(esd_handle);
84 static int LoadESDLibrary(void)
88 esd_handle = SDL_LoadObject(esd_library);
92 for ( i=0; i<SDL_arraysize(esd_functions); ++i ) {
93 *esd_functions[i].func = SDL_LoadFunction(esd_handle, esd_functions[i].name);
94 if ( !*esd_functions[i].func ) {
106 static void UnloadESDLibrary()
111 static int LoadESDLibrary(void)
116 #endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
118 /* Audio driver bootstrap functions */
120 static int Audio_Available(void)
126 if ( LoadESDLibrary() < 0 ) {
129 connection = SDL_NAME(esd_open_sound)(NULL);
130 if ( connection >= 0 ) {
132 SDL_NAME(esd_close)(connection);
138 static void Audio_DeleteDevice(SDL_AudioDevice *device)
140 SDL_free(device->hidden);
145 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
147 SDL_AudioDevice *this;
149 /* Initialize all variables that we clean on shutdown */
151 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
153 SDL_memset(this, 0, (sizeof *this));
154 this->hidden = (struct SDL_PrivateAudioData *)
155 SDL_malloc((sizeof *this->hidden));
157 if ( (this == NULL) || (this->hidden == NULL) ) {
164 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
167 /* Set the function pointers */
168 this->OpenAudio = ESD_OpenAudio;
169 this->WaitAudio = ESD_WaitAudio;
170 this->PlayAudio = ESD_PlayAudio;
171 this->GetAudioBuf = ESD_GetAudioBuf;
172 this->CloseAudio = ESD_CloseAudio;
174 this->free = Audio_DeleteDevice;
179 AudioBootStrap ESD_bootstrap = {
180 ESD_DRIVER_NAME, "Enlightened Sound Daemon",
181 Audio_Available, Audio_CreateDevice
184 /* This function waits until it is possible to write a full sound buffer */
185 static void ESD_WaitAudio(_THIS)
189 /* Check to see if the thread-parent process is still alive */
190 { static int cnt = 0;
191 /* Note that this only works with thread implementations
192 that use a different process id for each thread.
194 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
195 if ( kill(parent, 0) < 0 ) {
201 /* Use timer for general audio synchronization */
202 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
208 static void ESD_PlayAudio(_THIS)
212 /* Write the audio data, checking for EAGAIN on broken audio drivers */
214 written = write(audio_fd, mixbuf, mixlen);
215 if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
216 SDL_Delay(1); /* Let a little CPU time go by */
218 } while ( (written < 0) &&
219 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
221 /* Set the next write frame */
222 next_frame += frame_ticks;
224 /* If we couldn't write, assume fatal error for now */
230 static Uint8 *ESD_GetAudioBuf(_THIS)
235 static void ESD_CloseAudio(_THIS)
237 if ( mixbuf != NULL ) {
238 SDL_FreeAudioMem(mixbuf);
241 if ( audio_fd >= 0 ) {
242 SDL_NAME(esd_close)(audio_fd);
247 /* Try to get the name of the program */
248 static char *get_progname(void)
250 char *progname = NULL;
253 static char temp[BUFSIZ];
255 SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
256 fp = fopen(temp, "r");
258 if ( fgets(temp, sizeof(temp)-1, fp) ) {
259 progname = SDL_strrchr(temp, '/');
260 if ( progname == NULL ) {
263 progname = progname+1;
272 static int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec)
276 /* Convert audio spec to the ESD audio format */
277 format = (ESD_STREAM | ESD_PLAY);
278 switch ( spec->format & 0xFF ) {
283 format |= ESD_BITS16;
286 SDL_SetError("Unsupported ESD audio format");
289 if ( spec->channels == 1 ) {
292 format |= ESD_STEREO;
295 spec->samples = ESD_BUF_SIZE; /* Darn, no way to change this yet */
298 /* Open a connection to the ESD audio server */
299 audio_fd = SDL_NAME(esd_play_stream)(format, spec->freq, NULL, get_progname());
300 if ( audio_fd < 0 ) {
301 SDL_SetError("Couldn't open ESD connection");
305 /* Calculate the final parameters for this audio specification */
306 SDL_CalculateAudioSpec(spec);
307 frame_ticks = (float)(spec->samples*1000)/spec->freq;
308 next_frame = SDL_GetTicks()+frame_ticks;
310 /* Allocate mixing buffer */
312 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
313 if ( mixbuf == NULL ) {
316 SDL_memset(mixbuf, spec->silence, spec->size);
318 /* Get the parent process id (we're the parent of the audio thread) */
321 /* We're ready to rock and roll. :-) */