source: roaraudio/roarclients/roarctl.c @ 465:82fd66e50dc3

Last change on this file since 465:82fd66e50dc3 was 465:82fd66e50dc3, checked in by phi, 16 years ago

added support to get the size of optimal read() and write()s

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