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