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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifdef BUILD_ID
  33. const char *program_id = PROGRAM_NAME " " GIT_VER " build " BUILD_ID;
  34. #else
  35. const char *program_id = PROGRAM_NAME " " GIT_VER;
  36. #endif
  37. char *server_sig = PROGRAM_NAME;
  38. char *server_ver = GIT_VER;
  39. char *copyright = "Copyright (C) 2010-2011 Unix Solutions Ltd.";
  40. CONFIG *config;
  41. int keep_going = 1;
  42. int rcvsig = 0;
  43. void spawn_input_threads(CONFIG *conf) {
  44. LNODE *lc, *lctmp;
  45. LNODE *lr, *lrtmp;
  46. int spawned = 0;
  47. list_for_each(conf->channels, lc, lctmp) {
  48. CHANNEL *c = lc->data;
  49. int restreamer_active = 0;
  50. list_lock(conf->inputs);
  51. list_for_each(conf->inputs, lr, lrtmp) {
  52. INPUT *r = lr->data;
  53. if (xstrcmp(r->name, c->name)==0) {
  54. restreamer_active = 1;
  55. break;
  56. }
  57. }
  58. list_unlock(conf->inputs);
  59. if (!restreamer_active) {
  60. INPUT *nr = input_new(c->name, c);
  61. if (nr) {
  62. list_add(conf->inputs, nr);
  63. // LOGf("SPAWN : %s thread.\n", c->name);
  64. if (pthread_create(&nr->thread, NULL, &input_stream, nr) == 0) {
  65. spawned++;
  66. pthread_detach(nr->thread);
  67. } else {
  68. LOGf("ERROR: Can't create proxy for %s\n", c->name);
  69. }
  70. } else {
  71. LOGf("ERROR: Error creating proxy for %s\n", c->name);
  72. }
  73. }
  74. }
  75. LOGf("INPUT : %d thread%s spawned.\n", spawned, spawned > 1 ? "s" : "");
  76. }
  77. void spawn_output_threads(CONFIG *conf) {
  78. if (pthread_create(&conf->output->psi_thread, NULL, &output_handle_psi, conf) == 0) {
  79. pthread_detach(conf->output->psi_thread);
  80. } else {
  81. LOGf("ERROR: Can't spawn PSI output thread: %s\n", strerror(errno));
  82. exit(1);
  83. }
  84. if (pthread_create(&conf->output->mix_thread, NULL, &output_handle_mix, conf) == 0) {
  85. pthread_detach(conf->output->mix_thread);
  86. } else {
  87. LOGf("ERROR: Can't spawn MIX output thread: %s\n", strerror(errno));
  88. exit(1);
  89. }
  90. if (pthread_create(&conf->output->write_thread, NULL, &output_handle_write, conf) == 0) {
  91. pthread_detach(conf->output->write_thread);
  92. } else {
  93. LOGf("ERROR: Can't spawn WRITE output thread: %s\n", strerror(errno));
  94. exit(1);
  95. }
  96. }
  97. void kill_threads(CONFIG *conf) {
  98. int loops = 0;
  99. conf->output->dienow = 1;
  100. while (conf->inputs->items || conf->output->dienow < 4) {
  101. usleep(50000);
  102. if (loops++ > 60) // 3 seconds
  103. exit(0);
  104. }
  105. }
  106. /*
  107. void do_reconnect(CONFIG *conf) {
  108. LNODE *l, *tmp;
  109. list_lock(conf->inputs);
  110. list_for_each(conf->inputs, l, tmp) {
  111. INPUT *r = l->data;
  112. r->reconnect = 1;
  113. }
  114. list_unlock(conf->inputs);
  115. }
  116. void do_reconf(CONFIG *conf) {
  117. // load_channels_config();
  118. spawn_input_threads(conf);
  119. }
  120. */
  121. void signal_quit(int sig) {
  122. rcvsig = sig;
  123. keep_going = 0;
  124. }
  125. void init_signals(void) {
  126. signal(SIGCHLD, SIG_IGN);
  127. signal(SIGPIPE, SIG_IGN);
  128. // signal(SIGHUP , do_reconf);
  129. // signal(SIGUSR1, do_reconnect);
  130. signal(SIGINT , signal_quit);
  131. signal(SIGTERM, signal_quit);
  132. }
  133. int main(int argc, char **argv) {
  134. set_http_response_server_ident(server_sig, server_ver);
  135. ts_set_log_func(LOG);
  136. config = config_alloc();
  137. config_load(config, argc, argv);
  138. output_psi_init(config, config->output);
  139. daemonize(config->pidfile);
  140. web_server_start(config);
  141. log_init(config->logident, config->syslog_active, config->pidfile == NULL, config->loghost, config->logport);
  142. init_signals();
  143. LOGf("INIT : %s %s (%s)\n" , server_sig, server_ver, config->ident);
  144. connect_output(config->output);
  145. spawn_input_threads(config);
  146. spawn_output_threads(config);
  147. do { usleep(50000); } while(keep_going);
  148. kill_threads(config);
  149. web_server_stop(config);
  150. LOGf("SHUTDOWN: Signal %d | %s %s (%s)\n", rcvsig, server_sig, server_ver, config->ident);
  151. config_free(&config);
  152. log_close();
  153. exit(0);
  154. }