f64603ccc41fef8ac64cafed91ad20bd33742769
[paraslash.git] / oss_write.c
1 /*
2  * Copyright (C) 2009 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 <dirent.h>
13 #include <sys/soundcard.h>
14 #include <stdbool.h>
15
16 #include "para.h"
17 #include "fd.h"
18 #include "string.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "ggo.h"
22 #include "buffer_tree.h"
23 #include "write.h"
24 #include "write_common.h"
25 #include "oss_write.cmdline.h"
26 #include "error.h"
27
28 /** Always use 16 bit little endian. */
29 #define FORMAT AFMT_S16_LE
30
31 /** Data specific to the oss writer. */
32 struct private_oss_write_data {
33         /** The file handle of the device. */
34         int fd;
35         /**
36          * The samplerate given by command line option or the decoder
37          * of the writer node group.
38          */
39         int samplerate;
40         /**
41          * The number of channels, given by command line option or the
42          * decoder of the writer node group.
43          */
44         int channels;
45         /** Four bytes for stereo streams, two bytes for mono streams. */
46         int bytes_per_frame;
47 };
48
49 static void oss_pre_select_btr(struct sched *s, struct task *t)
50 {
51         struct writer_node *wn = container_of(t, struct writer_node, task);
52         struct private_oss_write_data *powd = wn->private_data;
53         int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
54
55         t->error = 0;
56         if (ret < 0)
57                 goto min_delay;
58         if (ret == 0)
59                 return;
60         if (!powd)
61                 goto min_delay;
62         para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
63         return;
64 min_delay:
65         s->timeout.tv_sec = 0;
66         s->timeout.tv_usec = 1;
67 }
68
69 static void oss_close(struct writer_node *wn)
70 {
71         struct private_oss_write_data *powd = wn->private_data;
72
73         close(powd->fd);
74         oss_cmdline_parser_free(wn->conf);
75         free(powd);
76 }
77
78 /*
79  * The Open Sound System Programmer's Guide sayeth:
80  *
81  * Set sampling parameters always so that number of channels (mono/stereo) is
82  * set before selecting sampling rate (speed).  Failing to do this will make
83  * your program incompatible with cards such as the SoundBlaster Pro which
84  * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
85  * selects 44.1 kHz speed and then sets the device to stereo mode will
86  * incorrectly believe that the device is still in 44.1 kHz mode when actually
87  * the speed is decreased to 22.05 kHz.
88  */
89 static int oss_init(struct writer_node *wn, unsigned samplerate, unsigned channels)
90 {
91         int ret, format = FORMAT, ch, rate;
92         struct oss_write_args_info *conf = wn->conf;
93         struct private_oss_write_data *powd = wn->private_data;
94
95         PARA_INFO_LOG("opening %s\n", conf->device_arg);
96         ret = para_open(conf->device_arg, O_WRONLY, 0);
97         if (ret < 0)
98                 return ret;
99         powd->fd = ret;
100         ret = mark_fd_nonblocking(powd->fd);
101         if (ret < 0)
102                 goto err;
103         /* set PCM format */
104         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
105         if (ret < 0) {
106                 ret = -ERRNO_TO_PARA_ERROR(errno);
107                 goto err;
108         }
109         ret = -E_BAD_SAMPLE_FORMAT;
110         if (format != FORMAT)
111                 goto err;
112         /* set number of channels */
113         if (!conf->channels_given && channels != 0)
114                 ch = channels;
115         else
116                 ch = conf->channels_arg;
117         ret = -E_BAD_CHANNEL_COUNT;
118         if (ch == 0)
119                 goto err;
120         powd->channels = ch;
121         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
122         if (ret < 0) {
123                 ret = -ERRNO_TO_PARA_ERROR(errno);
124                 goto err;
125         }
126         if (powd->channels != ch)
127                 goto err;
128         powd->bytes_per_frame = ch * 2;
129
130         /*
131          * Set sampling rate
132          *
133          * If we request a higher sampling rate than is supported by the
134          * device, the the highest possible speed is automatically used. The
135          * value actually used is returned as the new value of the argument.
136          */
137         if (!conf->samplerate_given && samplerate != 0)
138                 rate = samplerate;
139         else
140                 rate = conf->samplerate_arg;
141         powd->samplerate = rate;
142         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
143         if (ret < 0) {
144                 ret = -ERRNO_TO_PARA_ERROR(errno);
145                 goto err;
146         }
147         if (rate != powd->samplerate) {
148                 int min = PARA_MIN(rate, powd->samplerate),
149                         max = PARA_MAX(rate, powd->samplerate);
150                 /*
151                  * Check whether the returned sample rate differs significantly
152                  * from the requested one.
153                  */
154                 ret = -E_BAD_SAMPLERATE;
155                 if (100 * max > 110 * min) /* more than 10% deviation */
156                         goto err;
157                 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
158                         powd->samplerate);
159         }
160         wn->min_iqs = powd->bytes_per_frame;
161         return 1;
162 err:
163         close(powd->fd);
164         free(powd);
165         return ret;
166 }
167
168 static void oss_post_select_btr(__a_unused struct sched *s,
169                 struct task *t)
170 {
171         struct writer_node *wn = container_of(t, struct writer_node, task);
172         struct oss_write_args_info *conf = wn->conf;
173         struct private_oss_write_data *powd = wn->private_data;
174         struct btr_node *btrn = wn->btrn;
175         size_t frames, bytes;
176         int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
177         char *data;
178
179         if (ret < 0)
180                 goto out;
181         if (ret == 0)
182                 return;
183         if (powd->fd < 0) {
184                 int32_t rate, ch;
185                 ret = -1;
186                 if (!conf->samplerate_given) /* config option trumps btr_exec */
187                         ret = get_btr_samplerate(wn->btrn, &rate);
188                 if (ret < 0)
189                         rate = conf->samplerate_arg;
190                 ret = -1;
191                 if (!conf->channels_given)
192                         ret = get_btr_channels(wn->btrn, &ch);
193                 if (ret < 0)
194                         ch = conf->channels_arg;
195                 ret = oss_init(wn, rate, ch);
196                 if (ret < 0)
197                         goto out;
198                 return;
199         }
200         bytes = btr_next_buffer(btrn, &data);
201         frames = bytes / powd->bytes_per_frame;
202         if (!frames) { /* eof and less than a single frame available */
203                 ret = -E_OSS_EOF;
204                 goto out;
205         }
206         ret = 0;
207         if (!FD_ISSET(powd->fd, &s->wfds))
208                 goto out;
209         ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame, 0);
210         if (ret < 0)
211                 goto out;
212         btr_consume(btrn, ret);
213         ret = 0;
214 out:
215         t->error = ret;
216         if (ret < 0)
217                 btr_remove_node(btrn);
218 }
219
220 static int oss_open(struct writer_node *wn)
221 {
222         struct writer_node_group *wng;
223         struct private_oss_write_data *powd;
224
225         powd = para_calloc(sizeof(*powd));
226         wn->private_data = powd;
227         if (!wn->btrn) {
228                 wng = wn->wng;
229                 return oss_init(wn, wng->samplerate ? *wng->samplerate : 0,
230                         wng->channels? *wng->channels : 0);
231         }
232         powd->fd = -1;
233         return 1;
234 }
235
236 __malloc static void *oss_parse_config(const char *options)
237 {
238         int ret;
239         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
240
241         PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t"));
242         ret = oss_cmdline_parser_string(options, conf, "oss_write");
243         if (ret)
244                 goto err_out;
245         return conf;
246 err_out:
247         free(conf);
248         return NULL;
249 }
250
251 static void oss_free_config(void *conf)
252 {
253         oss_cmdline_parser_free(conf);
254 }
255
256 /**
257  * The init function of the oss writer.
258  *
259  * \param w Pointer to the writer to initialize.
260  *
261  * \sa struct writer.
262  */
263 void oss_write_init(struct writer *w)
264 {
265         struct oss_write_args_info dummy;
266
267         oss_cmdline_parser_init(&dummy);
268         w->open = oss_open;
269         w->close = oss_close;
270         w->pre_select_btr = oss_pre_select_btr;
271         w->post_select_btr = oss_post_select_btr;
272         w->parse_config = oss_parse_config;
273         w->free_config = oss_free_config;
274         w->shutdown = NULL;
275         w->help = (struct ggo_help) {
276                 .short_help = oss_write_args_info_help,
277                 .detailed_help = oss_write_args_info_detailed_help
278         };
279         oss_cmdline_parser_free(&dummy);
280 }