source: roaraudio/roarclients/roarctl.c @ 106:bbd14eeee9db

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

updated some code and added show_meta_type()

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