2 * Copyright (C) 2009-2012 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, struct task *t)
49 struct writer_node *wn = container_of(t, struct writer_node, task);
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;
158 static void oss_post_select(__a_unused struct sched *s,
161 struct writer_node *wn = container_of(t, struct writer_node, task);
162 struct private_oss_write_data *powd = wn->private_data;
163 struct btr_node *btrn = wn->btrn;
164 size_t frames, bytes;
165 int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
173 int32_t rate, ch, format;
174 get_btr_sample_rate(btrn, &rate);
175 get_btr_channels(btrn, &ch);
176 get_btr_sample_format(btrn, &format);
177 ret = oss_init(wn, rate, ch, format);
182 btr_merge(btrn, wn->min_iqs);
183 bytes = btr_next_buffer(btrn, &data);
184 frames = bytes / powd->bytes_per_frame;
185 if (!frames) { /* eof and less than a single frame available */
186 ret = -E_WRITE_COMMON_EOF;
190 if (!FD_ISSET(powd->fd, &s->wfds))
192 ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame);
195 btr_consume(btrn, ret);
200 btr_remove_node(btrn);
203 __malloc static void *oss_parse_config_or_die(const char *options)
205 struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
207 /* exits on errors */
208 oss_cmdline_parser_string(options, conf, "oss_write");
212 static void oss_free_config(void *conf)
214 oss_cmdline_parser_free(conf);
218 * The init function of the oss writer.
220 * \param w Pointer to the writer to initialize.
224 void oss_write_init(struct writer *w)
226 struct oss_write_args_info dummy;
228 oss_cmdline_parser_init(&dummy);
229 w->close = oss_close;
230 w->pre_select = oss_pre_select;
231 w->post_select = oss_post_select;
232 w->parse_config_or_die = oss_parse_config_or_die;
233 w->free_config = oss_free_config;
235 w->help = (struct ggo_help) {
236 .short_help = oss_write_args_info_help,
237 .detailed_help = oss_write_args_info_detailed_help
239 oss_cmdline_parser_free(&dummy);