src/audio/nas/SDL_nasaudio.c
author Ryan C. Gordon
Sun, 13 Sep 2009 22:19:56 +0000
branchSDL-1.2
changeset 4192 2f5e884f0a9d
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
Made NAS target dynamic, so you can safely include it in shipping binaries.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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
    This driver was written by:
slouken@0
    23
    Erik Inge Bolsų
slouken@0
    24
    knan@mo.himolde.no
slouken@0
    25
*/
slouken@1402
    26
#include "SDL_config.h"
slouken@0
    27
slouken@0
    28
/* Allow access to a raw mixing buffer */
slouken@0
    29
slouken@0
    30
#include <signal.h>
slouken@0
    31
#include <unistd.h>
slouken@0
    32
slouken@1358
    33
#include "SDL_timer.h"
slouken@0
    34
#include "SDL_audio.h"
slouken@1361
    35
#include "../SDL_audiomem.h"
slouken@1361
    36
#include "../SDL_audio_c.h"
slouken@1361
    37
#include "../SDL_audiodev_c.h"
slouken@0
    38
#include "SDL_nasaudio.h"
slouken@0
    39
icculus@4192
    40
#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
icculus@4192
    41
#include "SDL_loadso.h"
icculus@4192
    42
#endif
icculus@4192
    43
slouken@0
    44
/* The tag name used by artsc audio */
slouken@0
    45
#define NAS_DRIVER_NAME         "nas"
slouken@0
    46
slouken@0
    47
static struct SDL_PrivateAudioData *this2 = NULL;
slouken@0
    48
icculus@4192
    49
static void (*NAS_AuCloseServer) (AuServer *);
icculus@4192
    50
static void (*NAS_AuNextEvent) (AuServer *, AuBool, AuEvent *);
icculus@4192
    51
static AuBool(*NAS_AuDispatchEvent) (AuServer *, AuEvent *);
icculus@4192
    52
static AuFlowID(*NAS_AuCreateFlow) (AuServer *, AuStatus *);
icculus@4192
    53
static void (*NAS_AuStartFlow) (AuServer *, AuFlowID, AuStatus *);
icculus@4192
    54
static void (*NAS_AuSetElements)
icculus@4192
    55
  (AuServer *, AuFlowID, AuBool, int, AuElement *, AuStatus *);
icculus@4192
    56
static void (*NAS_AuWriteElement)
icculus@4192
    57
  (AuServer *, AuFlowID, int, AuUint32, AuPointer, AuBool, AuStatus *);
icculus@4192
    58
static AuServer *(*NAS_AuOpenServer)
icculus@4192
    59
  (_AuConst char *, int, _AuConst char *, int, _AuConst char *, char **);
icculus@4192
    60
static AuEventHandlerRec *(*NAS_AuRegisterEventHandler)
icculus@4192
    61
  (AuServer *, AuMask, int, AuID, AuEventHandlerCallback, AuPointer);
icculus@4192
    62
icculus@4192
    63
icculus@4192
    64
#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
icculus@4192
    65
icculus@4192
    66
static const char *nas_library = SDL_AUDIO_DRIVER_NAS_DYNAMIC;
icculus@4192
    67
static void *nas_handle = NULL;
icculus@4192
    68
icculus@4192
    69
static int
icculus@4192
    70
load_nas_sym(const char *fn, void **addr)
icculus@4192
    71
{
icculus@4192
    72
    *addr = SDL_LoadFunction(nas_handle, fn);
icculus@4192
    73
    if (*addr == NULL) {
icculus@4192
    74
        return 0;
icculus@4192
    75
    }
icculus@4192
    76
    return 1;
icculus@4192
    77
}
icculus@4192
    78
icculus@4192
    79
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
icculus@4192
    80
#define SDL_NAS_SYM(x) \
icculus@4192
    81
    if (!load_nas_sym(#x, (void **) (char *) &NAS_##x)) return -1
icculus@4192
    82
#else
icculus@4192
    83
#define SDL_NAS_SYM(x) NAS_##x = x
icculus@4192
    84
#endif
icculus@4192
    85
icculus@4192
    86
static int
icculus@4192
    87
load_nas_syms(void)
icculus@4192
    88
{
icculus@4192
    89
    SDL_NAS_SYM(AuCloseServer);
icculus@4192
    90
    SDL_NAS_SYM(AuNextEvent);
icculus@4192
    91
    SDL_NAS_SYM(AuDispatchEvent);
icculus@4192
    92
    SDL_NAS_SYM(AuCreateFlow);
icculus@4192
    93
    SDL_NAS_SYM(AuStartFlow);
icculus@4192
    94
    SDL_NAS_SYM(AuSetElements);
icculus@4192
    95
    SDL_NAS_SYM(AuWriteElement);
icculus@4192
    96
    SDL_NAS_SYM(AuOpenServer);
icculus@4192
    97
    SDL_NAS_SYM(AuRegisterEventHandler);
icculus@4192
    98
    return 0;
icculus@4192
    99
}
icculus@4192
   100
icculus@4192
   101
#undef SDL_NAS_SYM
icculus@4192
   102
icculus@4192
   103
#ifdef SDL_AUDIO_DRIVER_NAS_DYNAMIC
icculus@4192
   104
icculus@4192
   105
static void
icculus@4192
   106
UnloadNASLibrary(void)
icculus@4192
   107
{
icculus@4192
   108
    if (nas_handle != NULL) {
icculus@4192
   109
        SDL_UnloadObject(nas_handle);
icculus@4192
   110
        nas_handle = NULL;
icculus@4192
   111
    }
icculus@4192
   112
}
icculus@4192
   113
icculus@4192
   114
static int
icculus@4192
   115
LoadNASLibrary(void)
icculus@4192
   116
{
icculus@4192
   117
    int retval = 0;
icculus@4192
   118
    if (nas_handle == NULL) {
icculus@4192
   119
        nas_handle = SDL_LoadObject(nas_library);
icculus@4192
   120
        if (nas_handle == NULL) {
icculus@4192
   121
            /* Copy error string so we can use it in a new SDL_SetError(). */
icculus@4192
   122
            char *origerr = SDL_GetError();
icculus@4192
   123
            size_t len = SDL_strlen(origerr) + 1;
icculus@4192
   124
            char *err = (char *) alloca(len);
icculus@4192
   125
            SDL_strlcpy(err, origerr, len);
icculus@4192
   126
            retval = -1;
icculus@4192
   127
            SDL_SetError("NAS: SDL_LoadObject('%s') failed: %s\n",
icculus@4192
   128
                         nas_library, err);
icculus@4192
   129
        } else {
icculus@4192
   130
            retval = load_nas_syms();
icculus@4192
   131
            if (retval < 0) {
icculus@4192
   132
                UnloadNASLibrary();
icculus@4192
   133
            }
icculus@4192
   134
        }
icculus@4192
   135
    }
icculus@4192
   136
    return retval;
icculus@4192
   137
}
icculus@4192
   138
icculus@4192
   139
#else
icculus@4192
   140
icculus@4192
   141
static void
icculus@4192
   142
UnloadNASLibrary(void)
icculus@4192
   143
{
icculus@4192
   144
}
icculus@4192
   145
icculus@4192
   146
static int
icculus@4192
   147
LoadNASLibrary(void)
icculus@4192
   148
{
icculus@4192
   149
    load_nas_syms();
icculus@4192
   150
    return 0;
icculus@4192
   151
}
icculus@4192
   152
icculus@4192
   153
#endif /* SDL_AUDIO_DRIVER_NAS_DYNAMIC */
icculus@4192
   154
icculus@4192
   155
slouken@0
   156
/* Audio driver functions */
slouken@0
   157
static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
   158
static void NAS_WaitAudio(_THIS);
slouken@0
   159
static void NAS_PlayAudio(_THIS);
slouken@0
   160
static Uint8 *NAS_GetAudioBuf(_THIS);
slouken@0
   161
static void NAS_CloseAudio(_THIS);
slouken@0
   162
slouken@0
   163
/* Audio driver bootstrap functions */
slouken@0
   164
slouken@0
   165
static int Audio_Available(void)
slouken@0
   166
{
icculus@4192
   167
	if (LoadNASLibrary() == 0) {
icculus@4192
   168
		AuServer *aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
icculus@4192
   169
		if (!aud) {
icculus@4192
   170
			UnloadNASLibrary();
icculus@4192
   171
			return 0;
icculus@4192
   172
		}
icculus@4192
   173
		NAS_AuCloseServer(aud);
icculus@4192
   174
		UnloadNASLibrary();
icculus@4192
   175
		return 1;
icculus@4192
   176
	}
icculus@4192
   177
	return 0;
slouken@0
   178
}
slouken@0
   179
slouken@0
   180
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   181
{
icculus@4192
   182
	UnloadNASLibrary();
slouken@1336
   183
	SDL_free(device->hidden);
slouken@1336
   184
	SDL_free(device);
slouken@0
   185
}
slouken@0
   186
slouken@0
   187
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   188
{
slouken@0
   189
	SDL_AudioDevice *this;
slouken@0
   190
icculus@4192
   191
	if (LoadNASLibrary() < 0) {
icculus@4192
   192
		return NULL;
icculus@4192
   193
	}
icculus@4192
   194
slouken@0
   195
	/* Initialize all variables that we clean on shutdown */
slouken@1336
   196
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
slouken@0
   197
	if ( this ) {
slouken@1336
   198
		SDL_memset(this, 0, (sizeof *this));
slouken@0
   199
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@1336
   200
				SDL_malloc((sizeof *this->hidden));
slouken@0
   201
	}
slouken@0
   202
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   203
		SDL_OutOfMemory();
slouken@0
   204
		if ( this ) {
slouken@1336
   205
			SDL_free(this);
slouken@0
   206
		}
icculus@4192
   207
		return NULL;
slouken@0
   208
	}
slouken@1336
   209
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   210
slouken@0
   211
	/* Set the function pointers */
slouken@0
   212
	this->OpenAudio = NAS_OpenAudio;
slouken@0
   213
	this->WaitAudio = NAS_WaitAudio;
slouken@0
   214
	this->PlayAudio = NAS_PlayAudio;
slouken@0
   215
	this->GetAudioBuf = NAS_GetAudioBuf;
slouken@0
   216
	this->CloseAudio = NAS_CloseAudio;
slouken@0
   217
slouken@0
   218
	this->free = Audio_DeleteDevice;
slouken@0
   219
slouken@0
   220
	return this;
slouken@0
   221
}
slouken@0
   222
slouken@0
   223
AudioBootStrap NAS_bootstrap = {
slouken@0
   224
	NAS_DRIVER_NAME, "Network Audio System",
slouken@0
   225
	Audio_Available, Audio_CreateDevice
slouken@0
   226
};
slouken@0
   227
slouken@0
   228
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   229
static void NAS_WaitAudio(_THIS)
slouken@0
   230
{
slouken@0
   231
	while ( this->hidden->buf_free < this->hidden->mixlen ) {
slouken@0
   232
		AuEvent ev;
icculus@4192
   233
		NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
icculus@4192
   234
		NAS_AuDispatchEvent(this->hidden->aud, &ev);
slouken@0
   235
	}
slouken@0
   236
}
slouken@0
   237
slouken@0
   238
static void NAS_PlayAudio(_THIS)
slouken@0
   239
{
slouken@0
   240
	while (this->hidden->mixlen > this->hidden->buf_free) { /* We think the buffer is full? Yikes! Ask the server for events,
slouken@0
   241
				    in the hope that some of them is LowWater events telling us more
slouken@0
   242
				    of the buffer is free now than what we think. */
slouken@0
   243
		AuEvent ev;
icculus@4192
   244
		NAS_AuNextEvent(this->hidden->aud, AuTrue, &ev);
icculus@4192
   245
		NAS_AuDispatchEvent(this->hidden->aud, &ev);
slouken@0
   246
	}
slouken@0
   247
	this->hidden->buf_free -= this->hidden->mixlen;
slouken@0
   248
slouken@0
   249
	/* Write the audio data */
icculus@4192
   250
	NAS_AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
slouken@0
   251
slouken@0
   252
	this->hidden->written += this->hidden->mixlen;
slouken@0
   253
	
slouken@0
   254
#ifdef DEBUG_AUDIO
slouken@0
   255
	fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@0
   256
#endif
slouken@0
   257
}
slouken@0
   258
slouken@0
   259
static Uint8 *NAS_GetAudioBuf(_THIS)
slouken@0
   260
{
slouken@0
   261
	return(this->hidden->mixbuf);
slouken@0
   262
}
slouken@0
   263
slouken@0
   264
static void NAS_CloseAudio(_THIS)
slouken@0
   265
{
slouken@0
   266
	if ( this->hidden->mixbuf != NULL ) {
slouken@0
   267
		SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@0
   268
		this->hidden->mixbuf = NULL;
slouken@0
   269
	}
slouken@0
   270
	if ( this->hidden->aud ) {
icculus@4192
   271
		NAS_AuCloseServer(this->hidden->aud);
slouken@0
   272
		this->hidden->aud = 0;
slouken@0
   273
	}
slouken@0
   274
}
slouken@0
   275
slouken@0
   276
static unsigned char sdlformat_to_auformat(unsigned int fmt)
slouken@0
   277
{
slouken@0
   278
  switch (fmt)
slouken@0
   279
    {
slouken@0
   280
    case AUDIO_U8:
slouken@0
   281
      return AuFormatLinearUnsigned8;
slouken@0
   282
    case AUDIO_S8:
slouken@0
   283
      return AuFormatLinearSigned8;
slouken@0
   284
    case AUDIO_U16LSB:
slouken@0
   285
      return AuFormatLinearUnsigned16LSB;
slouken@0
   286
    case AUDIO_U16MSB:
slouken@0
   287
      return AuFormatLinearUnsigned16MSB;
slouken@0
   288
    case AUDIO_S16LSB:
slouken@0
   289
      return AuFormatLinearSigned16LSB;
slouken@0
   290
    case AUDIO_S16MSB:
slouken@0
   291
      return AuFormatLinearSigned16MSB;
slouken@0
   292
    }
slouken@0
   293
  return AuNone;
slouken@0
   294
}
slouken@0
   295
slouken@0
   296
static AuBool
slouken@0
   297
event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
slouken@0
   298
{
slouken@0
   299
	switch (ev->type) {
slouken@0
   300
	case AuEventTypeElementNotify: {
slouken@0
   301
		AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
slouken@0
   302
slouken@0
   303
		switch (event->kind) {
slouken@0
   304
		case AuElementNotifyKindLowWater:
slouken@0
   305
			if (this2->buf_free >= 0) {
slouken@0
   306
				this2->really += event->num_bytes;
slouken@0
   307
				gettimeofday(&this2->last_tv, 0);
slouken@0
   308
				this2->buf_free += event->num_bytes;
slouken@0
   309
			} else {
slouken@0
   310
				this2->buf_free = event->num_bytes;
slouken@0
   311
			}
slouken@0
   312
			break;
slouken@0
   313
		case AuElementNotifyKindState:
slouken@0
   314
			switch (event->cur_state) {
slouken@0
   315
			case AuStatePause:
slouken@0
   316
				if (event->reason != AuReasonUser) {
slouken@0
   317
					if (this2->buf_free >= 0) {
slouken@0
   318
						this2->really += event->num_bytes;
slouken@0
   319
						gettimeofday(&this2->last_tv, 0);
slouken@0
   320
						this2->buf_free += event->num_bytes;
slouken@0
   321
					} else {
slouken@0
   322
						this2->buf_free = event->num_bytes;
slouken@0
   323
					}
slouken@0
   324
				}
slouken@0
   325
				break;
slouken@0
   326
			}
slouken@0
   327
		}
slouken@0
   328
	}
slouken@0
   329
	}
slouken@0
   330
	return AuTrue;
slouken@0
   331
}
slouken@0
   332
slouken@0
   333
static AuDeviceID
slouken@0
   334
find_device(_THIS, int nch)
slouken@0
   335
{
icculus@4192
   336
    /* These "Au" things are all macros, not functions... */
slouken@0
   337
	int i;
slouken@0
   338
	for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
slouken@0
   339
		if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
slouken@0
   340
				AuComponentKindPhysicalOutput) &&
slouken@0
   341
			AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
slouken@0
   342
			return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
slouken@0
   343
		}
slouken@0
   344
	}
slouken@0
   345
	return AuNone;
slouken@0
   346
}
slouken@0
   347
slouken@0
   348
static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   349
{
slouken@0
   350
	AuElement elms[3];
slouken@0
   351
	int buffer_size;
slouken@0
   352
	Uint16 test_format, format;
slouken@0
   353
slouken@0
   354
	this->hidden->mixbuf = NULL;
slouken@0
   355
slouken@0
   356
	/* Try for a closest match on audio format */
slouken@0
   357
	format = 0;
slouken@0
   358
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   359
						! format && test_format; ) {
slouken@0
   360
		format = sdlformat_to_auformat(test_format);
slouken@0
   361
slouken@0
   362
		if (format == AuNone) {
slouken@0
   363
			test_format = SDL_NextAudioFormat();
slouken@0
   364
		}
slouken@0
   365
	}
slouken@0
   366
	if ( format == 0 ) {
slouken@0
   367
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   368
		return(-1);
slouken@0
   369
	}
slouken@0
   370
	spec->format = test_format;
slouken@0
   371
icculus@4192
   372
	this->hidden->aud = NAS_AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@0
   373
	if (this->hidden->aud == 0)
slouken@0
   374
	{
slouken@0
   375
		SDL_SetError("Couldn't open connection to NAS server");
slouken@0
   376
		return (-1);
slouken@0
   377
	}
slouken@0
   378
	
slouken@0
   379
	this->hidden->dev = find_device(this, spec->channels);
icculus@4192
   380
	if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = NAS_AuCreateFlow(this->hidden->aud, NULL)))) {
icculus@4192
   381
		NAS_AuCloseServer(this->hidden->aud);
slouken@0
   382
		this->hidden->aud = 0;
slouken@0
   383
		SDL_SetError("Couldn't find a fitting playback device on NAS server");
slouken@0
   384
		return (-1);
slouken@0
   385
	}
slouken@0
   386
	
slouken@0
   387
	buffer_size = spec->freq;
slouken@0
   388
	if (buffer_size < 4096)
slouken@0
   389
		buffer_size = 4096; 
slouken@0
   390
slouken@0
   391
	if (buffer_size > 32768)
slouken@0
   392
		buffer_size = 32768; /* So that the buffer won't get unmanageably big. */
slouken@0
   393
slouken@0
   394
	/* Calculate the final parameters for this audio specification */
slouken@0
   395
	SDL_CalculateAudioSpec(spec);
slouken@0
   396
slouken@0
   397
	this2 = this->hidden;
slouken@0
   398
icculus@4192
   399
    /* These "Au" things without a NAS_ prefix are macros, not functions... */
slouken@0
   400
	AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue,
slouken@0
   401
				buffer_size, buffer_size / 4, 0, NULL);
slouken@0
   402
	AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq,
slouken@0
   403
				AuUnlimitedSamples, 0, NULL);
icculus@4192
   404
	NAS_AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
icculus@4192
   405
	NAS_AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
slouken@0
   406
				event_handler, (AuPointer) NULL);
slouken@0
   407
icculus@4192
   408
	NAS_AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
slouken@0
   409
slouken@0
   410
	/* Allocate mixing buffer */
slouken@0
   411
	this->hidden->mixlen = spec->size;
slouken@0
   412
	this->hidden->mixbuf = (Uint8 *)SDL_AllocAudioMem(this->hidden->mixlen);
slouken@0
   413
	if ( this->hidden->mixbuf == NULL ) {
slouken@0
   414
		return(-1);
slouken@0
   415
	}
slouken@1336
   416
	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
slouken@0
   417
slouken@0
   418
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   419
	this->hidden->parent = getpid();
slouken@0
   420
slouken@0
   421
	/* We're ready to rock and roll. :-) */
slouken@0
   422
	return(0);
slouken@0
   423
}