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/

data.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * mptsd data
  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 <math.h>
  20. #include <ctype.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <sys/time.h>
  24. #include <regex.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include "libfuncs/io.h"
  29. #include "libfuncs/log.h"
  30. #include "libfuncs/list.h"
  31. #include "libfuncs/asyncdns.h"
  32. #include "libtsfuncs/tsfuncs.h"
  33. #include "data.h"
  34. #include "config.h"
  35. #include "output.h"
  36. extern CONFIG *config;
  37. channel_source get_sproto(char *url) {
  38. return strncmp(url, "http", 4)==0 ? tcp_sock : udp_sock;
  39. }
  40. CHANSRC *chansrc_init(char *url) {
  41. if (!url)
  42. return NULL;
  43. regex_t re;
  44. regmatch_t res[5];
  45. regcomp(&re, "^([a-z]+)://([^:/?]+):?([0-9]*)/?(.*)", REG_EXTENDED);
  46. if (regexec(&re,url,5,res,0)==0) {
  47. char *data = strdup(url);
  48. char *proto, *host, *port, *path;
  49. int iport;
  50. proto= data+res[1].rm_so; data[res[1].rm_eo]=0;
  51. host = data+res[2].rm_so; data[res[2].rm_eo]=0;
  52. port = data+res[3].rm_so; data[res[3].rm_eo]=0;
  53. path = data+res[4].rm_so; data[res[4].rm_eo]=0;
  54. iport = atoi(port);
  55. /* Setup */
  56. CHANSRC *src = calloc(1, sizeof(CHANSRC));
  57. src->proto = strdup(proto);
  58. src->sproto= get_sproto(url);
  59. src->host = strdup(host);
  60. src->port = iport ? iport : 80;
  61. src->path = strdup(path);
  62. FREE(data);
  63. regfree(&re);
  64. return src;
  65. }
  66. regfree(&re);
  67. return NULL;
  68. }
  69. void chansrc_free(CHANSRC **purl) {
  70. CHANSRC *url = *purl;
  71. if (url) {
  72. FREE(url->proto);
  73. FREE(url->host);
  74. FREE(url->path);
  75. FREE(*purl);
  76. }
  77. };
  78. void chansrc_add(CHANNEL *c, char *src) {
  79. if (c->num_src >= MAX_CHANNEL_SOURCES-1)
  80. return;
  81. c->sources[c->num_src] = strdup(src);
  82. if (c->num_src == 0) /* Set default source to first one */
  83. c->source = c->sources[c->num_src];
  84. c->num_src++;
  85. }
  86. void chansrc_next(CHANNEL *c) {
  87. if (c->num_src <= 1)
  88. return;
  89. // uint8_t old_src = c->curr_src;
  90. c->curr_src++;
  91. if (c->curr_src >= MAX_CHANNEL_SOURCES-1 || c->sources[c->curr_src] == NULL)
  92. c->curr_src = 0;
  93. c->source = c->sources[c->curr_src];
  94. // LOGf("CHAN : Switch source | Channel: %s OldSrc: %d %s NewSrc: %d %s\n", c->name, old_src, c->sources[old_src], c->curr_src, c->source);
  95. }
  96. void chansrc_set(CHANNEL *c, uint8_t src_id) {
  97. if (src_id >= MAX_CHANNEL_SOURCES-1 || c->sources[src_id] == NULL)
  98. return;
  99. // uint8_t old_src = c->curr_src;
  100. c->curr_src = src_id;
  101. c->source = c->sources[c->curr_src];
  102. // LOGf("CHAN : Set source | Channel: %s OldSrc: %d %s NewSrc: %d %s\n", c->name, old_src, c->sources[old_src], c->curr_src, c->source);
  103. }
  104. CHANNEL *channel_new(int service_id, int is_radio, char *id, char *name, char *source) {
  105. CHANNEL *c = calloc(1, sizeof(CHANNEL));
  106. c->service_id = service_id;
  107. c->radio = is_radio;
  108. c->base_pid = service_id * 32; // The first pid is saved for PMT
  109. c->pmt_pid = c->base_pid; // The first pid is saved for PMT
  110. c->id = strdup(id);
  111. c->name = strdup(name);
  112. chansrc_add(c, source);
  113. return c;
  114. }
  115. void channel_free_epg(CHANNEL *c) {
  116. epg_free(&c->epg_now);
  117. epg_free(&c->epg_next);
  118. ts_eit_free(&c->eit_now);
  119. ts_eit_free(&c->eit_next);
  120. }
  121. void channel_free(CHANNEL **pc) {
  122. CHANNEL *c = *pc;
  123. if (c) {
  124. channel_free_epg(c);
  125. FREE(c->id);
  126. FREE(c->name);
  127. int i;
  128. for (i=c->num_src-1; i>=0; i--) {
  129. FREE(c->sources[i]);
  130. }
  131. c->source = NULL;
  132. FREE(*pc);
  133. }
  134. }
  135. EPG_ENTRY *epg_new(time_t start, int duration, char *encoding, char *event, char *short_desc, char *long_desc) {
  136. EPG_ENTRY *e;
  137. if (!event)
  138. return NULL;
  139. e = calloc(1, sizeof(EPG_ENTRY));
  140. e->event_id = (start / 60) &~ 0xffff0000;
  141. e->start = start;
  142. e->duration = duration;
  143. if (encoding && strcmp(encoding, "iso-8859-5")==0) {
  144. e->event = init_dvb_string_iso_8859_5(event);
  145. e->short_desc = init_dvb_string_iso_8859_5(short_desc);
  146. e->long_desc = init_dvb_string_iso_8859_5(long_desc);
  147. } else { // Default is utf-8
  148. e->event = init_dvb_string_utf8(event);
  149. e->short_desc = init_dvb_string_utf8(short_desc);
  150. e->long_desc = init_dvb_string_utf8(long_desc);
  151. }
  152. return e;
  153. }
  154. void epg_free(EPG_ENTRY **pe) {
  155. EPG_ENTRY *e = *pe;
  156. if (e) {
  157. FREE(e->event);
  158. FREE(e->short_desc);
  159. FREE(e->long_desc);
  160. FREE(*pe);
  161. }
  162. }
  163. // Return 1 if they are different
  164. // Return 0 if they are the same
  165. int epg_changed(EPG_ENTRY *a, EPG_ENTRY *b) {
  166. if (!a && b) return 1;
  167. if (!b && a) return 1;
  168. if (!a && !b) return 0;
  169. if (a->event_id != b->event_id) return 1;
  170. if (a->start != b->start) return 1;
  171. if (a->duration != b->duration) return 1;
  172. if (xstrcmp(a->event, b->event) != 0) return 1;
  173. if (xstrcmp(a->short_desc, b->short_desc) != 0) return 1;
  174. if (xstrcmp(a->long_desc, b->long_desc) != 0) return 1;
  175. return 0;
  176. }
  177. void input_stream_alloc(INPUT *input) {
  178. input->stream.pidref = pidref_init(64, input->channel->base_pid);
  179. input->stream.pat = ts_pat_alloc();
  180. input->stream.pmt = ts_pmt_alloc();
  181. input->stream.last_pat = ts_pat_alloc();
  182. input->stream.last_pmt = ts_pmt_alloc();
  183. }
  184. void input_stream_free(INPUT *input) {
  185. ts_pmt_free(&input->stream.pmt);
  186. ts_pmt_free(&input->stream.pmt_rewritten);
  187. ts_pmt_free(&input->stream.last_pmt);
  188. ts_pat_free(&input->stream.pat);
  189. ts_pat_free(&input->stream.pat_rewritten);
  190. ts_pat_free(&input->stream.last_pat);
  191. pidref_free(&input->stream.pidref);
  192. input->stream.nit_pid = 0;
  193. input->stream.pmt_pid = 0;
  194. input->stream.pcr_pid = 0;
  195. input->stream.input_pcr = 0;
  196. }
  197. void input_stream_reset(INPUT *input) {
  198. input_stream_free(input);
  199. input_stream_alloc(input);
  200. }
  201. INPUT * input_new(const char *name, CHANNEL *channel) {
  202. char *tmp;
  203. INPUT *r = calloc(1, sizeof(INPUT));
  204. r->name = strdup(name);
  205. r->sock = -1;
  206. r->channel = channel;
  207. if (config->write_input_file) {
  208. asprintf(&tmp, "mptsd-input-%s.ts", channel->id);
  209. r->ifd = open(tmp, O_CREAT | O_WRONLY | O_TRUNC, 0644);
  210. FREE(tmp);
  211. }
  212. r->buf = cbuf_init(1428 * 1316, channel->id); // ~ 10000 x 188
  213. input_stream_alloc(r);
  214. return r;
  215. }
  216. void input_free(INPUT **pinput) {
  217. INPUT *r = *pinput;
  218. if (!r)
  219. return;
  220. if (r->sock > -1)
  221. shutdown_fd(&(r->sock));
  222. if (r->freechannel)
  223. channel_free(&r->channel);
  224. if (r->ifd)
  225. close(r->ifd);
  226. input_stream_free(r);
  227. cbuf_free(&r->buf);
  228. FREE(r->name);
  229. FREE(*pinput);
  230. }
  231. OUTPUT *output_new() {
  232. OUTPUT *o = calloc(1, sizeof(OUTPUT));
  233. o->obuf_ms = 100;
  234. o->psibuf = cbuf_init(50 * 1316, "psi");
  235. if (!o->psibuf) {
  236. LOGf("ERROR: Can't allocate PSI input buffer\n");
  237. exit(1);
  238. }
  239. cbuf_poison(o->psibuf, 'Y');
  240. return o;
  241. }
  242. void output_open_file(OUTPUT *o) {
  243. o->ofd = open("mptsd-output.ts", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  244. }
  245. void obuf_reset(OBUF *ob) {
  246. int i;
  247. memset(ob->buf, 0xff, ob->size);
  248. for (i=0; i<ob->size; i+=188) {
  249. ob->buf[i+0] = 0x47;
  250. ob->buf[i+1] = 0x1f;
  251. ob->buf[i+2] = 0xff;
  252. ob->buf[i+3] = 0x00;
  253. }
  254. ob->written = 0;
  255. ob->status = obuf_empty;
  256. }
  257. void output_buffer_alloc(OUTPUT *o, double output_bitrate) {
  258. if (!output_bitrate) {
  259. LOGf("No output bitrate, can't determine buffer!\n");
  260. exit(1);
  261. }
  262. o->output_bitrate = output_bitrate;
  263. long pps = ceil((double)output_bitrate / (FRAME_PACKET_SIZE * 8)); // Packets per second
  264. long ppms = ceil((double)pps / ((double)1000 / o->obuf_ms)); // Packets per o->buffer_ms miliseconds
  265. long obuf_size = ppms * 1316;
  266. o->obuf[0].size = obuf_size;
  267. o->obuf[0].status = obuf_empty;
  268. o->obuf[0].buf = malloc(o->obuf[0].size);
  269. obuf_reset(&o->obuf[0]);
  270. o->obuf[1].size = obuf_size;
  271. o->obuf[1].status = obuf_empty;
  272. o->obuf[1].buf = malloc(o->obuf[0].size);
  273. obuf_reset(&o->obuf[1]);
  274. LOGf("\tOutput buf size : %ld * 2 = %ld\n", obuf_size, obuf_size * 2);
  275. LOGf("\tOutput buf packets: %ld (188 bytes)\n", obuf_size / 188);
  276. LOGf("\tOutput buf frames : %ld (1316 bytes)\n", obuf_size / 1316);
  277. LOGf("\tOutput buf ms : %u ms\n", o->obuf_ms);
  278. }
  279. void output_free(OUTPUT **po) {
  280. OUTPUT *o = *po;
  281. if (!o)
  282. return;
  283. if (o->out_sock > -1)
  284. shutdown_fd(&(o->out_sock));
  285. if (o->ofd)
  286. close(o->ofd);
  287. cbuf_free(&o->psibuf);
  288. FREE(o->obuf[0].buf);
  289. FREE(o->obuf[1].buf);
  290. output_psi_free(o);
  291. FREE(*po);
  292. }
  293. NIT *nit_new(uint16_t ts_id, char *freq, char *mod, char *symbol_rate) {
  294. char tmp[9];
  295. unsigned i, pos;
  296. NIT *n = calloc(1, sizeof(NIT));
  297. if (strlen(freq) != 9 || strlen(symbol_rate) != 8)
  298. return NULL;
  299. n->freq = strdup(freq);
  300. n->modulation = strdup(mod);
  301. n->symbol_rate = strdup(symbol_rate);
  302. n->ts_id = ts_id;
  303. n->_modulation =
  304. strcmp(mod, "16-QAM") == 0 ? 0x01 :
  305. strcmp(mod, "32-QAM") == 0 ? 0x02 :
  306. strcmp(mod, "64-QAM") == 0 ? 0x03 :
  307. strcmp(mod, "128-QAM") == 0 ? 0x04 :
  308. strcmp(mod, "256-QAM") == 0 ? 0x05 : 0x00;
  309. memset(tmp, 0, sizeof(tmp));
  310. pos = 0;
  311. for (i=0;i<strlen(freq);i++) {
  312. if (isdigit(freq[i])) {
  313. tmp[pos] = freq[i];
  314. pos++;
  315. }
  316. }
  317. n->_freq = strtol(tmp, NULL, 16);
  318. memset(tmp, 0, sizeof(tmp));
  319. pos = 0;
  320. for (i=0;i<strlen(symbol_rate);i++) {
  321. if (isdigit(symbol_rate[i])) {
  322. tmp[pos] = symbol_rate[i];
  323. pos++;
  324. }
  325. }
  326. n->_symbol_rate = strtol(tmp, NULL, 16);
  327. return n;
  328. }
  329. void nit_free(NIT **pn) {
  330. NIT *n = *pn;
  331. if (n) {
  332. FREE(n->freq);
  333. FREE(n->modulation);
  334. FREE(n->symbol_rate);
  335. FREE(*pn);
  336. }
  337. }
  338. void proxy_log(INPUT *r, char *msg) {
  339. LOGf("INPUT : [%-12s] %s fd: %d src: %s\n", r->channel->id, msg, r->sock, r->channel->source);
  340. }
  341. void proxy_close(LIST *inputs, INPUT **input) {
  342. proxy_log(*input, "Stop");
  343. // If there are no clients left, no "Timeout" messages will be logged
  344. list_del_entry(inputs, *input);
  345. input_free(input);
  346. }