source: roaraudio/roard/roard.c @ 2085:4194fe29d77a

Last change on this file since 2085:4194fe29d77a was 2085:4194fe29d77a, checked in by phi, 15 years ago

added support for a output option 'q=...'

File size: 29.1 KB
Line 
1//roard.c:
2
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
25#include "roard.h"
26
27#ifdef ROAR_SUPPORT_LISTEN
28char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
29#endif
30
31#ifdef ROAR_HAVE_MAIN_ARGS
32void usage (void) {
33 printf("Usage: roard [OPTIONS]...\n\n");
34
35 printf("Misc Options:\n\n");
36 printf(
37        " --daemon              - Bring the server into background after init\n"
38        " --terminate           - Terminate after last client quited\n"
39        " --restart             - Trys to stop an old instance and start a new with new settings\n"
40        " --realtime            - Trys to get realtime priority,\n"
41        "                         give multible times for being more realtime\n"
42        " --chroot DIR          - chroots to the given dir\n"
43        " --setgid              - GroupID to the audio group as specified via -G\n"
44        " --setuid              - UserID to the audio user as specified via -U\n"
45        " --sysclocksync        - calculate exact sample rate using the system clock\n"
46        " --location  LOC       - Set lion readable location of server\n"
47       );
48
49 printf("\nAudio Options:\n\n");
50 printf(
51        " -R  --rate   RATE     - Set server rate\n"
52        " -B  --bits   BITS     - Set server bits\n"
53        " -C  --chans  CHANNELS - Set server channels\n"
54       );
55
56 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
57 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
58 printf(" -D  --device DEV      - Set the device\n");
59 printf(" -dO OPTS              - Set output options\n");
60 printf(" --list-driver         - List all drivers\n");
61
62 printf("\nOutput Options:\n\n");
63 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
64 printf(" -O  --odevice DEV     - Set the device\n");
65 printf(" -oO OPTS              - Set output options\n");
66 printf(" -oN                   - Adds another output\n");
67 printf(" -oP                   - Mark output as primary\n");
68
69 printf("\nSource Options:\n\n");
70 printf(" -s  --source DRV      - Use DRV as input driver\n"
71        " -S           DEV      - Use DEV as input device\n"
72        " -sO          OPTS     - Use OPTS as input options\n"
73        " -sN                   - Adds another source\n"
74        " -sP                   - Make souce as primary\n"
75       );
76
77 printf("\nCodec Filter Options:\n\n");
78 printf(" --list-cf             - List all codec filter\n"
79       );
80
81 printf("\nMIDI Options:\n\n");
82 printf(" --midi-no-console     - Disable console based MIDI synth\n"
83        " --midi-console DEV    - Set device for MIDI console\n"
84       );
85
86 printf("\nLight Control Options:\n\n");
87 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
88                                  LIGHT_CHANNELS_DEFAULT
89       );
90
91 printf("\nServer Options:\n\n");
92 printf(" -t  --tcp             - Use TCP listen socket\n"
93        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
94#ifdef ROAR_HAVE_LIBDNET
95        " -n  --decnet          - use DECnet listen socket\n"
96#endif
97        " -4                    - Use IPv4 connections (implies -t)\n"
98#ifdef PF_INET6
99        " -6                    - Use IPv6 connections (implies -t)\n"
100#endif
101#ifdef IPV6_ADDRFORM
102        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
103        "                         this is normaly not usefull.\n"
104#endif
105        " -p  --port            - TCP Port to bind to\n"
106        " -b  --bind            - IP/Hostname to bind to\n"
107        "     --sock            - Filename for UNIX Domain Socket\n"
108#ifdef ROAR_HAVE_LIBSLP
109        "     --slp             - Enable OpenSLP support\n"
110#endif
111        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
112        "                         You need the permittions to change the GID\n"
113        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
114        "                         You need the permittions to change the UID (normaly only root has)\n"
115        " --no-listen           - Do not listen for new clients\n"
116        "                         (only usefull for relaing, impleys --terminate)\n"
117        " --client-fh           - Comunicate with a client over this handle\n"
118        "                         (only usefull for relaing)\n"
119        " --close-fh            - Closes the given fh\n"
120        " --standby             - Start in standby state\n"
121        " --auto-standby        - Automatical goes into standby if there are no streams\n",
122        ROAR_DEFAULT_SOCKGRP
123       );
124// printf("\n Options:\n\n");
125 printf("\n");
126}
127#endif
128
129int restart_server (char * server) {
130 struct roar_connection con;
131 if ( roar_connect(&con, server) == -1 ) {
132  return -1;
133 }
134
135 if ( roar_terminate(&con, 1) == -1 ) {
136  return -1;
137 }
138
139 return roar_disconnect(&con);
140}
141
142#define R_SETUID 1
143#define R_SETGID 2
144
145int init_config (void) {
146 int i;
147
148 memset(g_config, 0, sizeof(struct roard_config));
149
150 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
151  g_config->streams[i].mixer_channels = 1;
152  g_config->streams[i].mixer.rpg_mul  = 1;
153  g_config->streams[i].mixer.rpg_div  = 1;
154  g_config->streams[i].mixer.scale    = 65535;
155  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
156 }
157
158 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
159
160 g_config->location = "***default***";
161
162 return 0;
163}
164
165int add_output (char * drv, char * dev, char * opts, int prim, int count) {
166 int stream;
167 struct roar_stream * s;
168 struct roar_stream_server * ss;
169 char * k, * v;
170#ifdef ROAR_DRIVER_CODEC
171 char * to_free = NULL;
172#endif
173 int codec;
174 int sync = 0, f_mmap = 0;
175 int32_t blocks = -1, blocksize = -1;
176 int dir = ROAR_DIR_OUTPUT;
177 int error = 0;
178 // DMX:
179 int32_t channel  = -1;
180 int32_t universe = -1;
181 uint16_t tu16;
182 float q = -32e6;
183
184 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
185
186 if ( drv == NULL && count == 0 ) {
187  drv  = ROAR_DRIVER_DEFAULT;
188  prim = 1;
189  sync = 1;
190
191#ifdef ROAR_DRIVER_CODEC
192  if ( opts == NULL ) {
193   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
194  }
195#endif
196 }
197
198 if ( opts == NULL && count == 0 ) {
199  sync = 1;
200  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
201 }
202
203 if ( (stream = streams_new()) == -1 ) {
204  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
205  if ( prim ) alive = 0;
206  return -1;
207 }
208
209 streams_get(stream, &ss);
210 s = ROAR_STREAM(ss);
211
212 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
213
214 s->pos_rel_id = -1;
215// s->info.codec = codec;
216
217 codec = s->info.codec;
218
219 k = strtok(opts, ",");
220 while (k != NULL) {
221//  ROAR_WARN("add_output(*): opts: %s", k);
222
223  if ( (v = strstr(k, "=")) != NULL ) {
224   *v++ = 0;
225  }
226
227  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
228  if ( strcmp(k, "rate") == 0 ) {
229   s->info.rate = atoi(v);
230  } else if ( strcmp(k, "channels") == 0 ) {
231   s->info.channels = atoi(v);
232  } else if ( strcmp(k, "bits") == 0 ) {
233   s->info.bits = atoi(v);
234  } else if ( strcmp(k, "codec") == 0 ) {
235   if ( (codec = roar_str2codec(v)) == -1 ) {
236    ROAR_ERR("add_output(*): unknown codec '%s'", v);
237    error++;
238   }
239  } else if ( strcmp(k, "q") == 0 ) {
240   q = atof(v);
241  } else if ( strcmp(k, "blocks") == 0 ) {
242   blocks = atoi(v);
243  } else if ( strcmp(k, "blocksize") == 0 ) {
244   blocksize = atoi(v);
245  } else if ( strcmp(k, "mmap") == 0 ) {
246   f_mmap = 1;
247  } else if ( strcmp(k, "subsystem") == 0 ) {
248   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
249    dir = ROAR_DIR_OUTPUT;
250   } else if ( !strcasecmp(v, "midi") ) {
251    dir = ROAR_DIR_MIDI_OUT;
252   } else if ( !strcasecmp(v, "light") ) {
253    dir = ROAR_DIR_LIGHT_OUT;
254   } else {
255    ROAR_ERR("add_output(*): unknown subsystem '%s'", k);
256    error++;
257   }
258  // DMX:
259  } else if ( strcmp(k, "channel") == 0 ) {
260   channel  = atoi(v);
261   if ( channel < 0 || channel > 65535 ) {
262    ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel);
263    channel = -1;
264    error++;
265   }
266  } else if ( strcmp(k, "universe") == 0 ) {
267   universe = atoi(v);
268   if ( universe < 0 || universe > 255 ) {
269    ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe);
270    universe = -1;
271    error++;
272   }
273
274
275  } else if ( strcmp(k, "meta") == 0 ) {
276   streams_set_flag(stream, ROAR_FLAG_META);
277  } else if ( strcmp(k, "sync") == 0 ) {
278   sync = 1;
279  } else if ( strcmp(k, "primary") == 0 ) {
280   prim = 1;
281
282  } else if ( strcmp(k, "cleanmeta") == 0 ) {
283   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
284  } else if ( strcmp(k, "autoconf") == 0 ) {
285   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
286  } else {
287   ROAR_ERR("add_output(*): unknown option '%s'", k);
288   error++;
289  }
290
291  if ( error ) {
292   streams_delete(stream);
293   if ( prim ) alive = 0;
294#ifdef ROAR_DRIVER_CODEC
295   if ( to_free != NULL )
296    free(to_free);
297#endif
298   return -1;
299  }
300
301  k = strtok(NULL, ",");
302 }
303
304 if ( streams_set_dir(stream, dir, 1) == -1 ) {
305  streams_delete(stream);
306  return -1;
307 }
308
309#ifdef ROAR_DRIVER_CODEC
310 if ( to_free != NULL )
311  free(to_free);
312#endif
313
314 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
315  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
316
317 s->info.codec = codec;
318 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
319
320 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
321  ss->driver_id = -1; // don't close a driver not opened...
322  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
323  streams_delete(stream);
324  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
325  if ( prim ) alive = 0;
326  return -1;
327 }
328
329 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
330 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
331
332 if ( q > -1e6 )
333  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
334
335 if ( blocks != -1 )
336  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
337
338 if ( blocksize != -1 )
339  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
340
341 // TODO: we shoudld *really* check for errors here...
342 if ( channel != -1 ) {
343  tu16 = channel;
344  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
345 }
346 if ( universe != -1 ) {
347  tu16 = universe;
348  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
349 }
350
351 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
352
353 streams_set_fh(stream, -1); // update some internal structures
354
355 client_stream_add(g_source_client, stream);
356
357 if ( prim ) {
358  streams_mark_primary(stream);
359  s->pos_rel_id = stream;
360 }
361
362 if ( sync ) {
363  streams_set_flag(stream, ROAR_FLAG_SYNC);
364 } else {
365  streams_reset_flag(stream, ROAR_FLAG_SYNC);
366 }
367
368 if ( f_mmap )
369  streams_set_flag(stream, ROAR_FLAG_MMAP);
370
371 return 0;
372}
373
374
375// SLP:
376void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
377 /* return the error code in the cookie */
378 *(SLPError*)cookie = errcode;
379}
380
381int register_slp (int unreg, char * sockname) {
382#ifdef ROAR_HAVE_LIBSLP
383 static int regged = 0;
384 static char * sn = NULL;
385 SLPError err;
386 SLPError callbackerr;
387 SLPHandle hslp;
388 char addr[1024];
389 char attr[1024] = "";
390 char * location;
391
392 if ( sockname != NULL )
393  sn = sockname;
394
395 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
396
397 err = SLPOpen("en", SLP_FALSE, &hslp);
398
399 if (err != SLP_OK) {
400  ROAR_ERR("Error opening slp handle: Error #%i", err);
401  return -1;
402 }
403
404 if (!unreg) {
405
406  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
407   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
408   SLPClose(hslp);
409   return -1;
410  }
411
412  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
413                               "(light-channels=%i),(location=%s)",
414           g_sa->rate, g_sa->channels, g_sa->bits,
415           g_light_state.channels,
416           location
417          );
418
419  /* Register a service with SLP */
420  err = SLPReg(hslp,
421               addr,
422               SLP_LIFETIME_MAXIMUM,
423               0,
424               attr,
425               SLP_TRUE,
426               register_slp_callback,
427               &callbackerr);
428  regged = 1;
429 } else if ( unreg && regged ) {
430  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
431  regged = 0;
432 } else {
433  SLPClose(hslp);
434  return -1;
435 }
436
437 /* err may contain an error code that occurred as the slp library    */
438 /* _prepared_ to make the call.                                     */
439 if ( err != SLP_OK ) {
440  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
441  return -1;
442 }
443
444 /* callbackerr may contain an error code (that was assigned through */
445 /* the callback cookie) that occurred as slp packets were sent on    */
446 /* the wire */
447 if (callbackerr != SLP_OK) {
448  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
449  return -1;
450 }
451
452 SLPClose(hslp);
453 return 0;
454#else
455 return -1;
456#endif
457}
458
459
460// MAIN:
461
462#ifdef ROAR_HAVE_MAIN_ARGS
463int main (int argc, char * argv[]) {
464#else
465int main (void) {
466#endif
467#ifdef ROAR_HAVE_MAIN_ARGS
468 int i;
469 char * k;
470#endif
471#ifdef ROAR_SUPPORT_LISTEN
472 char user_sock[80]  = {0};
473#endif
474 struct roar_audio_info sa, max_sa;
475 struct roard_config config;
476#ifdef ROAR_HAVE_FORK
477 int    daemon       = 0;
478#endif
479 int    realtime     = 0;
480 int    sysclocksync = 0;
481 char * driver    = NULL;
482 char * device    = NULL;
483#ifdef ROAR_HAVE_MAIN_ARGS
484 char * opts      = NULL;
485#endif
486// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
487#ifdef ROAR_SUPPORT_LISTEN
488 int      port    = ROAR_DEFAULT_PORT;
489#endif
490 int               drvid;
491 char * s_drv     = "cf";
492 char * s_dev     = NULL;
493 char * s_con     = NULL;
494 char * s_opt     = NULL;
495 int    s_prim    = 0;
496 char * o_drv     = getenv("ROAR_DRIVER");
497 char * o_dev     = getenv("ROAR_DEVICE");
498 char * o_opts    = NULL;
499 int    o_prim    = 0;
500 int    o_count   = 0;
501 int    light_channels = LIGHT_CHANNELS_DEFAULT;
502 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
503 char * sock_user = NULL;
504#ifdef ROAR_SUPPORT_LISTEN
505 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
506#endif
507#ifdef ROAR_HAVE_LIBSLP
508 int    reg_slp   = 0;
509#endif
510#ifdef ROAR_HAVE_CHROOT
511 char * chrootdir = NULL;
512#endif
513#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
514 int    setids    = 0;
515#endif
516#ifdef ROAR_HAVE_UNIX
517 char * env_roar_proxy_backup;
518#endif
519#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
520 struct group   * grp  = NULL;
521#endif
522#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
523 struct passwd  * pwd  = NULL;
524#endif
525#ifdef ROAR_HAVE_GETSERVBYNAME
526 struct servent * serv = NULL;
527#endif
528 DRIVER_USERDATA_T drvinst;
529 struct roar_client * self = NULL;
530#ifdef ROAR_HAVE_LIBDNET
531 char decnethost[80];
532#endif
533
534 g_standby       =  0;
535 g_autostandby   =  0;
536 alive           =  1;
537#ifdef ROAR_SUPPORT_LISTEN
538 g_no_listen     =  0;
539 g_listen_socket = -1;
540#else
541 g_terminate     =  1;
542#endif
543
544 sa.bits     = ROAR_BITS_DEFAULT;
545 sa.channels = ROAR_CHANNELS_DEFAULT;
546 sa.rate     = ROAR_RATE_DEFAULT;
547 sa.codec    = ROAR_CODEC_DEFAULT;
548
549 g_sa        = &sa;
550 g_max_sa    = &max_sa;
551
552 memcpy(g_max_sa, g_sa, sizeof(max_sa));
553
554 g_config = &config;
555
556 if ( init_config() == -1 ) {
557  ROAR_ERR("Can not init default config!");
558  return 1;
559 }
560
561 if ( midi_init_config() == -1 ) {
562  ROAR_ERR("Can not init MIDI config!");
563  return 1;
564 }
565
566#ifdef ROAR_SUPPORT_LISTEN
567#ifdef ROAR_HAVE_GETUID
568 if ( getuid() != 0 && getenv("HOME") != NULL ) {
569  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
570  server = user_sock;
571 }
572#endif
573
574 if ( getenv("ROAR_SERVER") != NULL )
575  server = getenv("ROAR_SERVER");
576#endif
577
578 if ( clients_init() == -1 ) {
579  ROAR_ERR("Can not init clients!");
580  return 1;
581 }
582
583 if ( streams_init() == -1 ) {
584  ROAR_ERR("Can not init streams!");
585  return 1;
586 }
587
588 if ( (g_self_client = clients_new()) == -1 ) {
589  ROAR_ERR("Can not create self client!");
590  return 1;
591 }
592
593 if ( sources_init() == -1 ) {
594  ROAR_ERR("Can not init sources!");
595  return 1;
596 }
597
598 if ( (sources_set_client(g_self_client)) == -1 ) {
599  ROAR_ERR("Can not init set source client!");
600  return 1;
601 }
602
603#ifdef ROAR_HAVE_MAIN_ARGS
604 for (i = 1; i < argc; i++) {
605  k = argv[i];
606
607  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
608   usage();
609   return 0;
610
611  } else if ( strcmp(k, "--restart") == 0 ) {
612#ifdef ROAR_SUPPORT_LISTEN
613   if ( restart_server(server) == -1 ) {
614    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
615   }
616#else
617   ROAR_ERR("--restart not supported");
618#endif
619
620  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
621#ifdef ROAR_HAVE_FORK
622   daemon = 1;
623#else
624   ROAR_ERR("--daemon not supported");
625#endif
626  } else if ( strcmp(k, "--terminate") == 0 ) {
627   g_terminate = 1;
628  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
629   sysclocksync = 1000;
630  } else if ( strcmp(k, "--realtime") == 0 ) {
631   realtime++;
632  } else if ( strcmp(k, "--chroot") == 0 ) {
633#ifdef ROAR_HAVE_CHROOT
634   chrootdir = argv[++i];
635#else
636   ROAR_ERR("--chroot not supported");
637   i++;
638#endif
639  } else if ( strcmp(k, "--setgid") == 0 ) {
640#ifdef ROAR_HAVE_SETGID
641   setids |= R_SETGID;
642#else
643   ROAR_ERR("--setgid not supported");
644#endif
645  } else if ( strcmp(k, "--setuid") == 0 ) {
646#ifdef ROAR_HAVE_SETUID
647   setids |= R_SETUID;
648#else
649   ROAR_ERR("--setuid not supported");
650#endif
651  } else if ( strcmp(k, "--location") == 0 ) {
652   g_config->location = argv[++i];
653
654  } else if ( strcmp(k, "--list-cf") == 0 ) {
655   print_codecfilterlist();
656   return 0;
657
658  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
659   sa.rate = atoi(argv[++i]);
660  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
661   sa.bits = atoi(argv[++i]);
662  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
663   sa.channels = atoi(argv[++i]);
664
665  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
666   driver = argv[++i];
667   if ( strcmp(driver, "list") == 0 ) {
668    ROAR_WARN("The option is obsolete, use --list-driver!");
669    print_driverlist();
670    return 0;
671   }
672  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
673   device = argv[++i];
674  } else if ( strcmp(k, "-dO") == 0 ) {
675   opts = argv[++i];
676  } else if ( strcmp(k, "--list-driver") == 0 ) {
677   print_driverlist();
678   return 0;
679
680  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
681   o_drv  = argv[++i];
682  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
683   o_dev  = argv[++i];
684  } else if ( strcmp(k, "-oO") == 0 ) {
685   o_opts = argv[++i];
686  } else if ( strcmp(k, "-oP") == 0 ) {
687   o_prim = 1;
688  } else if ( strcmp(k, "-oN") == 0 ) {
689   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
690    o_count++;
691
692   o_drv  = o_dev = o_opts = NULL;
693   o_prim = 0;
694
695  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
696   s_drv = argv[++i];
697  } else if ( strcmp(k, "-S") == 0 ) {
698   s_dev = argv[++i];
699  } else if ( strcmp(k, "-sO") == 0 ) {
700   s_opt = argv[++i];
701  } else if ( strcmp(k, "-sC") == 0 ) {
702   s_con = argv[++i];
703  } else if ( strcmp(k, "-sP") == 0 ) {
704   s_prim = 1;
705  } else if ( strcmp(k, "-sN") == 0 ) {
706   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
707    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
708   }
709   s_opt = s_dev = s_con = NULL;
710   s_drv = "cf";
711   s_prim = 0;
712
713  } else if ( strcmp(k, "--light-channels") == 0 ) {
714   light_channels = atoi(argv[++i]);
715
716  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
717   midi_config.init_cb = 0;
718  } else if ( strcmp(k, "--midi-console") == 0 ) {
719   midi_config.console_dev = argv[++i];
720
721  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
722   // This is only usefull in INET not UNIX mode.
723#ifdef ROAR_SUPPORT_LISTEN
724   if ( *server == '/' )
725    server = ROAR_DEFAULT_HOST;
726
727   errno = 0;
728   if ( (port = atoi(argv[++i])) < 1 ) {
729#ifdef ROAR_HAVE_GETSERVBYNAME
730    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
731     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
732     return 1;
733    }
734    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
735    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
736            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
737    port = ROAR_NET2HOST16(serv->s_port);
738#else
739    ROAR_ERR("invalite port number: %s", argv[i]);
740    return 1;
741#endif
742   }
743#endif
744  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
745#ifdef ROAR_SUPPORT_LISTEN
746   server = argv[++i];
747#endif
748
749  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
750#ifdef ROAR_SUPPORT_LISTEN
751   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
752    sock_type = ROAR_SOCKET_TYPE_TCP;
753
754   if ( *server == '/' )
755    server = ROAR_DEFAULT_HOST;
756#endif
757
758  } else if ( strcmp(k, "-4") == 0 ) {
759#ifdef ROAR_SUPPORT_LISTEN
760   sock_type = ROAR_SOCKET_TYPE_TCP;
761   if ( *server == '/' )
762    server = ROAR_DEFAULT_HOST;
763#endif
764  } else if ( strcmp(k, "-6") == 0 ) {
765#ifdef ROAR_SUPPORT_LISTEN
766#ifdef PF_INET6
767   sock_type = ROAR_SOCKET_TYPE_TCP6;
768   if ( *server == '/' )
769    server = ROAR_DEFAULT_HOST;
770#else
771    ROAR_ERR("No IPv6 support compiled in!");
772    return 1;
773#endif
774#endif
775
776  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
777#ifdef ROAR_SUPPORT_LISTEN
778   // ignore this case as it is the default behavor.
779   sock_type = ROAR_SOCKET_TYPE_UNIX;
780#endif
781
782  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
783#ifdef ROAR_SUPPORT_LISTEN
784#ifdef ROAR_HAVE_LIBDNET
785    port   = ROAR_DEFAULT_NUM;
786    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
787    server = decnethost;
788    sock_type = ROAR_SOCKET_TYPE_DECNET;
789#else
790    ROAR_ERR("No DECnet support compiled in!");
791    return 1;
792#endif
793#endif
794
795  } else if ( strcmp(k, "--slp") == 0 ) {
796#ifdef ROAR_HAVE_LIBSLP
797   reg_slp = 1;
798#else
799    ROAR_ERR("No OpenSLP support compiled in!");
800    return 1;
801#endif
802
803  } else if ( strcmp(k, "-G") == 0 ) {
804   sock_grp  = argv[++i];
805  } else if ( strcmp(k, "-U") == 0 ) {
806   sock_user = argv[++i];
807
808  } else if ( strcmp(k, "--no-listen") == 0 ) {
809#ifdef ROAR_SUPPORT_LISTEN
810   server      = "";
811   g_terminate = 1;
812   g_no_listen = 1;
813#endif
814  } else if ( strcmp(k, "--client-fh") == 0 ) {
815   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
816    ROAR_ERR("main(*): Can not set client's fh");
817    return 1;
818   }
819  } else if ( strcmp(k, "--close-fh") == 0 ) {
820#ifdef ROAR_HAVE_IO_POSIX
821   close(atoi(argv[++i]));
822#else
823   i++;
824   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
825#endif
826
827  } else if ( strcmp(k, "--standby") == 0 ) {
828   g_standby = 1;
829  } else if ( strcmp(k, "--auto-standby") == 0 ) {
830   g_autostandby = 1;
831  } else {
832   usage();
833   return 1;
834  }
835
836 }
837#endif
838
839 if ( s_dev != NULL ) {
840  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
841   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
842  }
843 }
844
845 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
846
847 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
848
849 if ( midi_init() == -1 ) {
850  ROAR_ERR("Can not initialize MIDI subsystem");
851 }
852
853 if ( light_init(light_channels) == -1 ) {
854  ROAR_ERR("Can not initialize light control subsystem");
855 }
856
857#ifdef ROAR_SUPPORT_LISTEN
858 if ( *server != 0 ) {
859  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
860#ifdef ROAR_HAVE_UNIX
861   if ( *server == '/' ) {
862    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
863     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
864     unsetenv("ROAR_PROXY");
865    }
866    if ( (i = roar_socket_connect(server, port)) != -1 ) {
867     close(i);
868     ROAR_ERR("Can not open listen socket!");
869     return 1;
870    } else {
871     unlink(server);
872     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
873      ROAR_ERR("Can not open listen socket!");
874      return 1;
875     }
876    }
877    if ( env_roar_proxy_backup != NULL ) {
878     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
879     free(env_roar_proxy_backup);
880    }
881#else
882   if (0) { // noop
883#endif
884   } else {
885    ROAR_ERR("Can not open listen socket!");
886    return 1;
887   }
888  }
889
890#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
891  if ( (grp = getgrnam(sock_grp)) == NULL ) {
892   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
893  }
894#endif
895#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
896  if ( sock_user || (setids & R_SETUID) ) {
897   if ( (pwd = getpwnam(sock_user)) == NULL ) {
898    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
899   }
900  }
901#endif
902
903#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
904  if ( *server == '/' ) {
905   if ( grp ) {
906    if ( pwd ) {
907     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
908      return 1;
909    } else {
910     if ( chown(server, -1, grp->gr_gid) == -1 )
911      return 1;
912    }
913#ifdef ROAR_HAVE_GETUID
914    if ( getuid() == 0 )
915     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
916      return 1;
917#endif
918   }
919  }
920#endif
921 }
922#endif
923
924 if ( output_buffer_init(&sa) == -1 ) {
925  ROAR_ERR("Can not init output buffer!");
926  return 1;
927 }
928
929 if ( driver == NULL ) {
930  driver = "null";
931 } else {
932  ROAR_WARN("Usage of old driver interface. use -o not -d!");
933 }
934
935 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
936  ROAR_ERR("Can not open output driver!");
937  return 1;
938 }
939
940 if ( samples_init() == -1 ) {
941  ROAR_ERR("Can not init samples!");
942  return 1;
943 }
944
945
946 // we should handle this on microcontrollers, too.
947#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
948 signal(SIGINT,  on_sig_int);
949 signal(SIGCHLD, on_sig_chld);
950 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
951#endif
952
953 if ( realtime ) {
954#ifdef DEBUG
955  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
956#endif
957
958#ifdef ROAR_HAVE_NICE
959  errno = 0;
960  nice(-5*realtime); // -5 for each --realtime
961  if ( errno ) {
962   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
963  }
964#else
965  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
966#endif
967/*
968#ifdef __linux__
969  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
970   ROAR_WARN("Can not set io priority: %s", strerror(errno));
971#endif
972*/
973 }
974
975#ifdef ROAR_HAVE_SETGID
976 if ( setids & R_SETGID ) {
977  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
978   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
979  }
980  if ( !grp || setgid(grp->gr_gid) == -1 ) {
981   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
982  }
983 }
984#endif
985
986
987 clients_set_pid(g_self_client, getpid());
988#ifdef ROAR_HAVE_GETUID
989 clients_set_uid(g_self_client, getuid());
990#endif
991#ifdef ROAR_HAVE_GETGID
992 clients_set_gid(g_self_client, getgid());
993#endif
994 clients_get(g_self_client, &self);
995
996 if ( self == NULL ) {
997  ROAR_ERR("Can not get self client!");
998  return 1;
999 }
1000
1001 strcpy(self->name, "RoarAudio daemon internal");
1002
1003#ifdef ROAR_HAVE_FORK
1004 if ( daemon ) {
1005  close(ROAR_STDIN );
1006  close(ROAR_STDOUT);
1007  close(ROAR_STDERR);
1008
1009  if ( fork() )
1010   ROAR_U_EXIT(0);
1011
1012#ifdef ROAR_HAVE_SETSID
1013  setsid();
1014#endif
1015  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1016 }
1017#endif
1018
1019#ifdef ROAR_HAVE_CHROOT
1020 if (chrootdir) {
1021  if ( chroot(chrootdir) == -1 ) {
1022   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1023   return 2;
1024  }
1025  if ( chdir("/") == -1 ) {
1026   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1027   return 2;
1028  }
1029 }
1030#endif
1031
1032#ifdef ROAR_HAVE_SETUID
1033 if ( setids & R_SETUID ) {
1034  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1035   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1036   return 3;
1037  }
1038#ifdef ROAR_HAVE_GETUID
1039  clients_set_uid(g_self_client, getuid());
1040#endif
1041 }
1042#endif
1043
1044 // Register with OpenSLP:
1045#ifdef ROAR_HAVE_LIBSLP
1046 if ( reg_slp ) {
1047  register_slp(0, server);
1048 }
1049#endif
1050
1051 // start main loop...
1052 main_loop(drvid, drvinst, &sa, sysclocksync);
1053
1054 // clean up.
1055 clean_quit_prep();
1056 driver_close(drvinst, drvid);
1057 output_buffer_free();
1058
1059 return 0;
1060}
1061
1062void cleanup_listen_socket (int terminate) {
1063 // Deregister from SLP:
1064#ifdef ROAR_HAVE_LIBSLP
1065 register_slp(1, NULL);
1066#endif
1067
1068#ifdef ROAR_SUPPORT_LISTEN
1069 if ( g_listen_socket != -1 ) {
1070#ifdef ROAR_HAVE_IO_POSIX
1071  close(g_listen_socket);
1072#endif // #else is useless because we are in void context.
1073
1074  g_listen_socket = -1;
1075
1076#ifdef ROAR_HAVE_UNIX
1077  if ( *server == '/' )
1078   unlink(server);
1079#endif
1080 }
1081
1082#endif
1083
1084 if ( terminate )
1085  g_terminate = 1;
1086}
1087
1088void clean_quit_prep (void) {
1089 cleanup_listen_socket(0);
1090
1091 sources_free();
1092 streams_free();
1093 clients_free();
1094 midi_cb_stop(); // stop console beep
1095 midi_free();
1096 light_free();
1097}
1098
1099void clean_quit (void) {
1100 clean_quit_prep();
1101// driver_close(drvinst, drvid);
1102// output_buffer_free();
1103 exit(0);
1104}
1105
1106//ll
Note: See TracBrowser for help on using the repository browser.