source: roaraudio/roarclients/roarctl.c @ 540:c1fd46366418

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

get display of orgiginal codec working

File size: 12.4 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           : %2i (%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 codec (streamed): %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
158                                      info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
159   printf("Input block size      : %i Byte\n", info.block_size);
160   printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
161  }
162  display_mixer(con, id[i]);
163  show_meta_all(con, id[i]);
164 }
165
166}
167
168int display_mixer (struct roar_connection * con, int stream) {
169 int channels;
170 struct roar_mixer_settings mixer;
171 int i;
172
173 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
174  fprintf(stderr, "Error: can not get stream mixer info\n");
175  return -1;
176 }
177
178 for (i = 0; i < channels; i++)
179  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
180
181 return 0;
182}
183
184int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
185 int chans = 0;
186 int id;
187 int i;
188 int len;
189 int old_chans;
190 int vol_l, vol_r;
191 char * k;
192 struct roar_mixer_settings mixer;
193 struct roar_mixer_settings old_mixer;
194
195 if (*cur + 2 > max)
196  return -1;
197
198 id = atoi(arg[++(*cur)]);
199
200 k = arg[++(*cur)];
201
202 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
203  fprintf(stderr, "Error: can not get stream mixer info\n");
204  return -1;
205 }
206
207
208 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
209  chans = 1;
210
211  if ( *cur + 1 > max )
212   return -1;
213
214  k   = arg[++(*cur)];
215  len = strlen(k);
216
217  if ( k[len - 1] == '%' ) {
218   k[len - 1] = 0;
219   vol_l = (atof(k)*65535)/100;
220  } else {
221   vol_l = atoi(k);
222  }
223
224  for (i = 0; i < old_chans; i++)
225   mixer.mixer[i] = vol_l;
226
227  chans = old_chans;
228
229 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
230  chans = 2;
231//  printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans);
232  ROAR_ERR("mode stereo not supported");
233  return -1;
234 } else {
235  if ( strcmp(k, "mono") == 0 ) {
236   chans = 1;
237  } else if ( strcmp(k, "stereo") == 0 ) {
238   chans = 2;
239  } else {
240   chans = atoi(k);
241  }
242
243//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
244
245  if ( *cur + chans > max )
246   return -1;
247
248  for (i = 0; i < chans; i++) {
249   k   = arg[++(*cur)];
250   len = strlen(k);
251
252   if ( k[len - 1] == '%' ) {
253    k[len - 1] = 0;
254    mixer.mixer[i] = (atof(k)*(int)65535)/100;
255   } else {
256    mixer.mixer[i] = atoi(k);
257   }
258  }
259 }
260
261 mixer.scale = 65535;
262
263 return roar_set_vol(con, id, &mixer, chans);
264}
265
266int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
267 struct roar_meta   meta;
268 struct roar_stream s;
269 int mode_i = ROAR_META_MODE_SET;
270
271 s.id = id;
272
273// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
274
275 if ( strcmp(mode, "add") == 0 ) {
276  mode_i = ROAR_META_MODE_ADD;
277 }
278
279 meta.type   = roar_meta_inttype(type);
280 meta.value  = val;
281 meta.key[0] = 0;
282
283 if ( meta.type == -1 ) {
284  fprintf(stderr, "Error: unknown type: %s\n", type);
285  return -1;
286 }
287
288// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
289
290 return roar_stream_meta_set(con, &s, mode_i, &meta);
291}
292
293int show_meta_type (struct roar_connection * con, int id, char * type) {
294 struct roar_meta   meta;
295 struct roar_stream s;
296
297 s.id = id;
298
299 meta.type  = roar_meta_inttype(type);
300
301 if ( meta.type == -1 ) {
302  fprintf(stderr, "Error: unknown type: %s\n", type);
303  return -1;
304 }
305
306 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
307  return -1;
308
309 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
310
311 roar_meta_free(&meta);
312
313 return 0;
314}
315
316int show_meta_all (struct roar_connection * con, int id) {
317 struct roar_stream s;
318 int types[ROAR_META_MAX_PER_STREAM];
319 int i;
320 int len;
321
322 s.id = id;
323
324 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
325  return -1;
326
327 for (i = 0; i < len; i++)
328  show_meta_type(con, id, roar_meta_strtype(types[i]));
329
330 return 0;
331}
332
333int main (int argc, char * argv[]) {
334 struct roar_connection con;
335 char * server   = NULL;
336 char * k = NULL;
337 int    i;
338 int    t = 0;
339
340 for (i = 1; i < argc; i++) {
341  k = argv[i];
342
343  if ( strcmp(k, "--server") == 0 ) {
344   server = argv[++i];
345  } else if ( strcmp(k, "--help") == 0 ) {
346   usage();
347   return 0;
348  } else if ( *k == '-' ) {
349   fprintf(stderr, "Error: unknown argument: %s\n", k);
350   usage();
351   return 1;
352  } else {
353   break;
354  }
355 }
356
357 // connect
358
359 if ( roar_connect(&con, server) == -1 ) {
360  fprintf(stderr, "Error: Can not connect to server\n");
361  return 1;
362 }
363
364 if ( roar_identify(&con, "roarctl") == -1 ) {
365  fprintf(stderr, "Error: Can not identify to server\n");
366  return 1;
367 }
368
369 if ( i == argc ) {
370  fprintf(stderr, "Error: No Commands given\n");
371  return 0; // this is not a fatal error...
372 }
373
374 for (; i < argc; i++) {
375  k = argv[i];
376  // cmd is in k
377
378  printf("--- [ %s ] ---\n", k);
379
380  if ( !strcmp(k, "help") ) {
381   usage();
382
383
384  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
385   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
386    fprintf(stderr, "Error: can not set mode to standby\n");
387   } else {
388    printf("going into standby\n");
389   }
390  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
391   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
392    fprintf(stderr, "Error: can not set mode to active\n");
393   } else {
394    printf("going into active mode\n");
395   }
396
397  } else if ( !strcmp(k, "exit") ) {
398   if ( roar_exit(&con) == -1 ) {
399    fprintf(stderr, "Error: can not quit server\n");
400   } else {
401    printf("Server quited\n");
402    break;
403   }
404
405  } else if ( !strcmp(k, "standbymode") ) {
406   t = roar_get_standby(&con);
407   if ( t == -1 ) {
408    fprintf(stderr, "Error: can not get stanby mode\n");
409   } else if ( t == ROAR_STANDBY_ACTIVE ) {
410    printf("Server is in standby\n");
411   } else if ( t == ROAR_STANDBY_INACTIVE ) {
412    printf("Server is active\n");
413   } else {
414    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
415   }
416
417  } else if ( !strcmp(k, "serveroinfo") ) {
418   server_oinfo(&con);
419  } else if ( !strcmp(k, "listclients") ) {
420   list_clients(&con);
421  } else if ( !strcmp(k, "liststreams") ) {
422   list_streams(&con);
423  } else if ( !strcmp(k, "allinfo") ) {
424   server_oinfo(&con);
425   printf("\n");
426   list_clients(&con);
427   printf("\n");
428   list_streams(&con);
429
430  } else if ( !strcmp(k, "kick") ) {
431   k = argv[++i];
432   if ( !strcmp(k, "client") ) {
433    t = ROAR_OT_CLIENT;
434   } else if ( !strcmp(k, "stream") ) {
435    t = ROAR_OT_STREAM;
436   } else if ( !strcmp(k, "sample") ) {
437    t = ROAR_OT_SAMPLE;
438   } else if ( !strcmp(k, "source") ) {
439    t = ROAR_OT_SOURCE;
440   } else {
441    fprintf(stderr, "Error: unknown type: %s\n", k);
442    continue;
443   }
444   //t = atoi(argv[i++]);
445   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
446    fprintf(stderr, "Error: can not kick %s\n", k);
447   } else {
448    printf("%s kicked\n", k);
449   }
450
451  } else if ( !strcmp(k, "volume") ) {
452   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
453    fprintf(stderr, "Error: can not set volume\n");
454   } else {
455    printf("volume changed\n");
456   }
457
458  } else if ( !strcmp(k, "metaset") ) {
459   i++;
460   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
461    fprintf(stderr, "Error: can not set meta data\n");
462   } else {
463    printf("meta data changed\n");
464   }
465   i += 3;
466  } else if ( !strcmp(k, "metaget") ) {
467   i++;
468   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
469    fprintf(stderr, "Error: can not get meta data\n");
470   }
471   i++;
472
473  } else {
474   fprintf(stderr, "Error: invalid command: %s\n", k);
475  }
476
477 }
478
479 roar_disconnect(&con);
480
481 return 0;
482}
483
484//ll
Note: See TracBrowser for help on using the repository browser.