main.c 22 KB

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