source: roaraudio/roarclients/roarctl.c @ 3539:1fd530767d7b

Last change on this file since 3539:1fd530767d7b was 3539:1fd530767d7b, checked in by phi, 14 years ago

support to read channel map from server

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