source: roaraudio/roarclients/roarctl.c @ 233:01c44c8eecf8

Last change on this file since 233:01c44c8eecf8 was 233:01c44c8eecf8, checked in by phi, 16 years ago

changed the name of the tool to the rigth one in help ;)

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