web: Remove style element in html.
[paraslash.git] / oss_write.c
1 /*
2  * Copyright (C) 2009-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file oss_write.c Paraslash's oss output plugin. */
8
9 #include <regex.h>
10 #include <sys/ioctl.h>
11 #include <fcntl.h>
12 #include <sys/soundcard.h>
13
14 #include "para.h"
15 #include "fd.h"
16 #include "string.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "buffer_tree.h"
21 #include "write.h"
22 #include "write_common.h"
23 #include "oss_write.cmdline.h"
24 #include "error.h"
25
26 /** Data specific to the oss writer. */
27 struct private_oss_write_data {
28         /** The file handle of the device. */
29         int fd;
30         /** Four bytes for stereo streams, two bytes for mono streams. */
31         int bytes_per_frame;
32 };
33
34 static int get_oss_format(enum sample_format sf)
35 {
36         switch (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;
44         }
45 }
46
47 static void oss_pre_select(struct sched *s, struct task *t)
48 {
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);
52
53         if (ret == 0)
54                 return;
55         if (ret < 0 || !powd)
56                 return sched_min_delay(s);
57         para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
58 }
59
60 static void oss_close(struct writer_node *wn)
61 {
62         struct private_oss_write_data *powd = wn->private_data;
63
64         if (!powd)
65                 return;
66         close(powd->fd);
67         free(powd);
68 }
69
70 /*
71  * The Open Sound System Programmer's Guide sayeth:
72  *
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.
80  */
81 static int oss_init(struct writer_node *wn, unsigned sample_rate,
82                 unsigned channels, int sample_format)
83 {
84         int ret, format;
85         unsigned ch, rate;
86         struct oss_write_args_info *conf = wn->conf;
87         struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
88
89         PARA_INFO_LOG("opening %s\n", conf->device_arg);
90         ret = para_open(conf->device_arg, O_WRONLY, 0);
91         if (ret < 0)
92                 goto err_free;
93         powd->fd = ret;
94         ret = mark_fd_nonblocking(powd->fd);
95         if (ret < 0)
96                 goto err;
97         /* set PCM format */
98         sample_format = format = get_oss_format(sample_format);
99         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
100         if (ret < 0) {
101                 ret = -ERRNO_TO_PARA_ERROR(errno);
102                 goto err;
103         }
104         ret = -E_BAD_SAMPLE_FORMAT;
105         if (format != sample_format)
106                 goto err;
107         /* set number of channels */
108         ch = channels;
109         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
110         if (ret < 0) {
111                 ret = -ERRNO_TO_PARA_ERROR(errno);
112                 goto err;
113         }
114         ret = -E_BAD_CHANNEL_COUNT;
115         if (ch != channels)
116                 goto err;
117         if (format == SF_U8 || format == SF_S8)
118                 powd->bytes_per_frame = ch;
119         else
120                 powd->bytes_per_frame = ch * 2;
121
122         /*
123          * Set sampling rate
124          *
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.
128          */
129         rate = sample_rate;
130         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
131         if (ret < 0) {
132                 ret = -ERRNO_TO_PARA_ERROR(errno);
133                 goto err;
134         }
135         if (rate != sample_rate) {
136                 unsigned min = PARA_MIN(rate, sample_rate),
137                         max = PARA_MAX(rate, sample_rate);
138                 /*
139                  * Check whether the returned sample rate differs significantly
140                  * from the requested one.
141                  */
142                 ret = -E_BAD_SAMPLERATE;
143                 if (100 * max > 110 * min) /* more than 10% deviation */
144                         goto err;
145                 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
146                         sample_rate);
147         }
148         wn->min_iqs = powd->bytes_per_frame;
149         wn->private_data = powd;
150         return 1;
151 err:
152         close(powd->fd);
153 err_free:
154         free(powd);
155         PARA_ERROR_LOG("failed to init %s: %s\n", conf->device_arg,
156                 para_strerror(-ret));
157         return ret;
158 }
159
160 static int oss_post_select(__a_unused struct sched *s,
161                 struct task *t)
162 {
163         struct writer_node *wn = container_of(t, struct writer_node, task);
164         struct private_oss_write_data *powd = wn->private_data;
165         struct btr_node *btrn = wn->btrn;
166         size_t frames, bytes;
167         int ret;
168         char *data;
169
170         ret = task_get_notification(t);
171         if (ret < 0)
172                 goto out;
173         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
174         if (ret <= 0)
175                 goto out;
176         if (!powd) {
177                 int32_t rate, ch, format;
178                 get_btr_sample_rate(btrn, &rate);
179                 get_btr_channels(btrn, &ch);
180                 get_btr_sample_format(btrn, &format);
181                 ret = oss_init(wn, rate, ch, format);
182                 if (ret < 0)
183                         goto out;
184                 return 0;
185         }
186         btr_merge(btrn, wn->min_iqs);
187         bytes = btr_next_buffer(btrn, &data);
188         frames = bytes / powd->bytes_per_frame;
189         if (!frames) { /* eof and less than a single frame available */
190                 ret = -E_WRITE_COMMON_EOF;
191                 goto out;
192         }
193         ret = 0;
194         if (!FD_ISSET(powd->fd, &s->wfds))
195                 goto out;
196         ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
197         if (ret < 0)
198                 goto out;
199         btr_consume(btrn, ret);
200         ret = 0;
201 out:
202         if (ret < 0)
203                 btr_remove_node(&wn->btrn);
204         return ret;
205 }
206
207 __malloc static void *oss_parse_config_or_die(int argc, char **argv)
208 {
209         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
210
211         /* exits on errors */
212         oss_write_cmdline_parser(argc, argv, conf);
213         return conf;
214 }
215
216 static void oss_free_config(void *conf)
217 {
218         oss_write_cmdline_parser_free(conf);
219 }
220
221 /**
222  * The init function of the oss writer.
223  *
224  * \param w Pointer to the writer to initialize.
225  *
226  * \sa struct writer.
227  */
228 void oss_write_init(struct writer *w)
229 {
230         struct oss_write_args_info dummy;
231
232         oss_write_cmdline_parser_init(&dummy);
233         w->close = oss_close;
234         w->pre_select = oss_pre_select;
235         w->post_select = oss_post_select;
236         w->parse_config_or_die = oss_parse_config_or_die;
237         w->free_config = oss_free_config;
238         w->help = (struct ggo_help)DEFINE_GGO_HELP(oss_write);
239         oss_write_cmdline_parser_free(&dummy);
240 }