source: roaraudio/roarclients/roarctl.c @ 4328:dd99a1c382a6

Last change on this file since 4328:dd99a1c382a6 was 4328:dd99a1c382a6, checked in by phi, 14 years ago

use roar_str2ot() function for object types

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