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.

data.c 10KB

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