Merge branch 't/configure_fix'
[paraslash.git] / oss_write.c
1 /*
2  * Copyright (C) 2009-2012 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         return ret;
156 }
157
158 static void oss_post_select(__a_unused struct sched *s,
159                 struct task *t)
160 {
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);
166         char *data;
167
168         if (ret < 0)
169                 goto out;
170         if (ret == 0)
171                 return;
172         if (!powd) {
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);
178                 if (ret < 0)
179                         goto out;
180                 return;
181         }
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;
187                 goto out;
188         }
189         ret = 0;
190         if (!FD_ISSET(powd->fd, &s->wfds))
191                 goto out;
192         ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
193         if (ret < 0)
194                 goto out;
195         btr_consume(btrn, ret);
196         ret = 0;
197 out:
198         t->error = ret;
199         if (ret < 0)
200                 btr_remove_node(btrn);
201 }
202
203 __malloc static void *oss_parse_config_or_die(const char *options)
204 {
205         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
206
207         /* exits on errors */
208         oss_cmdline_parser_string(options, conf, "oss_write");
209         return conf;
210 }
211
212 static void oss_free_config(void *conf)
213 {
214         oss_cmdline_parser_free(conf);
215 }
216
217 /**
218  * The init function of the oss writer.
219  *
220  * \param w Pointer to the writer to initialize.
221  *
222  * \sa struct writer.
223  */
224 void oss_write_init(struct writer *w)
225 {
226         struct oss_write_args_info dummy;
227
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;
234         w->shutdown = NULL;
235         w->help = (struct ggo_help) {
236                 .short_help = oss_write_args_info_help,
237                 .detailed_help = oss_write_args_info_detailed_help
238         };
239         oss_cmdline_parser_free(&dummy);
240 }