source: roaraudio/roarclients/roarinterconnect.c @ 3121:0ed0acfadd4f

Last change on this file since 3121:0ed0acfadd4f was 3121:0ed0acfadd4f, checked in by phi, 12 years ago

set default remote server to +slp

File size: 7.3 KB
Line 
1//roarinterconnect.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
27#ifdef ROAR_HAVE_ESD
28#include <esd.h>
29#endif
30
31#define MT_NONE     0x00
32#define MT_MASK     0xF0
33#define MT_ROAR     0x10
34#define MT_ESD      0x20
35#define MT_DEFAULT  MT_ROAR
36
37#define ST_NONE     0x00
38#define ST_MASK     0x0F
39#define ST_BIDIR    0x01
40#define ST_FILTER   0x02
41#define ST_TRANSMIT 0x03
42#define ST_RECEIVE  0x04
43// no default here as the default depend on the server type
44
45void usage (void) {
46 printf("roarinterconnect [OPTIONS]...\n");
47
48 printf("\nOptions:\n\n");
49
50 printf("  --server SERVER    - Set server hostname\n"
51        "  --remote SERVER    - Set remote server\n"
52        "  --type   TYPE      - Set type of remote server\n"
53        "  --rate   RATE      - Set sample rate\n"
54        "  --bits   BITS      - Set bits per sample\n"
55        "  --chans  CHANNELS  - Set number of channels\n"
56        "  --codec  CODEC     - Set the codec\n"
57        "  --help             - Show this help\n"
58       );
59
60 printf("\nPossible Types:\n\n");
61 printf("  roar               - RoarAudio Server\n"
62        "  esd                - EsounD Server\n"
63        "\n"
64        "  bidir              - Connect bidirectional\n"
65        "  filter             - Use local server as filter for remote server\n"
66        "  transmit           - Transmit data from local server to remote server\n"
67        "  receive            - Receive data from remote server\n"
68       );
69
70}
71
72int parse_type (char * type) {
73 int ret = MT_NONE|ST_NONE;
74 char * colon;
75
76 if ( type != NULL ) {
77  while (type != NULL && *type) {
78   if ( (colon = strstr(type, ":")) != NULL ) {
79    *colon = 0;
80     colon++;
81   }
82
83   if ( !strcmp(type, "roar") ) {
84    ret -= ret & MT_MASK;
85    ret += MT_ROAR;
86   } else if ( !strcmp(type, "esd") ) {
87    ret -= ret & MT_MASK;
88    ret += MT_ESD;
89   } else if ( !strcmp(type, "bidir") ) {
90    ret -= ret & ST_MASK;
91    ret += ST_BIDIR;
92   } else if ( !strcmp(type, "filter") ) {
93    ret -= ret & ST_MASK;
94    ret += ST_FILTER;
95   } else if ( !strcmp(type, "transmit") ) {
96    ret -= ret & ST_MASK;
97    ret += ST_TRANSMIT;
98   } else if ( !strcmp(type, "receive") ) {
99    ret -= ret & ST_MASK;
100    ret += ST_RECEIVE;
101   } else {
102    return MT_NONE|ST_NONE;
103   }
104
105   type = colon;
106  }
107 }
108
109 if ( (ret & MT_MASK) == MT_NONE )
110  ret |= MT_DEFAULT;
111
112 if ( (ret & ST_MASK) == ST_NONE ) {
113  switch (ret & MT_MASK) {
114   case MT_ROAR: ret |= ST_BIDIR;  break;
115   case MT_ESD:  ret |= ST_FILTER; break;
116   default:
117     return MT_NONE|ST_NONE; // error case
118    break;
119  }
120 }
121
122 return ret;
123}
124
125int main (int argc, char * argv[]) {
126 struct roar_connection con[1];
127 struct roar_stream     stream[1];
128 int    rate     = 44100;
129 int    bits     = 16;
130 int    channels = 2;
131 int    codec    = ROAR_CODEC_DEFAULT;
132 int    type     = parse_type(NULL);
133 int    tmp;
134 char * server   = NULL;
135 char * remote   = "+slp"; // we hope SLP located server is not local one
136 char * k;
137 int    rfh;
138 int    i;
139 int    localdir = ROAR_DIR_BIDIR;
140
141 for (i = 1; i < argc; i++) {
142  k = argv[i];
143
144  if ( strcmp(k, "--server") == 0 ) {
145   server = argv[++i];
146  } else if ( strcmp(k, "--remote") == 0 ) {
147   remote = argv[++i];
148  } else if ( strcmp(k, "--type") == 0 ) {
149   type = parse_type(argv[++i]);
150  } else if ( strcmp(k, "--rate") == 0 ) {
151   rate = atoi(argv[++i]);
152  } else if ( strcmp(k, "--bits") == 0 ) {
153   bits = atoi(argv[++i]);
154  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
155   channels = atoi(argv[++i]);
156  } else if ( strcmp(k, "--codec") == 0 ) {
157   codec = roar_str2codec(argv[++i]);
158  } else if ( strcmp(k, "--help") == 0 ) {
159   usage();
160   return 0;
161  } else {
162   fprintf(stderr, "Error: unknown argument: %s\n", k);
163   usage();
164   return 1;
165  }
166 }
167
168 switch (type & MT_MASK) {
169  case MT_ROAR:
170    switch (type & ST_MASK) {
171     case ST_BIDIR:
172       tmp      = ROAR_DIR_BIDIR;
173      break;
174     case ST_FILTER:
175       tmp      = ROAR_DIR_FILTER;
176      break;
177     case ST_TRANSMIT:
178       tmp      = ROAR_DIR_PLAY;
179       localdir = ROAR_DIR_MONITOR;
180      break;
181     case ST_RECEIVE:
182       tmp      = ROAR_DIR_MONITOR;
183       localdir = ROAR_DIR_PLAY;
184      break;
185     default:
186       fprintf(stderr, "Error: unknown stream type\n");
187       return 2;
188    }
189    rfh = roar_simple_stream(rate, channels, bits, codec, remote, tmp, "roarinterconnect");
190   break;
191#ifdef ROAR_HAVE_ESD
192  case MT_ESD:
193    tmp = ESD_STREAM|ESD_PLAY;
194
195    switch (bits) {
196     case  8: tmp |= ESD_BITS8;  break;
197     case 16: tmp |= ESD_BITS16; break;
198     default:
199       fprintf(stderr, "Error: EsounD only supports 8 and 16 bit streams\n");
200       return 2;
201    }
202
203    switch (channels) {
204     case 1: tmp |= ESD_MONO;   break;
205     case 2: tmp |= ESD_STEREO; break;
206     default:
207       fprintf(stderr, "Error: EsounD only supports mono and stereo streams\n");
208       return 2;
209    }
210
211    // this is only true if the esd runs on a LE system,...
212    if ( bits == 8 && codec != ROAR_CODEC_PCM_U_LE ) {
213     fprintf(stderr, "Error: EsounD only supports unsigned PCM in 8 bit mode\n");
214     return 2;
215    } else if ( bits == 16 && codec != ROAR_CODEC_DEFAULT ) {
216     fprintf(stderr, "Error: EsounD only supports signed PCM in 16 bit mode\n");
217     return 2;
218    }
219
220    switch (type & ST_MASK) {
221     case ST_FILTER:
222       rfh = esd_filter_stream(tmp, rate, remote, "roarinterconnect");
223      break;
224     case ST_TRANSMIT:
225       rfh = esd_play_stream(tmp, rate, remote, "roarinterconnect");
226      break;
227     case ST_RECEIVE:
228       rfh = esd_monitor_stream(tmp, rate, remote, "roarinterconnect");
229      break;
230     default:
231       fprintf(stderr, "Error: this type is not supported by EsounD\n");
232       return 2;
233      break;
234    }
235   break;
236#endif
237  default:
238    fprintf(stderr, "Error: unknown/not supported server type\n");
239    return 2;
240 }
241
242 if ( rfh == -1 ) {
243  fprintf(stderr, "Error: can not connect to remote server\n");
244  return 10;
245 }
246
247 if ( roar_simple_connect(con, server, "roarinterconnect") == -1 ) {
248  fprintf(stderr, "Can not connect to local server\n");
249  return 20;
250 }
251
252 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) {
253  roar_disconnect(con);
254  return 21;
255 }
256
257 if ( roar_stream_connect(con, stream, localdir) == -1 ) {
258  roar_disconnect(con);
259  return 22;
260 }
261
262 if ( roar_stream_passfh(con, stream, rfh) == -1 ) {
263  roar_disconnect(con);
264  return 23;
265 }
266
267 roar_simple_close(rfh);
268
269 if ( roar_stream_attach_simple(con, stream, 0) == -1 ) {
270  fprintf(stderr, "Can not attach remote stream to local server\n");
271 }
272
273 roar_disconnect(con);
274
275 return 0;
276}
277
278//ll
Note: See TracBrowser for help on using the repository browser.