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/

output_psi.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * mptsd output PSI handling
  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 "libfuncs/log.h"
  22. #include "libfuncs/list.h"
  23. #include "libtsfuncs/tsfuncs.h"
  24. #include "config.h"
  25. #include "data.h"
  26. static void output_psi_init_pat(CONFIG *conf, OUTPUT *o) {
  27. LNODE *lc, *lctmp;
  28. o->pat = ts_pat_alloc_init(conf->transport_stream_id);
  29. list_lock(conf->channels);
  30. list_for_each(conf->channels, lc, lctmp) {
  31. CHANNEL *c = lc->data;
  32. ts_pat_add_program(o->pat, c->service_id, c->pmt_pid);
  33. }
  34. list_unlock(conf->channels);
  35. gettimeofday(&o->pat_ts, NULL);
  36. }
  37. static void output_psi_init_nit(CONFIG *conf, OUTPUT *o) {
  38. struct ts_nit *nit = ts_nit_alloc_init(conf->network_id);
  39. ts_nit_add_network_name_descriptor(nit, conf->network_name);
  40. if (conf->nit->items < 64) {
  41. int num;
  42. LNODE *lc, *lctmp;
  43. uint32_t *freqs = malloc(conf->nit->items * sizeof(uint32_t));
  44. uint32_t *services = malloc(conf->channels->items * sizeof(uint32_t));
  45. num = 0;
  46. list_lock(conf->nit);
  47. list_for_each(conf->nit, lc, lctmp) {
  48. NIT *ndata = lc->data;
  49. freqs[num++] = ndata->_freq;
  50. }
  51. ts_nit_add_frequency_list_descriptor_cable(nit, conf->transport_stream_id, conf->network_id, freqs, num);
  52. list_for_each(conf->nit, lc, lctmp) {
  53. NIT *ndata = lc->data;
  54. ts_nit_add_cable_delivery_descriptor(nit, ndata->ts_id, conf->network_id, ndata->_freq, ndata->_modulation, ndata->_symbol_rate);
  55. }
  56. list_unlock(conf->nit);
  57. num = 0;
  58. list_lock(conf->channels);
  59. list_for_each(conf->channels, lc, lctmp) {
  60. CHANNEL *c = lc->data;
  61. uint32_t srv = 0;
  62. srv = (c->service_id &~ 0x00ff) << 16;
  63. srv |= (c->service_id &~ 0xff00) << 8;
  64. srv |= c->radio ? 0x02 : 0x01;
  65. services[num++] = srv;
  66. }
  67. list_unlock(conf->channels);
  68. ts_nit_add_service_list_descriptor(nit, conf->transport_stream_id, conf->network_id, services, num);
  69. free(freqs);
  70. free(services);
  71. } else {
  72. LOG("CONF : Too much items in the NIT, maximum is 64! NIT not generated.\n");
  73. }
  74. gettimeofday(&o->nit_ts, NULL);
  75. o->nit = nit;
  76. }
  77. static void output_psi_init_sdt(CONFIG *conf, OUTPUT *o) {
  78. LNODE *lc, *lctmp;
  79. struct ts_sdt *sdt = ts_sdt_alloc_init(conf->network_id, conf->transport_stream_id);
  80. list_lock(conf->channels);
  81. list_for_each(conf->channels, lc, lctmp) {
  82. CHANNEL *c = lc->data;
  83. ts_sdt_add_service_descriptor(sdt, c->service_id, c->radio == 0, conf->provider_name, c->name);
  84. }
  85. list_unlock(conf->channels);
  86. gettimeofday(&o->sdt_ts, NULL);
  87. o->sdt = sdt;
  88. }
  89. static void output_psi_init_tdt_tot(CONFIG *conf, OUTPUT *o) {
  90. (void)conf; // Silence warning
  91. o->pid_tdt_cont = 15;
  92. o->tdt = ts_tdt_alloc_init(time(NULL));
  93. o->tot = ts_tot_alloc_init(time(NULL));
  94. gettimeofday(&o->tdt_ts, NULL);
  95. gettimeofday(&o->tot_ts, NULL);
  96. }
  97. static void output_add_pat(OUTPUT *o) {
  98. if (!o->pat->programs_num) {
  99. LOG("OUTPUT: Error no programs in PAT!\n");
  100. return;
  101. }
  102. int i;
  103. struct ts_pat *pat = o->pat;
  104. // LOGf("OUTPUT: Outputing PAT with %d programs\n", o->pat->programs_num);
  105. for (i=0;i<pat->section_header->num_packets;i++) {
  106. ts_packet_set_cont(pat->section_header->packet_data + (i * TS_PACKET_SIZE), i + o->pid_pat_cont);
  107. }
  108. pat->ts_header.continuity = o->pid_pat_cont;
  109. o->pid_pat_cont += pat->section_header->num_packets;
  110. cbuf_fill(o->psibuf, pat->section_header->packet_data, pat->section_header->num_packets * TS_PACKET_SIZE);
  111. // ts_pat_dump(o->pat);
  112. }
  113. void output_add_nit(OUTPUT *o) {
  114. if (!o || !o->nit)
  115. return;
  116. int i;
  117. struct ts_nit *nit = o->nit;
  118. // LOGf("OUTPUT: Outputing NIT\n");
  119. for (i=0;i<nit->section_header->num_packets;i++) {
  120. ts_packet_set_cont(nit->section_header->packet_data + (i * TS_PACKET_SIZE), i + o->pid_nit_cont);
  121. }
  122. nit->ts_header.continuity = o->pid_nit_cont;
  123. o->pid_nit_cont += nit->section_header->num_packets;
  124. cbuf_fill(o->psibuf, nit->section_header->packet_data, nit->section_header->num_packets * TS_PACKET_SIZE);
  125. // ts_nit_dump(nit);
  126. }
  127. void output_add_sdt(OUTPUT *o) {
  128. if (!o || !o->sdt)
  129. return;
  130. int i;
  131. struct ts_sdt *sdt = o->sdt;
  132. // LOGf("OUTPUT: Outputing SDT\n");
  133. for (i=0;i<sdt->section_header->num_packets;i++) {
  134. ts_packet_set_cont(sdt->section_header->packet_data + (i * TS_PACKET_SIZE), i + o->pid_sdt_cont);
  135. }
  136. sdt->ts_header.continuity = o->pid_sdt_cont;
  137. o->pid_sdt_cont += sdt->section_header->num_packets;
  138. cbuf_fill(o->psibuf, sdt->section_header->packet_data, sdt->section_header->num_packets * TS_PACKET_SIZE);
  139. // ts_sdt_dump(o->sdt);
  140. }
  141. static void output_add_pid0x14(OUTPUT *o, struct ts_tdt *tdt) {
  142. if (!o || !o->tdt)
  143. return;
  144. int i;
  145. // LOGf("OUTPUT: Outputing TDT\n");
  146. for (i=0;i<tdt->section_header->num_packets;i++) {
  147. ts_packet_set_cont(tdt->section_header->packet_data + (i * TS_PACKET_SIZE), i + o->pid_tdt_cont);
  148. }
  149. tdt->ts_header.continuity = o->pid_tdt_cont;
  150. o->pid_tdt_cont += tdt->section_header->num_packets;
  151. cbuf_fill(o->psibuf, tdt->section_header->packet_data, tdt->section_header->num_packets * TS_PACKET_SIZE);
  152. }
  153. static void output_add_tdt(OUTPUT *o) {
  154. // LOGf("OUTPUT: Outputing TDT\n");
  155. ts_tdt_set_time(o->tdt, time(NULL));
  156. output_add_pid0x14(o, o->tdt);
  157. // ts_tdt_dump(o->tdt);
  158. }
  159. static void output_add_tot(OUTPUT *o) {
  160. // LOGf("OUTPUT: Outputing TOT\n");
  161. ts_tot_set_localtime_offset_sofia(o->tot, time(NULL));
  162. output_add_pid0x14(o, o->tot);
  163. // ts_tdt_dump(o->tot);
  164. }
  165. static void __output_add_eit(OUTPUT *o, struct ts_eit *eit) {
  166. if (!eit)
  167. return;
  168. // LOGf("OUTPUT: Outputing EIT\n");
  169. int i, pcnt = o->pid_eit_cont;
  170. if (eit->section_header && eit->section_header->packet_data) {
  171. for (i=0;i<eit->section_header->num_packets;i++) {
  172. ts_packet_set_cont(eit->section_header->packet_data + (i * TS_PACKET_SIZE), i + pcnt);
  173. }
  174. eit->ts_header.continuity = pcnt;
  175. o->pid_eit_cont += eit->section_header->num_packets;
  176. cbuf_fill(o->psibuf, eit->section_header->packet_data, eit->section_header->num_packets * TS_PACKET_SIZE);
  177. }
  178. // ts_eit_dump(eit);
  179. }
  180. static void output_add_eit(CONFIG *conf, OUTPUT *o) {
  181. LNODE *lr, *lrtmp;
  182. config_load_epg(conf);
  183. list_for_each(conf->inputs, lr, lrtmp) {
  184. INPUT *r = lr->data;
  185. __output_add_eit(o, r->channel->eit_now);
  186. __output_add_eit(o, r->channel->eit_next);
  187. }
  188. }
  189. static void output_psi_add(CONFIG *conf, OUTPUT *o, struct timeval *now) {
  190. if (timeval_diff_msec(&o->pat_ts, now) >= conf->timeouts.pat) {
  191. o->pat_ts = *now;
  192. output_add_pat(o);
  193. }
  194. if (timeval_diff_msec(&o->nit_ts, now) >= conf->timeouts.nit) {
  195. o->nit_ts = *now;
  196. output_add_nit(o);
  197. }
  198. if (timeval_diff_msec(&o->sdt_ts, now) >= conf->timeouts.sdt) {
  199. o->sdt_ts = *now;
  200. output_add_sdt(o);
  201. }
  202. if (timeval_diff_msec(&o->tdt_ts, now) >= conf->timeouts.tdt) {
  203. o->tdt_ts = *now;
  204. output_add_tdt(o);
  205. }
  206. if (timeval_diff_msec(&o->tot_ts, now) >= conf->timeouts.tot) {
  207. o->tot_ts = *now;
  208. output_add_tot(o);
  209. }
  210. if (timeval_diff_msec(&o->eit_ts, now) >= conf->timeouts.eit) {
  211. o->eit_ts = *now;
  212. output_add_eit(conf, o);
  213. }
  214. }
  215. void output_psi_init(CONFIG *conf, OUTPUT *output) {
  216. output_psi_init_pat(conf, output);
  217. output_psi_init_nit(conf, output);
  218. output_psi_init_sdt(conf, output);
  219. output_psi_init_tdt_tot(conf, output);
  220. gettimeofday(&output->eit_ts, NULL);
  221. }
  222. void output_psi_free(OUTPUT *o) {
  223. ts_pat_free(&o->pat);
  224. ts_nit_free(&o->nit);
  225. ts_sdt_free(&o->sdt);
  226. ts_tdt_free(&o->tdt);
  227. ts_tdt_free(&o->tot);
  228. }
  229. void * output_handle_psi(void *_config) {
  230. CONFIG *conf = _config;
  231. OUTPUT *o = conf->output;
  232. struct timeval now;
  233. signal(SIGPIPE, SIG_IGN);
  234. while (!o->dienow) {
  235. gettimeofday(&now, NULL);
  236. output_psi_add(conf, o, &now);
  237. usleep(10000); // 10 ms
  238. }
  239. LOG("OUTPUT: PSI thread stopped.\n");
  240. o->dienow++;
  241. return 0;
  242. }