source: roaraudio/roarclients/roarctl.c @ 3846:8e117a9c5be1

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

typos

File size: 28.5 KB
Line 
1//roarctl.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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#include <libroardsp/libroardsp.h>
31
32#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID)
33#define _POSIX_USERS
34#endif
35
36#ifdef _POSIX_USERS
37#include <pwd.h>
38#include <grp.h>
39#endif
40
41#include <sys/time.h>
42#include <time.h>
43
44#ifdef ROAR_HAVE_LIBM
45#include <math.h>
46#endif
47
48int g_verbose = 0;
49
50int display_mixer (struct roar_connection * con, int stream);
51int show_meta_all (struct roar_connection * con, int id);
52
53void usage (void) {
54 printf("roarctl [OPTIONS]... COMMAND [OPTS] [COMMAND [OPTS] [COMMAND [OPTS] [...]]]\n");
55
56 printf("\nOptions:\n\n");
57
58 printf("  --server SERVER         - Set server hostname\n"
59        "  --help                  - Show this help\n"
60        "  --verbose   -v          - Show verbose output\n"
61       );
62
63 printf("\nCommands:\n\n");
64 printf(
65        "  help                    - Show this help\n"
66        "  sleep TIME              - Sleeps for TIME seconds\n"
67#ifdef ROAR_HAVE_GETTIMEOFDAY
68        "  ping  NUM               - Do NUM pings using NOOP commands\n"
69#endif
70        "  whoami                  - Get own client ID\n"
71        "\n"
72        "  standby, off            - Go into standby mode\n"
73        "  resume, on              - Go into active mode\n"
74        "  standbymode             - Show current standby mode\n"
75        "  exit                    - Quits the roard (must be used as last command)\n"
76        "  terminate               - Like exit but let the server up to serve still connected clients,\n"
77        "                            new clients cann't connect and the server terminates after the last\n"
78        "                            client disconnected\n"
79        "\n"
80        "  volume ID CHAN [scale S] V0 V1...\n"
81        "                          - Sets volume for stream ID\n"
82        "                            CHAN is the number of channels or 'mono' or 'stereo'\n"
83        "                            if mono or stereo is chosen roarctl trys to set\n"
84        "                            sensfull values for all channels even if the output\n"
85        "                            is has more channels.\n"
86        "                            An optional scale can be given using S.\n"
87        "                            all other args are the volumes of the channels\n"
88        "                            you may use integer or percent values.\n"
89        "                            percent values can floating points.\n"
90        "\n"
91        "  flag   ID FLAGS         - Sets flags FLAGS on stream ID. FLAGS may be a comma\n"
92        "                            seperated list of flags.\n"
93        "  unflag ID FLAGS         - Unsets flags on a stream. See flag.\n"
94        "\n"
95        "  kick TYPE ID            - Kicks object of TYPE with id ID\n"
96        "                            Types: client stream sample source\n"
97        "  newvirtual P D E R B C  - Adds a new virtual (child) stream\n"
98        "                            Parameters:\n"
99        "                             P: Parent stream ID, D: Direction,\n"
100        "                             E: codEc, R: sample Rate,\n"
101        "                             B: bits per sample, C: nummer of channels\n"
102        "\n"
103        "  metaget  ID TYPE        - Read meta date of type TYPE from stream ID\n"
104// TODO: document metaset here.
105        "  metasave ID FILE        - Saves meta data of stream ID to file FILE\n"
106        "  metaload ID FILE        - Loads meta data from file FILE and set it on stream ID\n"
107        "\n"
108        "  serveroinfo             - Gets Informations about server output\n"
109        "  listclients             - Gets Informations about clients\n"
110        "  liststreams             - Gets Informations about streams\n"
111        "  allinfo                 - Get all infos\n"
112       );
113}
114
115#ifdef ROAR_HAVE_GETTIMEOFDAY
116int ping (struct roar_connection * con, int num) {
117 struct timeval         try, ans;
118 struct roar_message    m;
119 register int           ret;
120 int                    i;
121 double                 cur, min = 3600*1000, max = 0, sum = 0;
122
123 if ( num == 0 )
124  return 0;
125
126 for (i = 0; i < num; i++) {
127  m.cmd = ROAR_CMD_NOOP;
128  m.datalen = 0;
129
130  gettimeofday(&try, NULL);
131  ret = roar_req(con, &m, NULL);
132  gettimeofday(&ans, NULL);
133
134  if ( ret == -1 )
135   return -1;
136
137  while (ans.tv_sec > try.tv_sec) {
138   ans.tv_sec--;
139   ans.tv_usec += 1000000;
140  }
141  ans.tv_usec -= try.tv_usec;
142
143  printf("Pong from server: seq=%i time=%.3fms\n", i, (cur = ans.tv_usec/1000.0));
144
145  sum += cur;
146  if ( min > cur )
147   min = cur;
148  if ( cur > max )
149   max = cur;
150
151  if ( i != (num - 1) )
152   sleep(1);
153 }
154
155 printf("\n--- ping statistics ---\n");
156 printf("%i packets transmitted\n", i);
157 printf("rtt min/avg/max = %.3f/%.3f/%.3f ms\n", min, sum/(double)i, max);
158
159 return 0;
160}
161#endif
162
163void server_oinfo (struct roar_connection * con) {
164 struct roar_stream s;
165
166 if ( roar_server_oinfo(con, &s) == -1 ) {
167  fprintf(stderr, "Error: can not get server output info\n");
168  return;
169 }
170
171 printf("Stream direction      : %s\n", roar_dir2str(s.dir));
172 printf("Server Output rate    : %i\n", s.info.rate);
173 printf("Server Output bits    : %i\n", s.info.bits);
174 printf("Server Output channels: %i\n", s.info.channels);
175 printf("Server Output codec   : %i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
176                                     s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
177// printf("Server Output rate: %i", s.info.rate);
178  if ( g_verbose > 1 )
179   printf("Server Position       : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels));
180}
181
182const char * proc_name (pid_t pid) {
183 static char ret[80] = "?";
184#ifdef __linux__
185 char file[80], buf[80], *r;
186 int  i;
187
188 snprintf(file, 79, "/proc/%i/exe", pid);
189 file[79] = 0;
190
191 ret[0] = '?';
192 ret[1] = 0;
193
194 if ( (i = readlink(file, buf, 79)) != -1 ) {
195  buf[i] = 0;
196  if ( (r = strrchr(buf, '/')) != NULL ) {
197   r++;
198   if ( *r != 0 )
199    strcpy(ret, r);
200  }
201 }
202#else
203 (void)pid;
204#endif
205
206 return ret;
207}
208
209void list_clients (struct roar_connection * con) {
210 int i;
211 int num;
212 int h;
213 int id[ROAR_CLIENTS_MAX];
214 char tmp[80];
215 int self_id;
216 struct roar_client self_client;
217 struct roar_client c;
218#ifdef _POSIX_USERS
219 struct group  * grp = NULL;
220 struct passwd * pwd = NULL;
221#endif
222
223 if ( (self_id = roar_get_clientid(con)) != -1 ) {
224  if ( roar_get_client(con, &self_client, self_id) == -1 )
225   self_id = -1;
226 }
227
228 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
229  fprintf(stderr, "Error: can not get client list\n");
230  return;
231 }
232
233 for (i = 0; i < num; i++) {
234  printf("client %i:\n", id[i]);
235  if ( roar_get_client(con, &c, id[i]) == -1 ) {
236   fprintf(stderr, "Error: can not get client info\n");
237   continue;
238  }
239
240  if ( c.name[0] != '\0' )
241   printf("Client name           : %s\n", c.name);
242
243  if ( roar_nnode_get_socktype(&(c.nnode)) != ROAR_SOCKET_TYPE_UNKNOWN ) {
244   if ( roar_nnode_to_str(&(c.nnode), tmp, 80) == 0 ) {
245    printf("Client network node   : %s\n", tmp);
246   }
247  }
248
249  if ( c.pid != -1 ) {
250   if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
251    printf("Client PID            : %i(%s)\n", c.pid, proc_name(c.pid));
252   } else {
253    printf("Client PID            : %i\n", c.pid);
254   }
255  }
256  if ( c.uid != -1 ) {
257#ifdef _POSIX_USERS
258   if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
259    pwd = getpwuid(c.uid);
260    grp = getgrgid(c.gid);
261    printf("Client UID/GID        : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?");
262   } else {
263#else
264   if ( 1 ) {
265#endif
266    printf("Client UID/GID        : %i/%i\n", c.uid, c.gid);
267   }
268  }
269
270  if ( g_verbose && c.proto != ROAR_PROTO_NONE ) {
271   printf("Client Protocol       : %s\n", roar_proto2str(c.proto));
272  }
273
274  if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) {
275   if ( c.byteorder == ROAR_BYTEORDER_NETWORK ) {
276    strcpy(tmp, " (network byteorder");
277   } else {
278    *tmp = 0;
279   }
280
281   if ( c.byteorder == ROAR_BYTEORDER_NATIVE ) {
282    if ( *tmp ) {
283     strcat(tmp, ", native");
284    } else {
285     strcpy(tmp, " (native");
286    }
287   }
288
289   if ( *tmp )
290    strcat(tmp, ")");
291
292   printf("Client Byteorder      : %s%s\n", roar_byteorder2str(c.byteorder), tmp);
293  }
294
295  if ( c.execed != -1 )
296   printf("Execed stream         : %i\n", c.execed);
297
298  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
299   if ( c.streams[h] != -1 )
300    printf("stream                : %i\n", c.streams[h]);
301 }
302
303}
304
305void list_streams (struct roar_connection * con) {
306 int i;
307 int num;
308 int id[ROAR_STREAMS_MAX];
309 char chanmap[ROAR_MAX_CHANNELS];
310 struct roar_stream s;
311 struct roar_stream_info info;
312 char buffer[1024];
313 char * flags = buffer;
314 char * name  = buffer;
315 char * infotext;
316 size_t len;
317
318
319 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
320  fprintf(stderr, "Error: can not get stream list\n");
321  return;
322 }
323
324 for (i = 0; i < num; i++) {
325  printf("stream %i:\n", id[i]);
326  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
327   fprintf(stderr, "Error: can not get stream info\n");
328   continue;
329  }
330  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
331
332  if ( roar_stream_get_name(con, &s, name, 1024) == 0 )
333   printf("Stream name           : %s\n", name);
334
335  if ( (int)s.pos_rel_id == -1 ) {
336   printf("Relativ position id   : none (stream not synchronized)\n");
337  } else if ( (int)s.pos_rel_id == id[i] ) {
338   printf("Relativ position id   : %i (self synchronized)\n", s.pos_rel_id);
339  } else {
340   printf("Relativ position id   : %i (synchronized)\n", s.pos_rel_id);
341  }
342  if ( g_verbose > 1 ) {
343   if ( s.info.rate && s.info.channels ) {
344    printf("Position              : %lu S (%.3fs)\n", (unsigned long int) s.pos,
345                                    (float)s.pos/(s.info.rate*s.info.channels));
346   } else {
347    printf("Position              : %lu S\n", (unsigned long int) s.pos);
348   }
349  }
350
351  switch (s.dir) {
352   case ROAR_DIR_MIDI_IN:
353   case ROAR_DIR_MIDI_OUT:
354     infotext = " ticks/s";
355    break;
356   case ROAR_DIR_LIGHT_IN:
357   case ROAR_DIR_LIGHT_OUT:
358     infotext = " updates/s";
359    break;
360   default:
361     infotext = " Hz";
362  }
363  if ( s.info.rate )
364   printf("Stream sample rate    : %i%s\n", s.info.rate, infotext);
365  if ( s.info.bits )
366   printf("Stream bits           : %i\n", s.info.bits);
367  if ( s.info.channels )
368  printf("Stream channels       : %i\n", s.info.channels);
369
370  printf("Stream codec          : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
371                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
372  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
373   if ( info.codec != s.info.codec ) {
374    printf("Streamed codec        : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
375                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
376   }
377
378   if ( g_verbose ) {
379    if ( info.block_size )
380     printf("Stream block size     : %i Byte\n", info.block_size);
381
382    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
383    if ( g_verbose > 1 )
384     printf("Stream delay          : %ims (%.2fm)\n",   (int)info.delay/1000, (info.delay*(float)_SPEED_OF_SOUND));
385
386    if ( g_verbose > 1 )
387     printf("Stream mixer          : %i\n",   info.mixer);
388
389    if ( g_verbose > 1 )
390     printf("Stream state          : %s\n",   roar_streamstate2str(info.state));
391
392    if ( g_verbose > 1 )
393     printf("Stream role           : %s\n",   roar_role2str(info.role));
394
395    *flags = 0;
396    if ( info.flags & ROAR_FLAG_PRIMARY )
397     strcat(flags, "primary ");
398    if ( info.flags & ROAR_FLAG_SYNC )
399     strcat(flags, "sync ");
400    if ( info.flags & ROAR_FLAG_OUTPUT )
401     strcat(flags, "output ");
402    if ( info.flags & ROAR_FLAG_SOURCE )
403     strcat(flags, "source ");
404    if ( info.flags & ROAR_FLAG_META )
405     strcat(flags, "meta ");
406    if ( info.flags & ROAR_FLAG_AUTOCONF )
407     strcat(flags, "autoconf ");
408    if ( info.flags & ROAR_FLAG_CLEANMETA )
409     strcat(flags, "cleanmeta ");
410    if ( info.flags & ROAR_FLAG_HWMIXER )
411     strcat(flags, "hwmixer ");
412    if ( info.flags & ROAR_FLAG_PAUSE )
413     strcat(flags, "pause ");
414    if ( info.flags & ROAR_FLAG_MUTE )
415     strcat(flags, "mute ");
416    if ( info.flags & ROAR_FLAG_MMAP )
417     strcat(flags, "mmap ");
418    if ( info.flags & ROAR_FLAG_ANTIECHO )
419     strcat(flags, "antiecho ");
420    if ( info.flags & ROAR_FLAG_VIRTUAL )
421     strcat(flags, "virtual ");
422    if ( info.flags & ROAR_FLAG_RECSOURCE )
423     strcat(flags, "recsource ");
424    if ( info.flags & ROAR_FLAG_PASSMIXER )
425     strcat(flags, "passmixer ");
426    if ( info.flags & ROAR_FLAG_PRETHRU )
427     strcat(flags, "prethru ");
428    if ( info.flags & ROAR_FLAG_IMMUTABLE )
429     strcat(flags, "immutable ");
430    if ( info.flags & ROAR_FLAG_ENHANCE )
431     strcat(flags, "enhance ");
432
433    printf("Flags                 : %s\n", flags);
434   }
435  }
436
437  if ( g_verbose ) {
438   len = ROAR_MAX_CHANNELS;
439   if ( roar_stream_get_chanmap(con, &s, chanmap, &len) == -1 ) {
440    fprintf(stderr, "Error: can not get stream channel map\n");
441   } else {
442    if ( roardsp_chanlist2str(chanmap, len, buffer, 1024) == -1 ) {
443     fprintf(stderr, "Error: can not convert channel map into string\n");
444    } else {
445     printf("Channel Map           : %s\n", buffer);
446    }
447   }
448  }
449
450  if ( s.dir != ROAR_DIR_THRU ) {
451   display_mixer(con, id[i]);
452   show_meta_all(con, id[i]);
453  }
454 }
455
456}
457
458int display_mixer (struct roar_connection * con, int stream) {
459 struct roar_mixer_settings mixer;
460 int channels;
461 int i;
462 float fs;
463
464 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
465  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream);
466  return -1;
467 }
468
469#ifdef ROAR_HAVE_LIBM
470#define _DB ", %.2fdB"
471#else
472#define _DB ""
473#endif
474
475 fs = (float)mixer.scale / 100.;
476
477 if ( channels ) { // we hide RPG info for zero-channel streams
478  printf("Mixer ReplayGain      : %i/%i (%.2f%%" _DB ")\n", mixer.rpg_mul, mixer.rpg_div,
479                                                          100.*(float)mixer.rpg_mul/((float)mixer.rpg_div)
480#ifdef ROAR_HAVE_LIBM
481                           , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div)
482#endif
483        );
484 }
485
486 for (i = 0; i < channels; i++)
487  printf("Mixer volume chan %2i  : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale,
488                           (float)mixer.mixer[i]/fs
489#ifdef ROAR_HAVE_LIBM
490                          , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale)
491#endif
492        );
493
494 return 0;
495}
496
497static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) {
498 float fs = scale;
499
500 switch (k[len - 1]) {
501  case '%':
502    k[len - 1] = 0;
503    return (atof(k)*fs)/100.;
504   break;
505#ifdef ROAR_HAVE_LIBM
506  case 'b':
507  case 'B':
508    // TODO: can we hanle the error better?
509    if ( len < 2 )
510     return 0;
511
512    k[len - 2] = 0;
513    return powf(10, atof(k)/20.f)*fs;
514   break;
515#endif
516 }
517
518 return atoi(k);
519}
520
521int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
522 uint16_t scale = 65535;
523 int chans = 0;
524 int id;
525 int i;
526 int len;
527 int old_chans;
528 int vol_l, vol_r, vol_mono;
529 char * k;
530 struct roar_mixer_settings mixer;
531 struct roar_mixer_settings old_mixer;
532
533 if (*cur + 2 > max)
534  return -1;
535
536 id = atoi(arg[++(*cur)]);
537
538 k = arg[++(*cur)];
539
540 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
541  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
542  return -1;
543 }
544
545 if ( !strcmp(arg[*cur + 1], "scale") ) {
546  (*cur)++; // 'scale'
547  (*cur)++;
548  scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535);
549 }
550
551// TODO: clean up code here as the % vs. abs code is very duplicate...
552
553 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
554  chans = 1;
555
556  if ( *cur + 1 > max )
557   return -1;
558
559  k   = arg[++(*cur)];
560  len = strlen(k);
561
562  vol_mono = set_mixer_parse_volume(k, len, scale);
563
564  for (i = 0; i < old_chans; i++)
565   mixer.mixer[i] = vol_mono;
566
567  chans = old_chans;
568
569 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
570  chans = old_chans;
571
572  if ( *cur + 2 > max )
573   return -1;
574
575  k   = arg[++(*cur)];
576  len = strlen(k);
577
578  vol_l = set_mixer_parse_volume(k, len, scale);
579
580  k   = arg[++(*cur)];
581  len = strlen(k);
582
583  vol_r = set_mixer_parse_volume(k, len, scale);
584
585  vol_mono = (vol_l + vol_r) / 2;
586
587  mixer.mixer[0] = vol_l;
588  mixer.mixer[1] = vol_r;
589
590  switch (chans) {
591   case 1:
592     mixer.mixer[0] = vol_mono;
593    break;
594//   case 2: classic stereo...
595   case 3:
596     mixer.mixer[2] = vol_mono;
597    break;
598   case 4:
599     mixer.mixer[2] = vol_l;
600     mixer.mixer[3] = vol_r;
601    break;
602   case 5:
603     mixer.mixer[2] = vol_mono;
604     mixer.mixer[3] = vol_l;
605     mixer.mixer[4] = vol_r;
606    break;
607   case 6:
608     mixer.mixer[2] = vol_mono;
609     mixer.mixer[3] = vol_mono;
610     mixer.mixer[4] = vol_l;
611     mixer.mixer[5] = vol_r;
612    break;
613   default:
614     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
615     return -1;
616    break;
617  }
618
619 } else {
620  if ( strcmp(k, "mono") == 0 ) {
621   chans = 1;
622  } else if ( strcmp(k, "stereo") == 0 ) {
623   chans = 2;
624  } else {
625   chans = atoi(k);
626  }
627
628//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
629
630  if ( *cur + chans > max )
631   return -1;
632
633  for (i = 0; i < chans; i++) {
634   k   = arg[++(*cur)];
635   len = strlen(k);
636
637   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
638  }
639 }
640
641 mixer.scale = scale;
642
643 return roar_set_vol(con, id, &mixer, chans);
644}
645
646
647int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
648 struct roar_stream s;
649 int dir    = roar_str2dir(d_s);
650 int parent = atoi(p_s);
651
652 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
653
654 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
655  return -1;
656
657 return roar_simple_connect_virtual(con, &s, parent, dir);
658}
659
660int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
661 struct roar_meta   meta;
662 struct roar_stream s;
663 int mode_i = ROAR_META_MODE_SET;
664
665 if ( roar_stream_new_by_id(&s, id) == -1 )
666  return -1;
667
668// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
669
670 if ( strcmp(mode, "add") == 0 ) {
671  mode_i = ROAR_META_MODE_ADD;
672 }
673
674 meta.type   = roar_meta_inttype(type);
675 meta.value  = val;
676 meta.key[0] = 0;
677
678 if ( meta.type == -1 ) {
679  fprintf(stderr, "Error: unknown type: %s\n", type);
680  return -1;
681 }
682
683// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
684
685 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
686  return -1;
687
688 meta.type  = ROAR_META_TYPE_NONE;
689 meta.value = NULL;
690
691 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
692}
693
694int load_meta (struct roar_connection * con, int id, char * file) {
695 struct roar_meta   meta;
696 struct roar_stream s;
697 int mode_i = ROAR_META_MODE_SET;
698 FILE * in;
699 char lion[1024];
700 char * v;
701
702 if ( roar_stream_new_by_id(&s, id) == -1 )
703  return -1;
704
705 if ( (in = fopen(file, "r")) == NULL )
706  return -1;
707
708 while (fgets(lion, 1024, in) != NULL) {
709  if ( (v = strtok(lion, "\r\n")) != NULL )
710   if ( (v = strtok(NULL, "\r\n")) != NULL )
711    *(v-1) = 0;
712
713  if ( (v = strstr(lion, "=")) == NULL ) {
714   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
715   continue;
716  }
717
718  *v++ = 0;
719
720  meta.type   = roar_meta_inttype(lion);
721  meta.value  = v;
722  meta.key[0] = 0;
723
724  if ( meta.type == -1 ) {
725   fprintf(stderr, "Error: unknown type: %s\n", lion);
726   continue;
727  }
728
729  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
730   return -1;
731 }
732
733 fclose(in);
734
735 meta.type  = ROAR_META_TYPE_NONE;
736 meta.value = NULL;
737
738 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
739}
740
741int show_meta_type (struct roar_connection * con, int id, char * type) {
742 struct roar_meta   meta;
743 struct roar_stream s;
744
745 if ( roar_stream_new_by_id(&s, id) == -1 )
746  return -1;
747
748 meta.type  = roar_meta_inttype(type);
749
750 if ( meta.type == -1 ) {
751  fprintf(stderr, "Error: unknown type: %s\n", type);
752  return -1;
753 }
754
755 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
756  return -1;
757
758 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
759
760 roar_meta_free(&meta);
761
762 return 0;
763}
764
765int show_meta_all (struct roar_connection * con, int id) {
766 struct roar_stream s;
767 int types[ROAR_META_MAX_PER_STREAM];
768 int i;
769 int len;
770
771 if ( roar_stream_new_by_id(&s, id) == -1 )
772  return -1;
773
774 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
775  return -1;
776
777 for (i = 0; i < len; i++)
778  show_meta_type(con, id, roar_meta_strtype(types[i]));
779
780 return 0;
781}
782
783int save_meta (struct roar_connection * con, int id, char * file) {
784 struct roar_stream s;
785 struct roar_meta   meta;
786 int types[ROAR_META_MAX_PER_STREAM];
787 int i;
788 int len;
789 FILE * out;
790
791 if ( roar_stream_new_by_id(&s, id) == -1 )
792  return -1;
793
794 if ( (out = fopen(file, "w")) == NULL )
795  return -1;
796
797 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
798  return -1;
799
800 for (i = 0; i < len; i++) {
801/*
802  show_meta_type(con, id, roar_meta_strtype(types[i]));
803*/
804  meta.type  = types[i];
805
806  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
807   continue;
808
809//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
810
811  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
812
813  roar_meta_free(&meta);
814 }
815
816 fclose(out);
817
818 return 0;
819}
820
821int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
822 int f = ROAR_FLAG_NONE;
823 char * c;
824 struct roar_stream s[1];
825
826 if ( roar_stream_new_by_id(s, id) == -1 )
827  return -1;
828
829 c = strtok(flags, ",");
830 while (c != NULL) {
831  if ( !strcmp(c, "meta") ) {
832   f |= ROAR_FLAG_META;
833  } else if ( !strcmp(c, "primary") ) {
834   f |= ROAR_FLAG_PRIMARY;
835  } else if ( !strcmp(c, "sync") ) {
836   f |= ROAR_FLAG_SYNC;
837  } else if ( !strcmp(c, "cleanmeta") ) {
838   f |= ROAR_FLAG_CLEANMETA;
839  } else if ( !strcmp(c, "hwmixer") ) {
840   f |= ROAR_FLAG_HWMIXER;
841  } else if ( !strcmp(c, "pause") ) {
842   f |= ROAR_FLAG_PAUSE;
843  } else if ( !strcmp(c, "mute") ) {
844   f |= ROAR_FLAG_MUTE;
845  } else if ( !strcmp(c, "mmap") ) {
846   f |= ROAR_FLAG_MMAP;
847  } else if ( !strcmp(c, "antiecho") ) {
848   f |= ROAR_FLAG_ANTIECHO;
849  } else if ( !strcmp(c, "recsource") ) {
850   f |= ROAR_FLAG_RECSOURCE;
851  } else if ( !strcmp(c, "passmixer") ) {
852   f |= ROAR_FLAG_PASSMIXER;
853  } else if ( !strcmp(c, "virtual") ) {
854   f |= ROAR_FLAG_VIRTUAL;
855  } else if ( !strcmp(c, "prethru") ) {
856   f |= ROAR_FLAG_PRETHRU;
857  } else if ( !strcmp(c, "immutable") ) {
858   f |= ROAR_FLAG_IMMUTABLE;
859  } else if ( !strcmp(c, "enhance") ) {
860   f |= ROAR_FLAG_ENHANCE;
861  } else {
862   fprintf(stderr, "Error: unknown flag: %s\n", c);
863   return -1;
864  }
865
866  c = strtok(NULL, ",");
867 }
868
869 return roar_stream_set_flags(con, s, f, reset);
870}
871
872int main (int argc, char * argv[]) {
873 struct roar_connection con;
874 char * server   = NULL;
875 char * k = NULL;
876 int    i;
877 int    t = 0;
878
879 for (i = 1; i < argc; i++) {
880  k = argv[i];
881
882  if ( strcmp(k, "--server") == 0 ) {
883   server = argv[++i];
884  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
885   g_verbose++;
886  } else if ( strcmp(k, "--help") == 0 ) {
887   usage();
888   return 0;
889  } else if ( *k == '-' ) {
890   fprintf(stderr, "Error: unknown argument: %s\n", k);
891   usage();
892   return 1;
893  } else {
894   break;
895  }
896 }
897
898 // connect
899
900 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
901  fprintf(stderr, "Error: Can not connect to server\n");
902  return 1;
903 }
904
905 if ( i == argc ) {
906  fprintf(stderr, "Error: No Commands given\n");
907  return 0; // this is not a fatal error...
908 }
909
910 for (; i < argc; i++) {
911  k = argv[i];
912  // cmd is in k
913
914  printf("--- [ %s ] ---\n", k);
915
916  if ( !strcmp(k, "help") ) {
917   usage();
918
919  } else if ( !strcmp(k, "sleep") ) {
920   sleep(atoi(argv[++i]));
921
922  } else if ( !strcmp(k, "ping") ) {
923#ifdef ROAR_HAVE_GETTIMEOFDAY
924   if ( ping(&con, atoi(argv[++i])) == -1 ) {
925    fprintf(stderr, "Error: can not ping\n");
926   }
927#else
928    fprintf(stderr, "Error: ping not supported.\n");
929    i++;
930#endif
931
932  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
933   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
934    fprintf(stderr, "Error: can not set mode to standby\n");
935   } else {
936    printf("going into standby\n");
937   }
938  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
939   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
940    fprintf(stderr, "Error: can not set mode to active\n");
941   } else {
942    printf("going into active mode\n");
943   }
944
945  } else if ( !strcmp(k, "exit") ) {
946   if ( roar_exit(&con) == -1 ) {
947    fprintf(stderr, "Error: can not quit server\n");
948   } else {
949    printf("Server quited\n");
950    break;
951   }
952  } else if ( !strcmp(k, "terminate") ) {
953   if ( roar_terminate(&con, 1) == -1 ) {
954    fprintf(stderr, "Error: can not terminate server\n");
955   } else {
956    printf("Server got asked to quited\n");
957    break;
958   }
959
960  } else if ( !strcmp(k, "standbymode") ) {
961   t = roar_get_standby(&con);
962   if ( t == -1 ) {
963    fprintf(stderr, "Error: can not get stanby mode\n");
964   } else if ( t == ROAR_STANDBY_ACTIVE ) {
965    printf("Server is in standby\n");
966   } else if ( t == ROAR_STANDBY_INACTIVE ) {
967    printf("Server is active\n");
968   } else {
969    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
970   }
971
972  } else if ( !strcmp(k, "whoami") ) {
973   printf("My client ID is: %i\n", roar_get_clientid(&con));
974  } else if ( !strcmp(k, "serveroinfo") ) {
975   server_oinfo(&con);
976  } else if ( !strcmp(k, "listclients") ) {
977   list_clients(&con);
978  } else if ( !strcmp(k, "liststreams") ) {
979   list_streams(&con);
980  } else if ( !strcmp(k, "allinfo") ) {
981   server_oinfo(&con);
982   printf("\n");
983   list_clients(&con);
984   printf("\n");
985   list_streams(&con);
986
987  } else if ( !strcmp(k, "kick") ) {
988   k = argv[++i];
989   if ( !strcmp(k, "client") ) {
990    t = ROAR_OT_CLIENT;
991   } else if ( !strcmp(k, "stream") ) {
992    t = ROAR_OT_STREAM;
993   } else if ( !strcmp(k, "sample") ) {
994    t = ROAR_OT_SAMPLE;
995   } else if ( !strcmp(k, "source") ) {
996    t = ROAR_OT_SOURCE;
997   } else {
998    fprintf(stderr, "Error: unknown type: %s\n", k);
999    continue;
1000   }
1001   //t = atoi(argv[i++]);
1002   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
1003    fprintf(stderr, "Error: can not kick %s\n", k);
1004   } else {
1005    printf("%s kicked\n", k);
1006   }
1007
1008  } else if ( !strcmp(k, "newvirtual") ) {
1009   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
1010    fprintf(stderr, "Error: can not create new virtual stream\n");
1011   } else {
1012    printf("virtual stream created\n");
1013   }
1014   i += 6;
1015
1016  } else if ( !strcmp(k, "volume") ) {
1017   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
1018    fprintf(stderr, "Error: can not set volume\n");
1019   } else {
1020    printf("volume changed\n");
1021   }
1022
1023  } else if ( !strcmp(k, "flag") ) {
1024   i++;
1025   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
1026    fprintf(stderr, "Error: can not set flags\n");
1027   } else {
1028    printf("flags changed\n");
1029   }
1030   i++;
1031  } else if ( !strcmp(k, "unflag") ) {
1032   i++;
1033   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
1034    fprintf(stderr, "Error: can not reset flags\n");
1035   } else {
1036    printf("flags changed\n");
1037   }
1038   i++;
1039  } else if ( !strcmp(k, "metaset") ) {
1040   i++;
1041   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1042    fprintf(stderr, "Error: can not set meta data\n");
1043   } else {
1044    printf("meta data changed\n");
1045   }
1046   i += 3;
1047  } else if ( !strcmp(k, "metaget") ) {
1048   i++;
1049   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1050    fprintf(stderr, "Error: can not get meta data\n");
1051   }
1052   i++;
1053  } else if ( !strcmp(k, "metasave") ) {
1054   i++;
1055   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1056    fprintf(stderr, "Error: can not get meta data\n");
1057   } else {
1058    printf("meta data saved\n");
1059   }
1060   i++;
1061  } else if ( !strcmp(k, "metaload") ) {
1062   i++;
1063   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1064    fprintf(stderr, "Error: can not set meta data\n");
1065   } else {
1066    printf("meta data saved\n");
1067   }
1068   i++;
1069
1070  } else {
1071   fprintf(stderr, "Error: invalid command: %s\n", k);
1072  }
1073
1074 }
1075
1076 roar_disconnect(&con);
1077
1078 return 0;
1079}
1080
1081//ll
Note: See TracBrowser for help on using the repository browser.