source: roaraudio/roarclients/roarctl.c @ 2698:60d1738a6d5c

Last change on this file since 2698:60d1738a6d5c was 2698:60d1738a6d5c, checked in by phi, 15 years ago

support to create new virtual stream

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