source: roaraudio/roarclients/roarctl.c @ 4536:4c0dd434da4b

Last change on this file since 4536:4c0dd434da4b was 4536:4c0dd434da4b, checked in by phi, 14 years ago

added support for sinle sink mode

File size: 33.1 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    if ( info.flags & ROAR_FLAG_SINGLESINK )
536     strcat(flags, "singlesink ");
537
538    printf("Flags                 : %s\n", flags);
539   }
540  }
541
542  if ( g_verbose ) {
543   len = ROAR_MAX_CHANNELS;
544   if ( roar_stream_get_chanmap(con, &s, chanmap, &len) == -1 ) {
545    fprintf(stderr, "Error: can not get stream channel map\n");
546   } else {
547    if ( roardsp_chanlist2str(chanmap, len, buffer, 1024) == -1 ) {
548     fprintf(stderr, "Error: can not convert channel map into string\n");
549    } else {
550     printf("Channel Map           : %s\n", buffer);
551    }
552   }
553  }
554
555  if ( s.dir != ROAR_DIR_THRU ) {
556   display_mixer(con, id[i]);
557   show_meta_all(con, id[i]);
558  }
559 }
560
561}
562
563int display_mixer (struct roar_connection * con, int stream) {
564 struct roar_mixer_settings mixer;
565 int channels;
566 int i;
567 float fs;
568
569 if ( roar_get_vol(con, stream, &mixer, &channels) == -1 ) {
570  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", stream);
571  return -1;
572 }
573
574#ifdef ROAR_HAVE_LIBM
575#define _DB ", %.2fdB"
576#else
577#define _DB ""
578#endif
579
580 fs = (float)mixer.scale / 100.;
581
582 if ( channels ) { // we hide RPG info for zero-channel streams
583  printf("Mixer ReplayGain      : %i/%i (%.2f%%" _DB ")\n", mixer.rpg_mul, mixer.rpg_div,
584                                                          100.*(float)mixer.rpg_mul/((float)mixer.rpg_div)
585#ifdef ROAR_HAVE_LIBM
586                           , 20*log10f((float)mixer.rpg_mul/(float)mixer.rpg_div)
587#endif
588        );
589 }
590
591 for (i = 0; i < channels; i++)
592  printf("Mixer volume chan %2i  : %i/%i (%.2f%%" _DB ")\n", i, mixer.mixer[i], mixer.scale,
593                           (float)mixer.mixer[i]/fs
594#ifdef ROAR_HAVE_LIBM
595                          , 20*log10f((float)mixer.mixer[i]/(float)mixer.scale)
596#endif
597        );
598
599 return 0;
600}
601
602static unsigned int set_mixer_parse_volume (char * k, int len, uint16_t scale) {
603 float fs = scale;
604
605 switch (k[len - 1]) {
606  case '%':
607    k[len - 1] = 0;
608    return (atof(k)*fs)/100.;
609   break;
610#ifdef ROAR_HAVE_LIBM
611  case 'b':
612  case 'B':
613    // TODO: can we hanle the error better?
614    if ( len < 2 )
615     return 0;
616
617    k[len - 2] = 0;
618    return powf(10, atof(k)/20.f)*fs;
619   break;
620#endif
621 }
622
623 return atoi(k);
624}
625
626int set_mixer (struct roar_connection * con, int * cur, int max, char * arg[]) {
627 uint16_t scale = 65535;
628 int chans = 0;
629 int id;
630 int i;
631 int len;
632 int old_chans;
633 int vol_l, vol_r, vol_mono;
634 char * k;
635 struct roar_mixer_settings mixer;
636 struct roar_mixer_settings old_mixer;
637
638 if (*cur + 2 > max)
639  return -1;
640
641 id = atoi(arg[++(*cur)]);
642
643 k = arg[++(*cur)];
644
645 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
646  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
647  return -1;
648 }
649
650 if ( !strcmp(arg[*cur + 1], "scale") ) {
651  (*cur)++; // 'scale'
652  (*cur)++;
653  scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535);
654 }
655
656// TODO: clean up code here as the % vs. abs code is very duplicate...
657
658 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
659  chans = 1;
660
661  if ( *cur + 1 > max )
662   return -1;
663
664  k   = arg[++(*cur)];
665  len = strlen(k);
666
667  vol_mono = set_mixer_parse_volume(k, len, scale);
668
669  for (i = 0; i < old_chans; i++)
670   mixer.mixer[i] = vol_mono;
671
672  chans = old_chans;
673
674 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
675  chans = old_chans;
676
677  if ( *cur + 2 > max )
678   return -1;
679
680  k   = arg[++(*cur)];
681  len = strlen(k);
682
683  vol_l = set_mixer_parse_volume(k, len, scale);
684
685  k   = arg[++(*cur)];
686  len = strlen(k);
687
688  vol_r = set_mixer_parse_volume(k, len, scale);
689
690  vol_mono = (vol_l + vol_r) / 2;
691
692  mixer.mixer[0] = vol_l;
693  mixer.mixer[1] = vol_r;
694
695  switch (chans) {
696   case 1:
697     mixer.mixer[0] = vol_mono;
698    break;
699//   case 2: classic stereo...
700   case 3:
701     mixer.mixer[2] = vol_mono;
702    break;
703   case 4:
704     mixer.mixer[2] = vol_l;
705     mixer.mixer[3] = vol_r;
706    break;
707   case 5:
708     mixer.mixer[2] = vol_mono;
709     mixer.mixer[3] = vol_l;
710     mixer.mixer[4] = vol_r;
711    break;
712   case 6:
713     mixer.mixer[2] = vol_mono;
714     mixer.mixer[3] = vol_mono;
715     mixer.mixer[4] = vol_l;
716     mixer.mixer[5] = vol_r;
717    break;
718   default:
719     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
720     return -1;
721    break;
722  }
723
724 } else {
725  if ( strcmp(k, "mono") == 0 ) {
726   chans = 1;
727  } else if ( strcmp(k, "stereo") == 0 ) {
728   chans = 2;
729  } else {
730   chans = atoi(k);
731  }
732
733//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
734
735  if ( *cur + chans > max )
736   return -1;
737
738  for (i = 0; i < chans; i++) {
739   k   = arg[++(*cur)];
740   len = strlen(k);
741
742   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
743  }
744 }
745
746 mixer.scale = scale;
747
748 return roar_set_vol(con, id, &mixer, chans);
749}
750
751
752int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
753 struct roar_stream s;
754 int dir    = roar_str2dir(d_s);
755 int parent = atoi(p_s);
756
757 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
758
759 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
760  return -1;
761
762 return roar_simple_connect_virtual(con, &s, parent, dir);
763}
764
765int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
766 struct roar_meta   meta;
767 struct roar_stream s;
768 int mode_i = ROAR_META_MODE_SET;
769
770 if ( roar_stream_new_by_id(&s, id) == -1 )
771  return -1;
772
773// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
774
775 if ( strcmp(mode, "add") == 0 ) {
776  mode_i = ROAR_META_MODE_ADD;
777 }
778
779 meta.type   = roar_meta_inttype(type);
780 meta.value  = val;
781 meta.key[0] = 0;
782
783 if ( meta.type == -1 ) {
784  fprintf(stderr, "Error: unknown type: %s\n", type);
785  return -1;
786 }
787
788// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
789
790 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
791  return -1;
792
793 meta.type  = ROAR_META_TYPE_NONE;
794 meta.value = NULL;
795
796 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
797}
798
799int load_meta (struct roar_connection * con, int id, char * file) {
800 struct roar_meta   meta;
801 struct roar_stream s;
802 int mode_i = ROAR_META_MODE_SET;
803 FILE * in;
804 char lion[1024];
805 char * v;
806
807 if ( roar_stream_new_by_id(&s, id) == -1 )
808  return -1;
809
810 if ( (in = fopen(file, "r")) == NULL )
811  return -1;
812
813 while (fgets(lion, 1024, in) != NULL) {
814  if ( (v = strtok(lion, "\r\n")) != NULL )
815   if ( (v = strtok(NULL, "\r\n")) != NULL )
816    *(v-1) = 0;
817
818  if ( (v = strstr(lion, "=")) == NULL ) {
819   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
820   continue;
821  }
822
823  *v++ = 0;
824
825  meta.type   = roar_meta_inttype(lion);
826  meta.value  = v;
827  meta.key[0] = 0;
828
829  if ( meta.type == -1 ) {
830   fprintf(stderr, "Error: unknown type: %s\n", lion);
831   continue;
832  }
833
834  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) {
835   fclose(in);
836   return -1;
837  }
838 }
839
840 fclose(in);
841
842 meta.type  = ROAR_META_TYPE_NONE;
843 meta.value = NULL;
844
845 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
846}
847
848int show_meta_type (struct roar_connection * con, int id, char * type) {
849 struct roar_meta   meta;
850 struct roar_stream s;
851
852 if ( roar_stream_new_by_id(&s, id) == -1 )
853  return -1;
854
855 meta.type  = roar_meta_inttype(type);
856
857 if ( meta.type == -1 ) {
858  fprintf(stderr, "Error: unknown type: %s\n", type);
859  return -1;
860 }
861
862 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
863  return -1;
864
865 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
866
867 roar_meta_free(&meta);
868
869 return 0;
870}
871
872int show_meta_all (struct roar_connection * con, int id) {
873 struct roar_stream s;
874 int types[ROAR_META_MAX_PER_STREAM];
875 int i;
876 int len;
877
878 if ( roar_stream_new_by_id(&s, id) == -1 )
879  return -1;
880
881 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
882  return -1;
883
884 for (i = 0; i < len; i++)
885  show_meta_type(con, id, roar_meta_strtype(types[i]));
886
887 return 0;
888}
889
890int save_meta (struct roar_connection * con, int id, char * file) {
891 struct roar_stream s;
892 struct roar_meta   meta;
893 int types[ROAR_META_MAX_PER_STREAM];
894 int i;
895 int len;
896 FILE * out;
897
898 if ( roar_stream_new_by_id(&s, id) == -1 )
899  return -1;
900
901 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
902  return -1;
903
904 if ( (out = fopen(file, "w")) == NULL )
905  return -1;
906
907 for (i = 0; i < len; i++) {
908/*
909  show_meta_type(con, id, roar_meta_strtype(types[i]));
910*/
911  meta.type  = types[i];
912
913  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
914   continue;
915
916//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
917
918  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
919
920  roar_meta_free(&meta);
921 }
922
923 fclose(out);
924
925 return 0;
926}
927
928int set_flags (struct roar_connection * con, int id, int reset, char * flags) {
929 int f = ROAR_FLAG_NONE;
930 char * c;
931 struct roar_stream s[1];
932
933 if ( roar_stream_new_by_id(s, id) == -1 )
934  return -1;
935
936 c = strtok(flags, ",");
937 while (c != NULL) {
938  if ( !strcmp(c, "meta") ) {
939   f |= ROAR_FLAG_META;
940  } else if ( !strcmp(c, "primary") ) {
941   f |= ROAR_FLAG_PRIMARY;
942  } else if ( !strcmp(c, "sync") ) {
943   f |= ROAR_FLAG_SYNC;
944  } else if ( !strcmp(c, "cleanmeta") ) {
945   f |= ROAR_FLAG_CLEANMETA;
946  } else if ( !strcmp(c, "hwmixer") ) {
947   f |= ROAR_FLAG_HWMIXER;
948  } else if ( !strcmp(c, "pause") ) {
949   f |= ROAR_FLAG_PAUSE;
950  } else if ( !strcmp(c, "mute") ) {
951   f |= ROAR_FLAG_MUTE;
952  } else if ( !strcmp(c, "mmap") ) {
953   f |= ROAR_FLAG_MMAP;
954  } else if ( !strcmp(c, "antiecho") ) {
955   f |= ROAR_FLAG_ANTIECHO;
956  } else if ( !strcmp(c, "recsource") ) {
957   f |= ROAR_FLAG_RECSOURCE;
958  } else if ( !strcmp(c, "passmixer") ) {
959   f |= ROAR_FLAG_PASSMIXER;
960  } else if ( !strcmp(c, "virtual") ) {
961   f |= ROAR_FLAG_VIRTUAL;
962  } else if ( !strcmp(c, "prethru") ) {
963   f |= ROAR_FLAG_PRETHRU;
964  } else if ( !strcmp(c, "immutable") ) {
965   f |= ROAR_FLAG_IMMUTABLE;
966  } else if ( !strcmp(c, "enhance") ) {
967   f |= ROAR_FLAG_ENHANCE;
968  } else if ( !strcmp(c, "singlesink") ) {
969   f |= ROAR_FLAG_SINGLESINK;
970  } else {
971   fprintf(stderr, "Error: unknown flag: %s\n", c);
972   return -1;
973  }
974
975  c = strtok(NULL, ",");
976 }
977
978 return roar_stream_set_flags(con, s, f, reset);
979}
980
981int show_aiprofile (const char * profile) {
982 struct roar_audio_info info;
983
984 if ( roar_profile2info(&info, profile) == -1 ) {
985  fprintf(stderr, "Error: unknown profile: %s\n", profile);
986  return -1;
987 }
988
989 printf("Profile Name          : %s\n", profile);
990
991 if ( info.rate )
992  printf("Profile sample rate   : %i\n", info.rate);
993 if ( info.bits )
994  printf("Profile bits          : %i\n", info.bits);
995 if ( info.channels )
996  printf("Profile channels      : %i\n", info.channels);
997
998 printf("Profile codec         : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
999                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
1000
1001 return 0;
1002}
1003
1004int list_aiprofiles (void) {
1005 const char * list[1024];
1006 ssize_t ret;
1007 ssize_t i;
1008
1009 ret = roar_profiles_list(list, 1024, 0);
1010
1011 if ( ret == -1 ) {
1012  fprintf(stderr, "Error: can not read list of profiles\n");
1013  return -1;
1014 }
1015
1016 for (i = 0; i < ret; i++) {
1017  printf("profile %lli:\n", (long long signed int)i);
1018  show_aiprofile(list[i]);
1019 }
1020
1021 return 0;
1022}
1023
1024int main (int argc, char * argv[]) {
1025 struct roar_connection con;
1026 char * server   = NULL;
1027 char * k = NULL;
1028 int    i;
1029 int    t = 0;
1030
1031 for (i = 1; i < argc; i++) {
1032  k = argv[i];
1033
1034  if ( strcmp(k, "--server") == 0 ) {
1035   server = argv[++i];
1036  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
1037   g_verbose++;
1038  } else if ( strcmp(k, "--help") == 0 ) {
1039   usage();
1040   return 0;
1041  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1042   list_aiprofiles();
1043   return 0;
1044  } else if ( strcmp(k, "--enum-servers") == 0 ) {
1045   enum_servers();
1046   return 0;
1047  } else if ( *k == '-' ) {
1048   fprintf(stderr, "Error: unknown argument: %s\n", k);
1049   usage();
1050   return 1;
1051  } else {
1052   break;
1053  }
1054 }
1055
1056 // connect
1057
1058 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
1059  fprintf(stderr, "Error: Can not connect to server\n");
1060  return 1;
1061 }
1062
1063 if ( i == argc ) {
1064  fprintf(stderr, "Error: No Commands given\n");
1065  return 0; // this is not a fatal error...
1066 }
1067
1068 for (; i < argc; i++) {
1069  k = argv[i];
1070  // cmd is in k
1071
1072  printf("--- [ %s ] ---\n", k);
1073
1074  if ( !strcmp(k, "help") ) {
1075   usage();
1076
1077  } else if ( !strcmp(k, "sleep") ) {
1078   sleep(atoi(argv[++i]));
1079
1080  } else if ( !strcmp(k, "ping") ) {
1081#ifdef ROAR_HAVE_GETTIMEOFDAY
1082   if ( ping(&con, atoi(argv[++i])) == -1 ) {
1083    fprintf(stderr, "Error: can not ping\n");
1084   }
1085#else
1086    fprintf(stderr, "Error: ping not supported.\n");
1087    i++;
1088#endif
1089
1090  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
1091   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
1092    fprintf(stderr, "Error: can not set mode to standby\n");
1093   } else {
1094    printf("going into standby\n");
1095   }
1096  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
1097   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
1098    fprintf(stderr, "Error: can not set mode to active\n");
1099   } else {
1100    printf("going into active mode\n");
1101   }
1102
1103  } else if ( !strcmp(k, "exit") ) {
1104   if ( roar_terminate(&con, 0) == -1 ) {
1105    fprintf(stderr, "Error: can not quit server\n");
1106   } else {
1107    printf("Server quited\n");
1108    break;
1109   }
1110  } else if ( !strcmp(k, "terminate") ) {
1111   if ( roar_terminate(&con, 1) == -1 ) {
1112    fprintf(stderr, "Error: can not terminate server\n");
1113   } else {
1114    printf("Server got asked to quited\n");
1115    break;
1116   }
1117
1118  } else if ( !strcmp(k, "standbymode") ) {
1119   t = roar_get_standby(&con);
1120   if ( t == -1 ) {
1121    fprintf(stderr, "Error: can not get stanby mode\n");
1122   } else if ( t == ROAR_STANDBY_ACTIVE ) {
1123    printf("Server is in standby\n");
1124   } else if ( t == ROAR_STANDBY_INACTIVE ) {
1125    printf("Server is active\n");
1126   } else {
1127    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
1128   }
1129
1130  } else if ( !strcmp(k, "whoami") ) {
1131   printf("My client ID is: %i\n", roar_get_clientid(&con));
1132  } else if ( !strcmp(k, "serverinfo") ) {
1133   server_info(&con);
1134  } else if ( !strcmp(k, "serveroinfo") ) {
1135   server_oinfo(&con);
1136  } else if ( !strcmp(k, "serverstandards") ) {
1137   server_standards(&con);
1138  } else if ( !strcmp(k, "listclients") ) {
1139   list_clients(&con);
1140  } else if ( !strcmp(k, "liststreams") ) {
1141   list_streams(&con);
1142  } else if ( !strcmp(k, "allinfo") ) {
1143   server_oinfo(&con);
1144   printf("\n");
1145   list_clients(&con);
1146   printf("\n");
1147   list_streams(&con);
1148
1149  } else if ( !strcmp(k, "kick") ) {
1150   t = roar_str2ot((k = argv[++i]));
1151   if ( t == -1 ) {
1152    fprintf(stderr, "Error: unknown type: %s\n", k);
1153    continue;
1154   }
1155   //t = atoi(argv[i++]);
1156   if ( roar_kick(&con, t, atoi(argv[++i])) == -1 ) {
1157    fprintf(stderr, "Error: can not kick %s\n", k);
1158   } else {
1159    printf("%s kicked\n", k);
1160   }
1161
1162  } else if ( !strcmp(k, "newvirtual") ) {
1163   if ( newvirtual(&con, argv[i+1], argv[i+2], argv[i+3], argv[i+4], argv[i+5], argv[i+6]) == -1 ) {
1164    fprintf(stderr, "Error: can not create new virtual stream\n");
1165   } else {
1166    printf("virtual stream created\n");
1167   }
1168   i += 6;
1169
1170  } else if ( !strcmp(k, "volume") ) {
1171   if ( set_mixer(&con, &i, argc, argv) == -1 ) {
1172    fprintf(stderr, "Error: can not set volume\n");
1173   } else {
1174    printf("volume changed\n");
1175   }
1176
1177  } else if ( !strcmp(k, "flag") ) {
1178   i++;
1179   if ( set_flags(&con, atoi(argv[i]), ROAR_SET_FLAG, argv[i+1]) == -1 ) {
1180    fprintf(stderr, "Error: can not set flags\n");
1181   } else {
1182    printf("flags changed\n");
1183   }
1184   i++;
1185  } else if ( !strcmp(k, "unflag") ) {
1186   i++;
1187   if ( set_flags(&con, atoi(argv[i]), ROAR_RESET_FLAG, argv[i+1]) == -1 ) {
1188    fprintf(stderr, "Error: can not reset flags\n");
1189   } else {
1190    printf("flags changed\n");
1191   }
1192   i++;
1193  } else if ( !strcmp(k, "metaset") ) {
1194   i++;
1195   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1196    fprintf(stderr, "Error: can not set meta data\n");
1197   } else {
1198    printf("meta data changed\n");
1199   }
1200   i += 3;
1201  } else if ( !strcmp(k, "metaget") ) {
1202   i++;
1203   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1204    fprintf(stderr, "Error: can not get meta data\n");
1205   }
1206   i++;
1207  } else if ( !strcmp(k, "metasave") ) {
1208   i++;
1209   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1210    fprintf(stderr, "Error: can not get meta data\n");
1211   } else {
1212    printf("meta data saved\n");
1213   }
1214   i++;
1215  } else if ( !strcmp(k, "metaload") ) {
1216   i++;
1217   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1218    fprintf(stderr, "Error: can not set meta data\n");
1219   } else {
1220    printf("meta data saved\n");
1221   }
1222   i++;
1223
1224
1225  } else if ( !strcmp(k, "listaiprofiles") || !strcmp(k, "listprofiles") ) {
1226   if ( list_aiprofiles() == -1 ) {
1227    fprintf(stderr, "Error: can not list profiles\n");
1228   }
1229  } else if ( !strcmp(k, "aiprofileget") || !strcmp(k, "profileget") ) {
1230   i++;
1231   if ( show_aiprofile(argv[i]) == -1 ) {
1232    fprintf(stderr, "Error: can not get profile data\n");
1233   }
1234  } else {
1235   fprintf(stderr, "Error: invalid command: %s\n", k);
1236  }
1237
1238 }
1239
1240 roar_disconnect(&con);
1241
1242 return 0;
1243}
1244
1245//ll
Note: See TracBrowser for help on using the repository browser.