source: roaraudio/roarclients/roarctl.c @ 4012:28bea7336add

Last change on this file since 4012:28bea7336add was 4012:28bea7336add, checked in by phi, 14 years ago

typos

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