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