source: roaraudio/roarclients/roarctl.c @ 3530:8ca4bb4cc5b0

Last change on this file since 3530:8ca4bb4cc5b0 was 3530:8ca4bb4cc5b0, checked in by phi, 14 years ago

support to give scale parameter to roarctl

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