source: roaraudio/roarclients/roarctl.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

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, 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 int channels;
432 struct roar_mixer_settings mixer;
433 int i;
434
435 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
436  fprintf(stderr, "Error: can not get stream mixer info\n");
437  return -1;
438 }
439
440#ifdef ROAR_HAVE_LIBM
441#define _DB ", %.2fdB"
442#else
443#define _DB ""
444#endif
445
446 for (i = 0; i < channels; i++)
447  printf("Mixer volume chan %2i  : %i (%.2f%%" _DB ")\n", i, mixer.mixer[i],
448                           (float)mixer.mixer[i]/655.35f
449#ifdef ROAR_HAVE_LIBM
450                          , 20*log10f((float)mixer.mixer[i]/65535.f)
451#endif
452        );
453
454 return 0;
455}
456
457static unsigned int set_mixer_parse_volume (char * k, int len) {
458 switch (k[len - 1]) {
459  case '%':
460    k[len - 1] = 0;
461    return (atof(k)*65535.f)/100;
462   break;
463#ifdef ROAR_HAVE_LIBM
464  case 'b':
465  case 'B':
466    // TODO: can we hanle the error better?
467    if ( len < 2 )
468     return 0;
469
470    k[len - 2] = 0;
471    return powf(10, atof(k)/20.f)*65535.f;
472   break;
473#endif
474 }
475
476 return atoi(k);
477}
478
479int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
480 int chans = 0;
481 int id;
482 int i;
483 int len;
484 int old_chans;
485 int vol_l, vol_r, vol_mono;
486 char * k;
487 struct roar_mixer_settings mixer;
488 struct roar_mixer_settings old_mixer;
489
490 if (*cur + 2 > max)
491  return -1;
492
493 id = atoi(arg[++(*cur)]);
494
495 k = arg[++(*cur)];
496
497 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
498  fprintf(stderr, "Error: can not get stream mixer info\n");
499  return -1;
500 }
501
502
503// TODO: clean up code here as the % vs. abs code is very duplicate...
504
505 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
506  chans = 1;
507
508  if ( *cur + 1 > max )
509   return -1;
510
511  k   = arg[++(*cur)];
512  len = strlen(k);
513
514  vol_mono = set_mixer_parse_volume(k, len);
515
516  for (i = 0; i < old_chans; i++)
517   mixer.mixer[i] = vol_mono;
518
519  chans = old_chans;
520
521 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
522  chans = old_chans;
523
524  if ( *cur + 2 > max )
525   return -1;
526
527  k   = arg[++(*cur)];
528  len = strlen(k);
529
530  vol_l = set_mixer_parse_volume(k, len);
531
532  k   = arg[++(*cur)];
533  len = strlen(k);
534
535  vol_r = set_mixer_parse_volume(k, len);
536
537  vol_mono = (vol_l + vol_r) / 2;
538
539  mixer.mixer[0] = vol_l;
540  mixer.mixer[1] = vol_r;
541
542  switch (chans) {
543   case 1:
544     mixer.mixer[0] = vol_mono;
545    break;
546//   case 2: classic stereo...
547   case 3:
548     mixer.mixer[2] = vol_mono;
549    break;
550   case 4:
551     mixer.mixer[2] = vol_l;
552     mixer.mixer[3] = vol_r;
553    break;
554   case 5:
555     mixer.mixer[2] = vol_mono;
556     mixer.mixer[3] = vol_l;
557     mixer.mixer[4] = vol_r;
558    break;
559   case 6:
560     mixer.mixer[2] = vol_mono;
561     mixer.mixer[3] = vol_mono;
562     mixer.mixer[4] = vol_l;
563     mixer.mixer[5] = vol_r;
564    break;
565   default:
566     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
567     return -1;
568    break;
569  }
570
571 } else {
572  if ( strcmp(k, "mono") == 0 ) {
573   chans = 1;
574  } else if ( strcmp(k, "stereo") == 0 ) {
575   chans = 2;
576  } else {
577   chans = atoi(k);
578  }
579
580//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
581
582  if ( *cur + chans > max )
583   return -1;
584
585  for (i = 0; i < chans; i++) {
586   k   = arg[++(*cur)];
587   len = strlen(k);
588
589   mixer.mixer[i] = set_mixer_parse_volume(k, len);
590  }
591 }
592
593 mixer.scale = 65535;
594
595 return roar_set_vol(con, id, &mixer, chans);
596}
597
598
599int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
600 struct roar_stream s;
601 int dir    = roar_str2dir(d_s);
602 int parent = atoi(p_s);
603
604 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
605
606 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
607  return -1;
608
609 return roar_simple_connect_virtual(con, &s, parent, dir);
610}
611
612int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
613 struct roar_meta   meta;
614 struct roar_stream s;
615 int mode_i = ROAR_META_MODE_SET;
616
617 if ( roar_stream_new_by_id(&s, id) == -1 )
618  return -1;
619
620// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
621
622 if ( strcmp(mode, "add") == 0 ) {
623  mode_i = ROAR_META_MODE_ADD;
624 }
625
626 meta.type   = roar_meta_inttype(type);
627 meta.value  = val;
628 meta.key[0] = 0;
629
630 if ( meta.type == -1 ) {
631  fprintf(stderr, "Error: unknown type: %s\n", type);
632  return -1;
633 }
634
635// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
636
637 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
638  return -1;
639
640 meta.type  = ROAR_META_TYPE_NONE;
641 meta.value = NULL;
642
643 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
644}
645
646int load_meta (struct roar_connection * con, int id, char * file) {
647 struct roar_meta   meta;
648 struct roar_stream s;
649 int mode_i = ROAR_META_MODE_SET;
650 FILE * in;
651 char lion[1024];
652 char * v;
653
654 if ( roar_stream_new_by_id(&s, id) == -1 )
655  return -1;
656
657 if ( (in = fopen(file, "r")) == NULL )
658  return -1;
659
660 while (fgets(lion, 1024, in) != NULL) {
661  if ( (v = strtok(lion, "\r\n")) != NULL )
662   if ( (v = strtok(NULL, "\r\n")) != NULL )
663    *(v-1) = 0;
664
665  if ( (v = strstr(lion, "=")) == NULL ) {
666   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
667   continue;
668  }
669
670  *v++ = 0;
671
672  meta.type   = roar_meta_inttype(lion);
673  meta.value  = v;
674  meta.key[0] = 0;
675
676  if ( meta.type == -1 ) {
677   fprintf(stderr, "Error: unknown type: %s\n", lion);
678   continue;
679  }
680
681  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
682   return -1;
683 }
684
685 fclose(in);
686
687 meta.type  = ROAR_META_TYPE_NONE;
688 meta.value = NULL;
689
690 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
691}
692
693int show_meta_type (struct roar_connection * con, int id, char * type) {
694 struct roar_meta   meta;
695 struct roar_stream s;
696
697 if ( roar_stream_new_by_id(&s, id) == -1 )
698  return -1;
699
700 meta.type  = roar_meta_inttype(type);
701
702 if ( meta.type == -1 ) {
703  fprintf(stderr, "Error: unknown type: %s\n", type);
704  return -1;
705 }
706
707 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
708  return -1;
709
710 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
711
712 roar_meta_free(&meta);
713
714 return 0;
715}
716
717int show_meta_all (struct roar_connection * con, int id) {
718 struct roar_stream s;
719 int types[ROAR_META_MAX_PER_STREAM];
720 int i;
721 int len;
722
723 if ( roar_stream_new_by_id(&s, id) == -1 )
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  show_meta_type(con, id, roar_meta_strtype(types[i]));
731
732 return 0;
733}
734
735int save_meta (struct roar_connection * con, int id, char * file) {
736 struct roar_stream s;
737 struct roar_meta   meta;
738 int types[ROAR_META_MAX_PER_STREAM];
739 int i;
740 int len;
741 FILE * out;
742
743 if ( roar_stream_new_by_id(&s, id) == -1 )
744  return -1;
745
746 if ( (out = fopen(file, "w")) == NULL )
747  return -1;
748
749 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
750  return -1;
751
752 for (i = 0; i < len; i++) {
753/*
754  show_meta_type(con, id, roar_meta_strtype(types[i]));
755*/
756  meta.type  = types[i];
757
758  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
759   continue;
760
761//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
762
763  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
764
765  roar_meta_free(&meta);
766 }
767
768 fclose(out);
769
770 return 0;
771}
772
773int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
774 int f = ROAR_FLAG_NONE;
775 char * c;
776 struct roar_stream s[1];
777
778 if ( roar_stream_new_by_id(s, id) == -1 )
779  return -1;
780
781 c = strtok(flags, ",");
782 while (c != NULL) {
783  if ( !strcmp(c, "meta") ) {
784   f |= ROAR_FLAG_META;
785  } else if ( !strcmp(c, "primary") ) {
786   f |= ROAR_FLAG_PRIMARY;
787  } else if ( !strcmp(c, "sync") ) {
788   f |= ROAR_FLAG_SYNC;
789  } else if ( !strcmp(c, "cleanmeta") ) {
790   f |= ROAR_FLAG_CLEANMETA;
791  } else if ( !strcmp(c, "hwmixer") ) {
792   f |= ROAR_FLAG_HWMIXER;
793  } else if ( !strcmp(c, "pause") ) {
794   f |= ROAR_FLAG_PAUSE;
795  } else if ( !strcmp(c, "mute") ) {
796   f |= ROAR_FLAG_MUTE;
797  } else if ( !strcmp(c, "mmap") ) {
798   f |= ROAR_FLAG_MMAP;
799  } else if ( !strcmp(c, "antiecho") ) {
800   f |= ROAR_FLAG_ANTIECHO;
801  } else if ( !strcmp(c, "recsource") ) {
802   f |= ROAR_FLAG_RECSOURCE;
803  } else if ( !strcmp(c, "passmixer") ) {
804   f |= ROAR_FLAG_PASSMIXER;
805  } else if ( !strcmp(c, "virtual") ) {
806   f |= ROAR_FLAG_VIRTUAL;
807  } else if ( !strcmp(c, "prethru") ) {
808   f |= ROAR_FLAG_PRETHRU;
809  } else if ( !strcmp(c, "immutable") ) {
810   f |= ROAR_FLAG_IMMUTABLE;
811  } else if ( !strcmp(c, "enhance") ) {
812   f |= ROAR_FLAG_ENHANCE;
813  } else {
814   fprintf(stderr, "Error: unknown flag: %s\n", c);
815   return -1;
816  }
817
818  c = strtok(NULL, ",");
819 }
820
821 return roar_stream_set_flags(con, s, f, reset);
822}
823
824int main (int argc, char * argv[]) {
825 struct roar_connection con;
826 char * server   = NULL;
827 char * k = NULL;
828 int    i;
829 int    t = 0;
830
831 for (i = 1; i < argc; i++) {
832  k = argv[i];
833
834  if ( strcmp(k, "--server") == 0 ) {
835   server = argv[++i];
836  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
837   g_verbose++;
838  } else if ( strcmp(k, "--help") == 0 ) {
839   usage();
840   return 0;
841  } else if ( *k == '-' ) {
842   fprintf(stderr, "Error: unknown argument: %s\n", k);
843   usage();
844   return 1;
845  } else {
846   break;
847  }
848 }
849
850 // connect
851
852 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
853  fprintf(stderr, "Error: Can not connect to server\n");
854  return 1;
855 }
856
857 if ( i == argc ) {
858  fprintf(stderr, "Error: No Commands given\n");
859  return 0; // this is not a fatal error...
860 }
861
862 for (; i < argc; i++) {
863  k = argv[i];
864  // cmd is in k
865
866  printf("--- [ %s ] ---\n", k);
867
868  if ( !strcmp(k, "help") ) {
869   usage();
870
871  } else if ( !strcmp(k, "sleep") ) {
872   sleep(atoi(argv[++i]));
873
874  } else if ( !strcmp(k, "ping") ) {
875#ifdef ROAR_HAVE_GETTIMEOFDAY
876   if ( ping(&con, atoi(argv[++i])) == -1 ) {
877    fprintf(stderr, "Error: can not ping\n");
878   }
879#else
880    fprintf(stderr, "Error: ping not supported.\n");
881    i++;
882#endif
883
884  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
885   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
886    fprintf(stderr, "Error: can not set mode to standby\n");
887   } else {
888    printf("going into standby\n");
889   }
890  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
891   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
892    fprintf(stderr, "Error: can not set mode to active\n");
893   } else {
894    printf("going into active mode\n");
895   }
896
897  } else if ( !strcmp(k, "exit") ) {
898   if ( roar_exit(&con) == -1 ) {
899    fprintf(stderr, "Error: can not quit server\n");
900   } else {
901    printf("Server quited\n");
902    break;
903   }
904  } else if ( !strcmp(k, "terminate") ) {
905   if ( roar_terminate(&con, 1) == -1 ) {
906    fprintf(stderr, "Error: can not terminate server\n");
907   } else {
908    printf("Server got asked to quited\n");
909    break;
910   }
911
912  } else if ( !strcmp(k, "standbymode") ) {
913   t = roar_get_standby(&con);
914   if ( t == -1 ) {
915    fprintf(stderr, "Error: can not get stanby mode\n");
916   } else if ( t == ROAR_STANDBY_ACTIVE ) {
917    printf("Server is in standby\n");
918   } else if ( t == ROAR_STANDBY_INACTIVE ) {
919    printf("Server is active\n");
920   } else {
921    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
922   }
923
924  } else if ( !strcmp(k, "whoami") ) {
925   printf("My client ID is: %i\n", roar_get_clientid(&con));
926  } else if ( !strcmp(k, "serveroinfo") ) {
927   server_oinfo(&con);
928  } else if ( !strcmp(k, "listclients") ) {
929   list_clients(&con);
930  } else if ( !strcmp(k, "liststreams") ) {
931   list_streams(&con);
932  } else if ( !strcmp(k, "allinfo") ) {
933   server_oinfo(&con);
934   printf("\n");
935   list_clients(&con);
936   printf("\n");
937   list_streams(&con);
938
939  } else if ( !strcmp(k, "kick") ) {
940   k = argv[++i];
941   if ( !strcmp(k, "client") ) {
942    t = ROAR_OT_CLIENT;
943   } else if ( !strcmp(k, "stream") ) {
944    t = ROAR_OT_STREAM;
945   } else if ( !strcmp(k, "sample") ) {
946    t = ROAR_OT_SAMPLE;
947   } else if ( !strcmp(k, "source") ) {
948    t = ROAR_OT_SOURCE;
949   } else {
950    fprintf(stderr, "Error: unknown type: %s\n", k);
951    continue;
952   }
953   //t = atoi(argv[i++]);
954   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
955    fprintf(stderr, "Error: can not kick %s\n", k);
956   } else {
957    printf("%s kicked\n", k);
958   }
959
960  } else if ( !strcmp(k, "newvirtual") ) {
961   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
962    fprintf(stderr, "Error: can not create new virtual stream\n");
963   } else {
964    printf("virtual stream created\n");
965   }
966   i += 6;
967
968  } else if ( !strcmp(k, "volume") ) {
969   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
970    fprintf(stderr, "Error: can not set volume\n");
971   } else {
972    printf("volume changed\n");
973   }
974
975  } else if ( !strcmp(k, "flag") ) {
976   i++;
977   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
978    fprintf(stderr, "Error: can not set flags\n");
979   } else {
980    printf("flags changed\n");
981   }
982   i++;
983  } else if ( !strcmp(k, "unflag") ) {
984   i++;
985   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
986    fprintf(stderr, "Error: can not reset flags\n");
987   } else {
988    printf("flags changed\n");
989   }
990   i++;
991  } else if ( !strcmp(k, "metaset") ) {
992   i++;
993   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
994    fprintf(stderr, "Error: can not set meta data\n");
995   } else {
996    printf("meta data changed\n");
997   }
998   i += 3;
999  } else if ( !strcmp(k, "metaget") ) {
1000   i++;
1001   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1002    fprintf(stderr, "Error: can not get meta data\n");
1003   }
1004   i++;
1005  } else if ( !strcmp(k, "metasave") ) {
1006   i++;
1007   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1008    fprintf(stderr, "Error: can not get meta data\n");
1009   } else {
1010    printf("meta data saved\n");
1011   }
1012   i++;
1013  } else if ( !strcmp(k, "metaload") ) {
1014   i++;
1015   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1016    fprintf(stderr, "Error: can not set meta data\n");
1017   } else {
1018    printf("meta data saved\n");
1019   }
1020   i++;
1021
1022  } else {
1023   fprintf(stderr, "Error: invalid command: %s\n", k);
1024  }
1025
1026 }
1027
1028 roar_disconnect(&con);
1029
1030 return 0;
1031}
1032
1033//ll
Note: See TracBrowser for help on using the repository browser.