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.

output_psi.c 8.6KB

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