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