source: roaraudio/roard/roard.c @ 2087:30d3653999b9

Last change on this file since 2087:30d3653999b9 was 2087:30d3653999b9, checked in by phi, 15 years ago

added some debug outputs and got setting of quality for vorbis working :)

File size: 29.2 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 ( blocks != -1 )
333  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
334
335 if ( blocksize != -1 )
336  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
337
338 // TODO: we shoudld *really* check for errors here...
339 if ( channel != -1 ) {
340  tu16 = channel;
341  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16);
342 }
343 if ( universe != -1 ) {
344  tu16 = universe;
345  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16);
346 }
347
348 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
349
350 streams_set_fh(stream, -1); // update some internal structures
351
352 if ( q > -1e6 ) {
353  ROAR_DBG("add_output(*): setting q=%f", q);
354  streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q);
355 }
356
357 client_stream_add(g_source_client, stream);
358
359 if ( prim ) {
360  streams_mark_primary(stream);
361  s->pos_rel_id = stream;
362 }
363
364 if ( sync ) {
365  streams_set_flag(stream, ROAR_FLAG_SYNC);
366 } else {
367  streams_reset_flag(stream, ROAR_FLAG_SYNC);
368 }
369
370 if ( f_mmap )
371  streams_set_flag(stream, ROAR_FLAG_MMAP);
372
373 return 0;
374}
375
376
377// SLP:
378void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) {
379 /* return the error code in the cookie */
380 *(SLPError*)cookie = errcode;
381}
382
383int register_slp (int unreg, char * sockname) {
384#ifdef ROAR_HAVE_LIBSLP
385 static int regged = 0;
386 static char * sn = NULL;
387 SLPError err;
388 SLPError callbackerr;
389 SLPHandle hslp;
390 char addr[1024];
391 char attr[1024] = "";
392 char * location;
393
394 if ( sockname != NULL )
395  sn = sockname;
396
397 snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn);
398
399 err = SLPOpen("en", SLP_FALSE, &hslp);
400
401 if (err != SLP_OK) {
402  ROAR_ERR("Error opening slp handle: Error #%i", err);
403  return -1;
404 }
405
406 if (!unreg) {
407
408  if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) {
409   ROAR_ERR("Error using SLPEscape() on server location, really bad!");
410   SLPClose(hslp);
411   return -1;
412  }
413
414  snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i),"
415                               "(light-channels=%i),(location=%s)",
416           g_sa->rate, g_sa->channels, g_sa->bits,
417           g_light_state.channels,
418           location
419          );
420
421  /* Register a service with SLP */
422  err = SLPReg(hslp,
423               addr,
424               SLP_LIFETIME_MAXIMUM,
425               0,
426               attr,
427               SLP_TRUE,
428               register_slp_callback,
429               &callbackerr);
430  regged = 1;
431 } else if ( unreg && regged ) {
432  err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr);
433  regged = 0;
434 } else {
435  SLPClose(hslp);
436  return -1;
437 }
438
439 /* err may contain an error code that occurred as the slp library    */
440 /* _prepared_ to make the call.                                     */
441 if ( err != SLP_OK ) {
442  ROAR_ERR("Error (de)registering service with slp: Error #%i", err);
443  return -1;
444 }
445
446 /* callbackerr may contain an error code (that was assigned through */
447 /* the callback cookie) that occurred as slp packets were sent on    */
448 /* the wire */
449 if (callbackerr != SLP_OK) {
450  ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr);
451  return -1;
452 }
453
454 SLPClose(hslp);
455 return 0;
456#else
457 return -1;
458#endif
459}
460
461
462// MAIN:
463
464#ifdef ROAR_HAVE_MAIN_ARGS
465int main (int argc, char * argv[]) {
466#else
467int main (void) {
468#endif
469#ifdef ROAR_HAVE_MAIN_ARGS
470 int i;
471 char * k;
472#endif
473#ifdef ROAR_SUPPORT_LISTEN
474 char user_sock[80]  = {0};
475#endif
476 struct roar_audio_info sa, max_sa;
477 struct roard_config config;
478#ifdef ROAR_HAVE_FORK
479 int    daemon       = 0;
480#endif
481 int    realtime     = 0;
482 int    sysclocksync = 0;
483 char * driver    = NULL;
484 char * device    = NULL;
485#ifdef ROAR_HAVE_MAIN_ARGS
486 char * opts      = NULL;
487#endif
488// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
489#ifdef ROAR_SUPPORT_LISTEN
490 int      port    = ROAR_DEFAULT_PORT;
491#endif
492 int               drvid;
493 char * s_drv     = "cf";
494 char * s_dev     = NULL;
495 char * s_con     = NULL;
496 char * s_opt     = NULL;
497 int    s_prim    = 0;
498 char * o_drv     = getenv("ROAR_DRIVER");
499 char * o_dev     = getenv("ROAR_DEVICE");
500 char * o_opts    = NULL;
501 int    o_prim    = 0;
502 int    o_count   = 0;
503 int    light_channels = LIGHT_CHANNELS_DEFAULT;
504 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
505 char * sock_user = NULL;
506#ifdef ROAR_SUPPORT_LISTEN
507 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
508#endif
509#ifdef ROAR_HAVE_LIBSLP
510 int    reg_slp   = 0;
511#endif
512#ifdef ROAR_HAVE_CHROOT
513 char * chrootdir = NULL;
514#endif
515#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
516 int    setids    = 0;
517#endif
518#ifdef ROAR_HAVE_UNIX
519 char * env_roar_proxy_backup;
520#endif
521#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
522 struct group   * grp  = NULL;
523#endif
524#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
525 struct passwd  * pwd  = NULL;
526#endif
527#ifdef ROAR_HAVE_GETSERVBYNAME
528 struct servent * serv = NULL;
529#endif
530 DRIVER_USERDATA_T drvinst;
531 struct roar_client * self = NULL;
532#ifdef ROAR_HAVE_LIBDNET
533 char decnethost[80];
534#endif
535
536 g_standby       =  0;
537 g_autostandby   =  0;
538 alive           =  1;
539#ifdef ROAR_SUPPORT_LISTEN
540 g_no_listen     =  0;
541 g_listen_socket = -1;
542#else
543 g_terminate     =  1;
544#endif
545
546 sa.bits     = ROAR_BITS_DEFAULT;
547 sa.channels = ROAR_CHANNELS_DEFAULT;
548 sa.rate     = ROAR_RATE_DEFAULT;
549 sa.codec    = ROAR_CODEC_DEFAULT;
550
551 g_sa        = &sa;
552 g_max_sa    = &max_sa;
553
554 memcpy(g_max_sa, g_sa, sizeof(max_sa));
555
556 g_config = &config;
557
558 if ( init_config() == -1 ) {
559  ROAR_ERR("Can not init default config!");
560  return 1;
561 }
562
563 if ( midi_init_config() == -1 ) {
564  ROAR_ERR("Can not init MIDI config!");
565  return 1;
566 }
567
568#ifdef ROAR_SUPPORT_LISTEN
569#ifdef ROAR_HAVE_GETUID
570 if ( getuid() != 0 && getenv("HOME") != NULL ) {
571  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
572  server = user_sock;
573 }
574#endif
575
576 if ( getenv("ROAR_SERVER") != NULL )
577  server = getenv("ROAR_SERVER");
578#endif
579
580 if ( clients_init() == -1 ) {
581  ROAR_ERR("Can not init clients!");
582  return 1;
583 }
584
585 if ( streams_init() == -1 ) {
586  ROAR_ERR("Can not init streams!");
587  return 1;
588 }
589
590 if ( (g_self_client = clients_new()) == -1 ) {
591  ROAR_ERR("Can not create self client!");
592  return 1;
593 }
594
595 if ( sources_init() == -1 ) {
596  ROAR_ERR("Can not init sources!");
597  return 1;
598 }
599
600 if ( (sources_set_client(g_self_client)) == -1 ) {
601  ROAR_ERR("Can not init set source client!");
602  return 1;
603 }
604
605#ifdef ROAR_HAVE_MAIN_ARGS
606 for (i = 1; i < argc; i++) {
607  k = argv[i];
608
609  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
610   usage();
611   return 0;
612
613  } else if ( strcmp(k, "--restart") == 0 ) {
614#ifdef ROAR_SUPPORT_LISTEN
615   if ( restart_server(server) == -1 ) {
616    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
617   }
618#else
619   ROAR_ERR("--restart not supported");
620#endif
621
622  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
623#ifdef ROAR_HAVE_FORK
624   daemon = 1;
625#else
626   ROAR_ERR("--daemon not supported");
627#endif
628  } else if ( strcmp(k, "--terminate") == 0 ) {
629   g_terminate = 1;
630  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
631   sysclocksync = 1000;
632  } else if ( strcmp(k, "--realtime") == 0 ) {
633   realtime++;
634  } else if ( strcmp(k, "--chroot") == 0 ) {
635#ifdef ROAR_HAVE_CHROOT
636   chrootdir = argv[++i];
637#else
638   ROAR_ERR("--chroot not supported");
639   i++;
640#endif
641  } else if ( strcmp(k, "--setgid") == 0 ) {
642#ifdef ROAR_HAVE_SETGID
643   setids |= R_SETGID;
644#else
645   ROAR_ERR("--setgid not supported");
646#endif
647  } else if ( strcmp(k, "--setuid") == 0 ) {
648#ifdef ROAR_HAVE_SETUID
649   setids |= R_SETUID;
650#else
651   ROAR_ERR("--setuid not supported");
652#endif
653  } else if ( strcmp(k, "--location") == 0 ) {
654   g_config->location = argv[++i];
655
656  } else if ( strcmp(k, "--list-cf") == 0 ) {
657   print_codecfilterlist();
658   return 0;
659
660  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
661   sa.rate = atoi(argv[++i]);
662  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
663   sa.bits = atoi(argv[++i]);
664  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
665   sa.channels = atoi(argv[++i]);
666
667  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
668   driver = argv[++i];
669   if ( strcmp(driver, "list") == 0 ) {
670    ROAR_WARN("The option is obsolete, use --list-driver!");
671    print_driverlist();
672    return 0;
673   }
674  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
675   device = argv[++i];
676  } else if ( strcmp(k, "-dO") == 0 ) {
677   opts = argv[++i];
678  } else if ( strcmp(k, "--list-driver") == 0 ) {
679   print_driverlist();
680   return 0;
681
682  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
683   o_drv  = argv[++i];
684  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
685   o_dev  = argv[++i];
686  } else if ( strcmp(k, "-oO") == 0 ) {
687   o_opts = argv[++i];
688  } else if ( strcmp(k, "-oP") == 0 ) {
689   o_prim = 1;
690  } else if ( strcmp(k, "-oN") == 0 ) {
691   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
692    o_count++;
693
694   o_drv  = o_dev = o_opts = NULL;
695   o_prim = 0;
696
697  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
698   s_drv = argv[++i];
699  } else if ( strcmp(k, "-S") == 0 ) {
700   s_dev = argv[++i];
701  } else if ( strcmp(k, "-sO") == 0 ) {
702   s_opt = argv[++i];
703  } else if ( strcmp(k, "-sC") == 0 ) {
704   s_con = argv[++i];
705  } else if ( strcmp(k, "-sP") == 0 ) {
706   s_prim = 1;
707  } else if ( strcmp(k, "-sN") == 0 ) {
708   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
709    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
710   }
711   s_opt = s_dev = s_con = NULL;
712   s_drv = "cf";
713   s_prim = 0;
714
715  } else if ( strcmp(k, "--light-channels") == 0 ) {
716   light_channels = atoi(argv[++i]);
717
718  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
719   midi_config.init_cb = 0;
720  } else if ( strcmp(k, "--midi-console") == 0 ) {
721   midi_config.console_dev = argv[++i];
722
723  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
724   // This is only usefull in INET not UNIX mode.
725#ifdef ROAR_SUPPORT_LISTEN
726   if ( *server == '/' )
727    server = ROAR_DEFAULT_HOST;
728
729   errno = 0;
730   if ( (port = atoi(argv[++i])) < 1 ) {
731#ifdef ROAR_HAVE_GETSERVBYNAME
732    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
733     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
734     return 1;
735    }
736    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
737    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
738            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
739    port = ROAR_NET2HOST16(serv->s_port);
740#else
741    ROAR_ERR("invalite port number: %s", argv[i]);
742    return 1;
743#endif
744   }
745#endif
746  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
747#ifdef ROAR_SUPPORT_LISTEN
748   server = argv[++i];
749#endif
750
751  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
752#ifdef ROAR_SUPPORT_LISTEN
753   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
754    sock_type = ROAR_SOCKET_TYPE_TCP;
755
756   if ( *server == '/' )
757    server = ROAR_DEFAULT_HOST;
758#endif
759
760  } else if ( strcmp(k, "-4") == 0 ) {
761#ifdef ROAR_SUPPORT_LISTEN
762   sock_type = ROAR_SOCKET_TYPE_TCP;
763   if ( *server == '/' )
764    server = ROAR_DEFAULT_HOST;
765#endif
766  } else if ( strcmp(k, "-6") == 0 ) {
767#ifdef ROAR_SUPPORT_LISTEN
768#ifdef PF_INET6
769   sock_type = ROAR_SOCKET_TYPE_TCP6;
770   if ( *server == '/' )
771    server = ROAR_DEFAULT_HOST;
772#else
773    ROAR_ERR("No IPv6 support compiled in!");
774    return 1;
775#endif
776#endif
777
778  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
779#ifdef ROAR_SUPPORT_LISTEN
780   // ignore this case as it is the default behavor.
781   sock_type = ROAR_SOCKET_TYPE_UNIX;
782#endif
783
784  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
785#ifdef ROAR_SUPPORT_LISTEN
786#ifdef ROAR_HAVE_LIBDNET
787    port   = ROAR_DEFAULT_NUM;
788    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
789    server = decnethost;
790    sock_type = ROAR_SOCKET_TYPE_DECNET;
791#else
792    ROAR_ERR("No DECnet support compiled in!");
793    return 1;
794#endif
795#endif
796
797  } else if ( strcmp(k, "--slp") == 0 ) {
798#ifdef ROAR_HAVE_LIBSLP
799   reg_slp = 1;
800#else
801    ROAR_ERR("No OpenSLP support compiled in!");
802    return 1;
803#endif
804
805  } else if ( strcmp(k, "-G") == 0 ) {
806   sock_grp  = argv[++i];
807  } else if ( strcmp(k, "-U") == 0 ) {
808   sock_user = argv[++i];
809
810  } else if ( strcmp(k, "--no-listen") == 0 ) {
811#ifdef ROAR_SUPPORT_LISTEN
812   server      = "";
813   g_terminate = 1;
814   g_no_listen = 1;
815#endif
816  } else if ( strcmp(k, "--client-fh") == 0 ) {
817   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
818    ROAR_ERR("main(*): Can not set client's fh");
819    return 1;
820   }
821  } else if ( strcmp(k, "--close-fh") == 0 ) {
822#ifdef ROAR_HAVE_IO_POSIX
823   close(atoi(argv[++i]));
824#else
825   i++;
826   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
827#endif
828
829  } else if ( strcmp(k, "--standby") == 0 ) {
830   g_standby = 1;
831  } else if ( strcmp(k, "--auto-standby") == 0 ) {
832   g_autostandby = 1;
833  } else {
834   usage();
835   return 1;
836  }
837
838 }
839#endif
840
841 if ( s_dev != NULL ) {
842  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
843   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
844  }
845 }
846
847 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
848
849 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
850
851 if ( midi_init() == -1 ) {
852  ROAR_ERR("Can not initialize MIDI subsystem");
853 }
854
855 if ( light_init(light_channels) == -1 ) {
856  ROAR_ERR("Can not initialize light control subsystem");
857 }
858
859#ifdef ROAR_SUPPORT_LISTEN
860 if ( *server != 0 ) {
861  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
862#ifdef ROAR_HAVE_UNIX
863   if ( *server == '/' ) {
864    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
865     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
866     unsetenv("ROAR_PROXY");
867    }
868    if ( (i = roar_socket_connect(server, port)) != -1 ) {
869     close(i);
870     ROAR_ERR("Can not open listen socket!");
871     return 1;
872    } else {
873     unlink(server);
874     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
875      ROAR_ERR("Can not open listen socket!");
876      return 1;
877     }
878    }
879    if ( env_roar_proxy_backup != NULL ) {
880     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
881     free(env_roar_proxy_backup);
882    }
883#else
884   if (0) { // noop
885#endif
886   } else {
887    ROAR_ERR("Can not open listen socket!");
888    return 1;
889   }
890  }
891
892#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
893  if ( (grp = getgrnam(sock_grp)) == NULL ) {
894   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
895  }
896#endif
897#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
898  if ( sock_user || (setids & R_SETUID) ) {
899   if ( (pwd = getpwnam(sock_user)) == NULL ) {
900    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
901   }
902  }
903#endif
904
905#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
906  if ( *server == '/' ) {
907   if ( grp ) {
908    if ( pwd ) {
909     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
910      return 1;
911    } else {
912     if ( chown(server, -1, grp->gr_gid) == -1 )
913      return 1;
914    }
915#ifdef ROAR_HAVE_GETUID
916    if ( getuid() == 0 )
917     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
918      return 1;
919#endif
920   }
921  }
922#endif
923 }
924#endif
925
926 if ( output_buffer_init(&sa) == -1 ) {
927  ROAR_ERR("Can not init output buffer!");
928  return 1;
929 }
930
931 if ( driver == NULL ) {
932  driver = "null";
933 } else {
934  ROAR_WARN("Usage of old driver interface. use -o not -d!");
935 }
936
937 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
938  ROAR_ERR("Can not open output driver!");
939  return 1;
940 }
941
942 if ( samples_init() == -1 ) {
943  ROAR_ERR("Can not init samples!");
944  return 1;
945 }
946
947
948 // we should handle this on microcontrollers, too.
949#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
950 signal(SIGINT,  on_sig_int);
951 signal(SIGCHLD, on_sig_chld);
952 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
953#endif
954
955 if ( realtime ) {
956#ifdef DEBUG
957  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
958#endif
959
960#ifdef ROAR_HAVE_NICE
961  errno = 0;
962  nice(-5*realtime); // -5 for each --realtime
963  if ( errno ) {
964   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
965  }
966#else
967  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
968#endif
969/*
970#ifdef __linux__
971  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
972   ROAR_WARN("Can not set io priority: %s", strerror(errno));
973#endif
974*/
975 }
976
977#ifdef ROAR_HAVE_SETGID
978 if ( setids & R_SETGID ) {
979  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
980   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
981  }
982  if ( !grp || setgid(grp->gr_gid) == -1 ) {
983   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
984  }
985 }
986#endif
987
988
989 clients_set_pid(g_self_client, getpid());
990#ifdef ROAR_HAVE_GETUID
991 clients_set_uid(g_self_client, getuid());
992#endif
993#ifdef ROAR_HAVE_GETGID
994 clients_set_gid(g_self_client, getgid());
995#endif
996 clients_get(g_self_client, &self);
997
998 if ( self == NULL ) {
999  ROAR_ERR("Can not get self client!");
1000  return 1;
1001 }
1002
1003 strcpy(self->name, "RoarAudio daemon internal");
1004
1005#ifdef ROAR_HAVE_FORK
1006 if ( daemon ) {
1007  close(ROAR_STDIN );
1008  close(ROAR_STDOUT);
1009  close(ROAR_STDERR);
1010
1011  if ( fork() )
1012   ROAR_U_EXIT(0);
1013
1014#ifdef ROAR_HAVE_SETSID
1015  setsid();
1016#endif
1017  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
1018 }
1019#endif
1020
1021#ifdef ROAR_HAVE_CHROOT
1022 if (chrootdir) {
1023  if ( chroot(chrootdir) == -1 ) {
1024   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
1025   return 2;
1026  }
1027  if ( chdir("/") == -1 ) {
1028   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
1029   return 2;
1030  }
1031 }
1032#endif
1033
1034#ifdef ROAR_HAVE_SETUID
1035 if ( setids & R_SETUID ) {
1036  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
1037   ROAR_ERR("Can not set UserID: %s", strerror(errno));
1038   return 3;
1039  }
1040#ifdef ROAR_HAVE_GETUID
1041  clients_set_uid(g_self_client, getuid());
1042#endif
1043 }
1044#endif
1045
1046 // Register with OpenSLP:
1047#ifdef ROAR_HAVE_LIBSLP
1048 if ( reg_slp ) {
1049  register_slp(0, server);
1050 }
1051#endif
1052
1053 // start main loop...
1054 main_loop(drvid, drvinst, &sa, sysclocksync);
1055
1056 // clean up.
1057 clean_quit_prep();
1058 driver_close(drvinst, drvid);
1059 output_buffer_free();
1060
1061 return 0;
1062}
1063
1064void cleanup_listen_socket (int terminate) {
1065 // Deregister from SLP:
1066#ifdef ROAR_HAVE_LIBSLP
1067 register_slp(1, NULL);
1068#endif
1069
1070#ifdef ROAR_SUPPORT_LISTEN
1071 if ( g_listen_socket != -1 ) {
1072#ifdef ROAR_HAVE_IO_POSIX
1073  close(g_listen_socket);
1074#endif // #else is useless because we are in void context.
1075
1076  g_listen_socket = -1;
1077
1078#ifdef ROAR_HAVE_UNIX
1079  if ( *server == '/' )
1080   unlink(server);
1081#endif
1082 }
1083
1084#endif
1085
1086 if ( terminate )
1087  g_terminate = 1;
1088}
1089
1090void clean_quit_prep (void) {
1091 cleanup_listen_socket(0);
1092
1093 sources_free();
1094 streams_free();
1095 clients_free();
1096 midi_cb_stop(); // stop console beep
1097 midi_free();
1098 light_free();
1099}
1100
1101void clean_quit (void) {
1102 clean_quit_prep();
1103// driver_close(drvinst, drvid);
1104// output_buffer_free();
1105 exit(0);
1106}
1107
1108//ll
Note: See TracBrowser for help on using the repository browser.