source: roaraudio/roarclients/roarctl.c @ 2739:d186518934cd

Last change on this file since 2739:d186518934cd was 2739:d186518934cd, checked in by phi, 15 years ago

volume change support: stereo -> 1.0

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