2 * Copyright (C) 2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file oss_write.c Paraslash's oss output plugin. */
10 #include <sys/ioctl.h>
11 #include <sys/soundcard.h>
14 #include "write_cmd.lsg.h"
20 #include "buffer_tree.h"
24 /** Data specific to the oss writer. */
25 struct private_oss_write_data {
26 /** The file handle of the device. */
28 /** Four bytes for stereo streams, two bytes for mono streams. */
33 * We keep one bit of static storage to make sure only one instance of the oss
34 * writer is running at any given time.
36 static bool sound_device_busy;
38 static bool sound_device_is_busy(void)
40 return sound_device_busy;
43 static void set_sound_device_busy(void)
45 assert(!sound_device_busy);
46 sound_device_busy = true;
49 static void set_sound_device_idle(void)
51 assert(sound_device_busy);
52 sound_device_busy = false;
55 static int get_oss_format(enum sample_format sf)
58 case SF_S8: return AFMT_S8;
59 case SF_U8: return AFMT_U8;
60 case SF_S16_LE: return AFMT_S16_LE;
61 case SF_S16_BE: return AFMT_S16_BE;
62 case SF_U16_LE: return AFMT_U16_LE;
63 case SF_U16_BE: return AFMT_U16_BE;
64 default: return AFMT_S16_LE;
68 static void oss_pre_select(struct sched *s, void *context)
70 struct writer_node *wn = context;
71 struct private_oss_write_data *powd = wn->private_data;
72 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
74 if (ret == 0 || (sound_device_is_busy() && !powd))
77 return sched_min_delay(s);
78 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
81 static void oss_close(struct writer_node *wn)
83 struct private_oss_write_data *powd = wn->private_data;
89 set_sound_device_idle();
93 * The Open Sound System Programmer's Guide sayeth:
95 * Set sampling parameters always so that number of channels (mono/stereo) is
96 * set before selecting sampling rate (speed). Failing to do this will make
97 * your program incompatible with cards such as the SoundBlaster Pro which
98 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
99 * selects 44.1 kHz speed and then sets the device to stereo mode will
100 * incorrectly believe that the device is still in 44.1 kHz mode when actually
101 * the speed is decreased to 22.05 kHz.
103 static int oss_init(struct writer_node *wn, unsigned sample_rate,
104 unsigned channels, int sample_format)
108 struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
109 const char *dev = WRITE_CMD_OPT_STRING_VAL(OSS, DEVICE, wn->lpr);
111 PARA_INFO_LOG("opening %s\n", dev);
112 ret = para_open(dev, O_WRONLY, 0);
116 ret = mark_fd_nonblocking(powd->fd);
120 sample_format = format = get_oss_format(sample_format);
121 ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
123 ret = -ERRNO_TO_PARA_ERROR(errno);
126 ret = -E_BAD_SAMPLE_FORMAT;
127 if (format != sample_format)
129 /* set number of channels */
131 ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
133 ret = -ERRNO_TO_PARA_ERROR(errno);
136 ret = -E_BAD_CHANNEL_COUNT;
139 if (format == SF_U8 || format == SF_S8)
140 powd->bytes_per_frame = ch;
142 powd->bytes_per_frame = ch * 2;
147 * If we request a higher sampling rate than is supported by the
148 * device, the highest possible speed is automatically used. The
149 * value actually used is returned as the new value of the argument.
152 ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
154 ret = -ERRNO_TO_PARA_ERROR(errno);
157 if (rate != sample_rate) {
158 unsigned min = PARA_MIN(rate, sample_rate),
159 max = PARA_MAX(rate, sample_rate);
161 * Check whether the returned sample rate differs significantly
162 * from the requested one.
164 ret = -E_BAD_SAMPLERATE;
165 if (100 * max > 110 * min) /* more than 10% deviation */
167 PARA_NOTICE_LOG("using %uHz rather than %uHz\n", rate,
170 wn->min_iqs = powd->bytes_per_frame;
171 wn->private_data = powd;
177 PARA_ERROR_LOG("failed to init %s: %s\n", dev, para_strerror(-ret));
181 static int oss_post_select(__a_unused struct sched *s, void *context)
183 struct writer_node *wn = context;
184 struct private_oss_write_data *powd = wn->private_data;
185 struct btr_node *btrn = wn->btrn;
186 size_t frames, bytes;
191 ret = task_get_notification(wn->task);
194 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
198 int32_t rate, ch, format;
200 if (sound_device_is_busy())
202 get_btr_sample_rate(btrn, &rate);
203 get_btr_channels(btrn, &ch);
204 get_btr_sample_format(btrn, &format);
205 ret = oss_init(wn, rate, ch, format);
208 set_sound_device_busy();
211 btr_merge(btrn, wn->min_iqs);
212 bytes = btr_next_buffer(btrn, &data);
213 frames = bytes / powd->bytes_per_frame;
214 if (!frames) { /* eof and less than a single frame available */
215 ret = -E_WRITE_COMMON_EOF;
219 if (!FD_ISSET(powd->fd, &s->wfds))
221 /* get maximal number of bytes that can be written */
222 ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi);
224 size_t max_frames = abi.bytes / powd->bytes_per_frame;
227 /* cap number of frames to avoid sound artefacts */
228 frames = PARA_MIN(frames, max_frames);
230 ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
233 btr_consume(btrn, ret);
237 btr_remove_node(&wn->btrn);
241 const struct writer lsg_write_cmd_com_oss_user_data = {
242 .pre_select = oss_pre_select,
243 .post_select = oss_post_select,