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/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mptsd.c 4.5KB

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