src/audio/sndio/SDL_sndioaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 12:00:21 -0400
changeset 11994 8e094f91ddab
parent 11811 5d94cb6b24d3
child 12503 806492103856
permissions -rw-r--r--
thread: fixed compiler warnings on non-Linux systems that use pthread.

(static function rtkit_setpriority was unused, moved it in with rest of
__LINUX__ section.)
icculus@7367
     1
/*
icculus@7367
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
icculus@7367
     4
icculus@7367
     5
  This software is provided 'as-is', without any express or implied
icculus@7367
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@7367
     7
  arising from the use of this software.
icculus@7367
     8
icculus@7367
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@7367
    10
  including commercial applications, and to alter it and redistribute it
icculus@7367
    11
  freely, subject to the following restrictions:
icculus@7367
    12
icculus@7367
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@7367
    14
     claim that you wrote the original software. If you use this software
icculus@7367
    15
     in a product, an acknowledgment in the product documentation would be
icculus@7367
    16
     appreciated but is not required.
icculus@7367
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@7367
    18
     misrepresented as being the original software.
icculus@7367
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@7367
    20
*/
icculus@7367
    21
icculus@8093
    22
#include "../../SDL_internal.h"
icculus@7367
    23
icculus@7367
    24
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    25
icculus@7367
    26
/* OpenBSD sndio target */
icculus@7367
    27
icculus@7367
    28
#if HAVE_STDIO_H
icculus@7367
    29
#include <stdio.h>
icculus@7367
    30
#endif
icculus@7367
    31
icculus@7367
    32
#ifdef HAVE_SIGNAL_H
icculus@7367
    33
#include <signal.h>
icculus@7367
    34
#endif
icculus@7367
    35
slouken@11148
    36
#include <poll.h>
icculus@7367
    37
#include <unistd.h>
icculus@7367
    38
icculus@7367
    39
#include "SDL_audio.h"
icculus@7367
    40
#include "../SDL_audio_c.h"
icculus@7367
    41
#include "SDL_sndioaudio.h"
icculus@7367
    42
icculus@7367
    43
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    44
#include "SDL_loadso.h"
icculus@7367
    45
#endif
icculus@7367
    46
icculus@11193
    47
#ifndef INFTIM
icculus@11193
    48
#define INFTIM -1
icculus@11193
    49
#endif
icculus@11193
    50
icculus@11443
    51
#ifndef SIO_DEVANY
icculus@11443
    52
#define SIO_DEVANY "default"
icculus@11443
    53
#endif
icculus@11443
    54
icculus@7367
    55
static struct sio_hdl * (*SNDIO_sio_open)(const char *, unsigned int, int);
icculus@7367
    56
static void (*SNDIO_sio_close)(struct sio_hdl *);
icculus@7367
    57
static int (*SNDIO_sio_setpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    58
static int (*SNDIO_sio_getpar)(struct sio_hdl *, struct sio_par *);
icculus@7367
    59
static int (*SNDIO_sio_start)(struct sio_hdl *);
icculus@7367
    60
static int (*SNDIO_sio_stop)(struct sio_hdl *);
icculus@7367
    61
static size_t (*SNDIO_sio_read)(struct sio_hdl *, void *, size_t);
icculus@7367
    62
static size_t (*SNDIO_sio_write)(struct sio_hdl *, const void *, size_t);
icculus@11152
    63
static int (*SNDIO_sio_nfds)(struct sio_hdl *);
icculus@11152
    64
static int (*SNDIO_sio_pollfd)(struct sio_hdl *, struct pollfd *, int);
icculus@11152
    65
static int (*SNDIO_sio_revents)(struct sio_hdl *, struct pollfd *);
icculus@11152
    66
static int (*SNDIO_sio_eof)(struct sio_hdl *);
icculus@7367
    67
static void (*SNDIO_sio_initpar)(struct sio_par *);
icculus@7367
    68
icculus@7367
    69
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
    70
static const char *sndio_library = SDL_AUDIO_DRIVER_SNDIO_DYNAMIC;
icculus@7367
    71
static void *sndio_handle = NULL;
icculus@7367
    72
icculus@7367
    73
static int
icculus@7367
    74
load_sndio_sym(const char *fn, void **addr)
icculus@7367
    75
{
icculus@7367
    76
    *addr = SDL_LoadFunction(sndio_handle, fn);
icculus@7367
    77
    if (*addr == NULL) {
icculus@7367
    78
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
icculus@7367
    79
        return 0;
icculus@7367
    80
    }
icculus@7367
    81
icculus@7367
    82
    return 1;
icculus@7367
    83
}
icculus@7367
    84
icculus@7367
    85
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
icculus@7367
    86
#define SDL_SNDIO_SYM(x) \
icculus@7367
    87
    if (!load_sndio_sym(#x, (void **) (char *) &SNDIO_##x)) return -1
icculus@7367
    88
#else
icculus@7367
    89
#define SDL_SNDIO_SYM(x) SNDIO_##x = x
icculus@7367
    90
#endif
icculus@7367
    91
icculus@7367
    92
static int
icculus@7367
    93
load_sndio_syms(void)
icculus@7367
    94
{
icculus@7367
    95
    SDL_SNDIO_SYM(sio_open);
icculus@7367
    96
    SDL_SNDIO_SYM(sio_close);
icculus@7367
    97
    SDL_SNDIO_SYM(sio_setpar);
icculus@7367
    98
    SDL_SNDIO_SYM(sio_getpar);
icculus@7367
    99
    SDL_SNDIO_SYM(sio_start);
icculus@7367
   100
    SDL_SNDIO_SYM(sio_stop);
icculus@7367
   101
    SDL_SNDIO_SYM(sio_read);
icculus@7367
   102
    SDL_SNDIO_SYM(sio_write);
icculus@11152
   103
    SDL_SNDIO_SYM(sio_nfds);
icculus@11152
   104
    SDL_SNDIO_SYM(sio_pollfd);
icculus@11152
   105
    SDL_SNDIO_SYM(sio_revents);
icculus@11152
   106
    SDL_SNDIO_SYM(sio_eof);
icculus@7367
   107
    SDL_SNDIO_SYM(sio_initpar);
icculus@7367
   108
    return 0;
icculus@7367
   109
}
icculus@7367
   110
icculus@7367
   111
#undef SDL_SNDIO_SYM
icculus@7367
   112
icculus@7367
   113
#ifdef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC
icculus@7367
   114
icculus@7367
   115
static void
icculus@7367
   116
UnloadSNDIOLibrary(void)
icculus@7367
   117
{
icculus@7367
   118
    if (sndio_handle != NULL) {
icculus@7367
   119
        SDL_UnloadObject(sndio_handle);
icculus@7367
   120
        sndio_handle = NULL;
icculus@7367
   121
    }
icculus@7367
   122
}
icculus@7367
   123
icculus@7367
   124
static int
icculus@7367
   125
LoadSNDIOLibrary(void)
icculus@7367
   126
{
icculus@7367
   127
    int retval = 0;
icculus@7367
   128
    if (sndio_handle == NULL) {
icculus@7367
   129
        sndio_handle = SDL_LoadObject(sndio_library);
icculus@7367
   130
        if (sndio_handle == NULL) {
icculus@7367
   131
            retval = -1;
icculus@7367
   132
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
icculus@7367
   133
        } else {
icculus@7367
   134
            retval = load_sndio_syms();
icculus@7367
   135
            if (retval < 0) {
icculus@7367
   136
                UnloadSNDIOLibrary();
icculus@7367
   137
            }
icculus@7367
   138
        }
icculus@7367
   139
    }
icculus@7367
   140
    return retval;
icculus@7367
   141
}
icculus@7367
   142
icculus@7367
   143
#else
icculus@7367
   144
icculus@7367
   145
static void
icculus@7367
   146
UnloadSNDIOLibrary(void)
icculus@7367
   147
{
icculus@7367
   148
}
icculus@7367
   149
icculus@7367
   150
static int
icculus@7367
   151
LoadSNDIOLibrary(void)
icculus@7367
   152
{
icculus@7367
   153
    load_sndio_syms();
icculus@7367
   154
    return 0;
icculus@7367
   155
}
icculus@7367
   156
icculus@7367
   157
#endif /* SDL_AUDIO_DRIVER_SNDIO_DYNAMIC */
icculus@7367
   158
icculus@7367
   159
icculus@7367
   160
icculus@7367
   161
icculus@7367
   162
static void
icculus@7367
   163
SNDIO_WaitDevice(_THIS)
icculus@7367
   164
{
icculus@7377
   165
    /* no-op; SNDIO_sio_write() blocks if necessary. */
icculus@7367
   166
}
icculus@7367
   167
icculus@7367
   168
static void
icculus@7367
   169
SNDIO_PlayDevice(_THIS)
icculus@7367
   170
{
icculus@7377
   171
    const int written = SNDIO_sio_write(this->hidden->dev,
icculus@7377
   172
                                        this->hidden->mixbuf,
icculus@7377
   173
                                        this->hidden->mixlen);
icculus@7367
   174
icculus@7367
   175
    /* If we couldn't write, assume fatal error for now */
icculus@7367
   176
    if ( written == 0 ) {
icculus@9394
   177
        SDL_OpenedAudioDeviceDisconnected(this);
icculus@7367
   178
    }
icculus@7367
   179
#ifdef DEBUG_AUDIO
icculus@7367
   180
    fprintf(stderr, "Wrote %d bytes of audio data\n", written);
icculus@7367
   181
#endif
icculus@7367
   182
}
icculus@7367
   183
slouken@11148
   184
static int
slouken@11148
   185
SNDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
slouken@11148
   186
{
icculus@11152
   187
    size_t r;
slouken@11148
   188
    int revents;
slouken@11148
   189
    int nfds;
slouken@11148
   190
slouken@11148
   191
    /* Emulate a blocking read */
icculus@11152
   192
    r = SNDIO_sio_read(this->hidden->dev, buffer, buflen);
icculus@11152
   193
    while (r == 0 && !SNDIO_sio_eof(this->hidden->dev)) {
slouken@11148
   194
        if ((nfds = SNDIO_sio_pollfd(this->hidden->dev, this->hidden->pfd, POLLIN)) <= 0
icculus@11153
   195
            || poll(this->hidden->pfd, nfds, INFTIM) < 0) {
icculus@11152
   196
            return -1;
slouken@11148
   197
        }
icculus@11152
   198
        revents = SNDIO_sio_revents(this->hidden->dev, this->hidden->pfd);
slouken@11148
   199
        if (revents & POLLIN) {
icculus@11152
   200
            r = SNDIO_sio_read(this->hidden->dev, buffer, buflen);
slouken@11148
   201
        }
slouken@11148
   202
        if (revents & POLLHUP) {
slouken@11148
   203
            break;
slouken@11148
   204
        }
slouken@11148
   205
    }
icculus@11152
   206
    return (int) r;
slouken@11148
   207
}
slouken@11148
   208
slouken@11148
   209
static void
slouken@11148
   210
SNDIO_FlushCapture(_THIS)
slouken@11148
   211
{
slouken@11148
   212
    char buf[512];
slouken@11148
   213
slouken@11148
   214
    while (SNDIO_sio_read(this->hidden->dev, buf, sizeof(buf)) != 0) {
slouken@11148
   215
        /* do nothing */;
slouken@11148
   216
    }
slouken@11148
   217
}
slouken@11148
   218
icculus@7367
   219
static Uint8 *
icculus@7367
   220
SNDIO_GetDeviceBuf(_THIS)
icculus@7367
   221
{
icculus@7367
   222
    return this->hidden->mixbuf;
icculus@7367
   223
}
icculus@7367
   224
icculus@7367
   225
static void
icculus@7367
   226
SNDIO_CloseDevice(_THIS)
icculus@7367
   227
{
slouken@11148
   228
    if ( this->hidden->pfd != NULL ) {
slouken@11148
   229
        SDL_free(this->hidden->pfd);
slouken@11148
   230
    }
icculus@10255
   231
    if ( this->hidden->dev != NULL ) {
icculus@10473
   232
        SNDIO_sio_stop(this->hidden->dev);
icculus@10255
   233
        SNDIO_sio_close(this->hidden->dev);
icculus@7367
   234
    }
icculus@10256
   235
    SDL_free(this->hidden->mixbuf);
icculus@10255
   236
    SDL_free(this->hidden);
icculus@7367
   237
}
icculus@7367
   238
icculus@7367
   239
static int
icculus@9394
   240
SNDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
icculus@7367
   241
{
icculus@7367
   242
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@7367
   243
    struct sio_par par;
icculus@7367
   244
    int status;
icculus@7367
   245
icculus@7367
   246
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@7367
   247
        SDL_malloc(sizeof(*this->hidden));
icculus@7367
   248
    if (this->hidden == NULL) {
icculus@7367
   249
        return SDL_OutOfMemory();
icculus@7367
   250
    }
icculus@10257
   251
    SDL_zerop(this->hidden);
icculus@7367
   252
icculus@7367
   253
    this->hidden->mixlen = this->spec.size;
icculus@7367
   254
slouken@11148
   255
    /* Capture devices must be non-blocking for SNDIO_FlushCapture */
slouken@11148
   256
    if ((this->hidden->dev =
icculus@11152
   257
        SNDIO_sio_open(devname != NULL ? devname : SIO_DEVANY,
slouken@11148
   258
                       iscapture ? SIO_REC : SIO_PLAY, iscapture)) == NULL) {
icculus@7367
   259
        return SDL_SetError("sio_open() failed");
icculus@7367
   260
    }
icculus@7367
   261
slouken@11148
   262
    /* Allocate the pollfd array for capture devices */
slouken@11148
   263
    if (iscapture && (this->hidden->pfd =
slouken@11148
   264
        SDL_malloc(sizeof(struct pollfd) * SNDIO_sio_nfds(this->hidden->dev))) == NULL) {
slouken@11148
   265
        return SDL_OutOfMemory();
slouken@11148
   266
    }
slouken@11148
   267
icculus@7377
   268
    SNDIO_sio_initpar(&par);
icculus@7367
   269
icculus@7367
   270
    par.rate = this->spec.freq;
icculus@7367
   271
    par.pchan = this->spec.channels;
icculus@7367
   272
    par.round = this->spec.samples;
icculus@7367
   273
    par.appbufsz = par.round * 2;
icculus@7367
   274
icculus@7367
   275
    /* Try for a closest match on audio format */
icculus@7367
   276
    status = -1;
icculus@7367
   277
    while (test_format && (status < 0)) {
icculus@7367
   278
        if (!SDL_AUDIO_ISFLOAT(test_format)) {
icculus@7367
   279
            par.le = SDL_AUDIO_ISLITTLEENDIAN(test_format) ? 1 : 0;
icculus@7367
   280
            par.sig = SDL_AUDIO_ISSIGNED(test_format) ? 1 : 0;
icculus@7367
   281
            par.bits = SDL_AUDIO_BITSIZE(test_format);
icculus@7367
   282
slouken@8867
   283
            if (SNDIO_sio_setpar(this->hidden->dev, &par) == 0) {
slouken@8867
   284
                continue;
slouken@8867
   285
            }
slouken@8867
   286
            if (SNDIO_sio_getpar(this->hidden->dev, &par) == 0) {
slouken@8867
   287
                return SDL_SetError("sio_getpar() failed");
slouken@8867
   288
            }
slouken@8867
   289
            if (par.bps != SIO_BPS(par.bits)) {
slouken@8867
   290
                continue;
slouken@8867
   291
            }
slouken@8867
   292
            if ((par.bits == 8 * par.bps) || (par.msb)) {
icculus@7367
   293
                status = 0;
icculus@7367
   294
                break;
icculus@7367
   295
            }
icculus@7367
   296
        }
icculus@7367
   297
        test_format = SDL_NextAudioFormat();
icculus@7367
   298
    }
icculus@7367
   299
icculus@7367
   300
    if (status < 0) {
icculus@7367
   301
        return SDL_SetError("sndio: Couldn't find any hardware audio formats");
icculus@7367
   302
    }
icculus@7367
   303
slouken@8867
   304
    if ((par.bps == 4) && (par.sig) && (par.le))
icculus@7367
   305
        this->spec.format = AUDIO_S32LSB;
slouken@8867
   306
    else if ((par.bps == 4) && (par.sig) && (!par.le))
icculus@7367
   307
        this->spec.format = AUDIO_S32MSB;
slouken@8867
   308
    else if ((par.bps == 2) && (par.sig) && (par.le))
icculus@7367
   309
        this->spec.format = AUDIO_S16LSB;
slouken@8867
   310
    else if ((par.bps == 2) && (par.sig) && (!par.le))
icculus@7367
   311
        this->spec.format = AUDIO_S16MSB;
slouken@8867
   312
    else if ((par.bps == 2) && (!par.sig) && (par.le))
icculus@7367
   313
        this->spec.format = AUDIO_U16LSB;
slouken@8867
   314
    else if ((par.bps == 2) && (!par.sig) && (!par.le))
icculus@7367
   315
        this->spec.format = AUDIO_U16MSB;
slouken@8867
   316
    else if ((par.bps == 1) && (par.sig))
icculus@7367
   317
        this->spec.format = AUDIO_S8;
slouken@8867
   318
    else if ((par.bps == 1) && (!par.sig))
icculus@7367
   319
        this->spec.format = AUDIO_U8;
icculus@7367
   320
    else {
icculus@7367
   321
        return SDL_SetError("sndio: Got unsupported hardware audio format.");
icculus@7367
   322
    }
icculus@7367
   323
icculus@7367
   324
    this->spec.freq = par.rate;
icculus@7367
   325
    this->spec.channels = par.pchan;
icculus@7367
   326
    this->spec.samples = par.round;
icculus@7367
   327
icculus@7367
   328
    /* Calculate the final parameters for this audio specification */
icculus@7367
   329
    SDL_CalculateAudioSpec(&this->spec);
icculus@7367
   330
icculus@7367
   331
    /* Allocate mixing buffer */
icculus@7367
   332
    this->hidden->mixlen = this->spec.size;
icculus@10256
   333
    this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->hidden->mixlen);
icculus@7367
   334
    if (this->hidden->mixbuf == NULL) {
icculus@7367
   335
        return SDL_OutOfMemory();
icculus@7367
   336
    }
icculus@7367
   337
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);
icculus@7367
   338
icculus@7377
   339
    if (!SNDIO_sio_start(this->hidden->dev)) {
icculus@7367
   340
        return SDL_SetError("sio_start() failed");
icculus@7367
   341
    }
icculus@7367
   342
icculus@7367
   343
    /* We're ready to rock and roll. :-) */
icculus@7367
   344
    return 0;
icculus@7367
   345
}
icculus@7367
   346
icculus@7367
   347
static void
icculus@7367
   348
SNDIO_Deinitialize(void)
icculus@7367
   349
{
icculus@7367
   350
    UnloadSNDIOLibrary();
icculus@7367
   351
}
icculus@7367
   352
icculus@7367
   353
static int
icculus@7367
   354
SNDIO_Init(SDL_AudioDriverImpl * impl)
icculus@7367
   355
{
icculus@7367
   356
    if (LoadSNDIOLibrary() < 0) {
icculus@7367
   357
        return 0;
icculus@7367
   358
    }
icculus@7367
   359
icculus@7367
   360
    /* Set the function pointers */
icculus@7367
   361
    impl->OpenDevice = SNDIO_OpenDevice;
icculus@7367
   362
    impl->WaitDevice = SNDIO_WaitDevice;
icculus@7367
   363
    impl->PlayDevice = SNDIO_PlayDevice;
icculus@7367
   364
    impl->GetDeviceBuf = SNDIO_GetDeviceBuf;
icculus@7367
   365
    impl->CloseDevice = SNDIO_CloseDevice;
slouken@11148
   366
    impl->CaptureFromDevice = SNDIO_CaptureFromDevice;
slouken@11148
   367
    impl->FlushCapture = SNDIO_FlushCapture;
icculus@7367
   368
    impl->Deinitialize = SNDIO_Deinitialize;
slouken@11148
   369
slouken@11148
   370
    impl->AllowsArbitraryDeviceNames = 1;
slouken@11148
   371
    impl->HasCaptureSupport = SDL_TRUE;
icculus@7367
   372
icculus@7367
   373
    return 1;   /* this audio target is available. */
icculus@7367
   374
}
icculus@7367
   375
icculus@7367
   376
AudioBootStrap SNDIO_bootstrap = {
icculus@7367
   377
    "sndio", "OpenBSD sndio", SNDIO_Init, 0
icculus@7367
   378
};
icculus@7367
   379
icculus@7367
   380
#endif /* SDL_AUDIO_DRIVER_SNDIO */
icculus@7367
   381
icculus@7367
   382
/* vi: set ts=4 sw=4 expandtab: */