source: roaraudio/roarclients/roarctl.c @ 3529:fba86b5b08a7

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

support to read complet mixer from roard, including scale and rpg

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