source: roaraudio/roarclients/roarctl.c @ 2738:6ac10ee7a131

Last change on this file since 2738:6ac10ee7a131 was 2738:6ac10ee7a131, checked in by phi, 15 years ago

volume change support: stereo -> 3.1, 5.1, 6.1

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