source: roaraudio/roard/roard.c @ 2729:ad40b3b3d5be

Last change on this file since 2729:ad40b3b3d5be was 2729:ad40b3b3d5be, checked in by phi, 15 years ago

typo

File size: 42.8 KB
RevLine 
[0]1//roard.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard 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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
[0]25#include "roard.h"
26
[1494]27#ifdef ROAR_SUPPORT_LISTEN
[2530]28char * server[ROAR_MAX_LISTEN_SOCKETS];
[1494]29#endif
[60]30
[2104]31#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX)
32#define SUPPORT_PIDFILE
33char * pidfile = NULL;
34#endif
35
[2530]36#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
37 int    setids    = 0;
38#endif
39
[1503]40#ifdef ROAR_HAVE_MAIN_ARGS
[0]41void usage (void) {
42 printf("Usage: roard [OPTIONS]...\n\n");
43
[68]44 printf("Misc Options:\n\n");
45 printf(
[775]46        " --daemon              - Bring the server into background after init\n"
[71]47        " --terminate           - Terminate after last client quited\n"
[2109]48        " --start               - No op parameter (starting roard is default operation)\n"
[274]49        " --restart             - Trys to stop an old instance and start a new with new settings\n"
[2109]50        " --stop                - Stops a running roard (provide --pidfile!)\n"
51        " --shutdown            - Terminates a running roard (provide --pidfile!)\n"
[276]52        " --realtime            - Trys to get realtime priority,\n"
53        "                         give multible times for being more realtime\n"
[444]54        " --chroot DIR          - chroots to the given dir\n"
55        " --setgid              - GroupID to the audio group as specified via -G\n"
56        " --setuid              - UserID to the audio user as specified via -U\n"
[905]57        " --sysclocksync        - calculate exact sample rate using the system clock\n"
[2017]58        " --location  LOC       - Set lion readable location of server\n"
[2104]59#ifdef SUPPORT_PIDFILE
60        " --pidfile PIDFILE     - Write a pidfile at PIDFILE\n"
61#endif
[68]62       );
63
64 printf("\nAudio Options:\n\n");
[0]65 printf(
66        " -R  --rate   RATE     - Set server rate\n"
67        " -B  --bits   BITS     - Set server bits\n"
68        " -C  --chans  CHANNELS - Set server channels\n"
69       );
70
[2339]71 printf("\nStream Options:\n\n");
72 printf(
73        " --stream-flags D=F    - Set default flags for stream directions\n"
74        "                         D is the stream direction and F is a comma seperated\n"
75        "                         list of flags in form +flag or -flag to set or unset\n"
76        "                         a flag as default or remove it from the default\n"
77       );
78
[1923]79 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
[974]80 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
[0]81 printf(" -D  --device DEV      - Set the device\n");
82 printf(" -dO OPTS              - Set output options\n");
[973]83 printf(" --list-driver         - List all drivers\n");
[0]84
[932]85 printf("\nOutput Options:\n\n");
[974]86 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
[932]87 printf(" -O  --odevice DEV     - Set the device\n");
88 printf(" -oO OPTS              - Set output options\n");
89 printf(" -oN                   - Adds another output\n");
[961]90 printf(" -oP                   - Mark output as primary\n");
[932]91
[2485]92#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]93 printf("\nSource Options:\n\n");
94 printf(" -s  --source DRV      - Use DRV as input driver\n"
95        " -S           DEV      - Use DEV as input device\n"
96        " -sO          OPTS     - Use OPTS as input options\n"
[1512]97        " -sN                   - Adds another source\n"
[0]98        " -sP                   - Make souce as primary\n"
99       );
[2270]100 printf(" --list-sources        - List all sources\n");
[2485]101#endif
[0]102
[280]103 printf("\nCodec Filter Options:\n\n");
104 printf(" --list-cf             - List all codec filter\n"
105       );
[0]106
[2500]107#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1923]108 printf("\nMIDI Options:\n\n");
[1924]109 printf(" --midi-no-console     - Disable console based MIDI synth\n"
[2444]110        " --midi-console-enable - Enables the console based MIDI synth\n"
[1924]111        " --midi-console DEV    - Set device for MIDI console\n"
[2489]112#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]113        " --ssynth-enable       - Enable simple software synth\n"
114        " --ssynth-disable      - Disable simple software synth\n"
[2489]115#endif
[1924]116       );
[2500]117#endif
[1923]118
[2494]119#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]120 printf("\nLight Control Options:\n\n");
121 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
122                                  LIGHT_CHANNELS_DEFAULT
123       );
[2494]124#endif
[1923]125
[2725]126#ifndef ROAR_WITHOUT_DCOMP_RDTCS
127 printf("\nRadio Date and Transmitter Control System Options:\n\n");
128 printf(" --rds-pi   PI         - Sets the RDS Programme Identification (PI)\n"
129        " --rds-ps   PS         - Sets the RDS Programme Service Name (PS)\n"
130        " --rds-pty  PTY        - Sets the RDS Programme Type (PTY)\n"
131        " --rds-tp              - Sets the RDS Traffic Programme (TP) flag\n"
132        " --rds-ct              - Enables sending of RDS Clock Time (CT)\n"
133       );
134#endif
135
[0]136 printf("\nServer Options:\n\n");
137 printf(" -t  --tcp             - Use TCP listen socket\n"
138        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
[508]139#ifdef ROAR_HAVE_LIBDNET
140        " -n  --decnet          - use DECnet listen socket\n"
141#endif
[518]142        " -4                    - Use IPv4 connections (implies -t)\n"
143#ifdef PF_INET6
144        " -6                    - Use IPv6 connections (implies -t)\n"
145#endif
146#ifdef IPV6_ADDRFORM
147        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
148        "                         this is normaly not usefull.\n"
149#endif
[0]150        " -p  --port            - TCP Port to bind to\n"
151        " -b  --bind            - IP/Hostname to bind to\n"
[1115]152        "     --sock            - Filename for UNIX Domain Socket\n"
[2530]153        "     --proto PROTO     - Use PROTO as protocol on Socket\n"
154        "     --new-sock        - Parameters for new socket follows\n"
[1993]155#ifdef ROAR_HAVE_LIBSLP
156        "     --slp             - Enable OpenSLP support\n"
157#endif
[450]158        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
[60]159        "                         You need the permittions to change the GID\n"
[444]160        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
161        "                         You need the permittions to change the UID (normaly only root has)\n"
[548]162        " --no-listen           - Do not listen for new clients\n"
163        "                         (only usefull for relaing, impleys --terminate)\n"
[274]164        " --client-fh           - Comunicate with a client over this handle\n"
[501]165        "                         (only usefull for relaing)\n"
[920]166        " --close-fh            - Closes the given fh\n"
167        " --standby             - Start in standby state\n"
168        " --auto-standby        - Automatical goes into standby if there are no streams\n",
[450]169        ROAR_DEFAULT_SOCKGRP
[0]170       );
171// printf("\n Options:\n\n");
172 printf("\n");
173}
[1503]174#endif
[0]175
[2109]176int restart_server (char * server, int terminate) {
[579]177 struct roar_connection con;
[2111]178#ifdef ROAR_HAVE_KILL
179 char buf[80];
180 ssize_t l;
181 struct roar_vio_calls fh;
182 pid_t pid;
183 int ok;
184
185 if ( pidfile != NULL ) {
186  if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) {
187   ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile);
188  } else {
189   l = roar_vio_read(&fh, buf, 80);
190   roar_vio_close(&fh);
191   if ( l > 0 ) {
192    buf[l-1] = 0;
193    buf[79]  = 0;
194    pid = atoi(buf);
195    if ( terminate ) {
[2113]196     ok = kill(pid, SIGUSR1);
[2111]197    } else {
198     ok = kill(pid, SIGINT);
199    }
200    if ( ok == 0 ) {
201     return 0;
202    } else {
203     ROAR_WARN("restart_server(*): Can not kill roard by pidfile");
204    }
205   } else {
206    ROAR_WARN("restart_server(*): Can not find a PID in the pidfile");
207   }
208  }
209 }
210#endif
[2109]211
[579]212 if ( roar_connect(&con, server) == -1 ) {
213  return -1;
214 }
215
[2109]216 if ( roar_terminate(&con, terminate) == -1 ) {
[579]217  return -1;
218 }
219
220 return roar_disconnect(&con);
221}
222
[444]223#define R_SETUID 1
224#define R_SETGID 2
225
[1609]226int init_config (void) {
227 int i;
228
229 memset(g_config, 0, sizeof(struct roard_config));
230
231 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
232  g_config->streams[i].mixer_channels = 1;
233  g_config->streams[i].mixer.rpg_mul  = 1;
234  g_config->streams[i].mixer.rpg_div  = 1;
235  g_config->streams[i].mixer.scale    = 65535;
236  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
237 }
238
[2291]239 g_config->streams[ROAR_DIR_PLAY    ].flags = ROAR_FLAG_META;
[2413]240 g_config->streams[ROAR_DIR_OUTPUT  ].flags = ROAR_FLAG_PASSMIXER;
[2264]241 g_config->streams[ROAR_DIR_FILTER  ].flags = ROAR_FLAG_SYNC;
[1899]242 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
[2160]243 g_config->streams[ROAR_DIR_BIDIR   ].flags = ROAR_FLAG_ANTIECHO;
[1899]244
[2017]245 g_config->location = "***default***";
246
[1609]247 return 0;
248}
249
[2530]250#ifdef ROAR_SUPPORT_LISTEN
251int init_listening (void) {
252 int i;
253
254 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
255  g_listen_socket[i] = -1;
256  g_listen_proto[i]  = ROAR_PROTO_ROARAUDIO;
257  server[i]          = NULL;
258 }
259
260 return 0;
261}
262
263int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto) {
264#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
265 struct group   * grp  = NULL;
266#endif
267#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
268 struct passwd  * pwd  = NULL;
269#endif
270#ifdef ROAR_HAVE_UNIX
271 char * env_roar_proxy_backup;
272#endif
273 int    sockid = -1;
274 int    sock;
275 int    i;
276
277 if ( *addr != 0 ) {
278  for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
279   if ( g_listen_socket[i] == -1 ) {
280    sockid = i;
281    break;
282   }
283  }
284
285  if ( sockid == -1 )
286   return -1;
287
288  g_listen_proto[sockid] = proto;
289
290  ROAR_DBG("add_listen(*): proto=0x%.4x", proto);
291
292  if ( (g_listen_socket[sockid] = roar_socket_listen(sock_type, addr, port)) == -1 ) {
293#ifdef ROAR_HAVE_UNIX
294   if ( *addr == '/' ) {
295    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
296     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
297     unsetenv("ROAR_PROXY");
298    }
299    if ( (sock = roar_socket_connect(addr, port)) != -1 ) {
300     close(sock);
301     ROAR_ERR("Can not open listen socket!");
302     return 1;
303    } else {
304     unlink(addr);
305     if ( (g_listen_socket[sockid] = roar_socket_listen(sock_type, addr, port)) == -1 ) {
306      ROAR_ERR("Can not open listen socket!");
307      return 1;
308     }
309    }
310    if ( env_roar_proxy_backup != NULL ) {
311     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
312     free(env_roar_proxy_backup);
313    }
314#else
315   if (0) { // noop
316#endif
317   } else {
318    ROAR_ERR("Can not open listen socket!");
319    return 1;
320   }
321  }
322
323#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
324  if ( (grp = getgrnam(group)) == NULL ) {
325   ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno));
326  }
327#endif
328#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
329  if ( user || (setids & R_SETUID) ) {
330   if ( (pwd = getpwnam(user)) == NULL ) {
331    ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno));
332   }
333  }
334#endif
335
336#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
337  if ( *addr == '/' ) {
338   if ( grp ) {
339    if ( pwd ) {
340     if ( chown(addr, pwd->pw_uid, grp->gr_gid) == -1 )
341      return 1;
342    } else {
343     if ( chown(addr, -1, grp->gr_gid) == -1 )
344      return 1;
345    }
346#ifdef ROAR_HAVE_GETUID
347    if ( getuid() == 0 )
348     if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
349      return 1;
350#endif
351   }
352  }
353#endif
354 }
355
356 return 0;
357}
358#endif
359
[2339]360int update_stream_flags (char * str) {
361 int    dir;
362 char * flags;
363 char * k;
364 int    op;
365 int    flag;
366
367 if ( (flags = strstr(str, "=")) == NULL )
368  return -1;
369
370 *flags = 0;
371  flags++;
372
373 if ( (dir = roar_str2dir(str)) == -1 )
374  return -1;
375
376 while (flags != NULL) {
377  k = flags;
378  flags = strstr(flags, ",");
379
380  if ( flags != NULL )
381   *(flags++) = 0;
382
383  switch (*k) {
[2340]384   case '+': k++; op = ROAR_SET_FLAG;   break;
385   case '-': k++; op = ROAR_RESET_FLAG; break;
[2339]386   default:
[2340]387     op = ROAR_SET_FLAG;
[2339]388  }
389
390  flag = 0;
391
392  if ( !strcmp(k, "sync") ) {
393   flag = ROAR_FLAG_SYNC;
394  } else if ( !strcmp(k, "meta") ) {
395   flag = ROAR_FLAG_META;
396  } else if ( !strcmp(k, "cleanmeta") ) {
397   flag = ROAR_FLAG_CLEANMETA;
398  } else if ( !strcmp(k, "pause") ) {
399   flag = ROAR_FLAG_PAUSE;
400  } else if ( !strcmp(k, "mute") ) {
401   flag = ROAR_FLAG_MUTE;
402  } else if ( !strcmp(k, "antiecho") ) {
403   flag = ROAR_FLAG_ANTIECHO;
[2413]404  } else if ( !strcmp(k, "passmixer") ) {
405   flag = ROAR_FLAG_PASSMIXER;
[2339]406  } else {
407   return -1;
408  }
409
410  g_config->streams[dir].flags |= flag;
411
[2340]412  if ( op == ROAR_RESET_FLAG )
[2339]413   g_config->streams[dir].flags -= flag;
414 }
415
416 return 0;
417}
418
[1145]419int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]420 int stream;
421 struct roar_stream * s;
422 struct roar_stream_server * ss;
[938]423 char * k, * v;
[1208]424#ifdef ROAR_DRIVER_CODEC
425 char * to_free = NULL;
426#endif
[1926]427 int sync = 0, f_mmap = 0;
[1522]428 int32_t blocks = -1, blocksize = -1;
[1920]429 int dir = ROAR_DIR_OUTPUT;
[1991]430 int error = 0;
431 // DMX:
432 int32_t channel  = -1;
433 int32_t universe = -1;
434 uint16_t tu16;
[2085]435 float q = -32e6;
[933]436
[938]437 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
[933]438
[1145]439 if ( drv == NULL && count == 0 ) {
440  drv  = ROAR_DRIVER_DEFAULT;
441  prim = 1;
442  sync = 1;
[1208]443
444#ifdef ROAR_DRIVER_CODEC
445  if ( opts == NULL ) {
446   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
447  }
448#endif
[1145]449 }
450
[1227]451 if ( opts == NULL && count == 0 ) {
452  sync = 1;
453  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
454 }
455
[933]456 if ( (stream = streams_new()) == -1 ) {
[938]457  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]458  if ( prim ) alive = 0;
[933]459  return -1;
460 }
461
462 streams_get(stream, &ss);
463 s = ROAR_STREAM(ss);
464
[2511]465 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
[933]466
467 s->pos_rel_id = -1;
468// s->info.codec = codec;
469
[938]470 k = strtok(opts, ",");
471 while (k != NULL) {
472//  ROAR_WARN("add_output(*): opts: %s", k);
473
474  if ( (v = strstr(k, "=")) != NULL ) {
475   *v++ = 0;
476  }
477
478  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
479  if ( strcmp(k, "rate") == 0 ) {
480   s->info.rate = atoi(v);
481  } else if ( strcmp(k, "channels") == 0 ) {
482   s->info.channels = atoi(v);
483  } else if ( strcmp(k, "bits") == 0 ) {
484   s->info.bits = atoi(v);
485  } else if ( strcmp(k, "codec") == 0 ) {
[2511]486   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
[938]487    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]488    error++;
[938]489   }
[2085]490  } else if ( strcmp(k, "q") == 0 ) {
491   q = atof(v);
[1221]492  } else if ( strcmp(k, "blocks") == 0 ) {
493   blocks = atoi(v);
[1522]494  } else if ( strcmp(k, "blocksize") == 0 ) {
495   blocksize = atoi(v);
[1926]496  } else if ( strcmp(k, "mmap") == 0 ) {
497   f_mmap = 1;
[1919]498  } else if ( strcmp(k, "subsystem") == 0 ) {
499   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]500    dir = ROAR_DIR_OUTPUT;
[2494]501#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1919]502   } else if ( !strcasecmp(v, "midi") ) {
503    dir = ROAR_DIR_MIDI_OUT;
[2494]504#endif
505#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1919]506   } else if ( !strcasecmp(v, "light") ) {
507    dir = ROAR_DIR_LIGHT_OUT;
[2494]508#endif
509#ifndef ROAR_WITHOUT_DCOMP_RAW
[2234]510   } else if ( !strcasecmp(v, "raw") ) {
511    dir = ROAR_DIR_RAW_OUT;
[2494]512#endif
[2681]513   } else if ( !strcasecmp(v, "complex") ) {
514    dir = ROAR_DIR_COMPLEX_OUT;
[1919]515   } else {
[2494]516    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]517    error++;
518   }
519  // DMX:
520  } else if ( strcmp(k, "channel") == 0 ) {
521   channel  = atoi(v);
522   if ( channel < 0 || channel > 65535 ) {
523    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
524    channel = -1;
525    error++;
[1919]526   }
[1991]527  } else if ( strcmp(k, "universe") == 0 ) {
528   universe = atoi(v);
529   if ( universe < 0 || universe > 255 ) {
530    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
531    universe = -1;
532    error++;
533   }
534
[2115]535  } else if ( strcmp(k, "name") == 0 ) {
536   if ( streams_set_name(stream, v) == -1 ) {
537    ROAR_ERR("add_output(*): Can not set Stream name");
538    error++;
539   }
[1991]540
[1032]541  } else if ( strcmp(k, "meta") == 0 ) {
542   streams_set_flag(stream, ROAR_FLAG_META);
[1117]543  } else if ( strcmp(k, "sync") == 0 ) {
544   sync = 1;
[1221]545  } else if ( strcmp(k, "primary") == 0 ) {
546   prim = 1;
547
548  } else if ( strcmp(k, "cleanmeta") == 0 ) {
549   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
550  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]551   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]552  } else if ( strcmp(k, "recsource") == 0 ) {
553   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
554  } else if ( strcmp(k, "passmixer") == 0 ) {
555   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]556  } else {
557   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]558   error++;
559  }
560
561  if ( error ) {
[938]562   streams_delete(stream);
[982]563   if ( prim ) alive = 0;
[1208]564#ifdef ROAR_DRIVER_CODEC
565   if ( to_free != NULL )
566    free(to_free);
567#endif
[938]568   return -1;
569  }
570
571  k = strtok(NULL, ",");
572 }
573
[2511]574 // set audio info...
575 switch (dir) {
576  case ROAR_DIR_LIGHT_OUT:
577    switch (s->info.codec) {
578     case ROAR_CODEC_DMX512:
579     case -1:
580       if ( s->info.rate == -1 )
581        s->info.rate = ROAR_OUTPUT_CFREQ;
582
583       s->info.channels =   0;
584       s->info.bits     =   8;
585       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
586      break;
587    }
588   break;
589  case ROAR_DIR_MIDI_OUT:
590    switch (s->info.codec) {
591     case ROAR_CODEC_MIDI:
592     case -1:
593       if ( s->info.rate == -1 )
594        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
595
596       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
597       s->info.bits     = ROAR_MIDI_BITS;
598       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
599      break;
600    }
601   break;
602  case ROAR_DIR_RAW_OUT:
603    if ( s->info.rate == -1 )
604     s->info.rate = 0;
605    if ( s->info.bits == -1 )
606     s->info.bits = 0;
607    if ( s->info.channels == -1 )
608     s->info.channels = 0;
609    if ( s->info.codec == -1 )
610     s->info.codec = 0;
611   break;
612 }
613
614 if ( s->info.rate == -1 )
615  s->info.rate = g_sa->rate;
616 if ( s->info.bits == -1 )
617  s->info.bits = g_sa->bits;
618 if ( s->info.channels == -1 )
619  s->info.channels = g_sa->channels;
620 if ( s->info.codec == -1 )
621  s->info.codec = g_sa->codec;
622
623 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
624
[1919]625 if ( streams_set_dir(stream, dir, 1) == -1 ) {
626  streams_delete(stream);
627  return -1;
628 }
629
[1208]630#ifdef ROAR_DRIVER_CODEC
631 if ( to_free != NULL )
632  free(to_free);
633#endif
634
[2511]635 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]636  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
637
[2511]638 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]639
[2364]640 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]641  ss->driver_id = -1; // don't close a driver not opened...
642  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]643  streams_delete(stream);
[938]644  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]645  if ( prim ) alive = 0;
[933]646  return -1;
647 }
648
[1528]649 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
650 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
651
[1221]652 if ( blocks != -1 )
653  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
654
[1522]655 if ( blocksize != -1 )
656  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
657
[1991]658 // TODO: we shoudld *really* check for errors here...
659 if ( channel != -1 ) {
660  tu16 = channel;
661  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
662 }
663 if ( universe != -1 ) {
664  tu16 = universe;
665  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
666 }
667
[1156]668 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
669
[938]670 streams_set_fh(stream, -1); // update some internal structures
671
[2087]672 if ( q > -1e6 ) {
673  ROAR_DBG("add_output(*): setting q=%f", q);
674  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
675 }
676
[2490]677 client_stream_add(g_self_client, stream);
[933]678
[1200]679 if ( prim ) {
[961]680  streams_mark_primary(stream);
[1200]681  s->pos_rel_id = stream;
682 }
[961]683
[1120]684 if ( sync ) {
[1117]685  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]686 } else {
687  streams_reset_flag(stream, ROAR_FLAG_SYNC);
688 }
[1117]689
[1926]690 if ( f_mmap )
691  streams_set_flag(stream, ROAR_FLAG_MMAP);
692
[2511]693 ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec);
[933]694 return 0;
[932]695}
696
[1993]697
698// SLP:
699void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
700 /* return the error code in the cookie */
701 *(SLPError*)cookie = errcode;
702}
703
704int register_slp (int unreg, char * sockname) {
705#ifdef ROAR_HAVE_LIBSLP
706 static int regged = 0;
707 static char * sn = NULL;
708 SLPError err;
709 SLPError callbackerr;
710 SLPHandle hslp;
711 char addr[1024];
712 char attr[1024] = "";
[2017]713 char * location;
[1993]714
715 if ( sockname != NULL )
716  sn = sockname;
717
[2016]718 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
[1993]719
720 err = SLPOpen("en", SLP_FALSE, &hslp);
721
722 if (err != SLP_OK) {
723  ROAR_ERR("Error opening slp handle: Error #%i", err);
724  return -1;
725 }
726
727 if (!unreg) {
[2017]728
729  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
730   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
731   SLPClose(hslp);
732   return -1;
733  }
734
[1995]735  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]736#ifndef ROAR_WITHOUT_DCOMP_LIGHT
737                               "(light-channels=%i),"
738#endif
739                               "(location=%s)",
[1995]740           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]741#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]742           g_light_state.channels,
[2494]743#endif
[2017]744           location
[1995]745          );
746
[1993]747  /* Register a service with SLP */
748  err = SLPReg(hslp,
749               addr,
750               SLP_LIFETIME_MAXIMUM,
751               0,
752               attr,
753               SLP_TRUE,
754               register_slp_callback,
755               &callbackerr);
[1994]756  regged = 1;
757 } else if ( unreg && regged ) {
758  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
759  regged = 0;
760 } else {
761  SLPClose(hslp);
762  return -1;
763 }
[1993]764
[1994]765 /* err may contain an error code that occurred as the slp library    */
766 /* _prepared_ to make the call.                                     */
767 if ( err != SLP_OK ) {
768  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
769  return -1;
770 }
771
772 /* callbackerr may contain an error code (that was assigned through */
773 /* the callback cookie) that occurred as slp packets were sent on    */
774 /* the wire */
775 if (callbackerr != SLP_OK) {
776  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
777  return -1;
[1993]778 }
779
780 SLPClose(hslp);
781 return 0;
782#else
783 return -1;
784#endif
785}
786
787
788// MAIN:
789
[1503]790#ifdef ROAR_HAVE_MAIN_ARGS
[0]791int main (int argc, char * argv[]) {
[1503]792#else
793int main (void) {
794#endif
795#ifdef ROAR_HAVE_MAIN_ARGS
[0]796 int i;
797 char * k;
[1503]798#endif
[1494]799#ifdef ROAR_SUPPORT_LISTEN
[905]800 char user_sock[80]  = {0};
[1494]801#endif
[1609]802 struct roar_audio_info sa, max_sa;
803 struct roard_config config;
[1486]804#ifdef ROAR_HAVE_FORK
[905]805 int    daemon       = 0;
[1486]806#endif
[905]807 int    realtime     = 0;
808 int    sysclocksync = 0;
[1207]809 char * driver    = NULL;
810 char * device    = NULL;
[1503]811#ifdef ROAR_HAVE_MAIN_ARGS
[1207]812 char * opts      = NULL;
[1503]813#endif
[60]814// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]815#ifdef ROAR_SUPPORT_LISTEN
[2530]816 int    port       = ROAR_DEFAULT_PORT;
817 char * sock_addr  = NULL;
818 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[1494]819#endif
[0]820 int               drvid;
[2486]821#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]822 char * s_drv     = "cf";
[1110]823 char * s_dev     = NULL;
[444]824 char * s_con     = NULL;
825 char * s_opt     = NULL;
826 int    s_prim    = 0;
[2486]827#endif
[1207]828 char * o_drv     = getenv("ROAR_DRIVER");
829 char * o_dev     = getenv("ROAR_DEVICE");
[932]830 char * o_opts    = NULL;
[961]831 int    o_prim    = 0;
[1145]832 int    o_count   = 0;
[2494]833#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]834 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]835#endif
[450]836 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]837 char * sock_user = NULL;
[1494]838#ifdef ROAR_SUPPORT_LISTEN
[517]839 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]840#endif
[1993]841#ifdef ROAR_HAVE_LIBSLP
842 int    reg_slp   = 0;
843#endif
[1486]844#ifdef ROAR_HAVE_CHROOT
[444]845 char * chrootdir = NULL;
[1486]846#endif
847#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]848 struct group   * grp  = NULL;
[1486]849#endif
850#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]851 struct passwd  * pwd  = NULL;
[1486]852#endif
853#ifdef ROAR_HAVE_GETSERVBYNAME
[446]854 struct servent * serv = NULL;
[1486]855#endif
[0]856 DRIVER_USERDATA_T drvinst;
[39]857 struct roar_client * self = NULL;
[508]858#ifdef ROAR_HAVE_LIBDNET
859 char decnethost[80];
860#endif
[2104]861#ifdef SUPPORT_PIDFILE
862 struct roar_vio_calls pidfile_vio;
863#endif
[0]864
865 g_standby       =  0;
[920]866 g_autostandby   =  0;
[982]867 alive           =  1;
[1494]868#ifdef ROAR_SUPPORT_LISTEN
[1155]869 g_no_listen     =  0;
[1494]870#else
871 g_terminate     =  1;
872#endif
[0]873
874 sa.bits     = ROAR_BITS_DEFAULT;
875 sa.channels = ROAR_CHANNELS_DEFAULT;
876 sa.rate     = ROAR_RATE_DEFAULT;
877 sa.codec    = ROAR_CODEC_DEFAULT;
878
[1609]879 g_sa        = &sa;
880 g_max_sa    = &max_sa;
881
882 memcpy(g_max_sa, g_sa, sizeof(max_sa));
883
884 g_config = &config;
885
886 if ( init_config() == -1 ) {
887  ROAR_ERR("Can not init default config!");
888  return 1;
889 }
[0]890
[2530]891#ifdef ROAR_SUPPORT_LISTEN
892 if ( init_listening() == -1 ) {
893  ROAR_ERR("Can not init listening sockets!");
894  return 1;
895 }
896#endif
897
[2500]898#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]899 if ( midi_init_config() == -1 ) {
900  ROAR_ERR("Can not init MIDI config!");
901  return 1;
902 }
[2500]903#endif
[60]904
[2489]905#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]906 if ( ssynth_init_config() == -1 ) {
907  ROAR_ERR("Can not init ssynth config!");
908  return 1;
909 }
[2489]910#endif
[2451]911
[2718]912#ifndef ROAR_WITHOUT_DCOMP_RDTCS
913 if ( rdtcs_init_config() == -1 ) {
914  ROAR_ERR("Can not init RDTCS config!");
915  return 1;
916 }
917#endif
918
[1494]919#ifdef ROAR_SUPPORT_LISTEN
[2530]920 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[1753]921#ifdef ROAR_HAVE_GETUID
[1486]922 if ( getuid() != 0 && getenv("HOME") != NULL ) {
923  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[2530]924  sock_addr = user_sock;
[60]925 }
[1753]926#endif
[60]927
[279]928 if ( getenv("ROAR_SERVER") != NULL )
[2530]929  sock_addr = getenv("ROAR_SERVER");
[1494]930#endif
[279]931
[63]932 if ( clients_init() == -1 ) {
933  ROAR_ERR("Can not init clients!");
934  return 1;
935 }
936
937 if ( streams_init() == -1 ) {
938  ROAR_ERR("Can not init streams!");
939  return 1;
940 }
941
[64]942 if ( (g_self_client = clients_new()) == -1 ) {
943  ROAR_ERR("Can not create self client!");
944  return 1;
945 }
946
[2485]947#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]948 if ( sources_init() == -1 ) {
949  ROAR_ERR("Can not init sources!");
950  return 1;
951 }
952
[64]953 if ( (sources_set_client(g_self_client)) == -1 ) {
954  ROAR_ERR("Can not init set source client!");
955  return 1;
956 }
[2485]957#endif
[64]958
[1503]959#ifdef ROAR_HAVE_MAIN_ARGS
[0]960 for (i = 1; i < argc; i++) {
961  k = argv[i];
962
963  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
964   usage();
965   return 0;
966
[2109]967  } else if ( strcmp(k, "--start") == 0 ) {
968   // this is a no op
[579]969  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]970#ifdef ROAR_SUPPORT_LISTEN
[2530]971   if ( restart_server(sock_addr, 1) == -1 ) {
972    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
[579]973   }
[1494]974#else
975   ROAR_ERR("--restart not supported");
976#endif
[2109]977  } else if ( strcmp(k, "--shutdown") == 0 ) {
978#ifdef ROAR_SUPPORT_LISTEN
[2530]979   if ( restart_server(sock_addr, 1) == -1 ) {
980    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]981    return 1;
982   }
983   return 0;
984#else
985   ROAR_ERR("--shutdown not supported");
986   return 1;
987#endif
988  } else if ( strcmp(k, "--stop") == 0 ) {
989#ifdef ROAR_SUPPORT_LISTEN
[2530]990   if ( restart_server(sock_addr, 0) == -1 ) {
991    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]992    return 1;
993   }
994   return 0;
995#else
996   ROAR_ERR("--stop not supported");
997   return 1;
998#endif
999
[579]1000
[775]1001  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]1002#ifdef ROAR_HAVE_FORK
[775]1003   daemon = 1;
[1486]1004#else
1005   ROAR_ERR("--daemon not supported");
1006#endif
[71]1007  } else if ( strcmp(k, "--terminate") == 0 ) {
1008   g_terminate = 1;
[905]1009  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1010   sysclocksync = 1000;
[275]1011  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]1012   realtime++;
[444]1013  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]1014#ifdef ROAR_HAVE_CHROOT
[444]1015   chrootdir = argv[++i];
[1486]1016#else
1017   ROAR_ERR("--chroot not supported");
1018   i++;
1019#endif
[444]1020  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1021#ifdef ROAR_HAVE_SETGID
[444]1022   setids |= R_SETGID;
[1486]1023#else
1024   ROAR_ERR("--setgid not supported");
1025#endif
[444]1026  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1027#ifdef ROAR_HAVE_SETUID
[444]1028   setids |= R_SETUID;
[1486]1029#else
1030   ROAR_ERR("--setuid not supported");
1031#endif
[2017]1032  } else if ( strcmp(k, "--location") == 0 ) {
1033   g_config->location = argv[++i];
[2104]1034  } else if ( strcmp(k, "--pidfile") == 0 ) {
1035#ifdef SUPPORT_PIDFILE
1036   pidfile = argv[++i];
1037#else
1038   ROAR_ERR("--pidfile not supported");
1039   i++;
1040#endif
[68]1041
[280]1042  } else if ( strcmp(k, "--list-cf") == 0 ) {
1043   print_codecfilterlist();
1044   return 0;
1045
[0]1046  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1047   sa.rate = atoi(argv[++i]);
1048  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1049   sa.bits = atoi(argv[++i]);
1050  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1051   sa.channels = atoi(argv[++i]);
1052
[2339]1053  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1054   if ( update_stream_flags(argv[++i]) == -1 ) {
1055    ROAR_ERR("Can not set stream flags");
1056    return 1;
1057   }
1058
[0]1059  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1060   driver = argv[++i];
1061   if ( strcmp(driver, "list") == 0 ) {
[974]1062    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1063    print_driverlist();
[973]1064    return 0;
[0]1065   }
1066  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1067   device = argv[++i];
1068  } else if ( strcmp(k, "-dO") == 0 ) {
1069   opts = argv[++i];
[973]1070  } else if ( strcmp(k, "--list-driver") == 0 ) {
1071   print_driverlist();
1072   return 0;
[0]1073
[932]1074  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1075   o_drv  = argv[++i];
1076  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1077   o_dev  = argv[++i];
1078  } else if ( strcmp(k, "-oO") == 0 ) {
1079   o_opts = argv[++i];
[961]1080  } else if ( strcmp(k, "-oP") == 0 ) {
1081   o_prim = 1;
[932]1082  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1083   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1084    o_count++;
1085
[961]1086   o_drv  = o_dev = o_opts = NULL;
1087   o_prim = 0;
[932]1088
[0]1089  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]1090#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1091   s_drv = argv[++i];
[2485]1092#else
1093   ROAR_ERR("main(*): No support for sources compiled in");
1094#endif
[550]1095  } else if ( strcmp(k, "-S") == 0 ) {
[2485]1096#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1097   s_dev = argv[++i];
[2485]1098#else
1099   ROAR_ERR("main(*): No support for sources compiled in");
1100#endif
[0]1101  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]1102#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1103   s_opt = argv[++i];
[2485]1104#else
1105   ROAR_ERR("main(*): No support for sources compiled in");
1106#endif
[0]1107  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]1108#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1109   s_con = argv[++i];
[2485]1110#else
1111   ROAR_ERR("main(*): No support for sources compiled in");
1112#endif
[0]1113  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1114#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1115   s_prim = 1;
[2485]1116#else
1117   ROAR_ERR("main(*): No support for sources compiled in");
1118#endif
[1110]1119  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1120#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1121   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1122    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1123   }
1124   s_opt = s_dev = s_con = NULL;
1125   s_drv = "cf";
1126   s_prim = 0;
[2485]1127#else
1128   ROAR_ERR("main(*): No support for sources compiled in");
1129#endif
[2270]1130  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1131#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1132   print_sourcelist();
1133   return 0;
[2485]1134#else
1135   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1136   return 1;
[2485]1137#endif
[0]1138
[1923]1139  } else if ( strcmp(k, "--light-channels") == 0 ) {
[2494]1140#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1141   light_channels = atoi(argv[++i]);
[2494]1142#else
1143   ROAR_WARN("main(*): no light subsystem compiled in");
1144#endif
[1923]1145
[2725]1146  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1147#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1148   g_rdtcs.rds.pi = atoi(argv[++i]);
1149#else
1150   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1151#endif
[2728]1152  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1153#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1154   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
[2729]1155    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
[2728]1156    return 1;
1157   }
1158#else
1159   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1160#endif
1161  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1162#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1163   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
[2729]1164    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
[2728]1165    return 1;
1166   }
1167#else
1168   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1169#endif
1170  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1171#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1172   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1173    ROAR_ERR("Can not set RDS TP flag");
1174    return 1;
1175   }
1176#else
1177   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1178#endif
1179  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1180#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1181   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1182    ROAR_ERR("Can not set RDS CT flag");
1183    return 1;
1184   }
1185#else
1186   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1187#endif
1188
[2725]1189
[1924]1190  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1191#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1192   midi_config.init_cb = 0;
[2487]1193#else
1194   // no warning here as this is the disable option
1195#endif
[2444]1196  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1197#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]1198   midi_config.init_cb = 1;
[2487]1199#else
1200   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1201#endif
[1924]1202  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]1203#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1204   midi_config.console_dev = argv[++i];
[2444]1205   midi_config.init_cb = 1;
[2487]1206#else
1207   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1208#endif
[1924]1209
[2451]1210  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1211#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1212   ssynth_conf.enable = 1;
[2489]1213#else
1214   ROAR_ERR("main(*): No support for ssynth compiled in");
1215#endif
[2451]1216  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1217#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1218   ssynth_conf.enable = 0;
[2489]1219#else
1220   // we can safely ignore the disable
1221#endif
[2451]1222
[0]1223  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]1224   // This is only usefull in INET not UNIX mode.
[1494]1225#ifdef ROAR_SUPPORT_LISTEN
[2530]1226   if ( *sock_addr == '/' )
1227    sock_addr = ROAR_DEFAULT_HOST;
[447]1228
[446]1229   errno = 0;
1230   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1231#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1232    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1233     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1234     return 1;
1235    }
1236    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1237    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1238            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1239    port = ROAR_NET2HOST16(serv->s_port);
[1486]1240#else
1241    ROAR_ERR("invalite port number: %s", argv[i]);
1242    return 1;
1243#endif
[446]1244   }
[1494]1245#endif
[1115]1246  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]1247#ifdef ROAR_SUPPORT_LISTEN
[2530]1248   sock_addr = argv[++i];
1249#endif
1250
1251  } else if ( strcmp(k, "--proto") == 0 ) {
1252#ifdef ROAR_SUPPORT_LISTEN
[2549]1253   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]1254    ROAR_ERR("Unknown protocol: %s", argv[i]);
1255    return 1;
1256   }
[1494]1257#endif
[518]1258
[573]1259  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1260#ifdef ROAR_SUPPORT_LISTEN
[518]1261   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1262    sock_type = ROAR_SOCKET_TYPE_TCP;
1263
[2530]1264   if ( *sock_addr == '/' )
1265    sock_addr = ROAR_DEFAULT_HOST;
[1494]1266#endif
[518]1267
1268  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1269#ifdef ROAR_SUPPORT_LISTEN
[517]1270   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]1271   if ( *sock_addr == '/' )
1272    sock_addr = ROAR_DEFAULT_HOST;
[1494]1273#endif
[518]1274  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1275#ifdef ROAR_SUPPORT_LISTEN
[519]1276#ifdef PF_INET6
[518]1277   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]1278   if ( *sock_addr == '/' )
1279    sock_addr = ROAR_DEFAULT_HOST;
[519]1280#else
1281    ROAR_ERR("No IPv6 support compiled in!");
1282    return 1;
1283#endif
[1494]1284#endif
[518]1285
[573]1286  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1287#ifdef ROAR_SUPPORT_LISTEN
[62]1288   // ignore this case as it is the default behavor.
[517]1289   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1290#endif
[518]1291
[573]1292  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1293#ifdef ROAR_SUPPORT_LISTEN
[508]1294#ifdef ROAR_HAVE_LIBDNET
1295    port   = ROAR_DEFAULT_NUM;
1296    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]1297    sock_addr = decnethost;
[517]1298    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1299#else
1300    ROAR_ERR("No DECnet support compiled in!");
1301    return 1;
1302#endif
[1494]1303#endif
[2530]1304  } else if ( strcmp(k, "--new-sock") == 0 ) {
1305#ifdef ROAR_SUPPORT_LISTEN
1306   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1307    ROAR_ERR("Can not open listen socket!");
1308    return 1;
1309   }
1310#endif
[518]1311
[1993]1312  } else if ( strcmp(k, "--slp") == 0 ) {
1313#ifdef ROAR_HAVE_LIBSLP
1314   reg_slp = 1;
1315#else
1316    ROAR_ERR("No OpenSLP support compiled in!");
1317    return 1;
1318#endif
1319
[60]1320  } else if ( strcmp(k, "-G") == 0 ) {
[444]1321   sock_grp  = argv[++i];
1322  } else if ( strcmp(k, "-U") == 0 ) {
1323   sock_user = argv[++i];
[0]1324
[68]1325  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1326#ifdef ROAR_SUPPORT_LISTEN
[2530]1327   sock_addr   = "";
[548]1328   g_terminate = 1;
[1155]1329   g_no_listen = 1;
[1494]1330#endif
[68]1331  } else if ( strcmp(k, "--client-fh") == 0 ) {
1332   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1333    ROAR_ERR("main(*): Can not set client's fh");
1334    return 1;
1335   }
[501]1336  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1337#ifdef ROAR_HAVE_IO_POSIX
[501]1338   close(atoi(argv[++i]));
[1486]1339#else
1340   i++;
1341   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1342#endif
[68]1343
[920]1344  } else if ( strcmp(k, "--standby") == 0 ) {
1345   g_standby = 1;
1346  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1347   g_autostandby = 1;
[0]1348  } else {
1349   usage();
1350   return 1;
1351  }
1352
1353 }
[1503]1354#endif
[0]1355
[2485]1356#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1357 if ( s_dev != NULL ) {
1358  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1359   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1360  }
1361 }
[2485]1362#endif
[1110]1363
[1145]1364 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1365
[0]1366 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1367
[2500]1368#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]1369 if ( midi_init() == -1 ) {
[281]1370  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1371 }
[2500]1372#endif
[1819]1373
[2489]1374#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1375 if ( ssynth_init() == -1 ) {
1376  ROAR_ERR("Can not initialize ssynth subsystem");
1377 }
[2489]1378#endif
[2451]1379
[2494]1380#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1381 if ( light_init(light_channels) == -1 ) {
1382  ROAR_ERR("Can not initialize light control subsystem");
1383 }
[2494]1384#endif
[281]1385
[2718]1386#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1387 if ( rdtcs_init() == -1 ) {
1388  ROAR_ERR("Can not initialize RDTCS subsystem");
1389 }
1390#endif
1391
[1494]1392#ifdef ROAR_SUPPORT_LISTEN
[2530]1393 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1394  ROAR_ERR("Can not open listen socket!");
1395  return 1;
[60]1396 }
[1494]1397#endif
[60]1398
[0]1399 if ( output_buffer_init(&sa) == -1 ) {
1400  ROAR_ERR("Can not init output buffer!");
1401  return 1;
1402 }
1403
[1145]1404 if ( driver == NULL ) {
1405  driver = "null";
1406 } else {
1407  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1408 }
1409
[0]1410 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1411  ROAR_ERR("Can not open output driver!");
1412  return 1;
1413 }
1414
[44]1415 if ( samples_init() == -1 ) {
1416  ROAR_ERR("Can not init samples!");
1417  return 1;
1418 }
1419
1420
[1486]1421 // we should handle this on microcontrollers, too.
[1753]1422#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1423 signal(SIGINT,  on_sig_int);
[285]1424 signal(SIGCHLD, on_sig_chld);
[2112]1425 signal(SIGUSR1, on_sig_usr1);
[0]1426 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1427#endif
[0]1428
[275]1429 if ( realtime ) {
[278]1430#ifdef DEBUG
1431  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1432#endif
1433
[1486]1434#ifdef ROAR_HAVE_NICE
[275]1435  errno = 0;
[276]1436  nice(-5*realtime); // -5 for each --realtime
[1486]1437  if ( errno ) {
1438   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1439  }
1440#else
1441  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1442#endif
[277]1443/*
[276]1444#ifdef __linux__
[277]1445  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1446   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1447#endif
[277]1448*/
[275]1449 }
1450
[1486]1451#ifdef ROAR_HAVE_SETGID
[444]1452 if ( setids & R_SETGID ) {
[2584]1453  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1454   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1455   return 1;
1456  }
[444]1457  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1458   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1459  }
[523]1460  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1461   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1462  }
1463 }
[1486]1464#endif
[444]1465
[0]1466
[39]1467 clients_set_pid(g_self_client, getpid());
[1753]1468#ifdef ROAR_HAVE_GETUID
[440]1469 clients_set_uid(g_self_client, getuid());
[1753]1470#endif
1471#ifdef ROAR_HAVE_GETGID
[440]1472 clients_set_gid(g_self_client, getgid());
[1753]1473#endif
[39]1474 clients_get(g_self_client, &self);
[37]1475
[39]1476 if ( self == NULL ) {
1477  ROAR_ERR("Can not get self client!");
1478  return 1;
1479 }
1480
[775]1481 strcpy(self->name, "RoarAudio daemon internal");
[68]1482
[1486]1483#ifdef ROAR_HAVE_FORK
[775]1484 if ( daemon ) {
[68]1485  close(ROAR_STDIN );
1486  close(ROAR_STDOUT);
1487  close(ROAR_STDERR);
[1753]1488
[68]1489  if ( fork() )
[1486]1490   ROAR_U_EXIT(0);
[1753]1491
1492#ifdef ROAR_HAVE_SETSID
1493  setsid();
1494#endif
[1046]1495  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1496 }
[1486]1497#endif
[68]1498
[2104]1499#ifdef SUPPORT_PIDFILE
[2106]1500 if ( pidfile != NULL ) {
1501  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1502   ROAR_ERR("Can not write pidfile: %s", pidfile);
1503  } else {
1504   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1505   roar_vio_close(&pidfile_vio);
1506  }
[2104]1507 }
1508#endif
1509
[1486]1510#ifdef ROAR_HAVE_CHROOT
[444]1511 if (chrootdir) {
1512  if ( chroot(chrootdir) == -1 ) {
1513   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1514   return 2;
1515  }
1516  if ( chdir("/") == -1 ) {
1517   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1518   return 2;
1519  }
1520 }
[1486]1521#endif
[444]1522
[1486]1523#ifdef ROAR_HAVE_SETUID
[444]1524 if ( setids & R_SETUID ) {
[2584]1525  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1526   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1527   return 1;
1528  }
[444]1529  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1530   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1531   return 3;
1532  }
[1753]1533#ifdef ROAR_HAVE_GETUID
[444]1534  clients_set_uid(g_self_client, getuid());
[1753]1535#endif
[444]1536 }
[1486]1537#endif
[444]1538
[1993]1539 // Register with OpenSLP:
[2028]1540#ifdef ROAR_HAVE_LIBSLP
[1993]1541 if ( reg_slp ) {
[2530]1542  register_slp(0, sock_addr);
[1993]1543 }
[2028]1544#endif
[1993]1545
[0]1546 // start main loop...
[905]1547 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1548
1549 // clean up.
1550 clean_quit_prep();
1551 driver_close(drvinst, drvid);
1552 output_buffer_free();
1553
1554 return 0;
1555}
1556
[574]1557void cleanup_listen_socket (int terminate) {
[2530]1558 int i;
1559
[1993]1560 // Deregister from SLP:
[2028]1561#ifdef ROAR_HAVE_LIBSLP
[1993]1562 register_slp(1, NULL);
[2028]1563#endif
[580]1564
[1494]1565#ifdef ROAR_SUPPORT_LISTEN
[2530]1566 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1567  if ( g_listen_socket[i] != -1 ) {
[1486]1568#ifdef ROAR_HAVE_IO_POSIX
[2530]1569   close(g_listen_socket[i]);
[1486]1570#endif // #else is useless because we are in void context.
[60]1571
[2530]1572   g_listen_socket[i] = -1;
[576]1573
[1486]1574#ifdef ROAR_HAVE_UNIX
[2530]1575   if ( server[i] != NULL )
1576    if ( *(server[i]) == '/' )
1577     unlink(server[i]);
[1486]1578#endif
[2530]1579  }
[580]1580 }
[60]1581
[1494]1582#endif
1583
[574]1584 if ( terminate )
1585  g_terminate = 1;
1586}
1587
1588void clean_quit_prep (void) {
1589 cleanup_listen_socket(0);
[60]1590
[2485]1591#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1592 sources_free();
[2485]1593#endif
[0]1594 streams_free();
1595 clients_free();
[2489]1596#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1597 ssynth_free();
[2489]1598#endif
[2487]1599#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1600 midi_cb_stop(); // stop console beep
[2487]1601#endif
[2500]1602#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]1603 midi_free();
[2500]1604#endif
[2494]1605#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1606 light_free();
[2494]1607#endif
[2718]1608#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1609 rdtcs_free();
1610#endif
[2104]1611
1612#ifdef SUPPORT_PIDFILE
1613 if ( pidfile != NULL )
1614  unlink(pidfile);
1615#endif
[0]1616}
1617
1618void clean_quit (void) {
1619 clean_quit_prep();
1620// driver_close(drvinst, drvid);
1621// output_buffer_free();
1622 exit(0);
1623}
1624
1625//ll
Note: See TracBrowser for help on using the repository browser.