source: roaraudio/roarclients/roarctl.c @ 5005:2f39a39bc5c6

Last change on this file since 5005:2f39a39bc5c6 was 5005:2f39a39bc5c6, checked in by phi, 13 years ago

replace unsave str{cat,cpy}()

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