source: roaraudio/roarclients/roarinterconnect.c @ 2904:0b2b372fc50f

Last change on this file since 2904:0b2b372fc50f was 2904:0b2b372fc50f, checked in by phi, 15 years ago

added directions transmit/receive, works only for roar streams at the moment

File size: 6.5 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}
61
62int parse_type (char * type) {
63 int ret = MT_NONE|ST_NONE;
64 char * colon;
65
66 if ( type != NULL ) {
67  while (type != NULL && *type) {
68   if ( (colon = strstr(type, ":")) != NULL ) {
69    *colon = 0;
70     colon++;
71   }
72
73   if ( !strcmp(type, "roar") ) {
74    ret -= ret & MT_MASK;
75    ret += MT_ROAR;
76   } else if ( !strcmp(type, "esd") ) {
77    ret -= ret & MT_MASK;
78    ret += MT_ESD;
79   } else if ( !strcmp(type, "bidir") ) {
80    ret -= ret & ST_MASK;
81    ret += ST_BIDIR;
82   } else if ( !strcmp(type, "filter") ) {
83    ret -= ret & ST_MASK;
84    ret += ST_FILTER;
85   } else if ( !strcmp(type, "transmit") ) {
86    ret -= ret & ST_MASK;
87    ret += ST_TRANSMIT;
88   } else if ( !strcmp(type, "receive") ) {
89    ret -= ret & ST_MASK;
90    ret += ST_RECEIVE;
91   } else {
92    return MT_NONE|ST_NONE;
93   }
94
95   type = colon;
96  }
97 }
98
99 if ( (ret & MT_MASK) == MT_NONE )
100  ret |= MT_DEFAULT;
101
102 if ( (ret & ST_MASK) == ST_NONE ) {
103  switch (ret & MT_MASK) {
104   case MT_ROAR: ret |= ST_BIDIR;  break;
105   case MT_ESD:  ret |= ST_FILTER; break;
106   default:
107     return MT_NONE|ST_NONE; // error case
108    break;
109  }
110 }
111
112 return ret;
113}
114
115int main (int argc, char * argv[]) {
116 struct roar_connection con[1];
117 struct roar_stream     stream[1];
118 int    rate     = 44100;
119 int    bits     = 16;
120 int    channels = 2;
121 int    codec    = ROAR_CODEC_DEFAULT;
122 int    type     = parse_type(NULL);
123 int    tmp;
124 char * server   = NULL;
125 char * remote   = NULL;
126 char * k;
127 int    rfh;
128 int    i;
129 int    localdir = ROAR_DIR_BIDIR;
130
131 for (i = 1; i < argc; i++) {
132  k = argv[i];
133
134  if ( strcmp(k, "--server") == 0 ) {
135   server = argv[++i];
136  } else if ( strcmp(k, "--remote") == 0 ) {
137   remote = argv[++i];
138  } else if ( strcmp(k, "--type") == 0 ) {
139   type = parse_type(argv[++i]);
140  } else if ( strcmp(k, "--rate") == 0 ) {
141   rate = atoi(argv[++i]);
142  } else if ( strcmp(k, "--bits") == 0 ) {
143   bits = atoi(argv[++i]);
144  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
145   channels = atoi(argv[++i]);
146  } else if ( strcmp(k, "--codec") == 0 ) {
147   codec = roar_str2codec(argv[++i]);
148  } else if ( strcmp(k, "--help") == 0 ) {
149   usage();
150   return 0;
151  } else {
152   fprintf(stderr, "Error: unknown argument: %s\n", k);
153   usage();
154   return 1;
155  }
156 }
157
158 switch (type & MT_MASK) {
159  case MT_ROAR:
160    switch (type & ST_MASK) {
161     case ST_BIDIR:
162       tmp      = ROAR_DIR_BIDIR;
163      break;
164     case ST_FILTER:
165       tmp      = ROAR_DIR_FILTER;
166      break;
167     case ST_TRANSMIT:
168       tmp      = ROAR_DIR_PLAY;
169       localdir = ROAR_DIR_MONITOR;
170      break;
171     case ST_RECEIVE:
172       tmp      = ROAR_DIR_MONITOR;
173       localdir = ROAR_DIR_PLAY;
174      break;
175     default:
176       fprintf(stderr, "Error: unknown stream type\n");
177       return 2;
178    }
179    rfh = roar_simple_stream(rate, channels, bits, codec, remote, tmp, "roarinterconnect");
180   break;
181#ifdef ROAR_HAVE_ESD
182  case MT_ESD:
183    if ( (type & ST_MASK) != ST_FILTER ) {
184     fprintf(stderr, "Error: server type only supports stream type filter\n");
185     return 2;
186    }
187
188    tmp = ESD_STREAM|ESD_PLAY;
189
190    switch (bits) {
191     case  8: tmp |= ESD_BITS8;  break;
192     case 16: tmp |= ESD_BITS16; break;
193     default:
194       fprintf(stderr, "Error: EsounD only supports 8 and 16 bit streams\n");
195       return 2;
196    }
197
198    switch (channels) {
199     case 1: tmp |= ESD_MONO;   break;
200     case 2: tmp |= ESD_STEREO; break;
201     default:
202       fprintf(stderr, "Error: EsounD only supports mono and stereo streams\n");
203       return 2;
204    }
205
206    // this is only true if the esd runs on a LE system,...
207    if ( bits == 8 && codec != ROAR_CODEC_PCM_U_LE ) {
208     fprintf(stderr, "Error: EsounD only supports unsigned PCM in 8 bit mode\n");
209     return 2;
210    } else if ( bits == 16 && codec != ROAR_CODEC_DEFAULT ) {
211     fprintf(stderr, "Error: EsounD only supports signed PCM in 16 bit mode\n");
212     return 2;
213    }
214
215    rfh = esd_filter_stream(tmp, rate, remote, "roarinterconnect");
216   break;
217#endif
218  default:
219    fprintf(stderr, "Error: unknown/not supported server type\n");
220    return 2;
221 }
222
223 if ( rfh == -1 ) {
224  fprintf(stderr, "Error: can not connect to remote server\n");
225  return 10;
226 }
227
228 if ( roar_simple_connect(con, server, "roarinterconnect") == -1 ) {
229  fprintf(stderr, "Can not connect to local server\n");
230  return 20;
231 }
232
233 if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) {
234  roar_disconnect(con);
235  return 21;
236 }
237
238 if ( roar_stream_connect(con, stream, localdir) == -1 ) {
239  roar_disconnect(con);
240  return 22;
241 }
242
243 if ( roar_stream_passfh(con, stream, rfh) == -1 ) {
244  roar_disconnect(con);
245  return 23;
246 }
247
248 roar_simple_close(rfh);
249
250 if ( roar_stream_attach_simple(con, stream, 0) == -1 ) {
251  fprintf(stderr, "Can not attach remote stream to local server\n");
252 }
253
254 roar_disconnect(con);
255
256 return 0;
257}
258
259//ll
Note: See TracBrowser for help on using the repository browser.