chiark / gitweb /
rights now apply to commands; docs catch up a bit
[disorder] / server / normalize.c
CommitLineData
6d2d327c
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
717ba987 20/** @file server/normalize.c
6d2d327c
RK
21 * @brief Convert "raw" format output to the configured format
22 *
23 * Currently we invoke sox even for trivial conversions such as byte-swapping.
24 * Ideally we would do all conversion including resampling in this one process
25 * and eliminate the dependency on sox.
26 */
27
28#include <config.h>
29#include "types.h"
30
0ca6d097 31#include <getopt.h>
6d2d327c
RK
32#include <locale.h>
33#include <errno.h>
34#include <unistd.h>
35#include <syslog.h>
36#include <string.h>
37#include <sys/types.h>
38#include <sys/wait.h>
39
40#include "syscalls.h"
41#include "log.h"
42#include "configuration.h"
43#include "speaker-protocol.h"
0ca6d097
RK
44#include "defs.h"
45
46static const struct option options[] = {
47 { "help", no_argument, 0, 'h' },
48 { "version", no_argument, 0, 'V' },
49 { "config", required_argument, 0, 'c' },
50 { "debug", no_argument, 0, 'd' },
51 { "no-debug", no_argument, 0, 'D' },
52 { "syslog", no_argument, 0, 's' },
53 { "no-syslog", no_argument, 0, 'S' },
54 { 0, 0, 0, 0 }
55};
56
57/* display usage message and terminate */
58static void help(void) {
59 xprintf("Usage:\n"
60 " disorder-normalize [OPTIONS]\n"
61 "Options:\n"
62 " --help, -h Display usage message\n"
63 " --version, -V Display version number\n"
64 " --config PATH, -c PATH Set configuration file\n"
65 " --debug, -d Turn on debugging\n"
66 " --[no-]syslog Force logging\n"
67 "\n"
68 "Audio format normalizer for DisOrder. Not intended to be run\n"
69 "directly.\n");
70 xfclose(stdout);
71 exit(0);
72}
73
74/* display version number and terminate */
75static void version(void) {
a05e4467 76 xprintf("%s", disorder_version_string);
0ca6d097
RK
77 xfclose(stdout);
78 exit(0);
79}
6d2d327c
RK
80
81/** @brief Copy bytes from one file descriptor to another
82 * @param infd File descriptor read from
83 * @param outfd File descriptor to write to
84 * @param n Number of bytes to copy
85 */
86static void copy(int infd, int outfd, size_t n) {
62234e45 87 char buffer[4096];
88 ssize_t written;
6d2d327c
RK
89
90 while(n > 0) {
62234e45 91 const ssize_t readden = read(infd, buffer,
92 n > sizeof buffer ? sizeof buffer : n);
93 if(readden < 0) {
6d2d327c
RK
94 if(errno == EINTR)
95 continue;
96 else
97 fatal(errno, "read error");
98 }
62234e45 99 if(readden == 0)
6d2d327c 100 fatal(0, "unexpected EOF");
62234e45 101 n -= readden;
102 written = 0;
103 while(written < readden) {
104 const ssize_t w = write(outfd, buffer + written, readden - written);
6d2d327c
RK
105 if(w < 0)
106 fatal(errno, "write error");
62234e45 107 written += w;
6d2d327c
RK
108 }
109 }
110}
111
112static void soxargs(const char ***pp, char **qq,
113 const struct stream_header *header) {
114 *(*pp)++ = "-t.raw";
115 *(*pp)++ = "-s";
116 *qq += sprintf((char *)(*(*pp)++ = *qq), "-r%d", header->rate) + 1;
117 *qq += sprintf((char *)(*(*pp)++ = *qq), "-c%d", header->channels) + 1;
118 /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are
119 * deployed! */
120 switch(config->sox_generation) {
121 case 0:
122 if(header->bits != 8
123 && header->endian != ENDIAN_NATIVE)
124 *(*pp)++ = "-x";
125 switch(header->bits) {
126 case 8: *(*pp)++ = "-b"; break;
127 case 16: *(*pp)++ = "-w"; break;
128 case 32: *(*pp)++ = "-l"; break;
129 case 64: *(*pp)++ = "-d"; break;
130 default: fatal(0, "cannot handle sample size %d", header->bits);
131 }
132 break;
133 case 1:
134 if(header->bits != 8
135 && header->endian != ENDIAN_NATIVE)
136 switch(header->endian) {
137 case ENDIAN_BIG: *(*pp)++ = "-B"; break;
138 case ENDIAN_LITTLE: *(*pp)++ = "-L"; break;
139 }
140 if(header->bits % 8)
141 fatal(0, "cannot handle sample size %d", header->bits);
142 *qq += sprintf((char *)(*(*pp)++ = *qq), "-%d", header->bits / 8) + 1;
143 break;
144 default:
145 fatal(0, "unknown sox_generation %ld", config->sox_generation);
146 }
147}
148
149int main(int argc, char attribute((unused)) **argv) {
150 struct stream_header header, latest_format;
0ca6d097 151 int n, p[2], outfd = -1, logsyslog = !isatty(2);
6d2d327c
RK
152 pid_t pid = -1;
153
154 set_progname(argv);
155 if(!setlocale(LC_CTYPE, ""))
156 fatal(errno, "error calling setlocale");
0ca6d097
RK
157 while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
158 switch(n) {
159 case 'h': help();
160 case 'V': version();
161 case 'c': configfile = optarg; break;
162 case 'd': debugging = 1; break;
163 case 'D': debugging = 0; break;
164 case 'S': logsyslog = 0; break;
165 case 's': logsyslog = 1; break;
166 default: fatal(0, "invalid option");
167 }
168 }
c00fce3a 169 if(config_read(1))
6d2d327c 170 fatal(0, "cannot read configuration");
0ca6d097 171 if(logsyslog) {
6d2d327c
RK
172 openlog(progname, LOG_PID, LOG_DAEMON);
173 log_default = &log_syslog;
174 }
175 memset(&latest_format, 0, sizeof latest_format);
176 for(;;) {
e7fd1612
RK
177 n = 0;
178 while((size_t)n < sizeof header) {
179 int r = read(0, (char *)&header + n, sizeof header - n);
180
181 if(r < 0) {
182 if(errno != EINTR)
183 fatal(errno, "error reading header");
e99d42b1 184 } else if(r == 0) {
185 if(n)
186 fatal(0, "EOF reading header");
187 break;
188 } else
e7fd1612
RK
189 n += r;
190 }
24fc5e58 191 if(!n)
192 break;
6d2d327c
RK
193 /* Sanity check the header */
194 if(header.rate < 100 || header.rate > 1000000)
195 fatal(0, "implausible rate %"PRId32"Hz (%#"PRIx32")",
196 header.rate, header.rate);
197 if(header.channels < 1 || header.channels > 2)
198 fatal(0, "unsupported channel count %d", header.channels);
199 if(header.bits % 8 || !header.bits || header.bits > 64)
200 fatal(0, "unsupported sample size %d bits", header.bits);
201 if(header.endian != ENDIAN_BIG && header.endian != ENDIAN_LITTLE)
202 fatal(0, "unsupported byte order %x", header.bits);
203 /* Skip empty chunks regardless of their alleged format */
204 if(header.nbytes == 0)
205 continue;
206 /* If the format has changed we stop/start the converter */
207 if(!formats_equal(&header, &latest_format)) {
208 if(pid != -1) {
209 /* There's a running converter, stop it */
210 xclose(outfd);
211 if(waitpid(pid, &n, 0) < 0)
212 fatal(errno, "error calling waitpid");
213 if(n)
214 fatal(0, "sox failed: %#x", n);
215 pid = -1;
216 outfd = -1;
217 }
218 if(!formats_equal(&header, &config->sample_format)) {
219 const char *av[32], **pp = av;
220 char argbuf[1024], *q = argbuf;
221
222 /* Input format doesn't match target, need to start a converter */
223 *pp++ = "sox";
224 soxargs(&pp, &q, &header);
225 *pp++ = "-"; /* stdin */
226 soxargs(&pp, &q, &config->sample_format);
227 *pp++ = "-"; /* stdout */
228 *pp = 0;
229 /* This pipe will be sox's stdin */
230 xpipe(p);
231 if(!(pid = xfork())) {
232 exitfn = _exit;
233 xdup2(p[0], 0);
234 xclose(p[0]);
235 xclose(p[1]);
236 execvp(av[0], (char **)av);
237 fatal(errno, "sox");
238 }
239 xclose(p[0]);
240 outfd = p[1];
241 } else
242 /* Input format matches output, can just copy bytes */
243 outfd = 1;
244 /* Remember current format for next iteration */
245 latest_format = header;
246 }
247 /* Convert or copy this chunk */
248 copy(0, outfd, header.nbytes);
249 }
250 if(outfd != -1)
251 xclose(outfd);
252 return 0;
253}
254
255/*
256Local Variables:
257c-basic-offset:2
258comment-column:40
259fill-column:79
260indent-tabs-mode:nil
261End:
262*/