| 1 | /* |
| 2 | * ezstream - source client for Icecast with external en-/decoder support |
| 3 | * Copyright (C) 2003, 2004, 2005, 2006 Ed Zaleski <[email protected]> |
| 4 | * Copyright (C) 2007, 2009, 2015, 2017 Moritz Grimm <[email protected]> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #ifdef HAVE_CONFIG_H |
| 17 | # include "config.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "compat.h" |
| 21 | |
| 22 | #include "ezstream.h" |
| 23 | |
| 24 | #include <signal.h> |
| 25 | |
| 26 | #include "cfg.h" |
| 27 | #include "cmdline.h" |
| 28 | #include "log.h" |
| 29 | #include "mdata.h" |
| 30 | #include "playlist.h" |
| 31 | #include "stream.h" |
| 32 | #include "util.h" |
| 33 | #include "xalloc.h" |
| 34 | |
| 35 | #define STREAM_DONE 0 |
| 36 | #define STREAM_CONT 1 |
| 37 | #define STREAM_SKIP 2 |
| 38 | #define STREAM_SERVERR 3 |
| 39 | #define STREAM_UPDMDATA 4 |
| 40 | |
| 41 | stream_t main_stream; |
| 42 | playlist_t playlist; |
| 43 | int playlistMode; |
| 44 | unsigned int resource_errors; |
| 45 | |
| 46 | const int ezstream_signals[] = { |
| 47 | SIGTERM, SIGINT, SIGHUP, SIGUSR1, SIGUSR2 |
| 48 | }; |
| 49 | |
| 50 | volatile sig_atomic_t rereadPlaylist; |
| 51 | volatile sig_atomic_t rereadPlaylist_notify; |
| 52 | volatile sig_atomic_t skipTrack; |
| 53 | volatile sig_atomic_t queryMetadata; |
| 54 | volatile sig_atomic_t quit; |
| 55 | |
| 56 | void sig_handler(int); |
| 57 | |
| 58 | static char * _build_reencode_cmd(const char *, const char *, cfg_stream_t, |
| 59 | mdata_t); |
| 60 | static FILE * openResource(stream_t, const char *, int *, mdata_t *, |
| 61 | int *, long *); |
| 62 | int reconnect(stream_t); |
| 63 | const char * getTimeString(long); |
| 64 | int sendStream(stream_t, FILE *, const char *, int, const char *, |
| 65 | struct timespec *); |
| 66 | int streamFile(stream_t, const char *); |
| 67 | int streamPlaylist(stream_t); |
| 68 | int ez_shutdown(int); |
| 69 | |
| 70 | void |
| 71 | sig_handler(int sig) |
| 72 | { |
| 73 | switch (sig) { |
| 74 | case SIGTERM: |
| 75 | case SIGINT: |
| 76 | quit = 1; |
| 77 | break; |
| 78 | case SIGHUP: |
| 79 | rereadPlaylist = 1; |
| 80 | rereadPlaylist_notify = 1; |
| 81 | break; |
| 82 | case SIGUSR1: |
| 83 | skipTrack = 1; |
| 84 | break; |
| 85 | case SIGUSR2: |
| 86 | queryMetadata = 1; |
| 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static char * |
| 94 | _build_reencode_cmd(const char *extension, const char *filename, |
| 95 | cfg_stream_t cfg_stream, mdata_t md) |
| 96 | { |
| 97 | cfg_decoder_t decoder; |
| 98 | cfg_encoder_t encoder; |
| 99 | char *artist, *album, *title, *songinfo, *tmp; |
| 100 | char *filename_quoted; |
| 101 | char *custom_songinfo; |
| 102 | struct util_dict dicts[6]; |
| 103 | char *dec_str; |
| 104 | char *cmd_str; |
| 105 | size_t cmd_str_size; |
| 106 | |
| 107 | decoder = cfg_decoder_list_findext(cfg_get_decoders(), extension); |
| 108 | if (!decoder) { |
| 109 | log_error("cannot decode: %s: unsupported file extension %s", |
| 110 | filename, extension); |
| 111 | return (NULL); |
| 112 | } |
| 113 | encoder = cfg_encoder_list_find(cfg_get_encoders(), |
| 114 | cfg_stream_get_encoder(cfg_stream)); |
| 115 | if (!encoder) { |
| 116 | log_error("cannot encode: %s: unknown encoder", |
| 117 | cfg_stream_get_encoder(cfg_stream)); |
| 118 | return (NULL); |
| 119 | } |
| 120 | |
| 121 | tmp = util_utf82char(mdata_get_artist(md)); |
| 122 | artist = util_shellquote(tmp, 0); |
| 123 | xfree(tmp); |
| 124 | |
| 125 | tmp = util_utf82char(mdata_get_album(md)); |
| 126 | album = util_shellquote(tmp, 0); |
| 127 | xfree(tmp); |
| 128 | |
| 129 | tmp = util_utf82char(mdata_get_title(md)); |
| 130 | title = util_shellquote(tmp, 0); |
| 131 | xfree(tmp); |
| 132 | |
| 133 | tmp = util_utf82char(mdata_get_songinfo(md)); |
| 134 | songinfo = util_shellquote(tmp, 0); |
| 135 | xfree(tmp); |
| 136 | |
| 137 | filename_quoted = util_shellquote(filename, 0); |
| 138 | |
| 139 | /* |
| 140 | * if (prog && format) |
| 141 | * metatoformat |
| 142 | * else |
| 143 | * if (!prog && title) |
| 144 | * emptymeta |
| 145 | * else |
| 146 | * replacemeta |
| 147 | */ |
| 148 | if (cfg_get_metadata_program() && |
| 149 | cfg_get_metadata_format_str()) { |
| 150 | char buf[BUFSIZ]; |
| 151 | char *unquoted; |
| 152 | |
| 153 | mdata_strformat(md, buf, sizeof(buf), |
| 154 | cfg_get_metadata_format_str()); |
| 155 | unquoted = util_utf82char(buf); |
| 156 | custom_songinfo = util_shellquote(unquoted, 0); |
| 157 | xfree(unquoted); |
| 158 | } else { |
| 159 | if (!cfg_get_metadata_program() && |
| 160 | strstr(cfg_decoder_get_program(decoder), |
| 161 | PLACEHOLDER_TITLE) != NULL) { |
| 162 | custom_songinfo = xstrdup(""); |
| 163 | } else { |
| 164 | custom_songinfo = xstrdup(songinfo); |
| 165 | } |
| 166 | } |
| 167 | xfree(songinfo); |
| 168 | |
| 169 | memset(dicts, 0, sizeof(dicts)); |
| 170 | dicts[0].from = PLACEHOLDER_ARTIST; |
| 171 | dicts[0].to = artist; |
| 172 | dicts[1].from = PLACEHOLDER_ALBUM; |
| 173 | dicts[1].to = album; |
| 174 | dicts[2].from = PLACEHOLDER_TITLE; |
| 175 | dicts[2].to = title; |
| 176 | dicts[3].from = PLACEHOLDER_TRACK; |
| 177 | dicts[3].to = filename_quoted; |
| 178 | dicts[4].from = PLACEHOLDER_METADATA; |
| 179 | dicts[4].to = custom_songinfo; |
| 180 | |
| 181 | if (!cfg_get_metadata_program() && |
| 182 | strstr(cfg_encoder_get_program(encoder), |
| 183 | PLACEHOLDER_TITLE) != NULL) { |
| 184 | xfree(custom_songinfo); |
| 185 | dicts[4].to = custom_songinfo = xstrdup(""); |
| 186 | } |
| 187 | |
| 188 | dec_str = util_expand_words(cfg_decoder_get_program(decoder), dicts); |
| 189 | cmd_str_size = strlen(dec_str) + 1; |
| 190 | if (cfg_encoder_get_program(encoder)) { |
| 191 | char *enc_str; |
| 192 | |
| 193 | enc_str = util_expand_words(cfg_encoder_get_program(encoder), |
| 194 | dicts); |
| 195 | cmd_str_size += strlen(" | ") + strlen(enc_str); |
| 196 | cmd_str = xcalloc(cmd_str_size, sizeof(char)); |
| 197 | snprintf(cmd_str, cmd_str_size, "%s | %s", dec_str, enc_str); |
| 198 | xfree(enc_str); |
| 199 | xfree(dec_str); |
| 200 | } else { |
| 201 | cmd_str = xcalloc(cmd_str_size, sizeof(char)); |
| 202 | snprintf(cmd_str, cmd_str_size, "%s", dec_str); |
| 203 | xfree(dec_str); |
| 204 | } |
| 205 | |
| 206 | xfree(artist); |
| 207 | xfree(album); |
| 208 | xfree(title); |
| 209 | xfree(filename_quoted); |
| 210 | xfree(custom_songinfo); |
| 211 | |
| 212 | return (cmd_str); |
| 213 | } |
| 214 | |
| 215 | static FILE * |
| 216 | openResource(stream_t stream, const char *filename, int *popenFlag, |
| 217 | mdata_t *md_p, int *isStdin, long *songLen) |
| 218 | { |
| 219 | FILE *filep = NULL; |
| 220 | char extension[25]; |
| 221 | char *p = NULL; |
| 222 | char *pCommandString = NULL; |
| 223 | mdata_t md; |
| 224 | cfg_stream_t cfg_stream = stream_get_cfg_stream(stream); |
| 225 | |
| 226 | if (md_p != NULL) |
| 227 | *md_p = NULL; |
| 228 | if (songLen != NULL) |
| 229 | *songLen = 0; |
| 230 | |
| 231 | if ((isStdin && *isStdin) || |
| 232 | strcasecmp(filename, "stdin") == 0) { |
| 233 | if (cfg_get_metadata_program()) { |
| 234 | md = mdata_create(); |
| 235 | |
| 236 | if (0 > mdata_run_program(md, cfg_get_metadata_program()) || |
| 237 | 0 > stream_set_metadata(stream, md, NULL)) { |
| 238 | mdata_destroy(&md); |
| 239 | return (NULL); |
| 240 | } |
| 241 | |
| 242 | if (md_p != NULL) |
| 243 | *md_p = md; |
| 244 | else |
| 245 | mdata_destroy(&md); |
| 246 | } |
| 247 | |
| 248 | if (isStdin != NULL) |
| 249 | *isStdin = 1; |
| 250 | filep = stdin; |
| 251 | return (filep); |
| 252 | } |
| 253 | |
| 254 | if (isStdin != NULL) |
| 255 | *isStdin = 0; |
| 256 | |
| 257 | extension[0] = '\0'; |
| 258 | p = strrchr(filename, '.'); |
| 259 | if (p != NULL) |
| 260 | strlcpy(extension, p, sizeof(extension)); |
| 261 | for (p = extension; *p != '\0'; p++) |
| 262 | *p = (char)tolower((int)*p); |
| 263 | |
| 264 | if (strlen(extension) == 0) { |
| 265 | log_error("%s: cannot determine file type", filename); |
| 266 | return (filep); |
| 267 | } |
| 268 | |
| 269 | md = mdata_create(); |
| 270 | if (cfg_get_metadata_program()) { |
| 271 | if (0 > mdata_run_program(md, cfg_get_metadata_program())) |
| 272 | mdata_destroy(&md); |
| 273 | } else { |
| 274 | if (0 > mdata_parse_file(md, filename)) |
| 275 | mdata_destroy(&md); |
| 276 | } |
| 277 | if (NULL == md) |
| 278 | return (NULL); |
| 279 | if (songLen != NULL) |
| 280 | *songLen = mdata_get_length(md); |
| 281 | |
| 282 | *popenFlag = 0; |
| 283 | if (cfg_stream_get_encoder(cfg_stream)) { |
| 284 | int stderr_fd = -1; |
| 285 | |
| 286 | pCommandString = _build_reencode_cmd(extension, filename, |
| 287 | cfg_stream, md); |
| 288 | if (NULL == pCommandString) { |
| 289 | mdata_destroy(&md); |
| 290 | return (NULL); |
| 291 | } |
| 292 | if (md_p != NULL) |
| 293 | *md_p = md; |
| 294 | else |
| 295 | mdata_destroy(&md); |
| 296 | log_info("running command: %s", pCommandString); |
| 297 | |
| 298 | if (cfg_get_program_quiet_stderr()) { |
| 299 | int fd; |
| 300 | |
| 301 | stderr_fd = dup(fileno(stderr)); |
| 302 | if (0 > stderr_fd) { |
| 303 | log_alert("dup: %s", strerror(errno)); |
| 304 | exit(1); |
| 305 | } |
| 306 | if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) { |
| 307 | log_alert("%s: %s", _PATH_DEVNULL, |
| 308 | strerror(errno)); |
| 309 | exit(1); |
| 310 | } |
| 311 | |
| 312 | dup2(fd, fileno(stderr)); |
| 313 | close(fd); |
| 314 | } |
| 315 | |
| 316 | fflush(NULL); |
| 317 | errno = 0; |
| 318 | if ((filep = popen(pCommandString, "r")) == NULL) { |
| 319 | /* popen() does not set errno reliably ... */ |
| 320 | if (errno) |
| 321 | log_error("execution error: %s: %s", |
| 322 | pCommandString, strerror(errno)); |
| 323 | else |
| 324 | log_error("execution error: %s", |
| 325 | pCommandString); |
| 326 | } else { |
| 327 | *popenFlag = 1; |
| 328 | } |
| 329 | xfree(pCommandString); |
| 330 | |
| 331 | if (cfg_get_program_quiet_stderr()) |
| 332 | dup2(stderr_fd, fileno(stderr)); |
| 333 | |
| 334 | if (stderr_fd != -1) |
| 335 | close(stderr_fd); |
| 336 | |
| 337 | return (filep); |
| 338 | } |
| 339 | |
| 340 | if (md_p != NULL) |
| 341 | *md_p = md; |
| 342 | else |
| 343 | mdata_destroy(&md); |
| 344 | |
| 345 | if ((filep = fopen(filename, "rb")) == NULL) { |
| 346 | log_error("%s: %s", filename, strerror(errno)); |
| 347 | return (NULL); |
| 348 | } |
| 349 | |
| 350 | return (filep); |
| 351 | } |
| 352 | |
| 353 | int |
| 354 | reconnect(stream_t stream) |
| 355 | { |
| 356 | unsigned int i; |
| 357 | cfg_server_t cfg_server = stream_get_cfg_server(stream); |
| 358 | |
| 359 | i = 0; |
| 360 | while (++i) { |
| 361 | if (cfg_server_get_reconnect_attempts(cfg_server) > 0) |
| 362 | log_notice("reconnect: %s: attempt #%u/%u ...", |
| 363 | cfg_server_get_hostname(cfg_server), i, |
| 364 | cfg_server_get_reconnect_attempts(cfg_server)); |
| 365 | else |
| 366 | log_notice("reconnect: %s: attempt #%u ...", |
| 367 | cfg_server_get_hostname(cfg_server), i); |
| 368 | |
| 369 | stream_disconnect(stream); |
| 370 | if (0 == stream_connect(stream)) { |
| 371 | log_notice("reconnect: %s: success", |
| 372 | cfg_server_get_hostname(cfg_server)); |
| 373 | return (0); |
| 374 | } |
| 375 | |
| 376 | if (cfg_server_get_reconnect_attempts(cfg_server) > 0 && |
| 377 | i >= cfg_server_get_reconnect_attempts(cfg_server)) |
| 378 | break; |
| 379 | |
| 380 | if (quit) |
| 381 | return (-1); |
| 382 | else |
| 383 | sleep(5); |
| 384 | }; |
| 385 | |
| 386 | log_warning("reconnect failed: giving up"); |
| 387 | |
| 388 | return (-1); |
| 389 | } |
| 390 | |
| 391 | const char * |
| 392 | getTimeString(long seconds) |
| 393 | { |
| 394 | static char str[25]; |
| 395 | long secs, mins, hours; |
| 396 | |
| 397 | if (seconds < 0) |
| 398 | return (NULL); |
| 399 | |
| 400 | secs = seconds; |
| 401 | hours = secs / 3600; |
| 402 | secs %= 3600; |
| 403 | mins = secs / 60; |
| 404 | secs %= 60; |
| 405 | |
| 406 | snprintf(str, sizeof(str), "%ldh%02ldm%02lds", hours, mins, secs); |
| 407 | return ((const char *)str); |
| 408 | } |
| 409 | |
| 410 | int |
| 411 | sendStream(stream_t stream, FILE *filepstream, const char *fileName, |
| 412 | int isStdin, const char *songLenStr, struct timespec *tv) |
| 413 | { |
| 414 | char buff[4096]; |
| 415 | size_t bytes_read, total, oldTotal; |
| 416 | int ret; |
| 417 | double kbps = -1.0; |
| 418 | struct timespec timeStamp, *startTime = tv; |
| 419 | struct timespec callTime, currentTime; |
| 420 | cfg_server_t cfg_server = stream_get_cfg_server(stream); |
| 421 | cfg_intake_t cfg_intake = stream_get_cfg_intake(stream); |
| 422 | |
| 423 | clock_gettime(CLOCK_MONOTONIC, &callTime); |
| 424 | |
| 425 | timeStamp.tv_sec = startTime->tv_sec; |
| 426 | timeStamp.tv_nsec = startTime->tv_nsec; |
| 427 | |
| 428 | total = oldTotal = 0; |
| 429 | ret = STREAM_DONE; |
| 430 | while ((bytes_read = fread(buff, 1, sizeof(buff), filepstream)) > 0) { |
| 431 | if (!stream_get_connected(stream)) { |
| 432 | log_warning("%s: connection lost", |
| 433 | cfg_server_get_hostname(cfg_server)); |
| 434 | if (0 > reconnect(stream)) { |
| 435 | ret = STREAM_SERVERR; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | stream_sync(stream); |
| 441 | |
| 442 | if (0 > stream_send(stream, buff, bytes_read)) { |
| 443 | if (0 > reconnect(stream)) |
| 444 | ret = STREAM_SERVERR; |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | if (quit) |
| 449 | break; |
| 450 | if (rereadPlaylist_notify) { |
| 451 | rereadPlaylist_notify = 0; |
| 452 | if (CFG_INTAKE_PLAYLIST == cfg_intake_get_type(cfg_intake)) |
| 453 | log_notice("HUP signal received: playlist re-read scheduled"); |
| 454 | } |
| 455 | if (skipTrack) { |
| 456 | skipTrack = 0; |
| 457 | ret = STREAM_SKIP; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | clock_gettime(CLOCK_MONOTONIC, ¤tTime); |
| 462 | |
| 463 | if (queryMetadata || |
| 464 | (0 <= cfg_get_metadata_refresh_interval() && |
| 465 | (currentTime.tv_sec - callTime.tv_sec >= |
| 466 | cfg_get_metadata_refresh_interval()))) { |
| 467 | queryMetadata = 0; |
| 468 | if (cfg_get_metadata_program()) { |
| 469 | ret = STREAM_UPDMDATA; |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | total += bytes_read; |
| 475 | if (cfg_get_program_rtstatus_output()) { |
| 476 | double oldTime, newTime; |
| 477 | |
| 478 | if (!isStdin && playlistMode) { |
| 479 | if (CFG_INTAKE_PROGRAM == cfg_intake_get_type(cfg_intake)) { |
| 480 | char *tmp = xstrdup(cfg_intake_get_filename(cfg_intake)); |
| 481 | printf(" [%s]", basename(tmp)); |
| 482 | xfree(tmp); |
| 483 | } else |
| 484 | printf(" [%4lu/%-4lu]", |
| 485 | playlist_get_position(playlist), |
| 486 | playlist_get_num_items(playlist)); |
| 487 | } |
| 488 | |
| 489 | oldTime = (double)timeStamp.tv_sec |
| 490 | + (double)timeStamp.tv_nsec / 1000000000.0; |
| 491 | newTime = (double)currentTime.tv_sec |
| 492 | + (double)currentTime.tv_nsec / 1000000000.0; |
| 493 | if (songLenStr == NULL) |
| 494 | printf(" [ %s]", |
| 495 | getTimeString(currentTime.tv_sec - |
| 496 | startTime->tv_sec)); |
| 497 | else |
| 498 | printf(" [ %s/%s]", |
| 499 | getTimeString(currentTime.tv_sec - |
| 500 | startTime->tv_sec), |
| 501 | songLenStr); |
| 502 | if (newTime - oldTime >= 1.0) { |
| 503 | kbps = (((double)(total - oldTotal) |
| 504 | / (newTime - oldTime)) * 8.0) / 1000.0; |
| 505 | timeStamp.tv_sec = currentTime.tv_sec; |
| 506 | timeStamp.tv_nsec = currentTime.tv_nsec; |
| 507 | oldTotal = total; |
| 508 | } |
| 509 | if (kbps < 0) |
| 510 | printf(" "); |
| 511 | else |
| 512 | printf(" [%8.2f kbps]", kbps); |
| 513 | |
| 514 | printf(" \r"); |
| 515 | fflush(stdout); |
| 516 | } |
| 517 | } |
| 518 | if (ferror(filepstream)) { |
| 519 | if (errno == EINTR) { |
| 520 | clearerr(filepstream); |
| 521 | ret = STREAM_CONT; |
| 522 | } else if (errno == EBADF && isStdin) |
| 523 | log_notice("no (more) data available on standard input"); |
| 524 | else |
| 525 | log_error("sendStream: %s: %s", fileName, |
| 526 | strerror(errno)); |
| 527 | } |
| 528 | |
| 529 | return (ret); |
| 530 | } |
| 531 | |
| 532 | int |
| 533 | streamFile(stream_t stream, const char *fileName) |
| 534 | { |
| 535 | FILE *filepstream = NULL; |
| 536 | int popenFlag = 0; |
| 537 | char *songLenStr = NULL; |
| 538 | int ret, retval = 0; |
| 539 | long songLen; |
| 540 | mdata_t md = NULL; |
| 541 | struct timespec startTime; |
| 542 | cfg_stream_t cfg_stream = stream_get_cfg_stream(stream); |
| 543 | cfg_intake_t cfg_intake = stream_get_cfg_intake(stream); |
| 544 | int isStdin = cfg_intake_get_type(cfg_intake) == CFG_INTAKE_STDIN; |
| 545 | |
| 546 | if ((filepstream = openResource(stream, fileName, &popenFlag, &md, &isStdin, &songLen)) |
| 547 | == NULL) { |
| 548 | mdata_destroy(&md); |
| 549 | if (++resource_errors > 100) { |
| 550 | log_error("too many errors; giving up"); |
| 551 | return (0); |
| 552 | } |
| 553 | /* Continue with next resource on failure: */ |
| 554 | return (1); |
| 555 | } |
| 556 | resource_errors = 0; |
| 557 | |
| 558 | if (md != NULL) { |
| 559 | const char *tmp; |
| 560 | char *metaData; |
| 561 | |
| 562 | tmp = mdata_get_songinfo(md) ? |
| 563 | mdata_get_songinfo(md) : mdata_get_name(md); |
| 564 | metaData = util_utf82char(tmp); |
| 565 | log_notice("streaming: %s (%s)", metaData, |
| 566 | isStdin ? "stdin" : fileName); |
| 567 | xfree(metaData); |
| 568 | |
| 569 | /* MP3 streams are special, so set the metadata explicitly: */ |
| 570 | if (CFG_STREAM_MP3 == cfg_stream_get_format(cfg_stream)) |
| 571 | stream_set_metadata(stream, md, NULL); |
| 572 | |
| 573 | mdata_destroy(&md); |
| 574 | } else if (isStdin) |
| 575 | log_notice("streaming: standard input"); |
| 576 | |
| 577 | if (songLen > 0) |
| 578 | songLenStr = xstrdup(getTimeString(songLen)); |
| 579 | clock_gettime(CLOCK_MONOTONIC, &startTime); |
| 580 | do { |
| 581 | ret = sendStream(stream, filepstream, fileName, isStdin, |
| 582 | songLenStr, &startTime); |
| 583 | if (quit) |
| 584 | break; |
| 585 | if (ret != STREAM_DONE) { |
| 586 | if ((skipTrack && rereadPlaylist) || |
| 587 | (skipTrack && queryMetadata)) { |
| 588 | skipTrack = 0; |
| 589 | ret = STREAM_CONT; |
| 590 | } |
| 591 | if (queryMetadata && rereadPlaylist) { |
| 592 | queryMetadata = 0; |
| 593 | ret = STREAM_CONT; |
| 594 | } |
| 595 | if (ret == STREAM_SKIP || skipTrack) { |
| 596 | skipTrack = 0; |
| 597 | if (!isStdin) |
| 598 | log_notice("USR1 signal received: skipping current track"); |
| 599 | retval = 1; |
| 600 | ret = STREAM_DONE; |
| 601 | } |
| 602 | if (ret == STREAM_UPDMDATA || queryMetadata) { |
| 603 | queryMetadata = 0; |
| 604 | if (cfg_get_metadata_no_updates()) |
| 605 | continue; |
| 606 | if (cfg_get_metadata_program()) { |
| 607 | char *mdataStr = NULL; |
| 608 | |
| 609 | log_info("running metadata program: %s", |
| 610 | cfg_get_metadata_program()); |
| 611 | md = mdata_create(); |
| 612 | if (0 > mdata_run_program(md, cfg_get_metadata_program()) || |
| 613 | 0 > stream_set_metadata(stream, md, &mdataStr)) { |
| 614 | mdata_destroy(&md); |
| 615 | retval = 0; |
| 616 | ret = STREAM_DONE; |
| 617 | continue; |
| 618 | } |
| 619 | mdata_destroy(&md); |
| 620 | log_info("new metadata: %s", mdataStr); |
| 621 | xfree(mdataStr); |
| 622 | } |
| 623 | } |
| 624 | if (ret == STREAM_SERVERR) { |
| 625 | retval = 0; |
| 626 | ret = STREAM_DONE; |
| 627 | } |
| 628 | } else |
| 629 | retval = 1; |
| 630 | } while (ret != STREAM_DONE); |
| 631 | |
| 632 | if (popenFlag) |
| 633 | pclose(filepstream); |
| 634 | else if (!isStdin) |
| 635 | fclose(filepstream); |
| 636 | |
| 637 | if (songLenStr != NULL) |
| 638 | xfree(songLenStr); |
| 639 | |
| 640 | return (retval); |
| 641 | } |
| 642 | |
| 643 | int |
| 644 | streamPlaylist(stream_t stream) |
| 645 | { |
| 646 | const char *song; |
| 647 | char lastSong[PATH_MAX]; |
| 648 | cfg_intake_t cfg_intake = stream_get_cfg_intake(stream); |
| 649 | |
| 650 | if (playlist == NULL) { |
| 651 | switch (cfg_intake_get_type(cfg_intake)) { |
| 652 | case CFG_INTAKE_PROGRAM: |
| 653 | if ((playlist = playlist_program(cfg_intake_get_filename(cfg_intake))) == NULL) |
| 654 | return (0); |
| 655 | break; |
| 656 | case CFG_INTAKE_STDIN: |
| 657 | if ((playlist = playlist_read(NULL)) == NULL) |
| 658 | return (0); |
| 659 | break; |
| 660 | default: |
| 661 | if ((playlist = playlist_read(cfg_intake_get_filename(cfg_intake))) == NULL) |
| 662 | return (0); |
| 663 | if (playlist_get_num_items(playlist) == 0) |
| 664 | log_warning("%s: playlist empty", |
| 665 | cfg_intake_get_filename(cfg_intake)); |
| 666 | break; |
| 667 | } |
| 668 | } else { |
| 669 | /* |
| 670 | * XXX: This preserves traditional behavior, however, |
| 671 | * rereading the playlist after each walkthrough seems a |
| 672 | * bit more logical. |
| 673 | */ |
| 674 | playlist_rewind(playlist); |
| 675 | } |
| 676 | |
| 677 | if (CFG_INTAKE_PROGRAM != cfg_intake_get_type(cfg_intake) && |
| 678 | cfg_intake_get_shuffle(cfg_intake)) |
| 679 | playlist_shuffle(playlist); |
| 680 | |
| 681 | while ((song = playlist_get_next(playlist)) != NULL) { |
| 682 | strlcpy(lastSong, song, sizeof(lastSong)); |
| 683 | if (!streamFile(stream, song)) |
| 684 | return (0); |
| 685 | if (quit) |
| 686 | break; |
| 687 | if (rereadPlaylist) { |
| 688 | rereadPlaylist = rereadPlaylist_notify = 0; |
| 689 | if (CFG_INTAKE_PROGRAM == cfg_intake_get_type(cfg_intake)) |
| 690 | continue; |
| 691 | log_notice("rereading playlist"); |
| 692 | if (!playlist_reread(&playlist)) |
| 693 | return (0); |
| 694 | if (cfg_intake_get_shuffle(cfg_intake)) |
| 695 | playlist_shuffle(playlist); |
| 696 | else { |
| 697 | playlist_goto_entry(playlist, lastSong); |
| 698 | playlist_skip_next(playlist); |
| 699 | } |
| 700 | continue; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return (1); |
| 705 | } |
| 706 | |
| 707 | int |
| 708 | ez_shutdown(int exitval) |
| 709 | { |
| 710 | if (main_stream) |
| 711 | stream_destroy(&main_stream); |
| 712 | |
| 713 | stream_exit(); |
| 714 | playlist_exit(); |
| 715 | log_exit(); |
| 716 | cfg_exit(); |
| 717 | |
| 718 | return (exitval); |
| 719 | } |
| 720 | |
| 721 | int |
| 722 | main(int argc, char *argv[]) |
| 723 | { |
| 724 | int ret, cont; |
| 725 | const char *errstr; |
| 726 | extern char *optarg; |
| 727 | extern int optind; |
| 728 | struct sigaction act; |
| 729 | unsigned int i; |
| 730 | cfg_server_t cfg_server; |
| 731 | cfg_stream_t cfg_stream; |
| 732 | cfg_intake_t cfg_intake; |
| 733 | |
| 734 | ret = 1; |
| 735 | if (0 > cfg_init() || |
| 736 | 0 > cmdline_parse(argc, argv, &ret) || |
| 737 | 0 > log_init(cfg_get_program_name()) || |
| 738 | 0 > playlist_init() || |
| 739 | 0 > cfg_file_reload() || |
| 740 | 0 > stream_init()) |
| 741 | return (ez_shutdown(ret)); |
| 742 | |
| 743 | log_set_verbosity(cfg_get_program_verbosity()); |
| 744 | |
| 745 | if (0 > cfg_check(&errstr)) { |
| 746 | log_error("%s: %s", cfg_get_program_config_file(), errstr); |
| 747 | return (ez_shutdown(2)); |
| 748 | } |
| 749 | |
| 750 | main_stream = stream_create(CFG_DEFAULT); |
| 751 | if (0 > stream_configure(main_stream)) { |
| 752 | stream_destroy(&main_stream); |
| 753 | return (ez_shutdown(1)); |
| 754 | } |
| 755 | cfg_server = stream_get_cfg_server(main_stream); |
| 756 | cfg_stream = stream_get_cfg_stream(main_stream); |
| 757 | cfg_intake = stream_get_cfg_intake(main_stream); |
| 758 | |
| 759 | memset(&act, 0, sizeof(act)); |
| 760 | act.sa_handler = sig_handler; |
| 761 | #ifdef SA_RESTART |
| 762 | act.sa_flags = SA_RESTART; |
| 763 | #endif |
| 764 | for (i = 0; i < sizeof(ezstream_signals) / sizeof(int); i++) { |
| 765 | if (sigaction(ezstream_signals[i], &act, NULL) == -1) { |
| 766 | log_syserr(ERROR, errno, "sigaction"); |
| 767 | stream_destroy(&main_stream); |
| 768 | return (ez_shutdown(1)); |
| 769 | } |
| 770 | } |
| 771 | /* |
| 772 | * Ignore SIGPIPE, which has been seen to give a long-running ezstream |
| 773 | * process trouble. EOF and/or EPIPE are also easier to handle. |
| 774 | */ |
| 775 | #ifndef SIG_IGN |
| 776 | # define SIG_IGN (void (*)(int))1 |
| 777 | #endif /* !SIG_IGN */ |
| 778 | act.sa_handler = SIG_IGN; |
| 779 | if (sigaction(SIGPIPE, &act, NULL) == -1) { |
| 780 | log_syserr(ERROR, errno, "sigaction"); |
| 781 | stream_destroy(&main_stream); |
| 782 | return (ez_shutdown(1)); |
| 783 | } |
| 784 | |
| 785 | if (0 > util_write_pid_file(cfg_get_program_pid_file())) |
| 786 | log_syserr(WARNING, errno, cfg_get_program_pid_file()); |
| 787 | |
| 788 | if (0 > stream_connect(main_stream)) { |
| 789 | log_error("initial server connection failed"); |
| 790 | stream_destroy(&main_stream); |
| 791 | return (ez_shutdown(1)); |
| 792 | } |
| 793 | log_notice("connected: %s://%s:%u%s", |
| 794 | cfg_server_get_protocol_str(cfg_server), |
| 795 | cfg_server_get_hostname(cfg_server), |
| 796 | cfg_server_get_port(cfg_server), |
| 797 | cfg_stream_get_mountpoint(cfg_stream)); |
| 798 | |
| 799 | if (CFG_INTAKE_PROGRAM == cfg_intake_get_type(cfg_intake) || |
| 800 | CFG_INTAKE_PLAYLIST == cfg_intake_get_type(cfg_intake) || |
| 801 | (CFG_INTAKE_AUTODETECT == cfg_intake_get_type(cfg_intake) && |
| 802 | (util_strrcasecmp(cfg_intake_get_filename(cfg_intake), ".m3u") == 0 || |
| 803 | util_strrcasecmp(cfg_intake_get_filename(cfg_intake), ".txt") == 0))) |
| 804 | playlistMode = 1; |
| 805 | else |
| 806 | playlistMode = 0; |
| 807 | |
| 808 | do { |
| 809 | if (playlistMode) { |
| 810 | cont = streamPlaylist(main_stream); |
| 811 | } else { |
| 812 | cont = streamFile(main_stream, |
| 813 | cfg_intake_get_filename(cfg_intake)); |
| 814 | } |
| 815 | if (quit) |
| 816 | break; |
| 817 | if (cfg_intake_get_stream_once(cfg_intake)) |
| 818 | break; |
| 819 | } while (cont); |
| 820 | |
| 821 | stream_disconnect(main_stream); |
| 822 | stream_destroy(&main_stream); |
| 823 | |
| 824 | if (quit) { |
| 825 | if (cfg_get_program_quiet_stderr() && |
| 826 | cfg_get_program_verbosity()) |
| 827 | printf("\r"); |
| 828 | log_notice("INT or TERM signal received"); |
| 829 | } |
| 830 | |
| 831 | log_info("exiting"); |
| 832 | |
| 833 | playlist_free(&playlist); |
| 834 | |
| 835 | return (ez_shutdown(0)); |
| 836 | } |