source: roaraudio/roarclients/roarctl.c @ 4434:ff895ca43976

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

added support to print all possible values from the info struct

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