Merge branch 'refs/heads/t/com_tasks'
[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 audio_buf_info abi;
193
194 ret = task_get_notification(wn->task);
195 if (ret < 0)
196 goto out;
197 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
198 if (ret <= 0)
199 goto out;
200 if (!powd) {
201 int32_t rate, ch, format;
202
203 if (sound_device_is_busy())
204 return 0;
205 get_btr_sample_rate(btrn, &rate);
206 get_btr_channels(btrn, &ch);
207 get_btr_sample_format(btrn, &format);
208 ret = oss_init(wn, rate, ch, format);
209 if (ret < 0)
210 goto out;
211 set_sound_device_busy();
212 return 0;
213 }
214 btr_merge(btrn, wn->min_iqs);
215 bytes = btr_next_buffer(btrn, &data);
216 frames = bytes / powd->bytes_per_frame;
217 if (!frames) { /* eof and less than a single frame available */
218 ret = -E_WRITE_COMMON_EOF;
219 goto out;
220 }
221 ret = 0;
222 if (!FD_ISSET(powd->fd, &s->wfds))
223 goto out;
224 /* get maximal number of bytes that can be written */
225 ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi);
226 if (ret >= 0) {
227 size_t max_frames = abi.bytes / powd->bytes_per_frame;
228 if (max_frames == 0)
229 goto out;
230 /* cap number of frames to avoid sound artefacts */
231 frames = PARA_MIN(frames, max_frames);
232 }
233 ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
234 if (ret < 0)
235 goto out;
236 btr_consume(btrn, ret);
237 ret = 0;
238 out:
239 if (ret < 0)
240 btr_remove_node(&wn->btrn);
241 return ret;
242 }
243
244 __malloc static void *oss_parse_config_or_die(int argc, char **argv)
245 {
246 struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
247
248 /* exits on errors */
249 oss_write_cmdline_parser(argc, argv, conf);
250 return conf;
251 }
252
253 static void oss_free_config(void *conf)
254 {
255 oss_write_cmdline_parser_free(conf);
256 }
257
258 /**
259 * The init function of the oss writer.
260 *
261 * \param w Pointer to the writer to initialize.
262 *
263 * \sa struct writer.
264 */
265 void oss_write_init(struct writer *w)
266 {
267 struct oss_write_args_info dummy;
268
269 oss_write_cmdline_parser_init(&dummy);
270 w->close = oss_close;
271 w->pre_select = oss_pre_select;
272 w->post_select = oss_post_select;
273 w->parse_config_or_die = oss_parse_config_or_die;
274 w->free_config = oss_free_config;
275 w->help = (struct ggo_help)DEFINE_GGO_HELP(oss_write);
276 oss_write_cmdline_parser_free(&dummy);
277 }