The ogg/vorbis tagger.
[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
13 #include "para.h"
14 #include "fd.h"
15 #include "string.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "oss_write.cmdline.h"
23 #include "error.h"
24
25 /** Data specific to the oss writer. */
26 struct private_oss_write_data {
27         /** The file handle of the device. */
28         int fd;
29         /** Four bytes for stereo streams, two bytes for mono streams. */
30         int bytes_per_frame;
31 };
32
33 /*
34  * We keep one bit of static storage to make sure only one instance of the oss
35  * writer is running at any given time.
36  */
37 static bool sound_device_busy;
38
39 static bool sound_device_is_busy(void)
40 {
41         return sound_device_busy;
42 }
43
44 static void set_sound_device_busy(void)
45 {
46         assert(!sound_device_busy);
47         sound_device_busy = true;
48 }
49
50 static void set_sound_device_idle(void)
51 {
52         assert(sound_device_busy);
53         sound_device_busy = false;
54 }
55
56 static int get_oss_format(enum sample_format sf)
57 {
58         switch (sf) {
59         case SF_S8: return AFMT_S8;
60         case SF_U8: return AFMT_U8;
61         case SF_S16_LE: return AFMT_S16_LE;
62         case SF_S16_BE: return AFMT_S16_BE;
63         case SF_U16_LE: return AFMT_U16_LE;
64         case SF_U16_BE: return AFMT_U16_BE;
65         default: return AFMT_S16_LE;
66         }
67 }
68
69 static void oss_pre_select(struct sched *s, void *context)
70 {
71         struct writer_node *wn = context;
72         struct private_oss_write_data *powd = wn->private_data;
73         int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
74
75         if (ret == 0 || (sound_device_is_busy() && !powd))
76                 return;
77         if (ret < 0 || !powd)
78                 return sched_min_delay(s);
79         para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
80 }
81
82 static void oss_close(struct writer_node *wn)
83 {
84         struct private_oss_write_data *powd = wn->private_data;
85
86         if (!powd)
87                 return;
88         close(powd->fd);
89         free(powd);
90         set_sound_device_idle();
91 }
92
93 /*
94  * The Open Sound System Programmer's Guide sayeth:
95  *
96  * Set sampling parameters always so that number of channels (mono/stereo) is
97  * set before selecting sampling rate (speed).  Failing to do this will make
98  * your program incompatible with cards such as the SoundBlaster Pro which
99  * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
100  * selects 44.1 kHz speed and then sets the device to stereo mode will
101  * incorrectly believe that the device is still in 44.1 kHz mode when actually
102  * the speed is decreased to 22.05 kHz.
103  */
104 static int oss_init(struct writer_node *wn, unsigned sample_rate,
105                 unsigned channels, int sample_format)
106 {
107         int ret, format;
108         unsigned ch, rate;
109         struct oss_write_args_info *conf = wn->conf;
110         struct private_oss_write_data *powd = para_calloc(sizeof(*powd));
111
112         PARA_INFO_LOG("opening %s\n", conf->device_arg);
113         ret = para_open(conf->device_arg, O_WRONLY, 0);
114         if (ret < 0)
115                 goto err_free;
116         powd->fd = ret;
117         ret = mark_fd_nonblocking(powd->fd);
118         if (ret < 0)
119                 goto err;
120         /* set PCM format */
121         sample_format = format = get_oss_format(sample_format);
122         ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
123         if (ret < 0) {
124                 ret = -ERRNO_TO_PARA_ERROR(errno);
125                 goto err;
126         }
127         ret = -E_BAD_SAMPLE_FORMAT;
128         if (format != sample_format)
129                 goto err;
130         /* set number of channels */
131         ch = channels;
132         ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &ch);
133         if (ret < 0) {
134                 ret = -ERRNO_TO_PARA_ERROR(errno);
135                 goto err;
136         }
137         ret = -E_BAD_CHANNEL_COUNT;
138         if (ch != channels)
139                 goto err;
140         if (format == SF_U8 || format == SF_S8)
141                 powd->bytes_per_frame = ch;
142         else
143                 powd->bytes_per_frame = ch * 2;
144
145         /*
146          * Set sampling rate
147          *
148          * If we request a higher sampling rate than is supported by the
149          * device, the highest possible speed is automatically used. The
150          * value actually used is returned as the new value of the argument.
151          */
152         rate = sample_rate;
153         ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &rate);
154         if (ret < 0) {
155                 ret = -ERRNO_TO_PARA_ERROR(errno);
156                 goto err;
157         }
158         if (rate != sample_rate) {
159                 unsigned min = PARA_MIN(rate, sample_rate),
160                         max = PARA_MAX(rate, sample_rate);
161                 /*
162                  * Check whether the returned sample rate differs significantly
163                  * from the requested one.
164                  */
165                 ret = -E_BAD_SAMPLERATE;
166                 if (100 * max > 110 * min) /* more than 10% deviation */
167                         goto err;
168                 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", rate,
169                         sample_rate);
170         }
171         wn->min_iqs = powd->bytes_per_frame;
172         wn->private_data = powd;
173         return 1;
174 err:
175         close(powd->fd);
176 err_free:
177         free(powd);
178         PARA_ERROR_LOG("failed to init %s: %s\n", conf->device_arg,
179                 para_strerror(-ret));
180         return ret;
181 }
182
183 static int oss_post_select(__a_unused struct sched *s, void *context)
184 {
185         struct writer_node *wn = context;
186         struct private_oss_write_data *powd = wn->private_data;
187         struct btr_node *btrn = wn->btrn;
188         size_t frames, bytes;
189         int ret;
190         char *data;
191         audio_buf_info abi;
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         /* get maximal number of bytes that can be written */
224         ret = ioctl(powd->fd, SNDCTL_DSP_GETOSPACE, &abi);
225         if (ret >= 0) {
226                 size_t max_frames = abi.bytes / powd->bytes_per_frame;
227                 if (max_frames == 0)
228                         goto out;
229                 /* cap number of frames to avoid sound artefacts */
230                 frames = PARA_MIN(frames, max_frames);
231         }
232         ret = xwrite(powd->fd, data, frames * powd->bytes_per_frame);
233         if (ret < 0)
234                 goto out;
235         btr_consume(btrn, ret);
236         ret = 0;
237 out:
238         if (ret < 0)
239                 btr_remove_node(&wn->btrn);
240         return ret;
241 }
242
243 __malloc static void *oss_parse_config_or_die(int argc, char **argv)
244 {
245         struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
246
247         /* exits on errors */
248         oss_write_cmdline_parser(argc, argv, conf);
249         return conf;
250 }
251
252 static void oss_free_config(void *conf)
253 {
254         oss_write_cmdline_parser_free(conf);
255 }
256
257 /**
258  * The init function of the oss writer.
259  *
260  * \param w Pointer to the writer to initialize.
261  *
262  * \sa struct writer.
263  */
264 void oss_write_init(struct writer *w)
265 {
266         struct oss_write_args_info dummy;
267
268         oss_write_cmdline_parser_init(&dummy);
269         w->close = oss_close;
270         w->pre_select = oss_pre_select;
271         w->post_select = oss_post_select;
272         w->parse_config_or_die = oss_parse_config_or_die;
273         w->free_config = oss_free_config;
274         w->help = (struct ggo_help)DEFINE_GGO_HELP(oss_write);
275         oss_write_cmdline_parser_free(&dummy);
276 }