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.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * mptsd data header file
  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. #ifndef DATA_H
  19. #define DATA_H
  20. /* How much to wait for connection to be established with channel source (miliseconds) */
  21. #define PROXY_CONNECT_TIMEOUT 1000
  22. /* Seconds to sleep between retries (miliseconds) */
  23. #define PROXY_RETRY_TIMEOUT 1000
  24. /* 7 * 188 */
  25. #define FRAME_PACKET_SIZE 1316
  26. #include "libfuncs/libfuncs.h"
  27. #include "libtsfuncs/tsdata.h"
  28. #include "pidref.h"
  29. typedef enum { udp_sock, tcp_sock } channel_source;
  30. typedef struct {
  31. channel_source sproto;
  32. char *proto;
  33. char *host;
  34. char *path;
  35. unsigned int port;
  36. } CHANSRC;
  37. #define MAX_CHANNEL_SOURCES 8
  38. typedef struct {
  39. uint16_t event_id;
  40. time_t start;
  41. int duration;
  42. char * event;
  43. char * short_desc;
  44. char * long_desc;
  45. } EPG_ENTRY;
  46. typedef struct {
  47. /* Config */
  48. int base_pid;
  49. int service_id;
  50. int pmt_pid;
  51. int radio;
  52. char * id;
  53. char * name;
  54. /* Sources */
  55. char * source; /* Full source url */
  56. char * sources[MAX_CHANNEL_SOURCES];
  57. uint8_t num_src;
  58. uint8_t curr_src;
  59. int worktime_start;
  60. int worktime_end;
  61. /* EPG */
  62. uint8_t epg_version:5;
  63. EPG_ENTRY * epg_now;
  64. EPG_ENTRY * epg_next;
  65. struct ts_eit * eit_now;
  66. struct ts_eit * eit_next;
  67. } CHANNEL;
  68. typedef struct {
  69. uint64_t pcr;
  70. uint64_t last_pcr;
  71. int bytes;
  72. int ts_packets_per_output_bitrate;
  73. } PCR;
  74. typedef struct {
  75. PIDREF *pidref; /* Rewritten pids list */
  76. uint16_t nit_pid; /* Pid of the NIT, default 0x10 */
  77. uint16_t pmt_pid; /* Pid of the original PMT, used to replace PMT */
  78. uint16_t pcr_pid; /* PCR pid */
  79. struct ts_pat *pat; /* The PAT */
  80. struct ts_pmt *pmt; /* The PMT */
  81. uint64_t input_pcr; // Latest PCR entered into input buffer
  82. uint8_t pid_pat_cont:4;
  83. uint8_t pid_pmt_cont:4;
  84. struct ts_pat *pat_rewritten; /* The rewritten PAT */
  85. struct ts_pmt *pmt_rewritten; /* The rewritten PMT */
  86. struct ts_pat *last_pat; /* The last incoming PAT */
  87. struct ts_pmt *last_pmt; /* The last incoming PMT */
  88. } INPUT_STREAM;
  89. typedef struct {
  90. char *name;
  91. CHANNEL *channel;
  92. int sock; /* Server socket */
  93. struct sockaddr_in src_sockname;
  94. int reconnect:1, /* Set to 1 to force proxy reconnect */
  95. connected:1, /* It's set to 1 when proxy is connected and serving clients */
  96. insert_eit:1, /* When set to 1 input adds EIT table into stream (if there is info) */
  97. working:1, /* Set to 1 if the input is in worktime */
  98. dienow:1, /* Stop serving clients and exit now */
  99. freechannel:1; /* Free channel data on object free (this is used in chanconf) */
  100. int cookie; /* Used in chanconf to determine if the restreamer is alrady checked */
  101. int ifd;
  102. pthread_t thread;
  103. uint16_t output_pcr_pid;
  104. uint64_t output_last_pcr; // The PCR before latest outputed
  105. uint64_t output_pcr; // Latest outputed PCR
  106. int output_pcr_packets_needed; // Based on selected output rate how much packets should be between output_pcr and output_last_pcr
  107. int outputed_packets; // How much packets have been sent. This is reset on every PCR and incremented on every sent packet (every input and output padding)
  108. int disabled; /* Input is disabled, no data is fed to output buffers */
  109. int input_ready; /* Set to 1 from INPUT thread when input is ready to be mixed in output */
  110. CBUF *buf; // Input buffer */
  111. INPUT_STREAM stream;
  112. } INPUT;
  113. typedef enum {
  114. obuf_empty = 0, // Buffer is empty and can be used by mix thread
  115. obuf_filling = 1, // Buffer is being filled by mix thread
  116. obuf_full = 2, // Buffer is filled and can be used by write thread
  117. obuf_emptying = 3, // Buffer is being emptyed by write thread
  118. } OBUF_STATUS;
  119. typedef struct {
  120. uint8_t * buf;
  121. int size; // Output buffer size (must be size % 1316 == 0)
  122. int written;
  123. OBUF_STATUS status;
  124. } OBUF;
  125. typedef struct {
  126. struct in_addr out_host;
  127. int out_port;
  128. int out_sock; /* The udp socket */
  129. int ofd;
  130. int dienow; /* Instruct output to die */
  131. pthread_t psi_thread;
  132. pthread_t mix_thread;
  133. pthread_t write_thread;
  134. CBUF *psibuf; // Input buffer */
  135. unsigned int obuf_ms; // How much miliseconds of data output buffer holds
  136. OBUF obuf[2]; // Output buffers
  137. double output_bitrate; // Output bitrate (bps)
  138. uint64_t traffic;
  139. uint64_t traffic_period;
  140. uint64_t padding_period;
  141. uint8_t pid_pat_cont:4;
  142. uint8_t pid_nit_cont:4;
  143. uint8_t pid_sdt_cont:4;
  144. uint8_t pid_eit_cont:4;
  145. uint8_t pid_tdt_cont:4;
  146. struct ts_pat *pat;
  147. struct timeval pat_ts;
  148. struct ts_sdt *sdt;
  149. struct timeval sdt_ts;
  150. struct ts_nit *nit;
  151. struct timeval nit_ts;
  152. struct ts_tdt *tdt;
  153. struct timeval tdt_ts;
  154. struct ts_tdt *tot;
  155. struct timeval tot_ts;
  156. struct timeval eit_ts;
  157. uint64_t last_org_pcr[8193]; // Last PCR value indexed by PID x
  158. uint64_t last_pcr[8193]; // Last PCR value indexed by PID x
  159. uint64_t last_traffic[8193]; // Last traffic when PCR with PID x was seen
  160. } OUTPUT;
  161. typedef struct {
  162. char *freq;
  163. char *modulation;
  164. char *symbol_rate;
  165. uint16_t ts_id;
  166. uint32_t _freq;
  167. uint8_t _modulation;
  168. uint32_t _symbol_rate;
  169. } NIT;
  170. EPG_ENTRY * epg_new (time_t start, int duration, char *encoding, char *event, char *short_desc, char *long_desc);
  171. void epg_free (EPG_ENTRY **e);
  172. int epg_changed (EPG_ENTRY *a, EPG_ENTRY *b);
  173. CHANNEL * channel_new (int service_id, int is_radio, char *id, char *name, char *source);
  174. void channel_free (CHANNEL **c);
  175. void channel_free_epg(CHANNEL *c);
  176. channel_source get_sproto(char *url);
  177. CHANSRC * chansrc_init (char *url);
  178. void chansrc_free (CHANSRC **url);
  179. void chansrc_add (CHANNEL *c, char *src);
  180. void chansrc_next (CHANNEL *c);
  181. void chansrc_set (CHANNEL *c, uint8_t src_id);
  182. INPUT * input_new (const char *name, CHANNEL *channel);
  183. void input_free (INPUT **input);
  184. void input_stream_reset (INPUT *input);
  185. OUTPUT * output_new ();
  186. void output_free (OUTPUT **output);
  187. void output_open_file (OUTPUT *o);
  188. void output_buffer_alloc (OUTPUT *o, double output_bitrate);
  189. void obuf_reset (OBUF *ob);
  190. NIT * nit_new (uint16_t ts_id, char *freq, char *modulation, char *symbol_rate);
  191. void nit_free (NIT **nit);
  192. void proxy_log (INPUT *r, char *msg);
  193. void proxy_close (LIST *inputs, INPUT **input);
  194. #endif