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