1 | //roarctl.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roarclients a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <roaraudio.h> |
---|
26 | |
---|
27 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) |
---|
28 | #define _POSIX_USERS |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef _POSIX_USERS |
---|
32 | #include <pwd.h> |
---|
33 | #include <grp.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <sys/time.h> |
---|
37 | #include <time.h> |
---|
38 | |
---|
39 | int g_verbose = 0; |
---|
40 | |
---|
41 | int display_mixer (struct roar_connection * con, int stream); |
---|
42 | int show_meta_all (struct roar_connection * con, int id); |
---|
43 | |
---|
44 | void usage (void) { |
---|
45 | printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n"); |
---|
46 | |
---|
47 | printf("\nOptions:\n\n"); |
---|
48 | |
---|
49 | printf(" --server SERVER - Set server hostname\n" |
---|
50 | " --help - Show this help\n" |
---|
51 | " --verbose -v - Show verbose output\n" |
---|
52 | ); |
---|
53 | |
---|
54 | printf("\nCommands:\n\n"); |
---|
55 | printf( |
---|
56 | " help - Show this help\n" |
---|
57 | " sleep TIME - Sleeps for TIME seconds\n" |
---|
58 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
59 | " ping NUM - Do NUM pings using NOOP commands\n" |
---|
60 | #endif |
---|
61 | "\n" |
---|
62 | " standby, off - Go into standby mode\n" |
---|
63 | " resume, on - Go into active mode\n" |
---|
64 | " standbymode - Show current standby mode\n" |
---|
65 | " exit - Quits the roard (must be used as last command)\n" |
---|
66 | " terminate - Like exit but let the server up to serve still connected clients,\n" |
---|
67 | " new clients cann't connect and the server terminates after the last\n" |
---|
68 | " client disconnected\n" |
---|
69 | "\n" |
---|
70 | " volume ID CHAN V0 V1... - Sets volume for stream ID\n" |
---|
71 | " CHAN is the number of channels or 'mono' or 'stereo'\n" |
---|
72 | " if mono or stereo is chosen roarctl trys to set\n" |
---|
73 | " sensfull values for all channels even if the output\n" |
---|
74 | " is has more channels.\n" |
---|
75 | " all other args are the volumes of the channels\n" |
---|
76 | " you may use integer or percent values.\n" |
---|
77 | " percent values can flooding points.\n" |
---|
78 | "\n" |
---|
79 | " flag ID FLAGS - Sets flags FLAGS on stream ID. FLAGS may be a comma\n" |
---|
80 | " seperated list of flags.\n" |
---|
81 | " unflag ID FLAGS - Unsets flags on a stream. See flag.\n" |
---|
82 | "\n" |
---|
83 | " kick TYPE ID - Kicks object of TYPE with id ID\n" |
---|
84 | " Types: client stream sample source\n" |
---|
85 | "\n" |
---|
86 | " metasave ID FILE - Saves meta data of stream ID to file FILE\n" |
---|
87 | " metaload ID FILE - Loads meta data from file FILE and set it on stream ID\n" |
---|
88 | "\n" |
---|
89 | " serveroinfo - Gets Informations about server output\n" |
---|
90 | " listclients - Gets Informations about clients\n" |
---|
91 | " liststreams - Gets Informations about streams\n" |
---|
92 | " allinfo - Get all infos\n" |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
97 | int ping (struct roar_connection * con, int num) { |
---|
98 | struct timeval try, ans; |
---|
99 | struct roar_message m; |
---|
100 | register int ret; |
---|
101 | int i; |
---|
102 | double cur, min = 3600*1000, max = 0, sum = 0; |
---|
103 | |
---|
104 | if ( num == 0 ) |
---|
105 | return 0; |
---|
106 | |
---|
107 | for (i = 0; i < num; i++) { |
---|
108 | m.cmd = ROAR_CMD_NOOP; |
---|
109 | m.datalen = 0; |
---|
110 | |
---|
111 | gettimeofday(&try, NULL); |
---|
112 | ret = roar_req(con, &m, NULL); |
---|
113 | gettimeofday(&ans, NULL); |
---|
114 | |
---|
115 | if ( ret == -1 ) |
---|
116 | return -1; |
---|
117 | |
---|
118 | while (ans.tv_sec > try.tv_sec) { |
---|
119 | ans.tv_sec--; |
---|
120 | ans.tv_usec += 1000000; |
---|
121 | } |
---|
122 | ans.tv_usec -= try.tv_usec; |
---|
123 | |
---|
124 | printf("Pong from server: seq=%i time=%.3fms\n", i, (cur = ans.tv_usec/1000.0)); |
---|
125 | |
---|
126 | sum += cur; |
---|
127 | if ( min > cur ) |
---|
128 | min = cur; |
---|
129 | if ( cur > max ) |
---|
130 | max = cur; |
---|
131 | |
---|
132 | if ( i != (num - 1) ) |
---|
133 | sleep(1); |
---|
134 | } |
---|
135 | |
---|
136 | printf("\n--- ping statistics ---\n"); |
---|
137 | printf("%i packets transmitted\n", i); |
---|
138 | printf("rtt min/avg/max = %.3f/%.3f/%.3f ms\n", min, sum/(double)i, max); |
---|
139 | |
---|
140 | return 0; |
---|
141 | } |
---|
142 | #endif |
---|
143 | |
---|
144 | void server_oinfo (struct roar_connection * con) { |
---|
145 | struct roar_stream s; |
---|
146 | |
---|
147 | if ( roar_server_oinfo(con, &s) == -1 ) { |
---|
148 | fprintf(stderr, "Error: can not get server output info\n"); |
---|
149 | return; |
---|
150 | } |
---|
151 | |
---|
152 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
153 | printf("Server Output rate : %i\n", s.info.rate); |
---|
154 | printf("Server Output bits : %i\n", s.info.bits); |
---|
155 | printf("Server Output channels: %i\n", s.info.channels); |
---|
156 | printf("Server Output codec : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
157 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
158 | // printf("Server Output rate: %i", s.info.rate); |
---|
159 | if ( g_verbose > 1 ) |
---|
160 | printf("Server Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels)); |
---|
161 | } |
---|
162 | |
---|
163 | const char * proc_name (pid_t pid) { |
---|
164 | static char ret[80] = "?"; |
---|
165 | #ifdef __linux__ |
---|
166 | char file[80], buf[80], *r; |
---|
167 | int i; |
---|
168 | |
---|
169 | snprintf(file, 79, "/proc/%i/exe", pid); |
---|
170 | file[79] = 0; |
---|
171 | |
---|
172 | ret[0] = '?'; |
---|
173 | ret[1] = 0; |
---|
174 | |
---|
175 | if ( (i = readlink(file, buf, 79)) != -1 ) { |
---|
176 | buf[i] = 0; |
---|
177 | if ( (r = strrchr(buf, '/')) != NULL ) { |
---|
178 | r++; |
---|
179 | if ( *r != 0 ) |
---|
180 | strcpy(ret, r); |
---|
181 | } |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | return ret; |
---|
186 | } |
---|
187 | |
---|
188 | void list_clients (struct roar_connection * con) { |
---|
189 | int i; |
---|
190 | int num; |
---|
191 | int h; |
---|
192 | int id[ROAR_CLIENTS_MAX]; |
---|
193 | struct roar_client c; |
---|
194 | #ifdef _POSIX_USERS |
---|
195 | struct group * grp = NULL; |
---|
196 | struct passwd * pwd = NULL; |
---|
197 | #endif |
---|
198 | |
---|
199 | if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) { |
---|
200 | fprintf(stderr, "Error: can not get client list\n"); |
---|
201 | return; |
---|
202 | } |
---|
203 | |
---|
204 | for (i = 0; i < num; i++) { |
---|
205 | printf("client %i:\n", id[i]); |
---|
206 | if ( roar_get_client(con, &c, id[i]) == -1 ) { |
---|
207 | fprintf(stderr, "Error: can not get client info\n"); |
---|
208 | continue; |
---|
209 | } |
---|
210 | printf("Player name : %s\n", c.name); |
---|
211 | printf("Player PID : %i(%s)\n", c.pid, proc_name(c.pid)); |
---|
212 | if ( c.uid != -1 ) { |
---|
213 | #ifdef _POSIX_USERS |
---|
214 | pwd = getpwuid(c.uid); |
---|
215 | grp = getgrgid(c.gid); |
---|
216 | printf("Player UID/GID : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?"); |
---|
217 | #else |
---|
218 | printf("Player UID/GID : %i/%i\n", c.uid, c.gid); |
---|
219 | #endif |
---|
220 | } |
---|
221 | if ( c.execed != -1 ) |
---|
222 | printf("Execed stream : %i\n", c.execed); |
---|
223 | |
---|
224 | for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++) |
---|
225 | if ( c.streams[h] != -1 ) |
---|
226 | printf("stream : %i\n", c.streams[h]); |
---|
227 | } |
---|
228 | |
---|
229 | } |
---|
230 | |
---|
231 | void list_streams (struct roar_connection * con) { |
---|
232 | int i; |
---|
233 | int num; |
---|
234 | int id[ROAR_STREAMS_MAX]; |
---|
235 | struct roar_stream s; |
---|
236 | struct roar_stream_info info; |
---|
237 | char flags[1024]; |
---|
238 | char name[1024]; |
---|
239 | |
---|
240 | |
---|
241 | if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
242 | fprintf(stderr, "Error: can not get stream list\n"); |
---|
243 | return; |
---|
244 | } |
---|
245 | |
---|
246 | for (i = 0; i < num; i++) { |
---|
247 | printf("stream %i:\n", id[i]); |
---|
248 | if ( roar_get_stream(con, &s, id[i]) == -1 ) { |
---|
249 | fprintf(stderr, "Error: can not get stream info\n"); |
---|
250 | continue; |
---|
251 | } |
---|
252 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
253 | |
---|
254 | if ( roar_stream_get_name(con, &s, name, 1024) == 0 ) |
---|
255 | printf("Stream name : %s\n", name); |
---|
256 | |
---|
257 | if ( s.pos_rel_id == -1 ) { |
---|
258 | printf("Relativ position id : none (stream not synchronized)\n"); |
---|
259 | } else if ( s.pos_rel_id == id[i] ) { |
---|
260 | printf("Relativ position id : %i (self synchronized)\n", s.pos_rel_id); |
---|
261 | } else { |
---|
262 | printf("Relativ position id : %i (synchronized)\n", s.pos_rel_id); |
---|
263 | } |
---|
264 | if ( g_verbose > 1 ) { |
---|
265 | if ( s.info.rate && s.info.channels ) { |
---|
266 | printf("Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, |
---|
267 | (float)s.pos/(s.info.rate*s.info.channels)); |
---|
268 | } else { |
---|
269 | printf("Position : %lu S\n", (unsigned long int) s.pos); |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | if ( s.dir != ROAR_DIR_LIGHT_IN && s.dir != ROAR_DIR_LIGHT_OUT && |
---|
274 | s.info.rate && s.info.bits && s.info.channels |
---|
275 | ) { |
---|
276 | printf("Input rate : %i\n", s.info.rate); |
---|
277 | printf("Input bits : %i\n", s.info.bits); |
---|
278 | printf("Input channels : %i\n", s.info.channels); |
---|
279 | } |
---|
280 | |
---|
281 | printf("Input codec : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
282 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
283 | if ( roar_stream_get_info(con, &s, &info) != -1 ) { |
---|
284 | if ( info.codec != s.info.codec ) { |
---|
285 | printf("Input codec (streamed): %2i (%s%s)\n", info.codec, roar_codec2str(info.codec), |
---|
286 | info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
287 | } |
---|
288 | |
---|
289 | if ( g_verbose ) { |
---|
290 | if ( info.block_size ) |
---|
291 | printf("Input block size : %i Byte\n", info.block_size); |
---|
292 | |
---|
293 | printf("Underruns pre/post : %i/%i\n", info.pre_underruns, info.post_underruns); |
---|
294 | if ( g_verbose > 1 ) |
---|
295 | printf("Stream delay : %ims\n", (int)info.delay/1000); |
---|
296 | |
---|
297 | *flags = 0; |
---|
298 | if ( info.flags & ROAR_FLAG_PRIMARY ) |
---|
299 | strcat(flags, "primary "); |
---|
300 | if ( info.flags & ROAR_FLAG_SYNC ) |
---|
301 | strcat(flags, "sync "); |
---|
302 | if ( info.flags & ROAR_FLAG_OUTPUT ) |
---|
303 | strcat(flags, "output "); |
---|
304 | if ( info.flags & ROAR_FLAG_SOURCE ) |
---|
305 | strcat(flags, "source "); |
---|
306 | if ( info.flags & ROAR_FLAG_META ) |
---|
307 | strcat(flags, "meta "); |
---|
308 | if ( info.flags & ROAR_FLAG_AUTOCONF ) |
---|
309 | strcat(flags, "autoconf "); |
---|
310 | if ( info.flags & ROAR_FLAG_CLEANMETA ) |
---|
311 | strcat(flags, "cleanmeta "); |
---|
312 | if ( info.flags & ROAR_FLAG_HWMIXER ) |
---|
313 | strcat(flags, "hwmixer "); |
---|
314 | if ( info.flags & ROAR_FLAG_PAUSE ) |
---|
315 | strcat(flags, "pause "); |
---|
316 | if ( info.flags & ROAR_FLAG_MUTE ) |
---|
317 | strcat(flags, "mute "); |
---|
318 | |
---|
319 | printf("Flags : %s\n", flags); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | if ( s.dir != ROAR_DIR_THRU ) { |
---|
324 | display_mixer(con, id[i]); |
---|
325 | show_meta_all(con, id[i]); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | } |
---|
330 | |
---|
331 | int display_mixer (struct roar_connection * con, int stream) { |
---|
332 | int channels; |
---|
333 | struct roar_mixer_settings mixer; |
---|
334 | int i; |
---|
335 | |
---|
336 | if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) { |
---|
337 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
338 | return -1; |
---|
339 | } |
---|
340 | |
---|
341 | for (i = 0; i < channels; i++) |
---|
342 | printf("Mixer volume chan %2i : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35); |
---|
343 | |
---|
344 | return 0; |
---|
345 | } |
---|
346 | |
---|
347 | int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) { |
---|
348 | int chans = 0; |
---|
349 | int id; |
---|
350 | int i; |
---|
351 | int len; |
---|
352 | int old_chans; |
---|
353 | int vol_l, vol_r; |
---|
354 | char * k; |
---|
355 | struct roar_mixer_settings mixer; |
---|
356 | struct roar_mixer_settings old_mixer; |
---|
357 | |
---|
358 | if (*cur + 2 > max) |
---|
359 | return -1; |
---|
360 | |
---|
361 | id = atoi(arg[++(*cur)]); |
---|
362 | |
---|
363 | k = arg[++(*cur)]; |
---|
364 | |
---|
365 | if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) { |
---|
366 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
367 | return -1; |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | // TODO: clean up code here as the % vs. abs code is very duplicate... |
---|
372 | |
---|
373 | if ( strcmp(k, "mono") == 0 && old_chans != 1 ) { |
---|
374 | chans = 1; |
---|
375 | |
---|
376 | if ( *cur + 1 > max ) |
---|
377 | return -1; |
---|
378 | |
---|
379 | k = arg[++(*cur)]; |
---|
380 | len = strlen(k); |
---|
381 | |
---|
382 | if ( k[len - 1] == '%' ) { |
---|
383 | k[len - 1] = 0; |
---|
384 | vol_l = (atof(k)*65535)/100; |
---|
385 | } else { |
---|
386 | vol_l = atoi(k); |
---|
387 | } |
---|
388 | |
---|
389 | for (i = 0; i < old_chans; i++) |
---|
390 | mixer.mixer[i] = vol_l; |
---|
391 | |
---|
392 | chans = old_chans; |
---|
393 | |
---|
394 | } else if ( strcmp(k, "stereo") == 0 && old_chans == 4 ) { |
---|
395 | chans = 4; |
---|
396 | |
---|
397 | if ( *cur + 2 > max ) |
---|
398 | return -1; |
---|
399 | |
---|
400 | k = arg[++(*cur)]; |
---|
401 | len = strlen(k); |
---|
402 | |
---|
403 | if ( k[len - 1] == '%' ) { |
---|
404 | k[len - 1] = 0; |
---|
405 | vol_l = (atof(k)*65535)/100; |
---|
406 | } else { |
---|
407 | vol_l = atoi(k); |
---|
408 | } |
---|
409 | |
---|
410 | k = arg[++(*cur)]; |
---|
411 | len = strlen(k); |
---|
412 | |
---|
413 | if ( k[len - 1] == '%' ) { |
---|
414 | k[len - 1] = 0; |
---|
415 | vol_r = (atof(k)*65535)/100; |
---|
416 | } else { |
---|
417 | vol_r = atoi(k); |
---|
418 | } |
---|
419 | |
---|
420 | mixer.mixer[0] = vol_l; |
---|
421 | mixer.mixer[1] = vol_r; |
---|
422 | mixer.mixer[2] = vol_l; |
---|
423 | mixer.mixer[3] = vol_r; |
---|
424 | |
---|
425 | } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) { |
---|
426 | chans = 2; |
---|
427 | // printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
428 | ROAR_ERR("mode stereo not supported"); |
---|
429 | return -1; |
---|
430 | } else { |
---|
431 | if ( strcmp(k, "mono") == 0 ) { |
---|
432 | chans = 1; |
---|
433 | } else if ( strcmp(k, "stereo") == 0 ) { |
---|
434 | chans = 2; |
---|
435 | } else { |
---|
436 | chans = atoi(k); |
---|
437 | } |
---|
438 | |
---|
439 | // printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
440 | |
---|
441 | if ( *cur + chans > max ) |
---|
442 | return -1; |
---|
443 | |
---|
444 | for (i = 0; i < chans; i++) { |
---|
445 | k = arg[++(*cur)]; |
---|
446 | len = strlen(k); |
---|
447 | |
---|
448 | if ( k[len - 1] == '%' ) { |
---|
449 | k[len - 1] = 0; |
---|
450 | mixer.mixer[i] = (atof(k)*(int)65535)/100; |
---|
451 | } else { |
---|
452 | mixer.mixer[i] = atoi(k); |
---|
453 | } |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | mixer.scale = 65535; |
---|
458 | |
---|
459 | return roar_set_vol(con, id, &mixer, chans); |
---|
460 | } |
---|
461 | |
---|
462 | int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) { |
---|
463 | struct roar_meta meta; |
---|
464 | struct roar_stream s; |
---|
465 | int mode_i = ROAR_META_MODE_SET; |
---|
466 | |
---|
467 | memset(&s, 0, sizeof(s)); |
---|
468 | |
---|
469 | s.id = id; |
---|
470 | |
---|
471 | // printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val); |
---|
472 | |
---|
473 | if ( strcmp(mode, "add") == 0 ) { |
---|
474 | mode_i = ROAR_META_MODE_ADD; |
---|
475 | } |
---|
476 | |
---|
477 | meta.type = roar_meta_inttype(type); |
---|
478 | meta.value = val; |
---|
479 | meta.key[0] = 0; |
---|
480 | |
---|
481 | if ( meta.type == -1 ) { |
---|
482 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
483 | return -1; |
---|
484 | } |
---|
485 | |
---|
486 | // printf("D: type=%i, mode=%i\n", meta.type, mode_i); |
---|
487 | |
---|
488 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
489 | return -1; |
---|
490 | |
---|
491 | meta.type = ROAR_META_TYPE_NONE; |
---|
492 | meta.value = NULL; |
---|
493 | |
---|
494 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
495 | } |
---|
496 | |
---|
497 | int load_meta (struct roar_connection * con, int id, char * file) { |
---|
498 | struct roar_meta meta; |
---|
499 | struct roar_stream s; |
---|
500 | int mode_i = ROAR_META_MODE_SET; |
---|
501 | FILE * in; |
---|
502 | char lion[1024]; |
---|
503 | char * v; |
---|
504 | |
---|
505 | memset(&s, 0, sizeof(s)); |
---|
506 | |
---|
507 | s.id = id; |
---|
508 | |
---|
509 | if ( (in = fopen(file, "r")) == NULL ) |
---|
510 | return -1; |
---|
511 | |
---|
512 | while (fgets(lion, 1024, in) != NULL) { |
---|
513 | if ( (v = strtok(lion, "\r\n")) != NULL ) |
---|
514 | if ( (v = strtok(NULL, "\r\n")) != NULL ) |
---|
515 | *(v-1) = 0; |
---|
516 | |
---|
517 | if ( (v = strstr(lion, "=")) == NULL ) { |
---|
518 | fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion); |
---|
519 | continue; |
---|
520 | } |
---|
521 | |
---|
522 | *v++ = 0; |
---|
523 | |
---|
524 | meta.type = roar_meta_inttype(lion); |
---|
525 | meta.value = v; |
---|
526 | meta.key[0] = 0; |
---|
527 | |
---|
528 | if ( meta.type == -1 ) { |
---|
529 | fprintf(stderr, "Error: unknown type: %s\n", lion); |
---|
530 | continue; |
---|
531 | } |
---|
532 | |
---|
533 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
534 | return -1; |
---|
535 | } |
---|
536 | |
---|
537 | fclose(in); |
---|
538 | |
---|
539 | meta.type = ROAR_META_TYPE_NONE; |
---|
540 | meta.value = NULL; |
---|
541 | |
---|
542 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
543 | } |
---|
544 | |
---|
545 | int show_meta_type (struct roar_connection * con, int id, char * type) { |
---|
546 | struct roar_meta meta; |
---|
547 | struct roar_stream s; |
---|
548 | |
---|
549 | memset(&s, 0, sizeof(s)); |
---|
550 | |
---|
551 | s.id = id; |
---|
552 | |
---|
553 | meta.type = roar_meta_inttype(type); |
---|
554 | |
---|
555 | if ( meta.type == -1 ) { |
---|
556 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
557 | return -1; |
---|
558 | } |
---|
559 | |
---|
560 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
561 | return -1; |
---|
562 | |
---|
563 | printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
564 | |
---|
565 | roar_meta_free(&meta); |
---|
566 | |
---|
567 | return 0; |
---|
568 | } |
---|
569 | |
---|
570 | int show_meta_all (struct roar_connection * con, int id) { |
---|
571 | struct roar_stream s; |
---|
572 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
573 | int i; |
---|
574 | int len; |
---|
575 | |
---|
576 | memset(&s, 0, sizeof(s)); |
---|
577 | |
---|
578 | s.id = id; |
---|
579 | |
---|
580 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
581 | return -1; |
---|
582 | |
---|
583 | for (i = 0; i < len; i++) |
---|
584 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
585 | |
---|
586 | return 0; |
---|
587 | } |
---|
588 | |
---|
589 | int save_meta (struct roar_connection * con, int id, char * file) { |
---|
590 | struct roar_stream s; |
---|
591 | struct roar_meta meta; |
---|
592 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
593 | int i; |
---|
594 | int len; |
---|
595 | FILE * out; |
---|
596 | |
---|
597 | memset(&s, 0, sizeof(s)); |
---|
598 | |
---|
599 | s.id = id; |
---|
600 | |
---|
601 | if ( (out = fopen(file, "w")) == NULL ) |
---|
602 | return -1; |
---|
603 | |
---|
604 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
605 | return -1; |
---|
606 | |
---|
607 | for (i = 0; i < len; i++) { |
---|
608 | /* |
---|
609 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
610 | */ |
---|
611 | meta.type = types[i]; |
---|
612 | |
---|
613 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
614 | continue; |
---|
615 | |
---|
616 | // printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
617 | |
---|
618 | fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value); |
---|
619 | |
---|
620 | roar_meta_free(&meta); |
---|
621 | } |
---|
622 | |
---|
623 | fclose(out); |
---|
624 | |
---|
625 | return 0; |
---|
626 | } |
---|
627 | |
---|
628 | int set_flags (struct roar_connection * con, int id, int reset, char * flags) { |
---|
629 | int f = ROAR_FLAG_NONE; |
---|
630 | char * c; |
---|
631 | struct roar_stream s[1]; |
---|
632 | |
---|
633 | memset(s, 0, sizeof(struct roar_stream)); |
---|
634 | |
---|
635 | s->id = id; |
---|
636 | |
---|
637 | c = strtok(flags, ","); |
---|
638 | while (c != NULL) { |
---|
639 | if ( !strcmp(c, "meta") ) { |
---|
640 | f |= ROAR_FLAG_META; |
---|
641 | } else if ( !strcmp(c, "primary") ) { |
---|
642 | f |= ROAR_FLAG_PRIMARY; |
---|
643 | } else if ( !strcmp(c, "sync") ) { |
---|
644 | f |= ROAR_FLAG_SYNC; |
---|
645 | } else if ( !strcmp(c, "cleanmeta") ) { |
---|
646 | f |= ROAR_FLAG_CLEANMETA; |
---|
647 | } else if ( !strcmp(c, "hwmixer") ) { |
---|
648 | f |= ROAR_FLAG_HWMIXER; |
---|
649 | } else if ( !strcmp(c, "pause") ) { |
---|
650 | f |= ROAR_FLAG_PAUSE; |
---|
651 | } else if ( !strcmp(c, "mute") ) { |
---|
652 | f |= ROAR_FLAG_MUTE; |
---|
653 | } else { |
---|
654 | fprintf(stderr, "Error: unknown flag: %s\n", c); |
---|
655 | return -1; |
---|
656 | } |
---|
657 | |
---|
658 | c = strtok(NULL, ","); |
---|
659 | } |
---|
660 | |
---|
661 | return roar_stream_set_flags(con, s, f, reset); |
---|
662 | } |
---|
663 | |
---|
664 | int main (int argc, char * argv[]) { |
---|
665 | struct roar_connection con; |
---|
666 | char * server = NULL; |
---|
667 | char * k = NULL; |
---|
668 | int i; |
---|
669 | int t = 0; |
---|
670 | |
---|
671 | for (i = 1; i < argc; i++) { |
---|
672 | k = argv[i]; |
---|
673 | |
---|
674 | if ( strcmp(k, "--server") == 0 ) { |
---|
675 | server = argv[++i]; |
---|
676 | } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) { |
---|
677 | g_verbose++; |
---|
678 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
679 | usage(); |
---|
680 | return 0; |
---|
681 | } else if ( *k == '-' ) { |
---|
682 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
683 | usage(); |
---|
684 | return 1; |
---|
685 | } else { |
---|
686 | break; |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | // connect |
---|
691 | |
---|
692 | if ( roar_connect(&con, server) == -1 ) { |
---|
693 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
694 | return 1; |
---|
695 | } |
---|
696 | |
---|
697 | if ( roar_identify(&con, "roarctl") == -1 ) { |
---|
698 | fprintf(stderr, "Error: Can not identify to server\n"); |
---|
699 | return 1; |
---|
700 | } |
---|
701 | |
---|
702 | if ( i == argc ) { |
---|
703 | fprintf(stderr, "Error: No Commands given\n"); |
---|
704 | return 0; // this is not a fatal error... |
---|
705 | } |
---|
706 | |
---|
707 | for (; i < argc; i++) { |
---|
708 | k = argv[i]; |
---|
709 | // cmd is in k |
---|
710 | |
---|
711 | printf("--- [ %s ] ---\n", k); |
---|
712 | |
---|
713 | if ( !strcmp(k, "help") ) { |
---|
714 | usage(); |
---|
715 | |
---|
716 | } else if ( !strcmp(k, "sleep") ) { |
---|
717 | sleep(atoi(argv[++i])); |
---|
718 | |
---|
719 | } else if ( !strcmp(k, "ping") ) { |
---|
720 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
721 | if ( ping(&con, atoi(argv[++i])) == -1 ) { |
---|
722 | fprintf(stderr, "Error: can not ping\n"); |
---|
723 | } |
---|
724 | #else |
---|
725 | fprintf(stderr, "Error: ping not supported.\n"); |
---|
726 | i++; |
---|
727 | #endif |
---|
728 | |
---|
729 | } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) { |
---|
730 | if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) { |
---|
731 | fprintf(stderr, "Error: can not set mode to standby\n"); |
---|
732 | } else { |
---|
733 | printf("going into standby\n"); |
---|
734 | } |
---|
735 | } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) { |
---|
736 | if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) { |
---|
737 | fprintf(stderr, "Error: can not set mode to active\n"); |
---|
738 | } else { |
---|
739 | printf("going into active mode\n"); |
---|
740 | } |
---|
741 | |
---|
742 | } else if ( !strcmp(k, "exit") ) { |
---|
743 | if ( roar_exit(&con) == -1 ) { |
---|
744 | fprintf(stderr, "Error: can not quit server\n"); |
---|
745 | } else { |
---|
746 | printf("Server quited\n"); |
---|
747 | break; |
---|
748 | } |
---|
749 | } else if ( !strcmp(k, "terminate") ) { |
---|
750 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
751 | fprintf(stderr, "Error: can not terminate server\n"); |
---|
752 | } else { |
---|
753 | printf("Server got asked to quited\n"); |
---|
754 | break; |
---|
755 | } |
---|
756 | |
---|
757 | } else if ( !strcmp(k, "standbymode") ) { |
---|
758 | t = roar_get_standby(&con); |
---|
759 | if ( t == -1 ) { |
---|
760 | fprintf(stderr, "Error: can not get stanby mode\n"); |
---|
761 | } else if ( t == ROAR_STANDBY_ACTIVE ) { |
---|
762 | printf("Server is in standby\n"); |
---|
763 | } else if ( t == ROAR_STANDBY_INACTIVE ) { |
---|
764 | printf("Server is active\n"); |
---|
765 | } else { |
---|
766 | fprintf(stderr, "Error: unknown standby mode: %i\n", t); |
---|
767 | } |
---|
768 | |
---|
769 | } else if ( !strcmp(k, "whoami") ) { |
---|
770 | printf("My client ID is: %i\n", roar_get_clientid(&con)); |
---|
771 | } else if ( !strcmp(k, "serveroinfo") ) { |
---|
772 | server_oinfo(&con); |
---|
773 | } else if ( !strcmp(k, "listclients") ) { |
---|
774 | list_clients(&con); |
---|
775 | } else if ( !strcmp(k, "liststreams") ) { |
---|
776 | list_streams(&con); |
---|
777 | } else if ( !strcmp(k, "allinfo") ) { |
---|
778 | server_oinfo(&con); |
---|
779 | printf("\n"); |
---|
780 | list_clients(&con); |
---|
781 | printf("\n"); |
---|
782 | list_streams(&con); |
---|
783 | |
---|
784 | } else if ( !strcmp(k, "kick") ) { |
---|
785 | k = argv[++i]; |
---|
786 | if ( !strcmp(k, "client") ) { |
---|
787 | t = ROAR_OT_CLIENT; |
---|
788 | } else if ( !strcmp(k, "stream") ) { |
---|
789 | t = ROAR_OT_STREAM; |
---|
790 | } else if ( !strcmp(k, "sample") ) { |
---|
791 | t = ROAR_OT_SAMPLE; |
---|
792 | } else if ( !strcmp(k, "source") ) { |
---|
793 | t = ROAR_OT_SOURCE; |
---|
794 | } else { |
---|
795 | fprintf(stderr, "Error: unknown type: %s\n", k); |
---|
796 | continue; |
---|
797 | } |
---|
798 | //t = atoi(argv[i++]); |
---|
799 | if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) { |
---|
800 | fprintf(stderr, "Error: can not kick %s\n", k); |
---|
801 | } else { |
---|
802 | printf("%s kicked\n", k); |
---|
803 | } |
---|
804 | |
---|
805 | } else if ( !strcmp(k, "volume") ) { |
---|
806 | if ( set_mixer(&con, &i, argc, argv) == -1 ) { |
---|
807 | fprintf(stderr, "Error: can not set volume\n"); |
---|
808 | } else { |
---|
809 | printf("volume changed\n"); |
---|
810 | } |
---|
811 | |
---|
812 | } else if ( !strcmp(k, "flag") ) { |
---|
813 | i++; |
---|
814 | if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) { |
---|
815 | fprintf(stderr, "Error: can not set flags\n"); |
---|
816 | } else { |
---|
817 | printf("flags changed\n"); |
---|
818 | } |
---|
819 | i++; |
---|
820 | } else if ( !strcmp(k, "unflag") ) { |
---|
821 | i++; |
---|
822 | if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) { |
---|
823 | fprintf(stderr, "Error: can not reset flags\n"); |
---|
824 | } else { |
---|
825 | printf("flags changed\n"); |
---|
826 | } |
---|
827 | i++; |
---|
828 | } else if ( !strcmp(k, "metaset") ) { |
---|
829 | i++; |
---|
830 | if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) { |
---|
831 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
832 | } else { |
---|
833 | printf("meta data changed\n"); |
---|
834 | } |
---|
835 | i += 3; |
---|
836 | } else if ( !strcmp(k, "metaget") ) { |
---|
837 | i++; |
---|
838 | if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
839 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
840 | } |
---|
841 | i++; |
---|
842 | } else if ( !strcmp(k, "metasave") ) { |
---|
843 | i++; |
---|
844 | if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
845 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
846 | } else { |
---|
847 | printf("meta data saved\n"); |
---|
848 | } |
---|
849 | i++; |
---|
850 | } else if ( !strcmp(k, "metaload") ) { |
---|
851 | i++; |
---|
852 | if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
853 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
854 | } else { |
---|
855 | printf("meta data saved\n"); |
---|
856 | } |
---|
857 | i++; |
---|
858 | |
---|
859 | } else { |
---|
860 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
861 | } |
---|
862 | |
---|
863 | } |
---|
864 | |
---|
865 | roar_disconnect(&con); |
---|
866 | |
---|
867 | return 0; |
---|
868 | } |
---|
869 | |
---|
870 | //ll |
---|