source: roaraudio/roarclients/roarctl.c @ 3211:85d85a3413b7

Last change on this file since 3211:85d85a3413b7 was 3211:85d85a3413b7, checked in by phi, 14 years ago

display delay as meter

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