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