source: roaraudio/roarclients/roarctl.c @ 4708:c9d40761088a

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

updated copyright statements

File size: 34.7 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 int vol_l, vol_r, vol_mono;
647 char * k;
648 struct roar_mixer_settings mixer;
649 struct roar_mixer_settings old_mixer;
650
651 if (*cur + 2 > max)
652  return -1;
653
654 id = atoi(arg[++(*cur)]);
655
656 k = arg[++(*cur)];
657
658 if ( roar_get_vol(con, id, &old_mixer, &old_chans) == -1 ) {
659  fprintf(stderr, "Error: can not get stream mixer info for stream %i\n", id);
660  return -1;
661 }
662
663 if ( !strcmp(arg[*cur + 1], "scale") ) {
664  (*cur)++; // 'scale'
665  (*cur)++;
666  scale = set_mixer_parse_volume(arg[*cur], strlen(arg[*cur]), 65535);
667 }
668
669// TODO: clean up code here as the % vs. abs code is very duplicate...
670
671 if ( strcmp(k, "mono") == 0 && old_chans != 1 ) {
672  chans = 1;
673
674  if ( *cur + 1 > max )
675   return -1;
676
677  k   = arg[++(*cur)];
678  len = strlen(k);
679
680  vol_mono = set_mixer_parse_volume(k, len, scale);
681
682  for (i = 0; i < old_chans; i++)
683   mixer.mixer[i] = vol_mono;
684
685  chans = old_chans;
686
687 } else if ( strcmp(k, "stereo") == 0 && old_chans != 2 ) {
688  chans = old_chans;
689
690  if ( *cur + 2 > max )
691   return -1;
692
693  k   = arg[++(*cur)];
694  len = strlen(k);
695
696  vol_l = set_mixer_parse_volume(k, len, scale);
697
698  k   = arg[++(*cur)];
699  len = strlen(k);
700
701  vol_r = set_mixer_parse_volume(k, len, scale);
702
703  vol_mono = (vol_l + vol_r) / 2;
704
705  mixer.mixer[0] = vol_l;
706  mixer.mixer[1] = vol_r;
707
708  switch (chans) {
709   case 1:
710     mixer.mixer[0] = vol_mono;
711    break;
712//   case 2: classic stereo...
713   case 3:
714     mixer.mixer[2] = vol_mono;
715    break;
716   case 4:
717     mixer.mixer[2] = vol_l;
718     mixer.mixer[3] = vol_r;
719    break;
720   case 5:
721     mixer.mixer[2] = vol_mono;
722     mixer.mixer[3] = vol_l;
723     mixer.mixer[4] = vol_r;
724    break;
725   case 6:
726     mixer.mixer[2] = vol_mono;
727     mixer.mixer[3] = vol_mono;
728     mixer.mixer[4] = vol_l;
729     mixer.mixer[5] = vol_r;
730    break;
731   default:
732     ROAR_ERR("mode stereo not supported on stream with %i channels", chans);
733     return -1;
734    break;
735  }
736
737 } else {
738  if ( strcmp(k, "mono") == 0 ) {
739   chans = 1;
740  } else if ( strcmp(k, "stereo") == 0 ) {
741   chans = 2;
742  } else {
743   chans = atoi(k);
744  }
745
746//  printf("mode: int; chans=%i, old_chans=%i\n", chans, old_chans);
747
748  if ( *cur + chans > max )
749   return -1;
750
751  for (i = 0; i < chans; i++) {
752   k   = arg[++(*cur)];
753   len = strlen(k);
754
755   mixer.mixer[i] = set_mixer_parse_volume(k, len, scale);
756  }
757 }
758
759 mixer.scale = scale;
760
761 return roar_set_vol(con, id, &mixer, chans);
762}
763
764
765int newvirtual (struct roar_connection * con, char *p_s, char *d_s, char *e_s, char *r_s, char *b_s, char *c_s) {
766 struct roar_stream s;
767 int dir    = roar_str2dir(d_s);
768 int parent = atoi(p_s);
769
770 ROAR_DBG("newvirtual(*): dir=%i, parent=%i", dir, parent);
771
772 if ( roar_stream_new(&s, atoi(r_s), atoi(c_s), atoi(b_s), roar_str2codec(e_s)) == -1 )
773  return -1;
774
775 return roar_simple_connect_virtual(con, &s, parent, dir);
776}
777
778int set_meta (struct roar_connection * con, int id, char * mode, char * type, char * val) {
779 struct roar_meta   meta;
780 struct roar_stream s;
781 int mode_i = ROAR_META_MODE_SET;
782
783 if ( roar_stream_new_by_id(&s, id) == -1 )
784  return -1;
785
786// printf("set_meta(*): mode='%s', type='%s', val='%s'\n", mode, type, val);
787
788 if ( strcmp(mode, "add") == 0 ) {
789  mode_i = ROAR_META_MODE_ADD;
790 }
791
792 meta.type   = roar_meta_inttype(type);
793 meta.value  = val;
794 meta.key[0] = 0;
795
796 if ( meta.type == -1 ) {
797  fprintf(stderr, "Error: unknown type: %s\n", type);
798  return -1;
799 }
800
801// printf("D: type=%i, mode=%i\n", meta.type, mode_i);
802
803 if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 )
804  return -1;
805
806 meta.type  = ROAR_META_TYPE_NONE;
807 meta.value = NULL;
808
809 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
810}
811
812int load_meta (struct roar_connection * con, int id, char * file) {
813 struct roar_meta   meta;
814 struct roar_stream s;
815 int mode_i = ROAR_META_MODE_SET;
816 FILE * in;
817 char lion[1024];
818 char * v;
819
820 if ( roar_stream_new_by_id(&s, id) == -1 )
821  return -1;
822
823 if ( (in = fopen(file, "r")) == NULL )
824  return -1;
825
826 while (fgets(lion, 1024, in) != NULL) {
827  if ( (v = strtok(lion, "\r\n")) != NULL )
828   if ( (v = strtok(NULL, "\r\n")) != NULL )
829    *(v-1) = 0;
830
831  if ( (v = strstr(lion, "=")) == NULL ) {
832   fprintf(stderr, "Error: can not parse meta data lion: %s\n", lion);
833   continue;
834  }
835
836  *v++ = 0;
837
838  meta.type   = roar_meta_inttype(lion);
839  meta.value  = v;
840  meta.key[0] = 0;
841
842  if ( meta.type == -1 ) {
843   fprintf(stderr, "Error: unknown type: %s\n", lion);
844   continue;
845  }
846
847  if ( roar_stream_meta_set(con, &s, mode_i, &meta) == -1 ) {
848   fclose(in);
849   return -1;
850  }
851 }
852
853 fclose(in);
854
855 meta.type  = ROAR_META_TYPE_NONE;
856 meta.value = NULL;
857
858 return roar_stream_meta_set(con, &s, ROAR_META_MODE_FINALIZE, &meta);
859}
860
861int show_meta_type (struct roar_connection * con, int id, char * type) {
862 struct roar_meta   meta;
863 struct roar_stream s;
864
865 if ( roar_stream_new_by_id(&s, id) == -1 )
866  return -1;
867
868 meta.type  = roar_meta_inttype(type);
869
870 if ( meta.type == -1 ) {
871  fprintf(stderr, "Error: unknown type: %s\n", type);
872  return -1;
873 }
874
875 if ( roar_stream_meta_get(con, &s, &meta) == -1 )
876  return -1;
877
878 printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
879
880 roar_meta_free(&meta);
881
882 return 0;
883}
884
885int show_meta_all (struct roar_connection * con, int id) {
886 struct roar_stream s;
887 int types[ROAR_META_MAX_PER_STREAM];
888 int i;
889 int len;
890
891 if ( roar_stream_new_by_id(&s, id) == -1 )
892  return -1;
893
894 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
895  return -1;
896
897 for (i = 0; i < len; i++)
898  show_meta_type(con, id, roar_meta_strtype(types[i]));
899
900 return 0;
901}
902
903int save_meta (struct roar_connection * con, int id, char * file) {
904 struct roar_stream s;
905 struct roar_meta   meta;
906 int types[ROAR_META_MAX_PER_STREAM];
907 int i;
908 int len;
909 FILE * out;
910
911 if ( roar_stream_new_by_id(&s, id) == -1 )
912  return -1;
913
914 if ( (len = roar_stream_meta_list(con, &s, types, ROAR_META_MAX_PER_STREAM)) == -1 )
915  return -1;
916
917 if ( (out = fopen(file, "w")) == NULL )
918  return -1;
919
920 for (i = 0; i < len; i++) {
921/*
922  show_meta_type(con, id, roar_meta_strtype(types[i]));
923*/
924  meta.type  = types[i];
925
926  if ( roar_stream_meta_get(con, &s, &meta) == -1 )
927   continue;
928
929//  printf("Meta %-17s: %s\n", roar_meta_strtype(meta.type), meta.value);
930
931  fprintf(out, "%s=%s\n", roar_meta_strtype(meta.type), meta.value);
932
933  roar_meta_free(&meta);
934 }
935
936 fclose(out);
937
938 return 0;
939}
940
941int set_flags (struct roar_connection * con, int id, int action, char * flags) {
942 uint32_t f = ROAR_FLAG_NONE;
943 char * c;
944 struct roar_stream s[1];
945
946 if ( roar_stream_new_by_id(s, id) == -1 )
947  return -1;
948
949 c = strtok(flags, ",");
950 while (c != NULL) {
951  if ( !strcmp(c, "meta") ) {
952   f |= ROAR_FLAG_META;
953  } else if ( !strcmp(c, "primary") ) {
954   f |= ROAR_FLAG_PRIMARY;
955  } else if ( !strcmp(c, "sync") ) {
956   f |= ROAR_FLAG_SYNC;
957  } else if ( !strcmp(c, "cleanmeta") ) {
958   f |= ROAR_FLAG_CLEANMETA;
959  } else if ( !strcmp(c, "hwmixer") ) {
960   f |= ROAR_FLAG_HWMIXER;
961  } else if ( !strcmp(c, "pause") ) {
962   f |= ROAR_FLAG_PAUSE;
963  } else if ( !strcmp(c, "mute") ) {
964   f |= ROAR_FLAG_MUTE;
965  } else if ( !strcmp(c, "mmap") ) {
966   f |= ROAR_FLAG_MMAP;
967  } else if ( !strcmp(c, "antiecho") ) {
968   f |= ROAR_FLAG_ANTIECHO;
969  } else if ( !strcmp(c, "recsource") ) {
970   f |= ROAR_FLAG_RECSOURCE;
971  } else if ( !strcmp(c, "passmixer") ) {
972   f |= ROAR_FLAG_PASSMIXER;
973  } else if ( !strcmp(c, "virtual") ) {
974   f |= ROAR_FLAG_VIRTUAL;
975  } else if ( !strcmp(c, "prethru") ) {
976   f |= ROAR_FLAG_PRETHRU;
977  } else if ( !strcmp(c, "immutable") ) {
978   f |= ROAR_FLAG_IMMUTABLE;
979  } else if ( !strcmp(c, "enhance") ) {
980   f |= ROAR_FLAG_ENHANCE;
981  } else if ( !strcmp(c, "singlesink") ) {
982   f |= ROAR_FLAG_SINGLESINK;
983  } else {
984   fprintf(stderr, "Error: unknown flag: %s\n", c);
985   return -1;
986  }
987
988  c = strtok(NULL, ",");
989 }
990
991 return roar_stream_set_flags2(con, s, f, action);
992}
993
994int show_aiprofile (const char * profile) {
995 struct roar_audio_info info;
996
997 if ( roar_profile2info(&info, profile) == -1 ) {
998  fprintf(stderr, "Error: unknown profile: %s\n", profile);
999  return -1;
1000 }
1001
1002 printf("Profile Name          : %s\n", profile);
1003
1004 if ( info.rate )
1005  printf("Profile sample rate   : %i\n", info.rate);
1006 if ( info.bits )
1007  printf("Profile bits          : %i\n", info.bits);
1008 if ( info.channels )
1009  printf("Profile channels      : %i\n", info.channels);
1010
1011 printf("Profile codec         : %2i (%s%s)\n", info.codec, roar_codec2str(info.codec),
1012                                       info.codec == ROAR_CODEC_DEFAULT ? " native" : "");
1013
1014 return 0;
1015}
1016
1017int list_aiprofiles (void) {
1018 const char * list[1024];
1019 ssize_t ret;
1020 ssize_t i;
1021
1022 ret = roar_profiles_list(list, 1024, 0);
1023
1024 if ( ret == -1 ) {
1025  fprintf(stderr, "Error: can not read list of profiles\n");
1026  return -1;
1027 }
1028
1029 for (i = 0; i < ret; i++) {
1030  printf("profile %lli:\n", (long long signed int)i);
1031  show_aiprofile(list[i]);
1032 }
1033
1034 return 0;
1035}
1036
1037int main (int argc, char * argv[]) {
1038 struct roar_connection con;
1039 char * server   = NULL;
1040 char * k = NULL;
1041 int    i;
1042 int    t = 0;
1043
1044 for (i = 1; i < argc; i++) {
1045  k = argv[i];
1046
1047  if ( strcmp(k, "--server") == 0 ) {
1048   server = argv[++i];
1049  } else if ( strcmp(k, "-v") == 0 || strcmp(k, "--verbose") == 0 ) {
1050   g_verbose++;
1051  } else if ( strcmp(k, "--help") == 0 ) {
1052   usage();
1053   return 0;
1054  } else if ( strcmp(k, "--list-aiprofiles") == 0 ) {
1055   list_aiprofiles();
1056   return 0;
1057  } else if ( strcmp(k, "--enum-servers") == 0 ) {
1058   enum_servers();
1059   return 0;
1060  } else if ( *k == '-' ) {
1061   fprintf(stderr, "Error: unknown argument: %s\n", k);
1062   usage();
1063   return 1;
1064  } else {
1065   break;
1066  }
1067 }
1068
1069 // connect
1070
1071 if ( roar_simple_connect(&con, server, "roarctl") == -1 ) {
1072  fprintf(stderr, "Error: Can not connect to server\n");
1073  return 1;
1074 }
1075
1076 if ( i == argc ) {
1077  fprintf(stderr, "Error: No Commands given\n");
1078  return 0; // this is not a fatal error...
1079 }
1080
1081 for (; i < argc; i++) {
1082  k = argv[i];
1083  // cmd is in k
1084
1085  printf("--- [ %s ] ---\n", k);
1086
1087  if ( !strcmp(k, "help") ) {
1088   usage();
1089
1090  } else if ( !strcmp(k, "sleep") ) {
1091   sleep(atoi(argv[++i]));
1092
1093  } else if ( !strcmp(k, "ping") ) {
1094#ifdef ROAR_HAVE_GETTIMEOFDAY
1095   if ( ping(&con, atoi(argv[++i])) == -1 ) {
1096    fprintf(stderr, "Error: can not ping\n");
1097   }
1098#else
1099    fprintf(stderr, "Error: ping not supported.\n");
1100    i++;
1101#endif
1102
1103  } else if ( !strcmp(k, "standby") || !strcmp(k, "off") ) {
1104   if ( roar_set_standby(&con, ROAR_STANDBY_ACTIVE) == -1 ) {
1105    fprintf(stderr, "Error: can not set mode to standby\n");
1106   } else {
1107    printf("going into standby\n");
1108   }
1109  } else if ( !strcmp(k, "resume") || !strcmp(k, "on") ) {
1110   if ( roar_set_standby(&con, ROAR_STANDBY_INACTIVE) == -1 ) {
1111    fprintf(stderr, "Error: can not set mode to active\n");
1112   } else {
1113    printf("going into active mode\n");
1114   }
1115
1116  } else if ( !strcmp(k, "exit") ) {
1117   if ( roar_terminate(&con, 0) == -1 ) {
1118    fprintf(stderr, "Error: can not quit server\n");
1119   } else {
1120    printf("Server quited\n");
1121    break;
1122   }
1123  } else if ( !strcmp(k, "terminate") ) {
1124   if ( roar_terminate(&con, 1) == -1 ) {
1125    fprintf(stderr, "Error: can not terminate server\n");
1126   } else {
1127    printf("Server got asked to quited\n");
1128    break;
1129   }
1130
1131  } else if ( !strcmp(k, "standbymode") ) {
1132   t = roar_get_standby(&con);
1133   if ( t == -1 ) {
1134    fprintf(stderr, "Error: can not get stanby mode\n");
1135   } else if ( t == ROAR_STANDBY_ACTIVE ) {
1136    printf("Server is in standby\n");
1137   } else if ( t == ROAR_STANDBY_INACTIVE ) {
1138    printf("Server is active\n");
1139   } else {
1140    fprintf(stderr, "Error: unknown standby mode: %i\n", t);
1141   }
1142
1143  } else if ( !strcmp(k, "whoami") ) {
1144   printf("My client ID is: %i\n", roar_get_clientid(&con));
1145  } else if ( !strcmp(k, "serverinfo") ) {
1146   server_info(&con);
1147  } else if ( !strcmp(k, "serveroinfo") ) {
1148   server_oinfo(&con, -1);
1149  } else if ( !strcmp(k, "serveroinfo2") ) {
1150   t = roar_str2dir(argv[++i]);
1151   if ( t == -1 ) {
1152    fprintf(stderr, "Error: unknown stream direction: %s\n", argv[i]);
1153   } else {
1154    server_oinfo(&con, t);
1155   }
1156  } else if ( !strcmp(k, "serverstandards") ) {
1157   server_standards(&con);
1158  } else if ( !strcmp(k, "listclients") ) {
1159   list_clients(&con);
1160  } else if ( !strcmp(k, "liststreams") ) {
1161   list_streams(&con);
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  } else if ( !strcmp(k, "metaset") ) {
1230   i++;
1231   if ( set_meta(&con, atoi(argv[i]), argv[i+1], argv[i+2], argv[i+3]) == -1 ) {
1232    fprintf(stderr, "Error: can not set meta data\n");
1233   } else {
1234    printf("meta data changed\n");
1235   }
1236   i += 3;
1237  } else if ( !strcmp(k, "metaget") ) {
1238   i++;
1239   if ( show_meta_type(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1240    fprintf(stderr, "Error: can not get meta data\n");
1241   }
1242   i++;
1243  } else if ( !strcmp(k, "metasave") ) {
1244   i++;
1245   if ( save_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1246    fprintf(stderr, "Error: can not get meta data\n");
1247   } else {
1248    printf("meta data saved\n");
1249   }
1250   i++;
1251  } else if ( !strcmp(k, "metaload") ) {
1252   i++;
1253   if ( load_meta(&con, atoi(argv[i]), argv[i+1]) == -1 ) {
1254    fprintf(stderr, "Error: can not set meta data\n");
1255   } else {
1256    printf("meta data saved\n");
1257   }
1258   i++;
1259
1260
1261  } else if ( !strcmp(k, "listaiprofiles") || !strcmp(k, "listprofiles") ) {
1262   if ( list_aiprofiles() == -1 ) {
1263    fprintf(stderr, "Error: can not list profiles\n");
1264   }
1265  } else if ( !strcmp(k, "aiprofileget") || !strcmp(k, "profileget") ) {
1266   i++;
1267   if ( show_aiprofile(argv[i]) == -1 ) {
1268    fprintf(stderr, "Error: can not get profile data\n");
1269   }
1270  } else {
1271   fprintf(stderr, "Error: invalid command: %s\n", k);
1272  }
1273
1274 }
1275
1276 roar_disconnect(&con);
1277
1278 return 0;
1279}
1280
1281//ll
Note: See TracBrowser for help on using the repository browser.