source: roaraudio/roarclients/roarphone.c @ 2993:daefb5b9bc41

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

added --io-flush

File size: 15.9 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  } in;
67 } filter;
68} g_conf;
69
70int dtx_counter = 0;
71
72struct {
73 int state;
74 struct roar_connection con;
75 struct roar_stream     stream;
76 struct roar_vio_calls * svio;
77} g_cons;
78
79struct roar_bixcoder transcoder[1];
80
81struct {
82 struct roardsp_filterchain input;
83 struct roardsp_filterchain output;
84} g_filterchains;
85
86struct {
87 struct {
88  char key[ROAR_META_MAX_NAMELEN];
89  char value[LIBROAR_BUFFER_MSGDATA];
90 } tmp;
91 char * rn;
92 char * nick;
93 char * org;
94 char * email;
95 char * hp;
96 char * loc;
97 char * thumbnail;
98} g_meta;
99
100void usage (void) {
101 printf("roarphone [OPTIONS]...\n");
102
103 printf("\nServer Options:\n\n");
104
105 printf("  --server   SERVER    - Set server hostname\n"
106        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
107        " --io-flush  INTERVAL  - Flushs output every INTERVAL packets\n"
108       );
109
110 printf("\nAudio Options:\n\n");
111 printf("  --rate     RATE      - Set sample rate\n"
112        "  --bits     BITS      - Set bits per sample\n"
113        "  --chans    CHANNELS  - Set number of channels\n"
114       );
115
116 printf("\nAudio Filter Options:\n\n");
117 printf("  --afi-downmix        - Enable input downmixing\n"
118        "  --afi-lowpass FREQ   - Enable input lowpass at FREQ (in Hz)\n"
119       );
120
121 printf("\nCodec Options:\n\n");
122 printf("  --codec    CODEC     - Set the codec\n"
123        "  --transcode          - Use local transcodeing\n"
124       );
125
126 printf("\nDriver Options:\n\n");
127 printf("  --driver   DRIVER    - Set the driver\n"
128        "  --device   DEVICE    - Set the device\n"
129       );
130
131 printf("\nGeneral Options:\n\n");
132 printf("  --antiecho AEMODE    - Set the anti echo mode\n"
133        "  --threshold DTXTHRES - Set the DTX threshold, disabled by default\n"
134        "  --help               - Show this help\n"
135       );
136
137 printf("\nMeta Data Options:\n\n");
138 printf("  --m-rn    REALNAME   - Sets the real name\n"
139        "  --m-nick  NICK       - Sets the nick name\n"
140        "  --m-email EMAIL      - Sets the email address\n"
141        "  --m-hp    HOMEPAGE   - Sets the homepage URL\n"
142        "  --m-thumbn THUMBNAIL - Sets a URL to a thumbnail\n"
143        "  --m-loc   LOCATION   - Sets the location (room number)\n"
144        "  --m-org ORGANIZATION - Sets the organization/company name\n"
145       );
146}
147
148int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
149 int fh;
150
151 g_cons.svio = vio;
152
153 if ( !(g_cons.state & CON_CON) )
154  if ( roar_simple_connect(&(g_cons.con), server, "roarphone") == -1 )
155   return -1;
156
157 g_cons.state |= CON_CON;
158
159 // TODO: find out if this can be done more nicely by using VIOs
160
161 if ( (fh = roar_simple_new_stream_obj(&(g_cons.con), &(g_cons.stream),
162                                       info->rate, info->channels, info->bits, info->codec,
163                                       ROAR_DIR_BIDIR
164                                      )) == -1 )
165  return -1;
166
167 roar_vio_open_fh_socket(vio, fh);
168
169 g_cons.state |= CON_STREAM;
170
171 return 0;
172}
173
174#define _SET_META(ivar,itype) if ( (ivar) != NULL ) {  \
175                              meta.value = (ivar);     \
176                              meta.type  = (itype);    \
177                              roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_SET, &meta); \
178                              }
179int set_meta (void) {
180 struct roar_meta   meta;
181
182 meta.value  = g_meta.tmp.value;
183 meta.key[0] = 0;
184 meta.type   = ROAR_META_TYPE_NONE;
185
186 roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_CLEAR, &meta);
187
188 _SET_META(g_meta.thumbnail, ROAR_META_TYPE_THUMBNAIL);
189 _SET_META(g_meta.loc,       ROAR_META_TYPE_LOCATION);
190 _SET_META(g_meta.hp,        ROAR_META_TYPE_HOMEPAGE);
191 _SET_META(g_meta.org,       ROAR_META_TYPE_ORGANIZATION);
192
193 if ( g_meta.nick != NULL ) {
194  if ( g_meta.rn  != NULL ) {
195   snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s)", g_meta.rn, g_meta.nick);
196   g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
197   _SET_META(g_meta.tmp.value, ROAR_META_TYPE_AUTHOR);
198  } else {
199   _SET_META(g_meta.nick, ROAR_META_TYPE_AUTHOR);
200  }
201 } else {
202  if ( g_meta.rn  != NULL ) {
203   _SET_META(g_meta.rn, ROAR_META_TYPE_AUTHOR);
204  }
205 }
206
207 // TODO: make this more nice...
208 if ( g_meta.email != NULL ) {
209  if ( g_meta.nick != NULL ) {
210   if ( g_meta.rn != NULL ) {
211    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s) <%s>", g_meta.rn, g_meta.nick, g_meta.email);
212   } else {
213    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.nick, g_meta.email);
214   }
215  } else {
216   if ( g_meta.rn != NULL ) {
217    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.rn, g_meta.email);
218   } else {
219    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "<%s>", g_meta.email);
220   }
221  }
222  g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
223  _SET_META(g_meta.tmp.value, ROAR_META_TYPE_CONTACT);
224 }
225
226 return 0;
227}
228
229#ifdef _SPEEX_API_OLD
230int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
231 static SpeexEchoState * state = NULL;
232 size_t samples = info->rate / TIMEDIV;
233 static int16_t * obuf = NULL;
234
235 if ( info->channels != 1 )
236  return -1;
237
238 if (len != samples)
239  return -1;
240
241 ROAR_DBG("anti_echo_speex16(*) = ?");
242
243 if ( state == NULL ) {
244  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
245   return -1;
246
247  // todo: set sample rate.
248 }
249
250 ROAR_DBG("anti_echo_speex16(*) = ?");
251
252 if ( obuf == NULL ) {
253  if ( (obuf = malloc(2*samples)) == NULL )
254   return -1;
255 }
256
257 ROAR_DBG("anti_echo_speex16(*) = ?");
258
259/*
260 speex_echo_cancellation(state, buf, aebuf, obuf);
261*/
262
263 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
264
265 memcpy(buf, obuf, 2*samples);
266
267 ROAR_DBG("anti_echo_speex16(*) = 0");
268
269 return 0;
270}
271#endif
272
273int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
274 size_t i;
275
276 switch (g_conf.antiecho) {
277  case AE_NONE:
278    return 0;
279   break;
280  case AE_SIMPLE:
281    for (i = 0; i < len; i++)
282     buf[i] -= aebuf[i];
283    return 0;
284   break;
285#ifdef _SPEEX_API_OLD
286  case AE_SPEEX:
287    return anti_echo_speex16(buf, aebuf, len, info);
288   break;
289#endif
290  case AE_ROARD:
291    return 0;
292   break;
293  default:
294    return -1;
295   break;
296 }
297
298 return -1;
299}
300
301int zero_if_noise16 (int16_t * data, size_t samples) {
302 int64_t rms = roar_rms2_1_16(data, samples);
303
304 if ( rms < g_conf.dtx_threshold ) {
305  if ( dtx_counter ) {
306   dtx_counter--;
307  } else {
308   memset(data, 0, samples*2);
309  }
310 } else {
311  dtx_counter = DTX_F;
312 }
313
314 return 0;
315}
316
317int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
318 size_t len;
319 void * outbuf, * micbuf;
320 ssize_t outlen, miclen;
321 unsigned long int pkg_count = 0;
322
323 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
324 len = g_conf.samples * info->bits / 8;
325 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
326
327 if ( (outbuf = malloc(2*len)) == NULL )
328  return -1;
329
330 micbuf = outbuf + len;
331
332 while (1) {
333  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
334   break;
335
336  if ( roardsp_fchain_num(&(g_filterchains.input)) ) {
337   if ( roardsp_fchain_calc(&(g_filterchains.input), micbuf, len) == -1 )
338    break;
339  }
340
341  if ( g_conf.dtx_threshold > 0 )
342   if ( info->bits == 16 )
343    zero_if_noise16(micbuf, miclen/2);
344
345  if ( g_conf.transcode ) {
346   if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 )
347    break;
348  } else {
349   if ( roar_vio_write(s1, micbuf, miclen) != miclen )
350    break;
351  }
352
353  if ( g_conf.ioflush_interval != -1 ) {
354   if ( !(pkg_count % g_conf.ioflush_interval) )
355    roar_vio_sync(s1);
356  }
357
358  if ( g_conf.transcode ) {
359   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
360   if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 )
361    break;
362
363   outlen = len;
364  } else {
365   if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
366    break;
367  }
368
369  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
370   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
371
372  if ( roardsp_fchain_num(&(g_filterchains.output)) ) {
373   if ( roardsp_fchain_calc(&(g_filterchains.output), outbuf, outlen) == -1 )
374    break;
375  }
376
377  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
378   break;
379
380  pkg_count++;
381 }
382
383 free(outbuf);
384
385 return 0;
386}
387
388int main (int argc, char * argv[]) {
389 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
390                                .bits     = ROAR_BITS_DEFAULT,
391                                .channels = ROAR_CHANNELS_DEFAULT,
392                                .codec    = ROAR_CODEC_DEFAULT
393                               };
394 struct roar_audio_info dinfo;
395 struct roar_vio_calls dvio, svio, svio_jumbo, svio_real;
396 struct roar_vio_calls * svio_p;
397 struct roardsp_filter * filter;
398 char * driver   = DRIVER;
399 char * device   = NULL;
400 char * server   = NULL;
401 char * k;
402 int    i;
403
404 memset(&g_conf, 0, sizeof(g_conf));
405
406 g_conf.antiecho         = AE_ROARD;
407 g_conf.dtx_threshold    = -1;
408 g_conf.ioflush_interval = -1;
409
410 memset(&g_cons, 0, sizeof(g_cons));
411 g_cons.state = CON_NONE;
412
413 memset(&g_meta, 0, sizeof(g_meta));
414
415 roardsp_fchain_init(&(g_filterchains.input));
416 roardsp_fchain_init(&(g_filterchains.output));
417
418 for (i = 1; i < argc; i++) {
419  k = argv[i];
420
421  if ( strcmp(k, "--server") == 0 ) {
422   server = argv[++i];
423  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
424   g_conf.jumbo_mtu = atoi(argv[++i]);
425  } else if ( strcmp(k, "--io-flush") == 0 ) {
426   g_conf.ioflush_interval = atoi(argv[++i]);
427  } else if ( strcmp(k, "--rate") == 0 ) {
428   info.rate = atoi(argv[++i]);
429  } else if ( strcmp(k, "--bits") == 0 ) {
430   info.bits = atoi(argv[++i]);
431  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
432   info.channels = atoi(argv[++i]);
433
434/*
435 printf("  --afi-downmix        - Enable input downmixing\n"
436        "  --afi-lowpass FREQ   - Enable input lowpass at FREQ (in Hz)\n"
437*/
438  } else if ( strcmp(k, "--afi-downmix") == 0 ) {
439   g_conf.filter.in.downmix = 1;
440  } else if ( strcmp(k, "--afi-lowpass") == 0 ) {
441   g_conf.filter.in.lowp_freq = atof(argv[++i]);
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#undef _err
589
590 if ( g_conf.transcode ) {
591  dinfo.codec = info.codec;
592
593  if ( roar_bixcoder_init(transcoder, &dinfo, svio_p) == -1 ) {
594   roar_vio_close(&svio);
595   roar_vio_close(&dvio);
596   return 10;
597  }
598
599  // ignore errors as it may also work if this fails
600  roar_bixcoder_write_header(transcoder);
601  roar_bixcoder_read_header(transcoder);
602
603  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
604 }
605
606 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
607 run_stream(&dvio, svio_p, &info);
608 ROAR_DBG("main(*): RET");
609
610 roar_bixcoder_close(transcoder);
611
612 roar_vio_close(svio_p);
613 roar_vio_close(&dvio);
614
615 roardsp_fchain_uninit(&(g_filterchains.input));
616 roardsp_fchain_uninit(&(g_filterchains.output));
617
618 roar_disconnect(&(g_cons.con));
619
620 return 0;
621}
622
623//ll
Note: See TracBrowser for help on using the repository browser.