source: roaraudio/roarclients/roarctl.c @ 4412:4c720a643e75

Last change on this file since 4412:4c720a643e75 was 4328:dd99a1c382a6, checked in by phi, 14 years ago

use roar_str2ot() function for object types

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