Merge commit 'meins/master'
[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 /** 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         /** Four bytes for stereo streams, two bytes for mono streams. */
36         int bytes_per_frame;
37 };
38
39 static void oss_pre_select(struct sched *s, struct task *t)
40 {
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);
44
45         t->error = 0;
46         if (ret < 0)
47                 sched_min_delay(s);
48         else if (ret > 0)
49                 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
50 }
51
52 static void oss_close(struct writer_node *wn)
53 {
54         struct private_oss_write_data *powd = wn->private_data;
55
56         if (powd->fd >= 0)
57                 close(powd->fd);
58         free(powd);
59 }
60
61 /*
62  * The Open Sound System Programmer's Guide sayeth:
63  *
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.
71  */
72 static int oss_init(struct writer_node *wn, unsigned samplerate, unsigned channels)
73 {
74         int ret, format = FORMAT;
75         unsigned ch, rate;
76         struct oss_write_args_info *conf = wn->conf;
77         struct private_oss_write_data *powd = wn->private_data;
78
79         PARA_INFO_LOG("opening %s\n", conf->device_arg);
80         ret = para_open(conf->device_arg, O_WRONLY, 0);
81         if (ret < 0)
82                 return ret;
83         powd->fd = ret;
84         ret = mark_fd_nonblocking(powd->fd);
85         if (ret < 0)
86                 goto err;
87         /* set PCM format */
88         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
89         if (ret < 0) {
90                 ret = -ERRNO_TO_PARA_ERROR(errno);
91                 goto err;
92         }
93         ret = -E_BAD_SAMPLE_FORMAT;
94         if (format != FORMAT)
95                 goto err;
96         /* set number of channels */
97         ret = -E_BAD_CHANNEL_COUNT;
98         if (channels == 0)
99                 goto err;
100         ch = channels;
101         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
102         if (ret < 0) {
103                 ret = -ERRNO_TO_PARA_ERROR(errno);
104                 goto err;
105         }
106         ret = -E_BAD_CHANNEL_COUNT;
107         if (ch != channels)
108                 goto err;
109         powd->bytes_per_frame = ch * 2;
110
111         /*
112          * Set sampling rate
113          *
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.
117          */
118         rate = samplerate;
119         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
120         if (ret < 0) {
121                 ret = -ERRNO_TO_PARA_ERROR(errno);
122                 goto err;
123         }
124         if (rate != samplerate) {
125                 unsigned min = PARA_MIN(rate, samplerate),
126                         max = PARA_MAX(rate, samplerate);
127                 /*
128                  * Check whether the returned sample rate differs significantly
129                  * from the requested one.
130                  */
131                 ret = -E_BAD_SAMPLERATE;
132                 if (100 * max > 110 * min) /* more than 10% deviation */
133                         goto err;
134                 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
135                         samplerate);
136         }
137         wn->min_iqs = powd->bytes_per_frame;
138         return 1;
139 err:
140         close(powd->fd);
141         powd->fd = -1;
142         return ret;
143 }
144
145 static void oss_post_select(__a_unused struct sched *s,
146                 struct task *t)
147 {
148         struct writer_node *wn = container_of(t, struct writer_node, task);
149         struct oss_write_args_info *conf = wn->conf;
150         struct private_oss_write_data *powd = wn->private_data;
151         struct btr_node *btrn = wn->btrn;
152         size_t frames, bytes;
153         int ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
154         char *data;
155
156         if (ret < 0)
157                 goto out;
158         if (ret == 0)
159                 return;
160         if (powd->fd < 0) {
161                 int32_t rate, ch;
162                 ret = -1;
163                 if (!conf->samplerate_given) /* config option trumps btr_exec */
164                         ret = get_btr_samplerate(wn->btrn, &rate);
165                 if (ret < 0)
166                         rate = conf->samplerate_arg;
167                 ret = -1;
168                 if (!conf->channels_given)
169                         ret = get_btr_channels(wn->btrn, &ch);
170                 if (ret < 0)
171                         ch = conf->channels_arg;
172                 ret = oss_init(wn, rate, ch);
173                 if (ret < 0)
174                         goto out;
175                 return;
176         }
177         bytes = btr_next_buffer(btrn, &data);
178         frames = bytes / powd->bytes_per_frame;
179         if (!frames) { /* eof and less than a single frame available */
180                 ret = -E_OSS_EOF;
181                 goto out;
182         }
183         ret = 0;
184         if (!FD_ISSET(powd->fd, &s->wfds))
185                 goto out;
186         ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame, 0);
187         if (ret < 0)
188                 goto out;
189         btr_consume(btrn, ret);
190         ret = 0;
191 out:
192         t->error = ret;
193         if (ret < 0)
194                 btr_remove_node(btrn);
195 }
196
197 static int oss_open(struct writer_node *wn)
198 {
199         struct private_oss_write_data *powd;
200
201         powd = para_calloc(sizeof(*powd));
202         wn->private_data = powd;
203         powd->fd = -1;
204         return 1;
205 }
206
207 __malloc static void *oss_parse_config(const char *options)
208 {
209         int ret;
210         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
211
212         ret = oss_cmdline_parser_string(options, conf, "oss_write");
213         if (ret)
214                 goto err_out;
215         return conf;
216 err_out:
217         free(conf);
218         return NULL;
219 }
220
221 static void oss_free_config(void *conf)
222 {
223         oss_cmdline_parser_free(conf);
224 }
225
226 /**
227  * The init function of the oss writer.
228  *
229  * \param w Pointer to the writer to initialize.
230  *
231  * \sa struct writer.
232  */
233 void oss_write_init(struct writer *w)
234 {
235         struct oss_write_args_info dummy;
236
237         oss_cmdline_parser_init(&dummy);
238         w->open = oss_open;
239         w->close = oss_close;
240         w->pre_select = oss_pre_select;
241         w->post_select = oss_post_select;
242         w->parse_config = oss_parse_config;
243         w->free_config = oss_free_config;
244         w->shutdown = NULL;
245         w->help = (struct ggo_help) {
246                 .short_help = oss_write_args_info_help,
247                 .detailed_help = oss_write_args_info_detailed_help
248         };
249         oss_cmdline_parser_free(&dummy);
250 }