source: roaraudio/roarclients/roarctl.c @ 99:0ef9bfdde394

Last change on this file since 99:0ef9bfdde394 was 99:0ef9bfdde394, checked in by phi, 16 years ago

got meta data update working

File size: 10.0 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
237 printf("D: type=%i, mode=%i\n", meta.type, mode_i);
238
239 return roar_stream_meta_set(con, &s, mode_i, &meta);
240}
241
242int main (int argc, char * argv[]) {
243 struct roar_connection con;
244 char * server   = NULL;
245 char * k = NULL;
246 int    i;
247 int    t = 0;
248
249 for (i = 1; i < argc; i++) {
250  k = argv[i];
251
252  if ( strcmp(k, "--server") == 0 ) {
253   server = argv[++i];
254  } else if ( strcmp(k, "--help") == 0 ) {
255   usage();
256   return 0;
257  } else if ( *k == '-' ) {
258   fprintf(stderr, "Error: unknown argument: %s\n", k);
259   usage();
260   return 1;
261  } else {
262   break;
263  }
264 }
265
266 // connect
267
268 if ( roar_connect(&con, server) == -1 ) {
269  fprintf(stderr, "Error: Can not connect to server\n");
270  return 1;
271 }
272
273 if ( roar_identify(&con, "roarctl") == -1 ) {
274  fprintf(stderr, "Error: Can not identify to server\n");
275  return 1;
276 }
277
278 if ( i == argc ) {
279  fprintf(stderr, "Error: No Commands given\n");
280  return 0; // this is not a fatal error...
281 }
282
283 for (; i < argc; i++) {
284  k = argv[i];
285  // cmd is in k
286
287  printf("--- [ %s ] ---\n", k);
288
289  if ( !strcmp(k, "help") ) {
290   usage();
291
292
293  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
294   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
295    fprintf(stderr, "Error: can not set mode to standby\n");
296   } else {
297    printf("going into standby\n");
298   }
299  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
300   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
301    fprintf(stderr, "Error: can not set mode to active\n");
302   } else {
303    printf("going into active mode\n");
304   }
305
306  } else if ( !strcmp(k, "exit") ) {
307   if ( roar_exit(&con) == -1 ) {
308    fprintf(stderr, "Error: can not quit server\n");
309   } else {
310    printf("Server quited\n");
311    break;
312   }
313
314  } else if ( !strcmp(k, "standbymode") ) {
315   t = roar_get_standby(&con);
316   if ( t == -1 ) {
317    fprintf(stderr, "Error: can not get stanby mode\n");
318   } else if ( t == ROAR_STANDBY_ACTIVE ) {
319    printf("Server is in standby\n");
320   } else if ( t == ROAR_STANDBY_INACTIVE ) {
321    printf("Server is active\n");
322   } else {
323    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
324   }
325
326  } else if ( !strcmp(k, "serveroinfo") ) {
327   server_oinfo(&con);
328  } else if ( !strcmp(k, "listclients") ) {
329   list_clients(&con);
330  } else if ( !strcmp(k, "liststreams") ) {
331   list_streams(&con);
332  } else if ( !strcmp(k, "allinfo") ) {
333   server_oinfo(&con);
334   printf("\n");
335   list_clients(&con);
336   printf("\n");
337   list_streams(&con);
338
339  } else if ( !strcmp(k, "kick") ) {
340   k = argv[++i];
341   if ( !strcmp(k, "client") ) {
342    t = ROAR_OT_CLIENT;
343   } else if ( !strcmp(k, "stream") ) {
344    t = ROAR_OT_STREAM;
345   } else if ( !strcmp(k, "sample") ) {
346    t = ROAR_OT_SAMPLE;
347   } else if ( !strcmp(k, "source") ) {
348    t = ROAR_OT_SOURCE;
349   } else {
350    fprintf(stderr, "Error: unknown type: %s\n", k);
351    continue;
352   }
353   //t = atoi(argv[i++]);
354   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
355    fprintf(stderr, "Error: can not kick %s\n", k);
356   } else {
357    printf("%s kicked\n", k);
358   }
359
360  } else if ( !strcmp(k, "volume") ) {
361   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
362    fprintf(stderr, "Error: can not set volume\n");
363   } else {
364    printf("volume changed\n");
365   }
366
367  } else if ( !strcmp(k, "meta") ) {
368   i++;
369   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
370    fprintf(stderr, "Error: can not set meta data\n");
371   } else {
372    printf("meta data changed\n");
373   }
374   i += 3;
375
376  } else {
377   fprintf(stderr, "Error: invalid command: %s\n", k);
378  }
379
380 }
381
382 roar_disconnect(&con);
383
384 return 0;
385}
386
387//ll
Note: See TracBrowser for help on using the repository browser.