client: Silence a gcc-7.1,0 warning.
[paraslash.git] / oss_write.c
1 /*
2  * Copyright (C) 2009 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 <sys/soundcard.h>
12 #include <lopsub.h>
13
14 #include "write_cmd.lsg.h"
15 #include "para.h"
16 #include "fd.h"
17 #include "string.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "buffer_tree.h"
21 #include "write.h"
22 #include "error.h"
23
24 /** Data specific to the oss writer. */
25 struct private_oss_write_data {
26         /** The file handle of the device. */
27         int fd;
28         /** Four bytes for stereo streams, two bytes for mono streams. */
29         int bytes_per_frame;
30 };
31
32 /*
33  * We keep one bit of static storage to make sure only one instance of the oss
34  * writer is running at any given time.
35  */
36 static bool sound_device_busy;
37
38 static bool sound_device_is_busy(void)
39 {
40         return sound_device_busy;
41 }
42
43 static void set_sound_device_busy(void)
44 {
45         assert(!sound_device_busy);
46         sound_device_busy = true;
47 }
48
49 static void set_sound_device_idle(void)
50 {
51         assert(sound_device_busy);
52         sound_device_busy = false;
53 }
54
55 static int get_oss_format(enum sample_format sf)
56 {
57         switch (sf) {
58         case SF_S8: return AFMT_S8;
59         case SF_U8: return AFMT_U8;
60         case SF_S16_LE: return AFMT_S16_LE;
61         case SF_S16_BE: return AFMT_S16_BE;
62         case SF_U16_LE: return AFMT_U16_LE;
63         case SF_U16_BE: return AFMT_U16_BE;
64         default: return AFMT_S16_LE;
65         }
66 }
67
68 static void oss_pre_select(struct sched *s, void *context)
69 {
70         struct writer_node *wn = context;
71         struct private_oss_write_data *powd = wn->private_data;
72         int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
73
74         if (ret == 0 || (sound_device_is_busy() && !powd))
75                 return;
76         if (ret < 0 || !powd)
77                 return sched_min_delay(s);
78         para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
79 }
80
81 static void oss_close(struct writer_node *wn)
82 {
83         struct private_oss_write_data *powd = wn->private_data;
84
85         if (!powd)
86                 return;
87         close(powd->fd);
88         free(powd);
89         set_sound_device_idle();
90 }
91
92 /*
93  * The Open Sound System Programmer's Guide sayeth:
94  *
95  * Set sampling parameters always so that number of channels (mono/stereo) is
96  * set before selecting sampling rate (speed).  Failing to do this will make
97  * your program incompatible with cards such as the SoundBlaster Pro which
98  * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
99  * selects 44.1 kHz speed and then sets the device to stereo mode will
100  * incorrectly believe that the device is still in 44.1 kHz mode when actually
101  * the speed is decreased to 22.05 kHz.
102  */
103 static int oss_init(struct writer_node *wn, unsigned sample_rate,
104                 unsigned channels, int sample_format)
105 {
106         int ret, format;
107         unsigned ch, rate;
108         struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
109         const char *dev = WRITE_CMD_OPT_STRING_VAL(OSS, DEVICE, wn->lpr);
110
111         PARA_INFO_LOG("opening %s\n", dev);
112         ret = para_open(dev, O_WRONLY, 0);
113         if (ret < 0)
114                 goto err_free;
115         powd->fd = ret;
116         ret = mark_fd_nonblocking(powd->fd);
117         if (ret < 0)
118                 goto err;
119         /* set PCM format */
120         sample_format = format = get_oss_format(sample_format);
121         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
122         if (ret < 0) {
123                 ret = -ERRNO_TO_PARA_ERROR(errno);
124                 goto err;
125         }
126         ret = -E_BAD_SAMPLE_FORMAT;
127         if (format != sample_format)
128                 goto err;
129         /* set number of channels */
130         ch = channels;
131         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
132         if (ret < 0) {
133                 ret = -ERRNO_TO_PARA_ERROR(errno);
134                 goto err;
135         }
136         ret = -E_BAD_CHANNEL_COUNT;
137         if (ch != channels)
138                 goto err;
139         if (format == SF_U8 || format == SF_S8)
140                 powd->bytes_per_frame = ch;
141         else
142                 powd->bytes_per_frame = ch * 2;
143
144         /*
145          * Set sampling rate
146          *
147          * If we request a higher sampling rate than is supported by the
148          * device, the highest possible speed is automatically used. The
149          * value actually used is returned as the new value of the argument.
150          */
151         rate = sample_rate;
152         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
153         if (ret < 0) {
154                 ret = -ERRNO_TO_PARA_ERROR(errno);
155                 goto err;
156         }
157         if (rate != sample_rate) {
158                 unsigned min = PARA_MIN(rate, sample_rate),
159                         max = PARA_MAX(rate, sample_rate);
160                 /*
161                  * Check whether the returned sample rate differs significantly
162                  * from the requested one.
163                  */
164                 ret = -E_BAD_SAMPLERATE;
165                 if (100 * max > 110 * min) /* more than 10% deviation */
166                         goto err;
167                 PARA_NOTICE_LOG("using %uHz rather than %uHz\n", rate,
168                         sample_rate);
169         }
170         wn->min_iqs = powd->bytes_per_frame;
171         wn->private_data = powd;
172         return 1;
173 err:
174         close(powd->fd);
175 err_free:
176         free(powd);
177         PARA_ERROR_LOG("failed to init %s: %s\n", dev, para_strerror(-ret));
178         return ret;
179 }
180
181 static int oss_post_select(__a_unused struct sched *s, void *context)
182 {
183         struct writer_node *wn = context;
184         struct private_oss_write_data *powd = wn->private_data;
185         struct btr_node *btrn = wn->btrn;
186         size_t frames, bytes;
187         int ret;
188         char *data;
189         audio_buf_info abi;
190
191         ret = task_get_notification(wn->task);
192         if (ret < 0)
193                 goto out;
194         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
195         if (ret <= 0)
196                 goto out;
197         if (!powd) {
198                 int32_t rate, ch, format;
199
200                 if (sound_device_is_busy())
201                         return 0;
202                 get_btr_sample_rate(btrn, &rate);
203                 get_btr_channels(btrn, &ch);
204                 get_btr_sample_format(btrn, &format);
205                 ret = oss_init(wn, rate, ch, format);
206                 if (ret < 0)
207                         goto out;
208                 set_sound_device_busy();
209                 return 0;
210         }
211         btr_merge(btrn, wn->min_iqs);
212         bytes = btr_next_buffer(btrn, &data);
213         frames = bytes / powd->bytes_per_frame;
214         if (!frames) { /* eof and less than a single frame available */
215                 ret = -E_WRITE_COMMON_EOF;
216                 goto out;
217         }
218         ret = 0;
219         if (!FD_ISSET(powd->fd, &s->wfds))
220                 goto out;
221         /* get maximal number of bytes that can be written */
222         ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi);
223         if (ret >= 0) {
224                 size_t max_frames = abi.bytes / powd->bytes_per_frame;
225                 if (max_frames == 0)
226                         goto out;
227                 /* cap number of frames to avoid sound artefacts */
228                 frames = PARA_MIN(frames, max_frames);
229         }
230         ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
231         if (ret < 0)
232                 goto out;
233         btr_consume(btrn, ret);
234         ret = 0;
235 out:
236         if (ret < 0)
237                 btr_remove_node(&wn->btrn);
238         return ret;
239 }
240
241 const struct writer lsg_write_cmd_com_oss_user_data = {
242         .pre_select = oss_pre_select,
243         .post_select = oss_post_select,
244         .close = oss_close,
245 };