[0] | 1 | //roarctl.c: |
---|
| 2 | |
---|
[669] | 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 | |
---|
[0] | 25 | #include <roaraudio.h> |
---|
[1782] | 26 | |
---|
| 27 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) |
---|
| 28 | #define _POSIX_USERS |
---|
| 29 | #endif |
---|
| 30 | |
---|
| 31 | #ifdef _POSIX_USERS |
---|
[445] | 32 | #include <pwd.h> |
---|
| 33 | #include <grp.h> |
---|
[1782] | 34 | #endif |
---|
| 35 | |
---|
[1630] | 36 | #include <sys/time.h> |
---|
| 37 | #include <time.h> |
---|
[445] | 38 | |
---|
[963] | 39 | int g_verbose = 0; |
---|
[0] | 40 | |
---|
[25] | 41 | int display_mixer (struct roar_connection * con, int stream); |
---|
[112] | 42 | int show_meta_all (struct roar_connection * con, int id); |
---|
[25] | 43 | |
---|
[0] | 44 | void usage (void) { |
---|
[233] | 45 | printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n"); |
---|
[0] | 46 | |
---|
| 47 | printf("\nOptions:\n\n"); |
---|
| 48 | |
---|
[86] | 49 | printf(" --server SERVER - Set server hostname\n" |
---|
| 50 | " --help - Show this help\n" |
---|
[963] | 51 | " --verbose -v - Show verbose output\n" |
---|
[0] | 52 | ); |
---|
| 53 | |
---|
| 54 | printf("\nCommands:\n\n"); |
---|
| 55 | printf( |
---|
[86] | 56 | " help - Show this help\n" |
---|
[1204] | 57 | " sleep TIME - Sleeps for TIME seconds\n" |
---|
[1781] | 58 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[1630] | 59 | " ping NUM - Do NUM pings using NOOP commands\n" |
---|
[1781] | 60 | #endif |
---|
[0] | 61 | "\n" |
---|
[86] | 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" |
---|
[577] | 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" |
---|
[0] | 69 | "\n" |
---|
[86] | 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" |
---|
[0] | 78 | "\n" |
---|
[1123] | 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" |
---|
[86] | 83 | " kick TYPE ID - Kicks object of TYPE with id ID\n" |
---|
| 84 | " Types: client stream sample source\n" |
---|
| 85 | "\n" |
---|
[1160] | 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" |
---|
[86] | 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" |
---|
[0] | 93 | ); |
---|
| 94 | } |
---|
| 95 | |
---|
[1781] | 96 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[1630] | 97 | int ping (struct roar_connection * con, int num) { |
---|
| 98 | struct timeval try, ans; |
---|
| 99 | struct roar_message m; |
---|
| 100 | register int ret; |
---|
[1631] | 101 | int i; |
---|
| 102 | double cur, min = 3600*1000, max = 0, sum = 0; |
---|
| 103 | |
---|
| 104 | if ( num == 0 ) |
---|
| 105 | return 0; |
---|
[1630] | 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 | |
---|
[1631] | 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; |
---|
[1630] | 131 | |
---|
| 132 | if ( i != (num - 1) ) |
---|
| 133 | sleep(1); |
---|
| 134 | } |
---|
| 135 | |
---|
[1631] | 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 | |
---|
[1630] | 140 | return 0; |
---|
| 141 | } |
---|
[1781] | 142 | #endif |
---|
[1630] | 143 | |
---|
[0] | 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); |
---|
[984] | 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)); |
---|
[0] | 161 | } |
---|
| 162 | |
---|
[449] | 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 | |
---|
[0] | 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; |
---|
[1782] | 194 | #ifdef _POSIX_USERS |
---|
[445] | 195 | struct group * grp = NULL; |
---|
| 196 | struct passwd * pwd = NULL; |
---|
[1782] | 197 | #endif |
---|
[0] | 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); |
---|
[449] | 211 | printf("Player PID : %i(%s)\n", c.pid, proc_name(c.pid)); |
---|
[445] | 212 | if ( c.uid != -1 ) { |
---|
[1782] | 213 | #ifdef _POSIX_USERS |
---|
[445] | 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 : "?"); |
---|
[1782] | 217 | #else |
---|
| 218 | printf("Player UID/GID : %i/%i\n", c.uid, c.gid); |
---|
| 219 | #endif |
---|
[445] | 220 | } |
---|
[0] | 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; |
---|
[465] | 236 | struct roar_stream_info info; |
---|
[1219] | 237 | char flags[1024]; |
---|
[0] | 238 | |
---|
| 239 | |
---|
| 240 | if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) { |
---|
| 241 | fprintf(stderr, "Error: can not get stream list\n"); |
---|
| 242 | return; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | for (i = 0; i < num; i++) { |
---|
| 246 | printf("stream %i:\n", id[i]); |
---|
| 247 | if ( roar_get_stream(con, &s, id[i]) == -1 ) { |
---|
| 248 | fprintf(stderr, "Error: can not get stream info\n"); |
---|
| 249 | continue; |
---|
| 250 | } |
---|
| 251 | printf("Stream direction : %s\n", roar_dir2str(s.dir)); |
---|
[1201] | 252 | if ( s.pos_rel_id == -1 ) { |
---|
[0] | 253 | printf("Relativ position id : none (stream not synchronized)\n"); |
---|
[1201] | 254 | } else if ( s.pos_rel_id == id[i] ) { |
---|
| 255 | printf("Relativ position id : %i (self synchronized)\n", s.pos_rel_id); |
---|
| 256 | } else { |
---|
| 257 | printf("Relativ position id : %i (synchronized)\n", s.pos_rel_id); |
---|
| 258 | } |
---|
[964] | 259 | if ( g_verbose > 1 ) |
---|
| 260 | printf("Position : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels)); |
---|
[1810] | 261 | |
---|
| 262 | if ( s.dir != ROAR_DIR_LIGHT_IN && s.dir != ROAR_DIR_LIGHT_OUT ) { |
---|
| 263 | printf("Input rate : %i\n", s.info.rate); |
---|
| 264 | printf("Input bits : %i\n", s.info.bits); |
---|
| 265 | printf("Input channels : %i\n", s.info.channels); |
---|
| 266 | } |
---|
| 267 | |
---|
[540] | 268 | printf("Input codec : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec), |
---|
| 269 | s.info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
[465] | 270 | if ( roar_stream_get_info(con, &s, &info) != -1 ) { |
---|
[1833] | 271 | if ( info.codec != s.info.codec ) { |
---|
| 272 | printf("Input codec (streamed): %2i (%s%s)\n", info.codec, roar_codec2str(info.codec), |
---|
| 273 | info.codec == ROAR_CODEC_DEFAULT ? " native" : ""); |
---|
| 274 | } |
---|
| 275 | |
---|
[963] | 276 | if ( g_verbose ) { |
---|
| 277 | printf("Input block size : %i Byte\n", info.block_size); |
---|
| 278 | printf("Underruns pre/post : %i/%i\n", info.pre_underruns, info.post_underruns); |
---|
[1137] | 279 | if ( g_verbose > 1 ) |
---|
| 280 | printf("Stream delay : %ims\n", (int)info.delay/1000); |
---|
[963] | 281 | |
---|
| 282 | *flags = 0; |
---|
| 283 | if ( info.flags & ROAR_FLAG_PRIMARY ) |
---|
| 284 | strcat(flags, "primary "); |
---|
| 285 | if ( info.flags & ROAR_FLAG_SYNC ) |
---|
| 286 | strcat(flags, "sync "); |
---|
| 287 | if ( info.flags & ROAR_FLAG_OUTPUT ) |
---|
| 288 | strcat(flags, "output "); |
---|
| 289 | if ( info.flags & ROAR_FLAG_SOURCE ) |
---|
| 290 | strcat(flags, "source "); |
---|
[1032] | 291 | if ( info.flags & ROAR_FLAG_META ) |
---|
| 292 | strcat(flags, "meta "); |
---|
[1219] | 293 | if ( info.flags & ROAR_FLAG_AUTOCONF ) |
---|
| 294 | strcat(flags, "autoconf "); |
---|
| 295 | if ( info.flags & ROAR_FLAG_CLEANMETA ) |
---|
| 296 | strcat(flags, "cleanmeta "); |
---|
[1585] | 297 | if ( info.flags & ROAR_FLAG_HWMIXER ) |
---|
| 298 | strcat(flags, "hwmixer "); |
---|
| 299 | if ( info.flags & ROAR_FLAG_PAUSE ) |
---|
| 300 | strcat(flags, "pause "); |
---|
[963] | 301 | |
---|
| 302 | printf("Flags : %s\n", flags); |
---|
| 303 | } |
---|
[465] | 304 | } |
---|
[1811] | 305 | |
---|
| 306 | if ( s.dir != ROAR_DIR_THRU ) { |
---|
| 307 | display_mixer(con, id[i]); |
---|
| 308 | show_meta_all(con, id[i]); |
---|
| 309 | } |
---|
[0] | 310 | } |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | |
---|
[25] | 314 | int display_mixer (struct roar_connection * con, int stream) { |
---|
| 315 | int channels; |
---|
| 316 | struct roar_mixer_settings mixer; |
---|
| 317 | int i; |
---|
| 318 | |
---|
| 319 | if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) { |
---|
| 320 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
| 321 | return -1; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | for (i = 0; i < channels; i++) |
---|
| 325 | printf("Mixer volume chan %2i : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35); |
---|
| 326 | |
---|
| 327 | return 0; |
---|
| 328 | } |
---|
[0] | 329 | |
---|
[85] | 330 | int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) { |
---|
| 331 | int chans = 0; |
---|
| 332 | int id; |
---|
| 333 | int i; |
---|
| 334 | int len; |
---|
| 335 | int old_chans; |
---|
| 336 | int vol_l, vol_r; |
---|
| 337 | char * k; |
---|
| 338 | struct roar_mixer_settings mixer; |
---|
| 339 | struct roar_mixer_settings old_mixer; |
---|
| 340 | |
---|
| 341 | if (*cur + 2 > max) |
---|
| 342 | return -1; |
---|
| 343 | |
---|
| 344 | id = atoi(arg[++(*cur)]); |
---|
| 345 | |
---|
| 346 | k = arg[++(*cur)]; |
---|
| 347 | |
---|
| 348 | if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) { |
---|
| 349 | fprintf(stderr, "Error: can not get stream mixer info\n"); |
---|
| 350 | return -1; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | |
---|
[1203] | 354 | // TODO: clean up code here as the % vs. abs code is very duplicate... |
---|
| 355 | |
---|
[85] | 356 | if ( strcmp(k, "mono") == 0 && old_chans != 1 ) { |
---|
| 357 | chans = 1; |
---|
| 358 | |
---|
| 359 | if ( *cur + 1 > max ) |
---|
| 360 | return -1; |
---|
| 361 | |
---|
| 362 | k = arg[++(*cur)]; |
---|
| 363 | len = strlen(k); |
---|
| 364 | |
---|
| 365 | if ( k[len - 1] == '%' ) { |
---|
| 366 | k[len - 1] = 0; |
---|
| 367 | vol_l = (atof(k)*65535)/100; |
---|
| 368 | } else { |
---|
| 369 | vol_l = atoi(k); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | for (i = 0; i < old_chans; i++) |
---|
| 373 | mixer.mixer[i] = vol_l; |
---|
| 374 | |
---|
| 375 | chans = old_chans; |
---|
| 376 | |
---|
[1203] | 377 | } else if ( strcmp(k, "stereo") == 0 && old_chans == 4 ) { |
---|
| 378 | chans = 4; |
---|
| 379 | |
---|
| 380 | if ( *cur + 2 > max ) |
---|
| 381 | return -1; |
---|
| 382 | |
---|
| 383 | k = arg[++(*cur)]; |
---|
| 384 | len = strlen(k); |
---|
| 385 | |
---|
| 386 | if ( k[len - 1] == '%' ) { |
---|
| 387 | k[len - 1] = 0; |
---|
| 388 | vol_l = (atof(k)*65535)/100; |
---|
| 389 | } else { |
---|
| 390 | vol_l = atoi(k); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | k = arg[++(*cur)]; |
---|
| 394 | len = strlen(k); |
---|
| 395 | |
---|
| 396 | if ( k[len - 1] == '%' ) { |
---|
| 397 | k[len - 1] = 0; |
---|
| 398 | vol_r = (atof(k)*65535)/100; |
---|
| 399 | } else { |
---|
| 400 | vol_r = atoi(k); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | mixer.mixer[0] = vol_l; |
---|
| 404 | mixer.mixer[1] = vol_r; |
---|
| 405 | mixer.mixer[2] = vol_l; |
---|
| 406 | mixer.mixer[3] = vol_r; |
---|
| 407 | |
---|
[85] | 408 | } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) { |
---|
| 409 | chans = 2; |
---|
| 410 | // printf("mode: stereo; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
| 411 | ROAR_ERR("mode stereo not supported"); |
---|
| 412 | return -1; |
---|
| 413 | } else { |
---|
| 414 | if ( strcmp(k, "mono") == 0 ) { |
---|
| 415 | chans = 1; |
---|
| 416 | } else if ( strcmp(k, "stereo") == 0 ) { |
---|
| 417 | chans = 2; |
---|
| 418 | } else { |
---|
| 419 | chans = atoi(k); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | // printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans); |
---|
| 423 | |
---|
| 424 | if ( *cur + chans > max ) |
---|
| 425 | return -1; |
---|
| 426 | |
---|
| 427 | for (i = 0; i < chans; i++) { |
---|
| 428 | k = arg[++(*cur)]; |
---|
| 429 | len = strlen(k); |
---|
| 430 | |
---|
| 431 | if ( k[len - 1] == '%' ) { |
---|
| 432 | k[len - 1] = 0; |
---|
| 433 | mixer.mixer[i] = (atof(k)*(int)65535)/100; |
---|
| 434 | } else { |
---|
| 435 | mixer.mixer[i] = atoi(k); |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | mixer.scale = 65535; |
---|
| 441 | |
---|
| 442 | return roar_set_vol(con, id, &mixer, chans); |
---|
| 443 | } |
---|
| 444 | |
---|
[99] | 445 | int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) { |
---|
| 446 | struct roar_meta meta; |
---|
| 447 | struct roar_stream s; |
---|
| 448 | int mode_i = ROAR_META_MODE_SET; |
---|
| 449 | |
---|
[1127] | 450 | memset(&s, 0, sizeof(s)); |
---|
| 451 | |
---|
[99] | 452 | s.id = id; |
---|
| 453 | |
---|
[106] | 454 | // printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val); |
---|
[99] | 455 | |
---|
| 456 | if ( strcmp(mode, "add") == 0 ) { |
---|
| 457 | mode_i = ROAR_META_MODE_ADD; |
---|
| 458 | } |
---|
| 459 | |
---|
[109] | 460 | meta.type = roar_meta_inttype(type); |
---|
[106] | 461 | meta.value = val; |
---|
| 462 | meta.key[0] = 0; |
---|
[99] | 463 | |
---|
[109] | 464 | if ( meta.type == -1 ) { |
---|
| 465 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
| 466 | return -1; |
---|
| 467 | } |
---|
| 468 | |
---|
[106] | 469 | // printf("D: type=%i, mode=%i\n", meta.type, mode_i); |
---|
[99] | 470 | |
---|
[1127] | 471 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
| 472 | return -1; |
---|
| 473 | |
---|
| 474 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 475 | meta.value = NULL; |
---|
| 476 | |
---|
| 477 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
[99] | 478 | } |
---|
| 479 | |
---|
[1126] | 480 | int load_meta (struct roar_connection * con, int id, char * file) { |
---|
| 481 | struct roar_meta meta; |
---|
| 482 | struct roar_stream s; |
---|
| 483 | int mode_i = ROAR_META_MODE_SET; |
---|
| 484 | FILE * in; |
---|
| 485 | char lion[1024]; |
---|
| 486 | char * v; |
---|
| 487 | |
---|
| 488 | memset(&s, 0, sizeof(s)); |
---|
| 489 | |
---|
| 490 | s.id = id; |
---|
| 491 | |
---|
| 492 | if ( (in = fopen(file, "r")) == NULL ) |
---|
| 493 | return -1; |
---|
| 494 | |
---|
| 495 | while (fgets(lion, 1024, in) != NULL) { |
---|
| 496 | if ( (v = strtok(lion, "\r\n")) != NULL ) |
---|
| 497 | if ( (v = strtok(NULL, "\r\n")) != NULL ) |
---|
| 498 | *(v-1) = 0; |
---|
| 499 | |
---|
| 500 | if ( (v = strstr(lion, "=")) == NULL ) { |
---|
| 501 | fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion); |
---|
| 502 | continue; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | *v++ = 0; |
---|
| 506 | |
---|
| 507 | meta.type = roar_meta_inttype(lion); |
---|
| 508 | meta.value = v; |
---|
| 509 | meta.key[0] = 0; |
---|
| 510 | |
---|
| 511 | if ( meta.type == -1 ) { |
---|
| 512 | fprintf(stderr, "Error: unknown type: %s\n", lion); |
---|
| 513 | continue; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) |
---|
| 517 | return -1; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | fclose(in); |
---|
| 521 | |
---|
| 522 | meta.type = ROAR_META_TYPE_NONE; |
---|
| 523 | meta.value = NULL; |
---|
| 524 | |
---|
| 525 | return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta); |
---|
| 526 | } |
---|
| 527 | |
---|
[106] | 528 | int show_meta_type (struct roar_connection * con, int id, char * type) { |
---|
| 529 | struct roar_meta meta; |
---|
| 530 | struct roar_stream s; |
---|
| 531 | |
---|
[1127] | 532 | memset(&s, 0, sizeof(s)); |
---|
| 533 | |
---|
[106] | 534 | s.id = id; |
---|
| 535 | |
---|
[109] | 536 | meta.type = roar_meta_inttype(type); |
---|
| 537 | |
---|
| 538 | if ( meta.type == -1 ) { |
---|
| 539 | fprintf(stderr, "Error: unknown type: %s\n", type); |
---|
| 540 | return -1; |
---|
| 541 | } |
---|
[106] | 542 | |
---|
| 543 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
| 544 | return -1; |
---|
| 545 | |
---|
[112] | 546 | printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
[106] | 547 | |
---|
| 548 | roar_meta_free(&meta); |
---|
| 549 | |
---|
| 550 | return 0; |
---|
| 551 | } |
---|
| 552 | |
---|
[112] | 553 | int show_meta_all (struct roar_connection * con, int id) { |
---|
| 554 | struct roar_stream s; |
---|
| 555 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
| 556 | int i; |
---|
| 557 | int len; |
---|
| 558 | |
---|
[1127] | 559 | memset(&s, 0, sizeof(s)); |
---|
| 560 | |
---|
[112] | 561 | s.id = id; |
---|
| 562 | |
---|
| 563 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
| 564 | return -1; |
---|
| 565 | |
---|
| 566 | for (i = 0; i < len; i++) |
---|
| 567 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
| 568 | |
---|
| 569 | return 0; |
---|
| 570 | } |
---|
| 571 | |
---|
[1126] | 572 | int save_meta (struct roar_connection * con, int id, char * file) { |
---|
| 573 | struct roar_stream s; |
---|
| 574 | struct roar_meta meta; |
---|
| 575 | int types[ROAR_META_MAX_PER_STREAM]; |
---|
| 576 | int i; |
---|
| 577 | int len; |
---|
| 578 | FILE * out; |
---|
| 579 | |
---|
| 580 | memset(&s, 0, sizeof(s)); |
---|
| 581 | |
---|
| 582 | s.id = id; |
---|
| 583 | |
---|
| 584 | if ( (out = fopen(file, "w")) == NULL ) |
---|
| 585 | return -1; |
---|
| 586 | |
---|
| 587 | if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 ) |
---|
| 588 | return -1; |
---|
| 589 | |
---|
| 590 | for (i = 0; i < len; i++) { |
---|
| 591 | /* |
---|
| 592 | show_meta_type(con, id, roar_meta_strtype(types[i])); |
---|
| 593 | */ |
---|
| 594 | meta.type = types[i]; |
---|
| 595 | |
---|
| 596 | if ( roar_stream_meta_get(con, &s, &meta) == -1 ) |
---|
| 597 | continue; |
---|
| 598 | |
---|
| 599 | // printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value); |
---|
| 600 | |
---|
| 601 | fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value); |
---|
| 602 | |
---|
| 603 | roar_meta_free(&meta); |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | fclose(out); |
---|
| 607 | |
---|
| 608 | return 0; |
---|
| 609 | } |
---|
| 610 | |
---|
[1043] | 611 | int set_flags (struct roar_connection * con, int id, int reset, char * flags) { |
---|
| 612 | int f = ROAR_FLAG_NONE; |
---|
| 613 | char * c; |
---|
| 614 | struct roar_stream s[1]; |
---|
| 615 | |
---|
| 616 | memset(s, 0, sizeof(struct roar_stream)); |
---|
| 617 | |
---|
| 618 | s->id = id; |
---|
| 619 | |
---|
| 620 | c = strtok(flags, ","); |
---|
| 621 | while (c != NULL) { |
---|
| 622 | if ( !strcmp(c, "meta") ) { |
---|
| 623 | f |= ROAR_FLAG_META; |
---|
| 624 | } else if ( !strcmp(c, "primary") ) { |
---|
| 625 | f |= ROAR_FLAG_PRIMARY; |
---|
[1116] | 626 | } else if ( !strcmp(c, "sync") ) { |
---|
| 627 | f |= ROAR_FLAG_SYNC; |
---|
[1219] | 628 | } else if ( !strcmp(c, "cleanmeta") ) { |
---|
| 629 | f |= ROAR_FLAG_CLEANMETA; |
---|
[1585] | 630 | } else if ( !strcmp(c, "hwmixer") ) { |
---|
| 631 | f |= ROAR_FLAG_HWMIXER; |
---|
| 632 | } else if ( !strcmp(c, "pause") ) { |
---|
| 633 | f |= ROAR_FLAG_PAUSE; |
---|
[1043] | 634 | } else { |
---|
| 635 | fprintf(stderr, "Error: unknown flag: %s\n", c); |
---|
| 636 | return -1; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | c = strtok(NULL, ","); |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | return roar_stream_set_flags(con, s, f, reset); |
---|
| 643 | } |
---|
| 644 | |
---|
[0] | 645 | int main (int argc, char * argv[]) { |
---|
| 646 | struct roar_connection con; |
---|
| 647 | char * server = NULL; |
---|
| 648 | char * k = NULL; |
---|
| 649 | int i; |
---|
| 650 | int t = 0; |
---|
| 651 | |
---|
| 652 | for (i = 1; i < argc; i++) { |
---|
| 653 | k = argv[i]; |
---|
| 654 | |
---|
| 655 | if ( strcmp(k, "--server") == 0 ) { |
---|
[58] | 656 | server = argv[++i]; |
---|
[963] | 657 | } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) { |
---|
| 658 | g_verbose++; |
---|
[0] | 659 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
| 660 | usage(); |
---|
| 661 | return 0; |
---|
| 662 | } else if ( *k == '-' ) { |
---|
| 663 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 664 | usage(); |
---|
| 665 | return 1; |
---|
| 666 | } else { |
---|
| 667 | break; |
---|
| 668 | } |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | // connect |
---|
| 672 | |
---|
| 673 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 674 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
| 675 | return 1; |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | if ( roar_identify(&con, "roarctl") == -1 ) { |
---|
| 679 | fprintf(stderr, "Error: Can not identify to server\n"); |
---|
| 680 | return 1; |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if ( i == argc ) { |
---|
| 684 | fprintf(stderr, "Error: No Commands given\n"); |
---|
| 685 | return 0; // this is not a fatal error... |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | for (; i < argc; i++) { |
---|
| 689 | k = argv[i]; |
---|
| 690 | // cmd is in k |
---|
| 691 | |
---|
| 692 | printf("--- [ %s ] ---\n", k); |
---|
| 693 | |
---|
| 694 | if ( !strcmp(k, "help") ) { |
---|
| 695 | usage(); |
---|
| 696 | |
---|
[1202] | 697 | } else if ( !strcmp(k, "sleep") ) { |
---|
| 698 | sleep(atoi(argv[++i])); |
---|
[0] | 699 | |
---|
[1630] | 700 | } else if ( !strcmp(k, "ping") ) { |
---|
[1781] | 701 | #ifdef ROAR_HAVE_GETTIMEOFDAY |
---|
[1630] | 702 | if ( ping(&con, atoi(argv[++i])) == -1 ) { |
---|
| 703 | fprintf(stderr, "Error: can not ping\n"); |
---|
| 704 | } |
---|
[1781] | 705 | #else |
---|
| 706 | fprintf(stderr, "Error: ping not supported.\n"); |
---|
| 707 | i++; |
---|
| 708 | #endif |
---|
[1630] | 709 | |
---|
[0] | 710 | } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) { |
---|
| 711 | if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) { |
---|
| 712 | fprintf(stderr, "Error: can not set mode to standby\n"); |
---|
| 713 | } else { |
---|
| 714 | printf("going into standby\n"); |
---|
| 715 | } |
---|
| 716 | } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) { |
---|
| 717 | if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) { |
---|
| 718 | fprintf(stderr, "Error: can not set mode to active\n"); |
---|
| 719 | } else { |
---|
| 720 | printf("going into active mode\n"); |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | } else if ( !strcmp(k, "exit") ) { |
---|
| 724 | if ( roar_exit(&con) == -1 ) { |
---|
| 725 | fprintf(stderr, "Error: can not quit server\n"); |
---|
| 726 | } else { |
---|
| 727 | printf("Server quited\n"); |
---|
| 728 | break; |
---|
| 729 | } |
---|
[577] | 730 | } else if ( !strcmp(k, "terminate") ) { |
---|
| 731 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
| 732 | fprintf(stderr, "Error: can not terminate server\n"); |
---|
| 733 | } else { |
---|
| 734 | printf("Server got asked to quited\n"); |
---|
| 735 | break; |
---|
| 736 | } |
---|
[0] | 737 | |
---|
| 738 | } else if ( !strcmp(k, "standbymode") ) { |
---|
| 739 | t = roar_get_standby(&con); |
---|
| 740 | if ( t == -1 ) { |
---|
| 741 | fprintf(stderr, "Error: can not get stanby mode\n"); |
---|
| 742 | } else if ( t == ROAR_STANDBY_ACTIVE ) { |
---|
| 743 | printf("Server is in standby\n"); |
---|
| 744 | } else if ( t == ROAR_STANDBY_INACTIVE ) { |
---|
| 745 | printf("Server is active\n"); |
---|
| 746 | } else { |
---|
| 747 | fprintf(stderr, "Error: unknown standby mode: %i\n", t); |
---|
| 748 | } |
---|
| 749 | |
---|
[1162] | 750 | } else if ( !strcmp(k, "whoami") ) { |
---|
| 751 | printf("My client ID is: %i\n", roar_get_clientid(&con)); |
---|
[0] | 752 | } else if ( !strcmp(k, "serveroinfo") ) { |
---|
| 753 | server_oinfo(&con); |
---|
| 754 | } else if ( !strcmp(k, "listclients") ) { |
---|
| 755 | list_clients(&con); |
---|
| 756 | } else if ( !strcmp(k, "liststreams") ) { |
---|
| 757 | list_streams(&con); |
---|
| 758 | } else if ( !strcmp(k, "allinfo") ) { |
---|
| 759 | server_oinfo(&con); |
---|
| 760 | printf("\n"); |
---|
| 761 | list_clients(&con); |
---|
| 762 | printf("\n"); |
---|
| 763 | list_streams(&con); |
---|
| 764 | |
---|
| 765 | } else if ( !strcmp(k, "kick") ) { |
---|
| 766 | k = argv[++i]; |
---|
| 767 | if ( !strcmp(k, "client") ) { |
---|
| 768 | t = ROAR_OT_CLIENT; |
---|
| 769 | } else if ( !strcmp(k, "stream") ) { |
---|
| 770 | t = ROAR_OT_STREAM; |
---|
| 771 | } else if ( !strcmp(k, "sample") ) { |
---|
| 772 | t = ROAR_OT_SAMPLE; |
---|
| 773 | } else if ( !strcmp(k, "source") ) { |
---|
| 774 | t = ROAR_OT_SOURCE; |
---|
| 775 | } else { |
---|
| 776 | fprintf(stderr, "Error: unknown type: %s\n", k); |
---|
| 777 | continue; |
---|
| 778 | } |
---|
| 779 | //t = atoi(argv[i++]); |
---|
| 780 | if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) { |
---|
| 781 | fprintf(stderr, "Error: can not kick %s\n", k); |
---|
| 782 | } else { |
---|
| 783 | printf("%s kicked\n", k); |
---|
| 784 | } |
---|
| 785 | |
---|
[85] | 786 | } else if ( !strcmp(k, "volume") ) { |
---|
| 787 | if ( set_mixer(&con, &i, argc, argv) == -1 ) { |
---|
| 788 | fprintf(stderr, "Error: can not set volume\n"); |
---|
| 789 | } else { |
---|
| 790 | printf("volume changed\n"); |
---|
| 791 | } |
---|
| 792 | |
---|
[1043] | 793 | } else if ( !strcmp(k, "flag") ) { |
---|
| 794 | i++; |
---|
| 795 | if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) { |
---|
| 796 | fprintf(stderr, "Error: can not set flags\n"); |
---|
| 797 | } else { |
---|
| 798 | printf("flags changed\n"); |
---|
| 799 | } |
---|
| 800 | i++; |
---|
| 801 | } else if ( !strcmp(k, "unflag") ) { |
---|
| 802 | i++; |
---|
| 803 | if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) { |
---|
| 804 | fprintf(stderr, "Error: can not reset flags\n"); |
---|
| 805 | } else { |
---|
| 806 | printf("flags changed\n"); |
---|
| 807 | } |
---|
| 808 | i++; |
---|
[106] | 809 | } else if ( !strcmp(k, "metaset") ) { |
---|
[99] | 810 | i++; |
---|
| 811 | if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) { |
---|
| 812 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
| 813 | } else { |
---|
| 814 | printf("meta data changed\n"); |
---|
| 815 | } |
---|
| 816 | i += 3; |
---|
[106] | 817 | } else if ( !strcmp(k, "metaget") ) { |
---|
| 818 | i++; |
---|
| 819 | if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
| 820 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
| 821 | } |
---|
| 822 | i++; |
---|
[1126] | 823 | } else if ( !strcmp(k, "metasave") ) { |
---|
| 824 | i++; |
---|
| 825 | if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
| 826 | fprintf(stderr, "Error: can not get meta data\n"); |
---|
| 827 | } else { |
---|
| 828 | printf("meta data saved\n"); |
---|
| 829 | } |
---|
| 830 | i++; |
---|
| 831 | } else if ( !strcmp(k, "metaload") ) { |
---|
| 832 | i++; |
---|
| 833 | if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) { |
---|
| 834 | fprintf(stderr, "Error: can not set meta data\n"); |
---|
| 835 | } else { |
---|
| 836 | printf("meta data saved\n"); |
---|
| 837 | } |
---|
| 838 | i++; |
---|
[99] | 839 | |
---|
[0] | 840 | } else { |
---|
| 841 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
| 842 | } |
---|
| 843 | |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | roar_disconnect(&con); |
---|
| 847 | |
---|
| 848 | return 0; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | //ll |
---|