source: roaraudio/roarclients/roarinterconnect.c @ 2260:bf80036b2d54

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

check for codecs

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