source: roaraudio/roarclients/roarctl.c @ 4501:08d4027e47d5

Last change on this file since 4501:08d4027e47d5 was 4501:08d4027e47d5, checked in by phi, 13 years ago

basic serverstands listing

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