src/audio/esd/SDL_esdaudio.c
author Sam Lantinga
Sat, 28 Oct 2006 16:48:03 +0000
changeset 2060 866052b01ee5
parent 2049 5f6550e5184f
child 2669 e27bdcc80744
permissions -rw-r--r--
indent is evil
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[] = {
slouken@2060
    64
SDL_ESD_SYM(esd_open_sound),
slouken@2060
    65
        SDL_ESD_SYM(esd_close), SDL_ESD_SYM(esd_play_stream),};
icculus@2049
    66
#undef SDL_ESD_SYM
slouken@294
    67
slouken@1895
    68
static void
slouken@1895
    69
UnloadESDLibrary()
slouken@294
    70
{
icculus@2049
    71
    if (esd_handle != NULL) {
slouken@1895
    72
        SDL_UnloadObject(esd_handle);
slouken@1895
    73
        esd_handle = NULL;
slouken@1895
    74
    }
slouken@294
    75
}
slouken@294
    76
slouken@1895
    77
static int
slouken@1895
    78
LoadESDLibrary(void)
slouken@294
    79
{
slouken@1895
    80
    int i, retval = -1;
slouken@294
    81
icculus@2049
    82
    if (esd_handle == NULL) {
icculus@2049
    83
        esd_handle = SDL_LoadObject(esd_library);
icculus@2049
    84
        if (esd_handle) {
icculus@2049
    85
            retval = 0;
icculus@2049
    86
            for (i = 0; i < SDL_arraysize(esd_functions); ++i) {
icculus@2049
    87
                *esd_functions[i].func =
icculus@2049
    88
                    SDL_LoadFunction(esd_handle, esd_functions[i].name);
icculus@2049
    89
                if (!*esd_functions[i].func) {
icculus@2049
    90
                    retval = -1;
icculus@2049
    91
                    UnloadESDLibrary();
icculus@2049
    92
                    break;
icculus@2049
    93
                }
slouken@1895
    94
            }
slouken@1895
    95
        }
slouken@1895
    96
    }
slouken@1895
    97
    return retval;
slouken@294
    98
}
slouken@294
    99
slouken@294
   100
#else
slouken@294
   101
slouken@1895
   102
static void
slouken@1895
   103
UnloadESDLibrary()
slouken@294
   104
{
slouken@1895
   105
    return;
slouken@294
   106
}
slouken@294
   107
slouken@1895
   108
static int
slouken@1895
   109
LoadESDLibrary(void)
slouken@294
   110
{
slouken@1895
   111
    return 0;
slouken@294
   112
}
slouken@294
   113
slouken@1361
   114
#endif /* SDL_AUDIO_DRIVER_ESD_DYNAMIC */
slouken@294
   115
slouken@0
   116
slouken@0
   117
/* This function waits until it is possible to write a full sound buffer */
slouken@1895
   118
static void
icculus@2049
   119
ESD_WaitDevice(_THIS)
slouken@0
   120
{
slouken@1895
   121
    Sint32 ticks;
slouken@0
   122
slouken@1895
   123
    /* Check to see if the thread-parent process is still alive */
slouken@1895
   124
    {
slouken@1895
   125
        static int cnt = 0;
slouken@1895
   126
        /* Note that this only works with thread implementations 
slouken@1895
   127
           that use a different process id for each thread.
slouken@1895
   128
         */
icculus@2049
   129
        /* Check every 10 loops */
icculus@2049
   130
        if (this->hidden->parent && (((++cnt) % 10) == 0)) {
icculus@2049
   131
            if (kill(this->hidden->parent, 0) < 0) {
slouken@1895
   132
                this->enabled = 0;
slouken@1895
   133
            }
slouken@1895
   134
        }
slouken@1895
   135
    }
slouken@0
   136
slouken@1895
   137
    /* Use timer for general audio synchronization */
slouken@2060
   138
    ticks =
slouken@2060
   139
        ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
slouken@1895
   140
    if (ticks > 0) {
slouken@1895
   141
        SDL_Delay(ticks);
slouken@1895
   142
    }
slouken@0
   143
}
slouken@0
   144
slouken@1895
   145
static void
icculus@2049
   146
ESD_PlayDevice(_THIS)
slouken@0
   147
{
icculus@2049
   148
    int written = 0;
slouken@0
   149
slouken@1895
   150
    /* Write the audio data, checking for EAGAIN on broken audio drivers */
slouken@1895
   151
    do {
icculus@2049
   152
        written = write(this->hidden->audio_fd,
slouken@2060
   153
                        this->hidden->mixbuf, this->hidden->mixlen);
slouken@1895
   154
        if ((written < 0) && ((errno == 0) || (errno == EAGAIN))) {
slouken@1895
   155
            SDL_Delay(1);       /* Let a little CPU time go by */
slouken@1895
   156
        }
slouken@1895
   157
    }
slouken@1895
   158
    while ((written < 0) &&
slouken@1895
   159
           ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
slouken@0
   160
slouken@1895
   161
    /* Set the next write frame */
icculus@2049
   162
    this->hidden->next_frame += this->hidden->frame_ticks;
slouken@0
   163
slouken@1895
   164
    /* If we couldn't write, assume fatal error for now */
slouken@1895
   165
    if (written < 0) {
slouken@1895
   166
        this->enabled = 0;
slouken@1895
   167
    }
slouken@0
   168
}
slouken@0
   169
slouken@1895
   170
static Uint8 *
icculus@2049
   171
ESD_GetDeviceBuf(_THIS)
slouken@0
   172
{
icculus@2049
   173
    return (this->hidden->mixbuf);
slouken@0
   174
}
slouken@0
   175
slouken@1895
   176
static void
icculus@2049
   177
ESD_CloseDevice(_THIS)
slouken@0
   178
{
icculus@2049
   179
    if (this->hidden != NULL) {
icculus@2049
   180
        if (this->hidden->mixbuf != NULL) {
icculus@2049
   181
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
   182
            this->hidden->mixbuf = NULL;
icculus@2049
   183
        }
icculus@2049
   184
        if (this->hidden->audio_fd >= 0) {
icculus@2049
   185
            SDL_NAME(esd_close) (this->hidden->audio_fd);
icculus@2049
   186
            this->hidden->audio_fd = -1;
icculus@2049
   187
        }
icculus@2049
   188
icculus@2049
   189
        SDL_free(this->hidden);
icculus@2049
   190
        this->hidden = NULL;
slouken@1895
   191
    }
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
/* Try to get the name of the program */
slouken@1895
   195
static char *
slouken@1895
   196
get_progname(void)
slouken@0
   197
{
slouken@1895
   198
    char *progname = NULL;
slouken@1402
   199
#ifdef __LINUX__
slouken@1895
   200
    FILE *fp;
slouken@1895
   201
    static char temp[BUFSIZ];
slouken@0
   202
slouken@1895
   203
    SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
slouken@1895
   204
    fp = fopen(temp, "r");
slouken@1895
   205
    if (fp != NULL) {
slouken@1895
   206
        if (fgets(temp, sizeof(temp) - 1, fp)) {
slouken@1895
   207
            progname = SDL_strrchr(temp, '/');
slouken@1895
   208
            if (progname == NULL) {
slouken@1895
   209
                progname = temp;
slouken@1895
   210
            } else {
slouken@1895
   211
                progname = progname + 1;
slouken@1895
   212
            }
slouken@1895
   213
        }
slouken@1895
   214
        fclose(fp);
slouken@1895
   215
    }
slouken@0
   216
#endif
slouken@1895
   217
    return (progname);
slouken@0
   218
}
slouken@0
   219
icculus@2049
   220
slouken@1895
   221
static int
icculus@2049
   222
ESD_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   223
{
icculus@2049
   224
    esd_format_t format = (ESD_STREAM | ESD_PLAY);
icculus@2049
   225
    SDL_AudioFormat test_format = 0;
icculus@2049
   226
    int found = 0;
icculus@2049
   227
icculus@2049
   228
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   229
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   230
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   231
    if (this->hidden == NULL) {
icculus@2049
   232
        SDL_OutOfMemory();
icculus@2049
   233
        return 0;
icculus@2049
   234
    }
icculus@2049
   235
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   236
    this->hidden->audio_fd = -1;
slouken@0
   237
slouken@1895
   238
    /* Convert audio spec to the ESD audio format */
icculus@2049
   239
    /* Try for a closest match on audio format */
icculus@2049
   240
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@2049
   241
         !found && test_format; test_format = SDL_NextAudioFormat()) {
icculus@2049
   242
#ifdef DEBUG_AUDIO
icculus@2049
   243
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
icculus@2049
   244
#endif
icculus@2049
   245
        found = 1;
icculus@2049
   246
        switch (test_format) {
slouken@2060
   247
        case AUDIO_U8:
slouken@2060
   248
            format |= ESD_BITS8;
slouken@2060
   249
            break;
slouken@2060
   250
        case AUDIO_S16SYS:
slouken@2060
   251
            format |= ESD_BITS16;
slouken@2060
   252
            break;
slouken@2060
   253
        default:
slouken@2060
   254
            found = 0;
slouken@2060
   255
            break;
icculus@2049
   256
        }
slouken@1895
   257
    }
icculus@2049
   258
icculus@2049
   259
    if (!found) {
icculus@2049
   260
        ESD_CloseDevice(this);
icculus@2049
   261
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   262
        return 0;
icculus@2049
   263
    }
icculus@2049
   264
icculus@2049
   265
    if (this->spec.channels == 1) {
slouken@1895
   266
        format |= ESD_MONO;
slouken@1895
   267
    } else {
slouken@1895
   268
        format |= ESD_STEREO;
slouken@1895
   269
    }
slouken@0
   270
#if 0
slouken@2060
   271
    this->spec.samples = ESD_BUF_SIZE;  /* Darn, no way to change this yet */
slouken@0
   272
#endif
slouken@0
   273
slouken@1895
   274
    /* Open a connection to the ESD audio server */
icculus@2049
   275
    this->hidden->audio_fd =
slouken@2060
   276
        SDL_NAME(esd_play_stream) (format, this->spec.freq, NULL,
slouken@2060
   277
                                   get_progname());
icculus@2049
   278
icculus@2049
   279
    if (this->hidden->audio_fd < 0) {
icculus@2049
   280
        ESD_CloseDevice(this);
slouken@1895
   281
        SDL_SetError("Couldn't open ESD connection");
icculus@2049
   282
        return 0;
slouken@1895
   283
    }
slouken@0
   284
slouken@1895
   285
    /* Calculate the final parameters for this audio specification */
icculus@2049
   286
    SDL_CalculateAudioSpec(&this->spec);
slouken@2060
   287
    this->hidden->frame_ticks =
slouken@2060
   288
        (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
slouken@2060
   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: */