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