source: roaraudio/roarclients/roarctl.c @ 1123:9bc2451b9558

Last change on this file since 1123:9bc2451b9558 was 1123:9bc2451b9558, checked in by phi, 15 years ago

added cmds flag/unflag to help

File size: 15.9 KB
Line 
1//roarctl.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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#include <pwd.h>
27#include <grp.h>
28
29int g_verbose = 0;
30
31int display_mixer (struct roar_connection * con, int stream);
32int show_meta_all (struct roar_connection * con, int id);
33
34void usage (void) {
35 printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n");
36
37 printf("\nOptions:\n\n");
38
39 printf("  --server SERVER         - Set server hostname\n"
40        "  --help                  - Show this help\n"
41        "  --verbose   -v          - Show verbose output\n"
42       );
43
44 printf("\nCommands:\n\n");
45 printf(
46        "  help                    - Show this help\n"
47        "\n"
48        "  standby, off            - Go into standby mode\n"
49        "  resume, on              - Go into active mode\n"
50        "  standbymode             - Show current standby mode\n"
51        "  exit                    - Quits the roard (must be used as last command)\n"
52        "  terminate               - Like exit but let the server up to serve still connected clients,\n"
53        "                            new clients cann't connect and the server terminates after the last\n"
54        "                            client disconnected\n"
55        "\n"
56        "  volume ID CHAN V0 V1... - Sets volume for stream ID\n"
57        "                            CHAN is the number of channels or 'mono' or 'stereo'\n"
58        "                            if mono or stereo is chosen roarctl trys to set\n"
59        "                            sensfull values for all channels even if the output\n"
60        "                            is has more channels.\n"
61        "                            all other args are the volumes of the channels\n"
62        "                            you may use integer or percent values.\n"
63        "                            percent values can flooding points.\n"
64        "\n"
65        "  flag   ID FLAGS         - Sets flags FLAGS on stream ID. FLAGS may be a comma\n"
66        "                            seperated list of flags.\n"
67        "  unflag ID FLAGS         - Unsets flags on a stream. See flag.\n"
68        "\n"
69        "  kick TYPE ID            - Kicks object of TYPE with id ID\n"
70        "                            Types: client stream sample source\n"
71        "\n"
72        "  serveroinfo             - Gets Informations about server output\n"
73        "  listclients             - Gets Informations about clients\n"
74        "  liststreams             - Gets Informations about streams\n"
75        "  allinfo                 - Get all infos\n"
76       );
77}
78
79void server_oinfo (struct roar_connection * con) {
80 struct roar_stream s;
81
82 if ( roar_server_oinfo(con, &s) == -1 ) {
83  fprintf(stderr, "Error: can not get server output info\n");
84  return;
85 }
86
87 printf("Stream direction      : %s\n", roar_dir2str(s.dir));
88 printf("Server Output rate    : %i\n", s.info.rate);
89 printf("Server Output bits    : %i\n", s.info.bits);
90 printf("Server Output channels: %i\n", s.info.channels);
91 printf("Server Output codec   : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
92                                     s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
93// printf("Server Output rate: %i", s.info.rate);
94  if ( g_verbose > 1 )
95   printf("Server Position       : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels));
96}
97
98const char * proc_name (pid_t pid) {
99 static char ret[80] = "?";
100#ifdef __linux__
101 char file[80], buf[80], *r;
102 int  i;
103
104 snprintf(file, 79, "/proc/%i/exe", pid);
105 file[79] = 0;
106
107 ret[0] = '?';
108 ret[1] = 0;
109
110 if ( (i = readlink(file, buf, 79)) != -1 ) {
111  buf[i] = 0;
112  if ( (r = strrchr(buf, '/')) != NULL ) {
113   r++;
114   if ( *r != 0 )
115    strcpy(ret, r);
116  }
117 }
118#endif
119
120 return ret;
121}
122
123void list_clients (struct roar_connection * con) {
124 int i;
125 int num;
126 int h;
127 int id[ROAR_CLIENTS_MAX];
128 struct roar_client c;
129 struct group  * grp = NULL;
130 struct passwd * pwd = NULL;
131
132 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
133  fprintf(stderr, "Error: can not get client list\n");
134  return;
135 }
136
137 for (i = 0; i < num; i++) {
138  printf("client %i:\n", id[i]);
139  if ( roar_get_client(con, &c, id[i]) == -1 ) {
140   fprintf(stderr, "Error: can not get client info\n");
141   continue;
142  }
143  printf("Player name           : %s\n", c.name);
144  printf("Player PID            : %i(%s)\n", c.pid, proc_name(c.pid));
145  if ( c.uid != -1 ) {
146   pwd = getpwuid(c.uid);
147   grp = getgrgid(c.gid);
148   printf("Player UID/GID        : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?");
149  }
150  if ( c.execed != -1 )
151   printf("Execed stream         : %i\n", c.execed);
152
153  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
154   if ( c.streams[h] != -1 )
155    printf("stream                : %i\n", c.streams[h]);
156 }
157
158}
159
160void list_streams (struct roar_connection * con) {
161 int i;
162 int num;
163 int id[ROAR_STREAMS_MAX];
164 struct roar_stream s;
165 struct roar_stream_info info;
166 char flags[80];
167
168
169 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
170  fprintf(stderr, "Error: can not get stream list\n");
171  return;
172 }
173
174 for (i = 0; i < num; i++) {
175  printf("stream %i:\n", id[i]);
176  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
177   fprintf(stderr, "Error: can not get stream info\n");
178   continue;
179  }
180  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
181  if ( s.pos_rel_id == -1 )
182   printf("Relativ position id   : none (stream not synchronized)\n");
183  else
184   printf("Relativ position id   : %i\n", s.pos_rel_id);
185  if ( g_verbose > 1 )
186   printf("Position              : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels));
187  printf("Input rate            : %i\n", s.info.rate);
188  printf("Input bits            : %i\n", s.info.bits);
189  printf("Input channels        : %i\n", s.info.channels);
190  printf("Input codec           : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
191                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
192  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
193   printf("Input codec (streamed): %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
194                                      info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
195   if ( g_verbose ) {
196    printf("Input block size      : %i Byte\n", info.block_size);
197    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
198
199    *flags = 0;
200    if ( info.flags & ROAR_FLAG_PRIMARY )
201     strcat(flags, "primary ");
202    if ( info.flags & ROAR_FLAG_SYNC )
203     strcat(flags, "sync ");
204    if ( info.flags & ROAR_FLAG_OUTPUT )
205     strcat(flags, "output ");
206    if ( info.flags & ROAR_FLAG_SOURCE )
207     strcat(flags, "source ");
208    if ( info.flags & ROAR_FLAG_META )
209     strcat(flags, "meta ");
210
211    printf("Flags                 : %s\n", flags);
212   }
213  }
214  display_mixer(con, id[i]);
215  show_meta_all(con, id[i]);
216 }
217
218}
219
220int display_mixer (struct roar_connection * con, int stream) {
221 int channels;
222 struct roar_mixer_settings mixer;
223 int i;
224
225 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
226  fprintf(stderr, "Error: can not get stream mixer info\n");
227  return -1;
228 }
229
230 for (i = 0; i < channels; i++)
231  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
232
233 return 0;
234}
235
236int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
237 int chans = 0;
238 int id;
239 int i;
240 int len;
241 int old_chans;
242 int vol_l, vol_r;
243 char * k;
244 struct roar_mixer_settings mixer;
245 struct roar_mixer_settings old_mixer;
246
247 if (*cur + 2 > max)
248  return -1;
249
250 id = atoi(arg[++(*cur)]);
251
252 k = arg[++(*cur)];
253
254 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
255  fprintf(stderr, "Error: can not get stream mixer info\n");
256  return -1;
257 }
258
259
260 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
261  chans = 1;
262
263  if ( *cur + 1 > max )
264   return -1;
265
266  k   = arg[++(*cur)];
267  len = strlen(k);
268
269  if ( k[len - 1] == '%' ) {
270   k[len - 1] = 0;
271   vol_l = (atof(k)*65535)/100;
272  } else {
273   vol_l = atoi(k);
274  }
275
276  for (i = 0; i < old_chans; i++)
277   mixer.mixer[i] = vol_l;
278
279  chans = old_chans;
280
281 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
282  chans = 2;
283//  printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans);
284  ROAR_ERR("mode stereo not supported");
285  return -1;
286 } else {
287  if ( strcmp(k, "mono") == 0 ) {
288   chans = 1;
289  } else if ( strcmp(k, "stereo") == 0 ) {
290   chans = 2;
291  } else {
292   chans = atoi(k);
293  }
294
295//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
296
297  if ( *cur + chans > max )
298   return -1;
299
300  for (i = 0; i < chans; i++) {
301   k   = arg[++(*cur)];
302   len = strlen(k);
303
304   if ( k[len - 1] == '%' ) {
305    k[len - 1] = 0;
306    mixer.mixer[i] = (atof(k)*(int)65535)/100;
307   } else {
308    mixer.mixer[i] = atoi(k);
309   }
310  }
311 }
312
313 mixer.scale = 65535;
314
315 return roar_set_vol(con, id, &mixer, chans);
316}
317
318int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
319 struct roar_meta   meta;
320 struct roar_stream s;
321 int mode_i = ROAR_META_MODE_SET;
322
323 s.id = id;
324
325// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
326
327 if ( strcmp(mode, "add") == 0 ) {
328  mode_i = ROAR_META_MODE_ADD;
329 }
330
331 meta.type   = roar_meta_inttype(type);
332 meta.value  = val;
333 meta.key[0] = 0;
334
335 if ( meta.type == -1 ) {
336  fprintf(stderr, "Error: unknown type: %s\n", type);
337  return -1;
338 }
339
340// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
341
342 return roar_stream_meta_set(con, &s, mode_i, &meta);
343}
344
345int show_meta_type (struct roar_connection * con, int id, char * type) {
346 struct roar_meta   meta;
347 struct roar_stream s;
348
349 s.id = id;
350
351 meta.type  = roar_meta_inttype(type);
352
353 if ( meta.type == -1 ) {
354  fprintf(stderr, "Error: unknown type: %s\n", type);
355  return -1;
356 }
357
358 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
359  return -1;
360
361 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
362
363 roar_meta_free(&meta);
364
365 return 0;
366}
367
368int show_meta_all (struct roar_connection * con, int id) {
369 struct roar_stream s;
370 int types[ROAR_META_MAX_PER_STREAM];
371 int i;
372 int len;
373
374 s.id = id;
375
376 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
377  return -1;
378
379 for (i = 0; i < len; i++)
380  show_meta_type(con, id, roar_meta_strtype(types[i]));
381
382 return 0;
383}
384
385int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
386 int f = ROAR_FLAG_NONE;
387 char * c;
388 struct roar_stream s[1];
389
390 memset(s, 0, sizeof(struct roar_stream));
391
392 s->id = id;
393
394 c = strtok(flags, ",");
395 while (c != NULL) {
396  if ( !strcmp(c, "meta") ) {
397   f |= ROAR_FLAG_META;
398  } else if ( !strcmp(c, "primary") ) {
399   f |= ROAR_FLAG_PRIMARY;
400  } else if ( !strcmp(c, "sync") ) {
401   f |= ROAR_FLAG_SYNC;
402  } else {
403   fprintf(stderr, "Error: unknown flag: %s\n", c);
404   return -1;
405  }
406
407  c = strtok(NULL, ",");
408 }
409
410 return roar_stream_set_flags(con, s, f, reset);
411}
412
413int main (int argc, char * argv[]) {
414 struct roar_connection con;
415 char * server   = NULL;
416 char * k = NULL;
417 int    i;
418 int    t = 0;
419
420 for (i = 1; i < argc; i++) {
421  k = argv[i];
422
423  if ( strcmp(k, "--server") == 0 ) {
424   server = argv[++i];
425  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
426   g_verbose++;
427  } else if ( strcmp(k, "--help") == 0 ) {
428   usage();
429   return 0;
430  } else if ( *k == '-' ) {
431   fprintf(stderr, "Error: unknown argument: %s\n", k);
432   usage();
433   return 1;
434  } else {
435   break;
436  }
437 }
438
439 // connect
440
441 if ( roar_connect(&con, server) == -1 ) {
442  fprintf(stderr, "Error: Can not connect to server\n");
443  return 1;
444 }
445
446 if ( roar_identify(&con, "roarctl") == -1 ) {
447  fprintf(stderr, "Error: Can not identify to server\n");
448  return 1;
449 }
450
451 if ( i == argc ) {
452  fprintf(stderr, "Error: No Commands given\n");
453  return 0; // this is not a fatal error...
454 }
455
456 for (; i < argc; i++) {
457  k = argv[i];
458  // cmd is in k
459
460  printf("--- [ %s ] ---\n", k);
461
462  if ( !strcmp(k, "help") ) {
463   usage();
464
465
466  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
467   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
468    fprintf(stderr, "Error: can not set mode to standby\n");
469   } else {
470    printf("going into standby\n");
471   }
472  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
473   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
474    fprintf(stderr, "Error: can not set mode to active\n");
475   } else {
476    printf("going into active mode\n");
477   }
478
479  } else if ( !strcmp(k, "exit") ) {
480   if ( roar_exit(&con) == -1 ) {
481    fprintf(stderr, "Error: can not quit server\n");
482   } else {
483    printf("Server quited\n");
484    break;
485   }
486  } else if ( !strcmp(k, "terminate") ) {
487   if ( roar_terminate(&con, 1) == -1 ) {
488    fprintf(stderr, "Error: can not terminate server\n");
489   } else {
490    printf("Server got asked to quited\n");
491    break;
492   }
493
494  } else if ( !strcmp(k, "standbymode") ) {
495   t = roar_get_standby(&con);
496   if ( t == -1 ) {
497    fprintf(stderr, "Error: can not get stanby mode\n");
498   } else if ( t == ROAR_STANDBY_ACTIVE ) {
499    printf("Server is in standby\n");
500   } else if ( t == ROAR_STANDBY_INACTIVE ) {
501    printf("Server is active\n");
502   } else {
503    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
504   }
505
506  } else if ( !strcmp(k, "serveroinfo") ) {
507   server_oinfo(&con);
508  } else if ( !strcmp(k, "listclients") ) {
509   list_clients(&con);
510  } else if ( !strcmp(k, "liststreams") ) {
511   list_streams(&con);
512  } else if ( !strcmp(k, "allinfo") ) {
513   server_oinfo(&con);
514   printf("\n");
515   list_clients(&con);
516   printf("\n");
517   list_streams(&con);
518
519  } else if ( !strcmp(k, "kick") ) {
520   k = argv[++i];
521   if ( !strcmp(k, "client") ) {
522    t = ROAR_OT_CLIENT;
523   } else if ( !strcmp(k, "stream") ) {
524    t = ROAR_OT_STREAM;
525   } else if ( !strcmp(k, "sample") ) {
526    t = ROAR_OT_SAMPLE;
527   } else if ( !strcmp(k, "source") ) {
528    t = ROAR_OT_SOURCE;
529   } else {
530    fprintf(stderr, "Error: unknown type: %s\n", k);
531    continue;
532   }
533   //t = atoi(argv[i++]);
534   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
535    fprintf(stderr, "Error: can not kick %s\n", k);
536   } else {
537    printf("%s kicked\n", k);
538   }
539
540  } else if ( !strcmp(k, "volume") ) {
541   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
542    fprintf(stderr, "Error: can not set volume\n");
543   } else {
544    printf("volume changed\n");
545   }
546
547  } else if ( !strcmp(k, "flag") ) {
548   i++;
549   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
550    fprintf(stderr, "Error: can not set flags\n");
551   } else {
552    printf("flags changed\n");
553   }
554   i++;
555  } else if ( !strcmp(k, "unflag") ) {
556   i++;
557   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
558    fprintf(stderr, "Error: can not reset flags\n");
559   } else {
560    printf("flags changed\n");
561   }
562   i++;
563  } else if ( !strcmp(k, "metaset") ) {
564   i++;
565   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
566    fprintf(stderr, "Error: can not set meta data\n");
567   } else {
568    printf("meta data changed\n");
569   }
570   i += 3;
571  } else if ( !strcmp(k, "metaget") ) {
572   i++;
573   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
574    fprintf(stderr, "Error: can not get meta data\n");
575   }
576   i++;
577
578  } else {
579   fprintf(stderr, "Error: invalid command: %s\n", k);
580  }
581
582 }
583
584 roar_disconnect(&con);
585
586 return 0;
587}
588
589//ll
Note: See TracBrowser for help on using the repository browser.