1 | //roarphone.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
5 | * |
---|
6 | * This file is part of roarclients a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include <roaraudio.h> |
---|
27 | #include <libroardsp/libroardsp.h> |
---|
28 | #include <libroareio/libroareio.h> |
---|
29 | |
---|
30 | #if defined(ROAR_HAVE_LIBSPEEX) && !defined(ROAR_HAVE_LIBSPEEXDSP) |
---|
31 | #define _SPEEX_API_OLD |
---|
32 | #elif defined(ROAR_HAVE_LIBSPEEX) && defined(ROAR_HAVE_LIBSPEEXDSP) |
---|
33 | #define _SPEEX_API_NEW |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifdef _SPEEX_API_OLD |
---|
37 | #include <speex/speex_echo.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #define TIMEDIV 100 |
---|
41 | |
---|
42 | #define DRIVER "oss" |
---|
43 | |
---|
44 | // anti echo: |
---|
45 | #define AE_NONE 0 |
---|
46 | #define AE_SIMPLE 1 |
---|
47 | #define AE_SPEEX 2 |
---|
48 | #define AE_ROARD 3 |
---|
49 | |
---|
50 | #define DTX_F 25 |
---|
51 | |
---|
52 | #define CON_NONE 0x00 |
---|
53 | #define CON_CON 0x01 |
---|
54 | #define CON_STREAM 0x02 |
---|
55 | |
---|
56 | struct { |
---|
57 | int antiecho; |
---|
58 | int samples; |
---|
59 | int transcode; |
---|
60 | int64_t dtx_threshold; |
---|
61 | size_t jumbo_mtu; |
---|
62 | int ioflush_interval; |
---|
63 | struct { |
---|
64 | struct { |
---|
65 | int downmix; |
---|
66 | float lowp_freq; |
---|
67 | int speex_prep; |
---|
68 | int speex_prep_denoise; |
---|
69 | int speex_prep_agc; |
---|
70 | int speex_prep_vad; |
---|
71 | } in; |
---|
72 | } filter; |
---|
73 | } g_conf; |
---|
74 | |
---|
75 | int dtx_counter = 0; |
---|
76 | |
---|
77 | struct { |
---|
78 | int state; |
---|
79 | struct roar_connection con; |
---|
80 | struct roar_stream stream; |
---|
81 | struct roar_vio_calls * svio; |
---|
82 | } g_cons; |
---|
83 | |
---|
84 | struct roar_bixcoder transcoder[1]; |
---|
85 | |
---|
86 | struct { |
---|
87 | struct roardsp_filterchain input; |
---|
88 | struct roardsp_filterchain output; |
---|
89 | } g_filterchains; |
---|
90 | |
---|
91 | struct { |
---|
92 | struct { |
---|
93 | char key[ROAR_META_MAX_NAMELEN]; |
---|
94 | char value[LIBROAR_BUFFER_MSGDATA]; |
---|
95 | } tmp; |
---|
96 | char * rn; |
---|
97 | char * nick; |
---|
98 | char * org; |
---|
99 | char * email; |
---|
100 | char * hp; |
---|
101 | char * loc; |
---|
102 | char * thumbnail; |
---|
103 | } g_meta; |
---|
104 | |
---|
105 | void usage (void) { |
---|
106 | printf("roarphone [OPTIONS]...\n"); |
---|
107 | |
---|
108 | printf("\nServer Options:\n\n"); |
---|
109 | |
---|
110 | printf(" --server SERVER - Set server hostname\n" |
---|
111 | " --jumbo-mtu MTU - Sets the MTU for Jumbo Packets\n" |
---|
112 | " --io-flush INTERVAL - Flushs output every INTERVAL packets\n" |
---|
113 | ); |
---|
114 | |
---|
115 | printf("\nAudio Options:\n\n"); |
---|
116 | printf(" --rate RATE - Set sample rate\n" |
---|
117 | " --bits BITS - Set bits per sample\n" |
---|
118 | " --chans CHANNELS - Set number of channels\n" |
---|
119 | ); |
---|
120 | |
---|
121 | printf("\nAudio Filter Options:\n\n"); |
---|
122 | printf(" --afi-downmix - Enable input downmixing\n" |
---|
123 | " --afi-lowpass FREQ - Enable input lowpass at FREQ (in Hz)\n" |
---|
124 | " --afi-speex-prep - Enable speex preprocessor\n" |
---|
125 | " --afi-speex-denoise - Enable speex denoiser\n" |
---|
126 | " --afi-speex-agc - Enable speex AGC\n" |
---|
127 | " --afi-speex-vad - Enable speex VAD\n" |
---|
128 | ); |
---|
129 | |
---|
130 | printf("\nCodec Options:\n\n"); |
---|
131 | printf(" --codec CODEC - Set the codec\n" |
---|
132 | " --transcode - Use local transcodeing\n" |
---|
133 | ); |
---|
134 | |
---|
135 | printf("\nDriver Options:\n\n"); |
---|
136 | printf(" --driver DRIVER - Set the driver\n" |
---|
137 | " --device DEVICE - Set the device\n" |
---|
138 | ); |
---|
139 | |
---|
140 | printf("\nGeneral Options:\n\n"); |
---|
141 | printf(" --antiecho AEMODE - Set the anti echo mode\n" |
---|
142 | " --threshold DTXTHRES - Set the DTX threshold, disabled by default\n" |
---|
143 | " --help - Show this help\n" |
---|
144 | ); |
---|
145 | |
---|
146 | printf("\nMeta Data Options:\n\n"); |
---|
147 | printf(" --m-rn REALNAME - Sets the real name\n" |
---|
148 | " --m-nick NICK - Sets the nick name\n" |
---|
149 | " --m-email EMAIL - Sets the email address\n" |
---|
150 | " --m-hp HOMEPAGE - Sets the homepage URL\n" |
---|
151 | " --m-thumbn THUMBNAIL - Sets a URL to a thumbnail\n" |
---|
152 | " --m-loc LOCATION - Sets the location (room number)\n" |
---|
153 | " --m-org ORGANIZATION - Sets the organization/company name\n" |
---|
154 | ); |
---|
155 | } |
---|
156 | |
---|
157 | int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) { |
---|
158 | int fh; |
---|
159 | |
---|
160 | g_cons.svio = vio; |
---|
161 | |
---|
162 | if ( !(g_cons.state & CON_CON) ) |
---|
163 | if ( roar_simple_connect(&(g_cons.con), server, "roarphone") == -1 ) |
---|
164 | return -1; |
---|
165 | |
---|
166 | g_cons.state |= CON_CON; |
---|
167 | |
---|
168 | if ( (fh = roar_vio_simple_new_stream_obj(vio, &(g_cons.con), &(g_cons.stream), |
---|
169 | info->rate, info->channels, info->bits, info->codec, |
---|
170 | ROAR_DIR_BIDIR |
---|
171 | )) == -1 ) |
---|
172 | return -1; |
---|
173 | |
---|
174 | g_cons.state |= CON_STREAM; |
---|
175 | |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | #define _SET_META(ivar,itype) if ( (ivar) != NULL ) { \ |
---|
180 | meta.value = (ivar); \ |
---|
181 | meta.type = (itype); \ |
---|
182 | roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_SET, &meta); \ |
---|
183 | } |
---|
184 | int set_meta (void) { |
---|
185 | struct roar_meta meta; |
---|
186 | |
---|
187 | meta.value = g_meta.tmp.value; |
---|
188 | meta.key[0] = 0; |
---|
189 | meta.type = ROAR_META_TYPE_NONE; |
---|
190 | |
---|
191 | roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_CLEAR, &meta); |
---|
192 | |
---|
193 | _SET_META(g_meta.thumbnail, ROAR_META_TYPE_THUMBNAIL); |
---|
194 | _SET_META(g_meta.loc, ROAR_META_TYPE_LOCATION); |
---|
195 | _SET_META(g_meta.hp, ROAR_META_TYPE_HOMEPAGE); |
---|
196 | _SET_META(g_meta.org, ROAR_META_TYPE_ORGANIZATION); |
---|
197 | |
---|
198 | if ( g_meta.nick != NULL ) { |
---|
199 | if ( g_meta.rn != NULL ) { |
---|
200 | snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s)", g_meta.rn, g_meta.nick); |
---|
201 | g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
202 | _SET_META(g_meta.tmp.value, ROAR_META_TYPE_AUTHOR); |
---|
203 | } else { |
---|
204 | _SET_META(g_meta.nick, ROAR_META_TYPE_AUTHOR); |
---|
205 | } |
---|
206 | } else { |
---|
207 | if ( g_meta.rn != NULL ) { |
---|
208 | _SET_META(g_meta.rn, ROAR_META_TYPE_AUTHOR); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | // TODO: make this more nice... |
---|
213 | if ( g_meta.email != NULL ) { |
---|
214 | if ( g_meta.nick != NULL ) { |
---|
215 | if ( g_meta.rn != NULL ) { |
---|
216 | snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s) <%s>", g_meta.rn, g_meta.nick, g_meta.email); |
---|
217 | } else { |
---|
218 | snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.nick, g_meta.email); |
---|
219 | } |
---|
220 | } else { |
---|
221 | if ( g_meta.rn != NULL ) { |
---|
222 | snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.rn, g_meta.email); |
---|
223 | } else { |
---|
224 | snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "<%s>", g_meta.email); |
---|
225 | } |
---|
226 | } |
---|
227 | g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0; |
---|
228 | _SET_META(g_meta.tmp.value, ROAR_META_TYPE_CONTACT); |
---|
229 | } |
---|
230 | |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | #ifdef _SPEEX_API_OLD |
---|
235 | int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) { |
---|
236 | static SpeexEchoState * state = NULL; |
---|
237 | size_t samples = info->rate / TIMEDIV; |
---|
238 | static int16_t * obuf = NULL; |
---|
239 | |
---|
240 | if ( info->channels != 1 ) |
---|
241 | return -1; |
---|
242 | |
---|
243 | if (len != samples) |
---|
244 | return -1; |
---|
245 | |
---|
246 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
247 | |
---|
248 | if ( state == NULL ) { |
---|
249 | if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL ) |
---|
250 | return -1; |
---|
251 | |
---|
252 | // todo: set sample rate. |
---|
253 | } |
---|
254 | |
---|
255 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
256 | |
---|
257 | if ( obuf == NULL ) { |
---|
258 | if ( (obuf = malloc(2*samples)) == NULL ) |
---|
259 | return -1; |
---|
260 | } |
---|
261 | |
---|
262 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
263 | |
---|
264 | /* |
---|
265 | speex_echo_cancellation(state, buf, aebuf, obuf); |
---|
266 | */ |
---|
267 | |
---|
268 | speex_echo_cancel(state, buf, aebuf, obuf, NULL); |
---|
269 | |
---|
270 | memcpy(buf, obuf, 2*samples); |
---|
271 | |
---|
272 | ROAR_DBG("anti_echo_speex16(*) = 0"); |
---|
273 | |
---|
274 | return 0; |
---|
275 | } |
---|
276 | #endif |
---|
277 | |
---|
278 | int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) { |
---|
279 | size_t i; |
---|
280 | |
---|
281 | (void)info; |
---|
282 | |
---|
283 | switch (g_conf.antiecho) { |
---|
284 | case AE_NONE: |
---|
285 | return 0; |
---|
286 | break; |
---|
287 | case AE_SIMPLE: |
---|
288 | for (i = 0; i < len; i++) |
---|
289 | buf[i] -= aebuf[i]; |
---|
290 | return 0; |
---|
291 | break; |
---|
292 | #ifdef _SPEEX_API_OLD |
---|
293 | case AE_SPEEX: |
---|
294 | return anti_echo_speex16(buf, aebuf, len, info); |
---|
295 | break; |
---|
296 | #endif |
---|
297 | case AE_ROARD: |
---|
298 | return 0; |
---|
299 | break; |
---|
300 | default: |
---|
301 | return -1; |
---|
302 | break; |
---|
303 | } |
---|
304 | |
---|
305 | return -1; |
---|
306 | } |
---|
307 | |
---|
308 | int zero_if_noise16 (int16_t * data, size_t samples) { |
---|
309 | int64_t rms = roar_rms2_1_16(data, samples); |
---|
310 | |
---|
311 | if ( rms < g_conf.dtx_threshold ) { |
---|
312 | if ( dtx_counter ) { |
---|
313 | dtx_counter--; |
---|
314 | } else { |
---|
315 | memset(data, 0, samples*2); |
---|
316 | } |
---|
317 | } else { |
---|
318 | dtx_counter = DTX_F; |
---|
319 | } |
---|
320 | |
---|
321 | return 0; |
---|
322 | } |
---|
323 | |
---|
324 | int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) { |
---|
325 | size_t len; |
---|
326 | void * outbuf, * micbuf; |
---|
327 | ssize_t outlen, miclen; |
---|
328 | unsigned long int pkg_count = 0; |
---|
329 | |
---|
330 | ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits); |
---|
331 | len = g_conf.samples * info->bits / 8; |
---|
332 | ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len); |
---|
333 | |
---|
334 | if ( (outbuf = malloc(2*len)) == NULL ) |
---|
335 | return -1; |
---|
336 | |
---|
337 | micbuf = outbuf + len; |
---|
338 | |
---|
339 | while (1) { |
---|
340 | if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 ) |
---|
341 | break; |
---|
342 | |
---|
343 | if ( roardsp_fchain_num(&(g_filterchains.input)) ) { |
---|
344 | if ( roardsp_fchain_calc(&(g_filterchains.input), micbuf, len) == -1 ) |
---|
345 | break; |
---|
346 | } |
---|
347 | |
---|
348 | if ( g_conf.dtx_threshold > 0 ) |
---|
349 | if ( info->bits == 16 ) |
---|
350 | zero_if_noise16(micbuf, miclen/2); |
---|
351 | |
---|
352 | if ( g_conf.transcode ) { |
---|
353 | if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 ) |
---|
354 | break; |
---|
355 | } else { |
---|
356 | if ( roar_vio_write(s1, micbuf, miclen) != miclen ) |
---|
357 | break; |
---|
358 | } |
---|
359 | |
---|
360 | if ( g_conf.ioflush_interval != -1 ) { |
---|
361 | if ( !(pkg_count % g_conf.ioflush_interval) ) |
---|
362 | roar_vio_sync(s1); |
---|
363 | } |
---|
364 | |
---|
365 | if ( g_conf.transcode ) { |
---|
366 | ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len); |
---|
367 | if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 ) |
---|
368 | break; |
---|
369 | |
---|
370 | outlen = len; |
---|
371 | } else { |
---|
372 | if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 ) |
---|
373 | break; |
---|
374 | } |
---|
375 | |
---|
376 | if ( g_conf.antiecho != AE_NONE && info->bits == 16 ) |
---|
377 | anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info); |
---|
378 | |
---|
379 | if ( roardsp_fchain_num(&(g_filterchains.output)) ) { |
---|
380 | if ( roardsp_fchain_calc(&(g_filterchains.output), outbuf, outlen) == -1 ) |
---|
381 | break; |
---|
382 | } |
---|
383 | |
---|
384 | if ( roar_vio_write(s0, outbuf, outlen) != outlen ) |
---|
385 | break; |
---|
386 | |
---|
387 | pkg_count++; |
---|
388 | } |
---|
389 | |
---|
390 | free(outbuf); |
---|
391 | |
---|
392 | return 0; |
---|
393 | } |
---|
394 | |
---|
395 | int main (int argc, char * argv[]) { |
---|
396 | struct roar_audio_info info = {.rate = ROAR_RATE_DEFAULT, |
---|
397 | .bits = ROAR_BITS_DEFAULT, |
---|
398 | .channels = ROAR_CHANNELS_DEFAULT, |
---|
399 | .codec = ROAR_CODEC_DEFAULT |
---|
400 | }; |
---|
401 | struct roar_audio_info dinfo; |
---|
402 | struct roar_vio_calls dvio, svio, svio_jumbo, svio_real; |
---|
403 | struct roar_vio_calls * svio_p; |
---|
404 | struct roardsp_filter * filter; |
---|
405 | char * driver = DRIVER; |
---|
406 | char * device = NULL; |
---|
407 | char * server = NULL; |
---|
408 | char * k; |
---|
409 | int i; |
---|
410 | union { |
---|
411 | int32_t i32; |
---|
412 | size_t size; |
---|
413 | } tmp; |
---|
414 | |
---|
415 | memset(&g_conf, 0, sizeof(g_conf)); |
---|
416 | |
---|
417 | g_conf.antiecho = AE_ROARD; |
---|
418 | g_conf.dtx_threshold = -1; |
---|
419 | g_conf.ioflush_interval = -1; |
---|
420 | |
---|
421 | memset(&g_cons, 0, sizeof(g_cons)); |
---|
422 | g_cons.state = CON_NONE; |
---|
423 | |
---|
424 | memset(&g_meta, 0, sizeof(g_meta)); |
---|
425 | |
---|
426 | roardsp_fchain_init(&(g_filterchains.input)); |
---|
427 | roardsp_fchain_init(&(g_filterchains.output)); |
---|
428 | |
---|
429 | for (i = 1; i < argc; i++) { |
---|
430 | k = argv[i]; |
---|
431 | |
---|
432 | if ( strcmp(k, "--server") == 0 ) { |
---|
433 | server = argv[++i]; |
---|
434 | } else if ( strcmp(k, "--jumbo-mtu") == 0 ) { |
---|
435 | g_conf.jumbo_mtu = atoi(argv[++i]); |
---|
436 | } else if ( strcmp(k, "--io-flush") == 0 ) { |
---|
437 | g_conf.ioflush_interval = atoi(argv[++i]); |
---|
438 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
439 | info.rate = atoi(argv[++i]); |
---|
440 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
441 | info.bits = atoi(argv[++i]); |
---|
442 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
443 | info.channels = atoi(argv[++i]); |
---|
444 | |
---|
445 | } else if ( strcmp(k, "--afi-downmix") == 0 ) { |
---|
446 | g_conf.filter.in.downmix = 1; |
---|
447 | } else if ( strcmp(k, "--afi-lowpass") == 0 ) { |
---|
448 | g_conf.filter.in.lowp_freq = atof(argv[++i]); |
---|
449 | } else if ( strcmp(k, "--afi-speex-prep") == 0 ) { |
---|
450 | g_conf.filter.in.speex_prep = 1; |
---|
451 | } else if ( strcmp(k, "--afi-speex-denoise") == 0 ) { |
---|
452 | g_conf.filter.in.speex_prep = 1; |
---|
453 | g_conf.filter.in.speex_prep_denoise = 1; |
---|
454 | } else if ( strcmp(k, "--afi-speex-agc") == 0 ) { |
---|
455 | g_conf.filter.in.speex_prep = 1; |
---|
456 | g_conf.filter.in.speex_prep_agc = 1; |
---|
457 | } else if ( strcmp(k, "--afi-speex-vad") == 0 ) { |
---|
458 | g_conf.filter.in.speex_prep = 1; |
---|
459 | g_conf.filter.in.speex_prep_vad = 1; |
---|
460 | |
---|
461 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
462 | info.codec = roar_str2codec(argv[++i]); |
---|
463 | |
---|
464 | } else if ( strcmp(k, "--driver") == 0 ) { |
---|
465 | driver = argv[++i]; |
---|
466 | } else if ( strcmp(k, "--device") == 0 ) { |
---|
467 | device = argv[++i]; |
---|
468 | } else if ( strcmp(k, "--antiecho") == 0 ) { |
---|
469 | k = argv[++i]; |
---|
470 | if ( !strcmp(k, "none") ) { |
---|
471 | g_conf.antiecho = AE_NONE; |
---|
472 | } else if ( !strcmp(k, "simple") ) { |
---|
473 | g_conf.antiecho = AE_SIMPLE; |
---|
474 | } else if ( !strcmp(k, "speex") ) { |
---|
475 | g_conf.antiecho = AE_SPEEX; |
---|
476 | } else if ( !strcmp(k, "roard") ) { |
---|
477 | g_conf.antiecho = AE_ROARD; |
---|
478 | } else { |
---|
479 | fprintf(stderr, "Error: unknown mode: %s\n", k); |
---|
480 | return 1; |
---|
481 | } |
---|
482 | } else if ( strcmp(k, "--threshold") == 0 ) { |
---|
483 | g_conf.dtx_threshold = atol(argv[++i]); |
---|
484 | |
---|
485 | // use threshold^2 or threshold < 0 for not using DTX |
---|
486 | if ( g_conf.dtx_threshold > 0 ) |
---|
487 | g_conf.dtx_threshold *= g_conf.dtx_threshold; |
---|
488 | } else if ( strcmp(k, "--transcode") == 0 ) { |
---|
489 | g_conf.transcode = 1; |
---|
490 | |
---|
491 | // META DATA: |
---|
492 | } else if ( strcmp(k, "--m-rn") == 0 ) { |
---|
493 | g_meta.rn = argv[++i]; |
---|
494 | } else if ( strcmp(k, "--m-nick") == 0 ) { |
---|
495 | g_meta.nick = argv[++i]; |
---|
496 | } else if ( strcmp(k, "--m-email") == 0 ) { |
---|
497 | g_meta.email = argv[++i]; |
---|
498 | } else if ( strcmp(k, "--m-hp") == 0 ) { |
---|
499 | g_meta.hp = argv[++i]; |
---|
500 | } else if ( strcmp(k, "--m-thumbn") == 0 ) { |
---|
501 | g_meta.thumbnail = argv[++i]; |
---|
502 | } else if ( strcmp(k, "--m-loc") == 0 ) { |
---|
503 | g_meta.loc = argv[++i]; |
---|
504 | } else if ( strcmp(k, "--m-org") == 0 ) { |
---|
505 | g_meta.org = argv[++i]; |
---|
506 | |
---|
507 | |
---|
508 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
509 | usage(); |
---|
510 | return 0; |
---|
511 | } else { |
---|
512 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
513 | usage(); |
---|
514 | return 1; |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | // ignore errors, maybe it will work even if this fails |
---|
519 | // (btw. it will never fail without crashing the rest of the app ;) |
---|
520 | roar_libroar_set_server(server); |
---|
521 | |
---|
522 | if ( g_conf.antiecho == AE_SPEEX ) { |
---|
523 | ROAR_WARN("Speex Antiecho is obsolete and may be removed in future versions. Use --antiecho roard"); |
---|
524 | } |
---|
525 | |
---|
526 | g_conf.samples = info.channels * info.rate / TIMEDIV; |
---|
527 | |
---|
528 | memcpy(&dinfo, &info, sizeof(dinfo)); |
---|
529 | |
---|
530 | if ( g_conf.transcode ) { |
---|
531 | dinfo.bits = 16; |
---|
532 | dinfo.codec = ROAR_CODEC_DEFAULT; |
---|
533 | |
---|
534 | switch (info.codec) { |
---|
535 | case ROAR_CODEC_ALAW: |
---|
536 | case ROAR_CODEC_MULAW: |
---|
537 | info.bits = 8; |
---|
538 | break; |
---|
539 | case ROAR_CODEC_ROAR_CELT: |
---|
540 | info.bits = 16; |
---|
541 | break; |
---|
542 | case ROAR_CODEC_ROAR_SPEEX: |
---|
543 | info.bits = 16; |
---|
544 | break; |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | if ( roar_cdriver_open(&dvio, driver, device, &dinfo, ROAR_DIR_BIDIR) == -1 ) { |
---|
549 | ROAR_ERR("Can not open sound card."); |
---|
550 | return 1; |
---|
551 | } |
---|
552 | |
---|
553 | ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)"); |
---|
554 | if ( open_stream(&svio_real, server, &info) == -1 ) { |
---|
555 | ROAR_ERR("Can not open connection to server."); |
---|
556 | roar_vio_close(&dvio); |
---|
557 | return 2; |
---|
558 | } |
---|
559 | ROAR_DBG("main(*): RET"); |
---|
560 | |
---|
561 | if ( roar_vio_open_re(&svio, &svio_real) == -1 ) { |
---|
562 | ROAR_ERR("Can not open connection to server (RE VIO)."); |
---|
563 | roar_vio_close(&dvio); |
---|
564 | return 2; |
---|
565 | } |
---|
566 | |
---|
567 | if ( g_conf.jumbo_mtu ) { |
---|
568 | if ( roar_vio_open_jumbo(&svio_jumbo, &svio, g_conf.jumbo_mtu) == -1 ) { |
---|
569 | roar_vio_close(&dvio); |
---|
570 | roar_vio_close(&svio); |
---|
571 | return 2; |
---|
572 | } |
---|
573 | svio_p = &svio_jumbo; |
---|
574 | } else { |
---|
575 | svio_p = &svio; |
---|
576 | } |
---|
577 | |
---|
578 | set_meta(); |
---|
579 | |
---|
580 | if ( g_conf.transcode ) { |
---|
581 | dinfo.codec = info.codec; |
---|
582 | |
---|
583 | if ( roar_bixcoder_init(transcoder, &dinfo, svio_p) == -1 ) { |
---|
584 | roar_vio_close(&svio); |
---|
585 | roar_vio_close(&dvio); |
---|
586 | return 10; |
---|
587 | } |
---|
588 | |
---|
589 | // ignore errors as it may also work if this fails |
---|
590 | roar_bixcoder_write_header(transcoder); |
---|
591 | roar_bixcoder_read_header(transcoder); |
---|
592 | |
---|
593 | g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits; |
---|
594 | } |
---|
595 | |
---|
596 | #define _err(x) roar_vio_close(&dvio); roar_vio_close(&svio); return (x) |
---|
597 | |
---|
598 | if ( g_conf.filter.in.downmix ) { |
---|
599 | if ( roardsp_filter_new(&filter, &(g_cons.stream), ROARDSP_FILTER_DOWNMIX) == -1 ) { |
---|
600 | _err(2); |
---|
601 | } |
---|
602 | |
---|
603 | if ( roardsp_fchain_add(&(g_filterchains.input), filter) == -1 ) { |
---|
604 | _err(2); |
---|
605 | } |
---|
606 | } |
---|
607 | |
---|
608 | if ( g_conf.filter.in.lowp_freq > 1 ) { |
---|
609 | if ( roardsp_filter_new(&filter, &(g_cons.stream), ROARDSP_FILTER_LOWP) == -1 ) { |
---|
610 | _err(2); |
---|
611 | } |
---|
612 | |
---|
613 | if ( roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &(g_conf.filter.in.lowp_freq)) == -1 ) { |
---|
614 | _err(2); |
---|
615 | } |
---|
616 | |
---|
617 | if ( roardsp_fchain_add(&(g_filterchains.input), filter) == -1 ) { |
---|
618 | _err(2); |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | if ( g_conf.filter.in.speex_prep ) { |
---|
623 | if ( roardsp_filter_new(&filter, &(g_cons.stream), ROARDSP_FILTER_SPEEX_PREP) == -1 ) { |
---|
624 | _err(2); |
---|
625 | } |
---|
626 | |
---|
627 | tmp.size = g_conf.samples; |
---|
628 | if ( roardsp_filter_ctl(filter, ROARDSP_FCTL_PACKET_SIZE, &tmp) == -1 ) { |
---|
629 | _err(2); |
---|
630 | } |
---|
631 | |
---|
632 | tmp.i32 = 0; |
---|
633 | |
---|
634 | if ( g_conf.filter.in.speex_prep_denoise ) |
---|
635 | tmp.i32 |= ROARDSP_SPEEX_PREP_DENOISE_ON; |
---|
636 | |
---|
637 | if ( g_conf.filter.in.speex_prep_agc ) |
---|
638 | tmp.i32 |= ROARDSP_SPEEX_PREP_AGC_ON; |
---|
639 | |
---|
640 | if ( g_conf.filter.in.speex_prep_vad ) |
---|
641 | tmp.i32 |= ROARDSP_SPEEX_PREP_VAD_ON; |
---|
642 | |
---|
643 | if ( roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp) == -1 ) { |
---|
644 | _err(2); |
---|
645 | } |
---|
646 | |
---|
647 | if ( roardsp_fchain_add(&(g_filterchains.input), filter) == -1 ) { |
---|
648 | _err(2); |
---|
649 | } |
---|
650 | } |
---|
651 | |
---|
652 | #undef _err |
---|
653 | |
---|
654 | ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);"); |
---|
655 | run_stream(&dvio, svio_p, &info); |
---|
656 | ROAR_DBG("main(*): RET"); |
---|
657 | |
---|
658 | roar_bixcoder_close(transcoder); |
---|
659 | |
---|
660 | roar_vio_close(svio_p); |
---|
661 | roar_vio_close(&dvio); |
---|
662 | |
---|
663 | roardsp_fchain_uninit(&(g_filterchains.input)); |
---|
664 | roardsp_fchain_uninit(&(g_filterchains.output)); |
---|
665 | |
---|
666 | roar_disconnect(&(g_cons.con)); |
---|
667 | |
---|
668 | return 0; |
---|
669 | } |
---|
670 | |
---|
671 | //ll |
---|