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