source: roaraudio/roarclients/roarctl.c @ 3944:631ee4d9fc93

Last change on this file since 3944:631ee4d9fc93 was 3944:631ee4d9fc93, checked in by phi, 14 years ago

corrected display of profiles

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