source: roaraudio/roarclients/roarctl.c @ 4503:86f76891c2ab

Last change on this file since 4503:86f76891c2ab was 4503:86f76891c2ab, checked in by phi, 14 years ago

support names an version zero

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