source: roaraudio/roarclients/roarctl.c @ 984:edaf654b9f30

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

print server possition in very verbose mode

File size: 14.5 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
205    printf("Flags                 : %s\n", flags);
206   }
207  }
208  display_mixer(con, id[i]);
209  show_meta_all(con, id[i]);
210 }
211
212}
213
214int display_mixer (struct roar_connection * con, int stream) {
215 int channels;
216 struct roar_mixer_settings mixer;
217 int i;
218
219 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
220  fprintf(stderr, "Error: can not get stream mixer info\n");
221  return -1;
222 }
223
224 for (i = 0; i < channels; i++)
225  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
226
227 return 0;
228}
229
230int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
231 int chans = 0;
232 int id;
233 int i;
234 int len;
235 int old_chans;
236 int vol_l, vol_r;
237 char * k;
238 struct roar_mixer_settings mixer;
239 struct roar_mixer_settings old_mixer;
240
241 if (*cur + 2 > max)
242  return -1;
243
244 id = atoi(arg[++(*cur)]);
245
246 k = arg[++(*cur)];
247
248 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
249  fprintf(stderr, "Error: can not get stream mixer info\n");
250  return -1;
251 }
252
253
254 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
255  chans = 1;
256
257  if ( *cur + 1 > max )
258   return -1;
259
260  k   = arg[++(*cur)];
261  len = strlen(k);
262
263  if ( k[len - 1] == '%' ) {
264   k[len - 1] = 0;
265   vol_l = (atof(k)*65535)/100;
266  } else {
267   vol_l = atoi(k);
268  }
269
270  for (i = 0; i < old_chans; i++)
271   mixer.mixer[i] = vol_l;
272
273  chans = old_chans;
274
275 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
276  chans = 2;
277//  printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans);
278  ROAR_ERR("mode stereo not supported");
279  return -1;
280 } else {
281  if ( strcmp(k, "mono") == 0 ) {
282   chans = 1;
283  } else if ( strcmp(k, "stereo") == 0 ) {
284   chans = 2;
285  } else {
286   chans = atoi(k);
287  }
288
289//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
290
291  if ( *cur + chans > max )
292   return -1;
293
294  for (i = 0; i < chans; i++) {
295   k   = arg[++(*cur)];
296   len = strlen(k);
297
298   if ( k[len - 1] == '%' ) {
299    k[len - 1] = 0;
300    mixer.mixer[i] = (atof(k)*(int)65535)/100;
301   } else {
302    mixer.mixer[i] = atoi(k);
303   }
304  }
305 }
306
307 mixer.scale = 65535;
308
309 return roar_set_vol(con, id, &mixer, chans);
310}
311
312int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
313 struct roar_meta   meta;
314 struct roar_stream s;
315 int mode_i = ROAR_META_MODE_SET;
316
317 s.id = id;
318
319// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
320
321 if ( strcmp(mode, "add") == 0 ) {
322  mode_i = ROAR_META_MODE_ADD;
323 }
324
325 meta.type   = roar_meta_inttype(type);
326 meta.value  = val;
327 meta.key[0] = 0;
328
329 if ( meta.type == -1 ) {
330  fprintf(stderr, "Error: unknown type: %s\n", type);
331  return -1;
332 }
333
334// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
335
336 return roar_stream_meta_set(con, &s, mode_i, &meta);
337}
338
339int show_meta_type (struct roar_connection * con, int id, char * type) {
340 struct roar_meta   meta;
341 struct roar_stream s;
342
343 s.id = id;
344
345 meta.type  = roar_meta_inttype(type);
346
347 if ( meta.type == -1 ) {
348  fprintf(stderr, "Error: unknown type: %s\n", type);
349  return -1;
350 }
351
352 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
353  return -1;
354
355 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
356
357 roar_meta_free(&meta);
358
359 return 0;
360}
361
362int show_meta_all (struct roar_connection * con, int id) {
363 struct roar_stream s;
364 int types[ROAR_META_MAX_PER_STREAM];
365 int i;
366 int len;
367
368 s.id = id;
369
370 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
371  return -1;
372
373 for (i = 0; i < len; i++)
374  show_meta_type(con, id, roar_meta_strtype(types[i]));
375
376 return 0;
377}
378
379int main (int argc, char * argv[]) {
380 struct roar_connection con;
381 char * server   = NULL;
382 char * k = NULL;
383 int    i;
384 int    t = 0;
385
386 for (i = 1; i < argc; i++) {
387  k = argv[i];
388
389  if ( strcmp(k, "--server") == 0 ) {
390   server = argv[++i];
391  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
392   g_verbose++;
393  } else if ( strcmp(k, "--help") == 0 ) {
394   usage();
395   return 0;
396  } else if ( *k == '-' ) {
397   fprintf(stderr, "Error: unknown argument: %s\n", k);
398   usage();
399   return 1;
400  } else {
401   break;
402  }
403 }
404
405 // connect
406
407 if ( roar_connect(&con, server) == -1 ) {
408  fprintf(stderr, "Error: Can not connect to server\n");
409  return 1;
410 }
411
412 if ( roar_identify(&con, "roarctl") == -1 ) {
413  fprintf(stderr, "Error: Can not identify to server\n");
414  return 1;
415 }
416
417 if ( i == argc ) {
418  fprintf(stderr, "Error: No Commands given\n");
419  return 0; // this is not a fatal error...
420 }
421
422 for (; i < argc; i++) {
423  k = argv[i];
424  // cmd is in k
425
426  printf("--- [ %s ] ---\n", k);
427
428  if ( !strcmp(k, "help") ) {
429   usage();
430
431
432  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
433   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
434    fprintf(stderr, "Error: can not set mode to standby\n");
435   } else {
436    printf("going into standby\n");
437   }
438  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
439   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
440    fprintf(stderr, "Error: can not set mode to active\n");
441   } else {
442    printf("going into active mode\n");
443   }
444
445  } else if ( !strcmp(k, "exit") ) {
446   if ( roar_exit(&con) == -1 ) {
447    fprintf(stderr, "Error: can not quit server\n");
448   } else {
449    printf("Server quited\n");
450    break;
451   }
452  } else if ( !strcmp(k, "terminate") ) {
453   if ( roar_terminate(&con, 1) == -1 ) {
454    fprintf(stderr, "Error: can not terminate server\n");
455   } else {
456    printf("Server got asked to quited\n");
457    break;
458   }
459
460  } else if ( !strcmp(k, "standbymode") ) {
461   t = roar_get_standby(&con);
462   if ( t == -1 ) {
463    fprintf(stderr, "Error: can not get stanby mode\n");
464   } else if ( t == ROAR_STANDBY_ACTIVE ) {
465    printf("Server is in standby\n");
466   } else if ( t == ROAR_STANDBY_INACTIVE ) {
467    printf("Server is active\n");
468   } else {
469    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
470   }
471
472  } else if ( !strcmp(k, "serveroinfo") ) {
473   server_oinfo(&con);
474  } else if ( !strcmp(k, "listclients") ) {
475   list_clients(&con);
476  } else if ( !strcmp(k, "liststreams") ) {
477   list_streams(&con);
478  } else if ( !strcmp(k, "allinfo") ) {
479   server_oinfo(&con);
480   printf("\n");
481   list_clients(&con);
482   printf("\n");
483   list_streams(&con);
484
485  } else if ( !strcmp(k, "kick") ) {
486   k = argv[++i];
487   if ( !strcmp(k, "client") ) {
488    t = ROAR_OT_CLIENT;
489   } else if ( !strcmp(k, "stream") ) {
490    t = ROAR_OT_STREAM;
491   } else if ( !strcmp(k, "sample") ) {
492    t = ROAR_OT_SAMPLE;
493   } else if ( !strcmp(k, "source") ) {
494    t = ROAR_OT_SOURCE;
495   } else {
496    fprintf(stderr, "Error: unknown type: %s\n", k);
497    continue;
498   }
499   //t = atoi(argv[i++]);
500   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
501    fprintf(stderr, "Error: can not kick %s\n", k);
502   } else {
503    printf("%s kicked\n", k);
504   }
505
506  } else if ( !strcmp(k, "volume") ) {
507   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
508    fprintf(stderr, "Error: can not set volume\n");
509   } else {
510    printf("volume changed\n");
511   }
512
513  } else if ( !strcmp(k, "metaset") ) {
514   i++;
515   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
516    fprintf(stderr, "Error: can not set meta data\n");
517   } else {
518    printf("meta data changed\n");
519   }
520   i += 3;
521  } else if ( !strcmp(k, "metaget") ) {
522   i++;
523   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
524    fprintf(stderr, "Error: can not get meta data\n");
525   }
526   i++;
527
528  } else {
529   fprintf(stderr, "Error: invalid command: %s\n", k);
530  }
531
532 }
533
534 roar_disconnect(&con);
535
536 return 0;
537}
538
539//ll
Note: See TracBrowser for help on using the repository browser.