source: roaraudio/roarclients/roarctl.c @ 1043:1ca27ba72398

Last change on this file since 1043:1ca27ba72398 was 1043:1ca27ba72398, checked in by phi, 15 years ago

added support to set flags via roarctl, update meta data on change of meta flag

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