source: roaraudio/roarclients/roarctl.c @ 3630:89a9079f8e3f

Last change on this file since 3630:89a9079f8e3f was 3630:89a9079f8e3f, checked in by phi, 14 years ago

implemented roles, still need some support to set them

File size: 28.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#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 flooding 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#endif
203
204 return ret;
205}
206
207void list_clients (struct roar_connection * con) {
208 int i;
209 int num;
210 int h;
211 int id[ROAR_CLIENTS_MAX];
212 char tmp[80];
213 int self_id;
214 struct roar_client self_client;
215 struct roar_client c;
216#ifdef _POSIX_USERS
217 struct group  * grp = NULL;
218 struct passwd * pwd = NULL;
219#endif
220
221 if ( (self_id = roar_get_clientid(con)) != -1 ) {
222  if ( roar_get_client(con, &self_client, self_id) == -1 )
223   self_id = -1;
224 }
225
226 if ( (num = roar_list_clients(con, id, ROAR_CLIENTS_MAX)) == -1 ) {
227  fprintf(stderr, "Error: can not get client list\n");
228  return;
229 }
230
231 for (i = 0; i < num; i++) {
232  printf("client %i:\n", id[i]);
233  if ( roar_get_client(con, &c, id[i]) == -1 ) {
234   fprintf(stderr, "Error: can not get client info\n");
235   continue;
236  }
237  printf("Client name           : %s\n", c.name);
238
239  if ( roar_nnode_get_socktype(&(c.nnode)) != ROAR_SOCKET_TYPE_UNKNOWN ) {
240   if ( roar_nnode_to_str(&(c.nnode), tmp, 80) == 0 ) {
241    printf("Client network node   : %s\n", tmp);
242   }
243  }
244
245  if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
246   printf("Client PID            : %i(%s)\n", c.pid, proc_name(c.pid));
247  } else {
248   printf("Client PID            : %i\n", c.pid);
249  }
250  if ( c.uid != -1 ) {
251#ifdef _POSIX_USERS
252   if ( self_id != -1 && roar_nnode_cmp(&(self_client.nnode), &(c.nnode)) == 0 ) {
253    pwd = getpwuid(c.uid);
254    grp = getgrgid(c.gid);
255    printf("Client UID/GID        : %i(%s)/%i(%s)\n", c.uid, pwd ? pwd->pw_name : "?", c.gid, grp ? grp->gr_name : "?");
256   } else {
257#else
258   if ( 1 ) {
259#endif
260    printf("Client UID/GID        : %i/%i\n", c.uid, c.gid);
261   }
262  }
263
264  if ( g_verbose && c.proto != ROAR_PROTO_NONE ) {
265   printf("Client Protocol       : %s\n", roar_proto2str(c.proto));
266  }
267
268  if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) {
269   if ( c.byteorder == ROAR_BYTEORDER_NETWORK ) {
270    strcpy(tmp, " (network byteorder");
271   } else {
272    *tmp = 0;
273   }
274
275   if ( c.byteorder == ROAR_BYTEORDER_NATIVE ) {
276    if ( *tmp ) {
277     strcat(tmp, ", native");
278    } else {
279     strcpy(tmp, " (native");
280    }
281   }
282
283   if ( *tmp )
284    strcat(tmp, ")");
285
286   printf("Client Byteorder      : %s%s\n", roar_byteorder2str(c.byteorder), tmp);
287  }
288
289  if ( c.execed != -1 )
290   printf("Execed stream         : %i\n", c.execed);
291
292  for (h = 0; h < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; h++)
293   if ( c.streams[h] != -1 )
294    printf("stream                : %i\n", c.streams[h]);
295 }
296
297}
298
299void list_streams (struct roar_connection * con) {
300 int i;
301 int num;
302 int id[ROAR_STREAMS_MAX];
303 char chanmap[ROAR_MAX_CHANNELS];
304 struct roar_stream s;
305 struct roar_stream_info info;
306 char buffer[1024];
307 char * flags = buffer;
308 char * name  = buffer;
309 char * infotext;
310 size_t len;
311
312
313 if ( (num = roar_list_streams(con, id, ROAR_STREAMS_MAX)) == -1 ) {
314  fprintf(stderr, "Error: can not get stream list\n");
315  return;
316 }
317
318 for (i = 0; i < num; i++) {
319  printf("stream %i:\n", id[i]);
320  if ( roar_get_stream(con, &s, id[i]) == -1 ) {
321   fprintf(stderr, "Error: can not get stream info\n");
322   continue;
323  }
324  printf("Stream direction      : %s\n", roar_dir2str(s.dir));
325
326  if ( roar_stream_get_name(con, &s, name, 1024) == 0 )
327   printf("Stream name           : %s\n", name);
328
329  if ( s.pos_rel_id == -1 ) {
330   printf("Relativ position id   : none (stream not synchronized)\n");
331  } else if ( s.pos_rel_id == id[i] ) {
332   printf("Relativ position id   : %i (self synchronized)\n", s.pos_rel_id);
333  } else {
334   printf("Relativ position id   : %i (synchronized)\n", s.pos_rel_id);
335  }
336  if ( g_verbose > 1 ) {
337   if ( s.info.rate && s.info.channels ) {
338    printf("Position              : %lu S (%.3fs)\n", (unsigned long int) s.pos,
339                                    (float)s.pos/(s.info.rate*s.info.channels));
340   } else {
341    printf("Position              : %lu S\n", (unsigned long int) s.pos);
342   }
343  }
344
345  switch (s.dir) {
346   case ROAR_DIR_MIDI_IN:
347   case ROAR_DIR_MIDI_OUT:
348     infotext = " ticks/s";
349    break;
350   case ROAR_DIR_LIGHT_IN:
351   case ROAR_DIR_LIGHT_OUT:
352     infotext = " updates/s";
353    break;
354   default:
355     infotext = " Hz";
356  }
357  if ( s.info.rate )
358   printf("Stream sample rate    : %i%s\n", s.info.rate, infotext);
359  if ( s.info.bits )
360   printf("Stream bits           : %i\n", s.info.bits);
361  if ( s.info.channels )
362  printf("Stream channels       : %i\n", s.info.channels);
363
364  printf("Stream codec          : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
365                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
366  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
367   if ( info.codec != s.info.codec ) {
368    printf("Streamed codec        : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
369                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
370   }
371
372   if ( g_verbose ) {
373    if ( info.block_size )
374     printf("Stream block size     : %i Byte\n", info.block_size);
375
376    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
377    if ( g_verbose > 1 )
378     printf("Stream delay          : %ims (%.2fm)\n",   (int)info.delay/1000, (info.delay*(float)_SPEED_OF_SOUND));
379
380    if ( g_verbose > 1 )
381     printf("Stream mixer          : %i\n",   info.mixer);
382
383    if ( g_verbose > 1 )
384     printf("Stream state          : %s\n",   roar_streamstate2str(info.state));
385
386    if ( g_verbose > 1 )
387     printf("Stream role           : %s\n",   roar_role2str(info.role));
388
389    *flags = 0;
390    if ( info.flags & ROAR_FLAG_PRIMARY )
391     strcat(flags, "primary ");
392    if ( info.flags & ROAR_FLAG_SYNC )
393     strcat(flags, "sync ");
394    if ( info.flags & ROAR_FLAG_OUTPUT )
395     strcat(flags, "output ");
396    if ( info.flags & ROAR_FLAG_SOURCE )
397     strcat(flags, "source ");
398    if ( info.flags & ROAR_FLAG_META )
399     strcat(flags, "meta ");
400    if ( info.flags & ROAR_FLAG_AUTOCONF )
401     strcat(flags, "autoconf ");
402    if ( info.flags & ROAR_FLAG_CLEANMETA )
403     strcat(flags, "cleanmeta ");
404    if ( info.flags & ROAR_FLAG_HWMIXER )
405     strcat(flags, "hwmixer ");
406    if ( info.flags & ROAR_FLAG_PAUSE )
407     strcat(flags, "pause ");
408    if ( info.flags & ROAR_FLAG_MUTE )
409     strcat(flags, "mute ");
410    if ( info.flags & ROAR_FLAG_MMAP )
411     strcat(flags, "mmap ");
412    if ( info.flags & ROAR_FLAG_ANTIECHO )
413     strcat(flags, "antiecho ");
414    if ( info.flags & ROAR_FLAG_VIRTUAL )
415     strcat(flags, "virtual ");
416    if ( info.flags & ROAR_FLAG_RECSOURCE )
417     strcat(flags, "recsource ");
418    if ( info.flags & ROAR_FLAG_PASSMIXER )
419     strcat(flags, "passmixer ");
420    if ( info.flags & ROAR_FLAG_PRETHRU )
421     strcat(flags, "prethru ");
422    if ( info.flags & ROAR_FLAG_IMMUTABLE )
423     strcat(flags, "immutable ");
424    if ( info.flags & ROAR_FLAG_ENHANCE )
425     strcat(flags, "enhance ");
426
427    printf("Flags                 : %s\n", flags);
428   }
429  }
430
431  if ( g_verbose ) {
432   len = ROAR_MAX_CHANNELS;
433   if ( roar_stream_get_chanmap(con, &s, chanmap, &len) == -1 ) {
434    fprintf(stderr, "Error: can not get stream channel map\n");
435   } else {
436    if ( roardsp_chanlist2str(chanmap, len, buffer, 1024) == -1 ) {
437     fprintf(stderr, "Error: can not convert channel map into string\n");
438    } else {
439     printf("Channel Map           : %s\n", buffer);
440    }
441   }
442  }
443
444  if ( s.dir != ROAR_DIR_THRU ) {
445   display_mixer(con, id[i]);
446   show_meta_all(con, id[i]);
447  }
448 }
449
450}
451
452int display_mixer (struct roar_connection * con, int stream) {
453 struct roar_mixer_settings mixer;
454 int channels;
455 int i;
456 float fs;
457
458 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
459  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream);
460  return -1;
461 }
462
463#ifdef ROAR_HAVE_LIBM
464#define _DB ", %.2fdB"
465#else
466#define _DB ""
467#endif
468
469 fs = (float)mixer.scale / 100.;
470
471 if ( channels ) { // we hide RPG info for zero-channel streams
472  printf("Mixer ReplayGain      : %i/%i (%.2f%%" _DB ")\n", mixer.rpg_mul, mixer.rpg_div,
473                                                          100.*(float)mixer.rpg_mul/((float)mixer.rpg_div)
474#ifdef ROAR_HAVE_LIBM
475                           , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div)
476#endif
477        );
478 }
479
480 for (i = 0; i < channels; i++)
481  printf("Mixer volume chan %2i  : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale,
482                           (float)mixer.mixer[i]/fs
483#ifdef ROAR_HAVE_LIBM
484                          , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale)
485#endif
486        );
487
488 return 0;
489}
490
491static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) {
492 float fs = scale;
493
494 switch (k[len - 1]) {
495  case '%':
496    k[len - 1] = 0;
497    return (atof(k)*fs)/100.;
498   break;
499#ifdef ROAR_HAVE_LIBM
500  case 'b':
501  case 'B':
502    // TODO: can we hanle the error better?
503    if ( len < 2 )
504     return 0;
505
506    k[len - 2] = 0;
507    return powf(10, atof(k)/20.f)*fs;
508   break;
509#endif
510 }
511
512 return atoi(k);
513}
514
515int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
516 uint16_t scale = 65535;
517 int chans = 0;
518 int id;
519 int i;
520 int len;
521 int old_chans;
522 int vol_l, vol_r, vol_mono;
523 char * k;
524 struct roar_mixer_settings mixer;
525 struct roar_mixer_settings old_mixer;
526
527 if (*cur + 2 > max)
528  return -1;
529
530 id = atoi(arg[++(*cur)]);
531
532 k = arg[++(*cur)];
533
534 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
535  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
536  return -1;
537 }
538
539 if ( !strcmp(arg[*cur + 1], "scale") ) {
540  (*cur)++; // 'scale'
541  (*cur)++;
542  scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535);
543 }
544
545// TODO: clean up code here as the % vs. abs code is very duplicate...
546
547 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
548  chans = 1;
549
550  if ( *cur + 1 > max )
551   return -1;
552
553  k   = arg[++(*cur)];
554  len = strlen(k);
555
556  vol_mono = set_mixer_parse_volume(k, len, scale);
557
558  for (i = 0; i < old_chans; i++)
559   mixer.mixer[i] = vol_mono;
560
561  chans = old_chans;
562
563 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
564  chans = old_chans;
565
566  if ( *cur + 2 > max )
567   return -1;
568
569  k   = arg[++(*cur)];
570  len = strlen(k);
571
572  vol_l = set_mixer_parse_volume(k, len, scale);
573
574  k   = arg[++(*cur)];
575  len = strlen(k);
576
577  vol_r = set_mixer_parse_volume(k, len, scale);
578
579  vol_mono = (vol_l + vol_r) / 2;
580
581  mixer.mixer[0] = vol_l;
582  mixer.mixer[1] = vol_r;
583
584  switch (chans) {
585   case 1:
586     mixer.mixer[0] = vol_mono;
587    break;
588//   case 2: classic stereo...
589   case 3:
590     mixer.mixer[2] = vol_mono;
591    break;
592   case 4:
593     mixer.mixer[2] = vol_l;
594     mixer.mixer[3] = vol_r;
595    break;
596   case 5:
597     mixer.mixer[2] = vol_mono;
598     mixer.mixer[3] = vol_l;
599     mixer.mixer[4] = vol_r;
600    break;
601   case 6:
602     mixer.mixer[2] = vol_mono;
603     mixer.mixer[3] = vol_mono;
604     mixer.mixer[4] = vol_l;
605     mixer.mixer[5] = vol_r;
606    break;
607   default:
608     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
609     return -1;
610    break;
611  }
612
613 } else {
614  if ( strcmp(k, "mono") == 0 ) {
615   chans = 1;
616  } else if ( strcmp(k, "stereo") == 0 ) {
617   chans = 2;
618  } else {
619   chans = atoi(k);
620  }
621
622//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
623
624  if ( *cur + chans > max )
625   return -1;
626
627  for (i = 0; i < chans; i++) {
628   k   = arg[++(*cur)];
629   len = strlen(k);
630
631   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
632  }
633 }
634
635 mixer.scale = scale;
636
637 return roar_set_vol(con, id, &mixer, chans);
638}
639
640
641int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
642 struct roar_stream s;
643 int dir    = roar_str2dir(d_s);
644 int parent = atoi(p_s);
645
646 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
647
648 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
649  return -1;
650
651 return roar_simple_connect_virtual(con, &s, parent, dir);
652}
653
654int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
655 struct roar_meta   meta;
656 struct roar_stream s;
657 int mode_i = ROAR_META_MODE_SET;
658
659 if ( roar_stream_new_by_id(&s, id) == -1 )
660  return -1;
661
662// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
663
664 if ( strcmp(mode, "add") == 0 ) {
665  mode_i = ROAR_META_MODE_ADD;
666 }
667
668 meta.type   = roar_meta_inttype(type);
669 meta.value  = val;
670 meta.key[0] = 0;
671
672 if ( meta.type == -1 ) {
673  fprintf(stderr, "Error: unknown type: %s\n", type);
674  return -1;
675 }
676
677// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
678
679 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
680  return -1;
681
682 meta.type  = ROAR_META_TYPE_NONE;
683 meta.value = NULL;
684
685 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
686}
687
688int load_meta (struct roar_connection * con, int id, char * file) {
689 struct roar_meta   meta;
690 struct roar_stream s;
691 int mode_i = ROAR_META_MODE_SET;
692 FILE * in;
693 char lion[1024];
694 char * v;
695
696 if ( roar_stream_new_by_id(&s, id) == -1 )
697  return -1;
698
699 if ( (in = fopen(file, "r")) == NULL )
700  return -1;
701
702 while (fgets(lion, 1024, in) != NULL) {
703  if ( (v = strtok(lion, "\r\n")) != NULL )
704   if ( (v = strtok(NULL, "\r\n")) != NULL )
705    *(v-1) = 0;
706
707  if ( (v = strstr(lion, "=")) == NULL ) {
708   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
709   continue;
710  }
711
712  *v++ = 0;
713
714  meta.type   = roar_meta_inttype(lion);
715  meta.value  = v;
716  meta.key[0] = 0;
717
718  if ( meta.type == -1 ) {
719   fprintf(stderr, "Error: unknown type: %s\n", lion);
720   continue;
721  }
722
723  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
724   return -1;
725 }
726
727 fclose(in);
728
729 meta.type  = ROAR_META_TYPE_NONE;
730 meta.value = NULL;
731
732 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
733}
734
735int show_meta_type (struct roar_connection * con, int id, char * type) {
736 struct roar_meta   meta;
737 struct roar_stream s;
738
739 if ( roar_stream_new_by_id(&s, id) == -1 )
740  return -1;
741
742 meta.type  = roar_meta_inttype(type);
743
744 if ( meta.type == -1 ) {
745  fprintf(stderr, "Error: unknown type: %s\n", type);
746  return -1;
747 }
748
749 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
750  return -1;
751
752 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
753
754 roar_meta_free(&meta);
755
756 return 0;
757}
758
759int show_meta_all (struct roar_connection * con, int id) {
760 struct roar_stream s;
761 int types[ROAR_META_MAX_PER_STREAM];
762 int i;
763 int len;
764
765 if ( roar_stream_new_by_id(&s, id) == -1 )
766  return -1;
767
768 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
769  return -1;
770
771 for (i = 0; i < len; i++)
772  show_meta_type(con, id, roar_meta_strtype(types[i]));
773
774 return 0;
775}
776
777int save_meta (struct roar_connection * con, int id, char * file) {
778 struct roar_stream s;
779 struct roar_meta   meta;
780 int types[ROAR_META_MAX_PER_STREAM];
781 int i;
782 int len;
783 FILE * out;
784
785 if ( roar_stream_new_by_id(&s, id) == -1 )
786  return -1;
787
788 if ( (out = fopen(file, "w")) == NULL )
789  return -1;
790
791 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
792  return -1;
793
794 for (i = 0; i < len; i++) {
795/*
796  show_meta_type(con, id, roar_meta_strtype(types[i]));
797*/
798  meta.type  = types[i];
799
800  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
801   continue;
802
803//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
804
805  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
806
807  roar_meta_free(&meta);
808 }
809
810 fclose(out);
811
812 return 0;
813}
814
815int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
816 int f = ROAR_FLAG_NONE;
817 char * c;
818 struct roar_stream s[1];
819
820 if ( roar_stream_new_by_id(s, id) == -1 )
821  return -1;
822
823 c = strtok(flags, ",");
824 while (c != NULL) {
825  if ( !strcmp(c, "meta") ) {
826   f |= ROAR_FLAG_META;
827  } else if ( !strcmp(c, "primary") ) {
828   f |= ROAR_FLAG_PRIMARY;
829  } else if ( !strcmp(c, "sync") ) {
830   f |= ROAR_FLAG_SYNC;
831  } else if ( !strcmp(c, "cleanmeta") ) {
832   f |= ROAR_FLAG_CLEANMETA;
833  } else if ( !strcmp(c, "hwmixer") ) {
834   f |= ROAR_FLAG_HWMIXER;
835  } else if ( !strcmp(c, "pause") ) {
836   f |= ROAR_FLAG_PAUSE;
837  } else if ( !strcmp(c, "mute") ) {
838   f |= ROAR_FLAG_MUTE;
839  } else if ( !strcmp(c, "mmap") ) {
840   f |= ROAR_FLAG_MMAP;
841  } else if ( !strcmp(c, "antiecho") ) {
842   f |= ROAR_FLAG_ANTIECHO;
843  } else if ( !strcmp(c, "recsource") ) {
844   f |= ROAR_FLAG_RECSOURCE;
845  } else if ( !strcmp(c, "passmixer") ) {
846   f |= ROAR_FLAG_PASSMIXER;
847  } else if ( !strcmp(c, "virtual") ) {
848   f |= ROAR_FLAG_VIRTUAL;
849  } else if ( !strcmp(c, "prethru") ) {
850   f |= ROAR_FLAG_PRETHRU;
851  } else if ( !strcmp(c, "immutable") ) {
852   f |= ROAR_FLAG_IMMUTABLE;
853  } else if ( !strcmp(c, "enhance") ) {
854   f |= ROAR_FLAG_ENHANCE;
855  } else {
856   fprintf(stderr, "Error: unknown flag: %s\n", c);
857   return -1;
858  }
859
860  c = strtok(NULL, ",");
861 }
862
863 return roar_stream_set_flags(con, s, f, reset);
864}
865
866int main (int argc, char * argv[]) {
867 struct roar_connection con;
868 char * server   = NULL;
869 char * k = NULL;
870 int    i;
871 int    t = 0;
872
873 for (i = 1; i < argc; i++) {
874  k = argv[i];
875
876  if ( strcmp(k, "--server") == 0 ) {
877   server = argv[++i];
878  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
879   g_verbose++;
880  } else if ( strcmp(k, "--help") == 0 ) {
881   usage();
882   return 0;
883  } else if ( *k == '-' ) {
884   fprintf(stderr, "Error: unknown argument: %s\n", k);
885   usage();
886   return 1;
887  } else {
888   break;
889  }
890 }
891
892 // connect
893
894 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
895  fprintf(stderr, "Error: Can not connect to server\n");
896  return 1;
897 }
898
899 if ( i == argc ) {
900  fprintf(stderr, "Error: No Commands given\n");
901  return 0; // this is not a fatal error...
902 }
903
904 for (; i < argc; i++) {
905  k = argv[i];
906  // cmd is in k
907
908  printf("--- [ %s ] ---\n", k);
909
910  if ( !strcmp(k, "help") ) {
911   usage();
912
913  } else if ( !strcmp(k, "sleep") ) {
914   sleep(atoi(argv[++i]));
915
916  } else if ( !strcmp(k, "ping") ) {
917#ifdef ROAR_HAVE_GETTIMEOFDAY
918   if ( ping(&con, atoi(argv[++i])) == -1 ) {
919    fprintf(stderr, "Error: can not ping\n");
920   }
921#else
922    fprintf(stderr, "Error: ping not supported.\n");
923    i++;
924#endif
925
926  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
927   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
928    fprintf(stderr, "Error: can not set mode to standby\n");
929   } else {
930    printf("going into standby\n");
931   }
932  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
933   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
934    fprintf(stderr, "Error: can not set mode to active\n");
935   } else {
936    printf("going into active mode\n");
937   }
938
939  } else if ( !strcmp(k, "exit") ) {
940   if ( roar_exit(&con) == -1 ) {
941    fprintf(stderr, "Error: can not quit server\n");
942   } else {
943    printf("Server quited\n");
944    break;
945   }
946  } else if ( !strcmp(k, "terminate") ) {
947   if ( roar_terminate(&con, 1) == -1 ) {
948    fprintf(stderr, "Error: can not terminate server\n");
949   } else {
950    printf("Server got asked to quited\n");
951    break;
952   }
953
954  } else if ( !strcmp(k, "standbymode") ) {
955   t = roar_get_standby(&con);
956   if ( t == -1 ) {
957    fprintf(stderr, "Error: can not get stanby mode\n");
958   } else if ( t == ROAR_STANDBY_ACTIVE ) {
959    printf("Server is in standby\n");
960   } else if ( t == ROAR_STANDBY_INACTIVE ) {
961    printf("Server is active\n");
962   } else {
963    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
964   }
965
966  } else if ( !strcmp(k, "whoami") ) {
967   printf("My client ID is: %i\n", roar_get_clientid(&con));
968  } else if ( !strcmp(k, "serveroinfo") ) {
969   server_oinfo(&con);
970  } else if ( !strcmp(k, "listclients") ) {
971   list_clients(&con);
972  } else if ( !strcmp(k, "liststreams") ) {
973   list_streams(&con);
974  } else if ( !strcmp(k, "allinfo") ) {
975   server_oinfo(&con);
976   printf("\n");
977   list_clients(&con);
978   printf("\n");
979   list_streams(&con);
980
981  } else if ( !strcmp(k, "kick") ) {
982   k = argv[++i];
983   if ( !strcmp(k, "client") ) {
984    t = ROAR_OT_CLIENT;
985   } else if ( !strcmp(k, "stream") ) {
986    t = ROAR_OT_STREAM;
987   } else if ( !strcmp(k, "sample") ) {
988    t = ROAR_OT_SAMPLE;
989   } else if ( !strcmp(k, "source") ) {
990    t = ROAR_OT_SOURCE;
991   } else {
992    fprintf(stderr, "Error: unknown type: %s\n", k);
993    continue;
994   }
995   //t = atoi(argv[i++]);
996   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
997    fprintf(stderr, "Error: can not kick %s\n", k);
998   } else {
999    printf("%s kicked\n", k);
1000   }
1001
1002  } else if ( !strcmp(k, "newvirtual") ) {
1003   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
1004    fprintf(stderr, "Error: can not create new virtual stream\n");
1005   } else {
1006    printf("virtual stream created\n");
1007   }
1008   i += 6;
1009
1010  } else if ( !strcmp(k, "volume") ) {
1011   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
1012    fprintf(stderr, "Error: can not set volume\n");
1013   } else {
1014    printf("volume changed\n");
1015   }
1016
1017  } else if ( !strcmp(k, "flag") ) {
1018   i++;
1019   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
1020    fprintf(stderr, "Error: can not set flags\n");
1021   } else {
1022    printf("flags changed\n");
1023   }
1024   i++;
1025  } else if ( !strcmp(k, "unflag") ) {
1026   i++;
1027   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
1028    fprintf(stderr, "Error: can not reset flags\n");
1029   } else {
1030    printf("flags changed\n");
1031   }
1032   i++;
1033  } else if ( !strcmp(k, "metaset") ) {
1034   i++;
1035   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1036    fprintf(stderr, "Error: can not set meta data\n");
1037   } else {
1038    printf("meta data changed\n");
1039   }
1040   i += 3;
1041  } else if ( !strcmp(k, "metaget") ) {
1042   i++;
1043   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1044    fprintf(stderr, "Error: can not get meta data\n");
1045   }
1046   i++;
1047  } else if ( !strcmp(k, "metasave") ) {
1048   i++;
1049   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1050    fprintf(stderr, "Error: can not get meta data\n");
1051   } else {
1052    printf("meta data saved\n");
1053   }
1054   i++;
1055  } else if ( !strcmp(k, "metaload") ) {
1056   i++;
1057   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1058    fprintf(stderr, "Error: can not set meta data\n");
1059   } else {
1060    printf("meta data saved\n");
1061   }
1062   i++;
1063
1064  } else {
1065   fprintf(stderr, "Error: invalid command: %s\n", k);
1066  }
1067
1068 }
1069
1070 roar_disconnect(&con);
1071
1072 return 0;
1073}
1074
1075//ll
Note: See TracBrowser for help on using the repository browser.