source: roaraudio/roarclients/roarctl.c @ 4432:d2ded197053f

Last change on this file since 4432:d2ded197053f was 4432:d2ded197053f, checked in by phi, 14 years ago

very basic support for serverinfo

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