source: roaraudio/roarclients/roarphone.c @ 3006:d0c79207f9ac

Last change on this file since 3006:d0c79207f9ac was 3006:d0c79207f9ac, checked in by phi, 15 years ago

added support for speex prep, just need to add support to set options of the prep (MODE parameter)

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