source: roaraudio/roarclients/roarphone.c @ 2977:60f2e8ce2816

Last change on this file since 2977:60f2e8ce2816 was 2977:60f2e8ce2816, checked in by phi, 15 years ago

added support for filter chains

File size: 13.8 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} g_conf;
61
62int dtx_counter = 0;
63
64struct {
65 int state;
66 struct roar_connection con;
67 struct roar_stream     stream;
68 struct roar_vio_calls * svio;
69} g_cons;
70
71struct roar_bixcoder transcoder[1];
72
73struct {
74 struct roardsp_filterchain input;
75 struct roardsp_filterchain output;
76} g_filterchains;
77
78struct {
79 struct {
80  char key[ROAR_META_MAX_NAMELEN];
81  char value[LIBROAR_BUFFER_MSGDATA];
82 } tmp;
83 char * rn;
84 char * nick;
85 char * org;
86 char * email;
87 char * hp;
88 char * loc;
89 char * thumbnail;
90} g_meta;
91
92void usage (void) {
93 printf("roarphone [OPTIONS]...\n");
94
95 printf("\nServer Options:\n\n");
96
97 printf("  --server   SERVER    - Set server hostname\n"
98       );
99
100 printf("\nAudio Options:\n\n");
101 printf("  --rate     RATE      - Set sample rate\n"
102        "  --bits     BITS      - Set bits per sample\n"
103        "  --chans    CHANNELS  - Set number of channels\n"
104       );
105
106 printf("\nCodec Options:\n\n");
107 printf("  --codec    CODEC     - Set the codec\n"
108        "  --transcode          - Use local transcodeing\n"
109       );
110
111 printf("\nDriver Options:\n\n");
112 printf("  --driver   DRIVER    - Set the driver\n"
113        "  --device   DEVICE    - Set the device\n"
114       );
115
116 printf("\nGeneral Options:\n\n");
117 printf("  --antiecho AEMODE    - Set the anti echo mode\n"
118        "  --threshold DTXTHRES - Set the DTX threshold, disabled by default\n"
119        "  --help               - Show this help\n"
120       );
121
122 printf("\nMeta Data Options:\n\n");
123 printf("  --m-rn    REALNAME   - Sets the real name\n"
124        "  --m-nick  NICK       - Sets the nick name\n"
125        "  --m-email EMAIL      - Sets the email address\n"
126        "  --m-hp    HOMEPAGE   - Sets the homepage URL\n"
127        "  --m-thumbn THUMBNAIL - Sets a URL to a thumbnail\n"
128        "  --m-loc   LOCATION   - Sets the location (room number)\n"
129        "  --m-org ORGANIZATION - Sets the organization/company name\n"
130       );
131}
132
133int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
134 int fh;
135
136 g_cons.svio = vio;
137
138 if ( !(g_cons.state & CON_CON) )
139  if ( roar_simple_connect(&(g_cons.con), server, "roarphone") == -1 )
140   return -1;
141
142 g_cons.state |= CON_CON;
143
144 // TODO: find out if this can be done more nicely by using VIOs
145
146 if ( (fh = roar_simple_new_stream_obj(&(g_cons.con), &(g_cons.stream),
147                                       info->rate, info->channels, info->bits, info->codec,
148                                       ROAR_DIR_BIDIR
149                                      )) == -1 )
150  return -1;
151
152 roar_vio_open_fh_socket(vio, fh);
153
154 g_cons.state |= CON_STREAM;
155
156 return 0;
157}
158
159#define _SET_META(ivar,itype) if ( (ivar) != NULL ) {  \
160                              meta.value = (ivar);     \
161                              meta.type  = (itype);    \
162                              roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_SET, &meta); \
163                              }
164int set_meta (void) {
165 struct roar_meta   meta;
166
167 meta.value  = g_meta.tmp.value;
168 meta.key[0] = 0;
169 meta.type   = ROAR_META_TYPE_NONE;
170
171 roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_CLEAR, &meta);
172
173 _SET_META(g_meta.thumbnail, ROAR_META_TYPE_THUMBNAIL);
174 _SET_META(g_meta.loc,       ROAR_META_TYPE_LOCATION);
175 _SET_META(g_meta.hp,        ROAR_META_TYPE_HOMEPAGE);
176 _SET_META(g_meta.org,       ROAR_META_TYPE_ORGANIZATION);
177
178 if ( g_meta.nick != NULL ) {
179  if ( g_meta.rn  != NULL ) {
180   snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s)", g_meta.rn, g_meta.nick);
181   g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
182   _SET_META(g_meta.tmp.value, ROAR_META_TYPE_AUTHOR);
183  } else {
184   _SET_META(g_meta.nick, ROAR_META_TYPE_AUTHOR);
185  }
186 } else {
187  if ( g_meta.rn  != NULL ) {
188   _SET_META(g_meta.rn, ROAR_META_TYPE_AUTHOR);
189  }
190 }
191
192 // TODO: make this more nice...
193 if ( g_meta.email != NULL ) {
194  if ( g_meta.nick != NULL ) {
195   if ( g_meta.rn != NULL ) {
196    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s) <%s>", g_meta.rn, g_meta.nick, g_meta.email);
197   } else {
198    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.nick, g_meta.email);
199   }
200  } else {
201   if ( g_meta.rn != NULL ) {
202    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.rn, g_meta.email);
203   } else {
204    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "<%s>", g_meta.email);
205   }
206  }
207  g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
208  _SET_META(g_meta.tmp.value, ROAR_META_TYPE_CONTACT);
209 }
210
211 return 0;
212}
213
214#ifdef _SPEEX_API_OLD
215int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
216 static SpeexEchoState * state = NULL;
217 size_t samples = info->rate / TIMEDIV;
218 static int16_t * obuf = NULL;
219
220 if ( info->channels != 1 )
221  return -1;
222
223 if (len != samples)
224  return -1;
225
226 ROAR_DBG("anti_echo_speex16(*) = ?");
227
228 if ( state == NULL ) {
229  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
230   return -1;
231
232  // todo: set sample rate.
233 }
234
235 ROAR_DBG("anti_echo_speex16(*) = ?");
236
237 if ( obuf == NULL ) {
238  if ( (obuf = malloc(2*samples)) == NULL )
239   return -1;
240 }
241
242 ROAR_DBG("anti_echo_speex16(*) = ?");
243
244/*
245 speex_echo_cancellation(state, buf, aebuf, obuf);
246*/
247
248 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
249
250 memcpy(buf, obuf, 2*samples);
251
252 ROAR_DBG("anti_echo_speex16(*) = 0");
253
254 return 0;
255}
256#endif
257
258int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
259 size_t i;
260
261 switch (g_conf.antiecho) {
262  case AE_NONE:
263    return 0;
264   break;
265  case AE_SIMPLE:
266    for (i = 0; i < len; i++)
267     buf[i] -= aebuf[i];
268    return 0;
269   break;
270#ifdef _SPEEX_API_OLD
271  case AE_SPEEX:
272    return anti_echo_speex16(buf, aebuf, len, info);
273   break;
274#endif
275  case AE_ROARD:
276    return 0;
277   break;
278  default:
279    return -1;
280   break;
281 }
282
283 return -1;
284}
285
286int zero_if_noise16 (int16_t * data, size_t samples) {
287 int64_t rms = roar_rms2_1_16(data, samples);
288
289 if ( rms < g_conf.dtx_threshold ) {
290  if ( dtx_counter ) {
291   dtx_counter--;
292  } else {
293   memset(data, 0, samples*2);
294  }
295 } else {
296  dtx_counter = DTX_F;
297 }
298
299 return 0;
300}
301
302int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
303 size_t len;
304 void * outbuf, * micbuf;
305 ssize_t outlen, miclen;
306
307 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
308 len = g_conf.samples * info->bits / 8;
309 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
310
311 if ( (outbuf = malloc(2*len)) == NULL )
312  return -1;
313
314 micbuf = outbuf + len;
315
316 while (1) {
317  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
318   break;
319
320  if ( roardsp_fchain_num(&(g_filterchains.input)) ) {
321   if ( roardsp_fchain_calc(&(g_filterchains.input), micbuf, len) == -1 )
322    break;
323  }
324
325  if ( g_conf.dtx_threshold > 0 )
326   if ( info->bits == 16 )
327    zero_if_noise16(micbuf, miclen/2);
328
329  if ( g_conf.transcode ) {
330   if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 )
331    break;
332  } else {
333   if ( roar_vio_write(s1, micbuf, miclen) != miclen )
334    break;
335  }
336
337  if ( g_conf.transcode ) {
338   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
339   if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 )
340    break;
341
342   outlen = len;
343  } else {
344   if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
345    break;
346  }
347
348  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
349   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
350
351  if ( roardsp_fchain_num(&(g_filterchains.output)) ) {
352   if ( roardsp_fchain_calc(&(g_filterchains.output), outbuf, outlen) == -1 )
353    break;
354  }
355
356  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
357   break;
358 }
359
360 free(outbuf);
361
362 return 0;
363}
364
365int main (int argc, char * argv[]) {
366 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
367                                .bits     = ROAR_BITS_DEFAULT,
368                                .channels = ROAR_CHANNELS_DEFAULT,
369                                .codec    = ROAR_CODEC_DEFAULT
370                               };
371 struct roar_audio_info dinfo;
372 struct roar_vio_calls dvio, svio, svio_real;
373 char * driver   = DRIVER;
374 char * device   = NULL;
375 char * server   = NULL;
376 char * k;
377 int    i;
378
379 memset(&g_conf, 0, sizeof(g_conf));
380
381 g_conf.antiecho      = AE_ROARD;
382 g_conf.dtx_threshold = -1;
383
384 memset(&g_cons, 0, sizeof(g_cons));
385 g_cons.state = CON_NONE;
386
387 memset(&g_meta, 0, sizeof(g_meta));
388
389 roardsp_fchain_init(&(g_filterchains.input));
390 roardsp_fchain_init(&(g_filterchains.output));
391
392 for (i = 1; i < argc; i++) {
393  k = argv[i];
394
395  if ( strcmp(k, "--server") == 0 ) {
396   server = argv[++i];
397  } else if ( strcmp(k, "--rate") == 0 ) {
398   info.rate = atoi(argv[++i]);
399  } else if ( strcmp(k, "--bits") == 0 ) {
400   info.bits = atoi(argv[++i]);
401  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
402   info.channels = atoi(argv[++i]);
403  } else if ( strcmp(k, "--codec") == 0 ) {
404   info.codec = roar_str2codec(argv[++i]);
405  } else if ( strcmp(k, "--driver") == 0 ) {
406   driver = argv[++i];
407  } else if ( strcmp(k, "--device") == 0 ) {
408   device = argv[++i];
409  } else if ( strcmp(k, "--antiecho") == 0 ) {
410   k = argv[++i];
411   if ( !strcmp(k, "none") ) {
412    g_conf.antiecho = AE_NONE;
413   } else if ( !strcmp(k, "simple") ) {
414    g_conf.antiecho = AE_SIMPLE;
415   } else if ( !strcmp(k, "speex") ) {
416    g_conf.antiecho = AE_SPEEX;
417   } else if ( !strcmp(k, "roard") ) {
418    g_conf.antiecho = AE_ROARD;
419   } else {
420    fprintf(stderr, "Error: unknown mode: %s\n", k);
421    return 1;
422   }
423  } else if ( strcmp(k, "--threshold") == 0 ) {
424   g_conf.dtx_threshold = atol(argv[++i]);
425
426   // use threshold^2 or threshold < 0 for not using DTX
427   if ( g_conf.dtx_threshold > 0 )
428    g_conf.dtx_threshold *= g_conf.dtx_threshold;
429  } else if ( strcmp(k, "--transcode") == 0 ) {
430   g_conf.transcode = 1;
431
432  // META DATA:
433  } else if ( strcmp(k, "--m-rn") == 0 ) {
434   g_meta.rn = argv[++i];
435  } else if ( strcmp(k, "--m-nick") == 0 ) {
436   g_meta.nick = argv[++i];
437  } else if ( strcmp(k, "--m-email") == 0 ) {
438   g_meta.email = argv[++i];
439  } else if ( strcmp(k, "--m-hp") == 0 ) {
440   g_meta.hp = argv[++i];
441  } else if ( strcmp(k, "--m-thumbn") == 0 ) {
442   g_meta.thumbnail = argv[++i];
443  } else if ( strcmp(k, "--m-loc") == 0 ) {
444   g_meta.loc = argv[++i];
445  } else if ( strcmp(k, "--m-org") == 0 ) {
446   g_meta.org = argv[++i];
447
448
449  } else if ( strcmp(k, "--help") == 0 ) {
450   usage();
451   return 0;
452  } else {
453   fprintf(stderr, "Error: unknown argument: %s\n", k);
454   usage();
455   return 1;
456  }
457 }
458
459 // ignore errors, maybe it will work even if this fails
460 // (btw. it will never fail without crashing the rest of the app ;)
461 roar_libroar_set_server(server);
462
463 if ( g_conf.antiecho == AE_SPEEX ) {
464  ROAR_WARN("Speex Antiecho is obsolete and may be removed in future versions. Use --antiecho roard");
465 }
466
467 g_conf.samples = info.channels * info.rate / TIMEDIV;
468
469 memcpy(&dinfo, &info, sizeof(dinfo));
470
471 if ( g_conf.transcode ) {
472  dinfo.bits  = 16;
473  dinfo.codec = ROAR_CODEC_DEFAULT;
474
475  switch (info.codec) {
476   case ROAR_CODEC_ALAW:
477   case ROAR_CODEC_MULAW:
478     info.bits = 8;
479    break;
480   case ROAR_CODEC_ROAR_CELT:
481     info.bits = 16;
482    break;
483   case ROAR_CODEC_ROAR_SPEEX:
484     info.bits = 16;
485    break;
486  }
487 }
488
489 if ( roar_cdriver_open(&dvio, driver, device, &dinfo, ROAR_DIR_BIDIR) == -1 ) {
490  ROAR_ERR("Can not open sound card.");
491  return 1;
492 }
493
494 ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)");
495 if ( open_stream(&svio_real, server, &info) == -1 ) {
496  ROAR_ERR("Can not open connection to server.");
497  roar_vio_close(&dvio);
498  return 2;
499 }
500 ROAR_DBG("main(*): RET");
501
502 if ( roar_vio_open_re(&svio, &svio_real) == -1 ) {
503  ROAR_ERR("Can not open connection to server (RE VIO).");
504  roar_vio_close(&dvio);
505  return 2;
506 }
507
508 set_meta();
509
510 if ( g_conf.transcode ) {
511  dinfo.codec = info.codec;
512
513  if ( roar_bixcoder_init(transcoder, &dinfo, &svio) == -1 ) {
514   roar_vio_close(&svio);
515   roar_vio_close(&dvio);
516   return 10;
517  }
518
519  // ignore errors as it may also work if this fails
520  roar_bixcoder_write_header(transcoder);
521  roar_bixcoder_read_header(transcoder);
522
523  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
524 }
525
526 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
527 run_stream(&dvio, &svio, &info);
528 ROAR_DBG("main(*): RET");
529
530 roar_bixcoder_close(transcoder);
531
532 roar_vio_close(&svio);
533 roar_vio_close(&dvio);
534
535 roardsp_fchain_uninit(&(g_filterchains.input));
536 roardsp_fchain_uninit(&(g_filterchains.output));
537
538 roar_disconnect(&(g_cons.con));
539
540 return 0;
541}
542
543//ll
Note: See TracBrowser for help on using the repository browser.