source: roaraudio/roarclients/roarctl.c @ 964:0a0e4e32d700

Last change on this file since 964:0a0e4e32d700 was 964:0a0e4e32d700, checked in by phi, 15 years ago

added support for roarctl to show position info

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