source: roaraudio/roard/roard.c @ 2737:5d3d02d2b38b

Last change on this file since 2737:5d3d02d2b38b was 2737:5d3d02d2b38b, checked in by phi, 15 years ago

typo

File size: 42.9 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"
[2737]159        "                         You need the permissions to change the GID\n"
[444]160        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
[2737]161        "                         You need the permissions 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
[2736]356 // in case we opened the listening socket correctly.
357 server[sockid] = addr;
[2530]358 return 0;
359}
360#endif
361
[2339]362int update_stream_flags (char * str) {
363 int    dir;
364 char * flags;
365 char * k;
366 int    op;
367 int    flag;
368
369 if ( (flags = strstr(str, "=")) == NULL )
370  return -1;
371
372 *flags = 0;
373  flags++;
374
375 if ( (dir = roar_str2dir(str)) == -1 )
376  return -1;
377
378 while (flags != NULL) {
379  k = flags;
380  flags = strstr(flags, ",");
381
382  if ( flags != NULL )
383   *(flags++) = 0;
384
385  switch (*k) {
[2340]386   case '+': k++; op = ROAR_SET_FLAG;   break;
387   case '-': k++; op = ROAR_RESET_FLAG; break;
[2339]388   default:
[2340]389     op = ROAR_SET_FLAG;
[2339]390  }
391
392  flag = 0;
393
394  if ( !strcmp(k, "sync") ) {
395   flag = ROAR_FLAG_SYNC;
396  } else if ( !strcmp(k, "meta") ) {
397   flag = ROAR_FLAG_META;
398  } else if ( !strcmp(k, "cleanmeta") ) {
399   flag = ROAR_FLAG_CLEANMETA;
400  } else if ( !strcmp(k, "pause") ) {
401   flag = ROAR_FLAG_PAUSE;
402  } else if ( !strcmp(k, "mute") ) {
403   flag = ROAR_FLAG_MUTE;
404  } else if ( !strcmp(k, "antiecho") ) {
405   flag = ROAR_FLAG_ANTIECHO;
[2413]406  } else if ( !strcmp(k, "passmixer") ) {
407   flag = ROAR_FLAG_PASSMIXER;
[2339]408  } else {
409   return -1;
410  }
411
412  g_config->streams[dir].flags |= flag;
413
[2340]414  if ( op == ROAR_RESET_FLAG )
[2339]415   g_config->streams[dir].flags -= flag;
416 }
417
418 return 0;
419}
420
[1145]421int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]422 int stream;
423 struct roar_stream * s;
424 struct roar_stream_server * ss;
[938]425 char * k, * v;
[1208]426#ifdef ROAR_DRIVER_CODEC
427 char * to_free = NULL;
428#endif
[1926]429 int sync = 0, f_mmap = 0;
[1522]430 int32_t blocks = -1, blocksize = -1;
[1920]431 int dir = ROAR_DIR_OUTPUT;
[1991]432 int error = 0;
433 // DMX:
434 int32_t channel  = -1;
435 int32_t universe = -1;
436 uint16_t tu16;
[2085]437 float q = -32e6;
[933]438
[938]439 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
[933]440
[1145]441 if ( drv == NULL && count == 0 ) {
442  drv  = ROAR_DRIVER_DEFAULT;
443  prim = 1;
444  sync = 1;
[1208]445
446#ifdef ROAR_DRIVER_CODEC
447  if ( opts == NULL ) {
448   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
449  }
450#endif
[1145]451 }
452
[1227]453 if ( opts == NULL && count == 0 ) {
454  sync = 1;
455  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
456 }
457
[933]458 if ( (stream = streams_new()) == -1 ) {
[938]459  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]460  if ( prim ) alive = 0;
[933]461  return -1;
462 }
463
464 streams_get(stream, &ss);
465 s = ROAR_STREAM(ss);
466
[2511]467 memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1
[933]468
469 s->pos_rel_id = -1;
470// s->info.codec = codec;
471
[938]472 k = strtok(opts, ",");
473 while (k != NULL) {
474//  ROAR_WARN("add_output(*): opts: %s", k);
475
476  if ( (v = strstr(k, "=")) != NULL ) {
477   *v++ = 0;
478  }
479
480  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
481  if ( strcmp(k, "rate") == 0 ) {
482   s->info.rate = atoi(v);
483  } else if ( strcmp(k, "channels") == 0 ) {
484   s->info.channels = atoi(v);
485  } else if ( strcmp(k, "bits") == 0 ) {
486   s->info.bits = atoi(v);
487  } else if ( strcmp(k, "codec") == 0 ) {
[2511]488   if ( (s->info.codec = roar_str2codec(v)) == -1 ) {
[938]489    ROAR_ERR("add_output(*): unknown codec '%s'", v);
[1991]490    error++;
[938]491   }
[2085]492  } else if ( strcmp(k, "q") == 0 ) {
493   q = atof(v);
[1221]494  } else if ( strcmp(k, "blocks") == 0 ) {
495   blocks = atoi(v);
[1522]496  } else if ( strcmp(k, "blocksize") == 0 ) {
497   blocksize = atoi(v);
[1926]498  } else if ( strcmp(k, "mmap") == 0 ) {
499   f_mmap = 1;
[1919]500  } else if ( strcmp(k, "subsystem") == 0 ) {
501   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]502    dir = ROAR_DIR_OUTPUT;
[2494]503#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1919]504   } else if ( !strcasecmp(v, "midi") ) {
505    dir = ROAR_DIR_MIDI_OUT;
[2494]506#endif
507#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1919]508   } else if ( !strcasecmp(v, "light") ) {
509    dir = ROAR_DIR_LIGHT_OUT;
[2494]510#endif
511#ifndef ROAR_WITHOUT_DCOMP_RAW
[2234]512   } else if ( !strcasecmp(v, "raw") ) {
513    dir = ROAR_DIR_RAW_OUT;
[2494]514#endif
[2681]515   } else if ( !strcasecmp(v, "complex") ) {
516    dir = ROAR_DIR_COMPLEX_OUT;
[1919]517   } else {
[2494]518    ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k);
[1991]519    error++;
520   }
521  // DMX:
522  } else if ( strcmp(k, "channel") == 0 ) {
523   channel  = atoi(v);
524   if ( channel < 0 || channel > 65535 ) {
525    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
526    channel = -1;
527    error++;
[1919]528   }
[1991]529  } else if ( strcmp(k, "universe") == 0 ) {
530   universe = atoi(v);
531   if ( universe < 0 || universe > 255 ) {
532    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
533    universe = -1;
534    error++;
535   }
536
[2115]537  } else if ( strcmp(k, "name") == 0 ) {
538   if ( streams_set_name(stream, v) == -1 ) {
539    ROAR_ERR("add_output(*): Can not set Stream name");
540    error++;
541   }
[1991]542
[1032]543  } else if ( strcmp(k, "meta") == 0 ) {
544   streams_set_flag(stream, ROAR_FLAG_META);
[1117]545  } else if ( strcmp(k, "sync") == 0 ) {
546   sync = 1;
[1221]547  } else if ( strcmp(k, "primary") == 0 ) {
548   prim = 1;
549
550  } else if ( strcmp(k, "cleanmeta") == 0 ) {
551   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
552  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]553   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[2413]554  } else if ( strcmp(k, "recsource") == 0 ) {
555   streams_set_flag(stream, ROAR_FLAG_RECSOURCE);
556  } else if ( strcmp(k, "passmixer") == 0 ) {
557   streams_set_flag(stream, ROAR_FLAG_PASSMIXER);
[938]558  } else {
559   ROAR_ERR("add_output(*): unknown option '%s'", k);
[1991]560   error++;
561  }
562
563  if ( error ) {
[938]564   streams_delete(stream);
[982]565   if ( prim ) alive = 0;
[1208]566#ifdef ROAR_DRIVER_CODEC
567   if ( to_free != NULL )
568    free(to_free);
569#endif
[938]570   return -1;
571  }
572
573  k = strtok(NULL, ",");
574 }
575
[2511]576 // set audio info...
577 switch (dir) {
578  case ROAR_DIR_LIGHT_OUT:
579    switch (s->info.codec) {
580     case ROAR_CODEC_DMX512:
581     case -1:
582       if ( s->info.rate == -1 )
583        s->info.rate = ROAR_OUTPUT_CFREQ;
584
585       s->info.channels =   0;
586       s->info.bits     =   8;
587       s->info.codec    = ROAR_CODEC_DMX512; // in case codec == -1
588      break;
589    }
590   break;
591  case ROAR_DIR_MIDI_OUT:
592    switch (s->info.codec) {
593     case ROAR_CODEC_MIDI:
594     case -1:
595       if ( s->info.rate == -1 )
596        s->info.rate    = ROAR_MIDI_TICKS_PER_BEAT;
597
598       s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
599       s->info.bits     = ROAR_MIDI_BITS;
600       s->info.codec    = ROAR_CODEC_MIDI; // in case codec == -1
601      break;
602    }
603   break;
604  case ROAR_DIR_RAW_OUT:
605    if ( s->info.rate == -1 )
606     s->info.rate = 0;
607    if ( s->info.bits == -1 )
608     s->info.bits = 0;
609    if ( s->info.channels == -1 )
610     s->info.channels = 0;
611    if ( s->info.codec == -1 )
612     s->info.codec = 0;
613   break;
614 }
615
616 if ( s->info.rate == -1 )
617  s->info.rate = g_sa->rate;
618 if ( s->info.bits == -1 )
619  s->info.bits = g_sa->bits;
620 if ( s->info.channels == -1 )
621  s->info.channels = g_sa->channels;
622 if ( s->info.codec == -1 )
623  s->info.codec = g_sa->codec;
624
625 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);
626
[1919]627 if ( streams_set_dir(stream, dir, 1) == -1 ) {
628  streams_delete(stream);
629  return -1;
630 }
631
[1208]632#ifdef ROAR_DRIVER_CODEC
633 if ( to_free != NULL )
634  free(to_free);
635#endif
636
[2511]637 if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW )
[941]638  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
639
[2511]640 ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec;
[933]641
[2364]642 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) {
[1581]643  ss->driver_id = -1; // don't close a driver not opened...
644  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]645  streams_delete(stream);
[938]646  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]647  if ( prim ) alive = 0;
[933]648  return -1;
649 }
650
[1528]651 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
652 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
653
[1221]654 if ( blocks != -1 )
655  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
656
[1522]657 if ( blocksize != -1 )
658  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
659
[1991]660 // TODO: we shoudld *really* check for errors here...
661 if ( channel != -1 ) {
662  tu16 = channel;
663  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
664 }
665 if ( universe != -1 ) {
666  tu16 = universe;
667  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
668 }
669
[1156]670 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
671
[938]672 streams_set_fh(stream, -1); // update some internal structures
673
[2087]674 if ( q > -1e6 ) {
675  ROAR_DBG("add_output(*): setting q=%f", q);
676  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
677 }
678
[2490]679 client_stream_add(g_self_client, stream);
[933]680
[1200]681 if ( prim ) {
[961]682  streams_mark_primary(stream);
[1200]683  s->pos_rel_id = stream;
684 }
[961]685
[1120]686 if ( sync ) {
[1117]687  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]688 } else {
689  streams_reset_flag(stream, ROAR_FLAG_SYNC);
690 }
[1117]691
[1926]692 if ( f_mmap )
693  streams_set_flag(stream, ROAR_FLAG_MMAP);
694
[2511]695 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]696 return 0;
[932]697}
698
[1993]699
700// SLP:
701void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
702 /* return the error code in the cookie */
703 *(SLPError*)cookie = errcode;
704}
705
706int register_slp (int unreg, char * sockname) {
707#ifdef ROAR_HAVE_LIBSLP
708 static int regged = 0;
709 static char * sn = NULL;
710 SLPError err;
711 SLPError callbackerr;
712 SLPHandle hslp;
713 char addr[1024];
714 char attr[1024] = "";
[2017]715 char * location;
[1993]716
717 if ( sockname != NULL )
718  sn = sockname;
719
[2016]720 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
[1993]721
722 err = SLPOpen("en", SLP_FALSE, &hslp);
723
724 if (err != SLP_OK) {
725  ROAR_ERR("Error opening slp handle: Error #%i", err);
726  return -1;
727 }
728
729 if (!unreg) {
[2017]730
731  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
732   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
733   SLPClose(hslp);
734   return -1;
735  }
736
[1995]737  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
[2494]738#ifndef ROAR_WITHOUT_DCOMP_LIGHT
739                               "(light-channels=%i),"
740#endif
741                               "(location=%s)",
[1995]742           g_sa->rate, g_sa->channels, g_sa->bits,
[2494]743#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[2017]744           g_light_state.channels,
[2494]745#endif
[2017]746           location
[1995]747          );
748
[1993]749  /* Register a service with SLP */
750  err = SLPReg(hslp,
751               addr,
752               SLP_LIFETIME_MAXIMUM,
753               0,
754               attr,
755               SLP_TRUE,
756               register_slp_callback,
757               &callbackerr);
[1994]758  regged = 1;
759 } else if ( unreg && regged ) {
760  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
761  regged = 0;
762 } else {
763  SLPClose(hslp);
764  return -1;
765 }
[1993]766
[1994]767 /* err may contain an error code that occurred as the slp library    */
768 /* _prepared_ to make the call.                                     */
769 if ( err != SLP_OK ) {
770  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
771  return -1;
772 }
773
774 /* callbackerr may contain an error code (that was assigned through */
775 /* the callback cookie) that occurred as slp packets were sent on    */
776 /* the wire */
777 if (callbackerr != SLP_OK) {
778  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
779  return -1;
[1993]780 }
781
782 SLPClose(hslp);
783 return 0;
784#else
785 return -1;
786#endif
787}
788
789
790// MAIN:
791
[1503]792#ifdef ROAR_HAVE_MAIN_ARGS
[0]793int main (int argc, char * argv[]) {
[1503]794#else
795int main (void) {
796#endif
797#ifdef ROAR_HAVE_MAIN_ARGS
[0]798 int i;
799 char * k;
[1503]800#endif
[1494]801#ifdef ROAR_SUPPORT_LISTEN
[905]802 char user_sock[80]  = {0};
[1494]803#endif
[1609]804 struct roar_audio_info sa, max_sa;
805 struct roard_config config;
[1486]806#ifdef ROAR_HAVE_FORK
[905]807 int    daemon       = 0;
[1486]808#endif
[905]809 int    realtime     = 0;
810 int    sysclocksync = 0;
[1207]811 char * driver    = NULL;
812 char * device    = NULL;
[1503]813#ifdef ROAR_HAVE_MAIN_ARGS
[1207]814 char * opts      = NULL;
[1503]815#endif
[60]816// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]817#ifdef ROAR_SUPPORT_LISTEN
[2530]818 int    port       = ROAR_DEFAULT_PORT;
819 char * sock_addr  = NULL;
820 int    sock_proto = ROAR_PROTO_ROARAUDIO;
[1494]821#endif
[0]822 int               drvid;
[2486]823#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]824 char * s_drv     = "cf";
[1110]825 char * s_dev     = NULL;
[444]826 char * s_con     = NULL;
827 char * s_opt     = NULL;
828 int    s_prim    = 0;
[2486]829#endif
[1207]830 char * o_drv     = getenv("ROAR_DRIVER");
831 char * o_dev     = getenv("ROAR_DEVICE");
[932]832 char * o_opts    = NULL;
[961]833 int    o_prim    = 0;
[1145]834 int    o_count   = 0;
[2494]835#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]836 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[2494]837#endif
[450]838 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]839 char * sock_user = NULL;
[1494]840#ifdef ROAR_SUPPORT_LISTEN
[517]841 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]842#endif
[1993]843#ifdef ROAR_HAVE_LIBSLP
844 int    reg_slp   = 0;
845#endif
[1486]846#ifdef ROAR_HAVE_CHROOT
[444]847 char * chrootdir = NULL;
[1486]848#endif
849#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]850 struct group   * grp  = NULL;
[1486]851#endif
852#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]853 struct passwd  * pwd  = NULL;
[1486]854#endif
855#ifdef ROAR_HAVE_GETSERVBYNAME
[446]856 struct servent * serv = NULL;
[1486]857#endif
[0]858 DRIVER_USERDATA_T drvinst;
[39]859 struct roar_client * self = NULL;
[508]860#ifdef ROAR_HAVE_LIBDNET
861 char decnethost[80];
862#endif
[2104]863#ifdef SUPPORT_PIDFILE
864 struct roar_vio_calls pidfile_vio;
865#endif
[0]866
867 g_standby       =  0;
[920]868 g_autostandby   =  0;
[982]869 alive           =  1;
[1494]870#ifdef ROAR_SUPPORT_LISTEN
[1155]871 g_no_listen     =  0;
[1494]872#else
873 g_terminate     =  1;
874#endif
[0]875
876 sa.bits     = ROAR_BITS_DEFAULT;
877 sa.channels = ROAR_CHANNELS_DEFAULT;
878 sa.rate     = ROAR_RATE_DEFAULT;
879 sa.codec    = ROAR_CODEC_DEFAULT;
880
[1609]881 g_sa        = &sa;
882 g_max_sa    = &max_sa;
883
884 memcpy(g_max_sa, g_sa, sizeof(max_sa));
885
886 g_config = &config;
887
888 if ( init_config() == -1 ) {
889  ROAR_ERR("Can not init default config!");
890  return 1;
891 }
[0]892
[2530]893#ifdef ROAR_SUPPORT_LISTEN
894 if ( init_listening() == -1 ) {
895  ROAR_ERR("Can not init listening sockets!");
896  return 1;
897 }
898#endif
899
[2500]900#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1924]901 if ( midi_init_config() == -1 ) {
902  ROAR_ERR("Can not init MIDI config!");
903  return 1;
904 }
[2500]905#endif
[60]906
[2489]907#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]908 if ( ssynth_init_config() == -1 ) {
909  ROAR_ERR("Can not init ssynth config!");
910  return 1;
911 }
[2489]912#endif
[2451]913
[2718]914#ifndef ROAR_WITHOUT_DCOMP_RDTCS
915 if ( rdtcs_init_config() == -1 ) {
916  ROAR_ERR("Can not init RDTCS config!");
917  return 1;
918 }
919#endif
920
[1494]921#ifdef ROAR_SUPPORT_LISTEN
[2530]922 sock_addr = ROAR_DEFAULT_SOCK_GLOBAL;
[1753]923#ifdef ROAR_HAVE_GETUID
[1486]924 if ( getuid() != 0 && getenv("HOME") != NULL ) {
925  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[2530]926  sock_addr = user_sock;
[60]927 }
[1753]928#endif
[60]929
[279]930 if ( getenv("ROAR_SERVER") != NULL )
[2530]931  sock_addr = getenv("ROAR_SERVER");
[1494]932#endif
[279]933
[63]934 if ( clients_init() == -1 ) {
935  ROAR_ERR("Can not init clients!");
936  return 1;
937 }
938
939 if ( streams_init() == -1 ) {
940  ROAR_ERR("Can not init streams!");
941  return 1;
942 }
943
[64]944 if ( (g_self_client = clients_new()) == -1 ) {
945  ROAR_ERR("Can not create self client!");
946  return 1;
947 }
948
[2485]949#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]950 if ( sources_init() == -1 ) {
951  ROAR_ERR("Can not init sources!");
952  return 1;
953 }
954
[64]955 if ( (sources_set_client(g_self_client)) == -1 ) {
956  ROAR_ERR("Can not init set source client!");
957  return 1;
958 }
[2485]959#endif
[64]960
[1503]961#ifdef ROAR_HAVE_MAIN_ARGS
[0]962 for (i = 1; i < argc; i++) {
963  k = argv[i];
964
965  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
966   usage();
967   return 0;
968
[2109]969  } else if ( strcmp(k, "--start") == 0 ) {
970   // this is a no op
[579]971  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]972#ifdef ROAR_SUPPORT_LISTEN
[2530]973   if ( restart_server(sock_addr, 1) == -1 ) {
974    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr);
[579]975   }
[1494]976#else
977   ROAR_ERR("--restart not supported");
978#endif
[2109]979  } else if ( strcmp(k, "--shutdown") == 0 ) {
980#ifdef ROAR_SUPPORT_LISTEN
[2530]981   if ( restart_server(sock_addr, 1) == -1 ) {
982    ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr);
[2109]983    return 1;
984   }
985   return 0;
986#else
987   ROAR_ERR("--shutdown not supported");
988   return 1;
989#endif
990  } else if ( strcmp(k, "--stop") == 0 ) {
991#ifdef ROAR_SUPPORT_LISTEN
[2530]992   if ( restart_server(sock_addr, 0) == -1 ) {
993    ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr);
[2109]994    return 1;
995   }
996   return 0;
997#else
998   ROAR_ERR("--stop not supported");
999   return 1;
1000#endif
1001
[579]1002
[775]1003  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]1004#ifdef ROAR_HAVE_FORK
[775]1005   daemon = 1;
[1486]1006#else
1007   ROAR_ERR("--daemon not supported");
1008#endif
[71]1009  } else if ( strcmp(k, "--terminate") == 0 ) {
1010   g_terminate = 1;
[905]1011  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
1012   sysclocksync = 1000;
[275]1013  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]1014   realtime++;
[444]1015  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]1016#ifdef ROAR_HAVE_CHROOT
[444]1017   chrootdir = argv[++i];
[1486]1018#else
1019   ROAR_ERR("--chroot not supported");
1020   i++;
1021#endif
[444]1022  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]1023#ifdef ROAR_HAVE_SETGID
[444]1024   setids |= R_SETGID;
[1486]1025#else
1026   ROAR_ERR("--setgid not supported");
1027#endif
[444]1028  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]1029#ifdef ROAR_HAVE_SETUID
[444]1030   setids |= R_SETUID;
[1486]1031#else
1032   ROAR_ERR("--setuid not supported");
1033#endif
[2017]1034  } else if ( strcmp(k, "--location") == 0 ) {
1035   g_config->location = argv[++i];
[2104]1036  } else if ( strcmp(k, "--pidfile") == 0 ) {
1037#ifdef SUPPORT_PIDFILE
1038   pidfile = argv[++i];
1039#else
1040   ROAR_ERR("--pidfile not supported");
1041   i++;
1042#endif
[68]1043
[280]1044  } else if ( strcmp(k, "--list-cf") == 0 ) {
1045   print_codecfilterlist();
1046   return 0;
1047
[0]1048  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
1049   sa.rate = atoi(argv[++i]);
1050  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
1051   sa.bits = atoi(argv[++i]);
1052  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
1053   sa.channels = atoi(argv[++i]);
1054
[2339]1055  } else if ( strcmp(k, "--stream-flags") == 0 ) {
1056   if ( update_stream_flags(argv[++i]) == -1 ) {
1057    ROAR_ERR("Can not set stream flags");
1058    return 1;
1059   }
1060
[0]1061  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
1062   driver = argv[++i];
1063   if ( strcmp(driver, "list") == 0 ) {
[974]1064    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]1065    print_driverlist();
[973]1066    return 0;
[0]1067   }
1068  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
1069   device = argv[++i];
1070  } else if ( strcmp(k, "-dO") == 0 ) {
1071   opts = argv[++i];
[973]1072  } else if ( strcmp(k, "--list-driver") == 0 ) {
1073   print_driverlist();
1074   return 0;
[0]1075
[932]1076  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
1077   o_drv  = argv[++i];
1078  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
1079   o_dev  = argv[++i];
1080  } else if ( strcmp(k, "-oO") == 0 ) {
1081   o_opts = argv[++i];
[961]1082  } else if ( strcmp(k, "-oP") == 0 ) {
1083   o_prim = 1;
[932]1084  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]1085   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
1086    o_count++;
1087
[961]1088   o_drv  = o_dev = o_opts = NULL;
1089   o_prim = 0;
[932]1090
[0]1091  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[2485]1092#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[550]1093   s_drv = argv[++i];
[2485]1094#else
1095   ROAR_ERR("main(*): No support for sources compiled in");
1096#endif
[550]1097  } else if ( strcmp(k, "-S") == 0 ) {
[2485]1098#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1099   s_dev = argv[++i];
[2485]1100#else
1101   ROAR_ERR("main(*): No support for sources compiled in");
1102#endif
[0]1103  } else if ( strcmp(k, "-sO") == 0 ) {
[2485]1104#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1105   s_opt = argv[++i];
[2485]1106#else
1107   ROAR_ERR("main(*): No support for sources compiled in");
1108#endif
[0]1109  } else if ( strcmp(k, "-sC") == 0 ) {
[2485]1110#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1111   s_con = argv[++i];
[2485]1112#else
1113   ROAR_ERR("main(*): No support for sources compiled in");
1114#endif
[0]1115  } else if ( strcmp(k, "-sP") == 0 ) {
[2485]1116#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1117   s_prim = 1;
[2485]1118#else
1119   ROAR_ERR("main(*): No support for sources compiled in");
1120#endif
[1110]1121  } else if ( strcmp(k, "-sN") == 0 ) {
[2485]1122#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1123   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1124    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1125   }
1126   s_opt = s_dev = s_con = NULL;
1127   s_drv = "cf";
1128   s_prim = 0;
[2485]1129#else
1130   ROAR_ERR("main(*): No support for sources compiled in");
1131#endif
[2270]1132  } else if ( strcmp(k, "--list-sources") == 0 ) {
[2485]1133#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[2270]1134   print_sourcelist();
1135   return 0;
[2485]1136#else
1137   ROAR_ERR("main(*): No support for sources compiled in");
[2504]1138   return 1;
[2485]1139#endif
[0]1140
[1923]1141  } else if ( strcmp(k, "--light-channels") == 0 ) {
[2494]1142#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1923]1143   light_channels = atoi(argv[++i]);
[2494]1144#else
1145   ROAR_WARN("main(*): no light subsystem compiled in");
1146#endif
[1923]1147
[2725]1148  } else if ( strcmp(k, "--rds-pi") == 0 ) {
1149#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1150   g_rdtcs.rds.pi = atoi(argv[++i]);
1151#else
1152   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1153#endif
[2728]1154  } else if ( strcmp(k, "--rds-ps") == 0 ) {
1155#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1156   if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) {
[2729]1157    ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]);
[2728]1158    return 1;
1159   }
1160#else
1161   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1162#endif
1163  } else if ( strcmp(k, "--rds-pty") == 0 ) {
1164#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1165   if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) {
[2729]1166    ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]);
[2728]1167    return 1;
1168   }
1169#else
1170   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1171#endif
1172  } else if ( strcmp(k, "--rds-tp") == 0 ) {
1173#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1174   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) {
1175    ROAR_ERR("Can not set RDS TP flag");
1176    return 1;
1177   }
1178#else
1179   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1180#endif
1181  } else if ( strcmp(k, "--rds-ct") == 0 ) {
1182#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1183   if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) {
1184    ROAR_ERR("Can not set RDS CT flag");
1185    return 1;
1186   }
1187#else
1188   ROAR_WARN("main(*): no RDTCS subsystem compiled in");
1189#endif
1190
[2725]1191
[1924]1192  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
[2487]1193#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1194   midi_config.init_cb = 0;
[2487]1195#else
1196   // no warning here as this is the disable option
1197#endif
[2444]1198  } else if ( strcmp(k, "--midi-console-enable") == 0 ) {
[2487]1199#ifndef ROAR_WITHOUT_DCOMP_CB
[2444]1200   midi_config.init_cb = 1;
[2487]1201#else
1202   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1203#endif
[1924]1204  } else if ( strcmp(k, "--midi-console") == 0 ) {
[2487]1205#ifndef ROAR_WITHOUT_DCOMP_CB
[1924]1206   midi_config.console_dev = argv[++i];
[2444]1207   midi_config.init_cb = 1;
[2487]1208#else
1209   ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in");
1210#endif
[1924]1211
[2451]1212  } else if ( strcmp(k, "--ssynth-enable") == 0 ) {
[2489]1213#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1214   ssynth_conf.enable = 1;
[2489]1215#else
1216   ROAR_ERR("main(*): No support for ssynth compiled in");
1217#endif
[2451]1218  } else if ( strcmp(k, "--ssynth-disable") == 0 ) {
[2489]1219#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1220   ssynth_conf.enable = 0;
[2489]1221#else
1222   // we can safely ignore the disable
1223#endif
[2451]1224
[0]1225  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]1226   // This is only usefull in INET not UNIX mode.
[1494]1227#ifdef ROAR_SUPPORT_LISTEN
[2530]1228   if ( *sock_addr == '/' )
1229    sock_addr = ROAR_DEFAULT_HOST;
[447]1230
[446]1231   errno = 0;
1232   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]1233#ifdef ROAR_HAVE_GETSERVBYNAME
[446]1234    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
1235     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
1236     return 1;
1237    }
1238    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
1239    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
1240            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
1241    port = ROAR_NET2HOST16(serv->s_port);
[1486]1242#else
1243    ROAR_ERR("invalite port number: %s", argv[i]);
1244    return 1;
1245#endif
[446]1246   }
[1494]1247#endif
[1115]1248  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]1249#ifdef ROAR_SUPPORT_LISTEN
[2530]1250   sock_addr = argv[++i];
1251#endif
1252
1253  } else if ( strcmp(k, "--proto") == 0 ) {
1254#ifdef ROAR_SUPPORT_LISTEN
[2549]1255   if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) {
[2530]1256    ROAR_ERR("Unknown protocol: %s", argv[i]);
1257    return 1;
1258   }
[1494]1259#endif
[518]1260
[573]1261  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]1262#ifdef ROAR_SUPPORT_LISTEN
[518]1263   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
1264    sock_type = ROAR_SOCKET_TYPE_TCP;
1265
[2530]1266   if ( *sock_addr == '/' )
1267    sock_addr = ROAR_DEFAULT_HOST;
[1494]1268#endif
[518]1269
1270  } else if ( strcmp(k, "-4") == 0 ) {
[1494]1271#ifdef ROAR_SUPPORT_LISTEN
[517]1272   sock_type = ROAR_SOCKET_TYPE_TCP;
[2530]1273   if ( *sock_addr == '/' )
1274    sock_addr = ROAR_DEFAULT_HOST;
[1494]1275#endif
[518]1276  } else if ( strcmp(k, "-6") == 0 ) {
[1494]1277#ifdef ROAR_SUPPORT_LISTEN
[519]1278#ifdef PF_INET6
[518]1279   sock_type = ROAR_SOCKET_TYPE_TCP6;
[2530]1280   if ( *sock_addr == '/' )
1281    sock_addr = ROAR_DEFAULT_HOST;
[519]1282#else
1283    ROAR_ERR("No IPv6 support compiled in!");
1284    return 1;
1285#endif
[1494]1286#endif
[518]1287
[573]1288  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]1289#ifdef ROAR_SUPPORT_LISTEN
[62]1290   // ignore this case as it is the default behavor.
[517]1291   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]1292#endif
[518]1293
[573]1294  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]1295#ifdef ROAR_SUPPORT_LISTEN
[508]1296#ifdef ROAR_HAVE_LIBDNET
1297    port   = ROAR_DEFAULT_NUM;
1298    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
[2530]1299    sock_addr = decnethost;
[517]1300    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]1301#else
1302    ROAR_ERR("No DECnet support compiled in!");
1303    return 1;
1304#endif
[1494]1305#endif
[2530]1306  } else if ( strcmp(k, "--new-sock") == 0 ) {
1307#ifdef ROAR_SUPPORT_LISTEN
1308   if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1309    ROAR_ERR("Can not open listen socket!");
1310    return 1;
1311   }
1312#endif
[518]1313
[1993]1314  } else if ( strcmp(k, "--slp") == 0 ) {
1315#ifdef ROAR_HAVE_LIBSLP
1316   reg_slp = 1;
1317#else
1318    ROAR_ERR("No OpenSLP support compiled in!");
1319    return 1;
1320#endif
1321
[60]1322  } else if ( strcmp(k, "-G") == 0 ) {
[444]1323   sock_grp  = argv[++i];
1324  } else if ( strcmp(k, "-U") == 0 ) {
1325   sock_user = argv[++i];
[0]1326
[68]1327  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]1328#ifdef ROAR_SUPPORT_LISTEN
[2530]1329   sock_addr   = "";
[548]1330   g_terminate = 1;
[1155]1331   g_no_listen = 1;
[1494]1332#endif
[68]1333  } else if ( strcmp(k, "--client-fh") == 0 ) {
1334   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
1335    ROAR_ERR("main(*): Can not set client's fh");
1336    return 1;
1337   }
[501]1338  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]1339#ifdef ROAR_HAVE_IO_POSIX
[501]1340   close(atoi(argv[++i]));
[1486]1341#else
1342   i++;
1343   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
1344#endif
[68]1345
[920]1346  } else if ( strcmp(k, "--standby") == 0 ) {
1347   g_standby = 1;
1348  } else if ( strcmp(k, "--auto-standby") == 0 ) {
1349   g_autostandby = 1;
[0]1350  } else {
1351   usage();
1352   return 1;
1353  }
1354
1355 }
[1503]1356#endif
[0]1357
[2485]1358#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[1110]1359 if ( s_dev != NULL ) {
1360  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
1361   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
1362  }
1363 }
[2485]1364#endif
[1110]1365
[1145]1366 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]1367
[0]1368 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
1369
[2500]1370#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1819]1371 if ( midi_init() == -1 ) {
[281]1372  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]1373 }
[2500]1374#endif
[1819]1375
[2489]1376#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1377 if ( ssynth_init() == -1 ) {
1378  ROAR_ERR("Can not initialize ssynth subsystem");
1379 }
[2489]1380#endif
[2451]1381
[2494]1382#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1383 if ( light_init(light_channels) == -1 ) {
1384  ROAR_ERR("Can not initialize light control subsystem");
1385 }
[2494]1386#endif
[281]1387
[2718]1388#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1389 if ( rdtcs_init() == -1 ) {
1390  ROAR_ERR("Can not initialize RDTCS subsystem");
1391 }
1392#endif
1393
[1494]1394#ifdef ROAR_SUPPORT_LISTEN
[2530]1395 if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto) != 0 ) {
1396  ROAR_ERR("Can not open listen socket!");
1397  return 1;
[60]1398 }
[1494]1399#endif
[60]1400
[0]1401 if ( output_buffer_init(&sa) == -1 ) {
1402  ROAR_ERR("Can not init output buffer!");
1403  return 1;
1404 }
1405
[1145]1406 if ( driver == NULL ) {
1407  driver = "null";
1408 } else {
1409  ROAR_WARN("Usage of old driver interface. use -o not -d!");
1410 }
1411
[0]1412 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
1413  ROAR_ERR("Can not open output driver!");
1414  return 1;
1415 }
1416
[44]1417 if ( samples_init() == -1 ) {
1418  ROAR_ERR("Can not init samples!");
1419  return 1;
1420 }
1421
1422
[1486]1423 // we should handle this on microcontrollers, too.
[1753]1424#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]1425 signal(SIGINT,  on_sig_int);
[2732]1426 signal(SIGTERM, on_sig_term);
[285]1427 signal(SIGCHLD, on_sig_chld);
[2112]1428 signal(SIGUSR1, on_sig_usr1);
[0]1429 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]1430#endif
[0]1431
[275]1432 if ( realtime ) {
[278]1433#ifdef DEBUG
1434  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
1435#endif
1436
[1486]1437#ifdef ROAR_HAVE_NICE
[275]1438  errno = 0;
[276]1439  nice(-5*realtime); // -5 for each --realtime
[1486]1440  if ( errno ) {
1441   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1442  }
1443#else
1444  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
1445#endif
[277]1446/*
[276]1447#ifdef __linux__
[277]1448  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
1449   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]1450#endif
[277]1451*/
[275]1452 }
1453
[1486]1454#ifdef ROAR_HAVE_SETGID
[444]1455 if ( setids & R_SETGID ) {
[2584]1456  if ( (grp = getgrnam(sock_grp)) == NULL ) {
1457   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
1458   return 1;
1459  }
[444]1460  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
1461   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
1462  }
[523]1463  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]1464   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
1465  }
1466 }
[1486]1467#endif
[444]1468
[0]1469
[39]1470 clients_set_pid(g_self_client, getpid());
[1753]1471#ifdef ROAR_HAVE_GETUID
[440]1472 clients_set_uid(g_self_client, getuid());
[1753]1473#endif
1474#ifdef ROAR_HAVE_GETGID
[440]1475 clients_set_gid(g_self_client, getgid());
[1753]1476#endif
[39]1477 clients_get(g_self_client, &self);
[37]1478
[39]1479 if ( self == NULL ) {
1480  ROAR_ERR("Can not get self client!");
1481  return 1;
1482 }
1483
[775]1484 strcpy(self->name, "RoarAudio daemon internal");
[68]1485
[1486]1486#ifdef ROAR_HAVE_FORK
[775]1487 if ( daemon ) {
[68]1488  close(ROAR_STDIN );
1489  close(ROAR_STDOUT);
1490  close(ROAR_STDERR);
[1753]1491
[68]1492  if ( fork() )
[1486]1493   ROAR_U_EXIT(0);
[1753]1494
1495#ifdef ROAR_HAVE_SETSID
1496  setsid();
1497#endif
[1046]1498  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]1499 }
[1486]1500#endif
[68]1501
[2104]1502#ifdef SUPPORT_PIDFILE
[2106]1503 if ( pidfile != NULL ) {
1504  if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) {
1505   ROAR_ERR("Can not write pidfile: %s", pidfile);
1506  } else {
1507   roar_vio_printf(&pidfile_vio, "%i\n", getpid());
1508   roar_vio_close(&pidfile_vio);
1509  }
[2104]1510 }
1511#endif
1512
[1486]1513#ifdef ROAR_HAVE_CHROOT
[444]1514 if (chrootdir) {
1515  if ( chroot(chrootdir) == -1 ) {
1516   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1517   return 2;
1518  }
1519  if ( chdir("/") == -1 ) {
1520   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1521   return 2;
1522  }
1523 }
[1486]1524#endif
[444]1525
[1486]1526#ifdef ROAR_HAVE_SETUID
[444]1527 if ( setids & R_SETUID ) {
[2584]1528  if ( (pwd = getpwnam(sock_user)) == NULL ) {
1529   ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
1530   return 1;
1531  }
[444]1532  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1533   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1534   return 3;
1535  }
[1753]1536#ifdef ROAR_HAVE_GETUID
[444]1537  clients_set_uid(g_self_client, getuid());
[1753]1538#endif
[444]1539 }
[1486]1540#endif
[444]1541
[1993]1542 // Register with OpenSLP:
[2028]1543#ifdef ROAR_HAVE_LIBSLP
[1993]1544 if ( reg_slp ) {
[2530]1545  register_slp(0, sock_addr);
[1993]1546 }
[2028]1547#endif
[1993]1548
[0]1549 // start main loop...
[905]1550 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]1551
1552 // clean up.
1553 clean_quit_prep();
1554 driver_close(drvinst, drvid);
1555 output_buffer_free();
1556
1557 return 0;
1558}
1559
[574]1560void cleanup_listen_socket (int terminate) {
[2530]1561 int i;
1562
[1993]1563 // Deregister from SLP:
[2028]1564#ifdef ROAR_HAVE_LIBSLP
[1993]1565 register_slp(1, NULL);
[2028]1566#endif
[580]1567
[1494]1568#ifdef ROAR_SUPPORT_LISTEN
[2530]1569 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
1570  if ( g_listen_socket[i] != -1 ) {
[1486]1571#ifdef ROAR_HAVE_IO_POSIX
[2530]1572   close(g_listen_socket[i]);
[1486]1573#endif // #else is useless because we are in void context.
[60]1574
[2530]1575   g_listen_socket[i] = -1;
[576]1576
[1486]1577#ifdef ROAR_HAVE_UNIX
[2530]1578   if ( server[i] != NULL )
1579    if ( *(server[i]) == '/' )
1580     unlink(server[i]);
[1486]1581#endif
[2530]1582  }
[580]1583 }
[60]1584
[1494]1585#endif
1586
[574]1587 if ( terminate )
1588  g_terminate = 1;
1589}
1590
1591void clean_quit_prep (void) {
1592 cleanup_listen_socket(0);
[60]1593
[2485]1594#ifndef ROAR_WITHOUT_DCOMP_SOURCES
[0]1595 sources_free();
[2485]1596#endif
[0]1597 streams_free();
1598 clients_free();
[2489]1599#ifndef ROAR_WITHOUT_DCOMP_SSYNTH
[2451]1600 ssynth_free();
[2489]1601#endif
[2487]1602#ifndef ROAR_WITHOUT_DCOMP_CB
[282]1603 midi_cb_stop(); // stop console beep
[2487]1604#endif
[2500]1605#ifndef ROAR_WITHOUT_DCOMP_MIDI
[281]1606 midi_free();
[2500]1607#endif
[2494]1608#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1819]1609 light_free();
[2494]1610#endif
[2718]1611#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1612 rdtcs_free();
1613#endif
[2104]1614
1615#ifdef SUPPORT_PIDFILE
1616 if ( pidfile != NULL )
1617  unlink(pidfile);
1618#endif
[0]1619}
1620
1621void clean_quit (void) {
1622 clean_quit_prep();
1623// driver_close(drvinst, drvid);
1624// output_buffer_free();
1625 exit(0);
1626}
1627
1628//ll
Note: See TracBrowser for help on using the repository browser.