1 | //roarctl.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * ckport: ignore-symbol: roar_req of target libroar0 -- Used within ping command. |
---|
28 | */ |
---|
29 | |
---|
30 | #define _UNITS_T_BASE_USEC |
---|
31 | |
---|
32 | #include <roaraudio.h> |
---|
33 | #include <roaraudio/units.h> |
---|
34 | #include <libroardsp/libroardsp.h> |
---|
35 | |
---|
36 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) |
---|
37 | #define _POSIX_USERS |
---|
38 | #endif |
---|
39 | |
---|
40 | #ifdef _POSIX_USERS |
---|
41 | #include <pwd.h> |
---|
42 | #include <grp.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | #include <sys/time.h> |
---|
46 | #include <time.h> |
---|
47 | |
---|
48 | #ifdef ROAR_HAVE_LIBM |
---|
49 | #include <math.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | int g_verbose = 0; |
---|
53 | |
---|
54 | void display_client (struct roar_connection * con, int id); |
---|
55 | void display_stream (struct roar_connection * con, int id); |
---|
56 | int display_mixer (struct roar_connection * con, int stream); |
---|
57 | int show_meta_all (struct roar_connection * con, int id); |
---|
58 | |
---|
59 | void usage (void) { |
---|
60 | printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n"); |
---|
61 | |
---|
62 | printf("\nOptions:\n\n"); |
---|
63 | |
---|
64 | printf(" --server SERVER - Set server hostname\n" |
---|
65 | " --help - Show this help\n" |
---|
66 | " --verbose -v - Show verbose output\n" |
---|
67 | " --list-aiprofiles - Show audio info profiles and exit\n" |
---|
68 | " --list-libstandards - Gets list of library (libroar) supported standards\n" |
---|
69 | " --enum-servers - Show a list of possible servers\n" |
---|
70 | " --hash-password - Hash a password and exit\n" |
---|
71 | ); |
---|
72 | |
---|
73 | printf("\nCommands:\n\n"); |
---|
74 | printf( |
---|
75 | " help - Show this help\n" |
---|
76 | " sleep TIME - Sleeps for TIME seconds\n" |
---|
77 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
78 | " ping NUM - Do NUM pings using NOOP commands\n" |
---|
79 | #endif |
---|
80 | " whoami - Get own client ID\n" |
---|
81 | "\n" |
---|
82 | " beep - Ask the server to beep.\n" |
---|
83 | "\n" |
---|
84 | " listaiprofiles - list audio info profiles\n" |
---|
85 | " aiprofileget PROFILE - show audio info profile PROFILE\n" |
---|
86 | "\n" |
---|
87 | " standby, off - Go into standby mode\n" |
---|
88 | " resume, on - Go into active mode\n" |
---|
89 | " standbymode - Show current standby mode\n" |
---|
90 | " exit - Quits the roard (must be used as last command)\n" |
---|
91 | " terminate - Like exit but let the server up to serve still connected clients,\n" |
---|
92 | " new clients cann't connect and the server terminates after the last\n" |
---|
93 | " client disconnected\n" |
---|
94 | "\n" |
---|
95 | " volume ID CHAN [scale S] V0 V1...\n" |
---|
96 | " - Sets volume for stream ID\n" |
---|
97 | " CHAN is the number of channels or 'mono' or 'stereo'\n" |
---|
98 | " if mono or stereo is chosen roarctl trys to set\n" |
---|
99 | " sensfull values for all channels even if the output\n" |
---|
100 | " is has more channels.\n" |
---|
101 | " An optional scale can be given using S.\n" |
---|
102 | " all other args are the volumes of the channels\n" |
---|
103 | " you may use integer or percent values.\n" |
---|
104 | " percent values can floating points.\n" |
---|
105 | "\n" |
---|
106 | " flag ID FLAGS - Sets flags FLAGS on stream ID. FLAGS may be a comma\n" |
---|
107 | " separated list of flags.\n" |
---|
108 | " unflag ID FLAGS - Unsets flags on a stream. See flag.\n" |
---|
109 | " toogleflag ID FLAGS - Toggels flags on a stream. See flag.\n" |
---|
110 | " protectflag ID FLAGS - Protects flags on a stream. Those flags can not be\n" |
---|
111 | " changed anymore after the they got protected. See flag.\n" |
---|
112 | "\n" |
---|
113 | " role ID ROLE - Set role ROLE on stream ID.\n" |
---|
114 | "\n" |
---|
115 | " kick TYPE ID - Kicks object of TYPE with id ID\n" |
---|
116 | " Types: client stream sample source\n" |
---|
117 | " newvirtual P D E R B C - Adds a new virtual (child) stream\n" |
---|
118 | " Parameters:\n" |
---|
119 | " P: Parent stream ID, D: Direction,\n" |
---|
120 | " E: codEc, R: sample Rate,\n" |
---|
121 | " B: bits per sample, C: nummer of channels\n" |
---|
122 | "\n" |
---|
123 | " metaget ID TYPE - Read meta date of type TYPE from stream ID\n" |
---|
124 | // TODO: document metaset here. |
---|
125 | " metasave ID FILE - Saves meta data of stream ID to file FILE\n" |
---|
126 | " metaload ID FILE - Loads meta data from file FILE and set it on stream ID\n" |
---|
127 | "\n" |
---|
128 | " serverinfo - Gets general information about the server\n" |
---|
129 | " servertime - Gets server's time\n" |
---|
130 | " serveroinfo - Gets Information about server output\n" |
---|
131 | " serveroinfo2 DIR - Gets Information about server output for stream direction dir\n" |
---|
132 | "\n" |
---|
133 | " serverstandards - Gets list of server supported standards\n" |
---|
134 | " libstandards - Gets list of library (libroar) supported standards\n" |
---|
135 | "\n" |
---|
136 | " listclients - Gets Information about clients\n" |
---|
137 | " clientinfo ID - Gets Information about client ID\n" |
---|
138 | " liststreams - Gets Information about streams\n" |
---|
139 | " streaminfo ID - Gets Information about stream ID\n" |
---|
140 | " allinfo - Get all infos\n" |
---|
141 | ); |
---|
142 | } |
---|
143 | |
---|
144 | static void _bin2hex(char * out, char * in, size_t inlen, int uppercase) { |
---|
145 | const char * tab = uppercase ? "0123456789ABCDEF" : "0123456789abcdef"; |
---|
146 | unsigned char c; |
---|
147 | int nib0, nib1; |
---|
148 | |
---|
149 | for (; inlen; inlen--) { |
---|
150 | c = *(in++); |
---|
151 | nib0 = (c & 0x0F) >> 0; |
---|
152 | nib1 = (c & 0xF0) >> 4; |
---|
153 | // printf("inlen=%zu, c=%u, nibs={%i, %i}\n", inlen, (unsigned)c, nib1, nib0); |
---|
154 | *(out++) = tab[nib1]; |
---|
155 | *(out++) = tab[nib0]; |
---|
156 | } |
---|
157 | *out = 0; |
---|
158 | } |
---|
159 | |
---|
160 | int hash_password (void) { |
---|
161 | char * pw = NULL; |
---|
162 | char salt[8]; |
---|
163 | char digest[24]; |
---|
164 | char out[2*(8+24)+3+5+1] = "tiger:"; |
---|
165 | |
---|
166 | if ( roar_passwd_simple_ask_pw(&pw, "Password to hash?", NULL) == -1 ) |
---|
167 | return -1; |
---|
168 | |
---|
169 | roar_random_gen_nonce(salt, sizeof(salt)); |
---|
170 | |
---|
171 | roar_hash_salted_buffer(digest, pw, roar_mm_strlen(pw), ROAR_HT_TIGER, salt, sizeof(salt)); |
---|
172 | |
---|
173 | roar_mm_free(pw); |
---|
174 | |
---|
175 | _bin2hex(out+6, salt, sizeof(salt), 0); |
---|
176 | out[6+16] = ':'; |
---|
177 | _bin2hex(out+6+16+1, digest, sizeof(digest), 0); |
---|
178 | |
---|
179 | out[sizeof(out)-1] = 0; |
---|
180 | |
---|
181 | printf("Password: %s\n", out); |
---|
182 | |
---|
183 | return 0; |
---|
184 | } |
---|
185 | |
---|
186 | int enum_servers (void) { |
---|
187 | struct roar_server * list; |
---|
188 | struct roar_server * c; |
---|
189 | int flags = ROAR_ENUM_FLAG_DESC|ROAR_ENUM_FLAG_LOCATION; |
---|
190 | int dir = -1; |
---|
191 | int socktype = -1; |
---|
192 | int i; |
---|
193 | |
---|
194 | if ( (list = roar_enum_servers(flags, dir, socktype)) == NULL ) |
---|
195 | return -1; |
---|
196 | |
---|
197 | printf("Server Location Description\n"); |
---|
198 | printf("----------------------------------------------------------------------\n"); |
---|
199 | |
---|
200 | for (i = 0; ; i++) { |
---|
201 | c = &(list[i]); |
---|
202 | printf("%-16s %-16s %s\n", |
---|
203 | c->server == NULL ? "(default)" : c->server, |
---|
204 | c->location == NULL ? "" : c->location, |
---|
205 | c->description == NULL ? "" : c->description |
---|
206 | ); |
---|
207 | if ( c->server == NULL ) |
---|
208 | break; |
---|
209 | } |
---|
210 | |
---|
211 | roar_enum_servers_free(list); |
---|
212 | |
---|
213 | return 0; |
---|
214 | } |
---|
215 | |
---|
216 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
217 | int ping (struct roar_connection * con, int num) { |
---|
218 | struct timeval try, ans; |
---|
219 | struct roar_message m; |
---|
220 | register int ret; |
---|
221 | int i; |
---|
222 | double cur, min = 3600*1000, max = 0, sum = 0; |
---|
223 | |
---|
224 | if ( num == 0 ) |
---|
225 | return 0; |
---|
226 | |
---|
227 | for (i = 0; i < num; i++) { |
---|
228 | memset(&m, 0, sizeof(m)); |
---|
229 | |
---|
230 | m.cmd = ROAR_CMD_NOOP; |
---|
231 | m.stream = -1; |
---|
232 | m.datalen = 0; |
---|
233 | |
---|
234 | // we use roar_req() directly here because of speed. |
---|
235 | // roar_noop() does basicly the same but is a bit slower. |
---|
236 | |
---|
237 | gettimeofday(&try, NULL); |
---|
238 | ret = roar_req(con, &m, NULL); |
---|
239 | gettimeofday(&ans, NULL); |
---|
240 | |
---|
241 | if ( ret == -1 ) |
---|
242 | return -1; |
---|
243 | |
---|
244 | while (ans.tv_sec > try.tv_sec) { |
---|
245 | ans.tv_sec--; |
---|
246 | ans.tv_usec += 1000000; |
---|
247 | } |
---|
248 | ans.tv_usec -= try.tv_usec; |
---|
249 | |
---|
250 | cur = ans.tv_usec/1000.0; |
---|
251 | |
---|
252 | printf("Pong from server: seq=%i time=%.3f%cs\n", i, cur < 0.5 ? cur*1000.0 : cur, cur < 0.5 ? 'u' : 'm'); |
---|
253 | |
---|
254 | sum += cur; |
---|
255 | if ( min > cur ) |
---|
256 | min = cur; |
---|
257 | if ( cur > max ) |
---|
258 | max = cur; |
---|
259 | |
---|
260 | if ( i != (num - 1) ) |
---|
261 | roar_sleep(1); |
---|
262 | } |
---|
263 | |
---|
264 | printf("\n--- ping statistics ---\n"); |
---|
265 | printf("%i packets transmitted\n", i); |
---|
266 | printf("rtt min/avg/max = %.3f/%.3f/%.3f ms\n", min, sum/(double)i, max); |
---|
267 | |
---|
268 | return 0; |
---|
269 | } |
---|
270 | #endif |
---|
271 | |
---|
272 | #define _pm(m,n) if ( info->m != NULL ) printf("Server %-15s: %s\n", (n), (info->m)); |
---|
273 | void server_info (struct roar_connection * con) { |
---|
274 | struct roar_server_info * info = roar_server_info(con); |
---|
275 | long hostid; |
---|
276 | |
---|
277 | if ( info == NULL ) { |
---|
278 | fprintf(stderr, "Error: can not get server info\n"); |
---|
279 | return; |
---|
280 | } |
---|
281 | |
---|
282 | _pm(version, "Version"); |
---|
283 | _pm(location, "Location"); |
---|
284 | _pm(description, "Description"); |
---|
285 | _pm(contact, "Contact"); |
---|
286 | _pm(serial, "Serial"); |
---|
287 | _pm(address, "Address"); |
---|
288 | _pm(uiurl, "UI URL"); |
---|
289 | _pm(license, "License"); |
---|
290 | // _pm(hostid, "HostID"); |
---|
291 | if ( info->hostid != NULL ) { |
---|
292 | if ( sscanf(info->hostid, "0x%lx", &hostid) != 1 ) |
---|
293 | hostid = 0; |
---|
294 | |
---|
295 | if ( hostid ) { |
---|
296 | printf("Server %-15s: %s (%li.%li.%li.%li)\n", "HostID", info->hostid, |
---|
297 | ((hostid & 0x00FF0000UL) >> 16UL), |
---|
298 | ((hostid & 0xFF000000UL) >> 24UL), |
---|
299 | ((hostid & 0x000000FFUL) >> 0UL), |
---|
300 | ((hostid & 0x0000FF00UL) >> 8UL) |
---|
301 | ); |
---|
302 | } else { |
---|
303 | printf("Server %-15s: %s\n", "HostID", info->hostid); |
---|
304 | } |
---|
305 | } |
---|
306 | _pm(build, "Build"); |
---|
307 | _pm(un.sysname, "System sysname"); |
---|
308 | _pm(un.release, "System release"); |
---|
309 | _pm(un.nodename, "System nodename"); |
---|
310 | _pm(un.machine, "System machine"); |
---|
311 | |
---|
312 | roar_server_info_free(info); |
---|
313 | } |
---|
314 | #undef _pm |
---|
315 | |
---|
316 | void server_time (struct roar_connection * con) { |
---|
317 | struct roar_time time; |
---|
318 | #ifdef ROAR_HAVE_CTIME_R |
---|
319 | time_t server_time; |
---|
320 | char server_time_buf[32]; |
---|
321 | #endif |
---|
322 | |
---|
323 | |
---|
324 | if ( roar_get_time(con, &time) == -1 ) { |
---|
325 | fprintf(stderr, "Error: can not get server time: %s\n", roar_vs_strerr(roar_error)); |
---|
326 | return; |
---|
327 | } |
---|
328 | |
---|
329 | #ifdef ROAR_HAVE_CTIME_R |
---|
330 | server_time = time.t_sec; |
---|
331 | if ( ctime_r(&server_time, server_time_buf) == NULL ) |
---|
332 | server_time_buf[0] = 0; |
---|
333 | |
---|
334 | if ( server_time_buf[0] ) |
---|
335 | if ( server_time_buf[strlen(server_time_buf)-1] == '\n' ) |
---|
336 | server_time_buf[strlen(server_time_buf)-1] = 0; |
---|
337 | #endif |
---|
338 | |
---|
339 | if ( g_verbose ) { |
---|
340 | printf("Server time : %s%s%s%llu.%06llu [+%llu/2^64] sec\n", |
---|
341 | #ifdef ROAR_HAVE_CTIME_R |
---|
342 | "\"", |
---|
343 | server_time_buf, |
---|
344 | "\" ", |
---|
345 | #else |
---|
346 | "", "", "", |
---|
347 | #endif |
---|
348 | (long long unsigned int)time.t_sec, |
---|
349 | (long long unsigned int) time.t_ssec / 18446744073709LLU, |
---|
350 | (long long unsigned int) time.t_ssec); |
---|
351 | } else { |
---|
352 | if ( time.c_freq == 1000000000LL && time.t_ssec == 0 ) { |
---|
353 | printf("Server time : %s%s%s%llu sec\n", |
---|
354 | #ifdef ROAR_HAVE_CTIME_R |
---|
355 | "\"", |
---|
356 | server_time_buf, |
---|
357 | "\" ", |
---|
358 | #else |
---|
359 | "", "", "", |
---|
360 | #endif |
---|
361 | (long long unsigned int)time.t_sec); |
---|
362 | } else { |
---|
363 | printf("Server time : %s%s%s%llu.%06llu sec\n", |
---|
364 | #ifdef ROAR_HAVE_CTIME_R |
---|
365 | "\"", |
---|
366 | server_time_buf, |
---|
367 | "\" ", |
---|
368 | #else |
---|
369 | "", "", "", |
---|
370 | #endif |
---|
371 | (long long unsigned int)time.t_sec, |
---|
372 | (long long unsigned int) time.t_ssec / 18446744073709LLU); |
---|
373 | } |
---|
374 | } |
---|
375 | if ( time.c_freq ) |
---|
376 | printf("Server clock frequency: %f Hz\n", (float)((double)time.c_freq / (double)1000000000.)); |
---|
377 | if ( time.c_drift ) { |
---|
378 | #ifdef ROAR_HAVE_LIBM |
---|
379 | printf("Server clock drift : %llu:2^64 (~10^%f)\n", |
---|
380 | (long long unsigned int)time.c_drift, logf((float)time.c_drift)/2.302585f); |
---|
381 | #else |
---|
382 | printf("Server clock drift : %llu:2^64\n", |
---|
383 | (long long unsigned int)time.c_drift); |
---|
384 | #endif |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | void server_oinfo (struct roar_connection * con, int dir) { |
---|
389 | struct roar_stream s; |
---|
390 | |
---|
391 | if ( roar_server_oinfo(con, &s, dir) == -1 ) { |
---|
392 | fprintf(stderr, "Error: can not get server output info\n"); |
---|
393 | return; |
---|
394 | } |
---|
395 | |
---|
396 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
397 | printf("Server Output rate : %i Hz\n", s.info.rate); |
---|
398 | printf("Server Output bits : %i\n", s.info.bits); |
---|
399 | printf("Server Output channels: %i\n", s.info.channels); |
---|
400 | printf("Server Output codec : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
401 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
402 | // printf("Server Output rate: %i", s.info.rate); |
---|
403 | if ( g_verbose > 1 && s.pos != (uint32_t)-1 ) |
---|
404 | printf("Server Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels)); |
---|
405 | } |
---|
406 | |
---|
407 | static void print_stds (const struct roar_stds * stds) { |
---|
408 | size_t i; |
---|
409 | int vendor, standard, version; |
---|
410 | char numbuf[2][8]; |
---|
411 | const char * vendor_name; |
---|
412 | |
---|
413 | for (i = 0; i < stds->stds_len; i++) { |
---|
414 | vendor = ROAR_STD_VENDOR(stds->stds[i]); |
---|
415 | standard = ROAR_STD_STD(stds->stds[i]); |
---|
416 | version = ROAR_STD_VERSION(stds->stds[i]); |
---|
417 | |
---|
418 | if ( (vendor_name = roar_stds_vendor2str(vendor)) == NULL ) { |
---|
419 | snprintf(numbuf[0], sizeof(numbuf[0]), "%i", vendor); |
---|
420 | numbuf[0][sizeof(numbuf[0])-1] = 0; |
---|
421 | vendor_name = numbuf[0]; |
---|
422 | } |
---|
423 | |
---|
424 | if ( version == 0 ) { |
---|
425 | numbuf[1][0] = 0; |
---|
426 | } else { |
---|
427 | snprintf(numbuf[1], sizeof(numbuf[1]), "-%i", version); |
---|
428 | numbuf[1][sizeof(numbuf[1])-1] = 0; |
---|
429 | } |
---|
430 | |
---|
431 | printf("Server standard : %s-%i%s\n", vendor_name, standard, numbuf[1]); |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | void server_standards (struct roar_connection * con) { |
---|
436 | struct roar_stds * stds; |
---|
437 | |
---|
438 | if ( roar_caps_stds(con, &stds, NULL, -1) == -1 ) { |
---|
439 | fprintf(stderr, "Error: can not get server standards\n"); |
---|
440 | return; |
---|
441 | } |
---|
442 | |
---|
443 | print_stds(stds); |
---|
444 | |
---|
445 | roar_stds_free(stds); |
---|
446 | } |
---|
447 | |
---|
448 | void lib_standards (void) { |
---|
449 | print_stds(libroar_libstds()); |
---|
450 | } |
---|
451 | |
---|
452 | const char * proc_name (pid_t pid) { |
---|
453 | static char ret[80] = "?"; |
---|
454 | #ifdef __linux__ |
---|
455 | char file[80], buf[80], *r; |
---|
456 | int i; |
---|
457 | |
---|
458 | snprintf(file, sizeof(file)-1, "/proc/%i/exe", pid); |
---|
459 | file[sizeof(file)-1] = 0; |
---|
460 | |
---|
461 | ret[0] = '?'; |
---|
462 | ret[1] = 0; |
---|
463 | |
---|
464 | if ( (i = readlink(file, buf, sizeof(buf)-1)) != -1 ) { |
---|
465 | buf[i] = 0; |
---|
466 | if ( (r = strrchr(buf, '/')) != NULL ) { |
---|
467 | r++; |
---|
468 | if ( *r != 0 ) { |
---|
469 | strncpy(ret, r, sizeof(ret)-1); |
---|
470 | ret[sizeof(ret)-1] = 0; |
---|
471 | } |
---|
472 | } |
---|
473 | } |
---|
474 | #else |
---|
475 | (void)pid; |
---|
476 | #endif |
---|
477 | |
---|
478 | return ret; |
---|
479 | } |
---|
480 | |
---|
481 | void list_clients (struct roar_connection * con) { |
---|
482 | int i; |
---|
483 | int num; |
---|
484 | int id[ROAR_CLIENTS_MAX]; |
---|
485 | |
---|
486 | if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) { |
---|
487 | fprintf(stderr, "Error: can not get client list\n"); |
---|
488 | return; |
---|
489 | } |
---|
490 | |
---|
491 | for (i = 0; i < num; i++) { |
---|
492 | display_client(con, id[i]); |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | void display_client (struct roar_connection * con, int id) { |
---|
497 | static int self_id = -1; |
---|
498 | static struct roar_client self_client_store; |
---|
499 | static struct roar_client * self_client = NULL; |
---|
500 | struct roar_client c; |
---|
501 | char tmp[80]; |
---|
502 | int h; |
---|
503 | #ifdef _POSIX_USERS |
---|
504 | struct group * grp = NULL; |
---|
505 | struct passwd * pwd = NULL; |
---|
506 | #endif |
---|
507 | |
---|
508 | if ( self_id == -1 ) { |
---|
509 | if ( (self_id = roar_get_clientid(con)) != -1 ) { |
---|
510 | if ( roar_get_client(con, &self_client_store, self_id) == -1 ) { |
---|
511 | self_id = -1; |
---|
512 | } else { |
---|
513 | self_client = &self_client_store; |
---|
514 | } |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | printf("client %i:\n", id); |
---|
519 | if ( roar_get_client(con, &c, id) == -1 ) { |
---|
520 | fprintf(stderr, "Error: can not get client info\n"); |
---|
521 | return; |
---|
522 | } |
---|
523 | |
---|
524 | if ( c.name[0] != '\0' ) |
---|
525 | printf("Client name : %s\n", c.name); |
---|
526 | if ( roar_nnode_get_socktype(&(c.nnode)) != ROAR_SOCKET_TYPE_UNKNOWN ) { |
---|
527 | if ( roar_nnode_to_str(&(c.nnode), tmp, sizeof(tmp)) == 0 ) { |
---|
528 | printf("Client network node : %s\n", tmp); |
---|
529 | } |
---|
530 | } |
---|
531 | |
---|
532 | if ( c.pid != -1 ) { |
---|
533 | if ( self_id != -1 && roar_nnode_cmp(&(self_client->nnode), &(c.nnode)) == 0 ) { |
---|
534 | printf("Client PID : %i(%s)\n", c.pid, proc_name(c.pid)); |
---|
535 | } else { |
---|
536 | printf("Client PID : %i\n", c.pid); |
---|
537 | } |
---|
538 | } |
---|
539 | if ( c.uid != -1 ) { |
---|
540 | #ifdef _POSIX_USERS |
---|
541 | if ( self_id != -1 && roar_nnode_cmp(&(self_client->nnode), &(c.nnode)) == 0 ) { |
---|
542 | pwd = getpwuid(c.uid); |
---|
543 | grp = getgrgid(c.gid); |
---|
544 | printf("Client UID/GID : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?"); |
---|
545 | } else { |
---|
546 | #else |
---|
547 | if ( 1 ) { |
---|
548 | #endif |
---|
549 | printf("Client UID/GID : %i/%i\n", c.uid, c.gid); |
---|
550 | } |
---|
551 | } |
---|
552 | |
---|
553 | if ( g_verbose && c.proto != ROAR_PROTO_NONE ) { |
---|
554 | printf("Client Protocol : %s\n", roar_proto2str(c.proto)); |
---|
555 | } |
---|
556 | |
---|
557 | if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) { |
---|
558 | if ( c.byteorder == ROAR_BYTEORDER_NETWORK ) { |
---|
559 | roar_mm_strscpy(tmp, " (network byteorder"); |
---|
560 | } else { |
---|
561 | *tmp = 0; |
---|
562 | } |
---|
563 | |
---|
564 | if ( c.byteorder == ROAR_BYTEORDER_NATIVE ) { |
---|
565 | if ( *tmp ) { |
---|
566 | roar_mm_strscat(tmp, ", native"); |
---|
567 | } else { |
---|
568 | roar_mm_strscpy(tmp, " (native"); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | if ( *tmp ) |
---|
573 | roar_mm_strscat(tmp, ")"); |
---|
574 | |
---|
575 | printf("Client Byteorder : %s%s\n", roar_byteorder2str(c.byteorder), tmp); |
---|
576 | } |
---|
577 | |
---|
578 | if ( c.execed != -1 ) |
---|
579 | printf("Execed stream : %i\n", c.execed); |
---|
580 | |
---|
581 | for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++) |
---|
582 | if ( c.streams[h] != -1 ) |
---|
583 | printf("stream : %i\n", c.streams[h]); |
---|
584 | } |
---|
585 | |
---|
586 | void list_streams (struct roar_connection * con) { |
---|
587 | int i; |
---|
588 | int num; |
---|
589 | int id[ROAR_STREAMS_MAX]; |
---|
590 | |
---|
591 | if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
592 | fprintf(stderr, "Error: can not get stream list\n"); |
---|
593 | return; |
---|
594 | } |
---|
595 | |
---|
596 | for (i = 0; i < num; i++) { |
---|
597 | display_stream(con, id[i]); |
---|
598 | } |
---|
599 | } |
---|
600 | |
---|
601 | void display_stream (struct roar_connection * con, int id) { |
---|
602 | char chanmap[ROAR_MAX_CHANNELS]; |
---|
603 | struct roar_stream s; |
---|
604 | struct roar_stream_info info; |
---|
605 | char buffer[1024]; |
---|
606 | char * flags = buffer; |
---|
607 | char * name = buffer; |
---|
608 | char * infotext; |
---|
609 | const char * mime; |
---|
610 | size_t len; |
---|
611 | |
---|
612 | printf("stream %i:\n", id); |
---|
613 | if ( roar_get_stream(con, &s, id) == -1 ) { |
---|
614 | fprintf(stderr, "Error: can not get stream info\n"); |
---|
615 | return; |
---|
616 | } |
---|
617 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
618 | |
---|
619 | if ( roar_stream_get_name(con, &s, name, sizeof(buffer)) == 0 ) |
---|
620 | printf("Stream name : %s\n", name); |
---|
621 | |
---|
622 | if ( (int)s.pos_rel_id == -1 ) { |
---|
623 | printf("Relativ position id : none (stream not synchronized)\n"); |
---|
624 | } else if ( (int)s.pos_rel_id == id ) { |
---|
625 | printf("Relativ position id : %i (self synchronized)\n", s.pos_rel_id); |
---|
626 | } else { |
---|
627 | printf("Relativ position id : %i (synchronized)\n", s.pos_rel_id); |
---|
628 | } |
---|
629 | if ( g_verbose > 1 && s.pos != (uint32_t)-1 ) { |
---|
630 | if ( s.info.rate && s.info.channels ) { |
---|
631 | printf("Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, |
---|
632 | (float)s.pos/(s.info.rate*s.info.channels)); |
---|
633 | } else { |
---|
634 | printf("Position : %lu S\n", (unsigned long int) s.pos); |
---|
635 | } |
---|
636 | } |
---|
637 | |
---|
638 | switch (s.dir) { |
---|
639 | case ROAR_DIR_MIDI_IN: |
---|
640 | case ROAR_DIR_MIDI_OUT: |
---|
641 | infotext = " ticks/s"; |
---|
642 | break; |
---|
643 | case ROAR_DIR_LIGHT_IN: |
---|
644 | case ROAR_DIR_LIGHT_OUT: |
---|
645 | infotext = " updates/s"; |
---|
646 | break; |
---|
647 | default: |
---|
648 | infotext = " Hz"; |
---|
649 | } |
---|
650 | if ( s.info.rate ) |
---|
651 | printf("Stream sample rate : %i%s\n", s.info.rate, infotext); |
---|
652 | if ( s.info.bits ) |
---|
653 | printf("Stream bits : %i\n", s.info.bits); |
---|
654 | if ( s.info.channels ) |
---|
655 | printf("Stream channels : %i\n", s.info.channels); |
---|
656 | |
---|
657 | mime = roar_codec2mime(s.info.codec); |
---|
658 | printf("Stream codec : %2i (%s%s%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
659 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "", |
---|
660 | mime == NULL ? "" : " mimetype:", |
---|
661 | mime == NULL ? "" : mime |
---|
662 | ); |
---|
663 | if ( roar_stream_get_info(con, &s, &info) != -1 ) { |
---|
664 | if ( info.codec != s.info.codec ) { |
---|
665 | mime = roar_codec2mime(info.codec); |
---|
666 | printf("Streamed codec : %2i (%s%s%s%s)\n", info.codec, roar_codec2str(info.codec), |
---|
667 | info.codec == ROAR_CODEC_DEFAULT ? " native" : "", |
---|
668 | mime == NULL ? "" : " mimetype:", |
---|
669 | mime == NULL ? "" : mime |
---|
670 | ); |
---|
671 | } |
---|
672 | |
---|
673 | if ( g_verbose ) { |
---|
674 | if ( info.block_size ) |
---|
675 | printf("Stream block size : %i Byte\n", info.block_size); |
---|
676 | |
---|
677 | printf("Underruns pre/post : %i/%i\n", info.pre_underruns, info.post_underruns); |
---|
678 | if ( g_verbose > 1 ) |
---|
679 | printf("Stream delay : %ims (%.2fm)\n", (int)info.delay/1000, (info.delay*(float)_SPEED_OF_SOUND)); |
---|
680 | |
---|
681 | if ( g_verbose > 1 ) |
---|
682 | printf("Stream mixer : %i\n", info.mixer); |
---|
683 | |
---|
684 | if ( g_verbose > 1 ) |
---|
685 | printf("Stream state : %s\n", roar_streamstate2str(info.state)); |
---|
686 | |
---|
687 | if ( g_verbose > 1 ) |
---|
688 | printf("Stream role : %s\n", roar_role2str(info.role)); |
---|
689 | |
---|
690 | *flags = 0; |
---|
691 | #define _strcat(dst,src) roar_mm_strlcat((dst),(src), sizeof(buffer)) |
---|
692 | if ( info.flags & ROAR_FLAG_PRIMARY ) |
---|
693 | _strcat(flags, "primary "); |
---|
694 | if ( info.flags & ROAR_FLAG_SYNC ) |
---|
695 | _strcat(flags, "sync "); |
---|
696 | if ( info.flags & ROAR_FLAG_OUTPUT ) |
---|
697 | _strcat(flags, "output "); |
---|
698 | if ( info.flags & ROAR_FLAG_SOURCE ) |
---|
699 | _strcat(flags, "source "); |
---|
700 | if ( info.flags & ROAR_FLAG_META ) |
---|
701 | _strcat(flags, "meta "); |
---|
702 | if ( info.flags & ROAR_FLAG_AUTOCONF ) |
---|
703 | _strcat(flags, "autoconf "); |
---|
704 | if ( info.flags & ROAR_FLAG_CLEANMETA ) |
---|
705 | _strcat(flags, "cleanmeta "); |
---|
706 | if ( info.flags & ROAR_FLAG_HWMIXER ) |
---|
707 | _strcat(flags, "hwmixer "); |
---|
708 | if ( info.flags & ROAR_FLAG_PAUSE ) |
---|
709 | _strcat(flags, "pause "); |
---|
710 | if ( info.flags & ROAR_FLAG_MUTE ) |
---|
711 | _strcat(flags, "mute "); |
---|
712 | if ( info.flags & ROAR_FLAG_MMAP ) |
---|
713 | _strcat(flags, "mmap "); |
---|
714 | if ( info.flags & ROAR_FLAG_ANTIECHO ) |
---|
715 | _strcat(flags, "antiecho "); |
---|
716 | if ( info.flags & ROAR_FLAG_VIRTUAL ) |
---|
717 | _strcat(flags, "virtual "); |
---|
718 | if ( info.flags & ROAR_FLAG_RECSOURCE ) |
---|
719 | _strcat(flags, "recsource "); |
---|
720 | if ( info.flags & ROAR_FLAG_PASSMIXER ) |
---|
721 | _strcat(flags, "passmixer "); |
---|
722 | if ( info.flags & ROAR_FLAG_PRETHRU ) |
---|
723 | _strcat(flags, "prethru "); |
---|
724 | if ( info.flags & ROAR_FLAG_IMMUTABLE ) |
---|
725 | _strcat(flags, "immutable "); |
---|
726 | if ( info.flags & ROAR_FLAG_ENHANCE ) |
---|
727 | _strcat(flags, "enhance "); |
---|
728 | if ( info.flags & ROAR_FLAG_SINGLESINK ) |
---|
729 | _strcat(flags, "singlesink "); |
---|
730 | #undef _strcat |
---|
731 | |
---|
732 | printf("Flags : %s\n", flags); |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | if ( g_verbose ) { |
---|
737 | len = ROAR_MAX_CHANNELS; |
---|
738 | if ( roar_stream_get_chanmap(con, &s, chanmap, &len) == -1 ) { |
---|
739 | fprintf(stderr, "Error: can not get stream channel map\n"); |
---|
740 | } else { |
---|
741 | if ( roardsp_chanlist2str(chanmap, len, buffer, 1024) == -1 ) { |
---|
742 | fprintf(stderr, "Error: can not convert channel map into string\n"); |
---|
743 | } else { |
---|
744 | printf("Channel Map : %s\n", buffer); |
---|
745 | } |
---|
746 | } |
---|
747 | } |
---|
748 | |
---|
749 | if ( s.dir != ROAR_DIR_THRU ) { |
---|
750 | display_mixer(con, id); |
---|
751 | show_meta_all(con, id); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | int display_mixer (struct roar_connection * con, int stream) { |
---|
756 | struct roar_mixer_settings mixer; |
---|
757 | int channels; |
---|
758 | int i; |
---|
759 | float fs; |
---|
760 | |
---|
761 | if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) { |
---|
762 | fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream); |
---|
763 | return -1; |
---|
764 | } |
---|
765 | |
---|
766 | #ifdef ROAR_HAVE_LIBM |
---|
767 | #define _DB ", %.2fdB" |
---|
768 | #else |
---|
769 | #define _DB "" |
---|
770 | #endif |
---|
771 | |
---|
772 | fs = (float)mixer.scale / 100.; |
---|
773 | |
---|
774 | if ( channels ) { // we hide RPG info for zero-channel streams |
---|
775 | printf("Mixer ReplayGain : %i/%i (%.2f%%" _DB ")\n", mixer.rpg_mul, mixer.rpg_div, |
---|
776 | 100.*(float)mixer.rpg_mul/((float)mixer.rpg_div) |
---|
777 | #ifdef ROAR_HAVE_LIBM |
---|
778 | , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div) |
---|
779 | #endif |
---|
780 | ); |
---|
781 | } |
---|
782 | |
---|
783 | for (i = 0; i < channels; i++) |
---|
784 | printf("Mixer volume chan %2i : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale, |
---|
785 | (float)mixer.mixer[i]/fs |
---|
786 | #ifdef ROAR_HAVE_LIBM |
---|
787 | , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale) |
---|
788 | #endif |
---|
789 | ); |
---|
790 | |
---|
791 | return 0; |
---|
792 | } |
---|
793 | |
---|
794 | static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) { |
---|
795 | float fs = scale; |
---|
796 | |
---|
797 | switch (k[len - 1]) { |
---|
798 | case '%': |
---|
799 | k[len - 1] = 0; |
---|
800 | return (atof(k)*fs)/100.; |
---|
801 | break; |
---|
802 | #ifdef ROAR_HAVE_LIBM |
---|
803 | case 'b': |
---|
804 | case 'B': |
---|
805 | // TODO: can we hanle the error better? |
---|
806 | if ( len < 2 ) |
---|
807 | return 0; |
---|
808 | |
---|
809 | k[len - 2] = 0; |
---|
810 | return powf(10, atof(k)/20.f)*fs; |
---|
811 | break; |
---|
812 | #endif |
---|
813 | } |
---|
814 | |
---|
815 | return atoi(k); |
---|
816 | } |
---|
817 | |
---|
818 | int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) { |
---|
819 | uint16_t scale = 65535; |
---|
820 | int chans = 0; |
---|
821 | int id; |
---|
822 | int i; |
---|
823 | int len; |
---|
824 | int old_chans; |
---|
825 | char * k; |
---|
826 | struct roar_mixer_settings mixer; |
---|
827 | struct roar_mixer_settings old_mixer; |
---|
828 | int mode = ROAR_SET_VOL_ALL; |
---|
829 | |
---|
830 | if (*cur + 2 > max) |
---|
831 | return -1; |
---|
832 | |
---|
833 | id = atoi(arg[++(*cur)]); |
---|
834 | |
---|
835 | k = arg[++(*cur)]; |
---|
836 | |
---|
837 | if ( !strcmp(arg[*cur + 1], "scale") ) { |
---|
838 | (*cur)++; // 'scale' |
---|
839 | (*cur)++; |
---|
840 | scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535); |
---|
841 | } |
---|
842 | |
---|
843 | mixer.scale = scale; |
---|
844 | |
---|
845 | if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) { |
---|
846 | fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id); |
---|
847 | return -1; |
---|
848 | } |
---|
849 | |
---|
850 | if ( strcmp(k, "mono") == 0 ) { |
---|
851 | chans = 1; |
---|
852 | mode = ROAR_SET_VOL_UNMAPPED; |
---|
853 | } else if ( strcmp(k, "stereo") == 0 ) { |
---|
854 | chans = 2; |
---|
855 | mode = ROAR_SET_VOL_UNMAPPED; |
---|
856 | } else { |
---|
857 | chans = atoi(k); |
---|
858 | } |
---|
859 | |
---|
860 | // ensure most simple mode: |
---|
861 | if ( old_chans == chans && mode == ROAR_SET_VOL_UNMAPPED ) |
---|
862 | mode = ROAR_SET_VOL_ALL; |
---|
863 | |
---|
864 | if ( *cur + chans > max ) |
---|
865 | return -1; |
---|
866 | |
---|
867 | for (i = 0; i < chans; i++) { |
---|
868 | k = arg[++(*cur)]; |
---|
869 | len = strlen(k); |
---|
870 | |
---|
871 | mixer.mixer[i] = set_mixer_parse_volume(k, len, scale); |
---|
872 | } |
---|
873 | |
---|
874 | if ( roar_set_vol(con, id, &mixer, chans, mode) == 0 ) |
---|
875 | return 0; |
---|
876 | |
---|
877 | // no fallback if already using ROAR_SET_VOL_ALL: |
---|
878 | if ( mode == ROAR_SET_VOL_ALL ) |
---|
879 | return -1; |
---|
880 | |
---|
881 | if ( roar_conv_volume(&mixer, &mixer, old_chans, chans) == -1 ) |
---|
882 | return -1; |
---|
883 | |
---|
884 | chans = old_chans; |
---|
885 | mode = ROAR_SET_VOL_ALL; |
---|
886 | |
---|
887 | if ( roar_set_vol(con, id, &mixer, chans, mode) == 0 ) |
---|
888 | return 0; |
---|
889 | |
---|
890 | return -1; |
---|
891 | } |
---|
892 | |
---|
893 | |
---|
894 | int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) { |
---|
895 | struct roar_stream s; |
---|
896 | int dir = roar_str2dir(d_s); |
---|
897 | int parent = atoi(p_s); |
---|
898 | |
---|
899 | ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent); |
---|
900 | |
---|
901 | if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 ) |
---|
902 | return -1; |
---|
903 | |
---|
904 | return roar_simple_connect_virtual(con, &s, parent, dir); |
---|
905 | } |
---|
906 | |
---|
907 | int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) { |
---|
908 | struct roar_meta meta; |
---|
909 | struct roar_stream s; |
---|
910 | int mode_i = ROAR_META_MODE_SET; |
---|
911 | |
---|
912 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
913 | return -1; |
---|
914 | |
---|
915 | // printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val); |
---|
916 | |
---|
917 | if ( strcmp(mode, "add") == 0 ) { |
---|
918 | mode_i = ROAR_META_MODE_ADD; |
---|
919 | } |
---|
920 | |
---|
921 | meta.type = roar_meta_inttype(type); |
---|
922 | meta.value = val; |
---|
923 | meta.key[0] = 0; |
---|
924 | |
---|
925 | if ( meta.type == -1 ) { |
---|
926 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
927 | return -1; |
---|
928 | } |
---|
929 | |
---|
930 | // printf("D: type=%i, mode=%i\n", meta.type, mode_i); |
---|
931 | |
---|
932 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
933 | return -1; |
---|
934 | |
---|
935 | meta.type = ROAR_META_TYPE_NONE; |
---|
936 | meta.value = NULL; |
---|
937 | |
---|
938 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
939 | } |
---|
940 | |
---|
941 | int load_meta (struct roar_connection * con, int id, char * file) { |
---|
942 | struct roar_meta meta; |
---|
943 | struct roar_stream s; |
---|
944 | int mode_i = ROAR_META_MODE_SET; |
---|
945 | FILE * in; |
---|
946 | char lion[1024]; |
---|
947 | char * v; |
---|
948 | |
---|
949 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
950 | return -1; |
---|
951 | |
---|
952 | if ( (in = fopen(file, "r")) == NULL ) |
---|
953 | return -1; |
---|
954 | |
---|
955 | while (fgets(lion, 1024, in) != NULL) { |
---|
956 | if ( (v = strtok(lion, "\r\n")) != NULL ) |
---|
957 | if ( (v = strtok(NULL, "\r\n")) != NULL ) |
---|
958 | *(v-1) = 0; |
---|
959 | |
---|
960 | if ( (v = strstr(lion, "=")) == NULL ) { |
---|
961 | fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion); |
---|
962 | continue; |
---|
963 | } |
---|
964 | |
---|
965 | *v++ = 0; |
---|
966 | |
---|
967 | meta.type = roar_meta_inttype(lion); |
---|
968 | meta.value = v; |
---|
969 | meta.key[0] = 0; |
---|
970 | |
---|
971 | if ( meta.type == -1 ) { |
---|
972 | fprintf(stderr, "Error: unknown type: %s\n", lion); |
---|
973 | continue; |
---|
974 | } |
---|
975 | |
---|
976 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) { |
---|
977 | fclose(in); |
---|
978 | return -1; |
---|
979 | } |
---|
980 | } |
---|
981 | |
---|
982 | fclose(in); |
---|
983 | |
---|
984 | meta.type = ROAR_META_TYPE_NONE; |
---|
985 | meta.value = NULL; |
---|
986 | |
---|
987 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
988 | } |
---|
989 | |
---|
990 | int show_meta_type (struct roar_connection * con, int id, char * type) { |
---|
991 | struct roar_meta meta; |
---|
992 | struct roar_stream s; |
---|
993 | |
---|
994 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
995 | return -1; |
---|
996 | |
---|
997 | meta.type = roar_meta_inttype(type); |
---|
998 | |
---|
999 | if ( meta.type == -1 ) { |
---|
1000 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
1001 | return -1; |
---|
1002 | } |
---|
1003 | |
---|
1004 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
1005 | return -1; |
---|
1006 | |
---|
1007 | printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
1008 | |
---|
1009 | roar_meta_free(&meta); |
---|
1010 | |
---|
1011 | return 0; |
---|
1012 | } |
---|
1013 | |
---|
1014 | int show_meta_all (struct roar_connection * con, int id) { |
---|
1015 | struct roar_stream s; |
---|
1016 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
1017 | int i; |
---|
1018 | int len; |
---|
1019 | |
---|
1020 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
1021 | return -1; |
---|
1022 | |
---|
1023 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
1024 | return -1; |
---|
1025 | |
---|
1026 | for (i = 0; i < len; i++) |
---|
1027 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
1028 | |
---|
1029 | return 0; |
---|
1030 | } |
---|
1031 | |
---|
1032 | int save_meta (struct roar_connection * con, int id, char * file) { |
---|
1033 | struct roar_stream s; |
---|
1034 | struct roar_meta meta; |
---|
1035 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
1036 | int i; |
---|
1037 | int len; |
---|
1038 | FILE * out; |
---|
1039 | |
---|
1040 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
1041 | return -1; |
---|
1042 | |
---|
1043 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
1044 | return -1; |
---|
1045 | |
---|
1046 | if ( (out = fopen(file, "w")) == NULL ) |
---|
1047 | return -1; |
---|
1048 | |
---|
1049 | for (i = 0; i < len; i++) { |
---|
1050 | /* |
---|
1051 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
1052 | */ |
---|
1053 | meta.type = types[i]; |
---|
1054 | |
---|
1055 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
1056 | continue; |
---|
1057 | |
---|
1058 | // printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
1059 | |
---|
1060 | fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value); |
---|
1061 | |
---|
1062 | roar_meta_free(&meta); |
---|
1063 | } |
---|
1064 | |
---|
1065 | fclose(out); |
---|
1066 | |
---|
1067 | return 0; |
---|
1068 | } |
---|
1069 | |
---|
1070 | int set_flags (struct roar_connection * con, int id, int action, char * flags) { |
---|
1071 | uint32_t f = ROAR_FLAG_NONE; |
---|
1072 | char * c; |
---|
1073 | struct roar_stream s[1]; |
---|
1074 | |
---|
1075 | if ( roar_stream_new_by_id(s, id) == -1 ) |
---|
1076 | return -1; |
---|
1077 | |
---|
1078 | c = strtok(flags, ","); |
---|
1079 | while (c != NULL) { |
---|
1080 | if ( !strcmp(c, "meta") ) { |
---|
1081 | f |= ROAR_FLAG_META; |
---|
1082 | } else if ( !strcmp(c, "primary") ) { |
---|
1083 | f |= ROAR_FLAG_PRIMARY; |
---|
1084 | } else if ( !strcmp(c, "sync") ) { |
---|
1085 | f |= ROAR_FLAG_SYNC; |
---|
1086 | } else if ( !strcmp(c, "cleanmeta") ) { |
---|
1087 | f |= ROAR_FLAG_CLEANMETA; |
---|
1088 | } else if ( !strcmp(c, "hwmixer") ) { |
---|
1089 | f |= ROAR_FLAG_HWMIXER; |
---|
1090 | } else if ( !strcmp(c, "pause") ) { |
---|
1091 | f |= ROAR_FLAG_PAUSE; |
---|
1092 | } else if ( !strcmp(c, "mute") ) { |
---|
1093 | f |= ROAR_FLAG_MUTE; |
---|
1094 | } else if ( !strcmp(c, "mmap") ) { |
---|
1095 | f |= ROAR_FLAG_MMAP; |
---|
1096 | } else if ( !strcmp(c, "antiecho") ) { |
---|
1097 | f |= ROAR_FLAG_ANTIECHO; |
---|
1098 | } else if ( !strcmp(c, "recsource") ) { |
---|
1099 | f |= ROAR_FLAG_RECSOURCE; |
---|
1100 | } else if ( !strcmp(c, "passmixer") ) { |
---|
1101 | f |= ROAR_FLAG_PASSMIXER; |
---|
1102 | } else if ( !strcmp(c, "virtual") ) { |
---|
1103 | f |= ROAR_FLAG_VIRTUAL; |
---|
1104 | } else if ( !strcmp(c, "prethru") ) { |
---|
1105 | f |= ROAR_FLAG_PRETHRU; |
---|
1106 | } else if ( !strcmp(c, "immutable") ) { |
---|
1107 | f |= ROAR_FLAG_IMMUTABLE; |
---|
1108 | } else if ( !strcmp(c, "enhance") ) { |
---|
1109 | f |= ROAR_FLAG_ENHANCE; |
---|
1110 | } else if ( !strcmp(c, "singlesink") ) { |
---|
1111 | f |= ROAR_FLAG_SINGLESINK; |
---|
1112 | } else { |
---|
1113 | fprintf(stderr, "Error: unknown flag: %s\n", c); |
---|
1114 | return -1; |
---|
1115 | } |
---|
1116 | |
---|
1117 | c = strtok(NULL, ","); |
---|
1118 | } |
---|
1119 | |
---|
1120 | return roar_stream_set_flags(con, s, f, action); |
---|
1121 | } |
---|
1122 | |
---|
1123 | int set_role (struct roar_connection * con, int id, int role) { |
---|
1124 | struct roar_stream s; |
---|
1125 | |
---|
1126 | if ( id == -1 ) |
---|
1127 | return -1; |
---|
1128 | |
---|
1129 | if ( roar_stream_new_by_id(&s, id) == -1 ) |
---|
1130 | return -1; |
---|
1131 | |
---|
1132 | if ( roar_stream_set_role(con, &s, role) == -1 ) |
---|
1133 | return -1; |
---|
1134 | |
---|
1135 | return 0; |
---|
1136 | } |
---|
1137 | |
---|
1138 | int show_aiprofile (const char * profile) { |
---|
1139 | struct roar_audio_info info; |
---|
1140 | const char * mime; |
---|
1141 | |
---|
1142 | if ( roar_profile2info(&info, profile) == -1 ) { |
---|
1143 | fprintf(stderr, "Error: unknown profile: %s\n", profile); |
---|
1144 | return -1; |
---|
1145 | } |
---|
1146 | |
---|
1147 | mime = roar_codec2mime(info.codec); |
---|
1148 | |
---|
1149 | printf("Profile Name : %s\n", profile); |
---|
1150 | |
---|
1151 | if ( info.rate ) |
---|
1152 | printf("Profile sample rate : %i\n", info.rate); |
---|
1153 | if ( info.bits ) |
---|
1154 | printf("Profile bits : %i\n", info.bits); |
---|
1155 | if ( info.channels ) |
---|
1156 | printf("Profile channels : %i\n", info.channels); |
---|
1157 | |
---|
1158 | printf("Profile codec : %2i (%s%s%s%s)\n", info.codec, roar_codec2str(info.codec), |
---|
1159 | info.codec == ROAR_CODEC_DEFAULT ? " native" : "", |
---|
1160 | mime == NULL ? "" : " mimetype:", |
---|
1161 | mime == NULL ? "" : mime); |
---|
1162 | |
---|
1163 | return 0; |
---|
1164 | } |
---|
1165 | |
---|
1166 | int list_aiprofiles (void) { |
---|
1167 | const char * list[1024]; |
---|
1168 | ssize_t ret; |
---|
1169 | ssize_t i; |
---|
1170 | |
---|
1171 | ret = roar_profiles_list(list, 1024, 0); |
---|
1172 | |
---|
1173 | if ( ret == -1 ) { |
---|
1174 | fprintf(stderr, "Error: can not read list of profiles\n"); |
---|
1175 | return -1; |
---|
1176 | } |
---|
1177 | |
---|
1178 | for (i = 0; i < ret; i++) { |
---|
1179 | printf("profile %lli:\n", (long long signed int)i); |
---|
1180 | show_aiprofile(list[i]); |
---|
1181 | } |
---|
1182 | |
---|
1183 | return 0; |
---|
1184 | } |
---|
1185 | |
---|
1186 | int main (int argc, char * argv[]) { |
---|
1187 | struct roar_connection con; |
---|
1188 | char * server = NULL; |
---|
1189 | char * k = NULL; |
---|
1190 | int i; |
---|
1191 | int t = 0; |
---|
1192 | |
---|
1193 | for (i = 1; i < argc; i++) { |
---|
1194 | k = argv[i]; |
---|
1195 | |
---|
1196 | if ( strcmp(k, "--server") == 0 ) { |
---|
1197 | server = argv[++i]; |
---|
1198 | } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) { |
---|
1199 | g_verbose++; |
---|
1200 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
1201 | usage(); |
---|
1202 | return 0; |
---|
1203 | } else if ( strcmp(k, "--list-aiprofiles") == 0 ) { |
---|
1204 | list_aiprofiles(); |
---|
1205 | return 0; |
---|
1206 | } else if ( strcmp(k, "--list-libstandards") == 0 ) { |
---|
1207 | lib_standards(); |
---|
1208 | return 0; |
---|
1209 | } else if ( strcmp(k, "--enum-servers") == 0 ) { |
---|
1210 | enum_servers(); |
---|
1211 | return 0; |
---|
1212 | } else if ( strcmp(k, "--hash-password") == 0 ) { |
---|
1213 | hash_password(); |
---|
1214 | return 0; |
---|
1215 | } else if ( *k == '-' ) { |
---|
1216 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
1217 | usage(); |
---|
1218 | return 1; |
---|
1219 | } else { |
---|
1220 | break; |
---|
1221 | } |
---|
1222 | } |
---|
1223 | |
---|
1224 | // connect |
---|
1225 | |
---|
1226 | if ( roar_simple_connect(&con, server, "roarctl") == -1 ) { |
---|
1227 | fprintf(stderr, "Error: Can not connect to server: %s: %s(%i)\n", |
---|
1228 | server == NULL ? "(default)" : server, roar_vs_strerr(roar_error), roar_error); |
---|
1229 | return 1; |
---|
1230 | } |
---|
1231 | |
---|
1232 | if ( i == argc ) { |
---|
1233 | fprintf(stderr, "Error: No Commands given\n"); |
---|
1234 | return 0; // this is not a fatal error... |
---|
1235 | } |
---|
1236 | |
---|
1237 | for (; i < argc; i++) { |
---|
1238 | k = argv[i]; |
---|
1239 | // cmd is in k |
---|
1240 | |
---|
1241 | printf("--- [ %s ] ---\n", k); |
---|
1242 | |
---|
1243 | if ( !strcmp(k, "help") ) { |
---|
1244 | usage(); |
---|
1245 | |
---|
1246 | } else if ( !strcmp(k, "sleep") ) { |
---|
1247 | roar_sleep(atoi(argv[++i])); |
---|
1248 | |
---|
1249 | } else if ( !strcmp(k, "ping") ) { |
---|
1250 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
1251 | if ( ping(&con, atoi(argv[++i])) == -1 ) { |
---|
1252 | fprintf(stderr, "Error: can not ping\n"); |
---|
1253 | } |
---|
1254 | #else |
---|
1255 | fprintf(stderr, "Error: ping not supported.\n"); |
---|
1256 | i++; |
---|
1257 | #endif |
---|
1258 | |
---|
1259 | } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) { |
---|
1260 | if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) { |
---|
1261 | fprintf(stderr, "Error: can not set mode to standby\n"); |
---|
1262 | } else { |
---|
1263 | printf("going into standby\n"); |
---|
1264 | } |
---|
1265 | } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) { |
---|
1266 | if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) { |
---|
1267 | fprintf(stderr, "Error: can not set mode to active\n"); |
---|
1268 | } else { |
---|
1269 | printf("going into active mode\n"); |
---|
1270 | } |
---|
1271 | |
---|
1272 | } else if ( !strcmp(k, "exit") ) { |
---|
1273 | if ( roar_terminate(&con, 0) == -1 ) { |
---|
1274 | fprintf(stderr, "Error: can not quit server\n"); |
---|
1275 | } else { |
---|
1276 | printf("Server quited\n"); |
---|
1277 | break; |
---|
1278 | } |
---|
1279 | } else if ( !strcmp(k, "terminate") ) { |
---|
1280 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
1281 | fprintf(stderr, "Error: can not terminate server\n"); |
---|
1282 | } else { |
---|
1283 | printf("Server got asked to quited\n"); |
---|
1284 | break; |
---|
1285 | } |
---|
1286 | |
---|
1287 | } else if ( !strcmp(k, "standbymode") ) { |
---|
1288 | t = roar_get_standby(&con); |
---|
1289 | if ( t == -1 ) { |
---|
1290 | fprintf(stderr, "Error: can not get stanby mode\n"); |
---|
1291 | } else if ( t == ROAR_STANDBY_ACTIVE ) { |
---|
1292 | printf("Server is in standby\n"); |
---|
1293 | } else if ( t == ROAR_STANDBY_INACTIVE ) { |
---|
1294 | printf("Server is active\n"); |
---|
1295 | } else { |
---|
1296 | fprintf(stderr, "Error: unknown standby mode: %i\n", t); |
---|
1297 | } |
---|
1298 | |
---|
1299 | } else if ( !strcmp(k, "whoami") ) { |
---|
1300 | printf("My client ID is: %i\n", roar_get_clientid(&con)); |
---|
1301 | } else if ( !strcmp(k, "beep") ) { |
---|
1302 | roar_beep(&con, NULL); |
---|
1303 | } else if ( !strcmp(k, "serverinfo") ) { |
---|
1304 | server_info(&con); |
---|
1305 | } else if ( !strcmp(k, "servertime") ) { |
---|
1306 | server_time(&con); |
---|
1307 | } else if ( !strcmp(k, "serveroinfo") ) { |
---|
1308 | server_oinfo(&con, -1); |
---|
1309 | } else if ( !strcmp(k, "serveroinfo2") ) { |
---|
1310 | t = roar_str2dir(argv[++i]); |
---|
1311 | if ( t == -1 ) { |
---|
1312 | fprintf(stderr, "Error: unknown stream direction: %s\n", argv[i]); |
---|
1313 | } else { |
---|
1314 | server_oinfo(&con, t); |
---|
1315 | } |
---|
1316 | } else if ( !strcmp(k, "serverstandards") ) { |
---|
1317 | server_standards(&con); |
---|
1318 | } else if ( !strcmp(k, "libstandards") ) { |
---|
1319 | lib_standards(); |
---|
1320 | } else if ( !strcmp(k, "listclients") ) { |
---|
1321 | list_clients(&con); |
---|
1322 | } else if ( !strcmp(k, "clientinfo") ) { |
---|
1323 | display_client(&con, atoi(argv[++i])); |
---|
1324 | } else if ( !strcmp(k, "liststreams") ) { |
---|
1325 | list_streams(&con); |
---|
1326 | } else if ( !strcmp(k, "streaminfo") ) { |
---|
1327 | display_stream(&con, atoi(argv[++i])); |
---|
1328 | } else if ( !strcmp(k, "allinfo") ) { |
---|
1329 | server_info(&con); |
---|
1330 | printf("\n"); |
---|
1331 | server_time(&con); |
---|
1332 | printf("\n"); |
---|
1333 | server_standards(&con); |
---|
1334 | printf("\n"); |
---|
1335 | server_oinfo(&con, -1); |
---|
1336 | printf("\n"); |
---|
1337 | list_clients(&con); |
---|
1338 | printf("\n"); |
---|
1339 | list_streams(&con); |
---|
1340 | |
---|
1341 | } else if ( !strcmp(k, "kick") ) { |
---|
1342 | t = roar_str2ot((k = argv[++i])); |
---|
1343 | if ( t == -1 ) { |
---|
1344 | fprintf(stderr, "Error: unknown type: %s\n", k); |
---|
1345 | continue; |
---|
1346 | } |
---|
1347 | //t = atoi(argv[i++]); |
---|
1348 | if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) { |
---|
1349 | fprintf(stderr, "Error: can not kick %s\n", k); |
---|
1350 | } else { |
---|
1351 | printf("%s kicked\n", k); |
---|
1352 | } |
---|
1353 | |
---|
1354 | } else if ( !strcmp(k, "newvirtual") ) { |
---|
1355 | if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) { |
---|
1356 | fprintf(stderr, "Error: can not create new virtual stream\n"); |
---|
1357 | } else { |
---|
1358 | printf("virtual stream created\n"); |
---|
1359 | } |
---|
1360 | i += 6; |
---|
1361 | |
---|
1362 | } else if ( !strcmp(k, "volume") ) { |
---|
1363 | if ( set_mixer(&con, &i, argc, argv) == -1 ) { |
---|
1364 | fprintf(stderr, "Error: can not set volume\n"); |
---|
1365 | } else { |
---|
1366 | printf("volume changed\n"); |
---|
1367 | } |
---|
1368 | |
---|
1369 | } else if ( !strcmp(k, "flag") ) { |
---|
1370 | i++; |
---|
1371 | if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) { |
---|
1372 | fprintf(stderr, "Error: can not set flags\n"); |
---|
1373 | } else { |
---|
1374 | printf("flags changed\n"); |
---|
1375 | } |
---|
1376 | i++; |
---|
1377 | } else if ( !strcmp(k, "unflag") ) { |
---|
1378 | i++; |
---|
1379 | if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) { |
---|
1380 | fprintf(stderr, "Error: can not reset flags\n"); |
---|
1381 | } else { |
---|
1382 | printf("flags changed\n"); |
---|
1383 | } |
---|
1384 | i++; |
---|
1385 | } else if ( !strcmp(k, "toggleflag") ) { |
---|
1386 | i++; |
---|
1387 | if ( set_flags(&con, atoi(argv[i]), ROAR_TOGGLE_FLAG, argv[i+1]) == -1 ) { |
---|
1388 | fprintf(stderr, "Error: can not toggle flags\n"); |
---|
1389 | } else { |
---|
1390 | printf("flags changed\n"); |
---|
1391 | } |
---|
1392 | i++; |
---|
1393 | } else if ( !strcmp(k, "protectflag") ) { |
---|
1394 | i++; |
---|
1395 | if ( set_flags(&con, atoi(argv[i]), ROAR_NOOP_FLAG|ROAR_PROTECT_FLAG, argv[i+1]) == -1 ) { |
---|
1396 | fprintf(stderr, "Error: can not protect flags\n"); |
---|
1397 | } else { |
---|
1398 | printf("flags protected\n"); |
---|
1399 | } |
---|
1400 | i++; |
---|
1401 | |
---|
1402 | } else if ( !strcmp(k, "role") ) { |
---|
1403 | i++; |
---|
1404 | if ( set_role(&con, atoi(argv[i]), roar_str2role(argv[i+1])) == -1 ) { |
---|
1405 | fprintf(stderr, "Error: can not set stream role\n"); |
---|
1406 | } else { |
---|
1407 | printf("stream role set\n"); |
---|
1408 | } |
---|
1409 | i++; |
---|
1410 | |
---|
1411 | } else if ( !strcmp(k, "metaset") ) { |
---|
1412 | i++; |
---|
1413 | if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) { |
---|
1414 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
1415 | } else { |
---|
1416 | printf("meta data changed\n"); |
---|
1417 | } |
---|
1418 | i += 3; |
---|
1419 | } else if ( !strcmp(k, "metaget") ) { |
---|
1420 | i++; |
---|
1421 | if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
1422 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
1423 | } |
---|
1424 | i++; |
---|
1425 | } else if ( !strcmp(k, "metasave") ) { |
---|
1426 | i++; |
---|
1427 | if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
1428 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
1429 | } else { |
---|
1430 | printf("meta data saved\n"); |
---|
1431 | } |
---|
1432 | i++; |
---|
1433 | } else if ( !strcmp(k, "metaload") ) { |
---|
1434 | i++; |
---|
1435 | if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
1436 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
1437 | } else { |
---|
1438 | printf("meta data saved\n"); |
---|
1439 | } |
---|
1440 | i++; |
---|
1441 | |
---|
1442 | |
---|
1443 | } else if ( !strcmp(k, "listaiprofiles") || !strcmp(k, "listprofiles") ) { |
---|
1444 | if ( list_aiprofiles() == -1 ) { |
---|
1445 | fprintf(stderr, "Error: can not list profiles\n"); |
---|
1446 | } |
---|
1447 | } else if ( !strcmp(k, "aiprofileget") || !strcmp(k, "profileget") ) { |
---|
1448 | i++; |
---|
1449 | if ( show_aiprofile(argv[i]) == -1 ) { |
---|
1450 | fprintf(stderr, "Error: can not get profile data\n"); |
---|
1451 | } |
---|
1452 | } else { |
---|
1453 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
1454 | } |
---|
1455 | |
---|
1456 | } |
---|
1457 | |
---|
1458 | roar_disconnect(&con); |
---|
1459 | |
---|
1460 | return 0; |
---|
1461 | } |
---|
1462 | |
---|
1463 | //ll |
---|