source: roaraudio/roarclients/roarctl.c @ 4413:827dec01806f

Last change on this file since 4413:827dec01806f was 4413:827dec01806f, checked in by phi, 13 years ago

added support to enum servers with roarctl

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