source: roaraudio/roarclients/roarphone.c @ 2978:1bed68e57d66

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

added support for filter chains

File size: 13.8 KB
RevLine 
[2292]1//roarphone.c:
[2124]2
3/*
[2282]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
[2124]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>
[2185]26#include <libroardsp/libroardsp.h>
[2282]27#include <libroareio/libroareio.h>
[2124]28
[2215]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
[2141]36#include <speex/speex_echo.h>
37#endif
38
39#define TIMEDIV  100
[2124]40
[2125]41#define DRIVER  "oss"
42
[2140]43// anti echo:
44#define AE_NONE      0
45#define AE_SIMPLE    1
46#define AE_SPEEX     2
[2141]47#define AE_ROARD     3
[2140]48
[2322]49#define DTX_F        25
50
[2329]51#define CON_NONE     0x00
52#define CON_CON      0x01
53#define CON_STREAM   0x02
54
[2140]55struct {
56 int antiecho;
[2166]57 int samples;
[2167]58 int transcode;
[2321]59 int64_t dtx_threshold;
[2140]60} g_conf;
61
[2322]62int dtx_counter = 0;
63
[2329]64struct {
65 int state;
66 struct roar_connection con;
67 struct roar_stream     stream;
68 struct roar_vio_calls * svio;
69} g_cons;
70
[2185]71struct roar_bixcoder transcoder[1];
72
[2330]73struct {
[2977]74 struct roardsp_filterchain input;
75 struct roardsp_filterchain output;
76} g_filterchains;
77
78struct {
[2330]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
[2124]92void usage (void) {
[2292]93 printf("roarphone [OPTIONS]...\n");
[2124]94
[2330]95 printf("\nServer Options:\n\n");
[2124]96
[2141]97 printf("  --server   SERVER    - Set server hostname\n"
[2330]98       );
99
100 printf("\nAudio Options:\n\n");
101 printf("  --rate     RATE      - Set sample rate\n"
[2141]102        "  --bits     BITS      - Set bits per sample\n"
103        "  --chans    CHANNELS  - Set number of channels\n"
[2330]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"
[2141]113        "  --device   DEVICE    - Set the device\n"
[2330]114       );
115
116 printf("\nGeneral Options:\n\n");
117 printf("  --antiecho AEMODE    - Set the anti echo mode\n"
[2321]118        "  --threshold DTXTHRES - Set the DTX threshold, disabled by default\n"
[2141]119        "  --help               - Show this help\n"
[2124]120       );
121
[2330]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       );
[2124]131}
132
[2128]133int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
[2329]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;
[2128]157}
158
[2330]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
[2215]214#ifdef _SPEEX_API_OLD
[2141]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
[2142]226 ROAR_DBG("anti_echo_speex16(*) = ?");
227
[2141]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
[2142]235 ROAR_DBG("anti_echo_speex16(*) = ?");
236
[2141]237 if ( obuf == NULL ) {
238  if ( (obuf = malloc(2*samples)) == NULL )
239   return -1;
240 }
241
[2142]242 ROAR_DBG("anti_echo_speex16(*) = ?");
243
[2141]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
[2142]252 ROAR_DBG("anti_echo_speex16(*) = 0");
253
[2141]254 return 0;
255}
256#endif
257
258int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
[2140]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];
[2293]268    return 0;
[2140]269   break;
[2215]270#ifdef _SPEEX_API_OLD
[2141]271  case AE_SPEEX:
272    return anti_echo_speex16(buf, aebuf, len, info);
273   break;
274#endif
[2293]275  case AE_ROARD:
276    return 0;
277   break;
[2140]278  default:
279    return -1;
280   break;
281 }
282
283 return -1;
284}
285
[2321]286int zero_if_noise16 (int16_t * data, size_t samples) {
287 int64_t rms = roar_rms2_1_16(data, samples);
288
[2322]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 }
[2321]298
299 return 0;
300}
301
[2128]302int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
[2129]303 size_t len;
[2140]304 void * outbuf, * micbuf;
305 ssize_t outlen, miclen;
[2129]306
[2312]307 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
[2166]308 len = g_conf.samples * info->bits / 8;
[2312]309 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
[2129]310
[2140]311 if ( (outbuf = malloc(2*len)) == NULL )
[2129]312  return -1;
313
[2140]314 micbuf = outbuf + len;
315
[2129]316 while (1) {
[2140]317  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
[2129]318   break;
[2321]319
[2977]320  if ( roardsp_fchain_num(&(g_filterchains.input)) ) {
321   if ( roardsp_fchain_calc(&(g_filterchains.input), micbuf, len) == -1 )
322    break;
323  }
324
[2321]325  if ( g_conf.dtx_threshold > 0 )
326   if ( info->bits == 16 )
327    zero_if_noise16(micbuf, miclen/2);
328
[2185]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 ) {
[2312]338   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
[2185]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  }
[2140]347
[2162]348  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
[2141]349   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
[2140]350
[2977]351  if ( roardsp_fchain_num(&(g_filterchains.output)) ) {
352   if ( roardsp_fchain_calc(&(g_filterchains.output), outbuf, outlen) == -1 )
353    break;
354  }
355
[2140]356  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
[2129]357   break;
358 }
359
[2140]360 free(outbuf);
[2129]361
362 return 0;
[2128]363}
364
[2124]365int main (int argc, char * argv[]) {
[2125]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                               };
[2167]371 struct roar_audio_info dinfo;
[2570]372 struct roar_vio_calls dvio, svio, svio_real;
[2126]373 char * driver   = DRIVER;
374 char * device   = NULL;
[2124]375 char * server   = NULL;
376 char * k;
377 int    i;
378
[2140]379 memset(&g_conf, 0, sizeof(g_conf));
380
[2321]381 g_conf.antiecho      = AE_ROARD;
382 g_conf.dtx_threshold = -1;
[2140]383
[2329]384 memset(&g_cons, 0, sizeof(g_cons));
385 g_cons.state = CON_NONE;
386
[2330]387 memset(&g_meta, 0, sizeof(g_meta));
388
[2977]389 roardsp_fchain_init(&(g_filterchains.input));
390 roardsp_fchain_init(&(g_filterchains.output));
391
[2124]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 ) {
[2125]398   info.rate = atoi(argv[++i]);
[2124]399  } else if ( strcmp(k, "--bits") == 0 ) {
[2125]400   info.bits = atoi(argv[++i]);
[2124]401  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
[2125]402   info.channels = atoi(argv[++i]);
[2124]403  } else if ( strcmp(k, "--codec") == 0 ) {
[2125]404   info.codec = roar_str2codec(argv[++i]);
[2127]405  } else if ( strcmp(k, "--driver") == 0 ) {
406   driver = argv[++i];
407  } else if ( strcmp(k, "--device") == 0 ) {
408   device = argv[++i];
[2141]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   }
[2321]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;
[2167]429  } else if ( strcmp(k, "--transcode") == 0 ) {
430   g_conf.transcode = 1;
[2330]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
[2124]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
[2568]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
[2293]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
[2166]467 g_conf.samples = info.channels * info.rate / TIMEDIV;
468
[2167]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 ) {
[2570]490  ROAR_ERR("Can not open sound card.");
[2126]491  return 1;
492 }
493
[2312]494 ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)");
[2570]495 if ( open_stream(&svio_real, server, &info) == -1 ) {
496  ROAR_ERR("Can not open connection to server.");
[2128]497  roar_vio_close(&dvio);
498  return 2;
499 }
[2312]500 ROAR_DBG("main(*): RET");
[2128]501
[2570]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
[2330]508 set_meta();
509
[2185]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
[2925]519  // ignore errors as it may also work if this fails
520  roar_bixcoder_write_header(transcoder);
521  roar_bixcoder_read_header(transcoder);
522
[2196]523  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
[2185]524 }
525
[2312]526 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
[2140]527 run_stream(&dvio, &svio, &info);
[2312]528 ROAR_DBG("main(*): RET");
[2128]529
[2185]530 roar_bixcoder_close(transcoder);
531
[2128]532 roar_vio_close(&svio);
[2126]533 roar_vio_close(&dvio);
534
[2977]535 roardsp_fchain_uninit(&(g_filterchains.input));
536 roardsp_fchain_uninit(&(g_filterchains.output));
537
[2329]538 roar_disconnect(&(g_cons.con));
539
[2124]540 return 0;
541}
542
543//ll
Note: See TracBrowser for help on using the repository browser.