source: roaraudio/roarclients/roarinterconnect.c @ 6052:d48765b2475e

Last change on this file since 6052:d48765b2475e was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 15.1 KB
Line 
1//roarinterconnect.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2015
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26/* ckport options:
27 * ckport: ignore-symbol: roar_cdriver_oss of target libroareio0 -- For OSS streams
28 */
29
30// configure ROAR_INFO()
31int g_verbose = 0;
32#define ROAR_DBG_INFOVAR g_verbose
33
34#include <roaraudio.h>
35#include <libroareio/libroareio.h>
36
37#ifdef ROAR_HAVE_ESD
38#include <esd.h>
39#endif
40
41#ifdef ROAR_HAVE_LIBRSOUND
42#include <rsound.h>
43#ifdef RSD_EXEC
44#define _HAVE_RSOUND
45#endif
46#endif
47
48#if defined(ROAR_HAVE_OSS_BSD) || defined(ROAR_HAVE_OSS)
49#define _HAVE_OSS
50#endif
51
52#define CLIENT_NAME "roarinterconnect"
53
54#define MT_NONE     0x00
55#define MT_MASK     0xF0
56#define MT_ROAR     0x10
57#define MT_ESD      0x20
58#define MT_SIMPLE   0x30
59#define MT_OSS      0x40
60#define MT_RSOUND   0x50
61#define MT_DEFAULT  MT_ROAR
62
63#define ST_NONE     0x00
64#define ST_MASK     0x0F
65#define ST_BIDIR    0x01
66#define ST_FILTER   0x02
67#define ST_TRANSMIT 0x03
68#define ST_RECEIVE  0x04
69#define ST_RECPLAY  0x05
70#define ST_RECORD   0x06
71// no default here as the default depend on the server type
72
73void usage (void) {
74 printf("roarinterconnect [OPTIONS]...\n");
75
76 printf("\nOptions:\n\n");
77
78 printf("  --server SERVER     - Set server hostname\n"
79        "  --remote SERVER     - Set remote server\n"
80        "  --type   TYPE       - Set type of remote server\n"
81        "  --rate  -R RATE     - Set sample rate\n"
82        "  --bits  -B BITS     - Set bits per sample\n"
83        "  --chans -C CHANNELS - Set number of channels\n"
84        "  --codec -E CODEC    - Set the codec\n"
85        "  --aiprofile PROFILE - Set audio profile\n"
86        "  --help              - Show this help\n"
87        "  --verbose -v        - Be verbose\n"
88       );
89
90 printf("\nPossible Types:\n\n");
91 printf("  roar                - RoarAudio Server\n"
92#ifdef ROAR_HAVE_ESD
93        "  esd                 - EsounD Server\n"
94#endif
95        "  simple              - PulseAudio using simple protocol\n"
96#ifdef _HAVE_OSS
97        "  oss                 - Open Sound System (OSS) device\n"
98#endif
99#ifdef _HAVE_RSOUND
100        "  rsound              - RSound server\n"
101#endif
102        "\n"
103        "  bidir               - Connect bidirectional\n"
104        "  filter              - Use local server as filter for remote server\n"
105        "  transmit            - Transmit data from local server to remote server\n"
106        "  receive             - Receive data from remote server\n"
107        "  recplay             - Record from and play data to remote server\n"
108        "  record              - Record data from remote server\n"
109       );
110
111}
112
113int parse_type (char * type) {
114 int ret = MT_NONE|ST_NONE;
115 char * colon;
116
117 if ( type != NULL ) {
118  while (type != NULL && *type) {
119   if ( (colon = strstr(type, ":")) != NULL ) {
120    *colon = 0;
121     colon++;
122   }
123
124   if ( !strcmp(type, "roar") ) {
125    ret -= ret & MT_MASK;
126    ret += MT_ROAR;
127   } else if ( !strcmp(type, "esd") ) {
128    ret -= ret & MT_MASK;
129    ret += MT_ESD;
130   } else if ( !strcmp(type, "simple") ) {
131    ret -= ret & MT_MASK;
132    ret += MT_SIMPLE;
133   } else if ( !strcmp(type, "oss") ) {
134    ret -= ret & MT_MASK;
135    ret += MT_OSS;
136   } else if ( !strcmp(type, "rsound") ) {
137    ret -= ret & MT_MASK;
138    ret += MT_RSOUND;
139   } else if ( !strcmp(type, "bidir") ) {
140    ret -= ret & ST_MASK;
141    ret += ST_BIDIR;
142   } else if ( !strcmp(type, "filter") ) {
143    ret -= ret & ST_MASK;
144    ret += ST_FILTER;
145   } else if ( !strcmp(type, "transmit") ) {
146    ret -= ret & ST_MASK;
147    ret += ST_TRANSMIT;
148   } else if ( !strcmp(type, "receive") ) {
149    ret -= ret & ST_MASK;
150    ret += ST_RECEIVE;
151   } else if ( !strcmp(type, "recplay") ) {
152    ret -= ret & ST_MASK;
153    ret += ST_RECPLAY;
154   } else if ( !strcmp(type, "record") ) {
155    ret -= ret & ST_MASK;
156    ret += ST_RECORD;
157   } else {
158    return MT_NONE|ST_NONE;
159   }
160
161   type = colon;
162  }
163 }
164
165 if ( (ret & MT_MASK) == MT_NONE )
166  ret |= MT_DEFAULT;
167
168 if ( (ret & ST_MASK) == ST_NONE ) {
169  switch (ret & MT_MASK) {
170   case MT_ROAR:   ret |= ST_BIDIR;    break;
171   case MT_ESD:    ret |= ST_FILTER;   break;
172   case MT_SIMPLE: ret |= ST_TRANSMIT; break; // we use ST_TRANSMIT because ST_BIDIR is
173                                              // very unlike to be configured at the server side.
174   case MT_OSS:    ret |= ST_BIDIR;    break;
175   case MT_RSOUND: ret |= ST_TRANSMIT; break; // RSound does only handle playback streams.
176   default:
177     return MT_NONE|ST_NONE; // error case
178    break;
179  }
180 }
181
182 return ret;
183}
184
185#ifdef _HAVE_RSOUND
186// RSound format helper function:
187enum rsd_format para2rsdfmt (uint32_t bits, uint32_t codec) {
188 switch (codec) {
189  case ROAR_CODEC_PCM_S_LE:
190    switch (bits) {
191#ifdef RSD_S8
192     case  8: return RSD_S8;     break;
193#endif
194#ifdef RSD_S16_LE
195     case 16: return RSD_S16_LE; break;
196#endif
197#ifdef RSD_S32_LE
198     case 32: return RSD_S32_LE; break;
199#endif
200    }
201   break;
202  case ROAR_CODEC_PCM_S_BE:
203    switch (bits) {
204#ifdef RSD_S8
205     case  8: return RSD_S8;     break;
206#endif
207#ifdef RSD_S16_BE
208     case 16: return RSD_S16_BE; break;
209#endif
210#ifdef RSD_S32_BE
211     case 32: return RSD_S32_BE; break;
212#endif
213    }
214   break;
215  case ROAR_CODEC_PCM_U_LE:
216    switch (bits) {
217#ifdef RSD_U8
218     case  8: return RSD_U8;     break;
219#endif
220#ifdef RSD_U16_LE
221     case 16: return RSD_U16_LE; break;
222#endif
223#ifdef RSD_U32_LE
224     case 32: return RSD_U32_LE; break;
225#endif
226    }
227   break;
228  case ROAR_CODEC_PCM_U_BE:
229    switch (bits) {
230#ifdef RSD_U8
231     case  8: return RSD_U8;     break;
232#endif
233#ifdef RSD_U16_BE
234     case 16: return RSD_U16_BE; break;
235#endif
236#ifdef RSD_U32_BE
237     case 32: return RSD_U32_BE; break;
238#endif
239    }
240   break;
241#ifdef RSD_ALAW
242  case ROAR_CODEC_ALAW:
243    return RSD_ALAW;
244   break;
245#endif
246#ifdef RSD_MULAW
247  case ROAR_CODEC_MULAW:
248    return RSD_MULAW;
249   break;
250#endif
251 }
252 return 0;
253}
254#endif
255
256int main (int argc, char * argv[]) {
257 struct roar_connection con[1];
258 struct roar_stream     stream[1];
259 struct roar_vio_calls  vio;
260 struct roar_audio_info info;
261#ifdef _HAVE_RSOUND
262 rsound_t *rd;
263 enum rsd_format fmt = 0;
264#endif
265 int    type     = parse_type(NULL);
266 int    tmp;
267 const char * server   = NULL;
268 char * remote   = "+slp"; // we hope SLP located server is not local one
269 char * k;
270 int    rfh;
271 int    i;
272 int    localdir = ROAR_DIR_BIDIR;
273 int    rport;
274
275 if ( roar_profile2info(&info, "default") == -1 )
276  return 1;
277
278 for (i = 1; i < argc; i++) {
279  k = argv[i];
280
281  if ( strcmp(k, "--server") == 0 ) {
282   ROAR_CKHAVEARGS(1);
283   server = argv[++i];
284  } else if ( strcmp(k, "--remote") == 0 ) {
285   ROAR_CKHAVEARGS(1);
286   remote = argv[++i];
287  } else if ( strcmp(k, "--type") == 0 ) {
288   ROAR_CKHAVEARGS(1);
289   type = parse_type(argv[++i]);
290  } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 ) {
291   ROAR_CKHAVEARGS(1);
292   info.rate = roar_str2rate(argv[++i]);
293  } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) {
294   ROAR_CKHAVEARGS(1);
295   info.bits = roar_str2bits(argv[++i]);
296  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) {
297   ROAR_CKHAVEARGS(1);
298   info.channels = roar_str2channels(argv[++i]);
299  } else if ( strcmp(k, "--codec") == 0 || strcmp(k, "-E") == 0 ) {
300   ROAR_CKHAVEARGS(1);
301   info.codec = roar_str2codec(argv[++i]);
302  } else if ( !strcmp(k, "--aiprofile") ) {
303   ROAR_CKHAVEARGS(1);
304   if ( roar_profile2info(&info, argv[++i]) == -1 ) {
305    fprintf(stderr, "Error: Can not load audio profile: %s: %s\n", argv[i], roar_error2str(roar_error));
306    return 1;
307   }
308  } else if ( strcmp(k, "--verbose") == 0 || strcmp(k, "-v") == 0 ) {
309   g_verbose++;
310  } else if ( strcmp(k, "--help") == 0 ) {
311   usage();
312   return 0;
313  } else {
314   fprintf(stderr, "Error: unknown argument: %s\n", k);
315   usage();
316   return 1;
317  }
318 }
319
320 if ( server != NULL && !strcmp(server, "+invalid") ) {
321  return 0;
322 }
323
324 switch (type & MT_MASK) {
325  case MT_ROAR:
326    switch (type & ST_MASK) {
327     case ST_BIDIR:
328       tmp      = ROAR_DIR_BIDIR;
329      break;
330     case ST_FILTER:
331       tmp      = ROAR_DIR_FILTER;
332      break;
333     case ST_TRANSMIT:
334       tmp      = ROAR_DIR_PLAY;
335       localdir = ROAR_DIR_MONITOR;
336      break;
337     case ST_RECEIVE:
338       tmp      = ROAR_DIR_MONITOR;
339       localdir = ROAR_DIR_PLAY;
340      break;
341     case ST_RECPLAY:
342       tmp      = ROAR_DIR_RECPLAY;
343      break;
344     case ST_RECORD:
345       tmp      = ROAR_DIR_RECORD;
346       localdir = ROAR_DIR_PLAY;
347      break;
348     default:
349       fprintf(stderr, "Error: unknown stream type\n");
350       return 2;
351    }
352    if ( roar_vio_simple_stream(&vio,
353                                info.rate, info.channels, info.bits, info.codec,
354                                remote, tmp, CLIENT_NAME, -1) == -1 ) {
355     fprintf(stderr, "Error: can not open RoarAudio stream to %s: %s\n", remote, roar_error2str(roar_error));
356     return 2;
357    }
358    if ( roar_vio_ctl(&vio, ROAR_VIO_CTL_GET_FH, &rfh) == -1 ) {
359     fprintf(stderr, "Error: can not get filehandle for RoarAudio stream: %s\n", roar_error2str(roar_error));
360     roar_vio_close(&vio);
361     return 2;
362    }
363   break;
364#ifdef _HAVE_OSS
365  case MT_OSS:
366    switch (type & ST_MASK) {
367     case ST_BIDIR:
368       tmp      = ROAR_DIR_BIDIR;
369      break;
370     case ST_TRANSMIT:
371       tmp      = ROAR_DIR_PLAY;
372       localdir = ROAR_DIR_MONITOR;
373      break;
374     case ST_RECEIVE:
375       tmp      = ROAR_DIR_MONITOR;
376       localdir = ROAR_DIR_PLAY;
377      break;
378     case ST_RECPLAY:
379       tmp      = ROAR_DIR_RECPLAY;
380      break;
381     case ST_RECORD:
382       tmp      = ROAR_DIR_RECORD;
383       localdir = ROAR_DIR_PLAY;
384      break;
385     default:
386       fprintf(stderr, "Error: unknown stream type\n");
387       return 2;
388    }
389    if ( roar_cdriver_oss(&vio, "OSS", remote, &info, tmp) == -1 ) {
390     fprintf(stderr, "Error: can not open OSS device %s: %s\n", remote, roar_error2str(roar_error));
391     return 2;
392    }
393    if ( roar_vio_ctl(&vio, ROAR_VIO_CTL_GET_FH, &rfh) == -1 ) {
394     fprintf(stderr, "Error: can not get filehandle for OSS device %s: %s\n", remote, roar_error2str(roar_error));
395     roar_vio_close(&vio);
396     return 2;
397    }
398   break;
399#endif
400#ifdef ROAR_HAVE_ESD
401  case MT_ESD:
402    tmp = ESD_STREAM|ESD_PLAY;
403
404    switch (info.bits) {
405     case  8: tmp |= ESD_BITS8;  break;
406     case 16: tmp |= ESD_BITS16; break;
407     default:
408       fprintf(stderr, "Error: EsounD only supports 8 and 16 bit streams\n");
409       return 2;
410    }
411
412    switch (info.channels) {
413     case 1: tmp |= ESD_MONO;   break;
414     case 2: tmp |= ESD_STEREO; break;
415     default:
416       fprintf(stderr, "Error: EsounD only supports mono and stereo streams\n");
417       return 2;
418    }
419
420    // TODO: FIXME: this is only true if the esd runs on a LE system,...
421    if ( info.bits == 8 && info.codec != ROAR_CODEC_PCM_U_LE ) {
422     fprintf(stderr, "Error: EsounD only supports unsigned PCM in 8 bit mode\n");
423     return 2;
424    } else if ( info.bits == 16 && info.codec != ROAR_CODEC_DEFAULT ) {
425     fprintf(stderr, "Error: EsounD only supports signed PCM in 16 bit mode\n");
426     return 2;
427    }
428
429    switch (type & ST_MASK) {
430     case ST_FILTER:
431       rfh = esd_filter_stream(tmp, info.rate, remote, CLIENT_NAME);
432      break;
433     case ST_TRANSMIT:
434       rfh = esd_play_stream(tmp, info.rate, remote, CLIENT_NAME);
435       localdir = ROAR_DIR_MONITOR;
436      break;
437     case ST_RECEIVE:
438       rfh = esd_monitor_stream(tmp, info.rate, remote, CLIENT_NAME);
439       localdir = ROAR_DIR_PLAY;
440      break;
441     default:
442       fprintf(stderr, "Error: this type is not supported by EsounD\n");
443       return 2;
444      break;
445    }
446   break;
447#endif
448#ifdef _HAVE_RSOUND
449  case MT_RSOUND:
450    fmt = para2rsdfmt(info.bits, info.codec);
451
452    if ( fmt == 0 ) {
453     fprintf(stderr, "Error: Bits/Codec not supported by RSound\n");
454     return 2;
455    }
456
457    switch (type & ST_MASK) {
458     case ST_TRANSMIT:
459       localdir = ROAR_DIR_MONITOR;
460
461       rsd_init(&rd);
462       rsd_set_param(rd, RSD_HOST,       remote);
463       tmp = info.channels;
464       rsd_set_param(rd, RSD_CHANNELS,   &tmp);
465       tmp = info.rate;
466       rsd_set_param(rd, RSD_SAMPLERATE, &tmp);
467       rsd_set_param(rd, RSD_FORMAT,     &fmt);
468#ifdef RSD_IDENTITY
469       rsd_set_param(rd, RSD_IDENTITY,   CLIENT_NAME);
470#endif
471       rfh = rsd_exec(rd);
472       if ( rfh == -1 ) {
473        rsd_stop(rd);
474        rsd_free(rd);
475       }
476      break;
477     default:
478       fprintf(stderr, "Error: this type is not supported by RSound\n");
479       return 2;
480      break;
481    }
482   break;
483#endif
484  case MT_SIMPLE:
485    switch (type & ST_MASK) {
486     case ST_BIDIR:
487       tmp = -1;
488       localdir = ROAR_DIR_BIDIR;
489      break;
490     case ST_TRANSMIT:
491       tmp = SHUT_RD;
492       localdir = ROAR_DIR_MONITOR;
493      break;
494     case ST_RECEIVE:
495       tmp = SHUT_WR;
496       localdir = ROAR_DIR_PLAY;
497      break;
498     default:
499       fprintf(stderr, "Error: this type is not supported by PulseAudio\n");
500       return 2;
501    }
502    // we guess INET here...
503    if ( strstr(remote, "/") == NULL && (k = strstr(remote, ":")) != NULL ) {
504     *k = 0;
505     k++;
506     rport = atoi(k);
507    } else {
508     rport = ROAR_DEFAULT_PA_PORT;
509    }
510
511    if ( roar_vio_open_socket(&vio, remote, rport) == -1 ) {
512     fprintf(stderr, "Error: can not open socket for Simple stream: %s\n", roar_error2str(roar_error));
513     return 2;
514    }
515
516    roar_vio_shutdown(&vio, tmp);
517
518    if ( roar_vio_ctl(&vio, ROAR_VIO_CTL_GET_FH, &rfh) == -1 ) {
519     fprintf(stderr, "Error: can not get filehandle for Simple stream: %s\n", roar_error2str(roar_error));
520     roar_vio_close(&vio);
521     return 2;
522    }
523   break;
524  default:
525    fprintf(stderr, "Error: unknown/not supported server type\n");
526    return 2;
527 }
528
529 if ( rfh == -1 ) {
530  fprintf(stderr, "Error: can not connect to remote server\n");
531  return 10;
532 }
533
534 if ( roar_simple_connect(con, server, CLIENT_NAME) == -1 ) {
535  fprintf(stderr, "Can not connect to local server\n");
536  return 20;
537 }
538
539 if ( roar_stream_new_by_info(stream, &info) == -1 ) {
540  roar_disconnect(con);
541  return 21;
542 }
543
544 if ( roar_stream_connect(con, stream, localdir, -1) == -1 ) {
545  roar_disconnect(con);
546  return 22;
547 }
548
549 if ( roar_stream_passfh(con, stream, rfh) == -1 ) {
550  roar_disconnect(con);
551  return 23;
552 }
553
554 switch (type & MT_MASK) {
555  case MT_ESD:
556    close(rfh);
557   break;
558  case MT_RSOUND:
559    roar_vio_open_fh_socket(&vio, rfh);
560  case MT_ROAR:
561  case MT_OSS:
562  case MT_SIMPLE:
563    roar_vio_close(&vio);
564   break;
565  default:
566    close(rfh);
567 }
568
569 if ( roar_stream_attach_simple(con, stream, 0) == -1 ) {
570  fprintf(stderr, "Can not attach remote stream to local server\n");
571 }
572
573 roar_disconnect(con);
574
575 ROAR_INFO("Stream ID: %i", 1, stream->id);
576
577 return 0;
578}
579
580//ll
Note: See TracBrowser for help on using the repository browser.