source: roaraudio/roarclients/roarctl.c @ 3584:562486cb67e9

Last change on this file since 3584:562486cb67e9 was 3560:ea1d10e318e6, checked in by phi, 14 years ago

added scale option

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