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 | char * infotext; |
---|
240 | |
---|
241 | |
---|
242 | if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
243 | fprintf(stderr, "Error: can not get stream list\n"); |
---|
244 | return; |
---|
245 | } |
---|
246 | |
---|
247 | for (i = 0; i < num; i++) { |
---|
248 | printf("stream %i:\n", id[i]); |
---|
249 | if ( roar_get_stream(con, &s, id[i]) == -1 ) { |
---|
250 | fprintf(stderr, "Error: can not get stream info\n"); |
---|
251 | continue; |
---|
252 | } |
---|
253 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
254 | |
---|
255 | if ( roar_stream_get_name(con, &s, name, 1024) == 0 ) |
---|
256 | printf("Stream name : %s\n", name); |
---|
257 | |
---|
258 | if ( s.pos_rel_id == -1 ) { |
---|
259 | printf("Relativ position id : none (stream not synchronized)\n"); |
---|
260 | } else if ( s.pos_rel_id == id[i] ) { |
---|
261 | printf("Relativ position id : %i (self synchronized)\n", s.pos_rel_id); |
---|
262 | } else { |
---|
263 | printf("Relativ position id : %i (synchronized)\n", s.pos_rel_id); |
---|
264 | } |
---|
265 | if ( g_verbose > 1 ) { |
---|
266 | if ( s.info.rate && s.info.channels ) { |
---|
267 | printf("Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, |
---|
268 | (float)s.pos/(s.info.rate*s.info.channels)); |
---|
269 | } else { |
---|
270 | printf("Position : %lu S\n", (unsigned long int) s.pos); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | switch (s.dir) { |
---|
275 | case ROAR_DIR_MIDI_IN: |
---|
276 | case ROAR_DIR_MIDI_OUT: |
---|
277 | infotext = " ticks/s"; |
---|
278 | break; |
---|
279 | case ROAR_DIR_LIGHT_IN: |
---|
280 | case ROAR_DIR_LIGHT_OUT: |
---|
281 | infotext = " updates/s"; |
---|
282 | break; |
---|
283 | default: |
---|
284 | infotext = " Hz"; |
---|
285 | } |
---|
286 | if ( s.info.rate ) |
---|
287 | printf("Stream sample rate : %i%s\n", s.info.rate, infotext); |
---|
288 | if ( s.info.bits ) |
---|
289 | printf("Stream bits : %i\n", s.info.bits); |
---|
290 | if ( s.info.channels ) |
---|
291 | printf("Stream channels : %i\n", s.info.channels); |
---|
292 | |
---|
293 | printf("Stream codec : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
294 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
295 | if ( roar_stream_get_info(con, &s, &info) != -1 ) { |
---|
296 | if ( info.codec != s.info.codec ) { |
---|
297 | printf("Stremed codec : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec), |
---|
298 | info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
299 | } |
---|
300 | |
---|
301 | if ( g_verbose ) { |
---|
302 | if ( info.block_size ) |
---|
303 | printf("Stream block size : %i Byte\n", info.block_size); |
---|
304 | |
---|
305 | printf("Underruns pre/post : %i/%i\n", info.pre_underruns, info.post_underruns); |
---|
306 | if ( g_verbose > 1 ) |
---|
307 | printf("Stream delay : %ims\n", (int)info.delay/1000); |
---|
308 | |
---|
309 | *flags = 0; |
---|
310 | if ( info.flags & ROAR_FLAG_PRIMARY ) |
---|
311 | strcat(flags, "primary "); |
---|
312 | if ( info.flags & ROAR_FLAG_SYNC ) |
---|
313 | strcat(flags, "sync "); |
---|
314 | if ( info.flags & ROAR_FLAG_OUTPUT ) |
---|
315 | strcat(flags, "output "); |
---|
316 | if ( info.flags & ROAR_FLAG_SOURCE ) |
---|
317 | strcat(flags, "source "); |
---|
318 | if ( info.flags & ROAR_FLAG_META ) |
---|
319 | strcat(flags, "meta "); |
---|
320 | if ( info.flags & ROAR_FLAG_AUTOCONF ) |
---|
321 | strcat(flags, "autoconf "); |
---|
322 | if ( info.flags & ROAR_FLAG_CLEANMETA ) |
---|
323 | strcat(flags, "cleanmeta "); |
---|
324 | if ( info.flags & ROAR_FLAG_HWMIXER ) |
---|
325 | strcat(flags, "hwmixer "); |
---|
326 | if ( info.flags & ROAR_FLAG_PAUSE ) |
---|
327 | strcat(flags, "pause "); |
---|
328 | if ( info.flags & ROAR_FLAG_MUTE ) |
---|
329 | strcat(flags, "mute "); |
---|
330 | if ( info.flags & ROAR_FLAG_MMAP ) |
---|
331 | strcat(flags, "mmap "); |
---|
332 | if ( info.flags & ROAR_FLAG_ANTIECHO ) |
---|
333 | strcat(flags, "antiecho "); |
---|
334 | if ( info.flags & ROAR_FLAG_VIRTUAL ) |
---|
335 | strcat(flags, "virtual "); |
---|
336 | if ( info.flags & ROAR_FLAG_RECSOURCE ) |
---|
337 | strcat(flags, "recsource "); |
---|
338 | if ( info.flags & ROAR_FLAG_PASSMIXER ) |
---|
339 | strcat(flags, "passmixer "); |
---|
340 | |
---|
341 | printf("Flags : %s\n", flags); |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | if ( s.dir != ROAR_DIR_THRU ) { |
---|
346 | display_mixer(con, id[i]); |
---|
347 | show_meta_all(con, id[i]); |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | } |
---|
352 | |
---|
353 | int display_mixer (struct roar_connection * con, int stream) { |
---|
354 | int channels; |
---|
355 | struct roar_mixer_settings mixer; |
---|
356 | int i; |
---|
357 | |
---|
358 | if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) { |
---|
359 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
360 | return -1; |
---|
361 | } |
---|
362 | |
---|
363 | for (i = 0; i < channels; i++) |
---|
364 | printf("Mixer volume chan %2i : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35); |
---|
365 | |
---|
366 | return 0; |
---|
367 | } |
---|
368 | |
---|
369 | int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) { |
---|
370 | int chans = 0; |
---|
371 | int id; |
---|
372 | int i; |
---|
373 | int len; |
---|
374 | int old_chans; |
---|
375 | int vol_l, vol_r; |
---|
376 | char * k; |
---|
377 | struct roar_mixer_settings mixer; |
---|
378 | struct roar_mixer_settings old_mixer; |
---|
379 | |
---|
380 | if (*cur + 2 > max) |
---|
381 | return -1; |
---|
382 | |
---|
383 | id = atoi(arg[++(*cur)]); |
---|
384 | |
---|
385 | k = arg[++(*cur)]; |
---|
386 | |
---|
387 | if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) { |
---|
388 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
389 | return -1; |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | // TODO: clean up code here as the % vs. abs code is very duplicate... |
---|
394 | |
---|
395 | if ( strcmp(k, "mono") == 0 && old_chans != 1 ) { |
---|
396 | chans = 1; |
---|
397 | |
---|
398 | if ( *cur + 1 > max ) |
---|
399 | return -1; |
---|
400 | |
---|
401 | k = arg[++(*cur)]; |
---|
402 | len = strlen(k); |
---|
403 | |
---|
404 | if ( k[len - 1] == '%' ) { |
---|
405 | k[len - 1] = 0; |
---|
406 | vol_l = (atof(k)*65535)/100; |
---|
407 | } else { |
---|
408 | vol_l = atoi(k); |
---|
409 | } |
---|
410 | |
---|
411 | for (i = 0; i < old_chans; i++) |
---|
412 | mixer.mixer[i] = vol_l; |
---|
413 | |
---|
414 | chans = old_chans; |
---|
415 | |
---|
416 | } else if ( strcmp(k, "stereo") == 0 && old_chans == 4 ) { |
---|
417 | chans = 4; |
---|
418 | |
---|
419 | if ( *cur + 2 > max ) |
---|
420 | return -1; |
---|
421 | |
---|
422 | k = arg[++(*cur)]; |
---|
423 | len = strlen(k); |
---|
424 | |
---|
425 | if ( k[len - 1] == '%' ) { |
---|
426 | k[len - 1] = 0; |
---|
427 | vol_l = (atof(k)*65535)/100; |
---|
428 | } else { |
---|
429 | vol_l = atoi(k); |
---|
430 | } |
---|
431 | |
---|
432 | k = arg[++(*cur)]; |
---|
433 | len = strlen(k); |
---|
434 | |
---|
435 | if ( k[len - 1] == '%' ) { |
---|
436 | k[len - 1] = 0; |
---|
437 | vol_r = (atof(k)*65535)/100; |
---|
438 | } else { |
---|
439 | vol_r = atoi(k); |
---|
440 | } |
---|
441 | |
---|
442 | mixer.mixer[0] = vol_l; |
---|
443 | mixer.mixer[1] = vol_r; |
---|
444 | mixer.mixer[2] = vol_l; |
---|
445 | mixer.mixer[3] = vol_r; |
---|
446 | |
---|
447 | } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) { |
---|
448 | chans = 2; |
---|
449 | // printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
450 | ROAR_ERR("mode stereo not supported"); |
---|
451 | return -1; |
---|
452 | } else { |
---|
453 | if ( strcmp(k, "mono") == 0 ) { |
---|
454 | chans = 1; |
---|
455 | } else if ( strcmp(k, "stereo") == 0 ) { |
---|
456 | chans = 2; |
---|
457 | } else { |
---|
458 | chans = atoi(k); |
---|
459 | } |
---|
460 | |
---|
461 | // printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
462 | |
---|
463 | if ( *cur + chans > max ) |
---|
464 | return -1; |
---|
465 | |
---|
466 | for (i = 0; i < chans; i++) { |
---|
467 | k = arg[++(*cur)]; |
---|
468 | len = strlen(k); |
---|
469 | |
---|
470 | if ( k[len - 1] == '%' ) { |
---|
471 | k[len - 1] = 0; |
---|
472 | mixer.mixer[i] = (atof(k)*(int)65535)/100; |
---|
473 | } else { |
---|
474 | mixer.mixer[i] = atoi(k); |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | mixer.scale = 65535; |
---|
480 | |
---|
481 | return roar_set_vol(con, id, &mixer, chans); |
---|
482 | } |
---|
483 | |
---|
484 | int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) { |
---|
485 | struct roar_meta meta; |
---|
486 | struct roar_stream s; |
---|
487 | int mode_i = ROAR_META_MODE_SET; |
---|
488 | |
---|
489 | memset(&s, 0, sizeof(s)); |
---|
490 | |
---|
491 | s.id = id; |
---|
492 | |
---|
493 | // printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val); |
---|
494 | |
---|
495 | if ( strcmp(mode, "add") == 0 ) { |
---|
496 | mode_i = ROAR_META_MODE_ADD; |
---|
497 | } |
---|
498 | |
---|
499 | meta.type = roar_meta_inttype(type); |
---|
500 | meta.value = val; |
---|
501 | meta.key[0] = 0; |
---|
502 | |
---|
503 | if ( meta.type == -1 ) { |
---|
504 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
505 | return -1; |
---|
506 | } |
---|
507 | |
---|
508 | // printf("D: type=%i, mode=%i\n", meta.type, mode_i); |
---|
509 | |
---|
510 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
511 | return -1; |
---|
512 | |
---|
513 | meta.type = ROAR_META_TYPE_NONE; |
---|
514 | meta.value = NULL; |
---|
515 | |
---|
516 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
517 | } |
---|
518 | |
---|
519 | int load_meta (struct roar_connection * con, int id, char * file) { |
---|
520 | struct roar_meta meta; |
---|
521 | struct roar_stream s; |
---|
522 | int mode_i = ROAR_META_MODE_SET; |
---|
523 | FILE * in; |
---|
524 | char lion[1024]; |
---|
525 | char * v; |
---|
526 | |
---|
527 | memset(&s, 0, sizeof(s)); |
---|
528 | |
---|
529 | s.id = id; |
---|
530 | |
---|
531 | if ( (in = fopen(file, "r")) == NULL ) |
---|
532 | return -1; |
---|
533 | |
---|
534 | while (fgets(lion, 1024, in) != NULL) { |
---|
535 | if ( (v = strtok(lion, "\r\n")) != NULL ) |
---|
536 | if ( (v = strtok(NULL, "\r\n")) != NULL ) |
---|
537 | *(v-1) = 0; |
---|
538 | |
---|
539 | if ( (v = strstr(lion, "=")) == NULL ) { |
---|
540 | fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion); |
---|
541 | continue; |
---|
542 | } |
---|
543 | |
---|
544 | *v++ = 0; |
---|
545 | |
---|
546 | meta.type = roar_meta_inttype(lion); |
---|
547 | meta.value = v; |
---|
548 | meta.key[0] = 0; |
---|
549 | |
---|
550 | if ( meta.type == -1 ) { |
---|
551 | fprintf(stderr, "Error: unknown type: %s\n", lion); |
---|
552 | continue; |
---|
553 | } |
---|
554 | |
---|
555 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
556 | return -1; |
---|
557 | } |
---|
558 | |
---|
559 | fclose(in); |
---|
560 | |
---|
561 | meta.type = ROAR_META_TYPE_NONE; |
---|
562 | meta.value = NULL; |
---|
563 | |
---|
564 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
565 | } |
---|
566 | |
---|
567 | int show_meta_type (struct roar_connection * con, int id, char * type) { |
---|
568 | struct roar_meta meta; |
---|
569 | struct roar_stream s; |
---|
570 | |
---|
571 | memset(&s, 0, sizeof(s)); |
---|
572 | |
---|
573 | s.id = id; |
---|
574 | |
---|
575 | meta.type = roar_meta_inttype(type); |
---|
576 | |
---|
577 | if ( meta.type == -1 ) { |
---|
578 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
579 | return -1; |
---|
580 | } |
---|
581 | |
---|
582 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
583 | return -1; |
---|
584 | |
---|
585 | printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
586 | |
---|
587 | roar_meta_free(&meta); |
---|
588 | |
---|
589 | return 0; |
---|
590 | } |
---|
591 | |
---|
592 | int show_meta_all (struct roar_connection * con, int id) { |
---|
593 | struct roar_stream s; |
---|
594 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
595 | int i; |
---|
596 | int len; |
---|
597 | |
---|
598 | memset(&s, 0, sizeof(s)); |
---|
599 | |
---|
600 | s.id = id; |
---|
601 | |
---|
602 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
603 | return -1; |
---|
604 | |
---|
605 | for (i = 0; i < len; i++) |
---|
606 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
607 | |
---|
608 | return 0; |
---|
609 | } |
---|
610 | |
---|
611 | int save_meta (struct roar_connection * con, int id, char * file) { |
---|
612 | struct roar_stream s; |
---|
613 | struct roar_meta meta; |
---|
614 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
615 | int i; |
---|
616 | int len; |
---|
617 | FILE * out; |
---|
618 | |
---|
619 | memset(&s, 0, sizeof(s)); |
---|
620 | |
---|
621 | s.id = id; |
---|
622 | |
---|
623 | if ( (out = fopen(file, "w")) == NULL ) |
---|
624 | return -1; |
---|
625 | |
---|
626 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
627 | return -1; |
---|
628 | |
---|
629 | for (i = 0; i < len; i++) { |
---|
630 | /* |
---|
631 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
632 | */ |
---|
633 | meta.type = types[i]; |
---|
634 | |
---|
635 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
636 | continue; |
---|
637 | |
---|
638 | // printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
639 | |
---|
640 | fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value); |
---|
641 | |
---|
642 | roar_meta_free(&meta); |
---|
643 | } |
---|
644 | |
---|
645 | fclose(out); |
---|
646 | |
---|
647 | return 0; |
---|
648 | } |
---|
649 | |
---|
650 | int set_flags (struct roar_connection * con, int id, int reset, char * flags) { |
---|
651 | int f = ROAR_FLAG_NONE; |
---|
652 | char * c; |
---|
653 | struct roar_stream s[1]; |
---|
654 | |
---|
655 | memset(s, 0, sizeof(struct roar_stream)); |
---|
656 | |
---|
657 | s->id = id; |
---|
658 | |
---|
659 | c = strtok(flags, ","); |
---|
660 | while (c != NULL) { |
---|
661 | if ( !strcmp(c, "meta") ) { |
---|
662 | f |= ROAR_FLAG_META; |
---|
663 | } else if ( !strcmp(c, "primary") ) { |
---|
664 | f |= ROAR_FLAG_PRIMARY; |
---|
665 | } else if ( !strcmp(c, "sync") ) { |
---|
666 | f |= ROAR_FLAG_SYNC; |
---|
667 | } else if ( !strcmp(c, "cleanmeta") ) { |
---|
668 | f |= ROAR_FLAG_CLEANMETA; |
---|
669 | } else if ( !strcmp(c, "hwmixer") ) { |
---|
670 | f |= ROAR_FLAG_HWMIXER; |
---|
671 | } else if ( !strcmp(c, "pause") ) { |
---|
672 | f |= ROAR_FLAG_PAUSE; |
---|
673 | } else if ( !strcmp(c, "mute") ) { |
---|
674 | f |= ROAR_FLAG_MUTE; |
---|
675 | } else if ( !strcmp(c, "mmap") ) { |
---|
676 | f |= ROAR_FLAG_MMAP; |
---|
677 | } else if ( !strcmp(c, "antiecho") ) { |
---|
678 | f |= ROAR_FLAG_ANTIECHO; |
---|
679 | } else if ( !strcmp(c, "recsource") ) { |
---|
680 | f |= ROAR_FLAG_RECSOURCE; |
---|
681 | } else if ( !strcmp(c, "passmixer") ) { |
---|
682 | f |= ROAR_FLAG_PASSMIXER; |
---|
683 | } else { |
---|
684 | fprintf(stderr, "Error: unknown flag: %s\n", c); |
---|
685 | return -1; |
---|
686 | } |
---|
687 | |
---|
688 | c = strtok(NULL, ","); |
---|
689 | } |
---|
690 | |
---|
691 | return roar_stream_set_flags(con, s, f, reset); |
---|
692 | } |
---|
693 | |
---|
694 | int main (int argc, char * argv[]) { |
---|
695 | struct roar_connection con; |
---|
696 | char * server = NULL; |
---|
697 | char * k = NULL; |
---|
698 | int i; |
---|
699 | int t = 0; |
---|
700 | |
---|
701 | for (i = 1; i < argc; i++) { |
---|
702 | k = argv[i]; |
---|
703 | |
---|
704 | if ( strcmp(k, "--server") == 0 ) { |
---|
705 | server = argv[++i]; |
---|
706 | } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) { |
---|
707 | g_verbose++; |
---|
708 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
709 | usage(); |
---|
710 | return 0; |
---|
711 | } else if ( *k == '-' ) { |
---|
712 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
713 | usage(); |
---|
714 | return 1; |
---|
715 | } else { |
---|
716 | break; |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | // connect |
---|
721 | |
---|
722 | if ( roar_connect(&con, server) == -1 ) { |
---|
723 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
724 | return 1; |
---|
725 | } |
---|
726 | |
---|
727 | if ( roar_identify(&con, "roarctl") == -1 ) { |
---|
728 | fprintf(stderr, "Error: Can not identify to server\n"); |
---|
729 | return 1; |
---|
730 | } |
---|
731 | |
---|
732 | if ( i == argc ) { |
---|
733 | fprintf(stderr, "Error: No Commands given\n"); |
---|
734 | return 0; // this is not a fatal error... |
---|
735 | } |
---|
736 | |
---|
737 | for (; i < argc; i++) { |
---|
738 | k = argv[i]; |
---|
739 | // cmd is in k |
---|
740 | |
---|
741 | printf("--- [ %s ] ---\n", k); |
---|
742 | |
---|
743 | if ( !strcmp(k, "help") ) { |
---|
744 | usage(); |
---|
745 | |
---|
746 | } else if ( !strcmp(k, "sleep") ) { |
---|
747 | sleep(atoi(argv[++i])); |
---|
748 | |
---|
749 | } else if ( !strcmp(k, "ping") ) { |
---|
750 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
751 | if ( ping(&con, atoi(argv[++i])) == -1 ) { |
---|
752 | fprintf(stderr, "Error: can not ping\n"); |
---|
753 | } |
---|
754 | #else |
---|
755 | fprintf(stderr, "Error: ping not supported.\n"); |
---|
756 | i++; |
---|
757 | #endif |
---|
758 | |
---|
759 | } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) { |
---|
760 | if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) { |
---|
761 | fprintf(stderr, "Error: can not set mode to standby\n"); |
---|
762 | } else { |
---|
763 | printf("going into standby\n"); |
---|
764 | } |
---|
765 | } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) { |
---|
766 | if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) { |
---|
767 | fprintf(stderr, "Error: can not set mode to active\n"); |
---|
768 | } else { |
---|
769 | printf("going into active mode\n"); |
---|
770 | } |
---|
771 | |
---|
772 | } else if ( !strcmp(k, "exit") ) { |
---|
773 | if ( roar_exit(&con) == -1 ) { |
---|
774 | fprintf(stderr, "Error: can not quit server\n"); |
---|
775 | } else { |
---|
776 | printf("Server quited\n"); |
---|
777 | break; |
---|
778 | } |
---|
779 | } else if ( !strcmp(k, "terminate") ) { |
---|
780 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
781 | fprintf(stderr, "Error: can not terminate server\n"); |
---|
782 | } else { |
---|
783 | printf("Server got asked to quited\n"); |
---|
784 | break; |
---|
785 | } |
---|
786 | |
---|
787 | } else if ( !strcmp(k, "standbymode") ) { |
---|
788 | t = roar_get_standby(&con); |
---|
789 | if ( t == -1 ) { |
---|
790 | fprintf(stderr, "Error: can not get stanby mode\n"); |
---|
791 | } else if ( t == ROAR_STANDBY_ACTIVE ) { |
---|
792 | printf("Server is in standby\n"); |
---|
793 | } else if ( t == ROAR_STANDBY_INACTIVE ) { |
---|
794 | printf("Server is active\n"); |
---|
795 | } else { |
---|
796 | fprintf(stderr, "Error: unknown standby mode: %i\n", t); |
---|
797 | } |
---|
798 | |
---|
799 | } else if ( !strcmp(k, "whoami") ) { |
---|
800 | printf("My client ID is: %i\n", roar_get_clientid(&con)); |
---|
801 | } else if ( !strcmp(k, "serveroinfo") ) { |
---|
802 | server_oinfo(&con); |
---|
803 | } else if ( !strcmp(k, "listclients") ) { |
---|
804 | list_clients(&con); |
---|
805 | } else if ( !strcmp(k, "liststreams") ) { |
---|
806 | list_streams(&con); |
---|
807 | } else if ( !strcmp(k, "allinfo") ) { |
---|
808 | server_oinfo(&con); |
---|
809 | printf("\n"); |
---|
810 | list_clients(&con); |
---|
811 | printf("\n"); |
---|
812 | list_streams(&con); |
---|
813 | |
---|
814 | } else if ( !strcmp(k, "kick") ) { |
---|
815 | k = argv[++i]; |
---|
816 | if ( !strcmp(k, "client") ) { |
---|
817 | t = ROAR_OT_CLIENT; |
---|
818 | } else if ( !strcmp(k, "stream") ) { |
---|
819 | t = ROAR_OT_STREAM; |
---|
820 | } else if ( !strcmp(k, "sample") ) { |
---|
821 | t = ROAR_OT_SAMPLE; |
---|
822 | } else if ( !strcmp(k, "source") ) { |
---|
823 | t = ROAR_OT_SOURCE; |
---|
824 | } else { |
---|
825 | fprintf(stderr, "Error: unknown type: %s\n", k); |
---|
826 | continue; |
---|
827 | } |
---|
828 | //t = atoi(argv[i++]); |
---|
829 | if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) { |
---|
830 | fprintf(stderr, "Error: can not kick %s\n", k); |
---|
831 | } else { |
---|
832 | printf("%s kicked\n", k); |
---|
833 | } |
---|
834 | |
---|
835 | } else if ( !strcmp(k, "volume") ) { |
---|
836 | if ( set_mixer(&con, &i, argc, argv) == -1 ) { |
---|
837 | fprintf(stderr, "Error: can not set volume\n"); |
---|
838 | } else { |
---|
839 | printf("volume changed\n"); |
---|
840 | } |
---|
841 | |
---|
842 | } else if ( !strcmp(k, "flag") ) { |
---|
843 | i++; |
---|
844 | if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) { |
---|
845 | fprintf(stderr, "Error: can not set flags\n"); |
---|
846 | } else { |
---|
847 | printf("flags changed\n"); |
---|
848 | } |
---|
849 | i++; |
---|
850 | } else if ( !strcmp(k, "unflag") ) { |
---|
851 | i++; |
---|
852 | if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) { |
---|
853 | fprintf(stderr, "Error: can not reset flags\n"); |
---|
854 | } else { |
---|
855 | printf("flags changed\n"); |
---|
856 | } |
---|
857 | i++; |
---|
858 | } else if ( !strcmp(k, "metaset") ) { |
---|
859 | i++; |
---|
860 | if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) { |
---|
861 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
862 | } else { |
---|
863 | printf("meta data changed\n"); |
---|
864 | } |
---|
865 | i += 3; |
---|
866 | } else if ( !strcmp(k, "metaget") ) { |
---|
867 | i++; |
---|
868 | if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
869 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
870 | } |
---|
871 | i++; |
---|
872 | } else if ( !strcmp(k, "metasave") ) { |
---|
873 | i++; |
---|
874 | if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
875 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
876 | } else { |
---|
877 | printf("meta data saved\n"); |
---|
878 | } |
---|
879 | i++; |
---|
880 | } else if ( !strcmp(k, "metaload") ) { |
---|
881 | i++; |
---|
882 | if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
883 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
884 | } else { |
---|
885 | printf("meta data saved\n"); |
---|
886 | } |
---|
887 | i++; |
---|
888 | |
---|
889 | } else { |
---|
890 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
891 | } |
---|
892 | |
---|
893 | } |
---|
894 | |
---|
895 | roar_disconnect(&con); |
---|
896 | |
---|
897 | return 0; |
---|
898 | } |
---|
899 | |
---|
900 | //ll |
---|