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