fix EIT schedule signaling in SDT
[dvblast.git] / dvblast.h
bloba8d51e2b841234b16a672611dde61d0cc81701b6
1 /*****************************************************************************
2 * dvblast.h
3 *****************************************************************************
4 * Copyright (C) 2004, 2008-2011, 2015-2016 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Andy Gatward <a.j.gatward@reading.ac.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <sys/types.h> /* u_int16_t */
27 #include <netinet/udp.h>
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
31 #include "config.h"
33 #ifndef container_of
34 # define container_of(ptr, type, member) ({ \
35 const typeof( ((type *)0)->member ) *_mptr = (ptr); \
36 (type *)( (char *)_mptr - offsetof(type,member) );})
37 #endif
39 /* Defines for pid mapping */
40 #define N_MAP_PIDS 4
41 /* Offsets in the command line args for the pid mapping */
42 typedef enum
44 I_PMTPID = 0, I_APID, I_VPID, I_SPUPID
45 } pidmap_offset;
47 /* Impossible PID value */
48 #define UNUSED_PID (MAX_PIDS + 1)
50 /*****************************************************************************
51 * Raw udp packet structure with flexible-array payload
52 *****************************************************************************/
53 struct udpheader { // FAVOR_BSD hell ...
54 u_int16_t source;
55 u_int16_t dest;
56 u_int16_t len;
57 u_int16_t check;
60 #if defined(__FreeBSD__) || defined(__APPLE__)
61 struct iphdr {
62 unsigned int ihl:4;
63 unsigned int version:4;
64 uint8_t tos;
65 uint16_t tot_len;
66 uint16_t id;
67 uint16_t frag_off;
68 uint8_t ttl;
69 uint8_t protocol;
70 uint16_t check;
71 uint32_t saddr;
72 uint32_t daddr;
74 #endif
76 struct udprawpkt {
77 struct iphdr iph;
78 struct udpheader udph;
79 uint8_t payload[];
80 } __attribute__((packed));
82 /*****************************************************************************
83 * Output configuration flags (for output_t -> i_config) - bit values
84 * Bit 0 : Set for watch mode
85 * Bit 1 : Set output still present
86 * Bit 2 : Set if output is valid (replaces m_addr != 0 tests)
87 * Bit 3 : Set for UDP, otherwise use RTP if a network stream
88 * Bit 4 : Set for file / FIFO output, unset for network (future use)
89 * Bit 5 : Set if DVB conformance tables are inserted
90 * Bit 6 : Set if DVB EIT schedule tables are forwarded
91 * Bit 7 : Set for RAW socket output
92 *****************************************************************************/
94 #define OUTPUT_WATCH 0x01
95 #define OUTPUT_STILL_PRESENT 0x02
96 #define OUTPUT_VALID 0x04
97 #define OUTPUT_UDP 0x08
98 #define OUTPUT_FILE 0x10
99 #define OUTPUT_DVB 0x20
100 #define OUTPUT_EPG 0x40
101 #define OUTPUT_RAW 0x80
103 typedef int64_t mtime_t;
105 typedef struct block_t
107 uint8_t p_ts[TS_SIZE];
108 int i_refcount;
109 mtime_t i_dts;
110 uint16_t tmp_pid;
111 struct block_t *p_next;
112 } block_t;
114 typedef struct packet_t packet_t;
116 typedef struct dvb_string_t
118 uint8_t *p;
119 size_t i;
120 } dvb_string_t;
122 typedef struct output_config_t
124 /* identity */
125 int i_family;
126 struct sockaddr_storage connect_addr;
127 struct sockaddr_storage bind_addr;
128 int i_if_index_v6;
130 /* common config */
131 char *psz_displayname;
132 uint64_t i_config;
134 /* output config */
135 uint16_t i_network_id;
136 dvb_string_t network_name;
137 dvb_string_t service_name;
138 dvb_string_t provider_name;
139 uint8_t pi_ssrc[4];
140 mtime_t i_output_latency, i_max_retention;
141 int i_ttl;
142 uint8_t i_tos;
143 int i_mtu;
144 char *psz_srcaddr; /* raw packets */
145 int i_srcport;
147 /* demux config */
148 int i_tsid;
149 uint16_t i_sid; /* 0 if raw mode */
150 uint16_t *pi_pids;
151 int i_nb_pids;
152 uint16_t i_new_sid;
153 uint16_t i_onid;
154 bool b_passthrough;
156 /* for pidmap from config file */
157 bool b_do_remap;
158 uint16_t pi_confpids[N_MAP_PIDS];
159 } output_config_t;
161 typedef struct output_t
163 output_config_t config;
165 /* output */
166 int i_handle;
167 packet_t *p_packets, *p_last_packet;
168 packet_t *p_packet_lifo;
169 unsigned int i_packet_count;
170 uint16_t i_seqnum;
172 /* demux */
173 int i_nb_errors;
174 mtime_t i_last_error;
175 uint8_t *p_pat_section;
176 uint8_t i_pat_version, i_pat_cc;
177 uint8_t *p_pmt_section;
178 uint8_t i_pmt_version, i_pmt_cc;
179 uint8_t *p_nit_section;
180 uint8_t i_nit_version, i_nit_cc;
181 uint8_t *p_sdt_section;
182 uint8_t i_sdt_version, i_sdt_cc;
183 block_t *p_eit_ts_buffer;
184 uint8_t i_eit_ts_buffer_offset, i_eit_cc;
185 uint16_t i_tsid;
186 /* incomplete PID (only PCR packets) */
187 uint16_t i_pcr_pid;
188 // Arrays used for mapping pids.
189 // newpids is indexed using the original pid
190 uint16_t pi_newpids[MAX_PIDS];
191 uint16_t pi_freepids[MAX_PIDS]; // used where multiple streams of the same type are used
193 struct udprawpkt raw_pkt_header;
194 } output_t;
196 typedef struct ts_pid_info {
197 mtime_t i_first_packet_ts; /* Time of the first seen packet */
198 mtime_t i_last_packet_ts; /* Time of the last seen packet */
199 unsigned long i_packets; /* How much packets have been seen */
200 unsigned long i_cc_errors; /* Countinuity counter errors */
201 unsigned long i_transport_errors; /* Transport errors */
202 unsigned long i_bytes_per_sec; /* How much bytes were process last second */
203 uint8_t i_scrambling; /* Scrambling bits from the last ts packet */
204 /* 0 = Not scrambled
205 1 = Reserved for future use
206 2 = Scrambled with even key
207 3 = Scrambled with odd key */
208 } ts_pid_info_t;
210 extern struct ev_loop *event_loop;
211 extern int i_syslog;
212 extern int i_verbose;
213 extern output_t **pp_outputs;
214 extern int i_nb_outputs;
215 extern output_t output_dup;
216 extern bool b_passthrough;
217 extern char *psz_srv_socket;
218 extern int i_adapter;
219 extern int i_fenum;
220 extern int i_canum;
221 extern char *psz_delsys;
222 extern int i_dvr_buffer_size;
223 extern int i_frequency;
224 extern int i_srate;
225 extern int i_satnum;
226 extern int i_uncommitted;
227 extern int i_fec;
228 extern int i_rolloff;
229 extern int i_voltage;
230 extern int b_tone;
231 extern int i_bandwidth;
232 extern int i_inversion;
233 extern char *psz_modulation;
234 extern int i_pilot;
235 extern int i_mis;
236 extern int i_fec_lp;
237 extern int i_guard;
238 extern int i_transmission;
239 extern int i_hierarchy;
240 extern mtime_t i_frontend_timeout_duration;
241 extern mtime_t i_quit_timeout_duration;
242 extern int b_budget_mode;
243 extern int b_any_type;
244 extern int b_select_pmts;
245 extern int b_random_tsid;
246 extern int dvb_plp_id;
247 extern bool b_enable_emm;
248 extern bool b_enable_ecm;
249 extern mtime_t i_wallclock;
250 extern char *psz_udp_src;
251 extern int i_asi_adapter;
252 extern const char *psz_native_charset;
253 extern enum print_type_t i_print_type;
254 extern bool b_print_enabled;
255 extern FILE *print_fh;
256 extern mtime_t i_print_period;
257 extern mtime_t i_es_timeout;
259 /* pid mapping */
260 extern bool b_do_remap;
261 extern uint16_t pi_newpids[N_MAP_PIDS];
262 extern void init_pid_mapping( output_t * );
264 extern void (*pf_Open)( void );
265 extern void (*pf_Reset)( void );
266 extern int (*pf_SetFilter)( uint16_t i_pid );
267 extern void (*pf_UnsetFilter)( int i_fd, uint16_t i_pid );
269 /*****************************************************************************
270 * Prototypes
271 *****************************************************************************/
273 void config_Init( output_config_t *p_config );
274 void config_Free( output_config_t *p_config );
276 /* Connect/Disconnect from syslogd */
277 void msg_Connect( const char *ident );
278 void msg_Disconnect( void );
280 /* */
281 __attribute__ ((format(printf, 2, 3))) void msg_Info( void *_unused, const char *psz_format, ... );
282 __attribute__ ((format(printf, 2, 3))) void msg_Err( void *_unused, const char *psz_format, ... );
283 __attribute__ ((format(printf, 2, 3))) void msg_Warn( void *_unused, const char *psz_format, ... );
284 __attribute__ ((format(printf, 2, 3))) void msg_Dbg( void *_unused, const char *psz_format, ... );
285 __attribute__ ((format(printf, 2, 3))) void msg_Raw( void *_unused, const char *psz_format, ... );
287 /* */
288 bool streq(char *a, char *b);
289 char * xstrdup(char *str);
291 void dvb_string_init(dvb_string_t *p_dvb_string);
292 void dvb_string_clean(dvb_string_t *p_dvb_string);
293 void dvb_string_copy(dvb_string_t *p_dst, const dvb_string_t *p_src);
294 int dvb_string_cmp(const dvb_string_t *p_1, const dvb_string_t *p_2);
296 mtime_t mdate( void );
297 void msleep( mtime_t delay );
298 void hexDump( uint8_t *p_data, uint32_t i_len );
299 struct addrinfo *ParseNodeService( char *_psz_string, char **ppsz_end,
300 uint16_t i_default_port );
301 char *config_stropt( const char *psz_string );
302 void config_ReadFile(void);
304 uint8_t *psi_pack_section( uint8_t *p_sections, unsigned int *pi_size );
305 uint8_t *psi_pack_sections( uint8_t **pp_sections, unsigned int *pi_size );
306 uint8_t **psi_unpack_sections( uint8_t *p_flat_sections, unsigned int i_size );
308 void dvb_Open( void );
309 void dvb_Reset( void );
310 int dvb_SetFilter( uint16_t i_pid );
311 void dvb_UnsetFilter( int i_fd, uint16_t i_pid );
312 uint8_t dvb_FrontendStatus( uint8_t *p_answer, ssize_t *pi_size );
314 void udp_Open( void );
315 void udp_Reset( void );
316 int udp_SetFilter( uint16_t i_pid );
317 void udp_UnsetFilter( int i_fd, uint16_t i_pid );
319 void asi_Open( void );
320 void asi_Reset( void );
321 int asi_SetFilter( uint16_t i_pid );
322 void asi_UnsetFilter( int i_fd, uint16_t i_pid );
324 #ifdef HAVE_ASI_DELTACAST_SUPPORT
325 void asi_deltacast_Open( void );
326 void asi_deltacast_Reset( void );
327 int asi_deltacast_SetFilter( uint16_t i_pid );
328 void asi_deltacast_UnsetFilter( int i_fd, uint16_t i_pid );
329 #endif
331 void demux_Open( void );
332 void demux_Run( block_t *p_ts );
333 void demux_Change( output_t *p_output, const output_config_t *p_config );
334 void demux_ResendCAPMTs( void );
335 bool demux_PIDIsSelected( uint16_t i_pid );
336 char *demux_Iconv(void *_unused, const char *psz_encoding,
337 char *p_string, size_t i_length);
338 void demux_Close( void );
340 uint8_t *demux_get_current_packed_PAT( unsigned int *pi_pack_size );
341 uint8_t *demux_get_current_packed_CAT( unsigned int *pi_pack_size );
342 uint8_t *demux_get_current_packed_NIT( unsigned int *pi_pack_size );
343 uint8_t *demux_get_current_packed_SDT( unsigned int *pi_pack_size );
344 uint8_t *demux_get_packed_PMT( uint16_t service_id, unsigned int *pi_pack_size );
345 void demux_get_PID_info( uint16_t i_pid, uint8_t *p_data );
346 void demux_get_PIDS_info( uint8_t *p_data );
348 output_t *output_Create( const output_config_t *p_config );
349 int output_Init( output_t *p_output, const output_config_t *p_config );
350 void output_Close( output_t *p_output );
351 void output_Put( output_t *p_output, block_t *p_block );
352 output_t *output_Find( const output_config_t *p_config );
353 void output_Change( output_t *p_output, const output_config_t *p_config );
354 void outputs_Init( void );
355 void outputs_Close( int i_num_outputs );
357 void comm_Open( void );
358 void comm_Close( void );
360 block_t *block_New( void );
361 void block_Delete( block_t *p_block );
362 void block_Vacuum( void );
364 /*****************************************************************************
365 * block_DeleteChain
366 *****************************************************************************/
367 static inline void block_DeleteChain( block_t *p_block )
369 while ( p_block != NULL )
371 block_t *p_next = p_block->p_next;
372 block_Delete( p_block );
373 p_block = p_next;