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