source: roaraudio/roarclients/roarinterconnect.c @ 4885:01d694a7fdf7

Last change on this file since 4885:01d694a7fdf7 was 4885:01d694a7fdf7, checked in by phi, 13 years ago

Some cleanup of roarclients to fix ckport warnings

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