source: roaraudio/roarclients/roarctl.c @ 438:4343b7a1ec38

Last change on this file since 438:4343b7a1ec38 was 438:4343b7a1ec38, checked in by phi, 16 years ago

added UID/GID support to libroar and roarctl

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