aboutsummaryrefslogtreecommitdiff
path: root/oss.c
blob: f0a74bde077349a396a7429ad2d9a6e430522baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * QEMU OSS Audio output driver
 * 
 * Copyright (c) 2003 Vassili Karpov (malc)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"

#ifndef _WIN32
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>


/* http://www.df.lth.se/~john_e/gems/gem002d.html */
/* http://www.multi-platforms.com/Tips/PopCount.htm */
static inline uint32_t popcount (uint32_t u)
{
  u = ((u&0x55555555) + ((u>>1)&0x55555555));
  u = ((u&0x33333333) + ((u>>2)&0x33333333));
  u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
  u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
  u = ( u&0x0000ffff) + (u>>16);
  return u;
}

static inline uint32_t lsbindex (uint32_t u)
{
  return popcount ((u&-u)-1);
}

#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)<(b)?(b):(a))

#define DEREF(x) (void)x
#define log(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define ERRFail(...) do {                                       \
    int _errno = errno;                                         \
    fprintf (stderr, "oss: " __VA_ARGS__);                      \
    fprintf (stderr, "system error: %s\n", strerror (_errno));  \
    abort ();                                                   \
} while (0)
#define Fail(...) do {                          \
    fprintf (stderr, "oss: " __VA_ARGS__);      \
    fprintf (stderr, "\n");                     \
    abort ();                                   \
} while (0)

#ifdef DEBUG_OSS
#define lwarn(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define linfo(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define ldebug(...) fprintf (stderr, "oss: " __VA_ARGS__)
#else
#define lwarn(...)
#define linfo(...)
#define ldebug(...)
#endif


#define IOCTL(args) do {                        \
  int ret = ioctl args;                         \
  if (-1 == ret) {                              \
    ERRFail (#args);                            \
  }                                             \
  ldebug ("ioctl " #args " = %d\n", ret);       \
} while (0)

static struct {
    int fd;
    int freq;
    int bits16;
    int nchannels;
    int rpos;
    int wpos;
    int live;
    int oss_fmt;
    int bytes_per_second;
    int is_mapped;
    void *buf;
    int bufsize;
    int nfrags;
    int fragsize;
    int old_optr;
    int leftover;
    uint64_t old_ticks;
    void (*copy_fn)(void *, void *, int);
} oss = { .fd = -1 };

static struct {
    int try_mmap;
    int nfrags;
    int fragsize;
} conf = {
    .try_mmap = 0,
    .nfrags = 4,
    .fragsize = 4096
};

static enum {DONT, DSP, TID} est = DONT;

static void copy_no_conversion (void *dst, void *src, int size)
{
    memcpy (dst, src, size);
}

static void copy_u16_to_s16 (void *dst, void *src, int size)
{
    int i;
    uint16_t *out, *in;

    out = dst;
    in = src;

    for (i = 0; i < size / 2; i++) {
        out[i] = in[i] + 0x8000;
    }
}

static void pab (struct audio_buf_info *abinfo)
{
    DEREF (abinfo);

    ldebug ("fragments %d, fragstotal %d, fragsize %d, bytes %d\n"
            "rpos %d, wpos %d, live %d\n",
            abinfo->fragments,
            abinfo->fragstotal,
            abinfo->fragsize,
            abinfo->bytes,
            rpos, wpos, live);
}

static void do_open ()
{
    int mmmmssss;
    audio_buf_info abinfo;
    int fmt, freq, nchannels;

    if (oss.buf) {
        if (-1 == munmap (oss.buf, oss.bufsize)) {
            ERRFail ("failed to unmap audio buffer %p %d",
                     oss.buf, oss.bufsize);
        }
        oss.buf = NULL;
    }

    if (-1 != oss.fd)
        close (oss.fd);

    oss.fd = open ("/dev/dsp", O_RDWR | O_NONBLOCK);
    if (-1 == oss.fd) {
        ERRFail ("can not open /dev/dsp");
    }

    fmt = oss.oss_fmt;
    freq = oss.freq;
    nchannels = oss.nchannels;

    IOCTL ((oss.fd, SNDCTL_DSP_RESET, 1));
    IOCTL ((oss.fd, SNDCTL_DSP_SAMPLESIZE, &fmt));
    IOCTL ((oss.fd, SNDCTL_DSP_CHANNELS, &nchannels));
    IOCTL ((oss.fd, SNDCTL_DSP_SPEED, &freq));
    IOCTL ((oss.fd, SNDCTL_DSP_NONBLOCK));

    mmmmssss = (conf.nfrags << 16) | conf.fragsize;
    IOCTL ((oss.fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss));

    if ((oss.oss_fmt != fmt)
        || (oss.nchannels != nchannels)
        || (oss.freq != freq)) {
        Fail ("failed to set audio parameters\n"
              "parameter | requested value | obtained value\n"
              "format    |      %10d |     %10d\n"
              "channels  |      %10d |     %10d\n"
              "frequency |      %10d |     %10d\n",
              oss.oss_fmt, fmt,
              oss.nchannels, nchannels,
              oss.freq, freq);
    }

    IOCTL ((oss.fd, SNDCTL_DSP_GETOSPACE, &abinfo));

    oss.nfrags = abinfo.fragstotal;
    oss.fragsize = abinfo.fragsize;
    oss.bufsize = oss.nfrags * oss.fragsize;
    oss.old_optr = 0;

    oss.bytes_per_second = (freq << (nchannels >> 1)) << oss.bits16;

    linfo ("bytes per second %d\n", oss.bytes_per_second);

    linfo ("fragments %d, fragstotal %d, fragsize %d, bytes %d, bufsize %d\n",
           abinfo.fragments,
           abinfo.fragstotal,
           abinfo.fragsize,
           abinfo.bytes,
           oss.bufsize);

    oss.buf = MAP_FAILED;
    oss.is_mapped = 0;

    if (conf.try_mmap) {
        oss.buf = mmap (NULL, oss.bufsize, PROT_WRITE, MAP_SHARED, oss.fd, 0);
        if (MAP_FAILED == oss.buf) {
            int err;

            err = errno;
            log ("failed to mmap audio, size %d, fd %d\n"
                 "syserr: %s\n",
                 oss.bufsize, oss.fd, strerror (err));
        }
    else {
            est = TID;
            oss.is_mapped = 1;
        }
    }

    if (MAP_FAILED == oss.buf) {
        est = TID;
        oss.buf = mmap (NULL, oss.bufsize, PROT_READ | PROT_WRITE,
                        MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
        if (MAP_FAILED == oss.buf) {
            ERRFail ("mmap audio buf, size %d", oss.bufsize);
        }
    }

    oss.rpos = 0;
    oss.wpos = 0;
    oss.live = 0;

    if (oss.is_mapped) {
        int trig;

        trig = 0;
        IOCTL ((oss.fd, SNDCTL_DSP_SETTRIGGER, &trig));
        trig = PCM_ENABLE_OUTPUT;
        IOCTL ((oss.fd, SNDCTL_DSP_SETTRIGGER, &trig));
    }
}

static void maybe_open (int req_freq, int req_nchannels,
                        audfmt_e req_fmt, int force_open)
{
    int oss_fmt, bits16;

    switch (req_fmt) {
    case AUD_FMT_U8:
        bits16 = 0;
        oss_fmt = AFMT_U8;
        oss.copy_fn = copy_no_conversion;
        break;

    case AUD_FMT_S8:
        Fail ("can not play 8bit signed");

    case AUD_FMT_S16:
        bits16 = 1;
        oss_fmt = AFMT_S16_LE;
        oss.copy_fn = copy_no_conversion;
        break;

    case AUD_FMT_U16:
        bits16 = 1;
        oss_fmt = AFMT_S16_LE;
        oss.copy_fn = copy_u16_to_s16;
        break;

    default:
        abort ();
    }

    if (force_open
        || (-1 == oss.fd)
        || (oss_fmt != oss.oss_fmt)
        || (req_nchannels != oss.nchannels)
        || (req_freq != oss.freq)
        || (bits16 != oss.bits16)) {
        oss.oss_fmt = oss_fmt;
        oss.nchannels = req_nchannels;
        oss.freq = req_freq;
        oss.bits16 = bits16;
        do_open ();
    }
}

void AUD_reset (int req_freq, int req_nchannels, audfmt_e req_fmt)
{
    maybe_open (req_freq, req_nchannels, req_fmt, 0);
}

void AUD_open (int req_freq, int req_nchannels, audfmt_e req_fmt)
{
    maybe_open (req_freq, req_nchannels, req_fmt, 1);
}

int AUD_write (void *in_buf, int size)
{
    int to_copy, temp;
    uint8_t *in, *out;

    to_copy = MIN (oss.bufsize - oss.live, size);

    temp = to_copy;

    in = in_buf;
    out = oss.buf;

    while (temp) {
        int copy;

        copy = MIN (temp, oss.bufsize - oss.wpos);
        oss.copy_fn (out + oss.wpos, in, copy);

        oss.wpos += copy;
        if (oss.wpos == oss.bufsize) {
            oss.wpos = 0;
        }

        temp -= copy;
        in += copy;
        oss.live += copy;
    }

    return to_copy;
}

void AUD_run (void)
{
    int res;
    int bytes;
    struct audio_buf_info abinfo;

    if (0 == oss.live)
        return;

    if (oss.is_mapped) {
        count_info info;

        res = ioctl (oss.fd, SNDCTL_DSP_GETOPTR, &info);
        if (-1 == res) {
            int err;

            err = errno;
            lwarn ("SNDCTL_DSP_GETOPTR failed with %s\n", strerror (err));
            return;
        }

        if (info.ptr > oss.old_optr) {
            bytes = info.ptr - oss.old_optr;
        }
        else {
            bytes = oss.bufsize + info.ptr - oss.old_optr;
        }

        oss.old_optr = info.ptr;
        oss.live -= bytes;
        return;
    }

    res = ioctl (oss.fd, SNDCTL_DSP_GETOSPACE, &abinfo);

    if (-1 == res) {
        int err;

        err = errno;
        lwarn ("SNDCTL_DSP_GETOSPACE failed with %s\n", strerror (err));
    }

    bytes = abinfo.bytes;
    bytes = MIN (oss.live, bytes);
#if 0
    bytes = (bytes / fragsize) * fragsize;
#endif

    while (bytes) {
        int left, play, written;

        left = oss.bufsize - oss.rpos;
        play = MIN (left, bytes);
        written = write (oss.fd, (void *) ((uint32_t) oss.buf + oss.rpos), play);

        if (-1 == written) {
            if (EAGAIN == errno || EINTR == errno) {
                return;
            }
            else {
                ERRFail ("write audio");
            }
        }

        play = written;
        oss.live -= play;
        oss.rpos += play;
        bytes -= play;

        if (oss.rpos == oss.bufsize) {
            oss.rpos = 0;
        }
    }
}

static int get_dsp_bytes (void)
{
    int res;
    struct count_info info;

    res = ioctl (oss.fd, SNDCTL_DSP_GETOPTR, &info);
    if (-1 == res) {
        int err;

        err = errno;
        lwarn ("SNDCTL_DSP_GETOPTR failed with %s\n", strerror (err));
        return -1;
    }
    else {
        ldebug ("bytes %d\n", info.bytes);
        return info.bytes;
    }
}

void AUD_adjust_estimate (int leftover)
{
    oss.leftover = leftover;
}

int AUD_get_free (void)
{
    int free, elapsed;

    free = oss.bufsize - oss.live;

    if (0 == free)
        return 0;

    elapsed = free;
    switch (est) {
    case DONT:
        break;

    case DSP:
        {
            static int old_bytes;
            int bytes;

            bytes = get_dsp_bytes ();
            if (bytes <= 0)
                return free;

            elapsed = bytes - old_bytes;
            old_bytes = bytes;
            ldebug ("dsp elapsed %d bytes\n", elapsed);
            break;
        }

    case TID:
        {
            uint64_t ticks, delta;
            uint64_t ua_elapsed;
            uint64_t al_elapsed;

            ticks = qemu_get_clock(rt_clock);
            delta = ticks - oss.old_ticks;
            oss.old_ticks = ticks;

            ua_elapsed = (delta * oss.bytes_per_second) / 1000;
            al_elapsed = ua_elapsed & ~3ULL;

            ldebug ("tid elapsed %llu bytes\n", ua_elapsed);

            if (al_elapsed > (uint64_t) INT_MAX)
                elapsed = INT_MAX;
            else
                elapsed = al_elapsed;

            elapsed += oss.leftover;
        }
    }

    if (elapsed > free) {
        lwarn ("audio can not keep up elapsed %d free %d\n", elapsed, free);
        return free;
    }
    else {
        return elapsed;
    }
}

int AUD_get_live (void)
{
    return oss.live;
}

int AUD_get_buffer_size (void)
{
    return oss.bufsize;
}

#define QC_OSS_FRAGSIZE "QEMU_OSS_FRAGSIZE"
#define QC_OSS_NFRAGS "QEMU_OSS_NFRAGS"
#define QC_OSS_MMAP "QEMU_OSS_MMAP"

static int get_conf_val (const char *key, int defval)
{
    int val = defval;
    char *strval;

    strval = getenv (key);
    if (strval) {
        val = atoi (strval);
    }

    return val;
}

void AUD_init (void)
{
    int fsp;

    DEREF (pab);

    conf.fragsize = get_conf_val (QC_OSS_FRAGSIZE, conf.fragsize);
    conf.nfrags = get_conf_val (QC_OSS_NFRAGS, conf.nfrags);
    conf.try_mmap = get_conf_val (QC_OSS_MMAP, conf.try_mmap);

    fsp = conf.fragsize;
    if (0 != (fsp & (fsp - 1))) {
        Fail ("fragment size %d is not power of 2", fsp);
    }

    conf.fragsize = lsbindex (fsp);
}

#else

void AUD_run (void)
{
}

int AUD_write (void *in_buf, int size)
{
    return 0;
}

void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt)
{
}

void AUD_adjust_estimate (int _leftover)
{
}

int AUD_get_free (void)
{
    return 0;
}

int AUD_get_live (void)
{
    return 0;
}

int AUD_get_buffer_size (void)
{
    return 0;
}

void AUD_init (void)
{
}

#endif