mptsd reads mpegts streams from udp/multicast or http and combines them into one multiple program stream that is suitable for outputting to DVB-C modulator. Tested with Dektec DTE-3114 Quad QAM Modulator and used in production in small DVB-C networks. https://georgi.unixsol.org/programs/mptsd/

mptsd.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * mptsd main
  3. * Copyright (C) 2010-2011 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <signal.h>
  21. #include <errno.h>
  22. #include "libfuncs/libfuncs.h"
  23. #include "libtsfuncs/tsfuncs.h"
  24. #include "iniparser.h"
  25. #include "data.h"
  26. #include "config.h"
  27. #include "network.h"
  28. #include "input.h"
  29. #include "output.h"
  30. #include "web_server.h"
  31. #define PROGRAM_NAME "ux-mptsd"
  32. const char *program_id = PROGRAM_NAME " " GIT_VER " build " BUILD_ID;
  33. char *server_sig = PROGRAM_NAME;
  34. char *server_ver = GIT_VER;
  35. char *copyright = "Copyright (C) 2010-2011 Unix Solutions Ltd.";
  36. CONFIG *config;
  37. int keep_going = 1;
  38. int rcvsig = 0;
  39. void spawn_input_threads(CONFIG *conf) {
  40. LNODE *lc, *lctmp;
  41. LNODE *lr, *lrtmp;
  42. int spawned = 0;
  43. list_for_each(conf->channels, lc, lctmp) {
  44. CHANNEL *c = lc->data;
  45. int restreamer_active = 0;
  46. list_lock(conf->inputs);
  47. list_for_each(conf->inputs, lr, lrtmp) {
  48. INPUT *r = lr->data;
  49. if (xstrcmp(r->name, c->name)==0) {
  50. restreamer_active = 1;
  51. break;
  52. }
  53. }
  54. list_unlock(conf->inputs);
  55. if (!restreamer_active) {
  56. INPUT *nr = input_new(c->name, c);
  57. if (nr) {
  58. list_add(conf->inputs, nr);
  59. // LOGf("SPAWN : %s thread.\n", c->name);
  60. if (pthread_create(&nr->thread, NULL, &input_stream, nr) == 0) {
  61. spawned++;
  62. pthread_detach(nr->thread);
  63. } else {
  64. LOGf("ERROR: Can't create proxy for %s\n", c->name);
  65. }
  66. } else {
  67. LOGf("ERROR: Error creating proxy for %s\n", c->name);
  68. }
  69. }
  70. }
  71. LOGf("INPUT : %d thread%s spawned.\n", spawned, spawned > 1 ? "s" : "");
  72. }
  73. void spawn_output_threads(CONFIG *conf) {
  74. if (pthread_create(&conf->output->psi_thread, NULL, &output_handle_psi, conf) == 0) {
  75. pthread_detach(conf->output->psi_thread);
  76. } else {
  77. LOGf("ERROR: Can't spawn PSI output thread: %s\n", strerror(errno));
  78. exit(1);
  79. }
  80. if (pthread_create(&conf->output->mix_thread, NULL, &output_handle_mix, conf) == 0) {
  81. pthread_detach(conf->output->mix_thread);
  82. } else {
  83. LOGf("ERROR: Can't spawn MIX output thread: %s\n", strerror(errno));
  84. exit(1);
  85. }
  86. if (pthread_create(&conf->output->write_thread, NULL, &output_handle_write, conf) == 0) {
  87. pthread_detach(conf->output->write_thread);
  88. } else {
  89. LOGf("ERROR: Can't spawn WRITE output thread: %s\n", strerror(errno));
  90. exit(1);
  91. }
  92. }
  93. void kill_threads(CONFIG *conf) {
  94. int loops = 0;
  95. conf->output->dienow = 1;
  96. while (conf->inputs->items || conf->output->dienow < 4) {
  97. usleep(50000);
  98. if (loops++ > 60) // 3 seconds
  99. exit(0);
  100. }
  101. }
  102. /*
  103. void do_reconnect(CONFIG *conf) {
  104. LNODE *l, *tmp;
  105. list_lock(conf->inputs);
  106. list_for_each(conf->inputs, l, tmp) {
  107. INPUT *r = l->data;
  108. r->reconnect = 1;
  109. }
  110. list_unlock(conf->inputs);
  111. }
  112. void do_reconf(CONFIG *conf) {
  113. // load_channels_config();
  114. spawn_input_threads(conf);
  115. }
  116. */
  117. void signal_quit(int sig) {
  118. rcvsig = sig;
  119. keep_going = 0;
  120. }
  121. void init_signals() {
  122. signal(SIGCHLD, SIG_IGN);
  123. signal(SIGPIPE, SIG_IGN);
  124. // signal(SIGHUP , do_reconf);
  125. // signal(SIGUSR1, do_reconnect);
  126. signal(SIGINT , signal_quit);
  127. signal(SIGTERM, signal_quit);
  128. }
  129. int main(int argc, char **argv) {
  130. set_http_response_server_ident(server_sig, server_ver);
  131. ts_set_log_func(LOG);
  132. config = config_alloc();
  133. config_load(config, argc, argv);
  134. output_psi_init(config, config->output);
  135. daemonize(config->pidfile);
  136. web_server_start(config);
  137. log_init(config->logident, config->syslog_active, config->pidfile == NULL, config->loghost, config->logport);
  138. init_signals(config);
  139. LOGf("INIT : %s %s (%s)\n" , server_sig, server_ver, config->ident);
  140. connect_output(config->output);
  141. spawn_input_threads(config);
  142. spawn_output_threads(config);
  143. do { usleep(50000); } while(keep_going);
  144. kill_threads(config);
  145. web_server_stop(config);
  146. LOGf("SHUTDOWN: Signal %d | %s %s (%s)\n", rcvsig, server_sig, server_ver, config->ident);
  147. config_free(&config);
  148. log_close();
  149. exit(0);
  150. }