First draft of the oss writer.
[paraslash.git] / oss_write.c
1 /*
2 * Copyright (C) 2009 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 <sys/ioctl.h>
10 #include <fcntl.h>
11 #include <dirent.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 "write.h"
21 #include "oss_write.cmdline.h"
22 #include "error.h"
23
24 /** Always use 16 bit little endian. */
25 #define FORMAT AFMT_S16_LE
26
27 /** Data specific to the oss writer. */
28 struct private_oss_write_data {
29 /** The file handle of the device. */
30 int fd;
31 /**
32 * The samplerate given by command line option or the decoder
33 * of the writer node group.
34 */
35 int samplerate;
36 /**
37 * The number of channels, given by command line option or the
38 * decoder of the writer node group.
39 */
40 int channels;
41 /** Four bytes for stereo streams, two bytes for mono streams. */
42 int bytes_per_frame;
43 };
44
45 static int oss_pre_select(struct sched *s, struct writer_node *wn)
46 {
47 struct private_oss_write_data *powd = wn->private_data;
48 struct writer_node_group *wng = wn->wng;
49
50 if (!*wng->loaded)
51 return 0;
52 para_fd_set(powd->fd, &s->wfds, &s->max_fileno);
53 return 1;
54 }
55 static int oss_post_select(__a_unused struct sched *s,
56 struct writer_node *wn)
57 {
58 int ret;
59 struct private_oss_write_data *powd = wn->private_data;
60 struct writer_node_group *wng = wn->wng;
61 size_t frames, bytes = *wng->loaded - wn->written;
62 char *data = *wng->bufp + wn->written;
63
64 if (*wng->input_error < 0 && bytes < powd->bytes_per_frame) {
65 wn->written = *wng->loaded;
66 return *wng->input_error;
67 }
68 frames = bytes / powd->bytes_per_frame;
69 if (!frames) /* less than a single frame available */
70 goto out;
71 if (!FD_ISSET(powd->fd, &s->wfds))
72 goto out;
73 ret = write_nonblock(powd->fd, data, frames * powd->bytes_per_frame, 0);
74 if (ret < 0)
75 return ret;
76 wn->written += ret;
77 out:
78 return 1;
79 }
80
81 static void oss_close(struct writer_node *wn)
82 {
83 struct private_oss_write_data *powd = wn->private_data;
84
85 close(powd->fd);
86 free(powd);
87 }
88
89 /*
90 * The Open Sound System Programmer's Guide sayeth:
91 *
92 * Set sampling parameters always so that number of channels (mono/stereo) is
93 * set before selecting sampling rate (speed). Failing to do this will make
94 * your program incompatible with cards such as the SoundBlaster Pro which
95 * supports 44.1 kHz in mono but just 22.05 kHz in stereo. A program which
96 * selects 44.1 kHz speed and then sets the device to stereo mode will
97 * incorrectly believe that the device is still in 44.1 kHz mode when actually
98 * the speed is decreased to 22.05 kHz.
99 */
100 static int oss_open(struct writer_node *wn)
101 {
102 int ret, format = FORMAT, channels, samplerate;
103 struct oss_write_args_info *conf = wn->conf;
104 struct writer_node_group *wng = wn->wng;
105 struct private_oss_write_data *powd;
106
107 PARA_INFO_LOG("opening %s\n", conf->device_arg);
108 ret = para_open(conf->device_arg, O_WRONLY, 0);
109 if (ret < 0)
110 return ret;
111 powd = para_calloc(sizeof(*powd));
112 wn->private_data = powd;
113 powd->fd = ret;
114 ret = mark_fd_nonblocking(powd->fd);
115 if (ret < 0)
116 goto err;
117 /* set PCM format */
118 ret = ioctl(powd->fd, SNDCTL_DSP_SETFMT, &format);
119 if (ret < 0) {
120 ret = -ERRNO_TO_PARA_ERROR(errno);
121 goto err;
122 }
123 ret = -E_BAD_SAMPLE_FORMAT;
124 if (format != FORMAT)
125 goto err;
126 /* set number of channels */
127 if (!conf->channels_given && wng->channels)
128 channels = *wng->channels;
129 else
130 channels = conf->channels_arg;
131 ret = -E_BAD_CHANNEL_COUNT;
132 if (channels == 0)
133 goto err;
134 powd->channels = channels;
135 ret = ioctl(powd->fd, SNDCTL_DSP_CHANNELS, &channels);
136 if (ret < 0) {
137 ret = -ERRNO_TO_PARA_ERROR(errno);
138 goto err;
139 }
140 if (powd->channels != channels)
141 goto err;
142 powd->bytes_per_frame = channels * 2;
143
144 /*
145 * Set sampling rate
146 *
147 * If we request a higher sampling rate than is supported by the
148 * device, the the highest possible speed is automatically used. The
149 * value actually used is returned as the new value of the argument.
150 */
151 if (!conf->samplerate_given && wng->samplerate)
152 samplerate = *wng->samplerate;
153 else
154 samplerate = conf->samplerate_arg;
155 powd->samplerate = samplerate;
156 ret = ioctl(powd->fd, SNDCTL_DSP_SPEED, &samplerate);
157 if (ret < 0) {
158 ret = -ERRNO_TO_PARA_ERROR(errno);
159 goto err;
160 }
161 if (samplerate != powd->samplerate) {
162 int min = PARA_MIN(samplerate, powd->samplerate),
163 max = PARA_MAX(samplerate, powd->samplerate);
164 /*
165 * Check whether the returned sample rate differs significantly
166 * from the requested one.
167 */
168 ret = -E_BAD_SAMPLERATE;
169 if (100 * max > 110 * min) /* more than 10% deviation */
170 goto err;
171 PARA_NOTICE_LOG("using %dHz rather than %dHz\n", samplerate,
172 powd->samplerate);
173 }
174
175 return 1;
176 err:
177 close(powd->fd);
178 free(powd);
179 return ret;
180 }
181
182 __malloc static void *oss_parse_config(const char *options)
183 {
184 int ret;
185 struct oss_write_args_info *conf = para_calloc(sizeof(*conf));
186
187 PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t"));
188 ret = oss_cmdline_parser_string(options, conf, "oss_write");
189 if (ret)
190 goto err_out;
191 return conf;
192 err_out:
193 free(conf);
194 return NULL;
195 }
196
197 /**
198 * The init function of the oss writer.
199 *
200 * \param w Pointer to the writer to initialize.
201 *
202 * \sa struct writer.
203 */
204 void oss_write_init(struct writer *w)
205 {
206 struct oss_write_args_info dummy;
207
208 oss_cmdline_parser_init(&dummy);
209 w->open = oss_open;
210 w->close = oss_close;
211 w->pre_select = oss_pre_select;
212 w->post_select = oss_post_select;
213 w->parse_config = oss_parse_config;
214 w->shutdown = NULL;
215 w->help = (struct ggo_help) {
216 .short_help = oss_write_args_info_help,
217 .detailed_help = oss_write_args_info_detailed_help
218 };
219 }