main.c 22 KB

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