source: roaraudio/roarclients/roarctl.c @ 4740:f3abf82e2ead

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

Added support for UNMAPPED volume control (Closes: #55)

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