main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. #ifndef EMBEDDED
  269. int main(int argc, char **argv) {
  270. #else
  271. int squeezelite_main(int argc, char **argv) {
  272. #endif
  273. char *server = NULL;
  274. char *output_device = "default";
  275. char *include_codecs = NULL;
  276. char *exclude_codecs = "";
  277. char *name = NULL;
  278. char *namefile = NULL;
  279. char *modelname = NULL;
  280. extern bool pcm_check_header;
  281. extern bool user_rates;
  282. char *logfile = NULL;
  283. u8_t mac[6];
  284. unsigned stream_buf_size = STREAMBUF_SIZE;
  285. unsigned output_buf_size = 0; // set later
  286. unsigned rates[MAX_SUPPORTED_SAMPLERATES] = { 0 };
  287. unsigned rate_delay = 0;
  288. char *resample = NULL;
  289. char *output_params = NULL;
  290. unsigned idle = 0;
  291. #if LINUX || FREEBSD || SUN
  292. bool daemonize = false;
  293. char *pidfile = NULL;
  294. FILE *pidfp = NULL;
  295. #endif
  296. #if ALSA
  297. unsigned rt_priority = OUTPUT_RT_PRIORITY;
  298. char *mixer_device = output_device;
  299. char *output_mixer = NULL;
  300. bool output_mixer_unmute = false;
  301. bool linear_volume = false;
  302. #endif
  303. #if DSD
  304. unsigned dsd_delay = 0;
  305. dsd_format dsd_outfmt = PCM;
  306. #endif
  307. #if VISEXPORT
  308. bool visexport = false;
  309. #endif
  310. #if IR
  311. char *lircrc = NULL;
  312. #endif
  313. #if EMBEDDED
  314. int err = embedded_init();
  315. if (err) return err;
  316. #endif
  317. log_level log_output = lWARN;
  318. log_level log_stream = lWARN;
  319. log_level log_decode = lWARN;
  320. log_level log_slimproto = lWARN;
  321. #if IR
  322. log_level log_ir = lWARN;
  323. #endif
  324. int maxSampleRate = 0;
  325. char *optarg = NULL;
  326. int optind = 1;
  327. int i;
  328. #define MAXCMDLINE 512
  329. char cmdline[MAXCMDLINE] = "";
  330. get_mac(mac);
  331. for (i = 0; i < argc && (strlen(argv[i]) + strlen(cmdline) + 2 < MAXCMDLINE); i++) {
  332. strcat(cmdline, argv[i]);
  333. strcat(cmdline, " ");
  334. }
  335. while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') {
  336. char *opt = argv[optind] + 1;
  337. if (strstr("oabcCdefmMnNpPrsZ"
  338. #if ALSA
  339. "UVO"
  340. #endif
  341. , opt) && optind < argc - 1) {
  342. optarg = argv[optind + 1];
  343. optind += 2;
  344. } else if (strstr("ltz?W"
  345. #if ALSA
  346. "LX"
  347. #endif
  348. #if RESAMPLE || RESAMPLE16
  349. "uR"
  350. #endif
  351. #if DSD
  352. "D"
  353. #endif
  354. #if VISEXPORT
  355. "v"
  356. #endif
  357. #if IR
  358. "i"
  359. #endif
  360. #if defined(GPIO) && defined(RPI)
  361. "G"
  362. #endif
  363. #if GPIO
  364. "S"
  365. #endif
  366. , opt)) {
  367. optarg = NULL;
  368. optind += 1;
  369. } else {
  370. LOG_ERROR("=> Option error: -%s", opt);
  371. usage(argv[0]);
  372. exit(1);
  373. }
  374. switch (opt[0]) {
  375. case 'o':
  376. output_device = optarg;
  377. #if ALSA
  378. mixer_device = optarg;
  379. #endif
  380. break;
  381. case 'a':
  382. output_params = optarg;
  383. break;
  384. case 'b':
  385. {
  386. char *s = next_param(optarg, ':');
  387. char *o = next_param(NULL, ':');
  388. if (s) stream_buf_size = atoi(s) * 1024;
  389. if (o) output_buf_size = atoi(o) * 1024;
  390. }
  391. break;
  392. case 'c':
  393. include_codecs = optarg;
  394. break;
  395. case 'C':
  396. if (atoi(optarg) > 0) {
  397. idle = atoi(optarg) * 1000;
  398. }
  399. break;
  400. case 'e':
  401. exclude_codecs = optarg;
  402. break;
  403. case 'd':
  404. {
  405. char *l = strtok(optarg, "=");
  406. char *v = strtok(NULL, "=");
  407. log_level new = lWARN;
  408. if (l && v) {
  409. if (!strcmp(v, "info")) new = lINFO;
  410. if (!strcmp(v, "debug")) new = lDEBUG;
  411. if (!strcmp(v, "sdebug")) new = lSDEBUG;
  412. if (!strcmp(l, "all") || !strcmp(l, "slimproto")) log_slimproto = new;
  413. if (!strcmp(l, "all") || !strcmp(l, "stream")) log_stream = new;
  414. if (!strcmp(l, "all") || !strcmp(l, "decode")) log_decode = new;
  415. if (!strcmp(l, "all") || !strcmp(l, "output")) log_output = new;
  416. #if IR
  417. if (!strcmp(l, "all") || !strcmp(l, "ir")) log_ir = new;
  418. #endif
  419. } else {
  420. LOG_ERROR("=> Debug settings error: -d %s", optarg);
  421. usage(argv[0]);
  422. exit(1);
  423. }
  424. }
  425. break;
  426. case 'f':
  427. logfile = optarg;
  428. break;
  429. case 'm':
  430. {
  431. int byte = 0;
  432. char *tmp;
  433. if (!strncmp(optarg, "00:04:20", 8)) {
  434. LOG_ERROR("=> ignoring mac address from hardware player range 00:04:20:**:**:**");
  435. } else {
  436. char *t = strtok(optarg, ":");
  437. while (t && byte < 6) {
  438. mac[byte++] = (u8_t)strtoul(t, &tmp, 16);
  439. t = strtok(NULL, ":");
  440. }
  441. }
  442. }
  443. break;
  444. case 'M':
  445. modelname = optarg;
  446. break;
  447. case 'r':
  448. {
  449. char *rstr = next_param(optarg, ':');
  450. char *dstr = next_param(NULL, ':');
  451. if (rstr && strstr(rstr, ",")) {
  452. // parse sample rates and sort them
  453. char *r = next_param(rstr, ',');
  454. unsigned tmp[MAX_SUPPORTED_SAMPLERATES] = { 0 };
  455. int i, j;
  456. int last = 999999;
  457. for (i = 0; r && i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  458. tmp[i] = atoi(r);
  459. r = next_param(NULL, ',');
  460. }
  461. for (i = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  462. int largest = 0;
  463. for (j = 0; j < MAX_SUPPORTED_SAMPLERATES; ++j) {
  464. if (tmp[j] > largest && tmp[j] < last) {
  465. largest = tmp[j];
  466. }
  467. }
  468. rates[i] = last = largest;
  469. }
  470. } else if (rstr) {
  471. // optstr is <min>-<max> or <max>, extract rates from test rates within this range
  472. unsigned ref[] TEST_RATES;
  473. char *str1 = next_param(rstr, '-');
  474. char *str2 = next_param(NULL, '-');
  475. unsigned max = str2 ? atoi(str2) : (str1 ? atoi(str1) : ref[0]);
  476. unsigned min = str1 && str2 ? atoi(str1) : 0;
  477. unsigned tmp;
  478. int i, j;
  479. if (max < min) { tmp = max; max = min; min = tmp; }
  480. rates[0] = max;
  481. for (i = 0, j = 1; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  482. if (ref[i] < rates[j-1] && ref[i] >= min) {
  483. rates[j++] = ref[i];
  484. }
  485. }
  486. }
  487. if (dstr) {
  488. rate_delay = atoi(dstr);
  489. }
  490. if (rates[0]) {
  491. user_rates = true;
  492. }
  493. }
  494. break;
  495. case 's':
  496. server = optarg;
  497. break;
  498. case 'n':
  499. name = optarg;
  500. break;
  501. case 'N':
  502. namefile = optarg;
  503. break;
  504. case 'Z':
  505. maxSampleRate = atoi(optarg);
  506. break;
  507. case 'W':
  508. pcm_check_header = true;
  509. break;
  510. #if ALSA
  511. case 'p':
  512. rt_priority = atoi(optarg);
  513. if (rt_priority > 99 || rt_priority < 1) {
  514. fprintf(stderr, "\nError: invalid priority: %s\n\n", optarg);
  515. usage(argv[0]);
  516. exit(1);
  517. }
  518. break;
  519. #endif
  520. #if LINUX || FREEBSD || SUN
  521. case 'P':
  522. pidfile = optarg;
  523. break;
  524. #endif
  525. #if !EMBEDDED
  526. case 'l':
  527. list_devices();
  528. exit(0);
  529. break;
  530. #endif
  531. #if RESAMPLE || RESAMPLE16
  532. case 'u':
  533. case 'R':
  534. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  535. resample = argv[optind++];
  536. } else {
  537. resample = "";
  538. }
  539. break;
  540. #endif
  541. #if DSD
  542. case 'D':
  543. dsd_outfmt = DOP;
  544. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  545. char *dstr = next_param(argv[optind++], ':');
  546. char *fstr = next_param(NULL, ':');
  547. dsd_delay = dstr ? atoi(dstr) : 0;
  548. if (fstr) {
  549. if (!strcmp(fstr, "dop")) dsd_outfmt = DOP;
  550. if (!strcmp(fstr, "u8")) dsd_outfmt = DSD_U8;
  551. if (!strcmp(fstr, "u16le")) dsd_outfmt = DSD_U16_LE;
  552. if (!strcmp(fstr, "u32le")) dsd_outfmt = DSD_U32_LE;
  553. if (!strcmp(fstr, "u16be")) dsd_outfmt = DSD_U16_BE;
  554. if (!strcmp(fstr, "u32be")) dsd_outfmt = DSD_U32_BE;
  555. if (!strcmp(fstr, "dop24")) dsd_outfmt = DOP_S24_LE;
  556. if (!strcmp(fstr, "dop24_3")) dsd_outfmt = DOP_S24_3LE;
  557. }
  558. }
  559. break;
  560. #endif
  561. #if VISEXPORT
  562. case 'v':
  563. visexport = true;
  564. break;
  565. #endif
  566. #if ALSA
  567. case 'O':
  568. mixer_device = optarg;
  569. break;
  570. case 'L':
  571. list_mixers(mixer_device);
  572. exit(0);
  573. break;
  574. case 'X':
  575. linear_volume = true;
  576. break;
  577. case 'U':
  578. output_mixer_unmute = true;
  579. case 'V':
  580. if (output_mixer) {
  581. fprintf(stderr, "-U and -V option should not be used at same time\n");
  582. exit(1);
  583. }
  584. output_mixer = optarg;
  585. break;
  586. #endif
  587. #if IR
  588. case 'i':
  589. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  590. lircrc = argv[optind++];
  591. } else {
  592. lircrc = "~/.lircrc"; // liblirc_client will expand ~/
  593. }
  594. break;
  595. #endif
  596. #if defined(GPIO) && defined(RPI)
  597. case 'G':
  598. if (power_script != NULL){
  599. fprintf(stderr, "-G and -S options cannot be used together \n\n" );
  600. usage(argv[0]);
  601. exit(1);
  602. }
  603. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  604. char *gp = next_param(argv[optind++], ':');
  605. char *go = next_param (NULL, ':');
  606. gpio_pin = atoi(gp);
  607. if (go != NULL){
  608. if ((strcmp(go, "H")==0)|(strcmp(go, "h")==0)){
  609. gpio_active_low=false;
  610. }else if((strcmp(go, "L")==0)|(strcmp(go, "l")==0)){
  611. gpio_active_low=true;
  612. }else{
  613. fprintf(stderr,"Must set output to be active High or Low i.e. -G18:H or -G18:L\n");
  614. usage(argv[0]);
  615. exit(1);
  616. }
  617. }else{
  618. fprintf(stderr,"-G Option Error\n");
  619. usage(argv[0]);
  620. exit(1);
  621. }
  622. gpio_active = true;
  623. relay(0);
  624. } else {
  625. fprintf(stderr, "Error in GPIO Pin assignment.\n");
  626. usage(argv[0]);
  627. exit(1);
  628. }
  629. break;
  630. #endif
  631. #if GPIO
  632. case 'S':
  633. if (gpio_active){
  634. fprintf(stderr, "-G and -S options cannot be used together \n\n" );
  635. usage(argv[0]);
  636. exit(1);
  637. }
  638. if (optind < argc && argv[optind] && argv[optind][0] != '-') {
  639. power_script = argv[optind++];
  640. if( access( power_script, R_OK|X_OK ) == -1 ) {
  641. // file doesn't exist
  642. fprintf(stderr, "Script %s, not found\n\n", argv[optind-1]);
  643. usage(argv[0]);
  644. exit(1);
  645. }
  646. } else {
  647. fprintf(stderr, "No Script Name Given.\n\n");
  648. usage(argv[0]);
  649. exit(1);
  650. }
  651. relay_script(0);
  652. break;
  653. #endif
  654. #if LINUX || FREEBSD || SUN
  655. case 'z':
  656. daemonize = true;
  657. #if SUN
  658. init_daemonize();
  659. #endif /* SUN */
  660. break;
  661. #endif
  662. case 't':
  663. license();
  664. exit(0);
  665. break;
  666. case '?':
  667. usage(argv[0]);
  668. exit(0);
  669. break;
  670. default:
  671. LOG_ERROR("=> arg error: %s", argv[optind]);
  672. break;
  673. }
  674. }
  675. // warn if command line includes something which isn't parsed
  676. if (optind < argc) {
  677. LOG_ERROR("=> command line argument error");
  678. usage(argv[0]);
  679. exit(1);
  680. }
  681. signal(SIGINT, sighandler);
  682. signal(SIGTERM, sighandler);
  683. #if defined(SIGQUIT)
  684. signal(SIGQUIT, sighandler);
  685. #endif
  686. #if defined(SIGHUP)
  687. signal(SIGHUP, sighandler);
  688. #endif
  689. #if USE_SSL && !LINKALL && !NO_SSLSYM
  690. ssl_loaded = load_ssl_symbols();
  691. #endif
  692. // set the output buffer size if not specified on the command line, take account of resampling
  693. if (!output_buf_size) {
  694. output_buf_size = OUTPUTBUF_SIZE;
  695. if (resample) {
  696. unsigned scale = 8;
  697. if (rates[0]) {
  698. scale = rates[0] / 44100;
  699. if (scale > 8) scale = 8;
  700. if (scale < 1) scale = 1;
  701. }
  702. output_buf_size *= scale;
  703. }
  704. }
  705. if (logfile) {
  706. if (!freopen(logfile, "a", stderr)) {
  707. fprintf(stderr, "error opening logfile %s: %s\n", logfile, strerror(errno));
  708. } else {
  709. if (log_output >= lINFO || log_stream >= lINFO || log_decode >= lINFO || log_slimproto >= lINFO) {
  710. fprintf(stderr, "\n%s\n", cmdline);
  711. }
  712. }
  713. }
  714. #if LINUX || FREEBSD || SUN
  715. if (pidfile) {
  716. if (!(pidfp = fopen(pidfile, "w")) ) {
  717. fprintf(stderr, "Error opening pidfile %s: %s\n", pidfile, strerror(errno));
  718. exit(1);
  719. }
  720. pidfile = realpath(pidfile, NULL); // daemonize will change cwd
  721. }
  722. if (daemonize) {
  723. if (daemon(0, logfile ? 1 : 0)) {
  724. fprintf(stderr, "error daemonizing: %s\n", strerror(errno));
  725. }
  726. }
  727. if (pidfp) {
  728. fprintf(pidfp, "%d\n", (int) getpid());
  729. fclose(pidfp);
  730. }
  731. #endif
  732. #if WIN
  733. winsock_init();
  734. #endif
  735. stream_init(log_stream, stream_buf_size);
  736. #if EMBEDDED
  737. output_init_embedded(log_output, output_device, output_buf_size, output_params, rates, rate_delay, idle);
  738. #else
  739. if (!strcmp(output_device, "-")) {
  740. output_init_stdout(log_output, output_buf_size, output_params, rates, rate_delay);
  741. } else {
  742. #if ALSA
  743. output_init_alsa(log_output, output_device, output_buf_size, output_params, rates, rate_delay, rt_priority, idle, mixer_device, output_mixer,
  744. output_mixer_unmute, linear_volume);
  745. #endif
  746. #if PORTAUDIO
  747. output_init_pa(log_output, output_device, output_buf_size, output_params, rates, rate_delay, idle);
  748. #endif
  749. }
  750. #endif
  751. #if DSD
  752. dsd_init(dsd_outfmt, dsd_delay);
  753. #endif
  754. #if VISEXPORT
  755. if (visexport) {
  756. output_vis_init(log_output, mac);
  757. }
  758. #endif
  759. decode_init(log_decode, include_codecs, exclude_codecs);
  760. #if RESAMPLE || RESAMPLE16
  761. if (resample) {
  762. process_init(resample);
  763. }
  764. #endif
  765. #if IR
  766. if (lircrc) {
  767. ir_init(log_ir, lircrc);
  768. }
  769. #endif
  770. if (name && namefile) {
  771. LOG_ERROR("=> -n and -N option should not be used at same time");
  772. exit(1);
  773. }
  774. slimproto(log_slimproto, server, mac, name, namefile, modelname, maxSampleRate);
  775. decode_close();
  776. stream_close();
  777. #if EMBEDDED
  778. output_close_embedded();
  779. #else
  780. if (!strcmp(output_device, "-")) {
  781. output_close_stdout();
  782. } else {
  783. #if ALSA
  784. output_close_alsa();
  785. #endif
  786. #if PORTAUDIO
  787. output_close_pa();
  788. #endif
  789. }
  790. #endif
  791. #if IR
  792. ir_close();
  793. #endif
  794. #if WIN
  795. winsock_close();
  796. #endif
  797. #if LINUX || FREEBSD || SUN
  798. if (pidfile) {
  799. unlink(pidfile);
  800. free(pidfile);
  801. }
  802. #endif
  803. #if USE_SSL && !LINKALL && !NO_SSLSYM
  804. free_ssl_symbols();
  805. #endif
  806. return(0);
  807. }