source: roaraudio/roarclients/roarctl.c @ 85:261b53488dc2

Last change on this file since 85:261b53488dc2 was 85:261b53488dc2, checked in by phi, 16 years ago

added set_mixer() and volume command to roarctl

File size: 8.6 KB
Line 
1//roarctl.c:
2
3#include <roaraudio.h>
4
5int display_mixer (struct roar_connection * con, int stream);
6
7void usage (void) {
8 printf("roarcat [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n");
9
10 printf("\nOptions:\n\n");
11
12 printf("  --server SERVER    - Set server hostname\n"
13        "  --help             - Show this help\n"
14       );
15
16 printf("\nCommands:\n\n");
17 printf(
18        "  help               - Show this help\n"
19        "\n"
20        "  standby, off       - Go into standby mode\n"
21        "  resume, on         - Go into active mode\n"
22        "  standbymode        - Show current standby mode\n"
23        "  exit               - Quits the roard (must be used as last command)\n"
24        "\n"
25        "  kick TYPE ID       - Kicks object of TYPE with id ID\n"
26        "                       Types: client stream sample source\n"
27        "\n"
28        "  serveroinfo        - Gets Informations about server output\n"
29        "  listclients        - Gets Informations about clients\n"
30        "  liststreams        - Gets Informations about streams\n"
31        "  allinfo            - Get all infos\n"
32       );
33}
34
35void server_oinfo (struct roar_connection * con) {
36 struct roar_stream s;
37
38 if ( roar_server_oinfo(con, &s) == -1 ) {
39  fprintf(stderr, "Error: can not get server output info\n");
40  return;
41 }
42
43 printf("Stream direction      : %s\n", roar_dir2str(s.dir));
44 printf("Server Output rate    : %i\n", s.info.rate);
45 printf("Server Output bits    : %i\n", s.info.bits);
46 printf("Server Output channels: %i\n", s.info.channels);
47 printf("Server Output codec   : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
48                                     s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
49// printf("Server Output rate: %i", s.info.rate);
50}
51
52void list_clients (struct roar_connection * con) {
53 int i;
54 int num;
55 int h;
56 int id[ROAR_CLIENTS_MAX];
57 struct roar_client c;
58
59 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
60  fprintf(stderr, "Error: can not get client list\n");
61  return;
62 }
63
64 for (i = 0; i < num; i++) {
65  printf("client %i:\n", id[i]);
66  if ( roar_get_client(con, &c, id[i]) == -1 ) {
67   fprintf(stderr, "Error: can not get client info\n");
68   continue;
69  }
70  printf("Player name           : %s\n", c.name);
71  if ( c.execed != -1 )
72   printf("Execed stream         : %i\n", c.execed);
73
74  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
75   if ( c.streams[h] != -1 )
76    printf("stream                : %i\n", c.streams[h]);
77 }
78
79}
80
81void list_streams (struct roar_connection * con) {
82 int i;
83 int num;
84 int id[ROAR_STREAMS_MAX];
85 struct roar_stream s;
86
87
88 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
89  fprintf(stderr, "Error: can not get stream list\n");
90  return;
91 }
92
93 for (i = 0; i < num; i++) {
94  printf("stream %i:\n", id[i]);
95  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
96   fprintf(stderr, "Error: can not get stream info\n");
97   continue;
98  }
99  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
100  if ( s.pos_rel_id == -1 )
101   printf("Relativ position id   : none (stream not synchronized)\n");
102  else
103   printf("Relativ position id   : %i\n", s.pos_rel_id);
104  printf("Input rate            : %i\n", s.info.rate);
105  printf("Input bits            : %i\n", s.info.bits);
106  printf("Input channels        : %i\n", s.info.channels);
107  printf("Input codec           : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
108                                      s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
109  display_mixer(con, id[i]);
110 }
111
112}
113
114int display_mixer (struct roar_connection * con, int stream) {
115 int channels;
116 struct roar_mixer_settings mixer;
117 int i;
118
119 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
120  fprintf(stderr, "Error: can not get stream mixer info\n");
121  return -1;
122 }
123
124 for (i = 0; i < channels; i++)
125  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
126
127 return 0;
128}
129
130int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
131 int chans = 0;
132 int id;
133 int i;
134 int len;
135 int old_chans;
136 int vol_l, vol_r;
137 char * k;
138 struct roar_mixer_settings mixer;
139 struct roar_mixer_settings old_mixer;
140
141 if (*cur + 2 > max)
142  return -1;
143
144 id = atoi(arg[++(*cur)]);
145
146 k = arg[++(*cur)];
147
148 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
149  fprintf(stderr, "Error: can not get stream mixer info\n");
150  return -1;
151 }
152
153
154 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
155  chans = 1;
156
157  if ( *cur + 1 > max )
158   return -1;
159
160  k   = arg[++(*cur)];
161  len = strlen(k);
162
163  if ( k[len - 1] == '%' ) {
164   k[len - 1] = 0;
165   vol_l = (atof(k)*65535)/100;
166  } else {
167   vol_l = atoi(k);
168  }
169
170  for (i = 0; i < old_chans; i++)
171   mixer.mixer[i] = vol_l;
172
173  chans = old_chans;
174
175 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
176  chans = 2;
177//  printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans);
178  ROAR_ERR("mode stereo not supported");
179  return -1;
180 } else {
181  if ( strcmp(k, "mono") == 0 ) {
182   chans = 1;
183  } else if ( strcmp(k, "stereo") == 0 ) {
184   chans = 2;
185  } else {
186   chans = atoi(k);
187  }
188
189//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
190
191  if ( *cur + chans > max )
192   return -1;
193
194  for (i = 0; i < chans; i++) {
195   k   = arg[++(*cur)];
196   len = strlen(k);
197
198   if ( k[len - 1] == '%' ) {
199    k[len - 1] = 0;
200    mixer.mixer[i] = (atof(k)*(int)65535)/100;
201   } else {
202    mixer.mixer[i] = atoi(k);
203   }
204  }
205 }
206
207 mixer.scale = 65535;
208
209 return roar_set_vol(con, id, &mixer, chans);
210}
211
212int main (int argc, char * argv[]) {
213 struct roar_connection con;
214 char * server   = NULL;
215 char * k = NULL;
216 int    i;
217 int    t = 0;
218
219 for (i = 1; i < argc; i++) {
220  k = argv[i];
221
222  if ( strcmp(k, "--server") == 0 ) {
223   server = argv[++i];
224  } else if ( strcmp(k, "--help") == 0 ) {
225   usage();
226   return 0;
227  } else if ( *k == '-' ) {
228   fprintf(stderr, "Error: unknown argument: %s\n", k);
229   usage();
230   return 1;
231  } else {
232   break;
233  }
234 }
235
236 // connect
237
238 if ( roar_connect(&con, server) == -1 ) {
239  fprintf(stderr, "Error: Can not connect to server\n");
240  return 1;
241 }
242
243 if ( roar_identify(&con, "roarctl") == -1 ) {
244  fprintf(stderr, "Error: Can not identify to server\n");
245  return 1;
246 }
247
248 if ( i == argc ) {
249  fprintf(stderr, "Error: No Commands given\n");
250  return 0; // this is not a fatal error...
251 }
252
253 for (; i < argc; i++) {
254  k = argv[i];
255  // cmd is in k
256
257  printf("--- [ %s ] ---\n", k);
258
259  if ( !strcmp(k, "help") ) {
260   usage();
261
262
263  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
264   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
265    fprintf(stderr, "Error: can not set mode to standby\n");
266   } else {
267    printf("going into standby\n");
268   }
269  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
270   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
271    fprintf(stderr, "Error: can not set mode to active\n");
272   } else {
273    printf("going into active mode\n");
274   }
275
276  } else if ( !strcmp(k, "exit") ) {
277   if ( roar_exit(&con) == -1 ) {
278    fprintf(stderr, "Error: can not quit server\n");
279   } else {
280    printf("Server quited\n");
281    break;
282   }
283
284  } else if ( !strcmp(k, "standbymode") ) {
285   t = roar_get_standby(&con);
286   if ( t == -1 ) {
287    fprintf(stderr, "Error: can not get stanby mode\n");
288   } else if ( t == ROAR_STANDBY_ACTIVE ) {
289    printf("Server is in standby\n");
290   } else if ( t == ROAR_STANDBY_INACTIVE ) {
291    printf("Server is active\n");
292   } else {
293    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
294   }
295
296  } else if ( !strcmp(k, "serveroinfo") ) {
297   server_oinfo(&con);
298  } else if ( !strcmp(k, "listclients") ) {
299   list_clients(&con);
300  } else if ( !strcmp(k, "liststreams") ) {
301   list_streams(&con);
302  } else if ( !strcmp(k, "allinfo") ) {
303   server_oinfo(&con);
304   printf("\n");
305   list_clients(&con);
306   printf("\n");
307   list_streams(&con);
308
309  } else if ( !strcmp(k, "kick") ) {
310   k = argv[++i];
311   if ( !strcmp(k, "client") ) {
312    t = ROAR_OT_CLIENT;
313   } else if ( !strcmp(k, "stream") ) {
314    t = ROAR_OT_STREAM;
315   } else if ( !strcmp(k, "sample") ) {
316    t = ROAR_OT_SAMPLE;
317   } else if ( !strcmp(k, "source") ) {
318    t = ROAR_OT_SOURCE;
319   } else {
320    fprintf(stderr, "Error: unknown type: %s\n", k);
321    continue;
322   }
323   //t = atoi(argv[i++]);
324   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
325    fprintf(stderr, "Error: can not kick %s\n", k);
326   } else {
327    printf("%s kicked\n", k);
328   }
329
330  } else if ( !strcmp(k, "volume") ) {
331   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
332    fprintf(stderr, "Error: can not set volume\n");
333   } else {
334    printf("volume changed\n");
335   }
336
337  } else {
338   fprintf(stderr, "Error: invalid command: %s\n", k);
339  }
340
341 }
342
343 roar_disconnect(&con);
344
345 return 0;
346}
347
348//ll
Note: See TracBrowser for help on using the repository browser.