source: roaraudio/roarclients/roarctl.c @ 449:e23bae640634

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

added proc_name() to roarctl so it shows a proc name in addtion to the PID

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