src/audio/qsa/SDL_qsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Oct 2013 21:56:15 -0700
changeset 7860 2b0bcdea3a79
parent 7719 31b5f9ff36ca
child 8093 b43765095a6f
permissions -rw-r--r--
Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings

Andreas Ertelt

The problem in question is caused by changeset 7771 (http://hg.libsdl.org/SDL/rev/4434498bf4b9 / https://bugzilla.libsdl.org/show_bug.cgi?id=2121)

The redefinition of __inline__ (introduced by the addition of begin_code.h:128's "|| __STRICT_ANSI__") results in mingw's gcc throwing multiple

warning: always_inline function might not be inlinable [-Wattributes]

as well as a whole bunch of redefinitions of mingw internals which break linking of projects including the SDL2 headers.
slouken@3099
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@3099
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@3099
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@3099
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@3099
    20
*/
slouken@3099
    21
slouken@3099
    22
#include "SDL_config.h"
slouken@3099
    23
slouken@6044
    24
#if SDL_AUDIO_DRIVER_QSA
slouken@6044
    25
slouken@3099
    26
#include <errno.h>
slouken@3099
    27
#include <unistd.h>
slouken@3099
    28
#include <fcntl.h>
slouken@3099
    29
#include <signal.h>
slouken@3099
    30
#include <sys/types.h>
slouken@3099
    31
#include <sys/time.h>
slouken@3099
    32
#include <sched.h>
slouken@3099
    33
#include <sys/select.h>
slouken@3099
    34
#include <sys/neutrino.h>
slouken@3099
    35
#include <sys/asoundlib.h>
slouken@3099
    36
slouken@3099
    37
#include "SDL_timer.h"
slouken@3099
    38
#include "SDL_audio.h"
slouken@3099
    39
#include "../SDL_audiomem.h"
slouken@3099
    40
#include "../SDL_audio_c.h"
slouken@3099
    41
#include "SDL_qsa_audio.h"
slouken@3099
    42
slouken@3099
    43
/* default channel communication parameters */
slouken@3099
    44
#define DEFAULT_CPARAMS_RATE   44100
slouken@3099
    45
#define DEFAULT_CPARAMS_VOICES 1
slouken@3099
    46
slouken@3099
    47
#define DEFAULT_CPARAMS_FRAG_SIZE 4096
slouken@3099
    48
#define DEFAULT_CPARAMS_FRAGS_MIN 1
slouken@3099
    49
#define DEFAULT_CPARAMS_FRAGS_MAX 1
slouken@3099
    50
slouken@3099
    51
#define QSA_NO_WORKAROUNDS  0x00000000
slouken@3099
    52
#define QSA_MMAP_WORKAROUND 0x00000001
slouken@3099
    53
slouken@3099
    54
struct BuggyCards
slouken@3099
    55
{
slouken@3139
    56
    char *cardname;
slouken@3099
    57
    unsigned long bugtype;
slouken@3099
    58
};
slouken@3099
    59
slouken@3099
    60
#define QSA_WA_CARDS             3
slouken@3099
    61
#define QSA_MAX_CARD_NAME_LENGTH 33
slouken@3099
    62
slouken@3139
    63
struct BuggyCards buggycards[QSA_WA_CARDS] = {
slouken@3139
    64
    {"Sound Blaster Live!", QSA_MMAP_WORKAROUND},
slouken@3139
    65
    {"Vortex 8820", QSA_MMAP_WORKAROUND},
slouken@3139
    66
    {"Vortex 8830", QSA_MMAP_WORKAROUND},
slouken@3099
    67
};
slouken@3099
    68
slouken@3099
    69
/* List of found devices */
slouken@3099
    70
#define QSA_MAX_DEVICES       32
slouken@3139
    71
#define QSA_MAX_NAME_LENGTH   81+16     /* Hardcoded in QSA, can't be changed */
slouken@3099
    72
slouken@3099
    73
typedef struct _QSA_Device
slouken@3099
    74
{
slouken@3139
    75
    char name[QSA_MAX_NAME_LENGTH];     /* Long audio device name for SDL  */
slouken@3139
    76
    int cardno;
slouken@3139
    77
    int deviceno;
slouken@3099
    78
} QSA_Device;
slouken@3099
    79
slouken@3099
    80
QSA_Device qsa_playback_device[QSA_MAX_DEVICES];
slouken@3139
    81
uint32_t qsa_playback_devices;
slouken@3099
    82
slouken@3099
    83
QSA_Device qsa_capture_device[QSA_MAX_DEVICES];
slouken@3139
    84
uint32_t qsa_capture_devices;
slouken@3099
    85
slouken@7860
    86
static SDL_INLINE int
slouken@3139
    87
QSA_SetError(const char *fn, int status)
slouken@3099
    88
{
icculus@7038
    89
    return SDL_SetError("QSA: %s() failed: %s", fn, snd_strerror(status));
slouken@3099
    90
}
slouken@3099
    91
slouken@3099
    92
/* card names check to apply the workarounds */
slouken@3139
    93
static int
slouken@3139
    94
QSA_CheckBuggyCards(_THIS, unsigned long checkfor)
slouken@3099
    95
{
slouken@3139
    96
    char scardname[QSA_MAX_CARD_NAME_LENGTH];
slouken@3139
    97
    int it;
slouken@3099
    98
slouken@3139
    99
    if (snd_card_get_name
slouken@3139
   100
        (this->hidden->cardno, scardname, QSA_MAX_CARD_NAME_LENGTH - 1) < 0) {
slouken@3139
   101
        return 0;
slouken@3139
   102
    }
slouken@3099
   103
slouken@3139
   104
    for (it = 0; it < QSA_WA_CARDS; it++) {
slouken@3139
   105
        if (SDL_strcmp(buggycards[it].cardname, scardname) == 0) {
slouken@3139
   106
            if (buggycards[it].bugtype == checkfor) {
slouken@3139
   107
                return 1;
slouken@3139
   108
            }
slouken@3139
   109
        }
slouken@3139
   110
    }
slouken@3099
   111
slouken@3139
   112
    return 0;
slouken@3099
   113
}
slouken@3099
   114
icculus@5590
   115
/* !!! FIXME: does this need to be here? Does the SDL version not work? */
slouken@3139
   116
static void
slouken@3139
   117
QSA_ThreadInit(_THIS)
slouken@3099
   118
{
slouken@3139
   119
    struct sched_param param;
slouken@3139
   120
    int status;
slouken@3099
   121
slouken@3139
   122
    /* Increase default 10 priority to 25 to avoid jerky sound */
slouken@3139
   123
    status = SchedGet(0, 0, &param);
slouken@3139
   124
    param.sched_priority = param.sched_curpriority + 15;
slouken@3139
   125
    status = SchedSet(0, 0, SCHED_NOCHANGE, &param);
slouken@3099
   126
}
slouken@3099
   127
slouken@3099
   128
/* PCM channel parameters initialize function */
slouken@3139
   129
static void
slouken@3139
   130
QSA_InitAudioParams(snd_pcm_channel_params_t * cpars)
slouken@3099
   131
{
slouken@3139
   132
    SDL_memset(cpars, 0, sizeof(snd_pcm_channel_params_t));
slouken@3099
   133
slouken@3139
   134
    cpars->channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   135
    cpars->mode = SND_PCM_MODE_BLOCK;
slouken@3139
   136
    cpars->start_mode = SND_PCM_START_DATA;
slouken@3139
   137
    cpars->stop_mode = SND_PCM_STOP_STOP;
slouken@3139
   138
    cpars->format.format = SND_PCM_SFMT_S16_LE;
slouken@3139
   139
    cpars->format.interleave = 1;
slouken@3139
   140
    cpars->format.rate = DEFAULT_CPARAMS_RATE;
slouken@3139
   141
    cpars->format.voices = DEFAULT_CPARAMS_VOICES;
slouken@3139
   142
    cpars->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
slouken@3139
   143
    cpars->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
slouken@3139
   144
    cpars->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
slouken@3099
   145
}
slouken@3099
   146
slouken@3099
   147
/* This function waits until it is possible to write a full sound buffer */
slouken@3139
   148
static void
slouken@3139
   149
QSA_WaitDevice(_THIS)
slouken@3099
   150
{
slouken@3139
   151
    fd_set wfds;
slouken@3139
   152
    fd_set rfds;
slouken@3139
   153
    int selectret;
slouken@3139
   154
    struct timeval timeout;
slouken@3099
   155
slouken@3139
   156
    if (!this->hidden->iscapture) {
slouken@3139
   157
        FD_ZERO(&wfds);
slouken@3139
   158
        FD_SET(this->hidden->audio_fd, &wfds);
slouken@3139
   159
    } else {
slouken@3139
   160
        FD_ZERO(&rfds);
slouken@3139
   161
        FD_SET(this->hidden->audio_fd, &rfds);
slouken@3139
   162
    }
slouken@3099
   163
slouken@3139
   164
    do {
slouken@3139
   165
        /* Setup timeout for playing one fragment equal to 2 seconds          */
slouken@3139
   166
        /* If timeout occured than something wrong with hardware or driver    */
slouken@3139
   167
        /* For example, Vortex 8820 audio driver stucks on second DAC because */
slouken@3139
   168
        /* it doesn't exist !                                                 */
slouken@3139
   169
        timeout.tv_sec = 2;
slouken@3139
   170
        timeout.tv_usec = 0;
slouken@3139
   171
        this->hidden->timeout_on_wait = 0;
slouken@3099
   172
slouken@3139
   173
        if (!this->hidden->iscapture) {
slouken@3139
   174
            selectret =
slouken@3139
   175
                select(this->hidden->audio_fd + 1, NULL, &wfds, NULL,
slouken@3139
   176
                       &timeout);
slouken@3139
   177
        } else {
slouken@3139
   178
            selectret =
slouken@3139
   179
                select(this->hidden->audio_fd + 1, &rfds, NULL, NULL,
slouken@3139
   180
                       &timeout);
slouken@3139
   181
        }
slouken@3099
   182
slouken@3139
   183
        switch (selectret) {
slouken@3139
   184
        case -1:
slouken@3139
   185
            {
icculus@7038
   186
                SDL_SetError("QSA: select() failed: %s", strerror(errno));
slouken@3139
   187
                return;
slouken@3139
   188
            }
slouken@3139
   189
            break;
slouken@3139
   190
        case 0:
slouken@3139
   191
            {
icculus@7038
   192
                SDL_SetError("QSA: timeout on buffer waiting occured");
slouken@3139
   193
                this->hidden->timeout_on_wait = 1;
slouken@3139
   194
                return;
slouken@3139
   195
            }
slouken@3139
   196
            break;
slouken@3139
   197
        default:
slouken@3139
   198
            {
slouken@3139
   199
                if (!this->hidden->iscapture) {
slouken@3139
   200
                    if (FD_ISSET(this->hidden->audio_fd, &wfds)) {
slouken@3139
   201
                        return;
slouken@3099
   202
                    }
slouken@3139
   203
                } else {
slouken@3139
   204
                    if (FD_ISSET(this->hidden->audio_fd, &rfds)) {
slouken@3139
   205
                        return;
slouken@3099
   206
                    }
slouken@3139
   207
                }
slouken@3139
   208
            }
slouken@3139
   209
            break;
slouken@3139
   210
        }
slouken@3139
   211
    } while (1);
slouken@3099
   212
}
slouken@3099
   213
slouken@3139
   214
static void
slouken@3139
   215
QSA_PlayDevice(_THIS)
slouken@3099
   216
{
slouken@3139
   217
    snd_pcm_channel_status_t cstatus;
slouken@3139
   218
    int written;
slouken@3139
   219
    int status;
slouken@3139
   220
    int towrite;
slouken@3139
   221
    void *pcmbuffer;
slouken@3099
   222
slouken@3139
   223
    if ((!this->enabled) || (!this->hidden)) {
slouken@3139
   224
        return;
slouken@3139
   225
    }
slouken@3099
   226
slouken@3139
   227
    towrite = this->spec.size;
slouken@3139
   228
    pcmbuffer = this->hidden->pcm_buf;
slouken@3099
   229
slouken@3139
   230
    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
slouken@3139
   231
    do {
slouken@3139
   232
        written =
slouken@3139
   233
            snd_pcm_plugin_write(this->hidden->audio_handle, pcmbuffer,
slouken@3139
   234
                                 towrite);
slouken@3139
   235
        if (written != towrite) {
slouken@3139
   236
            /* Check if samples playback got stuck somewhere in hardware or in */
slouken@3139
   237
            /* the audio device driver */
slouken@3139
   238
            if ((errno == EAGAIN) && (written == 0)) {
slouken@3139
   239
                if (this->hidden->timeout_on_wait != 0) {
icculus@7038
   240
                    SDL_SetError("QSA: buffer playback timeout");
slouken@3139
   241
                    return;
slouken@3139
   242
                }
slouken@3099
   243
            }
slouken@3099
   244
slouken@3139
   245
            /* Check for errors or conditions */
slouken@3139
   246
            if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
slouken@3139
   247
                /* Let a little CPU time go by and try to write again */
slouken@3139
   248
                SDL_Delay(1);
slouken@3099
   249
slouken@3139
   250
                /* if we wrote some data */
slouken@3139
   251
                towrite -= written;
slouken@3139
   252
                pcmbuffer += written * this->spec.channels;
slouken@3139
   253
                continue;
slouken@3139
   254
            } else {
slouken@3139
   255
                if ((errno == EINVAL) || (errno == EIO)) {
slouken@3139
   256
                    SDL_memset(&cstatus, 0, sizeof(cstatus));
slouken@3139
   257
                    if (!this->hidden->iscapture) {
slouken@3139
   258
                        cstatus.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   259
                    } else {
slouken@3139
   260
                        cstatus.channel = SND_PCM_CHANNEL_CAPTURE;
slouken@3139
   261
                    }
slouken@3099
   262
slouken@3139
   263
                    status =
slouken@3139
   264
                        snd_pcm_plugin_status(this->hidden->audio_handle,
slouken@3139
   265
                                              &cstatus);
slouken@3139
   266
                    if (status < 0) {
slouken@3139
   267
                        QSA_SetError("snd_pcm_plugin_status", status);
slouken@3139
   268
                        return;
slouken@3139
   269
                    }
slouken@3099
   270
slouken@3139
   271
                    if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) ||
slouken@3139
   272
                        (cstatus.status == SND_PCM_STATUS_READY)) {
slouken@3139
   273
                        if (!this->hidden->iscapture) {
slouken@3139
   274
                            status =
slouken@3139
   275
                                snd_pcm_plugin_prepare(this->hidden->
slouken@3139
   276
                                                       audio_handle,
slouken@3139
   277
                                                       SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   278
                        } else {
slouken@3139
   279
                            status =
slouken@3139
   280
                                snd_pcm_plugin_prepare(this->hidden->
slouken@3139
   281
                                                       audio_handle,
slouken@3139
   282
                                                       SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   283
                        }
slouken@3139
   284
                        if (status < 0) {
slouken@3139
   285
                            QSA_SetError("snd_pcm_plugin_prepare", status);
slouken@3139
   286
                            return;
slouken@3139
   287
                        }
slouken@3139
   288
                    }
slouken@3139
   289
                    continue;
slouken@3139
   290
                } else {
slouken@3139
   291
                    return;
slouken@3139
   292
                }
slouken@3099
   293
            }
slouken@3139
   294
        } else {
slouken@3139
   295
            /* we wrote all remaining data */
slouken@3139
   296
            towrite -= written;
slouken@3139
   297
            pcmbuffer += written * this->spec.channels;
slouken@3139
   298
        }
slouken@3139
   299
    } while ((towrite > 0) && (this->enabled));
slouken@3099
   300
slouken@3139
   301
    /* If we couldn't write, assume fatal error for now */
slouken@3139
   302
    if (towrite != 0) {
slouken@3139
   303
        this->enabled = 0;
slouken@3139
   304
    }
slouken@3099
   305
}
slouken@3099
   306
slouken@3139
   307
static Uint8 *
slouken@3139
   308
QSA_GetDeviceBuf(_THIS)
slouken@3099
   309
{
slouken@3139
   310
    return this->hidden->pcm_buf;
slouken@3099
   311
}
slouken@3099
   312
slouken@3139
   313
static void
slouken@3139
   314
QSA_CloseDevice(_THIS)
slouken@3099
   315
{
slouken@3139
   316
    if (this->hidden != NULL) {
slouken@3139
   317
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   318
            if (!this->hidden->iscapture) {
slouken@3139
   319
                /* Finish playing available samples */
slouken@3139
   320
                snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   321
                                     SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   322
            } else {
slouken@3139
   323
                /* Cancel unread samples during capture */
slouken@3139
   324
                snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   325
                                     SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   326
            }
slouken@3139
   327
            snd_pcm_close(this->hidden->audio_handle);
slouken@3139
   328
            this->hidden->audio_handle = NULL;
slouken@3139
   329
        }
slouken@3099
   330
slouken@7719
   331
        SDL_FreeAudioMem(this->hidden->pcm_buf);
slouken@7719
   332
        this->hidden->pcm_buf = NULL;
slouken@3099
   333
slouken@3139
   334
        SDL_free(this->hidden);
slouken@3139
   335
        this->hidden = NULL;
slouken@3139
   336
    }
slouken@3099
   337
}
slouken@3099
   338
slouken@3139
   339
static int
slouken@3139
   340
QSA_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@3099
   341
{
slouken@3139
   342
    int status = 0;
slouken@3139
   343
    int format = 0;
slouken@3139
   344
    SDL_AudioFormat test_format = 0;
slouken@3139
   345
    int found = 0;
slouken@3139
   346
    snd_pcm_channel_setup_t csetup;
slouken@3139
   347
    snd_pcm_channel_params_t cparams;
slouken@3099
   348
slouken@3139
   349
    /* Initialize all variables that we clean on shutdown */
slouken@3139
   350
    this->hidden =
slouken@3139
   351
        (struct SDL_PrivateAudioData *) SDL_calloc(1,
slouken@3139
   352
                                                   (sizeof
slouken@3139
   353
                                                    (struct
slouken@3139
   354
                                                     SDL_PrivateAudioData)));
slouken@3139
   355
    if (this->hidden == NULL) {
icculus@7038
   356
        return SDL_OutOfMemory();
slouken@3139
   357
    }
slouken@3139
   358
    SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
slouken@3099
   359
slouken@3139
   360
    /* Initialize channel transfer parameters to default */
slouken@3139
   361
    QSA_InitAudioParams(&cparams);
slouken@3099
   362
slouken@3139
   363
    /* Initialize channel direction: capture or playback */
slouken@3139
   364
    this->hidden->iscapture = iscapture;
slouken@3099
   365
slouken@3139
   366
    /* Find deviceid and cardid by device name for playback */
slouken@3139
   367
    if ((!this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   368
        uint32_t device;
slouken@3139
   369
        int32_t status;
slouken@3099
   370
slouken@3139
   371
        /* Search in the playback devices */
slouken@3139
   372
        device = 0;
slouken@3139
   373
        do {
slouken@3139
   374
            status = SDL_strcmp(qsa_playback_device[device].name, devname);
slouken@3139
   375
            if (status == 0) {
slouken@3139
   376
                /* Found requested device */
slouken@3139
   377
                this->hidden->deviceno = qsa_playback_device[device].deviceno;
slouken@3139
   378
                this->hidden->cardno = qsa_playback_device[device].cardno;
slouken@3139
   379
                break;
slouken@3139
   380
            }
slouken@3139
   381
            device++;
slouken@3139
   382
            if (device >= qsa_playback_devices) {
slouken@3139
   383
                QSA_CloseDevice(this);
icculus@7038
   384
                return SDL_SetError("No such playback device");
slouken@3139
   385
            }
slouken@3139
   386
        } while (1);
slouken@3139
   387
    }
slouken@3139
   388
slouken@3139
   389
    /* Find deviceid and cardid by device name for capture */
slouken@3139
   390
    if ((this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   391
        /* Search in the capture devices */
slouken@3139
   392
        uint32_t device;
slouken@3139
   393
        int32_t status;
slouken@3139
   394
slouken@3139
   395
        /* Searching in the playback devices */
slouken@3139
   396
        device = 0;
slouken@3139
   397
        do {
slouken@3139
   398
            status = SDL_strcmp(qsa_capture_device[device].name, devname);
slouken@3139
   399
            if (status == 0) {
slouken@3139
   400
                /* Found requested device */
slouken@3139
   401
                this->hidden->deviceno = qsa_capture_device[device].deviceno;
slouken@3139
   402
                this->hidden->cardno = qsa_capture_device[device].cardno;
slouken@3139
   403
                break;
slouken@3139
   404
            }
slouken@3139
   405
            device++;
slouken@3139
   406
            if (device >= qsa_capture_devices) {
slouken@3139
   407
                QSA_CloseDevice(this);
icculus@7038
   408
                return SDL_SetError("No such capture device");
slouken@3139
   409
            }
slouken@3139
   410
        } while (1);
slouken@3139
   411
    }
slouken@3139
   412
slouken@3139
   413
    /* Check if SDL requested default audio device */
slouken@3139
   414
    if (devname == NULL) {
slouken@3139
   415
        /* Open system default audio device */
slouken@3139
   416
        if (!this->hidden->iscapture) {
slouken@3139
   417
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
slouken@3139
   418
                                            &this->hidden->cardno,
slouken@3139
   419
                                            &this->hidden->deviceno,
slouken@3139
   420
                                            SND_PCM_OPEN_PLAYBACK);
slouken@3139
   421
        } else {
slouken@3139
   422
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
slouken@3139
   423
                                            &this->hidden->cardno,
slouken@3139
   424
                                            &this->hidden->deviceno,
slouken@3139
   425
                                            SND_PCM_OPEN_CAPTURE);
slouken@3139
   426
        }
slouken@3139
   427
    } else {
slouken@3139
   428
        /* Open requested audio device */
slouken@3139
   429
        if (!this->hidden->iscapture) {
slouken@3139
   430
            status =
slouken@3139
   431
                snd_pcm_open(&this->hidden->audio_handle,
slouken@3139
   432
                             this->hidden->cardno, this->hidden->deviceno,
slouken@3139
   433
                             SND_PCM_OPEN_PLAYBACK);
slouken@3139
   434
        } else {
slouken@3139
   435
            status =
slouken@3139
   436
                snd_pcm_open(&this->hidden->audio_handle,
slouken@3139
   437
                             this->hidden->cardno, this->hidden->deviceno,
slouken@3139
   438
                             SND_PCM_OPEN_CAPTURE);
slouken@3139
   439
        }
slouken@3139
   440
    }
slouken@3139
   441
slouken@3139
   442
    /* Check if requested device is opened */
slouken@3139
   443
    if (status < 0) {
slouken@3139
   444
        this->hidden->audio_handle = NULL;
slouken@3139
   445
        QSA_CloseDevice(this);
icculus@7038
   446
        return QSA_SetError("snd_pcm_open", status);
slouken@3139
   447
    }
slouken@3139
   448
slouken@3139
   449
    if (!QSA_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) {
slouken@3139
   450
        /* Disable QSA MMAP plugin for buggy audio drivers */
slouken@3139
   451
        status =
slouken@3139
   452
            snd_pcm_plugin_set_disable(this->hidden->audio_handle,
slouken@3139
   453
                                       PLUGIN_DISABLE_MMAP);
slouken@3139
   454
        if (status < 0) {
slouken@3139
   455
            QSA_CloseDevice(this);
icculus@7038
   456
            return QSA_SetError("snd_pcm_plugin_set_disable", status);
slouken@3139
   457
        }
slouken@3139
   458
    }
slouken@3139
   459
slouken@3139
   460
    /* Try for a closest match on audio format */
slouken@3139
   461
    format = 0;
slouken@3139
   462
    /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
slouken@3139
   463
    found = 0;
slouken@3139
   464
slouken@3139
   465
    for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) {
slouken@3139
   466
        /* if match found set format to equivalent QSA format */
slouken@3139
   467
        switch (test_format) {
slouken@3139
   468
        case AUDIO_U8:
slouken@3139
   469
            {
slouken@3139
   470
                format = SND_PCM_SFMT_U8;
slouken@3139
   471
                found = 1;
slouken@3139
   472
            }
slouken@3099
   473
            break;
slouken@3139
   474
        case AUDIO_S8:
slouken@3139
   475
            {
slouken@3139
   476
                format = SND_PCM_SFMT_S8;
slouken@3139
   477
                found = 1;
slouken@3139
   478
            }
slouken@3139
   479
            break;
slouken@3139
   480
        case AUDIO_S16LSB:
slouken@3139
   481
            {
slouken@3139
   482
                format = SND_PCM_SFMT_S16_LE;
slouken@3139
   483
                found = 1;
slouken@3139
   484
            }
slouken@3139
   485
            break;
slouken@3139
   486
        case AUDIO_S16MSB:
slouken@3139
   487
            {
slouken@3139
   488
                format = SND_PCM_SFMT_S16_BE;
slouken@3139
   489
                found = 1;
slouken@3139
   490
            }
slouken@3139
   491
            break;
slouken@3139
   492
        case AUDIO_U16LSB:
slouken@3139
   493
            {
slouken@3139
   494
                format = SND_PCM_SFMT_U16_LE;
slouken@3139
   495
                found = 1;
slouken@3139
   496
            }
slouken@3139
   497
            break;
slouken@3139
   498
        case AUDIO_U16MSB:
slouken@3139
   499
            {
slouken@3139
   500
                format = SND_PCM_SFMT_U16_BE;
slouken@3139
   501
                found = 1;
slouken@3139
   502
            }
slouken@3139
   503
            break;
slouken@3139
   504
        case AUDIO_S32LSB:
slouken@3139
   505
            {
slouken@3139
   506
                format = SND_PCM_SFMT_S32_LE;
slouken@3139
   507
                found = 1;
slouken@3139
   508
            }
slouken@3139
   509
            break;
slouken@3139
   510
        case AUDIO_S32MSB:
slouken@3139
   511
            {
slouken@3139
   512
                format = SND_PCM_SFMT_S32_BE;
slouken@3139
   513
                found = 1;
slouken@3139
   514
            }
slouken@3139
   515
            break;
slouken@3139
   516
        case AUDIO_F32LSB:
slouken@3139
   517
            {
slouken@3139
   518
                format = SND_PCM_SFMT_FLOAT_LE;
slouken@3139
   519
                found = 1;
slouken@3139
   520
            }
slouken@3139
   521
            break;
slouken@3139
   522
        case AUDIO_F32MSB:
slouken@3139
   523
            {
slouken@3139
   524
                format = SND_PCM_SFMT_FLOAT_BE;
slouken@3139
   525
                found = 1;
slouken@3139
   526
            }
slouken@3139
   527
            break;
slouken@3139
   528
        default:
slouken@3139
   529
            {
slouken@3139
   530
                break;
slouken@3139
   531
            }
slouken@3139
   532
        }
slouken@3099
   533
slouken@3139
   534
        if (!found) {
slouken@3139
   535
            test_format = SDL_NextAudioFormat();
slouken@3139
   536
        }
slouken@3139
   537
    }
slouken@3099
   538
slouken@3139
   539
    /* assumes test_format not 0 on success */
slouken@3139
   540
    if (test_format == 0) {
slouken@3139
   541
        QSA_CloseDevice(this);
icculus@7038
   542
        return SDL_SetError("QSA: Couldn't find any hardware audio formats");
slouken@3139
   543
    }
slouken@3099
   544
slouken@3139
   545
    this->spec.format = test_format;
slouken@3099
   546
slouken@3139
   547
    /* Set the audio format */
slouken@3139
   548
    cparams.format.format = format;
slouken@3099
   549
slouken@3139
   550
    /* Set mono/stereo/4ch/6ch/8ch audio */
slouken@3139
   551
    cparams.format.voices = this->spec.channels;
slouken@3099
   552
slouken@3139
   553
    /* Set rate */
slouken@3139
   554
    cparams.format.rate = this->spec.freq;
slouken@3099
   555
slouken@3139
   556
    /* Setup the transfer parameters according to cparams */
slouken@3139
   557
    status = snd_pcm_plugin_params(this->hidden->audio_handle, &cparams);
slouken@3139
   558
    if (status < 0) {
slouken@3139
   559
        QSA_CloseDevice(this);
icculus@7038
   560
        return QSA_SetError("snd_pcm_channel_params", status);
slouken@3139
   561
    }
slouken@3099
   562
slouken@3139
   563
    /* Make sure channel is setup right one last time */
slouken@6352
   564
    SDL_memset(&csetup, 0, sizeof(csetup));
slouken@3139
   565
    if (!this->hidden->iscapture) {
slouken@3139
   566
        csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   567
    } else {
slouken@3139
   568
        csetup.channel = SND_PCM_CHANNEL_CAPTURE;
slouken@3139
   569
    }
slouken@3099
   570
slouken@3139
   571
    /* Setup an audio channel */
slouken@3139
   572
    if (snd_pcm_plugin_setup(this->hidden->audio_handle, &csetup) < 0) {
slouken@3139
   573
        QSA_CloseDevice(this);
icculus@7038
   574
        return SDL_SetError("QSA: Unable to setup channel");
slouken@3139
   575
    }
slouken@3099
   576
slouken@3139
   577
    /* Calculate the final parameters for this audio specification */
slouken@3139
   578
    SDL_CalculateAudioSpec(&this->spec);
slouken@3099
   579
slouken@3139
   580
    this->hidden->pcm_len = this->spec.size;
slouken@3099
   581
slouken@3139
   582
    if (this->hidden->pcm_len == 0) {
slouken@3139
   583
        this->hidden->pcm_len =
slouken@3139
   584
            csetup.buf.block.frag_size * this->spec.channels *
slouken@3139
   585
            (snd_pcm_format_width(format) / 8);
slouken@3139
   586
    }
slouken@3099
   587
slouken@3139
   588
    /*
slouken@3139
   589
     * Allocate memory to the audio buffer and initialize with silence
slouken@3139
   590
     *  (Note that buffer size must be a multiple of fragment size, so find
slouken@3139
   591
     *  closest multiple)
slouken@3139
   592
     */
slouken@3139
   593
    this->hidden->pcm_buf =
slouken@3139
   594
        (Uint8 *) SDL_AllocAudioMem(this->hidden->pcm_len);
slouken@3139
   595
    if (this->hidden->pcm_buf == NULL) {
slouken@3139
   596
        QSA_CloseDevice(this);
icculus@7038
   597
        return SDL_OutOfMemory();
slouken@3139
   598
    }
slouken@3139
   599
    SDL_memset(this->hidden->pcm_buf, this->spec.silence,
slouken@3139
   600
               this->hidden->pcm_len);
slouken@3099
   601
slouken@3139
   602
    /* get the file descriptor */
slouken@3139
   603
    if (!this->hidden->iscapture) {
slouken@3139
   604
        this->hidden->audio_fd =
slouken@3139
   605
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   606
                                    SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   607
    } else {
slouken@3139
   608
        this->hidden->audio_fd =
slouken@3139
   609
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   610
                                    SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   611
    }
slouken@3099
   612
slouken@3139
   613
    if (this->hidden->audio_fd < 0) {
slouken@3139
   614
        QSA_CloseDevice(this);
icculus@7038
   615
        return QSA_SetError("snd_pcm_file_descriptor", status);
slouken@3139
   616
    }
slouken@3099
   617
slouken@3139
   618
    /* Prepare an audio channel */
slouken@3139
   619
    if (!this->hidden->iscapture) {
slouken@3139
   620
        /* Prepare audio playback */
slouken@3139
   621
        status =
slouken@3139
   622
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   623
                                   SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   624
    } else {
slouken@3139
   625
        /* Prepare audio capture */
slouken@3139
   626
        status =
slouken@3139
   627
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   628
                                   SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   629
    }
slouken@3099
   630
slouken@3139
   631
    if (status < 0) {
slouken@3139
   632
        QSA_CloseDevice(this);
icculus@7038
   633
        return QSA_SetError("snd_pcm_plugin_prepare", status);
slouken@3139
   634
    }
slouken@3099
   635
slouken@3139
   636
    /* We're really ready to rock and roll. :-) */
icculus@7038
   637
    return 0;
slouken@3099
   638
}
slouken@3099
   639
icculus@5593
   640
static void
icculus@5593
   641
QSA_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
slouken@3099
   642
{
slouken@3139
   643
    uint32_t it;
slouken@3139
   644
    uint32_t cards;
slouken@3139
   645
    uint32_t devices;
slouken@3139
   646
    int32_t status;
slouken@3099
   647
slouken@3139
   648
    /* Detect amount of available devices       */
slouken@3139
   649
    /* this value can be changed in the runtime */
slouken@3139
   650
    cards = snd_cards();
slouken@3099
   651
slouken@3139
   652
    /* If io-audio manager is not running we will get 0 as number */
slouken@3139
   653
    /* of available audio devices                                 */
slouken@3139
   654
    if (cards == 0) {
slouken@3139
   655
        /* We have no any available audio devices */
icculus@5593
   656
        return;
slouken@3139
   657
    }
slouken@3099
   658
slouken@3139
   659
    /* Find requested devices by type */
slouken@3139
   660
    if (!iscapture) {
slouken@3139
   661
        /* Playback devices enumeration requested */
slouken@3139
   662
        for (it = 0; it < cards; it++) {
slouken@3139
   663
            devices = 0;
slouken@3139
   664
            do {
slouken@3139
   665
                status =
slouken@3139
   666
                    snd_card_get_longname(it,
slouken@3139
   667
                                          qsa_playback_device
slouken@3139
   668
                                          [qsa_playback_devices].name,
slouken@3139
   669
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   670
                if (status == EOK) {
slouken@3139
   671
                    snd_pcm_t *handle;
slouken@3099
   672
slouken@3139
   673
                    /* Add device number to device name */
slouken@3139
   674
                    sprintf(qsa_playback_device[qsa_playback_devices].name +
slouken@3139
   675
                            SDL_strlen(qsa_playback_device
slouken@3139
   676
                                       [qsa_playback_devices].name), " d%d",
slouken@3139
   677
                            devices);
slouken@3099
   678
slouken@3139
   679
                    /* Store associated card number id */
slouken@3139
   680
                    qsa_playback_device[qsa_playback_devices].cardno = it;
slouken@3099
   681
slouken@3139
   682
                    /* Check if this device id could play anything */
slouken@3139
   683
                    status =
slouken@3139
   684
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   685
                                     SND_PCM_OPEN_PLAYBACK);
slouken@3139
   686
                    if (status == EOK) {
slouken@3139
   687
                        qsa_playback_device[qsa_playback_devices].deviceno =
slouken@3139
   688
                            devices;
slouken@3139
   689
                        status = snd_pcm_close(handle);
slouken@3139
   690
                        if (status == EOK) {
icculus@5593
   691
                            addfn(qsa_playback_device[qsa_playback_devices].name);
slouken@3139
   692
                            qsa_playback_devices++;
slouken@3139
   693
                        }
slouken@3139
   694
                    } else {
slouken@3139
   695
                        /* Check if we got end of devices list */
slouken@3139
   696
                        if (status == -ENOENT) {
slouken@3139
   697
                            break;
slouken@3139
   698
                        }
slouken@3139
   699
                    }
slouken@3139
   700
                } else {
slouken@3139
   701
                    break;
slouken@3139
   702
                }
slouken@3139
   703
slouken@3139
   704
                /* Check if we reached maximum devices count */
slouken@3139
   705
                if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   706
                    break;
slouken@3139
   707
                }
slouken@3139
   708
                devices++;
slouken@3139
   709
            } while (1);
slouken@3099
   710
slouken@3099
   711
            /* Check if we reached maximum devices count */
slouken@3139
   712
            if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   713
                break;
slouken@3099
   714
            }
slouken@3139
   715
        }
slouken@3139
   716
    } else {
slouken@3139
   717
        /* Capture devices enumeration requested */
slouken@3139
   718
        for (it = 0; it < cards; it++) {
slouken@3139
   719
            devices = 0;
slouken@3139
   720
            do {
slouken@3139
   721
                status =
slouken@3139
   722
                    snd_card_get_longname(it,
slouken@3139
   723
                                          qsa_capture_device
slouken@3139
   724
                                          [qsa_capture_devices].name,
slouken@3139
   725
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   726
                if (status == EOK) {
slouken@3139
   727
                    snd_pcm_t *handle;
slouken@3099
   728
slouken@3139
   729
                    /* Add device number to device name */
slouken@3139
   730
                    sprintf(qsa_capture_device[qsa_capture_devices].name +
slouken@3139
   731
                            SDL_strlen(qsa_capture_device
slouken@3139
   732
                                       [qsa_capture_devices].name), " d%d",
slouken@3139
   733
                            devices);
slouken@3099
   734
slouken@3139
   735
                    /* Store associated card number id */
slouken@3139
   736
                    qsa_capture_device[qsa_capture_devices].cardno = it;
slouken@3099
   737
slouken@3139
   738
                    /* Check if this device id could play anything */
slouken@3139
   739
                    status =
slouken@3139
   740
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   741
                                     SND_PCM_OPEN_CAPTURE);
slouken@3139
   742
                    if (status == EOK) {
slouken@3139
   743
                        qsa_capture_device[qsa_capture_devices].deviceno =
slouken@3139
   744
                            devices;
slouken@3139
   745
                        status = snd_pcm_close(handle);
slouken@3139
   746
                        if (status == EOK) {
icculus@5593
   747
                            addfn(qsa_capture_device[qsa_capture_devices].name);
slouken@3139
   748
                            qsa_capture_devices++;
slouken@3139
   749
                        }
slouken@3139
   750
                    } else {
slouken@3139
   751
                        /* Check if we got end of devices list */
slouken@3139
   752
                        if (status == -ENOENT) {
slouken@3139
   753
                            break;
slouken@3139
   754
                        }
slouken@3139
   755
                    }
slouken@3099
   756
slouken@3139
   757
                    /* Check if we reached maximum devices count */
slouken@3139
   758
                    if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   759
                        break;
slouken@3139
   760
                    }
slouken@3139
   761
                } else {
slouken@3139
   762
                    break;
slouken@3139
   763
                }
slouken@3139
   764
                devices++;
slouken@3139
   765
            } while (1);
slouken@3099
   766
slouken@3139
   767
            /* Check if we reached maximum devices count */
slouken@3139
   768
            if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   769
                break;
slouken@3099
   770
            }
slouken@3139
   771
        }
slouken@3139
   772
    }
slouken@3099
   773
}
slouken@3099
   774
icculus@5584
   775
static void
slouken@3139
   776
QSA_WaitDone(_THIS)
slouken@3099
   777
{
slouken@3139
   778
    if (!this->hidden->iscapture) {
slouken@3139
   779
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   780
            /* Wait till last fragment is played and stop channel */
slouken@3139
   781
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   782
                                 SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   783
        }
slouken@3139
   784
    } else {
slouken@3139
   785
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   786
            /* Discard all unread data and stop channel */
slouken@3139
   787
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   788
                                 SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   789
        }
slouken@3139
   790
    }
slouken@3099
   791
}
slouken@3099
   792
icculus@5584
   793
static void
slouken@3139
   794
QSA_Deinitialize(void)
slouken@3099
   795
{
slouken@3139
   796
    /* Clear devices array on shutdown */
slouken@3139
   797
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   798
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   799
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   800
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   801
    qsa_playback_devices = 0;
slouken@3139
   802
    qsa_capture_devices = 0;
slouken@3099
   803
}
slouken@3099
   804
slouken@3139
   805
static int
slouken@3139
   806
QSA_Init(SDL_AudioDriverImpl * impl)
slouken@3099
   807
{
slouken@3139
   808
    snd_pcm_t *handle = NULL;
slouken@3139
   809
    int32_t status = 0;
slouken@3099
   810
slouken@3139
   811
    /* Clear devices array */
slouken@3139
   812
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   813
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   814
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   815
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   816
    qsa_playback_devices = 0;
slouken@3139
   817
    qsa_capture_devices = 0;
slouken@3099
   818
slouken@3139
   819
    /* Set function pointers                                     */
slouken@3139
   820
    /* DeviceLock and DeviceUnlock functions are used default,   */
slouken@3139
   821
    /* provided by SDL, which uses pthread_mutex for lock/unlock */
slouken@3139
   822
    impl->DetectDevices = QSA_DetectDevices;
slouken@3139
   823
    impl->OpenDevice = QSA_OpenDevice;
slouken@3139
   824
    impl->ThreadInit = QSA_ThreadInit;
slouken@3139
   825
    impl->WaitDevice = QSA_WaitDevice;
slouken@3139
   826
    impl->PlayDevice = QSA_PlayDevice;
slouken@3139
   827
    impl->GetDeviceBuf = QSA_GetDeviceBuf;
slouken@3139
   828
    impl->CloseDevice = QSA_CloseDevice;
slouken@3139
   829
    impl->WaitDone = QSA_WaitDone;
slouken@3139
   830
    impl->Deinitialize = QSA_Deinitialize;
slouken@3139
   831
    impl->LockDevice = NULL;
slouken@3139
   832
    impl->UnlockDevice = NULL;
slouken@3099
   833
slouken@3139
   834
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   835
    impl->ProvidesOwnCallbackThread = 0;
slouken@3139
   836
    impl->SkipMixerLock = 0;
slouken@3139
   837
    impl->HasCaptureSupport = 1;
slouken@3139
   838
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   839
    impl->OnlyHasDefaultInputDevice = 0;
slouken@3099
   840
slouken@3139
   841
    /* Check if io-audio manager is running or not */
slouken@3139
   842
    status = snd_cards();
slouken@3139
   843
    if (status == 0) {
slouken@3139
   844
        /* if no, return immediately */
slouken@3139
   845
        return 1;
slouken@3139
   846
    }
slouken@3099
   847
icculus@3699
   848
    return 1;   /* this audio target is available. */
slouken@3099
   849
}
slouken@3099
   850
slouken@3139
   851
AudioBootStrap QSAAUDIO_bootstrap = {
icculus@5594
   852
    "qsa", "QNX QSA Audio", QSA_Init, 0
slouken@3099
   853
};
slouken@3099
   854
slouken@6044
   855
#endif /* SDL_AUDIO_DRIVER_QSA */
slouken@6044
   856
slouken@3099
   857
/* vi: set ts=4 sw=4 expandtab: */