src/audio/esd/SDL_esdaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Dec 2009 10:59:51 +0000
changeset 3581 15eea7a1fa97
parent 3162 dc1eb82ffdaa
child 3697 f7b03b6838cb
permissions -rw-r--r--
Implemented SDL_setenv(), moved SDL_putenv() to compat.

Fixes Bugzilla #779.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     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@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@3162
    63
} const esd_functions[] = {
slouken@3162
    64
    SDL_ESD_SYM(esd_open_sound),
slouken@3162
    65
    SDL_ESD_SYM(esd_close), SDL_ESD_SYM(esd_play_stream),
slouken@3162
    66
};
slouken@3162
    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)) {
slouken@3068
   133
            if (kill(this->hidden->parent, 0) < 0 && errno == ESRCH) {
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 */
slouken@2060
   140
    ticks =
slouken@2060
   141
        ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
slouken@1895
   142
    if (ticks > 0) {
slouken@1895
   143
        SDL_Delay(ticks);
slouken@1895
   144
    }
slouken@0
   145
}
slouken@0
   146
slouken@1895
   147
static void
icculus@2049
   148
ESD_PlayDevice(_THIS)
slouken@0
   149
{
icculus@2049
   150
    int written = 0;
slouken@0
   151
slouken@1895
   152
    /* Write the audio data, checking for EAGAIN on broken audio drivers */
slouken@1895
   153
    do {
icculus@2049
   154
        written = write(this->hidden->audio_fd,
slouken@2060
   155
                        this->hidden->mixbuf, 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@2735
   159
    } while ((written < 0) &&
slouken@2735
   160
             ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)));
slouken@0
   161
slouken@1895
   162
    /* Set the next write frame */
icculus@2049
   163
    this->hidden->next_frame += this->hidden->frame_ticks;
slouken@0
   164
slouken@1895
   165
    /* If we couldn't write, assume fatal error for now */
slouken@1895
   166
    if (written < 0) {
slouken@1895
   167
        this->enabled = 0;
slouken@1895
   168
    }
slouken@0
   169
}
slouken@0
   170
slouken@1895
   171
static Uint8 *
icculus@2049
   172
ESD_GetDeviceBuf(_THIS)
slouken@0
   173
{
icculus@2049
   174
    return (this->hidden->mixbuf);
slouken@0
   175
}
slouken@0
   176
slouken@1895
   177
static void
icculus@2049
   178
ESD_CloseDevice(_THIS)
slouken@0
   179
{
icculus@2049
   180
    if (this->hidden != NULL) {
icculus@2049
   181
        if (this->hidden->mixbuf != NULL) {
icculus@2049
   182
            SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@2049
   183
            this->hidden->mixbuf = NULL;
icculus@2049
   184
        }
icculus@2049
   185
        if (this->hidden->audio_fd >= 0) {
icculus@2049
   186
            SDL_NAME(esd_close) (this->hidden->audio_fd);
icculus@2049
   187
            this->hidden->audio_fd = -1;
icculus@2049
   188
        }
icculus@2049
   189
icculus@2049
   190
        SDL_free(this->hidden);
icculus@2049
   191
        this->hidden = NULL;
slouken@1895
   192
    }
slouken@0
   193
}
slouken@0
   194
slouken@0
   195
/* Try to get the name of the program */
slouken@1895
   196
static char *
slouken@1895
   197
get_progname(void)
slouken@0
   198
{
slouken@1895
   199
    char *progname = NULL;
slouken@1402
   200
#ifdef __LINUX__
slouken@1895
   201
    FILE *fp;
slouken@1895
   202
    static char temp[BUFSIZ];
slouken@0
   203
slouken@1895
   204
    SDL_snprintf(temp, SDL_arraysize(temp), "/proc/%d/cmdline", getpid());
slouken@1895
   205
    fp = fopen(temp, "r");
slouken@1895
   206
    if (fp != NULL) {
slouken@1895
   207
        if (fgets(temp, sizeof(temp) - 1, fp)) {
slouken@1895
   208
            progname = SDL_strrchr(temp, '/');
slouken@1895
   209
            if (progname == NULL) {
slouken@1895
   210
                progname = temp;
slouken@1895
   211
            } else {
slouken@1895
   212
                progname = progname + 1;
slouken@1895
   213
            }
slouken@1895
   214
        }
slouken@1895
   215
        fclose(fp);
slouken@1895
   216
    }
slouken@0
   217
#endif
slouken@1895
   218
    return (progname);
slouken@0
   219
}
slouken@0
   220
icculus@2049
   221
slouken@1895
   222
static int
icculus@2049
   223
ESD_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@0
   224
{
icculus@2049
   225
    esd_format_t format = (ESD_STREAM | ESD_PLAY);
icculus@2049
   226
    SDL_AudioFormat test_format = 0;
icculus@2049
   227
    int found = 0;
icculus@2049
   228
icculus@2049
   229
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   230
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   231
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   232
    if (this->hidden == NULL) {
icculus@2049
   233
        SDL_OutOfMemory();
icculus@2049
   234
        return 0;
icculus@2049
   235
    }
icculus@2049
   236
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   237
    this->hidden->audio_fd = -1;
slouken@0
   238
slouken@1895
   239
    /* Convert audio spec to the ESD audio format */
icculus@2049
   240
    /* Try for a closest match on audio format */
icculus@2049
   241
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@2049
   242
         !found && test_format; test_format = SDL_NextAudioFormat()) {
icculus@2049
   243
#ifdef DEBUG_AUDIO
icculus@2049
   244
        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
icculus@2049
   245
#endif
icculus@2049
   246
        found = 1;
icculus@2049
   247
        switch (test_format) {
slouken@2060
   248
        case AUDIO_U8:
slouken@2060
   249
            format |= ESD_BITS8;
slouken@2060
   250
            break;
slouken@2060
   251
        case AUDIO_S16SYS:
slouken@2060
   252
            format |= ESD_BITS16;
slouken@2060
   253
            break;
slouken@2060
   254
        default:
slouken@2060
   255
            found = 0;
slouken@2060
   256
            break;
icculus@2049
   257
        }
slouken@1895
   258
    }
icculus@2049
   259
icculus@2049
   260
    if (!found) {
icculus@2049
   261
        ESD_CloseDevice(this);
icculus@2049
   262
        SDL_SetError("Couldn't find any hardware audio formats");
icculus@2049
   263
        return 0;
icculus@2049
   264
    }
icculus@2049
   265
icculus@2049
   266
    if (this->spec.channels == 1) {
slouken@1895
   267
        format |= ESD_MONO;
slouken@1895
   268
    } else {
slouken@1895
   269
        format |= ESD_STEREO;
slouken@1895
   270
    }
slouken@0
   271
#if 0
slouken@2060
   272
    this->spec.samples = ESD_BUF_SIZE;  /* Darn, no way to change this yet */
slouken@0
   273
#endif
slouken@0
   274
slouken@1895
   275
    /* Open a connection to the ESD audio server */
icculus@2049
   276
    this->hidden->audio_fd =
slouken@2060
   277
        SDL_NAME(esd_play_stream) (format, this->spec.freq, NULL,
slouken@2060
   278
                                   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);
slouken@2060
   288
    this->hidden->frame_ticks =
slouken@2060
   289
        (float) (this->spec.samples * 1000) / this->spec.freq;
icculus@2049
   290
    this->hidden->next_frame = SDL_GetTicks() + this->hidden->frame_ticks;
slouken@0
   291
slouken@1895
   292
    /* Allocate mixing buffer */
icculus@2049
   293
    this->hidden->mixlen = this->spec.size;
icculus@2049
   294
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@2049
   295
    if (this->hidden->mixbuf == NULL) {
icculus@2049
   296
        ESD_CloseDevice(this);
icculus@2049
   297
        SDL_OutOfMemory();
icculus@2049
   298
        return 0;
slouken@1895
   299
    }
icculus@2049
   300
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
slouken@0
   301
slouken@1895
   302
    /* Get the parent process id (we're the parent of the audio thread) */
icculus@2049
   303
    this->hidden->parent = getpid();
slouken@0
   304
slouken@1895
   305
    /* We're ready to rock and roll. :-) */
icculus@2049
   306
    return 1;
slouken@0
   307
}
slouken@1895
   308
icculus@2049
   309
static void
icculus@2049
   310
ESD_Deinitialize(void)
icculus@2049
   311
{
icculus@2049
   312
    UnloadESDLibrary();
icculus@2049
   313
}
icculus@2049
   314
icculus@2049
   315
static int
slouken@2060
   316
ESD_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   317
{
icculus@2049
   318
    if (LoadESDLibrary() < 0) {
icculus@2049
   319
        return 0;
icculus@2049
   320
    } else {
icculus@2049
   321
        int connection = 0;
icculus@2049
   322
icculus@2049
   323
        /* Don't start ESD if it's not running */
icculus@3581
   324
        SDL_setenv("ESD_NO_SPAWN", "1", 0);
icculus@2049
   325
icculus@2049
   326
        connection = SDL_NAME(esd_open_sound) (NULL);
icculus@2049
   327
        if (connection < 0) {
icculus@2049
   328
            UnloadESDLibrary();
icculus@2049
   329
            SDL_SetError("ESD: esd_open_sound failed (no audio server?)");
icculus@2049
   330
            return 0;
icculus@2049
   331
        }
icculus@2049
   332
        SDL_NAME(esd_close) (connection);
icculus@2049
   333
    }
icculus@2049
   334
icculus@2049
   335
    /* Set the function pointers */
icculus@2049
   336
    impl->OpenDevice = ESD_OpenDevice;
icculus@2049
   337
    impl->PlayDevice = ESD_PlayDevice;
icculus@2049
   338
    impl->WaitDevice = ESD_WaitDevice;
icculus@2049
   339
    impl->GetDeviceBuf = ESD_GetDeviceBuf;
icculus@2049
   340
    impl->CloseDevice = ESD_CloseDevice;
icculus@2049
   341
    impl->Deinitialize = ESD_Deinitialize;
icculus@2049
   342
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   343
slouken@2942
   344
    return 2;                   /* return 2 (definitely have a "device"). */
icculus@2049
   345
}
icculus@2049
   346
icculus@2049
   347
icculus@2049
   348
AudioBootStrap ESD_bootstrap = {
icculus@2049
   349
    ESD_DRIVER_NAME, "Enlightened Sound Daemon", ESD_Init, 0
icculus@2049
   350
};
icculus@2049
   351
slouken@1895
   352
/* vi: set ts=4 sw=4 expandtab: */