source: roaraudio/roarclients/roarphone.c @ 2986:aee721f3e7e1

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

patch for jumbo packets

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