2
0

main.c 23 KB

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