2 * Copyright (C) 2009-2010 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>
13 #include <sys/soundcard.h>
22 #include "buffer_tree.h"
24 #include "write_common.h"
25 #include "oss_write.cmdline.h"
28 /** Always use 16 bit little endian. */
29 #define FORMAT AFMT_S16_LE
31 /** Data specific to the oss writer. */
32 struct private_oss_write_data {
33 /** The file handle of the device. */
35 /** Four bytes for stereo streams, two bytes for mono streams. */
39 static void oss_pre_select(struct sched *s, struct task *t)
41 struct writer_node *wn = container_of(t, struct writer_node, task);
42 struct private_oss_write_data *powd = wn->private_data;
43 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
49 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
52 static void oss_close(struct writer_node *wn)
54 struct private_oss_write_data *powd = wn->private_data;
62 * The Open Sound System Programmer's Guide sayeth:
64 * Set sampling parameters always so that number of channels (mono/stereo) is
65 * set before selecting sampling rate (speed). Failing to do this will make
66 * your program incompatible with cards such as the SoundBlaster Pro which
67 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
68 * selects 44.1 kHz speed and then sets the device to stereo mode will
69 * incorrectly believe that the device is still in 44.1 kHz mode when actually
70 * the speed is decreased to 22.05 kHz.
72 static int oss_init(struct writer_node *wn, unsigned sample_rate, unsigned channels)
74 int ret, format = FORMAT;
76 struct oss_write_args_info *conf = wn->conf;
77 struct private_oss_write_data *powd = wn->private_data;
79 PARA_INFO_LOG("opening %s\n", conf->device_arg);
80 ret = para_open(conf->device_arg, O_WRONLY, 0);
84 ret = mark_fd_nonblocking(powd->fd);
88 ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
90 ret = -ERRNO_TO_PARA_ERROR(errno);
93 ret = -E_BAD_SAMPLE_FORMAT;
96 /* set number of channels */
97 ret = -E_BAD_CHANNEL_COUNT;
101 ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
103 ret = -ERRNO_TO_PARA_ERROR(errno);
106 ret = -E_BAD_CHANNEL_COUNT;
109 powd->bytes_per_frame = ch * 2;
114 * If we request a higher sampling rate than is supported by the
115 * device, the the highest possible speed is automatically used. The
116 * value actually used is returned as the new value of the argument.
119 ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
121 ret = -ERRNO_TO_PARA_ERROR(errno);
124 if (rate != sample_rate) {
125 unsigned min = PARA_MIN(rate, sample_rate),
126 max = PARA_MAX(rate, sample_rate);
128 * Check whether the returned sample rate differs significantly
129 * from the requested one.
131 ret = -E_BAD_SAMPLERATE;
132 if (100 * max > 110 * min) /* more than 10% deviation */
134 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
137 wn->min_iqs = powd->bytes_per_frame;
145 static void oss_post_select(__a_unused struct sched *s,
148 struct writer_node *wn = container_of(t, struct writer_node, task);
149 struct private_oss_write_data *powd = wn->private_data;
150 struct btr_node *btrn = wn->btrn;
151 size_t frames, bytes;
152 int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
161 get_btr_sample_rate(btrn, &rate);
162 get_btr_channels(btrn, &ch);
163 ret = oss_init(wn, rate, ch);
168 btr_merge(btrn, wn->min_iqs);
169 bytes = btr_next_buffer(btrn, &data);
170 frames = bytes / powd->bytes_per_frame;
171 if (!frames) { /* eof and less than a single frame available */
176 if (!FD_ISSET(powd->fd, &s->wfds))
178 ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame);
181 btr_consume(btrn, ret);
186 btr_remove_node(btrn);
189 static int oss_open(struct writer_node *wn)
191 struct private_oss_write_data *powd;
193 powd = para_calloc(sizeof(*powd));
194 wn->private_data = powd;
199 __malloc static void *oss_parse_config(const char *options)
202 struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
204 ret = oss_cmdline_parser_string(options, conf, "oss_write");
213 static void oss_free_config(void *conf)
215 oss_cmdline_parser_free(conf);
219 * The init function of the oss writer.
221 * \param w Pointer to the writer to initialize.
225 void oss_write_init(struct writer *w)
227 struct oss_write_args_info dummy;
229 oss_cmdline_parser_init(&dummy);
231 w->close = oss_close;
232 w->pre_select = oss_pre_select;
233 w->post_select = oss_post_select;
234 w->parse_config = oss_parse_config;
235 w->free_config = oss_free_config;
237 w->help = (struct ggo_help) {
238 .short_help = oss_write_args_info_help,
239 .detailed_help = oss_write_args_info_detailed_help
241 oss_cmdline_parser_free(&dummy);