source: roaraudio/roarclients/roarctl.c @ 2699:07978c63cd59

Last change on this file since 2699:07978c63cd59 was 2699:07978c63cd59, checked in by phi, 15 years ago

got it working

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 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
526
527 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
528  return -1;
529
530 return roar_simple_connect_virtual(con, &s, parent, dir);
531}
532
533int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
534 struct roar_meta   meta;
535 struct roar_stream s;
536 int mode_i = ROAR_META_MODE_SET;
537
538 memset(&s, 0, sizeof(s));
539
540 s.id = id;
541
542// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
543
544 if ( strcmp(mode, "add") == 0 ) {
545  mode_i = ROAR_META_MODE_ADD;
546 }
547
548 meta.type   = roar_meta_inttype(type);
549 meta.value  = val;
550 meta.key[0] = 0;
551
552 if ( meta.type == -1 ) {
553  fprintf(stderr, "Error: unknown type: %s\n", type);
554  return -1;
555 }
556
557// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
558
559 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
560  return -1;
561
562 meta.type  = ROAR_META_TYPE_NONE;
563 meta.value = NULL;
564
565 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
566}
567
568int load_meta (struct roar_connection * con, int id, char * file) {
569 struct roar_meta   meta;
570 struct roar_stream s;
571 int mode_i = ROAR_META_MODE_SET;
572 FILE * in;
573 char lion[1024];
574 char * v;
575
576 memset(&s, 0, sizeof(s));
577
578 s.id = id;
579
580 if ( (in = fopen(file, "r")) == NULL )
581  return -1;
582
583 while (fgets(lion, 1024, in) != NULL) {
584  if ( (v = strtok(lion, "\r\n")) != NULL )
585   if ( (v = strtok(NULL, "\r\n")) != NULL )
586    *(v-1) = 0;
587
588  if ( (v = strstr(lion, "=")) == NULL ) {
589   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
590   continue;
591  }
592
593  *v++ = 0;
594
595  meta.type   = roar_meta_inttype(lion);
596  meta.value  = v;
597  meta.key[0] = 0;
598
599  if ( meta.type == -1 ) {
600   fprintf(stderr, "Error: unknown type: %s\n", lion);
601   continue;
602  }
603
604  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
605   return -1;
606 }
607
608 fclose(in);
609
610 meta.type  = ROAR_META_TYPE_NONE;
611 meta.value = NULL;
612
613 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
614}
615
616int show_meta_type (struct roar_connection * con, int id, char * type) {
617 struct roar_meta   meta;
618 struct roar_stream s;
619
620 memset(&s, 0, sizeof(s));
621
622 s.id = id;
623
624 meta.type  = roar_meta_inttype(type);
625
626 if ( meta.type == -1 ) {
627  fprintf(stderr, "Error: unknown type: %s\n", type);
628  return -1;
629 }
630
631 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
632  return -1;
633
634 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
635
636 roar_meta_free(&meta);
637
638 return 0;
639}
640
641int show_meta_all (struct roar_connection * con, int id) {
642 struct roar_stream s;
643 int types[ROAR_META_MAX_PER_STREAM];
644 int i;
645 int len;
646
647 memset(&s, 0, sizeof(s));
648
649 s.id = id;
650
651 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
652  return -1;
653
654 for (i = 0; i < len; i++)
655  show_meta_type(con, id, roar_meta_strtype(types[i]));
656
657 return 0;
658}
659
660int save_meta (struct roar_connection * con, int id, char * file) {
661 struct roar_stream s;
662 struct roar_meta   meta;
663 int types[ROAR_META_MAX_PER_STREAM];
664 int i;
665 int len;
666 FILE * out;
667
668 memset(&s, 0, sizeof(s));
669
670 s.id = id;
671
672 if ( (out = fopen(file, "w")) == NULL )
673  return -1;
674
675 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
676  return -1;
677
678 for (i = 0; i < len; i++) {
679/*
680  show_meta_type(con, id, roar_meta_strtype(types[i]));
681*/
682  meta.type  = types[i];
683
684  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
685   continue;
686
687//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
688
689  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
690
691  roar_meta_free(&meta);
692 }
693
694 fclose(out);
695
696 return 0;
697}
698
699int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
700 int f = ROAR_FLAG_NONE;
701 char * c;
702 struct roar_stream s[1];
703
704 memset(s, 0, sizeof(struct roar_stream));
705
706 s->id = id;
707
708 c = strtok(flags, ",");
709 while (c != NULL) {
710  if ( !strcmp(c, "meta") ) {
711   f |= ROAR_FLAG_META;
712  } else if ( !strcmp(c, "primary") ) {
713   f |= ROAR_FLAG_PRIMARY;
714  } else if ( !strcmp(c, "sync") ) {
715   f |= ROAR_FLAG_SYNC;
716  } else if ( !strcmp(c, "cleanmeta") ) {
717   f |= ROAR_FLAG_CLEANMETA;
718  } else if ( !strcmp(c, "hwmixer") ) {
719   f |= ROAR_FLAG_HWMIXER;
720  } else if ( !strcmp(c, "pause") ) {
721   f |= ROAR_FLAG_PAUSE;
722  } else if ( !strcmp(c, "mute") ) {
723   f |= ROAR_FLAG_MUTE;
724  } else if ( !strcmp(c, "mmap") ) {
725   f |= ROAR_FLAG_MMAP;
726  } else if ( !strcmp(c, "antiecho") ) {
727   f |= ROAR_FLAG_ANTIECHO;
728  } else if ( !strcmp(c, "recsource") ) {
729   f |= ROAR_FLAG_RECSOURCE;
730  } else if ( !strcmp(c, "passmixer") ) {
731   f |= ROAR_FLAG_PASSMIXER;
732  } else if ( !strcmp(c, "virtual") ) {
733   f |= ROAR_FLAG_VIRTUAL;
734  } else {
735   fprintf(stderr, "Error: unknown flag: %s\n", c);
736   return -1;
737  }
738
739  c = strtok(NULL, ",");
740 }
741
742 return roar_stream_set_flags(con, s, f, reset);
743}
744
745int main (int argc, char * argv[]) {
746 struct roar_connection con;
747 char * server   = NULL;
748 char * k = NULL;
749 int    i;
750 int    t = 0;
751
752 for (i = 1; i < argc; i++) {
753  k = argv[i];
754
755  if ( strcmp(k, "--server") == 0 ) {
756   server = argv[++i];
757  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
758   g_verbose++;
759  } else if ( strcmp(k, "--help") == 0 ) {
760   usage();
761   return 0;
762  } else if ( *k == '-' ) {
763   fprintf(stderr, "Error: unknown argument: %s\n", k);
764   usage();
765   return 1;
766  } else {
767   break;
768  }
769 }
770
771 // connect
772
773 if ( roar_connect(&con, server) == -1 ) {
774  fprintf(stderr, "Error: Can not connect to server\n");
775  return 1;
776 }
777
778 if ( roar_identify(&con, "roarctl") == -1 ) {
779  fprintf(stderr, "Error: Can not identify to server\n");
780  return 1;
781 }
782
783 if ( i == argc ) {
784  fprintf(stderr, "Error: No Commands given\n");
785  return 0; // this is not a fatal error...
786 }
787
788 for (; i < argc; i++) {
789  k = argv[i];
790  // cmd is in k
791
792  printf("--- [ %s ] ---\n", k);
793
794  if ( !strcmp(k, "help") ) {
795   usage();
796
797  } else if ( !strcmp(k, "sleep") ) {
798   sleep(atoi(argv[++i]));
799
800  } else if ( !strcmp(k, "ping") ) {
801#ifdef ROAR_HAVE_GETTIMEOFDAY
802   if ( ping(&con, atoi(argv[++i])) == -1 ) {
803    fprintf(stderr, "Error: can not ping\n");
804   }
805#else
806    fprintf(stderr, "Error: ping not supported.\n");
807    i++;
808#endif
809
810  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
811   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
812    fprintf(stderr, "Error: can not set mode to standby\n");
813   } else {
814    printf("going into standby\n");
815   }
816  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
817   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
818    fprintf(stderr, "Error: can not set mode to active\n");
819   } else {
820    printf("going into active mode\n");
821   }
822
823  } else if ( !strcmp(k, "exit") ) {
824   if ( roar_exit(&con) == -1 ) {
825    fprintf(stderr, "Error: can not quit server\n");
826   } else {
827    printf("Server quited\n");
828    break;
829   }
830  } else if ( !strcmp(k, "terminate") ) {
831   if ( roar_terminate(&con, 1) == -1 ) {
832    fprintf(stderr, "Error: can not terminate server\n");
833   } else {
834    printf("Server got asked to quited\n");
835    break;
836   }
837
838  } else if ( !strcmp(k, "standbymode") ) {
839   t = roar_get_standby(&con);
840   if ( t == -1 ) {
841    fprintf(stderr, "Error: can not get stanby mode\n");
842   } else if ( t == ROAR_STANDBY_ACTIVE ) {
843    printf("Server is in standby\n");
844   } else if ( t == ROAR_STANDBY_INACTIVE ) {
845    printf("Server is active\n");
846   } else {
847    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
848   }
849
850  } else if ( !strcmp(k, "whoami") ) {
851   printf("My client ID is: %i\n", roar_get_clientid(&con));
852  } else if ( !strcmp(k, "serveroinfo") ) {
853   server_oinfo(&con);
854  } else if ( !strcmp(k, "listclients") ) {
855   list_clients(&con);
856  } else if ( !strcmp(k, "liststreams") ) {
857   list_streams(&con);
858  } else if ( !strcmp(k, "allinfo") ) {
859   server_oinfo(&con);
860   printf("\n");
861   list_clients(&con);
862   printf("\n");
863   list_streams(&con);
864
865  } else if ( !strcmp(k, "kick") ) {
866   k = argv[++i];
867   if ( !strcmp(k, "client") ) {
868    t = ROAR_OT_CLIENT;
869   } else if ( !strcmp(k, "stream") ) {
870    t = ROAR_OT_STREAM;
871   } else if ( !strcmp(k, "sample") ) {
872    t = ROAR_OT_SAMPLE;
873   } else if ( !strcmp(k, "source") ) {
874    t = ROAR_OT_SOURCE;
875   } else {
876    fprintf(stderr, "Error: unknown type: %s\n", k);
877    continue;
878   }
879   //t = atoi(argv[i++]);
880   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
881    fprintf(stderr, "Error: can not kick %s\n", k);
882   } else {
883    printf("%s kicked\n", k);
884   }
885
886  } else if ( !strcmp(k, "newvirtual") ) {
887   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
888    fprintf(stderr, "Error: can not create new virtual stream\n");
889   } else {
890    printf("virtual stream created\n");
891   }
892   i += 6;
893
894  } else if ( !strcmp(k, "volume") ) {
895   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
896    fprintf(stderr, "Error: can not set volume\n");
897   } else {
898    printf("volume changed\n");
899   }
900
901  } else if ( !strcmp(k, "flag") ) {
902   i++;
903   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
904    fprintf(stderr, "Error: can not set flags\n");
905   } else {
906    printf("flags changed\n");
907   }
908   i++;
909  } else if ( !strcmp(k, "unflag") ) {
910   i++;
911   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
912    fprintf(stderr, "Error: can not reset flags\n");
913   } else {
914    printf("flags changed\n");
915   }
916   i++;
917  } else if ( !strcmp(k, "metaset") ) {
918   i++;
919   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
920    fprintf(stderr, "Error: can not set meta data\n");
921   } else {
922    printf("meta data changed\n");
923   }
924   i += 3;
925  } else if ( !strcmp(k, "metaget") ) {
926   i++;
927   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
928    fprintf(stderr, "Error: can not get meta data\n");
929   }
930   i++;
931  } else if ( !strcmp(k, "metasave") ) {
932   i++;
933   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
934    fprintf(stderr, "Error: can not get meta data\n");
935   } else {
936    printf("meta data saved\n");
937   }
938   i++;
939  } else if ( !strcmp(k, "metaload") ) {
940   i++;
941   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
942    fprintf(stderr, "Error: can not set meta data\n");
943   } else {
944    printf("meta data saved\n");
945   }
946   i++;
947
948  } else {
949   fprintf(stderr, "Error: invalid command: %s\n", k);
950  }
951
952 }
953
954 roar_disconnect(&con);
955
956 return 0;
957}
958
959//ll
Note: See TracBrowser for help on using the repository browser.