source: roaraudio/roarclients/roarctl.c @ 1032:a06815278af3

Last change on this file since 1032:a06815278af3 was 1032:a06815278af3, checked in by phi, 15 years ago

added stream flag META

File size: 14.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 main (int argc, char * argv[]) {
382 struct roar_connection con;
383 char * server   = NULL;
384 char * k = NULL;
385 int    i;
386 int    t = 0;
387
388 for (i = 1; i < argc; i++) {
389  k = argv[i];
390
391  if ( strcmp(k, "--server") == 0 ) {
392   server = argv[++i];
393  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
394   g_verbose++;
395  } else if ( strcmp(k, "--help") == 0 ) {
396   usage();
397   return 0;
398  } else if ( *k == '-' ) {
399   fprintf(stderr, "Error: unknown argument: %s\n", k);
400   usage();
401   return 1;
402  } else {
403   break;
404  }
405 }
406
407 // connect
408
409 if ( roar_connect(&con, server) == -1 ) {
410  fprintf(stderr, "Error: Can not connect to server\n");
411  return 1;
412 }
413
414 if ( roar_identify(&con, "roarctl") == -1 ) {
415  fprintf(stderr, "Error: Can not identify to server\n");
416  return 1;
417 }
418
419 if ( i == argc ) {
420  fprintf(stderr, "Error: No Commands given\n");
421  return 0; // this is not a fatal error...
422 }
423
424 for (; i < argc; i++) {
425  k = argv[i];
426  // cmd is in k
427
428  printf("--- [ %s ] ---\n", k);
429
430  if ( !strcmp(k, "help") ) {
431   usage();
432
433
434  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
435   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
436    fprintf(stderr, "Error: can not set mode to standby\n");
437   } else {
438    printf("going into standby\n");
439   }
440  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
441   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
442    fprintf(stderr, "Error: can not set mode to active\n");
443   } else {
444    printf("going into active mode\n");
445   }
446
447  } else if ( !strcmp(k, "exit") ) {
448   if ( roar_exit(&con) == -1 ) {
449    fprintf(stderr, "Error: can not quit server\n");
450   } else {
451    printf("Server quited\n");
452    break;
453   }
454  } else if ( !strcmp(k, "terminate") ) {
455   if ( roar_terminate(&con, 1) == -1 ) {
456    fprintf(stderr, "Error: can not terminate server\n");
457   } else {
458    printf("Server got asked to quited\n");
459    break;
460   }
461
462  } else if ( !strcmp(k, "standbymode") ) {
463   t = roar_get_standby(&con);
464   if ( t == -1 ) {
465    fprintf(stderr, "Error: can not get stanby mode\n");
466   } else if ( t == ROAR_STANDBY_ACTIVE ) {
467    printf("Server is in standby\n");
468   } else if ( t == ROAR_STANDBY_INACTIVE ) {
469    printf("Server is active\n");
470   } else {
471    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
472   }
473
474  } else if ( !strcmp(k, "serveroinfo") ) {
475   server_oinfo(&con);
476  } else if ( !strcmp(k, "listclients") ) {
477   list_clients(&con);
478  } else if ( !strcmp(k, "liststreams") ) {
479   list_streams(&con);
480  } else if ( !strcmp(k, "allinfo") ) {
481   server_oinfo(&con);
482   printf("\n");
483   list_clients(&con);
484   printf("\n");
485   list_streams(&con);
486
487  } else if ( !strcmp(k, "kick") ) {
488   k = argv[++i];
489   if ( !strcmp(k, "client") ) {
490    t = ROAR_OT_CLIENT;
491   } else if ( !strcmp(k, "stream") ) {
492    t = ROAR_OT_STREAM;
493   } else if ( !strcmp(k, "sample") ) {
494    t = ROAR_OT_SAMPLE;
495   } else if ( !strcmp(k, "source") ) {
496    t = ROAR_OT_SOURCE;
497   } else {
498    fprintf(stderr, "Error: unknown type: %s\n", k);
499    continue;
500   }
501   //t = atoi(argv[i++]);
502   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
503    fprintf(stderr, "Error: can not kick %s\n", k);
504   } else {
505    printf("%s kicked\n", k);
506   }
507
508  } else if ( !strcmp(k, "volume") ) {
509   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
510    fprintf(stderr, "Error: can not set volume\n");
511   } else {
512    printf("volume changed\n");
513   }
514
515  } else if ( !strcmp(k, "metaset") ) {
516   i++;
517   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
518    fprintf(stderr, "Error: can not set meta data\n");
519   } else {
520    printf("meta data changed\n");
521   }
522   i += 3;
523  } else if ( !strcmp(k, "metaget") ) {
524   i++;
525   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
526    fprintf(stderr, "Error: can not get meta data\n");
527   }
528   i++;
529
530  } else {
531   fprintf(stderr, "Error: invalid command: %s\n", k);
532  }
533
534 }
535
536 roar_disconnect(&con);
537
538 return 0;
539}
540
541//ll
Note: See TracBrowser for help on using the repository browser.