src/audio/qsa/SDL_qsa_audio.c
author Sam Lantinga
Sat, 06 Oct 2012 11:23:47 -0700
changeset 6565 1f3c0df426dc
parent 6352 a9bcd26e7105
child 6885 700f1b25f77f
permissions -rw-r--r--
When using Xinerama, XVidMode always works on screen 0. Otherwise use the real X11 screen.
slouken@3099
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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@3139
    86
static inline void
slouken@3139
    87
QSA_SetError(const char *fn, int status)
slouken@3099
    88
{
slouken@3139
    89
    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@3139
   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@3139
   182
slouken@3139
   183
        switch (selectret) {
slouken@3139
   184
        case -1:
slouken@3139
   185
            {
slouken@3139
   186
                SDL_SetError("QSA: select() failed: %s\n", strerror(errno));
slouken@3139
   187
                return;
slouken@3139
   188
            }
slouken@3139
   189
            break;
slouken@3139
   190
        case 0:
slouken@3139
   191
            {
slouken@3139
   192
                SDL_SetError("QSA: timeout on buffer waiting occured\n");
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) {
slouken@3139
   240
                    SDL_SetError("QSA: buffer playback timeout\n");
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@3139
   331
        if (this->hidden->pcm_buf != NULL) {
slouken@3139
   332
            SDL_FreeAudioMem(this->hidden->pcm_buf);
slouken@3139
   333
            this->hidden->pcm_buf = NULL;
slouken@3139
   334
        }
slouken@3099
   335
slouken@3139
   336
        SDL_free(this->hidden);
slouken@3139
   337
        this->hidden = NULL;
slouken@3139
   338
    }
slouken@3099
   339
}
slouken@3099
   340
slouken@3139
   341
static int
slouken@3139
   342
QSA_OpenDevice(_THIS, const char *devname, int iscapture)
slouken@3099
   343
{
slouken@3139
   344
    int status = 0;
slouken@3139
   345
    int format = 0;
slouken@3139
   346
    SDL_AudioFormat test_format = 0;
slouken@3139
   347
    int found = 0;
slouken@3139
   348
    snd_pcm_channel_setup_t csetup;
slouken@3139
   349
    snd_pcm_channel_params_t cparams;
slouken@3099
   350
slouken@3139
   351
    /* Initialize all variables that we clean on shutdown */
slouken@3139
   352
    this->hidden =
slouken@3139
   353
        (struct SDL_PrivateAudioData *) SDL_calloc(1,
slouken@3139
   354
                                                   (sizeof
slouken@3139
   355
                                                    (struct
slouken@3139
   356
                                                     SDL_PrivateAudioData)));
slouken@3139
   357
    if (this->hidden == NULL) {
slouken@3139
   358
        SDL_OutOfMemory();
slouken@3139
   359
        return 0;
slouken@3139
   360
    }
slouken@3139
   361
    SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
slouken@3099
   362
slouken@3139
   363
    /* Initialize channel transfer parameters to default */
slouken@3139
   364
    QSA_InitAudioParams(&cparams);
slouken@3099
   365
slouken@3139
   366
    /* Initialize channel direction: capture or playback */
slouken@3139
   367
    this->hidden->iscapture = iscapture;
slouken@3099
   368
slouken@3139
   369
    /* Find deviceid and cardid by device name for playback */
slouken@3139
   370
    if ((!this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   371
        uint32_t device;
slouken@3139
   372
        int32_t status;
slouken@3099
   373
slouken@3139
   374
        /* Search in the playback devices */
slouken@3139
   375
        device = 0;
slouken@3139
   376
        do {
slouken@3139
   377
            status = SDL_strcmp(qsa_playback_device[device].name, devname);
slouken@3139
   378
            if (status == 0) {
slouken@3139
   379
                /* Found requested device */
slouken@3139
   380
                this->hidden->deviceno = qsa_playback_device[device].deviceno;
slouken@3139
   381
                this->hidden->cardno = qsa_playback_device[device].cardno;
slouken@3139
   382
                break;
slouken@3139
   383
            }
slouken@3139
   384
            device++;
slouken@3139
   385
            if (device >= qsa_playback_devices) {
slouken@3139
   386
                QSA_CloseDevice(this);
slouken@3139
   387
                SDL_SetError("No such playback device");
slouken@3139
   388
                return 0;
slouken@3139
   389
            }
slouken@3139
   390
        } while (1);
slouken@3139
   391
    }
slouken@3099
   392
slouken@3139
   393
    /* Find deviceid and cardid by device name for capture */
slouken@3139
   394
    if ((this->hidden->iscapture) && (devname != NULL)) {
slouken@3139
   395
        /* Search in the capture devices */
slouken@3139
   396
        uint32_t device;
slouken@3139
   397
        int32_t status;
slouken@3099
   398
slouken@3139
   399
        /* Searching in the playback devices */
slouken@3139
   400
        device = 0;
slouken@3139
   401
        do {
slouken@3139
   402
            status = SDL_strcmp(qsa_capture_device[device].name, devname);
slouken@3139
   403
            if (status == 0) {
slouken@3139
   404
                /* Found requested device */
slouken@3139
   405
                this->hidden->deviceno = qsa_capture_device[device].deviceno;
slouken@3139
   406
                this->hidden->cardno = qsa_capture_device[device].cardno;
slouken@3139
   407
                break;
slouken@3139
   408
            }
slouken@3139
   409
            device++;
slouken@3139
   410
            if (device >= qsa_capture_devices) {
slouken@3139
   411
                QSA_CloseDevice(this);
slouken@3139
   412
                SDL_SetError("No such capture device");
slouken@3139
   413
                return 0;
slouken@3139
   414
            }
slouken@3139
   415
        } while (1);
slouken@3139
   416
    }
slouken@3099
   417
slouken@3139
   418
    /* Check if SDL requested default audio device */
slouken@3139
   419
    if (devname == NULL) {
slouken@3139
   420
        /* Open system default audio device */
slouken@3139
   421
        if (!this->hidden->iscapture) {
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_PLAYBACK);
slouken@3139
   426
        } else {
slouken@3139
   427
            status = snd_pcm_open_preferred(&this->hidden->audio_handle,
slouken@3139
   428
                                            &this->hidden->cardno,
slouken@3139
   429
                                            &this->hidden->deviceno,
slouken@3139
   430
                                            SND_PCM_OPEN_CAPTURE);
slouken@3139
   431
        }
slouken@3139
   432
    } else {
slouken@3139
   433
        /* Open requested audio device */
slouken@3139
   434
        if (!this->hidden->iscapture) {
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_PLAYBACK);
slouken@3139
   439
        } else {
slouken@3139
   440
            status =
slouken@3139
   441
                snd_pcm_open(&this->hidden->audio_handle,
slouken@3139
   442
                             this->hidden->cardno, this->hidden->deviceno,
slouken@3139
   443
                             SND_PCM_OPEN_CAPTURE);
slouken@3139
   444
        }
slouken@3139
   445
    }
slouken@3099
   446
slouken@3139
   447
    /* Check if requested device is opened */
slouken@3139
   448
    if (status < 0) {
slouken@3139
   449
        this->hidden->audio_handle = NULL;
slouken@3139
   450
        QSA_CloseDevice(this);
slouken@3139
   451
        QSA_SetError("snd_pcm_open", status);
slouken@3139
   452
        return 0;
slouken@3139
   453
    }
slouken@3099
   454
slouken@3139
   455
    if (!QSA_CheckBuggyCards(this, QSA_MMAP_WORKAROUND)) {
slouken@3139
   456
        /* Disable QSA MMAP plugin for buggy audio drivers */
slouken@3139
   457
        status =
slouken@3139
   458
            snd_pcm_plugin_set_disable(this->hidden->audio_handle,
slouken@3139
   459
                                       PLUGIN_DISABLE_MMAP);
slouken@3139
   460
        if (status < 0) {
slouken@3139
   461
            QSA_CloseDevice(this);
slouken@3139
   462
            QSA_SetError("snd_pcm_plugin_set_disable", status);
slouken@3139
   463
            return 0;
slouken@3139
   464
        }
slouken@3139
   465
    }
slouken@3099
   466
slouken@3139
   467
    /* Try for a closest match on audio format */
slouken@3139
   468
    format = 0;
slouken@3139
   469
    /* can't use format as SND_PCM_SFMT_U8 = 0 in qsa */
slouken@3139
   470
    found = 0;
slouken@3099
   471
slouken@3139
   472
    for (test_format = SDL_FirstAudioFormat(this->spec.format); !found;) {
slouken@3139
   473
        /* if match found set format to equivalent QSA format */
slouken@3139
   474
        switch (test_format) {
slouken@3139
   475
        case AUDIO_U8:
slouken@3139
   476
            {
slouken@3139
   477
                format = SND_PCM_SFMT_U8;
slouken@3139
   478
                found = 1;
slouken@3139
   479
            }
slouken@3139
   480
            break;
slouken@3139
   481
        case AUDIO_S8:
slouken@3139
   482
            {
slouken@3139
   483
                format = SND_PCM_SFMT_S8;
slouken@3139
   484
                found = 1;
slouken@3139
   485
            }
slouken@3139
   486
            break;
slouken@3139
   487
        case AUDIO_S16LSB:
slouken@3139
   488
            {
slouken@3139
   489
                format = SND_PCM_SFMT_S16_LE;
slouken@3139
   490
                found = 1;
slouken@3139
   491
            }
slouken@3139
   492
            break;
slouken@3139
   493
        case AUDIO_S16MSB:
slouken@3139
   494
            {
slouken@3139
   495
                format = SND_PCM_SFMT_S16_BE;
slouken@3139
   496
                found = 1;
slouken@3139
   497
            }
slouken@3139
   498
            break;
slouken@3139
   499
        case AUDIO_U16LSB:
slouken@3139
   500
            {
slouken@3139
   501
                format = SND_PCM_SFMT_U16_LE;
slouken@3139
   502
                found = 1;
slouken@3139
   503
            }
slouken@3139
   504
            break;
slouken@3139
   505
        case AUDIO_U16MSB:
slouken@3139
   506
            {
slouken@3139
   507
                format = SND_PCM_SFMT_U16_BE;
slouken@3139
   508
                found = 1;
slouken@3139
   509
            }
slouken@3139
   510
            break;
slouken@3139
   511
        case AUDIO_S32LSB:
slouken@3139
   512
            {
slouken@3139
   513
                format = SND_PCM_SFMT_S32_LE;
slouken@3139
   514
                found = 1;
slouken@3139
   515
            }
slouken@3139
   516
            break;
slouken@3139
   517
        case AUDIO_S32MSB:
slouken@3139
   518
            {
slouken@3139
   519
                format = SND_PCM_SFMT_S32_BE;
slouken@3139
   520
                found = 1;
slouken@3139
   521
            }
slouken@3139
   522
            break;
slouken@3139
   523
        case AUDIO_F32LSB:
slouken@3139
   524
            {
slouken@3139
   525
                format = SND_PCM_SFMT_FLOAT_LE;
slouken@3139
   526
                found = 1;
slouken@3139
   527
            }
slouken@3139
   528
            break;
slouken@3139
   529
        case AUDIO_F32MSB:
slouken@3139
   530
            {
slouken@3139
   531
                format = SND_PCM_SFMT_FLOAT_BE;
slouken@3139
   532
                found = 1;
slouken@3139
   533
            }
slouken@3139
   534
            break;
slouken@3139
   535
        default:
slouken@3139
   536
            {
slouken@3139
   537
                break;
slouken@3139
   538
            }
slouken@3139
   539
        }
slouken@3099
   540
slouken@3139
   541
        if (!found) {
slouken@3139
   542
            test_format = SDL_NextAudioFormat();
slouken@3139
   543
        }
slouken@3139
   544
    }
slouken@3099
   545
slouken@3139
   546
    /* assumes test_format not 0 on success */
slouken@3139
   547
    if (test_format == 0) {
slouken@3139
   548
        QSA_CloseDevice(this);
slouken@3139
   549
        SDL_SetError("QSA: Couldn't find any hardware audio formats");
slouken@3139
   550
        return 0;
slouken@3139
   551
    }
slouken@3099
   552
slouken@3139
   553
    this->spec.format = test_format;
slouken@3099
   554
slouken@3139
   555
    /* Set the audio format */
slouken@3139
   556
    cparams.format.format = format;
slouken@3099
   557
slouken@3139
   558
    /* Set mono/stereo/4ch/6ch/8ch audio */
slouken@3139
   559
    cparams.format.voices = this->spec.channels;
slouken@3099
   560
slouken@3139
   561
    /* Set rate */
slouken@3139
   562
    cparams.format.rate = this->spec.freq;
slouken@3099
   563
slouken@3139
   564
    /* Setup the transfer parameters according to cparams */
slouken@3139
   565
    status = snd_pcm_plugin_params(this->hidden->audio_handle, &cparams);
slouken@3139
   566
    if (status < 0) {
slouken@3139
   567
        QSA_CloseDevice(this);
slouken@3139
   568
        QSA_SetError("snd_pcm_channel_params", status);
slouken@3139
   569
        return 0;
slouken@3139
   570
    }
slouken@3099
   571
slouken@3139
   572
    /* Make sure channel is setup right one last time */
slouken@6352
   573
    SDL_memset(&csetup, 0, sizeof(csetup));
slouken@3139
   574
    if (!this->hidden->iscapture) {
slouken@3139
   575
        csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
slouken@3139
   576
    } else {
slouken@3139
   577
        csetup.channel = SND_PCM_CHANNEL_CAPTURE;
slouken@3139
   578
    }
slouken@3099
   579
slouken@3139
   580
    /* Setup an audio channel */
slouken@3139
   581
    if (snd_pcm_plugin_setup(this->hidden->audio_handle, &csetup) < 0) {
slouken@3139
   582
        QSA_CloseDevice(this);
slouken@3139
   583
        SDL_SetError("QSA: Unable to setup channel\n");
slouken@3139
   584
        return 0;
slouken@3139
   585
    }
slouken@3099
   586
slouken@3139
   587
    /* Calculate the final parameters for this audio specification */
slouken@3139
   588
    SDL_CalculateAudioSpec(&this->spec);
slouken@3099
   589
slouken@3139
   590
    this->hidden->pcm_len = this->spec.size;
slouken@3099
   591
slouken@3139
   592
    if (this->hidden->pcm_len == 0) {
slouken@3139
   593
        this->hidden->pcm_len =
slouken@3139
   594
            csetup.buf.block.frag_size * this->spec.channels *
slouken@3139
   595
            (snd_pcm_format_width(format) / 8);
slouken@3139
   596
    }
slouken@3099
   597
slouken@3139
   598
    /*
slouken@3139
   599
     * Allocate memory to the audio buffer and initialize with silence
slouken@3139
   600
     *  (Note that buffer size must be a multiple of fragment size, so find
slouken@3139
   601
     *  closest multiple)
slouken@3139
   602
     */
slouken@3139
   603
    this->hidden->pcm_buf =
slouken@3139
   604
        (Uint8 *) SDL_AllocAudioMem(this->hidden->pcm_len);
slouken@3139
   605
    if (this->hidden->pcm_buf == NULL) {
slouken@3139
   606
        QSA_CloseDevice(this);
slouken@3139
   607
        SDL_OutOfMemory();
slouken@3139
   608
        return 0;
slouken@3139
   609
    }
slouken@3139
   610
    SDL_memset(this->hidden->pcm_buf, this->spec.silence,
slouken@3139
   611
               this->hidden->pcm_len);
slouken@3099
   612
slouken@3139
   613
    /* get the file descriptor */
slouken@3139
   614
    if (!this->hidden->iscapture) {
slouken@3139
   615
        this->hidden->audio_fd =
slouken@3139
   616
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   617
                                    SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   618
    } else {
slouken@3139
   619
        this->hidden->audio_fd =
slouken@3139
   620
            snd_pcm_file_descriptor(this->hidden->audio_handle,
slouken@3139
   621
                                    SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   622
    }
slouken@3099
   623
slouken@3139
   624
    if (this->hidden->audio_fd < 0) {
slouken@3139
   625
        QSA_CloseDevice(this);
slouken@3139
   626
        QSA_SetError("snd_pcm_file_descriptor", status);
slouken@3139
   627
        return 0;
slouken@3139
   628
    }
slouken@3099
   629
slouken@3139
   630
    /* Prepare an audio channel */
slouken@3139
   631
    if (!this->hidden->iscapture) {
slouken@3139
   632
        /* Prepare audio playback */
slouken@3139
   633
        status =
slouken@3139
   634
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   635
                                   SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   636
    } else {
slouken@3139
   637
        /* Prepare audio capture */
slouken@3139
   638
        status =
slouken@3139
   639
            snd_pcm_plugin_prepare(this->hidden->audio_handle,
slouken@3139
   640
                                   SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   641
    }
slouken@3099
   642
slouken@3139
   643
    if (status < 0) {
slouken@3139
   644
        QSA_CloseDevice(this);
slouken@3139
   645
        QSA_SetError("snd_pcm_plugin_prepare", status);
slouken@3139
   646
        return 0;
slouken@3139
   647
    }
slouken@3099
   648
slouken@3139
   649
    /* We're really ready to rock and roll. :-) */
slouken@3139
   650
    return 1;
slouken@3099
   651
}
slouken@3099
   652
icculus@5593
   653
static void
icculus@5593
   654
QSA_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
slouken@3099
   655
{
slouken@3139
   656
    uint32_t it;
slouken@3139
   657
    uint32_t cards;
slouken@3139
   658
    uint32_t devices;
slouken@3139
   659
    int32_t status;
slouken@3139
   660
slouken@3139
   661
    /* Detect amount of available devices       */
slouken@3139
   662
    /* this value can be changed in the runtime */
slouken@3139
   663
    cards = snd_cards();
slouken@3099
   664
slouken@3139
   665
    /* If io-audio manager is not running we will get 0 as number */
slouken@3139
   666
    /* of available audio devices                                 */
slouken@3139
   667
    if (cards == 0) {
slouken@3139
   668
        /* We have no any available audio devices */
icculus@5593
   669
        return;
slouken@3139
   670
    }
slouken@3099
   671
slouken@3139
   672
    /* Find requested devices by type */
slouken@3139
   673
    if (!iscapture) {
slouken@3139
   674
        /* Playback devices enumeration requested */
slouken@3139
   675
        for (it = 0; it < cards; it++) {
slouken@3139
   676
            devices = 0;
slouken@3139
   677
            do {
slouken@3139
   678
                status =
slouken@3139
   679
                    snd_card_get_longname(it,
slouken@3139
   680
                                          qsa_playback_device
slouken@3139
   681
                                          [qsa_playback_devices].name,
slouken@3139
   682
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   683
                if (status == EOK) {
slouken@3139
   684
                    snd_pcm_t *handle;
slouken@3099
   685
slouken@3139
   686
                    /* Add device number to device name */
slouken@3139
   687
                    sprintf(qsa_playback_device[qsa_playback_devices].name +
slouken@3139
   688
                            SDL_strlen(qsa_playback_device
slouken@3139
   689
                                       [qsa_playback_devices].name), " d%d",
slouken@3139
   690
                            devices);
slouken@3099
   691
slouken@3139
   692
                    /* Store associated card number id */
slouken@3139
   693
                    qsa_playback_device[qsa_playback_devices].cardno = it;
slouken@3099
   694
slouken@3139
   695
                    /* Check if this device id could play anything */
slouken@3139
   696
                    status =
slouken@3139
   697
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   698
                                     SND_PCM_OPEN_PLAYBACK);
slouken@3139
   699
                    if (status == EOK) {
slouken@3139
   700
                        qsa_playback_device[qsa_playback_devices].deviceno =
slouken@3139
   701
                            devices;
slouken@3139
   702
                        status = snd_pcm_close(handle);
slouken@3139
   703
                        if (status == EOK) {
icculus@5593
   704
                            addfn(qsa_playback_device[qsa_playback_devices].name);
slouken@3139
   705
                            qsa_playback_devices++;
slouken@3139
   706
                        }
slouken@3139
   707
                    } else {
slouken@3139
   708
                        /* Check if we got end of devices list */
slouken@3139
   709
                        if (status == -ENOENT) {
slouken@3139
   710
                            break;
slouken@3139
   711
                        }
slouken@3139
   712
                    }
slouken@3139
   713
                } else {
slouken@3139
   714
                    break;
slouken@3139
   715
                }
slouken@3139
   716
slouken@3139
   717
                /* Check if we reached maximum devices count */
slouken@3139
   718
                if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   719
                    break;
slouken@3139
   720
                }
slouken@3139
   721
                devices++;
slouken@3139
   722
            } while (1);
slouken@3099
   723
slouken@3099
   724
            /* Check if we reached maximum devices count */
slouken@3139
   725
            if (qsa_playback_devices >= QSA_MAX_DEVICES) {
slouken@3139
   726
                break;
slouken@3099
   727
            }
slouken@3139
   728
        }
slouken@3139
   729
    } else {
slouken@3139
   730
        /* Capture devices enumeration requested */
slouken@3139
   731
        for (it = 0; it < cards; it++) {
slouken@3139
   732
            devices = 0;
slouken@3139
   733
            do {
slouken@3139
   734
                status =
slouken@3139
   735
                    snd_card_get_longname(it,
slouken@3139
   736
                                          qsa_capture_device
slouken@3139
   737
                                          [qsa_capture_devices].name,
slouken@3139
   738
                                          QSA_MAX_NAME_LENGTH);
slouken@3139
   739
                if (status == EOK) {
slouken@3139
   740
                    snd_pcm_t *handle;
slouken@3099
   741
slouken@3139
   742
                    /* Add device number to device name */
slouken@3139
   743
                    sprintf(qsa_capture_device[qsa_capture_devices].name +
slouken@3139
   744
                            SDL_strlen(qsa_capture_device
slouken@3139
   745
                                       [qsa_capture_devices].name), " d%d",
slouken@3139
   746
                            devices);
slouken@3099
   747
slouken@3139
   748
                    /* Store associated card number id */
slouken@3139
   749
                    qsa_capture_device[qsa_capture_devices].cardno = it;
slouken@3099
   750
slouken@3139
   751
                    /* Check if this device id could play anything */
slouken@3139
   752
                    status =
slouken@3139
   753
                        snd_pcm_open(&handle, it, devices,
slouken@3139
   754
                                     SND_PCM_OPEN_CAPTURE);
slouken@3139
   755
                    if (status == EOK) {
slouken@3139
   756
                        qsa_capture_device[qsa_capture_devices].deviceno =
slouken@3139
   757
                            devices;
slouken@3139
   758
                        status = snd_pcm_close(handle);
slouken@3139
   759
                        if (status == EOK) {
icculus@5593
   760
                            addfn(qsa_capture_device[qsa_capture_devices].name);
slouken@3139
   761
                            qsa_capture_devices++;
slouken@3139
   762
                        }
slouken@3139
   763
                    } else {
slouken@3139
   764
                        /* Check if we got end of devices list */
slouken@3139
   765
                        if (status == -ENOENT) {
slouken@3139
   766
                            break;
slouken@3139
   767
                        }
slouken@3139
   768
                    }
slouken@3099
   769
slouken@3139
   770
                    /* Check if we reached maximum devices count */
slouken@3139
   771
                    if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   772
                        break;
slouken@3139
   773
                    }
slouken@3139
   774
                } else {
slouken@3139
   775
                    break;
slouken@3139
   776
                }
slouken@3139
   777
                devices++;
slouken@3139
   778
            } while (1);
slouken@3099
   779
slouken@3139
   780
            /* Check if we reached maximum devices count */
slouken@3139
   781
            if (qsa_capture_devices >= QSA_MAX_DEVICES) {
slouken@3139
   782
                break;
slouken@3139
   783
            }
slouken@3139
   784
        }
slouken@3139
   785
    }
slouken@3099
   786
}
slouken@3099
   787
icculus@5584
   788
static void
slouken@3139
   789
QSA_WaitDone(_THIS)
slouken@3099
   790
{
slouken@3139
   791
    if (!this->hidden->iscapture) {
slouken@3139
   792
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   793
            /* Wait till last fragment is played and stop channel */
slouken@3139
   794
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   795
                                 SND_PCM_CHANNEL_PLAYBACK);
slouken@3139
   796
        }
slouken@3139
   797
    } else {
slouken@3139
   798
        if (this->hidden->audio_handle != NULL) {
slouken@3139
   799
            /* Discard all unread data and stop channel */
slouken@3139
   800
            snd_pcm_plugin_flush(this->hidden->audio_handle,
slouken@3139
   801
                                 SND_PCM_CHANNEL_CAPTURE);
slouken@3139
   802
        }
slouken@3139
   803
    }
slouken@3139
   804
}
slouken@3139
   805
icculus@5584
   806
static void
slouken@3139
   807
QSA_Deinitialize(void)
slouken@3139
   808
{
slouken@3139
   809
    /* Clear devices array on shutdown */
slouken@3139
   810
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   811
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   812
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   813
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   814
    qsa_playback_devices = 0;
slouken@3139
   815
    qsa_capture_devices = 0;
slouken@3099
   816
}
slouken@3099
   817
slouken@3139
   818
static int
slouken@3139
   819
QSA_Init(SDL_AudioDriverImpl * impl)
slouken@3099
   820
{
slouken@3139
   821
    snd_pcm_t *handle = NULL;
slouken@3139
   822
    int32_t status = 0;
slouken@3139
   823
slouken@3139
   824
    /* Clear devices array */
slouken@3139
   825
    SDL_memset(qsa_playback_device, 0x00,
slouken@3139
   826
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   827
    SDL_memset(qsa_capture_device, 0x00,
slouken@3139
   828
               sizeof(QSA_Device) * QSA_MAX_DEVICES);
slouken@3139
   829
    qsa_playback_devices = 0;
slouken@3139
   830
    qsa_capture_devices = 0;
slouken@3139
   831
slouken@3139
   832
    /* Set function pointers                                     */
slouken@3139
   833
    /* DeviceLock and DeviceUnlock functions are used default,   */
slouken@3139
   834
    /* provided by SDL, which uses pthread_mutex for lock/unlock */
slouken@3139
   835
    impl->DetectDevices = QSA_DetectDevices;
slouken@3139
   836
    impl->OpenDevice = QSA_OpenDevice;
slouken@3139
   837
    impl->ThreadInit = QSA_ThreadInit;
slouken@3139
   838
    impl->WaitDevice = QSA_WaitDevice;
slouken@3139
   839
    impl->PlayDevice = QSA_PlayDevice;
slouken@3139
   840
    impl->GetDeviceBuf = QSA_GetDeviceBuf;
slouken@3139
   841
    impl->CloseDevice = QSA_CloseDevice;
slouken@3139
   842
    impl->WaitDone = QSA_WaitDone;
slouken@3139
   843
    impl->Deinitialize = QSA_Deinitialize;
slouken@3139
   844
    impl->LockDevice = NULL;
slouken@3139
   845
    impl->UnlockDevice = NULL;
slouken@3139
   846
slouken@3139
   847
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   848
    impl->ProvidesOwnCallbackThread = 0;
slouken@3139
   849
    impl->SkipMixerLock = 0;
slouken@3139
   850
    impl->HasCaptureSupport = 1;
slouken@3139
   851
    impl->OnlyHasDefaultOutputDevice = 0;
slouken@3139
   852
    impl->OnlyHasDefaultInputDevice = 0;
slouken@3139
   853
slouken@3139
   854
    /* Check if io-audio manager is running or not */
slouken@3139
   855
    status = snd_cards();
slouken@3139
   856
    if (status == 0) {
slouken@3139
   857
        /* if no, return immediately */
slouken@3139
   858
        return 1;
slouken@3139
   859
    }
slouken@3139
   860
icculus@3699
   861
    return 1;   /* this audio target is available. */
slouken@3099
   862
}
slouken@3099
   863
slouken@3139
   864
AudioBootStrap QSAAUDIO_bootstrap = {
icculus@5594
   865
    "qsa", "QNX QSA Audio", QSA_Init, 0
slouken@3099
   866
};
slouken@3099
   867
slouken@6044
   868
#endif /* SDL_AUDIO_DRIVER_QSA */
slouken@6044
   869
slouken@3099
   870
/* vi: set ts=4 sw=4 expandtab: */