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