2 * Copyright (C) 2009-2014 Andre Noll <maan@systemlinux.org>
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>
12 #include <sys/soundcard.h>
20 #include "buffer_tree.h"
22 #include "write_common.h"
23 #include "oss_write.cmdline.h"
26 /** Data specific to the oss writer. */
27 struct private_oss_write_data {
28 /** The file handle of the device. */
30 /** Four bytes for stereo streams, two bytes for mono streams. */
34 static int get_oss_format(enum sample_format sf)
37 case SF_S8: return AFMT_S8;
38 case SF_U8: return AFMT_U8;
39 case SF_S16_LE: return AFMT_S16_LE;
40 case SF_S16_BE: return AFMT_S16_BE;
41 case SF_U16_LE: return AFMT_U16_LE;
42 case SF_U16_BE: return AFMT_U16_BE;
43 default: return AFMT_S16_LE;
47 static void oss_pre_select(struct sched *s, void *context)
49 struct writer_node *wn = context;
50 struct private_oss_write_data *powd = wn->private_data;
51 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
56 return sched_min_delay(s);
57 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
60 static void oss_close(struct writer_node *wn)
62 struct private_oss_write_data *powd = wn->private_data;
71 * The Open Sound System Programmer's Guide sayeth:
73 * Set sampling parameters always so that number of channels (mono/stereo) is
74 * set before selecting sampling rate (speed). Failing to do this will make
75 * your program incompatible with cards such as the SoundBlaster Pro which
76 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
77 * selects 44.1 kHz speed and then sets the device to stereo mode will
78 * incorrectly believe that the device is still in 44.1 kHz mode when actually
79 * the speed is decreased to 22.05 kHz.
81 static int oss_init(struct writer_node *wn, unsigned sample_rate,
82 unsigned channels, int sample_format)
86 struct oss_write_args_info *conf = wn->conf;
87 struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
89 PARA_INFO_LOG("opening %s\n", conf->device_arg);
90 ret = para_open(conf->device_arg, O_WRONLY, 0);
94 ret = mark_fd_nonblocking(powd->fd);
98 sample_format = format = get_oss_format(sample_format);
99 ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
101 ret = -ERRNO_TO_PARA_ERROR(errno);
104 ret = -E_BAD_SAMPLE_FORMAT;
105 if (format != sample_format)
107 /* set number of channels */
109 ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
111 ret = -ERRNO_TO_PARA_ERROR(errno);
114 ret = -E_BAD_CHANNEL_COUNT;
117 if (format == SF_U8 || format == SF_S8)
118 powd->bytes_per_frame = ch;
120 powd->bytes_per_frame = ch * 2;
125 * If we request a higher sampling rate than is supported by the
126 * device, the the highest possible speed is automatically used. The
127 * value actually used is returned as the new value of the argument.
130 ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
132 ret = -ERRNO_TO_PARA_ERROR(errno);
135 if (rate != sample_rate) {
136 unsigned min = PARA_MIN(rate, sample_rate),
137 max = PARA_MAX(rate, sample_rate);
139 * Check whether the returned sample rate differs significantly
140 * from the requested one.
142 ret = -E_BAD_SAMPLERATE;
143 if (100 * max > 110 * min) /* more than 10% deviation */
145 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
148 wn->min_iqs = powd->bytes_per_frame;
149 wn->private_data = powd;
155 PARA_ERROR_LOG("failed to init %s: %s\n", conf->device_arg,
156 para_strerror(-ret));
160 static int oss_post_select(__a_unused struct sched *s, void *context)
162 struct writer_node *wn = context;
163 struct private_oss_write_data *powd = wn->private_data;
164 struct btr_node *btrn = wn->btrn;
165 size_t frames, bytes;
169 ret = task_get_notification(wn->task);
172 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
176 int32_t rate, ch, format;
177 get_btr_sample_rate(btrn, &rate);
178 get_btr_channels(btrn, &ch);
179 get_btr_sample_format(btrn, &format);
180 ret = oss_init(wn, rate, ch, format);
185 btr_merge(btrn, wn->min_iqs);
186 bytes = btr_next_buffer(btrn, &data);
187 frames = bytes / powd->bytes_per_frame;
188 if (!frames) { /* eof and less than a single frame available */
189 ret = -E_WRITE_COMMON_EOF;
193 if (!FD_ISSET(powd->fd, &s->wfds))
195 ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
198 btr_consume(btrn, ret);
202 btr_remove_node(&wn->btrn);
206 __malloc static void *oss_parse_config_or_die(int argc, char **argv)
208 struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
210 /* exits on errors */
211 oss_write_cmdline_parser(argc, argv, conf);
215 static void oss_free_config(void *conf)
217 oss_write_cmdline_parser_free(conf);
221 * The init function of the oss writer.
223 * \param w Pointer to the writer to initialize.
227 void oss_write_init(struct writer *w)
229 struct oss_write_args_info dummy;
231 oss_write_cmdline_parser_init(&dummy);
232 w->close = oss_close;
233 w->pre_select = oss_pre_select;
234 w->post_select = oss_post_select;
235 w->parse_config_or_die = oss_parse_config_or_die;
236 w->free_config = oss_free_config;
237 w->help = (struct ggo_help)DEFINE_GGO_HELP(oss_write);
238 oss_write_cmdline_parser_free(&dummy);