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.8KB

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