source: roaraudio/roarclients/roarctl.c @ 497:bafbb01ed403

Last change on this file since 497:bafbb01ed403 was 497:bafbb01ed403, checked in by phi, 16 years ago

added support for underruns

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