source: roaraudio/roarclients/roarctl.c @ 3531:c850208ec638

Last change on this file since 3531:c850208ec638 was 3531:c850208ec638, checked in by phi, 14 years ago

do not display rpg infos on zero-channel streams

File size: 27.6 KB
Line 
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#define _UNITS_T_BASE_USEC
27
28#include <roaraudio.h>
29#include <roaraudio/units.h>
30
31#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID)
32#define _POSIX_USERS
33#endif
34
35#ifdef _POSIX_USERS
36#include <pwd.h>
37#include <grp.h>
38#endif
39
40#include <sys/time.h>
41#include <time.h>
42
43#ifdef ROAR_HAVE_LIBM
44#include <math.h>
45#endif
46
47int g_verbose = 0;
48
49int display_mixer (struct roar_connection * con, int stream);
50int show_meta_all (struct roar_connection * con, int id);
51
52void usage (void) {
53 printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n");
54
55 printf("\nOptions:\n\n");
56
57 printf("  --server SERVER         - Set server hostname\n"
58        "  --help                  - Show this help\n"
59        "  --verbose   -v          - Show verbose output\n"
60       );
61
62 printf("\nCommands:\n\n");
63 printf(
64        "  help                    - Show this help\n"
65        "  sleep TIME              - Sleeps for TIME seconds\n"
66#ifdef ROAR_HAVE_GETTIMEOFDAY
67        "  ping  NUM               - Do NUM pings using NOOP commands\n"
68#endif
69        "  whoami                  - Get own client ID\n"
70        "\n"
71        "  standby, off            - Go into standby mode\n"
72        "  resume, on              - Go into active mode\n"
73        "  standbymode             - Show current standby mode\n"
74        "  exit                    - Quits the roard (must be used as last command)\n"
75        "  terminate               - Like exit but let the server up to serve still connected clients,\n"
76        "                            new clients cann't connect and the server terminates after the last\n"
77        "                            client disconnected\n"
78        "\n"
79        "  volume ID CHAN V0 V1... - Sets volume for stream ID\n"
80        "                            CHAN is the number of channels or 'mono' or 'stereo'\n"
81        "                            if mono or stereo is chosen roarctl trys to set\n"
82        "                            sensfull values for all channels even if the output\n"
83        "                            is has more channels.\n"
84        "                            all other args are the volumes of the channels\n"
85        "                            you may use integer or percent values.\n"
86        "                            percent values can flooding points.\n"
87        "\n"
88        "  flag   ID FLAGS         - Sets flags FLAGS on stream ID. FLAGS may be a comma\n"
89        "                            seperated list of flags.\n"
90        "  unflag ID FLAGS         - Unsets flags on a stream. See flag.\n"
91        "\n"
92        "  kick TYPE ID            - Kicks object of TYPE with id ID\n"
93        "                            Types: client stream sample source\n"
94        "  newvirtual P D E R B C  - Adds a new virtual (child) stream\n"
95        "                            Parameters:\n"
96        "                             P: Parent stream ID, D: Direction,\n"
97        "                             E: codEc, R: sample Rate,\n"
98        "                             B: bits per sample, C: nummer of channels\n"
99        "\n"
100        "  metaget  ID TYPE        - Read meta date of type TYPE from stream ID\n"
101// TODO: document metaset here.
102        "  metasave ID FILE        - Saves meta data of stream ID to file FILE\n"
103        "  metaload ID FILE        - Loads meta data from file FILE and set it on stream ID\n"
104        "\n"
105        "  serveroinfo             - Gets Informations about server output\n"
106        "  listclients             - Gets Informations about clients\n"
107        "  liststreams             - Gets Informations about streams\n"
108        "  allinfo                 - Get all infos\n"
109       );
110}
111
112#ifdef ROAR_HAVE_GETTIMEOFDAY
113int ping (struct roar_connection * con, int num) {
114 struct timeval         try, ans;
115 struct roar_message    m;
116 register int           ret;
117 int                    i;
118 double                 cur, min = 3600*1000, max = 0, sum = 0;
119
120 if ( num == 0 )
121  return 0;
122
123 for (i = 0; i < num; i++) {
124  m.cmd = ROAR_CMD_NOOP;
125  m.datalen = 0;
126
127  gettimeofday(&try, NULL);
128  ret = roar_req(con, &m, NULL);
129  gettimeofday(&ans, NULL);
130
131  if ( ret == -1 )
132   return -1;
133
134  while (ans.tv_sec > try.tv_sec) {
135   ans.tv_sec--;
136   ans.tv_usec += 1000000;
137  }
138  ans.tv_usec -= try.tv_usec;
139
140  printf("Pong from server: seq=%i time=%.3fms\n", i, (cur = ans.tv_usec/1000.0));
141
142  sum += cur;
143  if ( min > cur )
144   min = cur;
145  if ( cur > max )
146   max = cur;
147
148  if ( i != (num - 1) )
149   sleep(1);
150 }
151
152 printf("\n--- ping statistics ---\n");
153 printf("%i packets transmitted\n", i);
154 printf("rtt min/avg/max = %.3f/%.3f/%.3f ms\n", min, sum/(double)i, max);
155
156 return 0;
157}
158#endif
159
160void server_oinfo (struct roar_connection * con) {
161 struct roar_stream s;
162
163 if ( roar_server_oinfo(con, &s) == -1 ) {
164  fprintf(stderr, "Error: can not get server output info\n");
165  return;
166 }
167
168 printf("Stream direction      : %s\n", roar_dir2str(s.dir));
169 printf("Server Output rate    : %i\n", s.info.rate);
170 printf("Server Output bits    : %i\n", s.info.bits);
171 printf("Server Output channels: %i\n", s.info.channels);
172 printf("Server Output codec   : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
173                                     s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
174// printf("Server Output rate: %i", s.info.rate);
175  if ( g_verbose > 1 )
176   printf("Server Position       : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels));
177}
178
179const char * proc_name (pid_t pid) {
180 static char ret[80] = "?";
181#ifdef __linux__
182 char file[80], buf[80], *r;
183 int  i;
184
185 snprintf(file, 79, "/proc/%i/exe", pid);
186 file[79] = 0;
187
188 ret[0] = '?';
189 ret[1] = 0;
190
191 if ( (i = readlink(file, buf, 79)) != -1 ) {
192  buf[i] = 0;
193  if ( (r = strrchr(buf, '/')) != NULL ) {
194   r++;
195   if ( *r != 0 )
196    strcpy(ret, r);
197  }
198 }
199#endif
200
201 return ret;
202}
203
204void list_clients (struct roar_connection * con) {
205 int i;
206 int num;
207 int h;
208 int id[ROAR_CLIENTS_MAX];
209 char tmp[80];
210 int self_id;
211 struct roar_client self_client;
212 struct roar_client c;
213#ifdef _POSIX_USERS
214 struct group  * grp = NULL;
215 struct passwd * pwd = NULL;
216#endif
217
218 if ( (self_id = roar_get_clientid(con)) != -1 ) {
219  if ( roar_get_client(con, &self_client, self_id) == -1 )
220   self_id = -1;
221 }
222
223 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
224  fprintf(stderr, "Error: can not get client list\n");
225  return;
226 }
227
228 for (i = 0; i < num; i++) {
229  printf("client %i:\n", id[i]);
230  if ( roar_get_client(con, &c, id[i]) == -1 ) {
231   fprintf(stderr, "Error: can not get client info\n");
232   continue;
233  }
234  printf("Client name           : %s\n", c.name);
235
236  if ( roar_nnode_get_socktype(&(c.nnode)) != ROAR_SOCKET_TYPE_UNKNOWN ) {
237   if ( roar_nnode_to_str(&(c.nnode), tmp, 80) == 0 ) {
238    printf("Client network node   : %s\n", tmp);
239   }
240  }
241
242  if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
243   printf("Client PID            : %i(%s)\n", c.pid, proc_name(c.pid));
244  } else {
245   printf("Client PID            : %i\n", c.pid);
246  }
247  if ( c.uid != -1 ) {
248#ifdef _POSIX_USERS
249   if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
250    pwd = getpwuid(c.uid);
251    grp = getgrgid(c.gid);
252    printf("Client UID/GID        : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?");
253   } else {
254#else
255   if ( 1 ) {
256#endif
257    printf("Client UID/GID        : %i/%i\n", c.uid, c.gid);
258   }
259  }
260
261  if ( g_verbose && c.proto != ROAR_PROTO_NONE ) {
262   printf("Client Protocol       : %s\n", roar_proto2str(c.proto));
263  }
264
265  if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) {
266   if ( c.byteorder == ROAR_BYTEORDER_NETWORK ) {
267    strcpy(tmp, " (network byteorder");
268   } else {
269    *tmp = 0;
270   }
271
272   if ( c.byteorder == ROAR_BYTEORDER_NATIVE ) {
273    if ( *tmp ) {
274     strcat(tmp, ", native");
275    } else {
276     strcpy(tmp, " (native");
277    }
278   }
279
280   if ( *tmp )
281    strcat(tmp, ")");
282
283   printf("Client Byteorder      : %s%s\n", roar_byteorder2str(c.byteorder), tmp);
284  }
285
286  if ( c.execed != -1 )
287   printf("Execed stream         : %i\n", c.execed);
288
289  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
290   if ( c.streams[h] != -1 )
291    printf("stream                : %i\n", c.streams[h]);
292 }
293
294}
295
296void list_streams (struct roar_connection * con) {
297 int i;
298 int num;
299 int id[ROAR_STREAMS_MAX];
300 struct roar_stream s;
301 struct roar_stream_info info;
302 char flags[1024];
303 char name[1024];
304 char * infotext;
305
306
307 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
308  fprintf(stderr, "Error: can not get stream list\n");
309  return;
310 }
311
312 for (i = 0; i < num; i++) {
313  printf("stream %i:\n", id[i]);
314  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
315   fprintf(stderr, "Error: can not get stream info\n");
316   continue;
317  }
318  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
319
320  if ( roar_stream_get_name(con, &s, name, 1024) == 0 )
321   printf("Stream name           : %s\n", name);
322
323  if ( s.pos_rel_id == -1 ) {
324   printf("Relativ position id   : none (stream not synchronized)\n");
325  } else if ( s.pos_rel_id == id[i] ) {
326   printf("Relativ position id   : %i (self synchronized)\n", s.pos_rel_id);
327  } else {
328   printf("Relativ position id   : %i (synchronized)\n", s.pos_rel_id);
329  }
330  if ( g_verbose > 1 ) {
331   if ( s.info.rate && s.info.channels ) {
332    printf("Position              : %lu S (%.3fs)\n", (unsigned long int) s.pos,
333                                    (float)s.pos/(s.info.rate*s.info.channels));
334   } else {
335    printf("Position              : %lu S\n", (unsigned long int) s.pos);
336   }
337  }
338
339  switch (s.dir) {
340   case ROAR_DIR_MIDI_IN:
341   case ROAR_DIR_MIDI_OUT:
342     infotext = " ticks/s";
343    break;
344   case ROAR_DIR_LIGHT_IN:
345   case ROAR_DIR_LIGHT_OUT:
346     infotext = " updates/s";
347    break;
348   default:
349     infotext = " Hz";
350  }
351  if ( s.info.rate )
352   printf("Stream sample rate    : %i%s\n", s.info.rate, infotext);
353  if ( s.info.bits )
354   printf("Stream bits           : %i\n", s.info.bits);
355  if ( s.info.channels )
356  printf("Stream channels       : %i\n", s.info.channels);
357
358  printf("Stream codec          : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
359                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
360  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
361   if ( info.codec != s.info.codec ) {
362    printf("Streamed codec        : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
363                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
364   }
365
366   if ( g_verbose ) {
367    if ( info.block_size )
368     printf("Stream block size     : %i Byte\n", info.block_size);
369
370    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
371    if ( g_verbose > 1 )
372     printf("Stream delay          : %ims (%.2fm)\n",   (int)info.delay/1000, (info.delay*(float)_SPEED_OF_SOUND));
373
374    if ( g_verbose > 1 )
375     printf("Stream mixer          : %i\n",   info.mixer);
376
377    if ( g_verbose > 1 )
378     printf("Stream state          : %s\n",   roar_streamstate2str(info.state));
379
380    *flags = 0;
381    if ( info.flags & ROAR_FLAG_PRIMARY )
382     strcat(flags, "primary ");
383    if ( info.flags & ROAR_FLAG_SYNC )
384     strcat(flags, "sync ");
385    if ( info.flags & ROAR_FLAG_OUTPUT )
386     strcat(flags, "output ");
387    if ( info.flags & ROAR_FLAG_SOURCE )
388     strcat(flags, "source ");
389    if ( info.flags & ROAR_FLAG_META )
390     strcat(flags, "meta ");
391    if ( info.flags & ROAR_FLAG_AUTOCONF )
392     strcat(flags, "autoconf ");
393    if ( info.flags & ROAR_FLAG_CLEANMETA )
394     strcat(flags, "cleanmeta ");
395    if ( info.flags & ROAR_FLAG_HWMIXER )
396     strcat(flags, "hwmixer ");
397    if ( info.flags & ROAR_FLAG_PAUSE )
398     strcat(flags, "pause ");
399    if ( info.flags & ROAR_FLAG_MUTE )
400     strcat(flags, "mute ");
401    if ( info.flags & ROAR_FLAG_MMAP )
402     strcat(flags, "mmap ");
403    if ( info.flags & ROAR_FLAG_ANTIECHO )
404     strcat(flags, "antiecho ");
405    if ( info.flags & ROAR_FLAG_VIRTUAL )
406     strcat(flags, "virtual ");
407    if ( info.flags & ROAR_FLAG_RECSOURCE )
408     strcat(flags, "recsource ");
409    if ( info.flags & ROAR_FLAG_PASSMIXER )
410     strcat(flags, "passmixer ");
411    if ( info.flags & ROAR_FLAG_PRETHRU )
412     strcat(flags, "prethru ");
413    if ( info.flags & ROAR_FLAG_IMMUTABLE )
414     strcat(flags, "immutable ");
415    if ( info.flags & ROAR_FLAG_ENHANCE )
416     strcat(flags, "enhance ");
417
418    printf("Flags                 : %s\n", flags);
419   }
420  }
421
422  if ( s.dir != ROAR_DIR_THRU ) {
423   display_mixer(con, id[i]);
424   show_meta_all(con, id[i]);
425  }
426 }
427
428}
429
430int display_mixer (struct roar_connection * con, int stream) {
431 struct roar_mixer_settings mixer;
432 int channels;
433 int i;
434 float fs;
435
436 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
437  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream);
438  return -1;
439 }
440
441#ifdef ROAR_HAVE_LIBM
442#define _DB ", %.2fdB"
443#else
444#define _DB ""
445#endif
446
447 fs = (float)mixer.scale / 100.;
448
449 if ( channels ) { // we hide RPG info for zero-channel streams
450  printf("Mixer ReplayGain      : %i/%i (%.2f%%" _DB ")\n", mixer.rpg_mul, mixer.rpg_div,
451                                                          100.*(float)mixer.rpg_mul/((float)mixer.rpg_div)
452#ifdef ROAR_HAVE_LIBM
453                           , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div)
454#endif
455        );
456 }
457
458 for (i = 0; i < channels; i++)
459  printf("Mixer volume chan %2i  : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale,
460                           (float)mixer.mixer[i]/fs
461#ifdef ROAR_HAVE_LIBM
462                          , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale)
463#endif
464        );
465
466 return 0;
467}
468
469static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) {
470 float fs = scale;
471
472 switch (k[len - 1]) {
473  case '%':
474    k[len - 1] = 0;
475    return (atof(k)*fs)/100.;
476   break;
477#ifdef ROAR_HAVE_LIBM
478  case 'b':
479  case 'B':
480    // TODO: can we hanle the error better?
481    if ( len < 2 )
482     return 0;
483
484    k[len - 2] = 0;
485    return powf(10, atof(k)/20.f)*fs;
486   break;
487#endif
488 }
489
490 return atoi(k);
491}
492
493int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
494 uint16_t scale = 65535;
495 int chans = 0;
496 int id;
497 int i;
498 int len;
499 int old_chans;
500 int vol_l, vol_r, vol_mono;
501 char * k;
502 struct roar_mixer_settings mixer;
503 struct roar_mixer_settings old_mixer;
504
505 if (*cur + 2 > max)
506  return -1;
507
508 id = atoi(arg[++(*cur)]);
509
510 k = arg[++(*cur)];
511
512 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
513  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
514  return -1;
515 }
516
517 if ( !strcmp(arg[*cur + 1], "scale") ) {
518  (*cur)++; // 'scale'
519  scale = atoi(arg[++(*cur)]);
520 }
521
522// TODO: clean up code here as the % vs. abs code is very duplicate...
523
524 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
525  chans = 1;
526
527  if ( *cur + 1 > max )
528   return -1;
529
530  k   = arg[++(*cur)];
531  len = strlen(k);
532
533  vol_mono = set_mixer_parse_volume(k, len, scale);
534
535  for (i = 0; i < old_chans; i++)
536   mixer.mixer[i] = vol_mono;
537
538  chans = old_chans;
539
540 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
541  chans = old_chans;
542
543  if ( *cur + 2 > max )
544   return -1;
545
546  k   = arg[++(*cur)];
547  len = strlen(k);
548
549  vol_l = set_mixer_parse_volume(k, len, scale);
550
551  k   = arg[++(*cur)];
552  len = strlen(k);
553
554  vol_r = set_mixer_parse_volume(k, len, scale);
555
556  vol_mono = (vol_l + vol_r) / 2;
557
558  mixer.mixer[0] = vol_l;
559  mixer.mixer[1] = vol_r;
560
561  switch (chans) {
562   case 1:
563     mixer.mixer[0] = vol_mono;
564    break;
565//   case 2: classic stereo...
566   case 3:
567     mixer.mixer[2] = vol_mono;
568    break;
569   case 4:
570     mixer.mixer[2] = vol_l;
571     mixer.mixer[3] = vol_r;
572    break;
573   case 5:
574     mixer.mixer[2] = vol_mono;
575     mixer.mixer[3] = vol_l;
576     mixer.mixer[4] = vol_r;
577    break;
578   case 6:
579     mixer.mixer[2] = vol_mono;
580     mixer.mixer[3] = vol_mono;
581     mixer.mixer[4] = vol_l;
582     mixer.mixer[5] = vol_r;
583    break;
584   default:
585     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
586     return -1;
587    break;
588  }
589
590 } else {
591  if ( strcmp(k, "mono") == 0 ) {
592   chans = 1;
593  } else if ( strcmp(k, "stereo") == 0 ) {
594   chans = 2;
595  } else {
596   chans = atoi(k);
597  }
598
599//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
600
601  if ( *cur + chans > max )
602   return -1;
603
604  for (i = 0; i < chans; i++) {
605   k   = arg[++(*cur)];
606   len = strlen(k);
607
608   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
609  }
610 }
611
612 mixer.scale = scale;
613
614 return roar_set_vol(con, id, &mixer, chans);
615}
616
617
618int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
619 struct roar_stream s;
620 int dir    = roar_str2dir(d_s);
621 int parent = atoi(p_s);
622
623 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
624
625 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
626  return -1;
627
628 return roar_simple_connect_virtual(con, &s, parent, dir);
629}
630
631int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
632 struct roar_meta   meta;
633 struct roar_stream s;
634 int mode_i = ROAR_META_MODE_SET;
635
636 if ( roar_stream_new_by_id(&s, id) == -1 )
637  return -1;
638
639// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
640
641 if ( strcmp(mode, "add") == 0 ) {
642  mode_i = ROAR_META_MODE_ADD;
643 }
644
645 meta.type   = roar_meta_inttype(type);
646 meta.value  = val;
647 meta.key[0] = 0;
648
649 if ( meta.type == -1 ) {
650  fprintf(stderr, "Error: unknown type: %s\n", type);
651  return -1;
652 }
653
654// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
655
656 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
657  return -1;
658
659 meta.type  = ROAR_META_TYPE_NONE;
660 meta.value = NULL;
661
662 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
663}
664
665int load_meta (struct roar_connection * con, int id, char * file) {
666 struct roar_meta   meta;
667 struct roar_stream s;
668 int mode_i = ROAR_META_MODE_SET;
669 FILE * in;
670 char lion[1024];
671 char * v;
672
673 if ( roar_stream_new_by_id(&s, id) == -1 )
674  return -1;
675
676 if ( (in = fopen(file, "r")) == NULL )
677  return -1;
678
679 while (fgets(lion, 1024, in) != NULL) {
680  if ( (v = strtok(lion, "\r\n")) != NULL )
681   if ( (v = strtok(NULL, "\r\n")) != NULL )
682    *(v-1) = 0;
683
684  if ( (v = strstr(lion, "=")) == NULL ) {
685   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
686   continue;
687  }
688
689  *v++ = 0;
690
691  meta.type   = roar_meta_inttype(lion);
692  meta.value  = v;
693  meta.key[0] = 0;
694
695  if ( meta.type == -1 ) {
696   fprintf(stderr, "Error: unknown type: %s\n", lion);
697   continue;
698  }
699
700  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
701   return -1;
702 }
703
704 fclose(in);
705
706 meta.type  = ROAR_META_TYPE_NONE;
707 meta.value = NULL;
708
709 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
710}
711
712int show_meta_type (struct roar_connection * con, int id, char * type) {
713 struct roar_meta   meta;
714 struct roar_stream s;
715
716 if ( roar_stream_new_by_id(&s, id) == -1 )
717  return -1;
718
719 meta.type  = roar_meta_inttype(type);
720
721 if ( meta.type == -1 ) {
722  fprintf(stderr, "Error: unknown type: %s\n", type);
723  return -1;
724 }
725
726 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
727  return -1;
728
729 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
730
731 roar_meta_free(&meta);
732
733 return 0;
734}
735
736int show_meta_all (struct roar_connection * con, int id) {
737 struct roar_stream s;
738 int types[ROAR_META_MAX_PER_STREAM];
739 int i;
740 int len;
741
742 if ( roar_stream_new_by_id(&s, id) == -1 )
743  return -1;
744
745 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
746  return -1;
747
748 for (i = 0; i < len; i++)
749  show_meta_type(con, id, roar_meta_strtype(types[i]));
750
751 return 0;
752}
753
754int save_meta (struct roar_connection * con, int id, char * file) {
755 struct roar_stream s;
756 struct roar_meta   meta;
757 int types[ROAR_META_MAX_PER_STREAM];
758 int i;
759 int len;
760 FILE * out;
761
762 if ( roar_stream_new_by_id(&s, id) == -1 )
763  return -1;
764
765 if ( (out = fopen(file, "w")) == NULL )
766  return -1;
767
768 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
769  return -1;
770
771 for (i = 0; i < len; i++) {
772/*
773  show_meta_type(con, id, roar_meta_strtype(types[i]));
774*/
775  meta.type  = types[i];
776
777  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
778   continue;
779
780//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
781
782  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
783
784  roar_meta_free(&meta);
785 }
786
787 fclose(out);
788
789 return 0;
790}
791
792int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
793 int f = ROAR_FLAG_NONE;
794 char * c;
795 struct roar_stream s[1];
796
797 if ( roar_stream_new_by_id(s, id) == -1 )
798  return -1;
799
800 c = strtok(flags, ",");
801 while (c != NULL) {
802  if ( !strcmp(c, "meta") ) {
803   f |= ROAR_FLAG_META;
804  } else if ( !strcmp(c, "primary") ) {
805   f |= ROAR_FLAG_PRIMARY;
806  } else if ( !strcmp(c, "sync") ) {
807   f |= ROAR_FLAG_SYNC;
808  } else if ( !strcmp(c, "cleanmeta") ) {
809   f |= ROAR_FLAG_CLEANMETA;
810  } else if ( !strcmp(c, "hwmixer") ) {
811   f |= ROAR_FLAG_HWMIXER;
812  } else if ( !strcmp(c, "pause") ) {
813   f |= ROAR_FLAG_PAUSE;
814  } else if ( !strcmp(c, "mute") ) {
815   f |= ROAR_FLAG_MUTE;
816  } else if ( !strcmp(c, "mmap") ) {
817   f |= ROAR_FLAG_MMAP;
818  } else if ( !strcmp(c, "antiecho") ) {
819   f |= ROAR_FLAG_ANTIECHO;
820  } else if ( !strcmp(c, "recsource") ) {
821   f |= ROAR_FLAG_RECSOURCE;
822  } else if ( !strcmp(c, "passmixer") ) {
823   f |= ROAR_FLAG_PASSMIXER;
824  } else if ( !strcmp(c, "virtual") ) {
825   f |= ROAR_FLAG_VIRTUAL;
826  } else if ( !strcmp(c, "prethru") ) {
827   f |= ROAR_FLAG_PRETHRU;
828  } else if ( !strcmp(c, "immutable") ) {
829   f |= ROAR_FLAG_IMMUTABLE;
830  } else if ( !strcmp(c, "enhance") ) {
831   f |= ROAR_FLAG_ENHANCE;
832  } else {
833   fprintf(stderr, "Error: unknown flag: %s\n", c);
834   return -1;
835  }
836
837  c = strtok(NULL, ",");
838 }
839
840 return roar_stream_set_flags(con, s, f, reset);
841}
842
843int main (int argc, char * argv[]) {
844 struct roar_connection con;
845 char * server   = NULL;
846 char * k = NULL;
847 int    i;
848 int    t = 0;
849
850 for (i = 1; i < argc; i++) {
851  k = argv[i];
852
853  if ( strcmp(k, "--server") == 0 ) {
854   server = argv[++i];
855  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
856   g_verbose++;
857  } else if ( strcmp(k, "--help") == 0 ) {
858   usage();
859   return 0;
860  } else if ( *k == '-' ) {
861   fprintf(stderr, "Error: unknown argument: %s\n", k);
862   usage();
863   return 1;
864  } else {
865   break;
866  }
867 }
868
869 // connect
870
871 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
872  fprintf(stderr, "Error: Can not connect to server\n");
873  return 1;
874 }
875
876 if ( i == argc ) {
877  fprintf(stderr, "Error: No Commands given\n");
878  return 0; // this is not a fatal error...
879 }
880
881 for (; i < argc; i++) {
882  k = argv[i];
883  // cmd is in k
884
885  printf("--- [ %s ] ---\n", k);
886
887  if ( !strcmp(k, "help") ) {
888   usage();
889
890  } else if ( !strcmp(k, "sleep") ) {
891   sleep(atoi(argv[++i]));
892
893  } else if ( !strcmp(k, "ping") ) {
894#ifdef ROAR_HAVE_GETTIMEOFDAY
895   if ( ping(&con, atoi(argv[++i])) == -1 ) {
896    fprintf(stderr, "Error: can not ping\n");
897   }
898#else
899    fprintf(stderr, "Error: ping not supported.\n");
900    i++;
901#endif
902
903  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
904   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
905    fprintf(stderr, "Error: can not set mode to standby\n");
906   } else {
907    printf("going into standby\n");
908   }
909  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
910   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
911    fprintf(stderr, "Error: can not set mode to active\n");
912   } else {
913    printf("going into active mode\n");
914   }
915
916  } else if ( !strcmp(k, "exit") ) {
917   if ( roar_exit(&con) == -1 ) {
918    fprintf(stderr, "Error: can not quit server\n");
919   } else {
920    printf("Server quited\n");
921    break;
922   }
923  } else if ( !strcmp(k, "terminate") ) {
924   if ( roar_terminate(&con, 1) == -1 ) {
925    fprintf(stderr, "Error: can not terminate server\n");
926   } else {
927    printf("Server got asked to quited\n");
928    break;
929   }
930
931  } else if ( !strcmp(k, "standbymode") ) {
932   t = roar_get_standby(&con);
933   if ( t == -1 ) {
934    fprintf(stderr, "Error: can not get stanby mode\n");
935   } else if ( t == ROAR_STANDBY_ACTIVE ) {
936    printf("Server is in standby\n");
937   } else if ( t == ROAR_STANDBY_INACTIVE ) {
938    printf("Server is active\n");
939   } else {
940    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
941   }
942
943  } else if ( !strcmp(k, "whoami") ) {
944   printf("My client ID is: %i\n", roar_get_clientid(&con));
945  } else if ( !strcmp(k, "serveroinfo") ) {
946   server_oinfo(&con);
947  } else if ( !strcmp(k, "listclients") ) {
948   list_clients(&con);
949  } else if ( !strcmp(k, "liststreams") ) {
950   list_streams(&con);
951  } else if ( !strcmp(k, "allinfo") ) {
952   server_oinfo(&con);
953   printf("\n");
954   list_clients(&con);
955   printf("\n");
956   list_streams(&con);
957
958  } else if ( !strcmp(k, "kick") ) {
959   k = argv[++i];
960   if ( !strcmp(k, "client") ) {
961    t = ROAR_OT_CLIENT;
962   } else if ( !strcmp(k, "stream") ) {
963    t = ROAR_OT_STREAM;
964   } else if ( !strcmp(k, "sample") ) {
965    t = ROAR_OT_SAMPLE;
966   } else if ( !strcmp(k, "source") ) {
967    t = ROAR_OT_SOURCE;
968   } else {
969    fprintf(stderr, "Error: unknown type: %s\n", k);
970    continue;
971   }
972   //t = atoi(argv[i++]);
973   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
974    fprintf(stderr, "Error: can not kick %s\n", k);
975   } else {
976    printf("%s kicked\n", k);
977   }
978
979  } else if ( !strcmp(k, "newvirtual") ) {
980   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
981    fprintf(stderr, "Error: can not create new virtual stream\n");
982   } else {
983    printf("virtual stream created\n");
984   }
985   i += 6;
986
987  } else if ( !strcmp(k, "volume") ) {
988   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
989    fprintf(stderr, "Error: can not set volume\n");
990   } else {
991    printf("volume changed\n");
992   }
993
994  } else if ( !strcmp(k, "flag") ) {
995   i++;
996   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
997    fprintf(stderr, "Error: can not set flags\n");
998   } else {
999    printf("flags changed\n");
1000   }
1001   i++;
1002  } else if ( !strcmp(k, "unflag") ) {
1003   i++;
1004   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
1005    fprintf(stderr, "Error: can not reset flags\n");
1006   } else {
1007    printf("flags changed\n");
1008   }
1009   i++;
1010  } else if ( !strcmp(k, "metaset") ) {
1011   i++;
1012   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1013    fprintf(stderr, "Error: can not set meta data\n");
1014   } else {
1015    printf("meta data changed\n");
1016   }
1017   i += 3;
1018  } else if ( !strcmp(k, "metaget") ) {
1019   i++;
1020   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1021    fprintf(stderr, "Error: can not get meta data\n");
1022   }
1023   i++;
1024  } else if ( !strcmp(k, "metasave") ) {
1025   i++;
1026   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1027    fprintf(stderr, "Error: can not get meta data\n");
1028   } else {
1029    printf("meta data saved\n");
1030   }
1031   i++;
1032  } else if ( !strcmp(k, "metaload") ) {
1033   i++;
1034   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1035    fprintf(stderr, "Error: can not set meta data\n");
1036   } else {
1037    printf("meta data saved\n");
1038   }
1039   i++;
1040
1041  } else {
1042   fprintf(stderr, "Error: invalid command: %s\n", k);
1043  }
1044
1045 }
1046
1047 roar_disconnect(&con);
1048
1049 return 0;
1050}
1051
1052//ll
Note: See TracBrowser for help on using the repository browser.