src/audio/esd/SDL_esdaudio.c
author Ryan C. Gordon
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
parent 1895 c121d94672cb
child 2060 866052b01ee5
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
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@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@1361
    48
#ifdef SDL_AUDIO_DRIVER_ESD_DYNAMIC
slouken@294
    49
slouken@1361
    50
static const char *esd_library = SDL_AUDIO_DRIVER_ESD_DYNAMIC;
slouken@294
    51
static void *esd_handle = NULL;
slouken@294
    52
slouken@1895
    53
static int (*SDL_NAME(esd_open_sound)) (const char *host);
slouken@1895
    54
static int (*SDL_NAME(esd_close)) (int esd);
slouken@1895
    55
static int (*SDL_NAME(esd_play_stream)) (esd_format_t format, int rate,
slouken@1895
    56
                                         const char *host, const char *name);
icculus@2049
    57
icculus@2049
    58
#define SDL_ESD_SYM(x) { #x, (void **) (char *) &SDL_NAME(x) }
slouken@1895
    59
static struct
slouken@1895
    60
{
slouken@1895
    61
    const char *name;
slouken@1895
    62
    void **func;
slouken@294
    63
} esd_functions[] = {
icculus@2049
    64
    SDL_ESD_SYM(esd_open_sound),
icculus@2049
    65
    SDL_ESD_SYM(esd_close),
icculus@2049
    66
    SDL_ESD_SYM(esd_play_stream),
icculus@2049
    67
};
icculus@2049
    68
#undef SDL_ESD_SYM
slouken@294
    69
slouken@1895
    70
static void
slouken@1895
    71
UnloadESDLibrary()
slouken@294
    72
{
icculus@2049
    73
    if (esd_handle != NULL) {
slouken@1895
    74
        SDL_UnloadObject(esd_handle);
slouken@1895
    75
        esd_handle = NULL;
slouken@1895
    76
    }
slouken@294
    77
}
slouken@294
    78
slouken@1895
    79
static int
slouken@1895
    80
LoadESDLibrary(void)
slouken@294
    81
{
slouken@1895
    82
    int i, retval = -1;
slouken@294
    83
icculus@2049
    84
    if (esd_handle == NULL) {
icculus@2049
    85
        esd_handle = SDL_LoadObject(esd_library);
icculus@2049
    86
        if (esd_handle) {
icculus@2049
    87
            retval = 0;
icculus@2049
    88
            for (i = 0; i < SDL_arraysize(esd_functions); ++i) {
icculus@2049
    89
                *esd_functions[i].func =
icculus@2049
    90
                    SDL_LoadFunction(esd_handle, esd_functions[i].name);
icculus@2049
    91
                if (!*esd_functions[i].func) {
icculus@2049
    92
                    retval = -1;
icculus@2049
    93
                    UnloadESDLibrary();
icculus@2049
    94
                    break;
icculus@2049
    95
                }
slouken@1895
    96
            }
slouken@1895
    97
        }
slouken@1895
    98
    }
slouken@1895
    99
    return retval;
slouken@294
   100
}
slouken@294
   101
slouken@294
   102
#else
slouken@294
   103
slouken@1895
   104
static void
slouken@1895
   105
UnloadESDLibrary()
slouken@294
   106
{
slouken@1895
   107
    return;
slouken@294
   108
}
slouken@294
   109
slouken@1895
   110
static int
slouken@1895
   111
LoadESDLibrary(void)
slouken@294
   112
{
slouken@1895
   113
    return 0;
slouken@294
   114
}
slouken@294
   115
slouken@1361
   116
#endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
slouken@294
   117
slouken@0
   118
slouken@0
   119
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   120
static void
icculus@2049
   121
ESD_WaitDevice(_THIS)
slouken@0
   122
{
slouken@1895
   123
    Sint32 ticks;
slouken@0
   124
slouken@1895
   125
    /* Check to see if the thread-parent process is still alive */
slouken@1895
   126
    {
slouken@1895
   127
        static int cnt = 0;
slouken@1895
   128
        /* Note that this only works with thread implementations 
slouken@1895
   129
           that use a different process id for each thread.
slouken@1895
   130
         */
icculus@2049
   131
        /* Check every 10 loops */
icculus@2049
   132
        if (this->hidden->parent && (((++cnt) % 10) == 0)) {
icculus@2049
   133
            if (kill(this->hidden->parent, 0) < 0) {
slouken@1895
   134
                this->enabled = 0;
slouken@1895
   135
            }
slouken@1895
   136
        }
slouken@1895
   137
    }
slouken@0
   138
slouken@1895
   139
    /* Use timer for general audio synchronization */
icculus@2049
   140
    ticks = ((Sint32) (this->hidden->next_frame-SDL_GetTicks())) - FUDGE_TICKS;
slouken@1895
   141
    if (ticks > 0) {
slouken@1895
   142
        SDL_Delay(ticks);
slouken@1895
   143
    }
slouken@0
   144
}
slouken@0
   145
slouken@1895
   146
static void
icculus@2049
   147
ESD_PlayDevice(_THIS)
slouken@0
   148
{
icculus@2049
   149
    int written = 0;
slouken@0
   150
slouken@1895
   151
    /* Write the audio data, checking for EAGAIN on broken audio drivers */
slouken@1895
   152
    do {
icculus@2049
   153
        written = write(this->hidden->audio_fd,
icculus@2049
   154
                        this->hidden->mixbuf,
icculus@2049
   155
                        this->hidden->mixlen);
slouken@1895
   156
        if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) {
slouken@1895
   157
            SDL_Delay(1);       /* Let a little CPU time go by */
slouken@1895
   158
        }
slouken@1895
   159
    }
slouken@1895
   160
    while ((written < 0) &&
slouken@1895
   161
           ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
slouken@0
   162
slouken@1895
   163
    /* Set the next write frame */
icculus@2049
   164
    this->hidden->next_frame += this->hidden->frame_ticks;
slouken@0
   165
slouken@1895
   166
    /* If we couldn't write, assume fatal error for now */
slouken@1895
   167
    if (written < 0) {
slouken@1895
   168
        this->enabled = 0;
slouken@1895
   169
    }
slouken@0
   170
}
slouken@0
   171
slouken@1895
   172
static Uint8 *
icculus@2049
   173
ESD_GetDeviceBuf(_THIS)
slouken@0
   174
{
icculus@2049
   175
    return (this->hidden->mixbuf);
slouken@0
   176
}
slouken@0
   177
slouken@1895
   178
static void
icculus@2049
   179
ESD_CloseDevice(_THIS)
slouken@0
   180
{
icculus@2049
   181
    if (this->hidden != NULL) {
icculus@2049
   182
        if (this->hidden->mixbuf != NULL) {
icculus@2049
   183
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
   184
            this->hidden->mixbuf = NULL;
icculus@2049
   185
        }
icculus@2049
   186
        if (this->hidden->audio_fd >= 0) {
icculus@2049
   187
            SDL_NAME(esd_close) (this->hidden->audio_fd);
icculus@2049
   188
            this->hidden->audio_fd = -1;
icculus@2049
   189
        }
icculus@2049
   190
icculus@2049
   191
        SDL_free(this->hidden);
icculus@2049
   192
        this->hidden = NULL;
slouken@1895
   193
    }
slouken@0
   194
}
slouken@0
   195
slouken@0
   196
/* Try to get the name of the program */
slouken@1895
   197
static char *
slouken@1895
   198
get_progname(void)
slouken@0
   199
{
slouken@1895
   200
    char *progname = NULL;
slouken@1402
   201
#ifdef __LINUX__
slouken@1895
   202
    FILE *fp;
slouken@1895
   203
    static char temp[BUFSIZ];
slouken@0
   204
slouken@1895
   205
    SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
slouken@1895
   206
    fp = fopen(temp, "r");
slouken@1895
   207
    if (fp != NULL) {
slouken@1895
   208
        if (fgets(temp, sizeof(temp) - 1, fp)) {
slouken@1895
   209
            progname = SDL_strrchr(temp, '/');
slouken@1895
   210
            if (progname == NULL) {
slouken@1895
   211
                progname = temp;
slouken@1895
   212
            } else {
slouken@1895
   213
                progname = progname + 1;
slouken@1895
   214
            }
slouken@1895
   215
        }
slouken@1895
   216
        fclose(fp);
slouken@1895
   217
    }
slouken@0
   218
#endif
slouken@1895
   219
    return (progname);
slouken@0
   220
}
slouken@0
   221
icculus@2049
   222
slouken@1895
   223
static int
icculus@2049
   224
ESD_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   225
{
icculus@2049
   226
    esd_format_t format = (ESD_STREAM | ESD_PLAY);
icculus@2049
   227
    SDL_AudioFormat test_format = 0;
icculus@2049
   228
    int found = 0;
icculus@2049
   229
icculus@2049
   230
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   231
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@2049
   232
                        SDL_malloc((sizeof *this->hidden));
icculus@2049
   233
    if (this->hidden == NULL) {
icculus@2049
   234
        SDL_OutOfMemory();
icculus@2049
   235
        return 0;
icculus@2049
   236
    }
icculus@2049
   237
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   238
    this->hidden->audio_fd = -1;
slouken@0
   239
slouken@1895
   240
    /* Convert audio spec to the ESD audio format */
icculus@2049
   241
    /* Try for a closest match on audio format */
icculus@2049
   242
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@2049
   243
         !found && test_format; test_format = SDL_NextAudioFormat()) {
icculus@2049
   244
#ifdef DEBUG_AUDIO
icculus@2049
   245
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
icculus@2049
   246
#endif
icculus@2049
   247
        found = 1;
icculus@2049
   248
        switch (test_format) {
icculus@2049
   249
            case AUDIO_U8:
icculus@2049
   250
                format |= ESD_BITS8;
icculus@2049
   251
                break;
icculus@2049
   252
            case AUDIO_S16SYS:
icculus@2049
   253
                format |= ESD_BITS16;
icculus@2049
   254
                break;
icculus@2049
   255
            default:
icculus@2049
   256
                found = 0;
icculus@2049
   257
                break;
icculus@2049
   258
        }
slouken@1895
   259
    }
icculus@2049
   260
icculus@2049
   261
    if (!found) {
icculus@2049
   262
        ESD_CloseDevice(this);
icculus@2049
   263
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   264
        return 0;
icculus@2049
   265
    }
icculus@2049
   266
icculus@2049
   267
    if (this->spec.channels == 1) {
slouken@1895
   268
        format |= ESD_MONO;
slouken@1895
   269
    } else {
slouken@1895
   270
        format |= ESD_STEREO;
slouken@1895
   271
    }
slouken@0
   272
#if 0
icculus@2049
   273
    this->spec.samples = ESD_BUF_SIZE;   /* Darn, no way to change this yet */
slouken@0
   274
#endif
slouken@0
   275
slouken@1895
   276
    /* Open a connection to the ESD audio server */
icculus@2049
   277
    this->hidden->audio_fd =
icculus@2049
   278
        SDL_NAME(esd_play_stream)(format,this->spec.freq,NULL,get_progname());
icculus@2049
   279
icculus@2049
   280
    if (this->hidden->audio_fd < 0) {
icculus@2049
   281
        ESD_CloseDevice(this);
slouken@1895
   282
        SDL_SetError("Couldn't open ESD connection");
icculus@2049
   283
        return 0;
slouken@1895
   284
    }
slouken@0
   285
slouken@1895
   286
    /* Calculate the final parameters for this audio specification */
icculus@2049
   287
    SDL_CalculateAudioSpec(&this->spec);
icculus@2049
   288
    this->hidden->frame_ticks = (float) (this->spec.samples*1000) / this->spec.freq;
icculus@2049
   289
    this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks;
slouken@0
   290
slouken@1895
   291
    /* Allocate mixing buffer */
icculus@2049
   292
    this->hidden->mixlen = this->spec.size;
icculus@2049
   293
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   294
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   295
        ESD_CloseDevice(this);
icculus@2049
   296
        SDL_OutOfMemory();
icculus@2049
   297
        return 0;
slouken@1895
   298
    }
icculus@2049
   299
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   300
slouken@1895
   301
    /* Get the parent process id (we're the parent of the audio thread) */
icculus@2049
   302
    this->hidden->parent = getpid();
slouken@0
   303
slouken@1895
   304
    /* We're ready to rock and roll. :-) */
icculus@2049
   305
    return 1;
icculus@2049
   306
}
icculus@2049
   307
icculus@2049
   308
static void
icculus@2049
   309
ESD_Deinitialize(void)
icculus@2049
   310
{
icculus@2049
   311
    UnloadESDLibrary();
slouken@0
   312
}
slouken@1895
   313
icculus@2049
   314
static int
icculus@2049
   315
ESD_Init(SDL_AudioDriverImpl *impl)
icculus@2049
   316
{
icculus@2049
   317
    if (LoadESDLibrary() < 0) {
icculus@2049
   318
        return 0;
icculus@2049
   319
    } else {
icculus@2049
   320
        int connection = 0;
icculus@2049
   321
icculus@2049
   322
        /* Don't start ESD if it's not running */
icculus@2049
   323
        if (SDL_getenv("ESD_NO_SPAWN") == NULL) {
icculus@2049
   324
            SDL_putenv("ESD_NO_SPAWN=1");
icculus@2049
   325
        }
icculus@2049
   326
icculus@2049
   327
        connection = SDL_NAME(esd_open_sound) (NULL);
icculus@2049
   328
        if (connection < 0) {
icculus@2049
   329
            UnloadESDLibrary();
icculus@2049
   330
            SDL_SetError("ESD: esd_open_sound failed (no audio server?)");
icculus@2049
   331
            return 0;
icculus@2049
   332
        }
icculus@2049
   333
        SDL_NAME(esd_close) (connection);
icculus@2049
   334
    }
icculus@2049
   335
icculus@2049
   336
    /* Set the function pointers */
icculus@2049
   337
    impl->OpenDevice = ESD_OpenDevice;
icculus@2049
   338
    impl->PlayDevice = ESD_PlayDevice;
icculus@2049
   339
    impl->WaitDevice = ESD_WaitDevice;
icculus@2049
   340
    impl->GetDeviceBuf = ESD_GetDeviceBuf;
icculus@2049
   341
    impl->CloseDevice = ESD_CloseDevice;
icculus@2049
   342
    impl->Deinitialize = ESD_Deinitialize;
icculus@2049
   343
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   344
icculus@2049
   345
    return 1;
icculus@2049
   346
}
icculus@2049
   347
icculus@2049
   348
icculus@2049
   349
AudioBootStrap ESD_bootstrap = {
icculus@2049
   350
    ESD_DRIVER_NAME, "Enlightened Sound Daemon", ESD_Init, 0
icculus@2049
   351
};
icculus@2049
   352
slouken@1895
   353
/* vi: set ts=4 sw=4 expandtab: */