source: roaraudio/roarclients/roarctl.c @ 445:98a28217dc32

Last change on this file since 445:98a28217dc32 was 445:98a28217dc32, checked in by phi, 16 years ago

added resolv of user and group name

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