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

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

added support to print all possible values from the info struct

File size: 31.9 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        "  serveroinfo             - Gets Information about server output\n"
114        "  listclients             - Gets Information about clients\n"
115        "  liststreams             - Gets Information about streams\n"
116        "  allinfo                 - Get all infos\n"
117       );
118}
119
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
150#ifdef ROAR_HAVE_GETTIMEOFDAY
151int ping (struct roar_connection * con, int num) {
152 struct timeval         try, ans;
153 struct roar_message    m;
154 register int           ret;
155 int                    i;
156 double                 cur, min = 3600*1000, max = 0, sum = 0;
157
158 if ( num == 0 )
159  return 0;
160
161 for (i = 0; i < num; i++) {
162  memset(&m, 0, sizeof(m));
163
164  m.cmd = ROAR_CMD_NOOP;
165  m.datalen = 0;
166
167  // we use roar_req() directly here because of speed.
168  // roar_noop() does basicly the same but is a bit slower.
169
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
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;
190
191  if ( i != (num - 1) )
192   sleep(1);
193 }
194
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
199 return 0;
200}
201#endif
202
203#define _pm(m,n) if ( info->m != NULL ) printf("Server %-15s: %s\n", (n), (info->m));
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
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");
223
224 roar_server_info_free(info);
225}
226#undef _pm
227
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);
243  if ( g_verbose > 1 && s.pos != (uint32_t)-1 )
244   printf("Server Position       : %lu S (%.3fs)\n", (unsigned long int) s.pos, (float)s.pos/(s.info.rate*s.info.channels));
245}
246
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
253 snprintf(file, sizeof(file)-1, "/proc/%i/exe", pid);
254 file[sizeof(file)-1] = 0;
255
256 ret[0] = '?';
257 ret[1] = 0;
258
259 if ( (i = readlink(file, buf, sizeof(buf)-1)) != -1 ) {
260  buf[i] = 0;
261  if ( (r = strrchr(buf, '/')) != NULL ) {
262   r++;
263   if ( *r != 0 ) {
264    strncpy(ret, r, sizeof(ret)-1);
265    ret[sizeof(ret)-1] = 0;
266   }
267  }
268 }
269#else
270 (void)pid;
271#endif
272
273 return ret;
274}
275
276void list_clients (struct roar_connection * con) {
277 int i;
278 int num;
279 int h;
280 int id[ROAR_CLIENTS_MAX];
281 char tmp[80];
282 int self_id;
283 struct roar_client self_client;
284 struct roar_client c;
285#ifdef _POSIX_USERS
286 struct group  * grp = NULL;
287 struct passwd * pwd = NULL;
288#endif
289
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
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  }
306
307  if ( c.name[0] != '\0' )
308   printf("Client name           : %s\n", c.name);
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
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   }
322  }
323  if ( c.uid != -1 ) {
324#ifdef _POSIX_USERS
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 {
330#else
331   if ( 1 ) {
332#endif
333    printf("Client UID/GID        : %i/%i\n", c.uid, c.gid);
334   }
335  }
336
337  if ( g_verbose && c.proto != ROAR_PROTO_NONE ) {
338   printf("Client Protocol       : %s\n", roar_proto2str(c.proto));
339  }
340
341  if ( g_verbose && c.byteorder != ROAR_BYTEORDER_UNKNOWN ) {
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
359   printf("Client Byteorder      : %s%s\n", roar_byteorder2str(c.byteorder), tmp);
360  }
361
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];
376 char chanmap[ROAR_MAX_CHANNELS];
377 struct roar_stream s;
378 struct roar_stream_info info;
379 char buffer[1024];
380 char * flags = buffer;
381 char * name  = buffer;
382 char * infotext;
383 size_t len;
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));
398
399  if ( roar_stream_get_name(con, &s, name, 1024) == 0 )
400   printf("Stream name           : %s\n", name);
401
402  if ( (int)s.pos_rel_id == -1 ) {
403   printf("Relativ position id   : none (stream not synchronized)\n");
404  } else if ( (int)s.pos_rel_id == id[i] ) {
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  }
409  if ( g_verbose > 1 && s.pos != (uint32_t)-1 ) {
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  }
417
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 )
433   printf("Stream bits           : %i\n", s.info.bits);
434  if ( s.info.channels )
435  printf("Stream channels       : %i\n", s.info.channels);
436
437  printf("Stream codec          : %2i (%s%s)\n", s.info.codec, roar_codec2str(s.info.codec),
438                                       s.info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
439  if ( roar_stream_get_info(con, &s, &info) != -1 ) {
440   if ( info.codec != s.info.codec ) {
441    printf("Streamed codec        : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
442                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
443   }
444
445   if ( g_verbose ) {
446    if ( info.block_size )
447     printf("Stream block size     : %i Byte\n", info.block_size);
448
449    printf("Underruns pre/post    : %i/%i\n",   info.pre_underruns, info.post_underruns);
450    if ( g_verbose > 1 )
451     printf("Stream delay          : %ims (%.2fm)\n",   (int)info.delay/1000, (info.delay*(float)_SPEED_OF_SOUND));
452
453    if ( g_verbose > 1 )
454     printf("Stream mixer          : %i\n",   info.mixer);
455
456    if ( g_verbose > 1 )
457     printf("Stream state          : %s\n",   roar_streamstate2str(info.state));
458
459    if ( g_verbose > 1 )
460     printf("Stream role           : %s\n",   roar_role2str(info.role));
461
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 ");
471    if ( info.flags & ROAR_FLAG_META )
472     strcat(flags, "meta ");
473    if ( info.flags & ROAR_FLAG_AUTOCONF )
474     strcat(flags, "autoconf ");
475    if ( info.flags & ROAR_FLAG_CLEANMETA )
476     strcat(flags, "cleanmeta ");
477    if ( info.flags & ROAR_FLAG_HWMIXER )
478     strcat(flags, "hwmixer ");
479    if ( info.flags & ROAR_FLAG_PAUSE )
480     strcat(flags, "pause ");
481    if ( info.flags & ROAR_FLAG_MUTE )
482     strcat(flags, "mute ");
483    if ( info.flags & ROAR_FLAG_MMAP )
484     strcat(flags, "mmap ");
485    if ( info.flags & ROAR_FLAG_ANTIECHO )
486     strcat(flags, "antiecho ");
487    if ( info.flags & ROAR_FLAG_VIRTUAL )
488     strcat(flags, "virtual ");
489    if ( info.flags & ROAR_FLAG_RECSOURCE )
490     strcat(flags, "recsource ");
491    if ( info.flags & ROAR_FLAG_PASSMIXER )
492     strcat(flags, "passmixer ");
493    if ( info.flags & ROAR_FLAG_PRETHRU )
494     strcat(flags, "prethru ");
495    if ( info.flags & ROAR_FLAG_IMMUTABLE )
496     strcat(flags, "immutable ");
497    if ( info.flags & ROAR_FLAG_ENHANCE )
498     strcat(flags, "enhance ");
499
500    printf("Flags                 : %s\n", flags);
501   }
502  }
503
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
517  if ( s.dir != ROAR_DIR_THRU ) {
518   display_mixer(con, id[i]);
519   show_meta_all(con, id[i]);
520  }
521 }
522
523}
524
525int display_mixer (struct roar_connection * con, int stream) {
526 struct roar_mixer_settings mixer;
527 int channels;
528 int i;
529 float fs;
530
531 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
532  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream);
533  return -1;
534 }
535
536#ifdef ROAR_HAVE_LIBM
537#define _DB ", %.2fdB"
538#else
539#define _DB ""
540#endif
541
542 fs = (float)mixer.scale / 100.;
543
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)
547#ifdef ROAR_HAVE_LIBM
548                           , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div)
549#endif
550        );
551 }
552
553 for (i = 0; i < channels; i++)
554  printf("Mixer volume chan %2i  : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale,
555                           (float)mixer.mixer[i]/fs
556#ifdef ROAR_HAVE_LIBM
557                          , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale)
558#endif
559        );
560
561 return 0;
562}
563
564static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) {
565 float fs = scale;
566
567 switch (k[len - 1]) {
568  case '%':
569    k[len - 1] = 0;
570    return (atof(k)*fs)/100.;
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;
580    return powf(10, atof(k)/20.f)*fs;
581   break;
582#endif
583 }
584
585 return atoi(k);
586}
587
588int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
589 uint16_t scale = 65535;
590 int chans = 0;
591 int id;
592 int i;
593 int len;
594 int old_chans;
595 int vol_l, vol_r, vol_mono;
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 ) {
608  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
609  return -1;
610 }
611
612 if ( !strcmp(arg[*cur + 1], "scale") ) {
613  (*cur)++; // 'scale'
614  (*cur)++;
615  scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535);
616 }
617
618// TODO: clean up code here as the % vs. abs code is very duplicate...
619
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
629  vol_mono = set_mixer_parse_volume(k, len, scale);
630
631  for (i = 0; i < old_chans; i++)
632   mixer.mixer[i] = vol_mono;
633
634  chans = old_chans;
635
636 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
637  chans = old_chans;
638
639  if ( *cur + 2 > max )
640   return -1;
641
642  k   = arg[++(*cur)];
643  len = strlen(k);
644
645  vol_l = set_mixer_parse_volume(k, len, scale);
646
647  k   = arg[++(*cur)];
648  len = strlen(k);
649
650  vol_r = set_mixer_parse_volume(k, len, scale);
651
652  vol_mono = (vol_l + vol_r) / 2;
653
654  mixer.mixer[0] = vol_l;
655  mixer.mixer[1] = vol_r;
656
657  switch (chans) {
658   case 1:
659     mixer.mixer[0] = vol_mono;
660    break;
661//   case 2: classic stereo...
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:
681     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
682     return -1;
683    break;
684  }
685
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
704   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
705  }
706 }
707
708 mixer.scale = scale;
709
710 return roar_set_vol(con, id, &mixer, chans);
711}
712
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
719 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
720
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
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
732 if ( roar_stream_new_by_id(&s, id) == -1 )
733  return -1;
734
735// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
736
737 if ( strcmp(mode, "add") == 0 ) {
738  mode_i = ROAR_META_MODE_ADD;
739 }
740
741 meta.type   = roar_meta_inttype(type);
742 meta.value  = val;
743 meta.key[0] = 0;
744
745 if ( meta.type == -1 ) {
746  fprintf(stderr, "Error: unknown type: %s\n", type);
747  return -1;
748 }
749
750// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
751
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);
759}
760
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
769 if ( roar_stream_new_by_id(&s, id) == -1 )
770  return -1;
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
796  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) {
797   fclose(in);
798   return -1;
799  }
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
810int show_meta_type (struct roar_connection * con, int id, char * type) {
811 struct roar_meta   meta;
812 struct roar_stream s;
813
814 if ( roar_stream_new_by_id(&s, id) == -1 )
815  return -1;
816
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 }
823
824 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
825  return -1;
826
827 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
828
829 roar_meta_free(&meta);
830
831 return 0;
832}
833
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
840 if ( roar_stream_new_by_id(&s, id) == -1 )
841  return -1;
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
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
860 if ( roar_stream_new_by_id(&s, id) == -1 )
861  return -1;
862
863 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
864  return -1;
865
866 if ( (out = fopen(file, "w")) == NULL )
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
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
895 if ( roar_stream_new_by_id(s, id) == -1 )
896  return -1;
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;
904  } else if ( !strcmp(c, "sync") ) {
905   f |= ROAR_FLAG_SYNC;
906  } else if ( !strcmp(c, "cleanmeta") ) {
907   f |= ROAR_FLAG_CLEANMETA;
908  } else if ( !strcmp(c, "hwmixer") ) {
909   f |= ROAR_FLAG_HWMIXER;
910  } else if ( !strcmp(c, "pause") ) {
911   f |= ROAR_FLAG_PAUSE;
912  } else if ( !strcmp(c, "mute") ) {
913   f |= ROAR_FLAG_MUTE;
914  } else if ( !strcmp(c, "mmap") ) {
915   f |= ROAR_FLAG_MMAP;
916  } else if ( !strcmp(c, "antiecho") ) {
917   f |= ROAR_FLAG_ANTIECHO;
918  } else if ( !strcmp(c, "recsource") ) {
919   f |= ROAR_FLAG_RECSOURCE;
920  } else if ( !strcmp(c, "passmixer") ) {
921   f |= ROAR_FLAG_PASSMIXER;
922  } else if ( !strcmp(c, "virtual") ) {
923   f |= ROAR_FLAG_VIRTUAL;
924  } else if ( !strcmp(c, "prethru") ) {
925   f |= ROAR_FLAG_PRETHRU;
926  } else if ( !strcmp(c, "immutable") ) {
927   f |= ROAR_FLAG_IMMUTABLE;
928  } else if ( !strcmp(c, "enhance") ) {
929   f |= ROAR_FLAG_ENHANCE;
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
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);
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
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
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 ) {
995   server = argv[++i];
996  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
997   g_verbose++;
998  } else if ( strcmp(k, "--help") == 0 ) {
999   usage();
1000   return 0;
1001  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1002   list_aiprofiles();
1003   return 0;
1004  } else if ( strcmp(k, "--enum-servers") == 0 ) {
1005   enum_servers();
1006   return 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
1018 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
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
1037  } else if ( !strcmp(k, "sleep") ) {
1038   sleep(atoi(argv[++i]));
1039
1040  } else if ( !strcmp(k, "ping") ) {
1041#ifdef ROAR_HAVE_GETTIMEOFDAY
1042   if ( ping(&con, atoi(argv[++i])) == -1 ) {
1043    fprintf(stderr, "Error: can not ping\n");
1044   }
1045#else
1046    fprintf(stderr, "Error: ping not supported.\n");
1047    i++;
1048#endif
1049
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") ) {
1064   if ( roar_terminate(&con, 0) == -1 ) {
1065    fprintf(stderr, "Error: can not quit server\n");
1066   } else {
1067    printf("Server quited\n");
1068    break;
1069   }
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   }
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
1090  } else if ( !strcmp(k, "whoami") ) {
1091   printf("My client ID is: %i\n", roar_get_clientid(&con));
1092  } else if ( !strcmp(k, "serverinfo") ) {
1093   server_info(&con);
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") ) {
1108   t = roar_str2ot((k = argv[++i]));
1109   if ( t == -1 ) {
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
1120  } else if ( !strcmp(k, "newvirtual") ) {
1121   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
1122    fprintf(stderr, "Error: can not create new virtual stream\n");
1123   } else {
1124    printf("virtual stream created\n");
1125   }
1126   i += 6;
1127
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
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++;
1151  } else if ( !strcmp(k, "metaset") ) {
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;
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++;
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++;
1181
1182
1183  } else if ( !strcmp(k, "listaiprofiles") || !strcmp(k, "listprofiles") ) {
1184   if ( list_aiprofiles() == -1 ) {
1185    fprintf(stderr, "Error: can not list profiles\n");
1186   }
1187  } else if ( !strcmp(k, "aiprofileget") || !strcmp(k, "profileget") ) {
1188   i++;
1189   if ( show_aiprofile(argv[i]) == -1 ) {
1190    fprintf(stderr, "Error: can not get profile data\n");
1191   }
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.