summaryrefslogtreecommitdiffstats
path: root/media/libcubeb/src/cubeb_sun.c
blob: b768bca5614a01e657a45b37ed6d15dfcb278287 (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
/*
 * Copyright (c) 2013, 2017 Ginn Chen <ginnchen@gmail.com>
 *
 * This program is made available under an ISC-style license.  See the
 * accompanying file LICENSE for details.
 */
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/audio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/stropts.h>
#include "cubeb/cubeb.h"
#include "cubeb-internal.h"

/* Macros copied from audio_oss.h */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#define OSSIOCPARM_MASK 0x1fff          /* parameters must be < 8192 bytes */
#define OSSIOC_VOID     0x00000000      /* no parameters */
#define OSSIOC_OUT      0x20000000      /* copy out parameters */
#define OSSIOC_IN       0x40000000      /* copy in parameters */
#define OSSIOC_INOUT    (OSSIOC_IN|OSSIOC_OUT)
#define OSSIOC_SZ(t)    ((sizeof (t) & OSSIOCPARM_MASK) << 16)
#define __OSSIO(x, y)           ((int)(OSSIOC_VOID|(x<<8)|y))
#define __OSSIOR(x, y, t)       ((int)(OSSIOC_OUT|OSSIOC_SZ(t)|(x<<8)|y))
#define __OSSIOWR(x, y, t)      ((int)(OSSIOC_INOUT|OSSIOC_SZ(t)|(x<<8)|y))
#define SNDCTL_DSP_SPEED        __OSSIOWR('P', 2, int)
#define SNDCTL_DSP_CHANNELS     __OSSIOWR('P', 6, int)
#define SNDCTL_DSP_SETFMT       __OSSIOWR('P', 5, int)  /* Selects ONE fmt */
#define SNDCTL_DSP_GETODELAY    __OSSIOR('P', 23, int)
#define SNDCTL_DSP_HALT_OUTPUT  __OSSIO('P', 34)
#define AFMT_S16_LE     0x00000010
#define AFMT_S16_BE     0x00000020

#if defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__)
#define AFMT_S16_NE    AFMT_S16_BE
#else
#define AFMT_S16_NE    AFMT_S16_LE
#endif

#define DEFAULT_AUDIO_DEVICE "/dev/audio"
#define DEFAULT_DSP_DEVICE   "/dev/dsp"

#define BUF_SIZE_MS 10

#if defined(CUBEB_SUNAUDIO_DEBUG)
#define DPR(...) fprintf(stderr, __VA_ARGS__);
#else
#define DPR(...) do {} while(0)
#endif

static struct cubeb_ops const sunaudio_ops;

struct cubeb {
  struct cubeb_ops const * ops;
};

struct cubeb_stream {
  cubeb * context;
  pthread_t th;			  /* to run real-time audio i/o */
  pthread_mutex_t mutex;	  /* protects fd and frm_played */
  int fd;			  /* link us to sunaudio */
  int active;			  /* cubec_start() called */
  int conv;			  /* need float->s16 conversion */
  int using_oss;
  unsigned char *buf;		  /* data is prepared here */
  unsigned int rate;
  unsigned int n_channles;
  unsigned int bytes_per_ch;
  unsigned int n_frm;
  unsigned int buffer_size;
  int64_t frm_played;
  cubeb_data_callback data_cb;    /* cb to preapare data */
  cubeb_state_callback state_cb;  /* cb to notify about state changes */
  void *arg;			  /* user arg to {data,state}_cb */
};

static void
float_to_s16(void *ptr, long nsamp)
{
  int16_t *dst = ptr;
  float *src = ptr;

  while (nsamp-- > 0)
    *(dst++) = *(src++) * 32767;
}

static void *
sunaudio_mainloop(void *arg)
{
  struct cubeb_stream *s = arg;
  int state;

  DPR("sunaudio_mainloop()\n");

  s->state_cb(s, s->arg, CUBEB_STATE_STARTED);

  pthread_mutex_lock(&s->mutex);
  DPR("sunaudio_mainloop(), started\n");

  for (;;) {
    if (!s->active) {
      DPR("sunaudio_mainloop() stopped\n");
      state = CUBEB_STATE_STOPPED;
      break;
    }

    if (!s->using_oss) {
      audio_info_t info;
      ioctl(s->fd, AUDIO_GETINFO, &info);
      if (s->frm_played > info.play.samples + 3 * s->n_frm) {
        pthread_mutex_unlock(&s->mutex);
        struct timespec ts = {0, 10000}; // 10 ms
        nanosleep(&ts, NULL);
        pthread_mutex_lock(&s->mutex);
        continue;
      }
    }

    pthread_mutex_unlock(&s->mutex);
    unsigned int got = s->data_cb(s, s->arg, NULL, s->buf, s->n_frm);
    DPR("sunaudio_mainloop() ask %d got %d\n", s->n_frm, got);
    pthread_mutex_lock(&s->mutex);

    if (got < 0) {
      DPR("sunaudio_mainloop() cb err\n");
      state = CUBEB_STATE_ERROR;
      break;
    }

    if (s->conv) {
      float_to_s16(s->buf, got * s->n_channles);
    }

    unsigned int avail = got * 2 * s->n_channles; // coverted to s16
    unsigned int pos = 0;

    while (avail > 0 && s->active) {
      int written = write(s->fd, s->buf + pos, avail);
      if (written == -1) {
        if (errno != EINTR && errno != EWOULDBLOCK) {
          DPR("sunaudio_mainloop() write err\n");
          state = CUBEB_STATE_ERROR;
          break;
        }
        pthread_mutex_unlock(&s->mutex);
        struct timespec ts = {0, 10000}; // 10 ms
        nanosleep(&ts, NULL);
        pthread_mutex_lock(&s->mutex);
      } else {
        pos += written;
        DPR("sunaudio_mainloop() write %d pos %d\n", written, pos);
        s->frm_played += written / 2 / s->n_channles;
        avail -= written;
      }
    }

    if ((got  < s->n_frm)) {
      DPR("sunaudio_mainloop() drained\n");
      state = CUBEB_STATE_DRAINED;
      break;
    }
  }

  pthread_mutex_unlock(&s->mutex);
  s->state_cb(s, s->arg, state);

  return NULL;
}

/*static*/ int
sunaudio_init(cubeb **context, char const *context_name)
{
  DPR("sunaudio_init(%s)\n", context_name);
  *context = malloc(sizeof(*context));
  (*context)->ops = &sunaudio_ops;
  (void)context_name;
  return CUBEB_OK;
}

static char const *
sunaudio_get_backend_id(cubeb *context)
{
  return "sunaudio";
}

static void
sunaudio_destroy(cubeb *context)
{
  DPR("sunaudio_destroy()\n");
  free(context);
}

static int
sunaudio_stream_init(cubeb *context,
                  cubeb_stream **stream,
                  char const *stream_name,
                  cubeb_devid input_device,
                  cubeb_stream_params * input_stream_params,
                  cubeb_devid output_device,
                  cubeb_stream_params * output_stream_params,
                  unsigned int latency,
                  cubeb_data_callback data_callback,
                  cubeb_state_callback state_callback,
                  void *user_ptr)
{
  struct cubeb_stream *s;
  DPR("sunaudio_stream_init(%s)\n", stream_name);
  size_t size;

  s = malloc(sizeof(struct cubeb_stream));
  if (s == NULL)
    return CUBEB_ERROR;
  s->context = context;

  // If UTAUDIODEV is set, use it with Sun Audio interface
  char * sa_device_name = getenv("UTAUDIODEV");
  char * dsp_device_name = NULL;
  if (!sa_device_name) {
    dsp_device_name = getenv("AUDIODSP");
    if (!dsp_device_name) {
      dsp_device_name = DEFAULT_DSP_DEVICE;
    }
    sa_device_name = getenv("AUDIODEV");
    if (!sa_device_name) {
      sa_device_name = DEFAULT_AUDIO_DEVICE;
    }
  }

  s->using_oss = 0;
  // Try to use OSS if available
  if (dsp_device_name) {
    s->fd = open(dsp_device_name, O_WRONLY | O_NONBLOCK);
    if (s->fd >= 0) {
      s->using_oss = 1;
    }
  }

  // Try Sun Audio
  if (!s->using_oss) {
    s->fd = open(sa_device_name, O_WRONLY | O_NONBLOCK);
  }

  if (s->fd < 0) {
    free(s);
    DPR("sunaudio_stream_init(), open() failed\n");
    return CUBEB_ERROR;
  }

  if (s->using_oss) {
    if (ioctl(s->fd, SNDCTL_DSP_SPEED, &output_stream_params->rate) < 0) {
      DPR("ioctl SNDCTL_DSP_SPEED failed.\n");
      close(s->fd);
      free(s);
      return CUBEB_ERROR_INVALID_FORMAT;
    }

    if (ioctl(s->fd, SNDCTL_DSP_CHANNELS, &output_stream_params->channels) < 0) {
      DPR("ioctl SNDCTL_DSP_CHANNELS failed.\n");
      close(s->fd);
      free(s);
      return CUBEB_ERROR_INVALID_FORMAT;
    }

    int format = AFMT_S16_NE;
    if (ioctl(s->fd, SNDCTL_DSP_SETFMT, &format) < 0) {
      DPR("ioctl SNDCTL_DSP_SETFMT failed.\n");
      close(s->fd);
      free(s);
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  } else {
    audio_info_t audio_info;
    AUDIO_INITINFO(&audio_info)
    audio_info.play.sample_rate = output_stream_params->rate;
    audio_info.play.channels = output_stream_params->channels;
    audio_info.play.encoding = AUDIO_ENCODING_LINEAR;
    audio_info.play.precision = 16;
    if (ioctl(s->fd, AUDIO_SETINFO, &audio_info) == -1) {
      DPR("ioctl AUDIO_SETINFO failed.\n");
      close(s->fd);
      free(s);
      return CUBEB_ERROR_INVALID_FORMAT;
    }
  }

  s->conv = 0;
  switch (output_stream_params->format) {
    case CUBEB_SAMPLE_S16NE:
      s->bytes_per_ch = 2;
      break;
    case CUBEB_SAMPLE_FLOAT32NE:
      s->bytes_per_ch = 4;
      s->conv = 1;
      break;
    default:
      DPR("sunaudio_stream_init() unsupported format\n");
      close(s->fd);
      free(s);
      return CUBEB_ERROR_INVALID_FORMAT;
  }

  s->active = 0;
  s->rate = output_stream_params->rate;
  s->n_channles = output_stream_params->channels;
  s->data_cb = data_callback;
  s->state_cb = state_callback;
  s->arg = user_ptr;
  if (pthread_mutex_init(&s->mutex, NULL) != 0) {
    free(s);
    return CUBEB_ERROR;
  }
  s->frm_played = 0;
  s->n_frm = s->rate * BUF_SIZE_MS / 1000;
  s->buffer_size = s->bytes_per_ch * s->n_channles * s->n_frm;
  s->buf = malloc(s->buffer_size);
  if (s->buf == NULL) {
    close(s->fd);
    free(s);
    return CUBEB_ERROR;
  }

  *stream = s;
  DPR("sunaudio_stream_init() end, ok\n");
  return CUBEB_OK;
}

static void
sunaudio_stream_destroy(cubeb_stream *s)
{
  DPR("sunaudio_stream_destroy()\n");
  if (s->fd > 0) {
    // Flush buffer
    if (s->using_oss) {
      ioctl(s->fd, SNDCTL_DSP_HALT_OUTPUT);
    } else {
      ioctl(s->fd, I_FLUSH);
    }
    close(s->fd);
  }
  free(s->buf);
  free(s);
}

static int
sunaudio_stream_start(cubeb_stream *s)
{
  int err;

  DPR("sunaudio_stream_start()\n");
  s->active = 1;
  err = pthread_create(&s->th, NULL, sunaudio_mainloop, s);
  if (err) {
    s->active = 0;
    return CUBEB_ERROR;
  }
  return CUBEB_OK;
}

static int
sunaudio_stream_stop(cubeb_stream *s)
{
  void *dummy;

  DPR("sunaudio_stream_stop()\n");
  if (s->active) {
    s->active = 0;
    pthread_join(s->th, &dummy);
  }
  return CUBEB_OK;
}

static int
sunaudio_stream_get_position(cubeb_stream *s, uint64_t *p)
{
  int rv = CUBEB_OK;
  pthread_mutex_lock(&s->mutex);
  if (s->active && s->fd > 0) {
    if (s->using_oss) {
      int delay;
      ioctl(s->fd, SNDCTL_DSP_GETODELAY, &delay);
      int64_t t = s->frm_played - delay / s->n_channles / 2;
      if (t < 0) {
        *p = 0;
      } else {
        *p = t;
      }
    } else {
      audio_info_t info;
      ioctl(s->fd, AUDIO_GETINFO, &info);
      *p = info.play.samples;
    }
    DPR("sunaudio_stream_get_position() %lld\n", *p);
  } else {
    rv = CUBEB_ERROR;
  }
  pthread_mutex_unlock(&s->mutex);
  return rv;
}

static int
sunaudio_get_max_channel_count(cubeb * ctx, uint32_t * max_channels)
{
  if (!ctx || !max_channels)
    return CUBEB_ERROR;

  *max_channels = 2;

  return CUBEB_OK;
}

static int
sunaudio_get_preferred_sample_rate(cubeb * ctx, uint32_t * rate)
{
  if (!ctx || !rate)
    return CUBEB_ERROR;

  // XXX Not yet implemented.
  *rate = 44100;

  return CUBEB_OK;
}

static int
sunaudio_get_min_latency(cubeb * ctx, cubeb_stream_params params, uint32_t * latency_ms)
{
  if (!ctx || !latency_ms)
    return CUBEB_ERROR;

  // XXX Not yet implemented.
  *latency_ms = 20;

  return CUBEB_OK;
}

static int
sunaudio_stream_get_latency(cubeb_stream * s, uint32_t * latency)
{
  if (!s || !latency)
    return CUBEB_ERROR;

  int rv = CUBEB_OK;
  pthread_mutex_lock(&s->mutex);
  if (s->active && s->fd > 0) {
    if (s->using_oss) {
      int delay;
      ioctl(s->fd, SNDCTL_DSP_GETODELAY, &delay);
      *latency = delay / s->n_channles / 2 / s->rate;
    } else {
      audio_info_t info;
      ioctl(s->fd, AUDIO_GETINFO, &info);
      *latency = (s->frm_played - info.play.samples) / s->rate;
    }
    DPR("sunaudio_stream_get_position() %lld\n", *p);
  } else {
    rv = CUBEB_ERROR;
  }
  pthread_mutex_unlock(&s->mutex);
  return rv;
}

static struct cubeb_ops const sunaudio_ops = {
  .init = sunaudio_init,
  .get_backend_id = sunaudio_get_backend_id,
  .destroy = sunaudio_destroy,
  .get_preferred_sample_rate = sunaudio_get_preferred_sample_rate,
  .stream_init = sunaudio_stream_init,
  .stream_destroy = sunaudio_stream_destroy,
  .stream_start = sunaudio_stream_start,
  .stream_stop = sunaudio_stream_stop,
  .stream_get_position = sunaudio_stream_get_position,
  .get_max_channel_count = sunaudio_get_max_channel_count,
  .get_min_latency = sunaudio_get_min_latency,
  .stream_get_latency = sunaudio_stream_get_latency
};