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