~ubuntu-branches/ubuntu/hardy/kvm/hardy-backports

« back to all changes in this revision

Viewing changes to qemu/audio/esdaudio.c

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2008-02-26 13:10:57 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20080226131057-s67x6l89mtjw1x9b
Tags: 1:62+dfsg-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * QEMU ESD audio driver
 
3
 *
 
4
 * Copyright (c) 2006 Frederick Reeve (brushed up by malc)
 
5
 *
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
 * of this software and associated documentation files (the "Software"), to deal
 
8
 * in the Software without restriction, including without limitation the rights
 
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
 * copies of the Software, and to permit persons to whom the Software is
 
11
 * furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice shall be included in
 
14
 * all copies or substantial portions of the Software.
 
15
 *
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
22
 * THE SOFTWARE.
 
23
 */
 
24
#include <esd.h>
 
25
#include "qemu-common.h"
 
26
#include "audio.h"
 
27
#include <signal.h>
 
28
 
 
29
#define AUDIO_CAP "esd"
 
30
#include "audio_int.h"
 
31
#include "audio_pt_int.h"
 
32
 
 
33
typedef struct {
 
34
    HWVoiceOut hw;
 
35
    int done;
 
36
    int live;
 
37
    int decr;
 
38
    int rpos;
 
39
    void *pcm_buf;
 
40
    int fd;
 
41
    struct audio_pt pt;
 
42
} ESDVoiceOut;
 
43
 
 
44
typedef struct {
 
45
    HWVoiceIn hw;
 
46
    int done;
 
47
    int dead;
 
48
    int incr;
 
49
    int wpos;
 
50
    void *pcm_buf;
 
51
    int fd;
 
52
    struct audio_pt pt;
 
53
} ESDVoiceIn;
 
54
 
 
55
static struct {
 
56
    int samples;
 
57
    int divisor;
 
58
    char *dac_host;
 
59
    char *adc_host;
 
60
} conf = {
 
61
    1024,
 
62
    2,
 
63
    NULL,
 
64
    NULL
 
65
};
 
66
 
 
67
static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...)
 
68
{
 
69
    va_list ap;
 
70
 
 
71
    va_start (ap, fmt);
 
72
    AUD_vlog (AUDIO_CAP, fmt, ap);
 
73
    va_end (ap);
 
74
 
 
75
    AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
 
76
}
 
77
 
 
78
/* playback */
 
79
static void *qesd_thread_out (void *arg)
 
80
{
 
81
    ESDVoiceOut *esd = arg;
 
82
    HWVoiceOut *hw = &esd->hw;
 
83
    int threshold;
 
84
 
 
85
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 
86
 
 
87
    for (;;) {
 
88
        int decr, to_mix, rpos;
 
89
 
 
90
        for (;;) {
 
91
            if (esd->done) {
 
92
                goto exit;
 
93
            }
 
94
 
 
95
            if (esd->live > threshold) {
 
96
                break;
 
97
            }
 
98
 
 
99
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 
100
                goto exit;
 
101
            }
 
102
        }
 
103
 
 
104
        decr = to_mix = esd->live;
 
105
        rpos = hw->rpos;
 
106
 
 
107
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 
108
            return NULL;
 
109
        }
 
110
 
 
111
        while (to_mix) {
 
112
            ssize_t written;
 
113
            int chunk = audio_MIN (to_mix, hw->samples - rpos);
 
114
            st_sample_t *src = hw->mix_buf + rpos;
 
115
 
 
116
            hw->clip (esd->pcm_buf, src, chunk);
 
117
 
 
118
        again:
 
119
            written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
 
120
            if (written == -1) {
 
121
                if (errno == EINTR || errno == EAGAIN) {
 
122
                    goto again;
 
123
                }
 
124
                qesd_logerr (errno, "write failed\n");
 
125
                return NULL;
 
126
            }
 
127
 
 
128
            if (written != chunk << hw->info.shift) {
 
129
                int wsamples = written >> hw->info.shift;
 
130
                int wbytes = wsamples << hw->info.shift;
 
131
                if (wbytes != written) {
 
132
                    dolog ("warning: Misaligned write %d (requested %d), "
 
133
                           "alignment %d\n",
 
134
                           wbytes, written, hw->info.align + 1);
 
135
                }
 
136
                to_mix -= wsamples;
 
137
                rpos = (rpos + wsamples) % hw->samples;
 
138
                break;
 
139
            }
 
140
 
 
141
            rpos = (rpos + chunk) % hw->samples;
 
142
            to_mix -= chunk;
 
143
        }
 
144
 
 
145
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 
146
            return NULL;
 
147
        }
 
148
 
 
149
        esd->rpos = rpos;
 
150
        esd->live -= decr;
 
151
        esd->decr += decr;
 
152
    }
 
153
 
 
154
 exit:
 
155
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 
156
    return NULL;
 
157
}
 
158
 
 
159
static int qesd_run_out (HWVoiceOut *hw)
 
160
{
 
161
    int live, decr;
 
162
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 
163
 
 
164
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 
165
        return 0;
 
166
    }
 
167
 
 
168
    live = audio_pcm_hw_get_live_out (hw);
 
169
    decr = audio_MIN (live, esd->decr);
 
170
    esd->decr -= decr;
 
171
    esd->live = live - decr;
 
172
    hw->rpos = esd->rpos;
 
173
    if (esd->live > 0) {
 
174
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 
175
    }
 
176
    else {
 
177
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 
178
    }
 
179
    return decr;
 
180
}
 
181
 
 
182
static int qesd_write (SWVoiceOut *sw, void *buf, int len)
 
183
{
 
184
    return audio_pcm_sw_write (sw, buf, len);
 
185
}
 
186
 
 
187
static int qesd_init_out (HWVoiceOut *hw, audsettings_t *as)
 
188
{
 
189
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 
190
    audsettings_t obt_as = *as;
 
191
    int esdfmt = ESD_STREAM | ESD_PLAY;
 
192
    int err;
 
193
    sigset_t set, old_set;
 
194
 
 
195
    sigfillset (&set);
 
196
 
 
197
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 
198
    switch (as->fmt) {
 
199
    case AUD_FMT_S8:
 
200
    case AUD_FMT_U8:
 
201
        esdfmt |= ESD_BITS8;
 
202
        obt_as.fmt = AUD_FMT_U8;
 
203
        break;
 
204
 
 
205
    case AUD_FMT_S32:
 
206
    case AUD_FMT_U32:
 
207
        dolog ("Will use 16 instead of 32 bit samples\n");
 
208
 
 
209
    case AUD_FMT_S16:
 
210
    case AUD_FMT_U16:
 
211
    deffmt:
 
212
        esdfmt |= ESD_BITS16;
 
213
        obt_as.fmt = AUD_FMT_S16;
 
214
        break;
 
215
 
 
216
    default:
 
217
        dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
 
218
#ifdef DEBUG_FMOD
 
219
        abort ();
 
220
#endif
 
221
        goto deffmt;
 
222
 
 
223
    }
 
224
    obt_as.endianness = 0;
 
225
 
 
226
    audio_pcm_init_info (&hw->info, &obt_as);
 
227
 
 
228
    hw->samples = conf.samples;
 
229
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 
230
    if (!esd->pcm_buf) {
 
231
        dolog ("Could not allocate buffer (%d bytes)\n",
 
232
               hw->samples << hw->info.shift);
 
233
        return -1;
 
234
    }
 
235
 
 
236
    esd->fd = -1;
 
237
    err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
 
238
    if (err) {
 
239
        qesd_logerr (err, "pthread_sigmask failed\n");
 
240
        goto fail1;
 
241
    }
 
242
 
 
243
    esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
 
244
    if (esd->fd < 0) {
 
245
        qesd_logerr (errno, "esd_play_stream failed\n");
 
246
        goto fail2;
 
247
    }
 
248
 
 
249
    if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
 
250
        goto fail3;
 
251
    }
 
252
 
 
253
    err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
 
254
    if (err) {
 
255
        qesd_logerr (err, "pthread_sigmask(restore) failed\n");
 
256
    }
 
257
 
 
258
    return 0;
 
259
 
 
260
 fail3:
 
261
    if (close (esd->fd)) {
 
262
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 
263
                     AUDIO_FUNC, esd->fd);
 
264
    }
 
265
    esd->fd = -1;
 
266
 
 
267
 fail2:
 
268
    err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
 
269
    if (err) {
 
270
        qesd_logerr (err, "pthread_sigmask(restore) failed\n");
 
271
    }
 
272
 
 
273
 fail1:
 
274
    qemu_free (esd->pcm_buf);
 
275
    esd->pcm_buf = NULL;
 
276
    return -1;
 
277
}
 
278
 
 
279
static void qesd_fini_out (HWVoiceOut *hw)
 
280
{
 
281
    void *ret;
 
282
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 
283
 
 
284
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 
285
    esd->done = 1;
 
286
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 
287
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 
288
 
 
289
    if (esd->fd >= 0) {
 
290
        if (close (esd->fd)) {
 
291
            qesd_logerr (errno, "failed to close esd socket\n");
 
292
        }
 
293
        esd->fd = -1;
 
294
    }
 
295
 
 
296
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 
297
 
 
298
    qemu_free (esd->pcm_buf);
 
299
    esd->pcm_buf = NULL;
 
300
}
 
301
 
 
302
static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
 
303
{
 
304
    (void) hw;
 
305
    (void) cmd;
 
306
    return 0;
 
307
}
 
308
 
 
309
/* capture */
 
310
static void *qesd_thread_in (void *arg)
 
311
{
 
312
    ESDVoiceIn *esd = arg;
 
313
    HWVoiceIn *hw = &esd->hw;
 
314
    int threshold;
 
315
 
 
316
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 
317
 
 
318
    for (;;) {
 
319
        int incr, to_grab, wpos;
 
320
 
 
321
        for (;;) {
 
322
            if (esd->done) {
 
323
                goto exit;
 
324
            }
 
325
 
 
326
            if (esd->dead > threshold) {
 
327
                break;
 
328
            }
 
329
 
 
330
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 
331
                goto exit;
 
332
            }
 
333
        }
 
334
 
 
335
        incr = to_grab = esd->dead;
 
336
        wpos = hw->wpos;
 
337
 
 
338
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 
339
            return NULL;
 
340
        }
 
341
 
 
342
        while (to_grab) {
 
343
            ssize_t nread;
 
344
            int chunk = audio_MIN (to_grab, hw->samples - wpos);
 
345
            void *buf = advance (esd->pcm_buf, wpos);
 
346
 
 
347
        again:
 
348
            nread = read (esd->fd, buf, chunk << hw->info.shift);
 
349
            if (nread == -1) {
 
350
                if (errno == EINTR || errno == EAGAIN) {
 
351
                    goto again;
 
352
                }
 
353
                qesd_logerr (errno, "read failed\n");
 
354
                return NULL;
 
355
            }
 
356
 
 
357
            if (nread != chunk << hw->info.shift) {
 
358
                int rsamples = nread >> hw->info.shift;
 
359
                int rbytes = rsamples << hw->info.shift;
 
360
                if (rbytes != nread) {
 
361
                    dolog ("warning: Misaligned write %d (requested %d), "
 
362
                           "alignment %d\n",
 
363
                           rbytes, nread, hw->info.align + 1);
 
364
                }
 
365
                to_grab -= rsamples;
 
366
                wpos = (wpos + rsamples) % hw->samples;
 
367
                break;
 
368
            }
 
369
 
 
370
            hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift,
 
371
                      &nominal_volume);
 
372
            wpos = (wpos + chunk) % hw->samples;
 
373
            to_grab -= chunk;
 
374
        }
 
375
 
 
376
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 
377
            return NULL;
 
378
        }
 
379
 
 
380
        esd->wpos = wpos;
 
381
        esd->dead -= incr;
 
382
        esd->incr += incr;
 
383
    }
 
384
 
 
385
 exit:
 
386
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 
387
    return NULL;
 
388
}
 
389
 
 
390
static int qesd_run_in (HWVoiceIn *hw)
 
391
{
 
392
    int live, incr, dead;
 
393
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 
394
 
 
395
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 
396
        return 0;
 
397
    }
 
398
 
 
399
    live = audio_pcm_hw_get_live_in (hw);
 
400
    dead = hw->samples - live;
 
401
    incr = audio_MIN (dead, esd->incr);
 
402
    esd->incr -= incr;
 
403
    esd->dead = dead - incr;
 
404
    hw->wpos = esd->wpos;
 
405
    if (esd->dead > 0) {
 
406
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 
407
    }
 
408
    else {
 
409
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 
410
    }
 
411
    return incr;
 
412
}
 
413
 
 
414
static int qesd_read (SWVoiceIn *sw, void *buf, int len)
 
415
{
 
416
    return audio_pcm_sw_read (sw, buf, len);
 
417
}
 
418
 
 
419
static int qesd_init_in (HWVoiceIn *hw, audsettings_t *as)
 
420
{
 
421
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 
422
    audsettings_t obt_as = *as;
 
423
    int esdfmt = ESD_STREAM | ESD_RECORD;
 
424
    int err;
 
425
    sigset_t set, old_set;
 
426
 
 
427
    sigfillset (&set);
 
428
 
 
429
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 
430
    switch (as->fmt) {
 
431
    case AUD_FMT_S8:
 
432
    case AUD_FMT_U8:
 
433
        esdfmt |= ESD_BITS8;
 
434
        obt_as.fmt = AUD_FMT_U8;
 
435
        break;
 
436
 
 
437
    case AUD_FMT_S16:
 
438
    case AUD_FMT_U16:
 
439
        esdfmt |= ESD_BITS16;
 
440
        obt_as.fmt = AUD_FMT_S16;
 
441
        break;
 
442
 
 
443
    case AUD_FMT_S32:
 
444
    case AUD_FMT_U32:
 
445
        dolog ("Will use 16 instead of 32 bit samples\n");
 
446
        esdfmt |= ESD_BITS16;
 
447
        obt_as.fmt = AUD_FMT_S16;
 
448
        break;
 
449
    }
 
450
    obt_as.endianness = 0;
 
451
 
 
452
    audio_pcm_init_info (&hw->info, &obt_as);
 
453
 
 
454
    hw->samples = conf.samples;
 
455
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 
456
    if (!esd->pcm_buf) {
 
457
        dolog ("Could not allocate buffer (%d bytes)\n",
 
458
               hw->samples << hw->info.shift);
 
459
        return -1;
 
460
    }
 
461
 
 
462
    esd->fd = -1;
 
463
 
 
464
    err = pthread_sigmask (SIG_BLOCK, &set, &old_set);
 
465
    if (err) {
 
466
        qesd_logerr (err, "pthread_sigmask failed\n");
 
467
        goto fail1;
 
468
    }
 
469
 
 
470
    esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
 
471
    if (esd->fd < 0) {
 
472
        qesd_logerr (errno, "esd_record_stream failed\n");
 
473
        goto fail2;
 
474
    }
 
475
 
 
476
    if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
 
477
        goto fail3;
 
478
    }
 
479
 
 
480
    err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
 
481
    if (err) {
 
482
        qesd_logerr (err, "pthread_sigmask(restore) failed\n");
 
483
    }
 
484
 
 
485
    return 0;
 
486
 
 
487
 fail3:
 
488
    if (close (esd->fd)) {
 
489
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 
490
                     AUDIO_FUNC, esd->fd);
 
491
    }
 
492
    esd->fd = -1;
 
493
 
 
494
 fail2:
 
495
    err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
 
496
    if (err) {
 
497
        qesd_logerr (err, "pthread_sigmask(restore) failed\n");
 
498
    }
 
499
 
 
500
 fail1:
 
501
    qemu_free (esd->pcm_buf);
 
502
    esd->pcm_buf = NULL;
 
503
    return -1;
 
504
}
 
505
 
 
506
static void qesd_fini_in (HWVoiceIn *hw)
 
507
{
 
508
    void *ret;
 
509
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 
510
 
 
511
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 
512
    esd->done = 1;
 
513
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 
514
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 
515
 
 
516
    if (esd->fd >= 0) {
 
517
        if (close (esd->fd)) {
 
518
            qesd_logerr (errno, "failed to close esd socket\n");
 
519
        }
 
520
        esd->fd = -1;
 
521
    }
 
522
 
 
523
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 
524
 
 
525
    qemu_free (esd->pcm_buf);
 
526
    esd->pcm_buf = NULL;
 
527
}
 
528
 
 
529
static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
 
530
{
 
531
    (void) hw;
 
532
    (void) cmd;
 
533
    return 0;
 
534
}
 
535
 
 
536
/* common */
 
537
static void *qesd_audio_init (void)
 
538
{
 
539
    return &conf;
 
540
}
 
541
 
 
542
static void qesd_audio_fini (void *opaque)
 
543
{
 
544
    (void) opaque;
 
545
    ldebug ("esd_fini");
 
546
}
 
547
 
 
548
struct audio_option qesd_options[] = {
 
549
    {"SAMPLES", AUD_OPT_INT, &conf.samples,
 
550
     "buffer size in samples", NULL, 0},
 
551
 
 
552
    {"DIVISOR", AUD_OPT_INT, &conf.divisor,
 
553
     "threshold divisor", NULL, 0},
 
554
 
 
555
    {"DAC_HOST", AUD_OPT_STR, &conf.dac_host,
 
556
     "playback host", NULL, 0},
 
557
 
 
558
    {"ADC_HOST", AUD_OPT_STR, &conf.adc_host,
 
559
     "capture host", NULL, 0},
 
560
 
 
561
    {NULL, 0, NULL, NULL, NULL, 0}
 
562
};
 
563
 
 
564
struct audio_pcm_ops qesd_pcm_ops = {
 
565
    qesd_init_out,
 
566
    qesd_fini_out,
 
567
    qesd_run_out,
 
568
    qesd_write,
 
569
    qesd_ctl_out,
 
570
 
 
571
    qesd_init_in,
 
572
    qesd_fini_in,
 
573
    qesd_run_in,
 
574
    qesd_read,
 
575
    qesd_ctl_in,
 
576
};
 
577
 
 
578
struct audio_driver esd_audio_driver = {
 
579
    INIT_FIELD (name           = ) "esd",
 
580
    INIT_FIELD (descr          = )
 
581
    "http://en.wikipedia.org/wiki/Esound",
 
582
    INIT_FIELD (options        = ) qesd_options,
 
583
    INIT_FIELD (init           = ) qesd_audio_init,
 
584
    INIT_FIELD (fini           = ) qesd_audio_fini,
 
585
    INIT_FIELD (pcm_ops        = ) &qesd_pcm_ops,
 
586
    INIT_FIELD (can_be_default = ) 0,
 
587
    INIT_FIELD (max_voices_out = ) INT_MAX,
 
588
    INIT_FIELD (max_voices_in  = ) INT_MAX,
 
589
    INIT_FIELD (voice_size_out = ) sizeof (ESDVoiceOut),
 
590
    INIT_FIELD (voice_size_in  = ) sizeof (ESDVoiceIn)
 
591
};