source: roaraudio/roarclients/roarctl.c @ 3213:da8251c98c0a

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

support to transpher mixer stream id

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