main.c 22 KB

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