source: roaraudio/roarclients/roarctl.c @ 3787:81e536463f01

Last change on this file since 3787:81e536463f01 was 3787:81e536463f01, checked in by phi, 14 years ago

get some warnings away on win32

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