source: roaraudio/roarclients/roarctl.c @ 4552:47a0412f706d

Last change on this file since 4552:47a0412f706d was 4552:47a0412f706d, checked in by phi, 14 years ago

implemented flag toggling and flag protection

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