source: roaraudio/roarclients/roarctl.c @ 4024:7b6b2eadfbb4

Last change on this file since 4024:7b6b2eadfbb4 was 4014:2fee6bf2dc0c, checked in by phi, 14 years ago

fixed some resurce leaks

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
[4014]738  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) {
739   fclose(in);
[1126]740   return -1;
[4014]741  }
[1126]742 }
743
744 fclose(in);
745
746 meta.type  = ROAR_META_TYPE_NONE;
747 meta.value = NULL;
748
749 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
750}
751
[106]752int show_meta_type (struct roar_connection * con, int id, char * type) {
753 struct roar_meta   meta;
754 struct roar_stream s;
755
[3072]756 if ( roar_stream_new_by_id(&s, id) == -1 )
757  return -1;
[106]758
[109]759 meta.type  = roar_meta_inttype(type);
760
761 if ( meta.type == -1 ) {
762  fprintf(stderr, "Error: unknown type: %s\n", type);
763  return -1;
764 }
[106]765
766 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
767  return -1;
768
[112]769 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
[106]770
771 roar_meta_free(&meta);
772
773 return 0;
774}
775
[112]776int show_meta_all (struct roar_connection * con, int id) {
777 struct roar_stream s;
778 int types[ROAR_META_MAX_PER_STREAM];
779 int i;
780 int len;
781
[3072]782 if ( roar_stream_new_by_id(&s, id) == -1 )
783  return -1;
[112]784
785 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
786  return -1;
787
788 for (i = 0; i < len; i++)
789  show_meta_type(con, id, roar_meta_strtype(types[i]));
790
791 return 0;
792}
793
[1126]794int save_meta (struct roar_connection * con, int id, char * file) {
795 struct roar_stream s;
796 struct roar_meta   meta;
797 int types[ROAR_META_MAX_PER_STREAM];
798 int i;
799 int len;
800 FILE * out;
801
[3072]802 if ( roar_stream_new_by_id(&s, id) == -1 )
803  return -1;
[1126]804
[4014]805 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
[1126]806  return -1;
807
[4014]808 if ( (out = fopen(file, "w")) == NULL )
[1126]809  return -1;
810
811 for (i = 0; i < len; i++) {
812/*
813  show_meta_type(con, id, roar_meta_strtype(types[i]));
814*/
815  meta.type  = types[i];
816
817  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
818   continue;
819
820//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
821
822  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
823
824  roar_meta_free(&meta);
825 }
826
827 fclose(out);
828
829 return 0;
830}
831
[1043]832int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
833 int f = ROAR_FLAG_NONE;
834 char * c;
835 struct roar_stream s[1];
836
[3072]837 if ( roar_stream_new_by_id(s, id) == -1 )
838  return -1;
[1043]839
840 c = strtok(flags, ",");
841 while (c != NULL) {
842  if ( !strcmp(c, "meta") ) {
843   f |= ROAR_FLAG_META;
844  } else if ( !strcmp(c, "primary") ) {
845   f |= ROAR_FLAG_PRIMARY;
[1116]846  } else if ( !strcmp(c, "sync") ) {
847   f |= ROAR_FLAG_SYNC;
[1219]848  } else if ( !strcmp(c, "cleanmeta") ) {
849   f |= ROAR_FLAG_CLEANMETA;
[1585]850  } else if ( !strcmp(c, "hwmixer") ) {
851   f |= ROAR_FLAG_HWMIXER;
852  } else if ( !strcmp(c, "pause") ) {
853   f |= ROAR_FLAG_PAUSE;
[1883]854  } else if ( !strcmp(c, "mute") ) {
855   f |= ROAR_FLAG_MUTE;
[1926]856  } else if ( !strcmp(c, "mmap") ) {
857   f |= ROAR_FLAG_MMAP;
[2154]858  } else if ( !strcmp(c, "antiecho") ) {
859   f |= ROAR_FLAG_ANTIECHO;
[2412]860  } else if ( !strcmp(c, "recsource") ) {
861   f |= ROAR_FLAG_RECSOURCE;
862  } else if ( !strcmp(c, "passmixer") ) {
863   f |= ROAR_FLAG_PASSMIXER;
[2627]864  } else if ( !strcmp(c, "virtual") ) {
865   f |= ROAR_FLAG_VIRTUAL;
[2814]866  } else if ( !strcmp(c, "prethru") ) {
867   f |= ROAR_FLAG_PRETHRU;
[2953]868  } else if ( !strcmp(c, "immutable") ) {
869   f |= ROAR_FLAG_IMMUTABLE;
870  } else if ( !strcmp(c, "enhance") ) {
871   f |= ROAR_FLAG_ENHANCE;
[1043]872  } else {
873   fprintf(stderr, "Error: unknown flag: %s\n", c);
874   return -1;
875  }
876
877  c = strtok(NULL, ",");
878 }
879
880 return roar_stream_set_flags(con, s, f, reset);
881}
882
[3942]883int show_aiprofile (const char * profile) {
884 struct roar_audio_info info;
885
886 if ( roar_profile2info(&info, profile) == -1 ) {
887  fprintf(stderr, "Error: unknown profile: %s\n", profile);
888  return -1;
889 }
890
891 printf("Profile Name          : %s\n", profile);
[3944]892
893 if ( info.rate )
894  printf("Profile sample rate   : %i\n", info.rate);
895 if ( info.bits )
896  printf("Profile bits          : %i\n", info.bits);
897 if ( info.channels )
898  printf("Profile channels      : %i\n", info.channels);
899
[3942]900 printf("Profile codec         : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
901                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
902
903 return 0;
904}
905
906int list_aiprofiles (void) {
907 const char * list[1024];
908 ssize_t ret;
909 ssize_t i;
910
911 ret = roar_profiles_list(list, 1024, 0);
912
913 if ( ret == -1 ) {
914  fprintf(stderr, "Error: can not read list of profiles\n");
915  return -1;
916 }
917
918 for (i = 0; i < ret; i++) {
919  printf("profile %lli:\n", (long long signed int)i);
920  show_aiprofile(list[i]);
921 }
922
923 return 0;
924}
925
[0]926int main (int argc, char * argv[]) {
927 struct roar_connection con;
928 char * server   = NULL;
929 char * k = NULL;
930 int    i;
931 int    t = 0;
932
933 for (i = 1; i < argc; i++) {
934  k = argv[i];
935
936  if ( strcmp(k, "--server") == 0 ) {
[58]937   server = argv[++i];
[963]938  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
939   g_verbose++;
[0]940  } else if ( strcmp(k, "--help") == 0 ) {
941   usage();
942   return 0;
[3960]943  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
944   list_aiprofiles();
945   return 0;
[0]946  } else if ( *k == '-' ) {
947   fprintf(stderr, "Error: unknown argument: %s\n", k);
948   usage();
949   return 1;
950  } else {
951   break;
952  }
953 }
954
955 // connect
956
[3510]957 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
[0]958  fprintf(stderr, "Error: Can not connect to server\n");
959  return 1;
960 }
961
962 if ( i == argc ) {
963  fprintf(stderr, "Error: No Commands given\n");
964  return 0; // this is not a fatal error...
965 }
966
967 for (; i < argc; i++) {
968  k = argv[i];
969  // cmd is in k
970
971  printf("--- [ %s ] ---\n", k);
972
973  if ( !strcmp(k, "help") ) {
974   usage();
975
[1202]976  } else if ( !strcmp(k, "sleep") ) {
977   sleep(atoi(argv[++i]));
[0]978
[1630]979  } else if ( !strcmp(k, "ping") ) {
[1781]980#ifdef ROAR_HAVE_GETTIMEOFDAY
[1630]981   if ( ping(&con, atoi(argv[++i])) == -1 ) {
982    fprintf(stderr, "Error: can not ping\n");
983   }
[1781]984#else
985    fprintf(stderr, "Error: ping not supported.\n");
986    i++;
987#endif
[1630]988
[0]989  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
990   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
991    fprintf(stderr, "Error: can not set mode to standby\n");
992   } else {
993    printf("going into standby\n");
994   }
995  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
996   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
997    fprintf(stderr, "Error: can not set mode to active\n");
998   } else {
999    printf("going into active mode\n");
1000   }
1001
1002  } else if ( !strcmp(k, "exit") ) {
1003   if ( roar_exit(&con) == -1 ) {
1004    fprintf(stderr, "Error: can not quit server\n");
1005   } else {
1006    printf("Server quited\n");
1007    break;
1008   }
[577]1009  } else if ( !strcmp(k, "terminate") ) {
1010   if ( roar_terminate(&con, 1) == -1 ) {
1011    fprintf(stderr, "Error: can not terminate server\n");
1012   } else {
1013    printf("Server got asked to quited\n");
1014    break;
1015   }
[0]1016
1017  } else if ( !strcmp(k, "standbymode") ) {
1018   t = roar_get_standby(&con);
1019   if ( t == -1 ) {
1020    fprintf(stderr, "Error: can not get stanby mode\n");
1021   } else if ( t == ROAR_STANDBY_ACTIVE ) {
1022    printf("Server is in standby\n");
1023   } else if ( t == ROAR_STANDBY_INACTIVE ) {
1024    printf("Server is active\n");
1025   } else {
1026    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
1027   }
1028
[1162]1029  } else if ( !strcmp(k, "whoami") ) {
1030   printf("My client ID is: %i\n", roar_get_clientid(&con));
[0]1031  } else if ( !strcmp(k, "serveroinfo") ) {
1032   server_oinfo(&con);
1033  } else if ( !strcmp(k, "listclients") ) {
1034   list_clients(&con);
1035  } else if ( !strcmp(k, "liststreams") ) {
1036   list_streams(&con);
1037  } else if ( !strcmp(k, "allinfo") ) {
1038   server_oinfo(&con);
1039   printf("\n");
1040   list_clients(&con);
1041   printf("\n");
1042   list_streams(&con);
1043
1044  } else if ( !strcmp(k, "kick") ) {
1045   k = argv[++i];
1046   if ( !strcmp(k, "client") ) {
1047    t = ROAR_OT_CLIENT;
1048   } else if ( !strcmp(k, "stream") ) {
1049    t = ROAR_OT_STREAM;
1050   } else if ( !strcmp(k, "sample") ) {
1051    t = ROAR_OT_SAMPLE;
1052   } else if ( !strcmp(k, "source") ) {
1053    t = ROAR_OT_SOURCE;
1054   } else {
1055    fprintf(stderr, "Error: unknown type: %s\n", k);
1056    continue;
1057   }
1058   //t = atoi(argv[i++]);
1059   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
1060    fprintf(stderr, "Error: can not kick %s\n", k);
1061   } else {
1062    printf("%s kicked\n", k);
1063   }
1064
[2698]1065  } else if ( !strcmp(k, "newvirtual") ) {
[2699]1066   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
[2698]1067    fprintf(stderr, "Error: can not create new virtual stream\n");
1068   } else {
1069    printf("virtual stream created\n");
1070   }
[2699]1071   i += 6;
[2698]1072
[85]1073  } else if ( !strcmp(k, "volume") ) {
1074   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
1075    fprintf(stderr, "Error: can not set volume\n");
1076   } else {
1077    printf("volume changed\n");
1078   }
1079
[1043]1080  } else if ( !strcmp(k, "flag") ) {
1081   i++;
1082   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
1083    fprintf(stderr, "Error: can not set flags\n");
1084   } else {
1085    printf("flags changed\n");
1086   }
1087   i++;
1088  } else if ( !strcmp(k, "unflag") ) {
1089   i++;
1090   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
1091    fprintf(stderr, "Error: can not reset flags\n");
1092   } else {
1093    printf("flags changed\n");
1094   }
1095   i++;
[106]1096  } else if ( !strcmp(k, "metaset") ) {
[99]1097   i++;
1098   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1099    fprintf(stderr, "Error: can not set meta data\n");
1100   } else {
1101    printf("meta data changed\n");
1102   }
1103   i += 3;
[106]1104  } else if ( !strcmp(k, "metaget") ) {
1105   i++;
1106   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1107    fprintf(stderr, "Error: can not get meta data\n");
1108   }
1109   i++;
[1126]1110  } else if ( !strcmp(k, "metasave") ) {
1111   i++;
1112   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1113    fprintf(stderr, "Error: can not get meta data\n");
1114   } else {
1115    printf("meta data saved\n");
1116   }
1117   i++;
1118  } else if ( !strcmp(k, "metaload") ) {
1119   i++;
1120   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1121    fprintf(stderr, "Error: can not set meta data\n");
1122   } else {
1123    printf("meta data saved\n");
1124   }
1125   i++;
[99]1126
[3942]1127
[3961]1128  } else if ( !strcmp(k, "listaiprofiles") || !strcmp(k, "listprofiles") ) {
[3942]1129   if ( list_aiprofiles() == -1 ) {
1130    fprintf(stderr, "Error: can not list profiles\n");
1131   }
[3961]1132  } else if ( !strcmp(k, "aiprofileget") || !strcmp(k, "profileget") ) {
[3942]1133   i++;
1134   if ( show_aiprofile(argv[i]) == -1 ) {
1135    fprintf(stderr, "Error: can not get profile data\n");
1136   }
[0]1137  } else {
1138   fprintf(stderr, "Error: invalid command: %s\n", k);
1139  }
1140
1141 }
1142
1143 roar_disconnect(&con);
1144
1145 return 0;
1146}
1147
1148//ll
Note: See TracBrowser for help on using the repository browser.