source: roaraudio/roarclients/roarctl.c @ 435:7f7f7570942d

Last change on this file since 435:7f7f7570942d was 435:7f7f7570942d, checked in by phi, 16 years ago

added support for PID

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  printf("Player PID            : %i\n", c.pid);
82  if ( c.execed != -1 )
83   printf("Execed stream         : %i\n", c.execed);
84
85  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
86   if ( c.streams[h] != -1 )
87    printf("stream                : %i\n", c.streams[h]);
88 }
89
90}
91
92void list_streams (struct roar_connection * con) {
93 int i;
94 int num;
95 int id[ROAR_STREAMS_MAX];
96 struct roar_stream s;
97
98
99 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
100  fprintf(stderr, "Error: can not get stream list\n");
101  return;
102 }
103
104 for (i = 0; i < num; i++) {
105  printf("stream %i:\n", id[i]);
106  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
107   fprintf(stderr, "Error: can not get stream info\n");
108   continue;
109  }
110  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
111  if ( s.pos_rel_id == -1 )
112   printf("Relativ position id   : none (stream not synchronized)\n");
113  else
114   printf("Relativ position id   : %i\n", s.pos_rel_id);
115  printf("Input rate            : %i\n", s.info.rate);
116  printf("Input bits            : %i\n", s.info.bits);
117  printf("Input channels        : %i\n", s.info.channels);
118  printf("Input codec           : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
119                                      s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
120  display_mixer(con, id[i]);
121  show_meta_all(con, id[i]);
122 }
123
124}
125
126int display_mixer (struct roar_connection * con, int stream) {
127 int channels;
128 struct roar_mixer_settings mixer;
129 int i;
130
131 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
132  fprintf(stderr, "Error: can not get stream mixer info\n");
133  return -1;
134 }
135
136 for (i = 0; i < channels; i++)
137  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
138
139 return 0;
140}
141
142int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
143 int chans = 0;
144 int id;
145 int i;
146 int len;
147 int old_chans;
148 int vol_l, vol_r;
149 char * k;
150 struct roar_mixer_settings mixer;
151 struct roar_mixer_settings old_mixer;
152
153 if (*cur + 2 > max)
154  return -1;
155
156 id = atoi(arg[++(*cur)]);
157
158 k = arg[++(*cur)];
159
160 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
161  fprintf(stderr, "Error: can not get stream mixer info\n");
162  return -1;
163 }
164
165
166 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
167  chans = 1;
168
169  if ( *cur + 1 > max )
170   return -1;
171
172  k   = arg[++(*cur)];
173  len = strlen(k);
174
175  if ( k[len - 1] == '%' ) {
176   k[len - 1] = 0;
177   vol_l = (atof(k)*65535)/100;
178  } else {
179   vol_l = atoi(k);
180  }
181
182  for (i = 0; i < old_chans; i++)
183   mixer.mixer[i] = vol_l;
184
185  chans = old_chans;
186
187 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
188  chans = 2;
189//  printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans);
190  ROAR_ERR("mode stereo not supported");
191  return -1;
192 } else {
193  if ( strcmp(k, "mono") == 0 ) {
194   chans = 1;
195  } else if ( strcmp(k, "stereo") == 0 ) {
196   chans = 2;
197  } else {
198   chans = atoi(k);
199  }
200
201//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
202
203  if ( *cur + chans > max )
204   return -1;
205
206  for (i = 0; i < chans; i++) {
207   k   = arg[++(*cur)];
208   len = strlen(k);
209
210   if ( k[len - 1] == '%' ) {
211    k[len - 1] = 0;
212    mixer.mixer[i] = (atof(k)*(int)65535)/100;
213   } else {
214    mixer.mixer[i] = atoi(k);
215   }
216  }
217 }
218
219 mixer.scale = 65535;
220
221 return roar_set_vol(con, id, &mixer, chans);
222}
223
224int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
225 struct roar_meta   meta;
226 struct roar_stream s;
227 int mode_i = ROAR_META_MODE_SET;
228
229 s.id = id;
230
231// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
232
233 if ( strcmp(mode, "add") == 0 ) {
234  mode_i = ROAR_META_MODE_ADD;
235 }
236
237 meta.type   = roar_meta_inttype(type);
238 meta.value  = val;
239 meta.key[0] = 0;
240
241 if ( meta.type == -1 ) {
242  fprintf(stderr, "Error: unknown type: %s\n", type);
243  return -1;
244 }
245
246// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
247
248 return roar_stream_meta_set(con, &s, mode_i, &meta);
249}
250
251int show_meta_type (struct roar_connection * con, int id, char * type) {
252 struct roar_meta   meta;
253 struct roar_stream s;
254
255 s.id = id;
256
257 meta.type  = roar_meta_inttype(type);
258
259 if ( meta.type == -1 ) {
260  fprintf(stderr, "Error: unknown type: %s\n", type);
261  return -1;
262 }
263
264 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
265  return -1;
266
267 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
268
269 roar_meta_free(&meta);
270
271 return 0;
272}
273
274int show_meta_all (struct roar_connection * con, int id) {
275 struct roar_stream s;
276 int types[ROAR_META_MAX_PER_STREAM];
277 int i;
278 int len;
279
280 s.id = id;
281
282 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
283  return -1;
284
285 for (i = 0; i < len; i++)
286  show_meta_type(con, id, roar_meta_strtype(types[i]));
287
288 return 0;
289}
290
291int main (int argc, char * argv[]) {
292 struct roar_connection con;
293 char * server   = NULL;
294 char * k = NULL;
295 int    i;
296 int    t = 0;
297
298 for (i = 1; i < argc; i++) {
299  k = argv[i];
300
301  if ( strcmp(k, "--server") == 0 ) {
302   server = argv[++i];
303  } else if ( strcmp(k, "--help") == 0 ) {
304   usage();
305   return 0;
306  } else if ( *k == '-' ) {
307   fprintf(stderr, "Error: unknown argument: %s\n", k);
308   usage();
309   return 1;
310  } else {
311   break;
312  }
313 }
314
315 // connect
316
317 if ( roar_connect(&con, server) == -1 ) {
318  fprintf(stderr, "Error: Can not connect to server\n");
319  return 1;
320 }
321
322 if ( roar_identify(&con, "roarctl") == -1 ) {
323  fprintf(stderr, "Error: Can not identify to server\n");
324  return 1;
325 }
326
327 if ( i == argc ) {
328  fprintf(stderr, "Error: No Commands given\n");
329  return 0; // this is not a fatal error...
330 }
331
332 for (; i < argc; i++) {
333  k = argv[i];
334  // cmd is in k
335
336  printf("--- [ %s ] ---\n", k);
337
338  if ( !strcmp(k, "help") ) {
339   usage();
340
341
342  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
343   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
344    fprintf(stderr, "Error: can not set mode to standby\n");
345   } else {
346    printf("going into standby\n");
347   }
348  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
349   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
350    fprintf(stderr, "Error: can not set mode to active\n");
351   } else {
352    printf("going into active mode\n");
353   }
354
355  } else if ( !strcmp(k, "exit") ) {
356   if ( roar_exit(&con) == -1 ) {
357    fprintf(stderr, "Error: can not quit server\n");
358   } else {
359    printf("Server quited\n");
360    break;
361   }
362
363  } else if ( !strcmp(k, "standbymode") ) {
364   t = roar_get_standby(&con);
365   if ( t == -1 ) {
366    fprintf(stderr, "Error: can not get stanby mode\n");
367   } else if ( t == ROAR_STANDBY_ACTIVE ) {
368    printf("Server is in standby\n");
369   } else if ( t == ROAR_STANDBY_INACTIVE ) {
370    printf("Server is active\n");
371   } else {
372    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
373   }
374
375  } else if ( !strcmp(k, "serveroinfo") ) {
376   server_oinfo(&con);
377  } else if ( !strcmp(k, "listclients") ) {
378   list_clients(&con);
379  } else if ( !strcmp(k, "liststreams") ) {
380   list_streams(&con);
381  } else if ( !strcmp(k, "allinfo") ) {
382   server_oinfo(&con);
383   printf("\n");
384   list_clients(&con);
385   printf("\n");
386   list_streams(&con);
387
388  } else if ( !strcmp(k, "kick") ) {
389   k = argv[++i];
390   if ( !strcmp(k, "client") ) {
391    t = ROAR_OT_CLIENT;
392   } else if ( !strcmp(k, "stream") ) {
393    t = ROAR_OT_STREAM;
394   } else if ( !strcmp(k, "sample") ) {
395    t = ROAR_OT_SAMPLE;
396   } else if ( !strcmp(k, "source") ) {
397    t = ROAR_OT_SOURCE;
398   } else {
399    fprintf(stderr, "Error: unknown type: %s\n", k);
400    continue;
401   }
402   //t = atoi(argv[i++]);
403   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
404    fprintf(stderr, "Error: can not kick %s\n", k);
405   } else {
406    printf("%s kicked\n", k);
407   }
408
409  } else if ( !strcmp(k, "volume") ) {
410   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
411    fprintf(stderr, "Error: can not set volume\n");
412   } else {
413    printf("volume changed\n");
414   }
415
416  } else if ( !strcmp(k, "metaset") ) {
417   i++;
418   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
419    fprintf(stderr, "Error: can not set meta data\n");
420   } else {
421    printf("meta data changed\n");
422   }
423   i += 3;
424  } else if ( !strcmp(k, "metaget") ) {
425   i++;
426   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
427    fprintf(stderr, "Error: can not get meta data\n");
428   }
429   i++;
430
431  } else {
432   fprintf(stderr, "Error: invalid command: %s\n", k);
433  }
434
435 }
436
437 roar_disconnect(&con);
438
439 return 0;
440}
441
442//ll
Note: See TracBrowser for help on using the repository browser.