vss: Cleanup num_slices().
[paraslash.git] / oss_write.c
1 /*
2  * Copyright (C) 2009-2010 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 /** Data specific to the oss writer. */
29 struct private_oss_write_data {
30         /** The file handle of the device. */
31         int fd;
32         /** Four bytes for stereo streams, two bytes for mono streams. */
33         int bytes_per_frame;
34 };
35
36 static int get_oss_format(enum sample_format sf)
37 {
38         switch (sf) {
39         case SF_S8: return AFMT_S8;
40         case SF_U8: return AFMT_U8;
41         case SF_S16_LE: return AFMT_S16_LE;
42         case SF_S16_BE: return AFMT_S16_BE;
43         case SF_U16_LE: return AFMT_U16_LE;
44         case SF_U16_BE: return AFMT_U16_BE;
45         default: return AFMT_S16_LE;
46         }
47 }
48
49 static void oss_pre_select(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                 sched_min_delay(s);
58         else if (ret > 0)
59                 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
60 }
61
62 static void oss_close(struct writer_node *wn)
63 {
64         struct private_oss_write_data *powd = wn->private_data;
65
66         if (powd->fd >= 0)
67                 close(powd->fd);
68         free(powd);
69 }
70
71 /*
72  * The Open Sound System Programmer's Guide sayeth:
73  *
74  * Set sampling parameters always so that number of channels (mono/stereo) is
75  * set before selecting sampling rate (speed).  Failing to do this will make
76  * your program incompatible with cards such as the SoundBlaster Pro which
77  * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
78  * selects 44.1 kHz speed and then sets the device to stereo mode will
79  * incorrectly believe that the device is still in 44.1 kHz mode when actually
80  * the speed is decreased to 22.05 kHz.
81  */
82 static int oss_init(struct writer_node *wn, unsigned sample_rate,
83                 unsigned channels, int sample_format)
84 {
85         int ret, format;
86         unsigned ch, rate;
87         struct oss_write_args_info *conf = wn->conf;
88         struct private_oss_write_data *powd = wn->private_data;
89
90         PARA_INFO_LOG("opening %s\n", conf->device_arg);
91         ret = para_open(conf->device_arg, O_WRONLY, 0);
92         if (ret < 0)
93                 return ret;
94         powd->fd = ret;
95         ret = mark_fd_nonblocking(powd->fd);
96         if (ret < 0)
97                 goto err;
98         /* set PCM format */
99         sample_format = format = get_oss_format(sample_format);
100         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
101         if (ret < 0) {
102                 ret = -ERRNO_TO_PARA_ERROR(errno);
103                 goto err;
104         }
105         ret = -E_BAD_SAMPLE_FORMAT;
106         if (format != sample_format)
107                 goto err;
108         /* set number of channels */
109         ch = channels;
110         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
111         if (ret < 0) {
112                 ret = -ERRNO_TO_PARA_ERROR(errno);
113                 goto err;
114         }
115         ret = -E_BAD_CHANNEL_COUNT;
116         if (ch != channels)
117                 goto err;
118         if (format == SF_U8 || format == SF_S8)
119                 powd->bytes_per_frame = ch;
120         else
121                 powd->bytes_per_frame = ch * 2;
122
123         /*
124          * Set sampling rate
125          *
126          * If we request a higher sampling rate than is supported by the
127          * device, the the highest possible speed is automatically used. The
128          * value actually used is returned as the new value of the argument.
129          */
130         rate = sample_rate;
131         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
132         if (ret < 0) {
133                 ret = -ERRNO_TO_PARA_ERROR(errno);
134                 goto err;
135         }
136         if (rate != sample_rate) {
137                 unsigned min = PARA_MIN(rate, sample_rate),
138                         max = PARA_MAX(rate, sample_rate);
139                 /*
140                  * Check whether the returned sample rate differs significantly
141                  * from the requested one.
142                  */
143                 ret = -E_BAD_SAMPLERATE;
144                 if (100 * max > 110 * min) /* more than 10% deviation */
145                         goto err;
146                 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
147                         sample_rate);
148         }
149         wn->min_iqs = powd->bytes_per_frame;
150         return 1;
151 err:
152         close(powd->fd);
153         powd->fd = -1;
154         return ret;
155 }
156
157 static void oss_post_select(__a_unused struct sched *s,
158                 struct task *t)
159 {
160         struct writer_node *wn = container_of(t, struct writer_node, task);
161         struct private_oss_write_data *powd = wn->private_data;
162         struct btr_node *btrn = wn->btrn;
163         size_t frames, bytes;
164         int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
165         char *data;
166
167         if (ret < 0)
168                 goto out;
169         if (ret == 0)
170                 return;
171         if (powd->fd < 0) {
172                 int32_t rate, ch, format;
173                 get_btr_sample_rate(btrn, &rate);
174                 get_btr_channels(btrn, &ch);
175                 get_btr_sample_format(btrn, &format);
176                 ret = oss_init(wn, rate, ch, format);
177                 if (ret < 0)
178                         goto out;
179                 return;
180         }
181         btr_merge(btrn, wn->min_iqs);
182         bytes = btr_next_buffer(btrn, &data);
183         frames = bytes / powd->bytes_per_frame;
184         if (!frames) { /* eof and less than a single frame available */
185                 ret = -E_OSS_EOF;
186                 goto out;
187         }
188         ret = 0;
189         if (!FD_ISSET(powd->fd, &s->wfds))
190                 goto out;
191         ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame);
192         if (ret < 0)
193                 goto out;
194         btr_consume(btrn, ret);
195         ret = 0;
196 out:
197         t->error = ret;
198         if (ret < 0)
199                 btr_remove_node(btrn);
200 }
201
202 static int oss_open(struct writer_node *wn)
203 {
204         struct private_oss_write_data *powd;
205
206         powd = para_calloc(sizeof(*powd));
207         wn->private_data = powd;
208         powd->fd = -1;
209         return 1;
210 }
211
212 __malloc static void *oss_parse_config(const char *options)
213 {
214         int ret;
215         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
216
217         ret = oss_cmdline_parser_string(options, conf, "oss_write");
218         if (ret)
219                 goto err_out;
220         return conf;
221 err_out:
222         free(conf);
223         return NULL;
224 }
225
226 static void oss_free_config(void *conf)
227 {
228         oss_cmdline_parser_free(conf);
229 }
230
231 /**
232  * The init function of the oss writer.
233  *
234  * \param w Pointer to the writer to initialize.
235  *
236  * \sa struct writer.
237  */
238 void oss_write_init(struct writer *w)
239 {
240         struct oss_write_args_info dummy;
241
242         oss_cmdline_parser_init(&dummy);
243         w->open = oss_open;
244         w->close = oss_close;
245         w->pre_select = oss_pre_select;
246         w->post_select = oss_post_select;
247         w->parse_config = oss_parse_config;
248         w->free_config = oss_free_config;
249         w->shutdown = NULL;
250         w->help = (struct ggo_help) {
251                 .short_help = oss_write_args_info_help,
252                 .detailed_help = oss_write_args_info_detailed_help
253         };
254         oss_cmdline_parser_free(&dummy);
255 }