source: roaraudio/roarclients/roarctl.c @ 2875:5c14d2001848

Last change on this file since 2875:5c14d2001848 was 2814:40065eb210bc, checked in by phi, 15 years ago

added support for nnode and prethru

File size: 26.2 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 int self_id;
203 struct roar_client self_client;
204 struct roar_client c;
205#ifdef _POSIX_USERS
206 struct group  * grp = NULL;
207 struct passwd * pwd = NULL;
208#endif
209
210 if ( (self_id = roar_get_clientid(con)) != -1 ) {
211  if ( roar_get_client(con, &self_client, self_id) == -1 )
212   self_id = -1;
213 }
214
215 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
216  fprintf(stderr, "Error: can not get client list\n");
217  return;
218 }
219
220 for (i = 0; i < num; i++) {
221  printf("client %i:\n", id[i]);
222  if ( roar_get_client(con, &c, id[i]) == -1 ) {
223   fprintf(stderr, "Error: can not get client info\n");
224   continue;
225  }
226  printf("Client name           : %s\n", c.name);
227
228  if ( roar_nnode_get_socktype(&(c.nnode)) != ROAR_SOCKET_TYPE_UNKNOWN ) {
229   if ( roar_nnode_to_str(&(c.nnode), tmp, 80) == 0 ) {
230    printf("Client network node   : %s\n", tmp);
231   }
232  }
233
234  if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
235   printf("Client PID            : %i(%s)\n", c.pid, proc_name(c.pid));
236  } else {
237   printf("Client PID            : %i\n", c.pid);
238  }
239  if ( c.uid != -1 ) {
240#ifdef _POSIX_USERS
241   if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
242    pwd = getpwuid(c.uid);
243    grp = getgrgid(c.gid);
244    printf("Client UID/GID        : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?");
245   } else {
246#else
247   if ( 1 ) {
248#endif
249    printf("Client UID/GID        : %i/%i\n", c.uid, c.gid);
250   }
251  }
252
253  if ( g_verbose && c.proto != ROAR_PROTO_NONE ) {
254   printf("Client Protocol       : %s\n", roar_proto2str(c.proto));
255  }
256
257  if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) {
258   if ( c.byteorder == ROAR_BYTEORDER_NETWORK ) {
259    strcpy(tmp, " (network byteorder");
260   } else {
261    *tmp = 0;
262   }
263
264   if ( c.byteorder == ROAR_BYTEORDER_NATIVE ) {
265    if ( *tmp ) {
266     strcat(tmp, ", native");
267    } else {
268     strcpy(tmp, " (native");
269    }
270   }
271
272   if ( *tmp )
273    strcat(tmp, ")");
274
275   printf("Client Byteorder      : %s%s\n", roar_byteorder2str(c.byteorder), tmp);
276  }
277
278  if ( c.execed != -1 )
279   printf("Execed stream         : %i\n", c.execed);
280
281  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
282   if ( c.streams[h] != -1 )
283    printf("stream                : %i\n", c.streams[h]);
284 }
285
286}
287
288void list_streams (struct roar_connection * con) {
289 int i;
290 int num;
291 int id[ROAR_STREAMS_MAX];
292 struct roar_stream s;
293 struct roar_stream_info info;
294 char flags[1024];
295 char name[1024];
296 char * infotext;
297
298
299 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
300  fprintf(stderr, "Error: can not get stream list\n");
301  return;
302 }
303
304 for (i = 0; i < num; i++) {
305  printf("stream %i:\n", id[i]);
306  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
307   fprintf(stderr, "Error: can not get stream info\n");
308   continue;
309  }
310  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
311
312  if ( roar_stream_get_name(con, &s, name, 1024) == 0 )
313   printf("Stream name           : %s\n", name);
314
315  if ( s.pos_rel_id == -1 ) {
316   printf("Relativ position id   : none (stream not synchronized)\n");
317  } else if ( s.pos_rel_id == id[i] ) {
318   printf("Relativ position id   : %i (self synchronized)\n", s.pos_rel_id);
319  } else {
320   printf("Relativ position id   : %i (synchronized)\n", s.pos_rel_id);
321  }
322  if ( g_verbose > 1 ) {
323   if ( s.info.rate && s.info.channels ) {
324    printf("Position              : %lu S (%.3fs)\n", (unsigned long int) s.pos,
325                                    (float)s.pos/(s.info.rate*s.info.channels));
326   } else {
327    printf("Position              : %lu S\n", (unsigned long int) s.pos);
328   }
329  }
330
331  switch (s.dir) {
332   case ROAR_DIR_MIDI_IN:
333   case ROAR_DIR_MIDI_OUT:
334     infotext = " ticks/s";
335    break;
336   case ROAR_DIR_LIGHT_IN:
337   case ROAR_DIR_LIGHT_OUT:
338     infotext = " updates/s";
339    break;
340   default:
341     infotext = " Hz";
342  }
343  if ( s.info.rate )
344   printf("Stream sample rate    : %i%s\n", s.info.rate, infotext);
345  if ( s.info.bits )
346   printf("Stream bits           : %i\n", s.info.bits);
347  if ( s.info.channels )
348  printf("Stream channels       : %i\n", s.info.channels);
349
350  printf("Stream codec          : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
351                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
352  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
353   if ( info.codec != s.info.codec ) {
354    printf("Streamed codec        : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
355                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
356   }
357
358   if ( g_verbose ) {
359    if ( info.block_size )
360     printf("Stream block size     : %i Byte\n", info.block_size);
361
362    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
363    if ( g_verbose > 1 )
364     printf("Stream delay          : %ims\n",   (int)info.delay/1000);
365
366    if ( g_verbose > 1 )
367     printf("Stream state          : %s\n",   roar_streamstate2str(info.state));
368
369    *flags = 0;
370    if ( info.flags & ROAR_FLAG_PRIMARY )
371     strcat(flags, "primary ");
372    if ( info.flags & ROAR_FLAG_SYNC )
373     strcat(flags, "sync ");
374    if ( info.flags & ROAR_FLAG_OUTPUT )
375     strcat(flags, "output ");
376    if ( info.flags & ROAR_FLAG_SOURCE )
377     strcat(flags, "source ");
378    if ( info.flags & ROAR_FLAG_META )
379     strcat(flags, "meta ");
380    if ( info.flags & ROAR_FLAG_AUTOCONF )
381     strcat(flags, "autoconf ");
382    if ( info.flags & ROAR_FLAG_CLEANMETA )
383     strcat(flags, "cleanmeta ");
384    if ( info.flags & ROAR_FLAG_HWMIXER )
385     strcat(flags, "hwmixer ");
386    if ( info.flags & ROAR_FLAG_PAUSE )
387     strcat(flags, "pause ");
388    if ( info.flags & ROAR_FLAG_MUTE )
389     strcat(flags, "mute ");
390    if ( info.flags & ROAR_FLAG_MMAP )
391     strcat(flags, "mmap ");
392    if ( info.flags & ROAR_FLAG_ANTIECHO )
393     strcat(flags, "antiecho ");
394    if ( info.flags & ROAR_FLAG_VIRTUAL )
395     strcat(flags, "virtual ");
396    if ( info.flags & ROAR_FLAG_RECSOURCE )
397     strcat(flags, "recsource ");
398    if ( info.flags & ROAR_FLAG_PASSMIXER )
399     strcat(flags, "passmixer ");
400    if ( info.flags & ROAR_FLAG_PRETHRU )
401     strcat(flags, "prethru ");
402
403    printf("Flags                 : %s\n", flags);
404   }
405  }
406
407  if ( s.dir != ROAR_DIR_THRU ) {
408   display_mixer(con, id[i]);
409   show_meta_all(con, id[i]);
410  }
411 }
412
413}
414
415int display_mixer (struct roar_connection * con, int stream) {
416 int channels;
417 struct roar_mixer_settings mixer;
418 int i;
419
420 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
421  fprintf(stderr, "Error: can not get stream mixer info\n");
422  return -1;
423 }
424
425 for (i = 0; i < channels; i++)
426  printf("Mixer volume chan %2i  : %i (%.2f%%)\n", i, mixer.mixer[i], (float)mixer.mixer[i]/655.35);
427
428 return 0;
429}
430
431int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
432 int chans = 0;
433 int id;
434 int i;
435 int len;
436 int old_chans;
437 int vol_l, vol_r, vol_mono;
438 char * k;
439 struct roar_mixer_settings mixer;
440 struct roar_mixer_settings old_mixer;
441
442 if (*cur + 2 > max)
443  return -1;
444
445 id = atoi(arg[++(*cur)]);
446
447 k = arg[++(*cur)];
448
449 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
450  fprintf(stderr, "Error: can not get stream mixer info\n");
451  return -1;
452 }
453
454
455// TODO: clean up code here as the % vs. abs code is very duplicate...
456
457 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
458  chans = 1;
459
460  if ( *cur + 1 > max )
461   return -1;
462
463  k   = arg[++(*cur)];
464  len = strlen(k);
465
466  if ( k[len - 1] == '%' ) {
467   k[len - 1] = 0;
468   vol_mono = (atof(k)*65535)/100;
469  } else {
470   vol_mono = atoi(k);
471  }
472
473  for (i = 0; i < old_chans; i++)
474   mixer.mixer[i] = vol_mono;
475
476  chans = old_chans;
477
478 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
479  chans = old_chans;
480
481  if ( *cur + 2 > max )
482   return -1;
483
484  k   = arg[++(*cur)];
485  len = strlen(k);
486
487  if ( k[len - 1] == '%' ) {
488   k[len - 1] = 0;
489   vol_l = (atof(k)*65535)/100;
490  } else {
491   vol_l = atoi(k);
492  }
493
494  k   = arg[++(*cur)];
495  len = strlen(k);
496
497  if ( k[len - 1] == '%' ) {
498   k[len - 1] = 0;
499   vol_r = (atof(k)*65535)/100;
500  } else {
501   vol_r = atoi(k);
502  }
503
504  vol_mono = (vol_l + vol_r) / 2;
505
506  mixer.mixer[0] = vol_l;
507  mixer.mixer[1] = vol_r;
508
509  switch (chans) {
510   case 1:
511     mixer.mixer[0] = vol_mono;
512    break;
513//   case 2: classic stereo...
514   case 3:
515     mixer.mixer[2] = vol_mono;
516    break;
517   case 4:
518     mixer.mixer[2] = vol_l;
519     mixer.mixer[3] = vol_r;
520    break;
521   case 5:
522     mixer.mixer[2] = vol_mono;
523     mixer.mixer[3] = vol_l;
524     mixer.mixer[4] = vol_r;
525    break;
526   case 6:
527     mixer.mixer[2] = vol_mono;
528     mixer.mixer[3] = vol_mono;
529     mixer.mixer[4] = vol_l;
530     mixer.mixer[5] = vol_r;
531    break;
532   default:
533     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
534     return -1;
535    break;
536  }
537
538 } else {
539  if ( strcmp(k, "mono") == 0 ) {
540   chans = 1;
541  } else if ( strcmp(k, "stereo") == 0 ) {
542   chans = 2;
543  } else {
544   chans = atoi(k);
545  }
546
547//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
548
549  if ( *cur + chans > max )
550   return -1;
551
552  for (i = 0; i < chans; i++) {
553   k   = arg[++(*cur)];
554   len = strlen(k);
555
556   if ( k[len - 1] == '%' ) {
557    k[len - 1] = 0;
558    mixer.mixer[i] = (atof(k)*(int)65535)/100;
559   } else {
560    mixer.mixer[i] = atoi(k);
561   }
562  }
563 }
564
565 mixer.scale = 65535;
566
567 return roar_set_vol(con, id, &mixer, chans);
568}
569
570
571int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
572 struct roar_stream s;
573 int dir    = roar_str2dir(d_s);
574 int parent = atoi(p_s);
575
576 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
577
578 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
579  return -1;
580
581 return roar_simple_connect_virtual(con, &s, parent, dir);
582}
583
584int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
585 struct roar_meta   meta;
586 struct roar_stream s;
587 int mode_i = ROAR_META_MODE_SET;
588
589 memset(&s, 0, sizeof(s));
590
591 s.id = id;
592
593// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
594
595 if ( strcmp(mode, "add") == 0 ) {
596  mode_i = ROAR_META_MODE_ADD;
597 }
598
599 meta.type   = roar_meta_inttype(type);
600 meta.value  = val;
601 meta.key[0] = 0;
602
603 if ( meta.type == -1 ) {
604  fprintf(stderr, "Error: unknown type: %s\n", type);
605  return -1;
606 }
607
608// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
609
610 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
611  return -1;
612
613 meta.type  = ROAR_META_TYPE_NONE;
614 meta.value = NULL;
615
616 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
617}
618
619int load_meta (struct roar_connection * con, int id, char * file) {
620 struct roar_meta   meta;
621 struct roar_stream s;
622 int mode_i = ROAR_META_MODE_SET;
623 FILE * in;
624 char lion[1024];
625 char * v;
626
627 memset(&s, 0, sizeof(s));
628
629 s.id = id;
630
631 if ( (in = fopen(file, "r")) == NULL )
632  return -1;
633
634 while (fgets(lion, 1024, in) != NULL) {
635  if ( (v = strtok(lion, "\r\n")) != NULL )
636   if ( (v = strtok(NULL, "\r\n")) != NULL )
637    *(v-1) = 0;
638
639  if ( (v = strstr(lion, "=")) == NULL ) {
640   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
641   continue;
642  }
643
644  *v++ = 0;
645
646  meta.type   = roar_meta_inttype(lion);
647  meta.value  = v;
648  meta.key[0] = 0;
649
650  if ( meta.type == -1 ) {
651   fprintf(stderr, "Error: unknown type: %s\n", lion);
652   continue;
653  }
654
655  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
656   return -1;
657 }
658
659 fclose(in);
660
661 meta.type  = ROAR_META_TYPE_NONE;
662 meta.value = NULL;
663
664 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
665}
666
667int show_meta_type (struct roar_connection * con, int id, char * type) {
668 struct roar_meta   meta;
669 struct roar_stream s;
670
671 memset(&s, 0, sizeof(s));
672
673 s.id = id;
674
675 meta.type  = roar_meta_inttype(type);
676
677 if ( meta.type == -1 ) {
678  fprintf(stderr, "Error: unknown type: %s\n", type);
679  return -1;
680 }
681
682 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
683  return -1;
684
685 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
686
687 roar_meta_free(&meta);
688
689 return 0;
690}
691
692int show_meta_all (struct roar_connection * con, int id) {
693 struct roar_stream s;
694 int types[ROAR_META_MAX_PER_STREAM];
695 int i;
696 int len;
697
698 memset(&s, 0, sizeof(s));
699
700 s.id = id;
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  show_meta_type(con, id, roar_meta_strtype(types[i]));
707
708 return 0;
709}
710
711int save_meta (struct roar_connection * con, int id, char * file) {
712 struct roar_stream s;
713 struct roar_meta   meta;
714 int types[ROAR_META_MAX_PER_STREAM];
715 int i;
716 int len;
717 FILE * out;
718
719 memset(&s, 0, sizeof(s));
720
721 s.id = id;
722
723 if ( (out = fopen(file, "w")) == NULL )
724  return -1;
725
726 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
727  return -1;
728
729 for (i = 0; i < len; i++) {
730/*
731  show_meta_type(con, id, roar_meta_strtype(types[i]));
732*/
733  meta.type  = types[i];
734
735  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
736   continue;
737
738//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
739
740  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
741
742  roar_meta_free(&meta);
743 }
744
745 fclose(out);
746
747 return 0;
748}
749
750int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
751 int f = ROAR_FLAG_NONE;
752 char * c;
753 struct roar_stream s[1];
754
755 memset(s, 0, sizeof(struct roar_stream));
756
757 s->id = id;
758
759 c = strtok(flags, ",");
760 while (c != NULL) {
761  if ( !strcmp(c, "meta") ) {
762   f |= ROAR_FLAG_META;
763  } else if ( !strcmp(c, "primary") ) {
764   f |= ROAR_FLAG_PRIMARY;
765  } else if ( !strcmp(c, "sync") ) {
766   f |= ROAR_FLAG_SYNC;
767  } else if ( !strcmp(c, "cleanmeta") ) {
768   f |= ROAR_FLAG_CLEANMETA;
769  } else if ( !strcmp(c, "hwmixer") ) {
770   f |= ROAR_FLAG_HWMIXER;
771  } else if ( !strcmp(c, "pause") ) {
772   f |= ROAR_FLAG_PAUSE;
773  } else if ( !strcmp(c, "mute") ) {
774   f |= ROAR_FLAG_MUTE;
775  } else if ( !strcmp(c, "mmap") ) {
776   f |= ROAR_FLAG_MMAP;
777  } else if ( !strcmp(c, "antiecho") ) {
778   f |= ROAR_FLAG_ANTIECHO;
779  } else if ( !strcmp(c, "recsource") ) {
780   f |= ROAR_FLAG_RECSOURCE;
781  } else if ( !strcmp(c, "passmixer") ) {
782   f |= ROAR_FLAG_PASSMIXER;
783  } else if ( !strcmp(c, "virtual") ) {
784   f |= ROAR_FLAG_VIRTUAL;
785  } else if ( !strcmp(c, "prethru") ) {
786   f |= ROAR_FLAG_PRETHRU;
787  } else {
788   fprintf(stderr, "Error: unknown flag: %s\n", c);
789   return -1;
790  }
791
792  c = strtok(NULL, ",");
793 }
794
795 return roar_stream_set_flags(con, s, f, reset);
796}
797
798int main (int argc, char * argv[]) {
799 struct roar_connection con;
800 char * server   = NULL;
801 char * k = NULL;
802 int    i;
803 int    t = 0;
804
805 for (i = 1; i < argc; i++) {
806  k = argv[i];
807
808  if ( strcmp(k, "--server") == 0 ) {
809   server = argv[++i];
810  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
811   g_verbose++;
812  } else if ( strcmp(k, "--help") == 0 ) {
813   usage();
814   return 0;
815  } else if ( *k == '-' ) {
816   fprintf(stderr, "Error: unknown argument: %s\n", k);
817   usage();
818   return 1;
819  } else {
820   break;
821  }
822 }
823
824 // connect
825
826 if ( roar_connect(&con, server) == -1 ) {
827  fprintf(stderr, "Error: Can not connect to server\n");
828  return 1;
829 }
830
831 if ( roar_identify(&con, "roarctl") == -1 ) {
832  fprintf(stderr, "Error: Can not identify to server\n");
833  return 1;
834 }
835
836 if ( i == argc ) {
837  fprintf(stderr, "Error: No Commands given\n");
838  return 0; // this is not a fatal error...
839 }
840
841 for (; i < argc; i++) {
842  k = argv[i];
843  // cmd is in k
844
845  printf("--- [ %s ] ---\n", k);
846
847  if ( !strcmp(k, "help") ) {
848   usage();
849
850  } else if ( !strcmp(k, "sleep") ) {
851   sleep(atoi(argv[++i]));
852
853  } else if ( !strcmp(k, "ping") ) {
854#ifdef ROAR_HAVE_GETTIMEOFDAY
855   if ( ping(&con, atoi(argv[++i])) == -1 ) {
856    fprintf(stderr, "Error: can not ping\n");
857   }
858#else
859    fprintf(stderr, "Error: ping not supported.\n");
860    i++;
861#endif
862
863  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
864   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
865    fprintf(stderr, "Error: can not set mode to standby\n");
866   } else {
867    printf("going into standby\n");
868   }
869  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
870   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
871    fprintf(stderr, "Error: can not set mode to active\n");
872   } else {
873    printf("going into active mode\n");
874   }
875
876  } else if ( !strcmp(k, "exit") ) {
877   if ( roar_exit(&con) == -1 ) {
878    fprintf(stderr, "Error: can not quit server\n");
879   } else {
880    printf("Server quited\n");
881    break;
882   }
883  } else if ( !strcmp(k, "terminate") ) {
884   if ( roar_terminate(&con, 1) == -1 ) {
885    fprintf(stderr, "Error: can not terminate server\n");
886   } else {
887    printf("Server got asked to quited\n");
888    break;
889   }
890
891  } else if ( !strcmp(k, "standbymode") ) {
892   t = roar_get_standby(&con);
893   if ( t == -1 ) {
894    fprintf(stderr, "Error: can not get stanby mode\n");
895   } else if ( t == ROAR_STANDBY_ACTIVE ) {
896    printf("Server is in standby\n");
897   } else if ( t == ROAR_STANDBY_INACTIVE ) {
898    printf("Server is active\n");
899   } else {
900    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
901   }
902
903  } else if ( !strcmp(k, "whoami") ) {
904   printf("My client ID is: %i\n", roar_get_clientid(&con));
905  } else if ( !strcmp(k, "serveroinfo") ) {
906   server_oinfo(&con);
907  } else if ( !strcmp(k, "listclients") ) {
908   list_clients(&con);
909  } else if ( !strcmp(k, "liststreams") ) {
910   list_streams(&con);
911  } else if ( !strcmp(k, "allinfo") ) {
912   server_oinfo(&con);
913   printf("\n");
914   list_clients(&con);
915   printf("\n");
916   list_streams(&con);
917
918  } else if ( !strcmp(k, "kick") ) {
919   k = argv[++i];
920   if ( !strcmp(k, "client") ) {
921    t = ROAR_OT_CLIENT;
922   } else if ( !strcmp(k, "stream") ) {
923    t = ROAR_OT_STREAM;
924   } else if ( !strcmp(k, "sample") ) {
925    t = ROAR_OT_SAMPLE;
926   } else if ( !strcmp(k, "source") ) {
927    t = ROAR_OT_SOURCE;
928   } else {
929    fprintf(stderr, "Error: unknown type: %s\n", k);
930    continue;
931   }
932   //t = atoi(argv[i++]);
933   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
934    fprintf(stderr, "Error: can not kick %s\n", k);
935   } else {
936    printf("%s kicked\n", k);
937   }
938
939  } else if ( !strcmp(k, "newvirtual") ) {
940   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
941    fprintf(stderr, "Error: can not create new virtual stream\n");
942   } else {
943    printf("virtual stream created\n");
944   }
945   i += 6;
946
947  } else if ( !strcmp(k, "volume") ) {
948   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
949    fprintf(stderr, "Error: can not set volume\n");
950   } else {
951    printf("volume changed\n");
952   }
953
954  } else if ( !strcmp(k, "flag") ) {
955   i++;
956   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
957    fprintf(stderr, "Error: can not set flags\n");
958   } else {
959    printf("flags changed\n");
960   }
961   i++;
962  } else if ( !strcmp(k, "unflag") ) {
963   i++;
964   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
965    fprintf(stderr, "Error: can not reset flags\n");
966   } else {
967    printf("flags changed\n");
968   }
969   i++;
970  } else if ( !strcmp(k, "metaset") ) {
971   i++;
972   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
973    fprintf(stderr, "Error: can not set meta data\n");
974   } else {
975    printf("meta data changed\n");
976   }
977   i += 3;
978  } else if ( !strcmp(k, "metaget") ) {
979   i++;
980   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
981    fprintf(stderr, "Error: can not get meta data\n");
982   }
983   i++;
984  } else if ( !strcmp(k, "metasave") ) {
985   i++;
986   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
987    fprintf(stderr, "Error: can not get meta data\n");
988   } else {
989    printf("meta data saved\n");
990   }
991   i++;
992  } else if ( !strcmp(k, "metaload") ) {
993   i++;
994   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
995    fprintf(stderr, "Error: can not set meta data\n");
996   } else {
997    printf("meta data saved\n");
998   }
999   i++;
1000
1001  } else {
1002   fprintf(stderr, "Error: invalid command: %s\n", k);
1003  }
1004
1005 }
1006
1007 roar_disconnect(&con);
1008
1009 return 0;
1010}
1011
1012//ll
Note: See TracBrowser for help on using the repository browser.