main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Additions (c) Paul Hermann, 2015-2017 under the same license terms
  21. * -Control of Raspberry pi GPIO for amplifier power
  22. * -Launch script on power status change from LMS
  23. */
  24. #include "squeezelite.h"
  25. #include <signal.h>
  26. #define TITLE "Squeezelite " VERSION ", Copyright 2012-2015 Adrian Smith, 2015-2019 Ralph Irving."
  27. #define CODECS_BASE "flac,pcm,mp3,ogg"
  28. #if NO_FAAD
  29. #define CODECS_AAC ""
  30. #else
  31. #define CODECS_AAC ",aac"
  32. #endif
  33. #if FFMPEG
  34. #define CODECS_FF ",wma,alac"
  35. #else
  36. #define CODECS_FF ""
  37. #endif
  38. #if DSD
  39. #define CODECS_DSD ",dsd"
  40. #else
  41. #define CODECS_DSD ""
  42. #endif
  43. #define CODECS_MP3 " (mad,mpg for specific mp3 codec)"
  44. #define CODECS CODECS_BASE CODECS_AAC CODECS_FF CODECS_DSD CODECS_MP3
  45. static void usage(const char *argv0) {
  46. printf(TITLE " See -t for license terms\n"
  47. "Usage: %s [options]\n"
  48. " -s <server>[:<port>]\tConnect to specified server, otherwise uses autodiscovery to find server\n"
  49. #if !EMBEDDED
  50. " -o <output device>\tSpecify output device, default \"default\", - = output to stdout\n"
  51. " -l \t\t\tList output devices\n"
  52. #endif
  53. #if ALSA
  54. " -a <b>:<p>:<f>:<m>\tSpecify ALSA params to open output device, b = buffer time in ms or size in bytes, p = period count or size in bytes, f sample format (16|24|24_3|32), m = use mmap (0|1)\n"
  55. #endif
  56. #if PORTAUDIO
  57. #if PA18API
  58. " -a <frames>:<buffers>\tSpecify output target 4 byte frames per buffer, number of buffers\n"
  59. #elif OSX && !defined(OSXPPC)
  60. " -a <l>:<r>\t\tSpecify Portaudio params to open output device, l = target latency in ms, r = allow OSX to resample (0|1)\n"
  61. #elif WIN
  62. " -a <l>:<e>\t\tSpecify Portaudio params to open output device, l = target latency in ms, e = use exclusive mode for WASAPI (0|1)\n"
  63. #else
  64. " -a <l>\t\tSpecify Portaudio params to open output device, l = target latency in ms\n"
  65. #endif
  66. #endif
  67. " -a <f>\t\tSpecify sample format (16|24|32) of output file when using -o - to output samples to stdout (interleaved little endian only)\n"
  68. " -b <stream>:<output>\tSpecify internal Stream and Output buffer sizes in Kbytes\n"
  69. " -c <codec1>,<codec2>\tRestrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS "\n"
  70. " \t\t\tCodecs reported to LMS in order listed, allowing codec priority refinement.\n"
  71. " -C <timeout>\t\tClose output device when idle after timeout seconds, default is to keep it open while player is 'on'\n"
  72. #if !IR
  73. " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output, level: info|debug|sdebug\n"
  74. #else
  75. " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  76. #endif
  77. #if defined(GPIO) && defined(RPI)
  78. " -G <Rpi GPIO#>:<H/L>\tSpecify the BCM GPIO# to use for Amp Power Relay and if the output should be Active High or Low\n"
  79. #endif
  80. " -e <codec1>,<codec2>\tExplicitly exclude native support of one or more codecs; known codecs: " CODECS "\n"
  81. " -f <logfile>\t\tWrite debug to logfile\n"
  82. #if IR
  83. " -i [<filename>]\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n"
  84. #endif
  85. " -m <mac addr>\t\tSet mac address, format: ab:cd:ef:12:34:56\n"
  86. " -M <modelname>\tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n"
  87. " -n <name>\t\tSet the player name\n"
  88. " -N <filename>\t\tStore player name in filename to allow server defined name changes to be shared between servers (not supported with -n)\n"
  89. " -W\t\t\tRead wave and aiff format from header, ignore server parameters\n"
  90. #if ALSA
  91. " -p <priority>\t\tSet real time priority of output thread (1-99)\n"
  92. #endif
  93. #if LINUX || FREEBSD || SUN
  94. " -P <filename>\t\tStore the process id (PID) in filename\n"
  95. #endif
  96. " -r <rates>[:<delay>]\tSample rates supported, allows output to be off when squeezelite is started; rates = <maxrate>|<minrate>-<maxrate>|<rate1>,<rate2>,<rate3>; delay = optional delay switching rates in ms\n"
  97. #if GPIO
  98. " -S <Power Script>\tAbsolute path to script to launch on power commands from LMS\n"
  99. #endif
  100. #if RESAMPLE
  101. " -R -u [params]\tResample, params = <recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>,\n"
  102. " \t\t\t recipe = (v|h|m|l|q)(L|I|M)(s) [E|X], E = exception - resample only if native rate not supported, X = async - resample to max rate for device, otherwise to max sync rate\n"
  103. " \t\t\t flags = num in hex,\n"
  104. " \t\t\t attenuation = attenuation in dB to apply (default is -1db if not explicitly set),\n"
  105. " \t\t\t precision = number of bits precision (NB. HQ = 20. VHQ = 28),\n"
  106. " \t\t\t passband_end = number in percent (0dB pt. bandwidth to preserve. nyquist = 100%%),\n"
  107. " \t\t\t stopband_start = number in percent (Aliasing/imaging control. > passband_end),\n"
  108. " \t\t\t phase_response = 0-100 (0 = minimum / 50 = linear / 100 = maximum)\n"
  109. #endif
  110. #if RESAMPLE16
  111. " -R -u [params]\tResample, params = (b|l|m)[:i],\n"
  112. " \t\t\t b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients\n"
  113. #endif
  114. #if DSD
  115. #if ALSA
  116. " -D [delay][:format]\tOutput device supports DSD, delay = optional delay switching between PCM and DSD in ms\n"
  117. " \t\t\t format = dop (default if not specified), u8, u16le, u16be, u32le or u32be.\n"
  118. #else
  119. " -D [delay]\t\tOutput device supports DSD over PCM (DoP), delay = optional delay switching between PCM and DoP in ms\n"
  120. #endif
  121. #endif
  122. #if VISEXPORT
  123. " -v \t\t\tVisualiser support\n"
  124. #endif
  125. # if ALSA
  126. " -O <mixer device>\tSpecify mixer device, defaults to 'output device'\n"
  127. " -L \t\t\tList volume controls for output device\n"
  128. " -U <control>\t\tUnmute ALSA control and set to full volume (not supported with -V)\n"
  129. " -V <control>\t\tUse ALSA control for volume adjustment, otherwise use software volume adjustment\n"
  130. " -X \t\t\tUse linear volume adjustments instead of in terms of dB (only for hardware volume control)\n"
  131. #endif
  132. #if LINUX || FREEBSD || SUN
  133. " -z \t\t\tDaemonize\n"
  134. #endif
  135. #if RESAMPLE || RESAMPLE16
  136. " -Z <rate>\t\tReport rate to server in helo as the maximum sample rate we can support\n"
  137. #endif
  138. " -t \t\t\tLicense terms\n"
  139. " -? \t\t\tDisplay this help text\n"
  140. "\n"
  141. "Build options:"
  142. #if SUN
  143. " SOLARIS"
  144. #elif LINUX
  145. " LINUX"
  146. #endif
  147. #if WIN
  148. " WIN"
  149. #endif
  150. #if OSX
  151. " OSX"
  152. #endif
  153. #if OSXPPC
  154. "PPC"
  155. #endif
  156. #if FREEBSD
  157. " FREEBSD"
  158. #endif
  159. #if ALSA
  160. " ALSA"
  161. #endif
  162. #if PORTAUDIO
  163. " PORTAUDIO"
  164. #if PA18API
  165. "18"
  166. #endif
  167. #endif
  168. #if EMBEDDED
  169. " EMBEDDED"
  170. #endif
  171. #if EVENTFD
  172. " EVENTFD"
  173. #endif
  174. #if SELFPIPE
  175. " SELFPIPE"
  176. #endif
  177. #if LOOPBACK
  178. " LOOPBACK"
  179. #endif
  180. #if WINEVENT
  181. " WINEVENT"
  182. #endif
  183. #if RESAMPLE_MP
  184. " RESAMPLE_MP"
  185. #else
  186. #if RESAMPLE
  187. " RESAMPLE"
  188. #endif
  189. #if RESAMPLE16
  190. " RESAMPLE16"
  191. #endif
  192. #endif
  193. #if FFMPEG
  194. " FFMPEG"
  195. #endif
  196. #if NO_FAAD
  197. " NO_FAAD"
  198. #endif
  199. #if VISEXPORT
  200. " VISEXPORT"
  201. #endif
  202. #if IR
  203. " IR"
  204. #endif
  205. #if GPIO
  206. " GPIO"
  207. #endif
  208. #if RPI
  209. " RPI"
  210. #endif
  211. #if DSD
  212. " DSD"
  213. #endif
  214. #if USE_SSL
  215. " SSL"
  216. #endif
  217. #if NO_SSLSYM
  218. " NO_SSLSYM"
  219. #endif
  220. #if LINKALL
  221. " LINKALL"
  222. #endif
  223. "\n\n",
  224. argv0);
  225. }
  226. static void license(void) {
  227. printf(TITLE "\n\n"
  228. "This program is free software: you can redistribute it and/or modify\n"
  229. "it under the terms of the GNU General Public License as published by\n"
  230. "the Free Software Foundation, either version 3 of the License, or\n"
  231. "(at your option) any later version.\n\n"
  232. "This program is distributed in the hope that it will be useful,\n"
  233. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  234. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  235. "GNU General Public License for more details.\n\n"
  236. "You should have received a copy of the GNU General Public License\n"
  237. "along with this program. If not, see <http://www.gnu.org/licenses/>.\n"
  238. #if DSD
  239. "\nContains dsd2pcm library Copyright 2009, 2011 Sebastian Gesemann which\n"
  240. "is subject to its own license.\n"
  241. "\nContains the Daphile Project full dsd patch Copyright 2013-2017 Daphile,\n"
  242. "which is subject to its own license.\n"
  243. #endif
  244. "\nOption to allow server side upsampling for PCM streams (-W) from\n"
  245. "squeezelite-R2 (c) Marco Curti 2015, marcoc1712@gmail.com.\n"
  246. #if GPIO
  247. "\nAdditions (c) Paul Hermann, 2015, 2017 under the same license terms\n"
  248. "- Launch a script on power status change\n"
  249. "- Control of Raspberry pi GPIO for amplifier power\n"
  250. #endif
  251. #if RPI
  252. "\nContains wiringpi GPIO Interface library Copyright (c) 2012-2017\n"
  253. "Gordon Henderson, which is subject to its own license.\n"
  254. #endif
  255. #if FFMPEG
  256. "\nThis software uses libraries from the FFmpeg project under\n"
  257. "the LGPLv2.1 and its source can be downloaded from\n"
  258. "<https://sourceforge.net/projects/lmsclients/files/source/>\n"
  259. #endif
  260. "\n"
  261. );
  262. }
  263. static void sighandler(int signum) {
  264. slimproto_stop();
  265. // remove ourselves in case above does not work, second SIGINT will cause non gracefull shutdown
  266. signal(signum, SIG_DFL);
  267. }
  268. int main(int argc, char **argv) {
  269. char *server = NULL;
  270. char *output_device = "default";
  271. char *include_codecs = NULL;
  272. char *exclude_codecs = "";
  273. char *name = NULL;
  274. char *namefile = NULL;
  275. char *modelname = NULL;
  276. extern bool pcm_check_header;
  277. extern bool user_rates;
  278. char *logfile = NULL;
  279. u8_t mac[6];
  280. unsigned stream_buf_size = STREAMBUF_SIZE;
  281. unsigned output_buf_size = 0; // set later
  282. unsigned rates[MAX_SUPPORTED_SAMPLERATES] = { 0 };
  283. unsigned rate_delay = 0;
  284. char *resample = NULL;
  285. char *output_params = NULL;
  286. unsigned idle = 0;
  287. #if LINUX || FREEBSD || SUN
  288. bool daemonize = false;
  289. char *pidfile = NULL;
  290. FILE *pidfp = NULL;
  291. #endif
  292. #if ALSA
  293. unsigned rt_priority = OUTPUT_RT_PRIORITY;
  294. char *mixer_device = output_device;
  295. char *output_mixer = NULL;
  296. bool output_mixer_unmute = false;
  297. bool linear_volume = false;
  298. #endif
  299. #if DSD
  300. unsigned dsd_delay = 0;
  301. dsd_format dsd_outfmt = PCM;
  302. #endif
  303. #if VISEXPORT
  304. bool visexport = false;
  305. #endif
  306. #if IR
  307. char *lircrc = NULL;
  308. #endif
  309. #if EMBEDDED
  310. int err = embedded_init();
  311. if (err) return err;
  312. #endif
  313. log_level log_output = lWARN;
  314. log_level log_stream = lWARN;
  315. log_level log_decode = lWARN;
  316. log_level log_slimproto = lWARN;
  317. #if IR
  318. log_level log_ir = lWARN;
  319. #endif
  320. int maxSampleRate = 0;
  321. char *optarg = NULL;
  322. int optind = 1;
  323. int i;
  324. #define MAXCMDLINE 512
  325. char cmdline[MAXCMDLINE] = "";
  326. get_mac(mac);
  327. for (i = 0; i < argc && (strlen(argv[i]) + strlen(cmdline) + 2 < MAXCMDLINE); i++) {
  328. strcat(cmdline, argv[i]);
  329. strcat(cmdline, " ");
  330. }
  331. while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') {
  332. char *opt = argv[optind] + 1;
  333. if (strstr("oabcCdefmMnNpPrsZ"
  334. #if ALSA
  335. "UVO"
  336. #endif
  337. , opt) && optind < argc - 1) {
  338. optarg = argv[optind + 1];
  339. optind += 2;
  340. } else if (strstr("ltz?W"
  341. #if ALSA
  342. "LX"
  343. #endif
  344. #if RESAMPLE || RESAMPLE16
  345. "uR"
  346. #endif
  347. #if DSD
  348. "D"
  349. #endif
  350. #if VISEXPORT
  351. "v"
  352. #endif
  353. #if IR
  354. "i"
  355. #endif
  356. #if defined(GPIO) && defined(RPI)
  357. "G"
  358. #endif
  359. #if GPIO
  360. "S"
  361. #endif
  362. , opt)) {
  363. optarg = NULL;
  364. optind += 1;
  365. } else {
  366. fprintf(stderr, "\nOption error: -%s\n\n", opt);
  367. usage(argv[0]);
  368. exit(1);
  369. }
  370. switch (opt[0]) {
  371. case 'o':
  372. output_device = optarg;
  373. #if ALSA
  374. mixer_device = optarg;
  375. #endif
  376. break;
  377. case 'a':
  378. output_params = optarg;
  379. break;
  380. case 'b':
  381. {
  382. char *s = next_param(optarg, ':');
  383. char *o = next_param(NULL, ':');
  384. if (s) stream_buf_size = atoi(s) * 1024;
  385. if (o) output_buf_size = atoi(o) * 1024;
  386. }
  387. break;
  388. case 'c':
  389. include_codecs = optarg;
  390. break;
  391. case 'C':
  392. if (atoi(optarg) > 0) {
  393. idle = atoi(optarg) * 1000;
  394. }
  395. break;
  396. case 'e':
  397. exclude_codecs = optarg;
  398. break;
  399. case 'd':
  400. {
  401. char *l = strtok(optarg, "=");
  402. char *v = strtok(NULL, "=");
  403. log_level new = lWARN;
  404. if (l && v) {
  405. if (!strcmp(v, "info")) new = lINFO;
  406. if (!strcmp(v, "debug")) new = lDEBUG;
  407. if (!strcmp(v, "sdebug")) new = lSDEBUG;
  408. if (!strcmp(l, "all") || !strcmp(l, "slimproto")) log_slimproto = new;
  409. if (!strcmp(l, "all") || !strcmp(l, "stream")) log_stream = new;
  410. if (!strcmp(l, "all") || !strcmp(l, "decode")) log_decode = new;
  411. if (!strcmp(l, "all") || !strcmp(l, "output")) log_output = new;
  412. #if IR
  413. if (!strcmp(l, "all") || !strcmp(l, "ir")) log_ir = new;
  414. #endif
  415. } else {
  416. fprintf(stderr, "\nDebug settings error: -d %s\n\n", optarg);
  417. usage(argv[0]);
  418. exit(1);
  419. }
  420. }
  421. break;
  422. case 'f':
  423. logfile = optarg;
  424. break;
  425. case 'm':
  426. {
  427. int byte = 0;
  428. char *tmp;
  429. if (!strncmp(optarg, "00:04:20", 8)) {
  430. LOG_ERROR("ignoring mac address from hardware player range 00:04:20:**:**:**");
  431. } else {
  432. char *t = strtok(optarg, ":");
  433. while (t && byte < 6) {
  434. mac[byte++] = (u8_t)strtoul(t, &tmp, 16);
  435. t = strtok(NULL, ":");
  436. }
  437. }
  438. }
  439. break;
  440. case 'M':
  441. modelname = optarg;
  442. break;
  443. case 'r':
  444. {
  445. char *rstr = next_param(optarg, ':');
  446. char *dstr = next_param(NULL, ':');
  447. if (rstr && strstr(rstr, ",")) {
  448. // parse sample rates and sort them
  449. char *r = next_param(rstr, ',');
  450. unsigned tmp[MAX_SUPPORTED_SAMPLERATES] = { 0 };
  451. int i, j;
  452. int last = 999999;
  453. for (i = 0; r && i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  454. tmp[i] = atoi(r);
  455. r = next_param(NULL, ',');
  456. }
  457. for (i = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  458. int largest = 0;
  459. for (j = 0; j < MAX_SUPPORTED_SAMPLERATES; ++j) {
  460. if (tmp[j] > largest && tmp[j] < last) {
  461. largest = tmp[j];
  462. }
  463. }
  464. rates[i] = last = largest;
  465. }
  466. } else if (rstr) {
  467. // optstr is <min>-<max> or <max>, extract rates from test rates within this range
  468. unsigned ref[] TEST_RATES;
  469. char *str1 = next_param(rstr, '-');
  470. char *str2 = next_param(NULL, '-');
  471. unsigned max = str2 ? atoi(str2) : (str1 ? atoi(str1) : ref[0]);
  472. unsigned min = str1 && str2 ? atoi(str1) : 0;
  473. unsigned tmp;
  474. int i, j;
  475. if (max < min) { tmp = max; max = min; min = tmp; }
  476. rates[0] = max;
  477. for (i = 0, j = 1; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  478. if (ref[i] < rates[j-1] && ref[i] >= min) {
  479. rates[j++] = ref[i];
  480. }
  481. }
  482. }
  483. if (dstr) {
  484. rate_delay = atoi(dstr);
  485. }
  486. if (rates[0]) {
  487. user_rates = true;
  488. }
  489. }
  490. break;
  491. case 's':
  492. server = optarg;
  493. break;
  494. case 'n':
  495. name = optarg;
  496. break;
  497. case 'N':
  498. namefile = optarg;
  499. break;
  500. case 'Z':
  501. maxSampleRate = atoi(optarg);
  502. break;
  503. case 'W':
  504. pcm_check_header = true;
  505. break;
  506. #if ALSA
  507. case 'p':
  508. rt_priority = atoi(optarg);
  509. if (rt_priority > 99 || rt_priority < 1) {
  510. fprintf(stderr, "\nError: invalid priority: %s\n\n", optarg);
  511. usage(argv[0]);
  512. exit(1);
  513. }
  514. break;
  515. #endif
  516. #if LINUX || FREEBSD || SUN
  517. case 'P':
  518. pidfile = optarg;
  519. break;
  520. #endif
  521. #if !EMBEDDED
  522. case 'l':
  523. list_devices();
  524. exit(0);
  525. break;
  526. #endif
  527. #if RESAMPLE || RESAMPLE16
  528. case 'u':
  529. case 'R':
  530. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  531. resample = argv[optind++];
  532. } else {
  533. resample = "";
  534. }
  535. break;
  536. #endif
  537. #if DSD
  538. case 'D':
  539. dsd_outfmt = DOP;
  540. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  541. char *dstr = next_param(argv[optind++], ':');
  542. char *fstr = next_param(NULL, ':');
  543. dsd_delay = dstr ? atoi(dstr) : 0;
  544. if (fstr) {
  545. if (!strcmp(fstr, "dop")) dsd_outfmt = DOP;
  546. if (!strcmp(fstr, "u8")) dsd_outfmt = DSD_U8;
  547. if (!strcmp(fstr, "u16le")) dsd_outfmt = DSD_U16_LE;
  548. if (!strcmp(fstr, "u32le")) dsd_outfmt = DSD_U32_LE;
  549. if (!strcmp(fstr, "u16be")) dsd_outfmt = DSD_U16_BE;
  550. if (!strcmp(fstr, "u32be")) dsd_outfmt = DSD_U32_BE;
  551. if (!strcmp(fstr, "dop24")) dsd_outfmt = DOP_S24_LE;
  552. if (!strcmp(fstr, "dop24_3")) dsd_outfmt = DOP_S24_3LE;
  553. }
  554. }
  555. break;
  556. #endif
  557. #if VISEXPORT
  558. case 'v':
  559. visexport = true;
  560. break;
  561. #endif
  562. #if ALSA
  563. case 'O':
  564. mixer_device = optarg;
  565. break;
  566. case 'L':
  567. list_mixers(mixer_device);
  568. exit(0);
  569. break;
  570. case 'X':
  571. linear_volume = true;
  572. break;
  573. case 'U':
  574. output_mixer_unmute = true;
  575. case 'V':
  576. if (output_mixer) {
  577. fprintf(stderr, "-U and -V option should not be used at same time\n");
  578. exit(1);
  579. }
  580. output_mixer = optarg;
  581. break;
  582. #endif
  583. #if IR
  584. case 'i':
  585. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  586. lircrc = argv[optind++];
  587. } else {
  588. lircrc = "~/.lircrc"; // liblirc_client will expand ~/
  589. }
  590. break;
  591. #endif
  592. #if defined(GPIO) && defined(RPI)
  593. case 'G':
  594. if (power_script != NULL){
  595. fprintf(stderr, "-G and -S options cannot be used together \n\n" );
  596. usage(argv[0]);
  597. exit(1);
  598. }
  599. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  600. char *gp = next_param(argv[optind++], ':');
  601. char *go = next_param (NULL, ':');
  602. gpio_pin = atoi(gp);
  603. if (go != NULL){
  604. if ((strcmp(go, "H")==0)|(strcmp(go, "h")==0)){
  605. gpio_active_low=false;
  606. }else if((strcmp(go, "L")==0)|(strcmp(go, "l")==0)){
  607. gpio_active_low=true;
  608. }else{
  609. fprintf(stderr,"Must set output to be active High or Low i.e. -G18:H or -G18:L\n");
  610. usage(argv[0]);
  611. exit(1);
  612. }
  613. }else{
  614. fprintf(stderr,"-G Option Error\n");
  615. usage(argv[0]);
  616. exit(1);
  617. }
  618. gpio_active = true;
  619. relay(0);
  620. } else {
  621. fprintf(stderr, "Error in GPIO Pin assignment.\n");
  622. usage(argv[0]);
  623. exit(1);
  624. }
  625. break;
  626. #endif
  627. #if GPIO
  628. case 'S':
  629. if (gpio_active){
  630. fprintf(stderr, "-G and -S options cannot be used together \n\n" );
  631. usage(argv[0]);
  632. exit(1);
  633. }
  634. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  635. power_script = argv[optind++];
  636. if( access( power_script, R_OK|X_OK ) == -1 ) {
  637. // file doesn't exist
  638. fprintf(stderr, "Script %s, not found\n\n", argv[optind-1]);
  639. usage(argv[0]);
  640. exit(1);
  641. }
  642. } else {
  643. fprintf(stderr, "No Script Name Given.\n\n");
  644. usage(argv[0]);
  645. exit(1);
  646. }
  647. relay_script(0);
  648. break;
  649. #endif
  650. #if LINUX || FREEBSD || SUN
  651. case 'z':
  652. daemonize = true;
  653. #if SUN
  654. init_daemonize();
  655. #endif /* SUN */
  656. break;
  657. #endif
  658. case 't':
  659. license();
  660. exit(0);
  661. break;
  662. case '?':
  663. usage(argv[0]);
  664. exit(0);
  665. break;
  666. default:
  667. fprintf(stderr, "Arg error: %s\n", argv[optind]);
  668. break;
  669. }
  670. }
  671. // warn if command line includes something which isn't parsed
  672. if (optind < argc) {
  673. fprintf(stderr, "\nError: command line argument error\n\n");
  674. usage(argv[0]);
  675. exit(1);
  676. }
  677. signal(SIGINT, sighandler);
  678. signal(SIGTERM, sighandler);
  679. #if defined(SIGQUIT)
  680. signal(SIGQUIT, sighandler);
  681. #endif
  682. #if defined(SIGHUP)
  683. signal(SIGHUP, sighandler);
  684. #endif
  685. #if USE_SSL && !LINKALL && !NO_SSLSYM
  686. ssl_loaded = load_ssl_symbols();
  687. #endif
  688. // set the output buffer size if not specified on the command line, take account of resampling
  689. if (!output_buf_size) {
  690. output_buf_size = OUTPUTBUF_SIZE;
  691. if (resample) {
  692. unsigned scale = 8;
  693. if (rates[0]) {
  694. scale = rates[0] / 44100;
  695. if (scale > 8) scale = 8;
  696. if (scale < 1) scale = 1;
  697. }
  698. output_buf_size *= scale;
  699. }
  700. }
  701. if (logfile) {
  702. if (!freopen(logfile, "a", stderr)) {
  703. fprintf(stderr, "error opening logfile %s: %s\n", logfile, strerror(errno));
  704. } else {
  705. if (log_output >= lINFO || log_stream >= lINFO || log_decode >= lINFO || log_slimproto >= lINFO) {
  706. fprintf(stderr, "\n%s\n", cmdline);
  707. }
  708. }
  709. }
  710. #if LINUX || FREEBSD || SUN
  711. if (pidfile) {
  712. if (!(pidfp = fopen(pidfile, "w")) ) {
  713. fprintf(stderr, "Error opening pidfile %s: %s\n", pidfile, strerror(errno));
  714. exit(1);
  715. }
  716. pidfile = realpath(pidfile, NULL); // daemonize will change cwd
  717. }
  718. if (daemonize) {
  719. if (daemon(0, logfile ? 1 : 0)) {
  720. fprintf(stderr, "error daemonizing: %s\n", strerror(errno));
  721. }
  722. }
  723. if (pidfp) {
  724. fprintf(pidfp, "%d\n", (int) getpid());
  725. fclose(pidfp);
  726. }
  727. #endif
  728. #if WIN
  729. winsock_init();
  730. #endif
  731. stream_init(log_stream, stream_buf_size);
  732. #if EMBEDDED
  733. output_init_embedded(log_output, output_device, output_buf_size, output_params, rates, rate_delay, idle);
  734. #else
  735. if (!strcmp(output_device, "-")) {
  736. output_init_stdout(log_output, output_buf_size, output_params, rates, rate_delay);
  737. } else {
  738. #if ALSA
  739. output_init_alsa(log_output, output_device, output_buf_size, output_params, rates, rate_delay, rt_priority, idle, mixer_device, output_mixer,
  740. output_mixer_unmute, linear_volume);
  741. #endif
  742. #if PORTAUDIO
  743. output_init_pa(log_output, output_device, output_buf_size, output_params, rates, rate_delay, idle);
  744. #endif
  745. }
  746. #endif
  747. #if DSD
  748. dsd_init(dsd_outfmt, dsd_delay);
  749. #endif
  750. #if VISEXPORT
  751. if (visexport) {
  752. output_vis_init(log_output, mac);
  753. }
  754. #endif
  755. decode_init(log_decode, include_codecs, exclude_codecs);
  756. #if RESAMPLE || RESAMPLE16
  757. if (resample) {
  758. process_init(resample);
  759. }
  760. #endif
  761. #if IR
  762. if (lircrc) {
  763. ir_init(log_ir, lircrc);
  764. }
  765. #endif
  766. if (name && namefile) {
  767. fprintf(stderr, "-n and -N option should not be used at same time\n");
  768. exit(1);
  769. }
  770. slimproto(log_slimproto, server, mac, name, namefile, modelname, maxSampleRate);
  771. decode_close();
  772. stream_close();
  773. #if EMBEDDED
  774. output_close_embedded();
  775. #else
  776. if (!strcmp(output_device, "-")) {
  777. output_close_stdout();
  778. } else {
  779. #if ALSA
  780. output_close_alsa();
  781. #endif
  782. #if PORTAUDIO
  783. output_close_pa();
  784. #endif
  785. }
  786. #endif
  787. #if IR
  788. ir_close();
  789. #endif
  790. #if WIN
  791. winsock_close();
  792. #endif
  793. #if LINUX || FREEBSD || SUN
  794. if (pidfile) {
  795. unlink(pidfile);
  796. free(pidfile);
  797. }
  798. #endif
  799. #if USE_SSL && !LINKALL && !NO_SSLSYM
  800. free_ssl_symbols();
  801. #endif
  802. exit(0);
  803. }