add -3 option to pass through to stdout
[dvblast.git] / dvblast.h
blobc03cc47579ad501f38ed62fad85175d28b6ca3a4
1 /*****************************************************************************
2 * dvblast.h
3 *****************************************************************************
4 * Copyright (C) 2004, 2008-2011, 2015 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 <netinet/udp.h>
27 #include <netinet/ip.h>
29 #include "config.h"
31 /* Defines for pid mapping */
32 #define N_MAP_PIDS 4
33 /* Offsets in the command line args for the pid mapping */
34 typedef enum
36 I_PMTPID = 0, I_APID, I_VPID, I_SPUPID
37 } pidmap_offset;
39 /* Impossible PID value */
40 #define UNUSED_PID (MAX_PIDS + 1)
42 /*****************************************************************************
43 * Raw udp packet structure with flexible-array payload
44 *****************************************************************************/
45 struct udpheader { // FAVOR_BSD hell ...
46 u_int16_t source;
47 u_int16_t dest;
48 u_int16_t len;
49 u_int16_t check;
52 #if defined(__FreeBSD__) || defined(__APPLE__)
53 struct iphdr {
54 unsigned int ihl:4;
55 unsigned int version:4;
56 uint8_t tos;
57 uint16_t tot_len;
58 uint16_t id;
59 uint16_t frag_off;
60 uint8_t ttl;
61 uint8_t protocol;
62 uint16_t check;
63 uint32_t saddr;
64 uint32_t daddr;
66 #endif
68 struct udprawpkt {
69 struct iphdr iph;
70 struct udpheader udph;
71 uint8_t payload[];
72 } __attribute__((packed));
74 /*****************************************************************************
75 * Output configuration flags (for output_t -> i_config) - bit values
76 * Bit 0 : Set for watch mode
77 * Bit 1 : Set output still present
78 * Bit 2 : Set if output is valid (replaces m_addr != 0 tests)
79 * Bit 3 : Set for UDP, otherwise use RTP if a network stream
80 * Bit 4 : Set for file / FIFO output, unset for network (future use)
81 * Bit 5 : Set if DVB conformance tables are inserted
82 * Bit 6 : Set if DVB EIT schedule tables are forwarded
83 * Bit 7 : Set for RAW socket output
84 *****************************************************************************/
86 #define OUTPUT_WATCH 0x01
87 #define OUTPUT_STILL_PRESENT 0x02
88 #define OUTPUT_VALID 0x04
89 #define OUTPUT_UDP 0x08
90 #define OUTPUT_FILE 0x10
91 #define OUTPUT_DVB 0x20
92 #define OUTPUT_EPG 0x40
93 #define OUTPUT_RAW 0x80
95 typedef int64_t mtime_t;
97 typedef struct block_t
99 uint8_t p_ts[TS_SIZE];
100 int i_refcount;
101 mtime_t i_dts;
102 uint16_t tmp_pid;
103 struct block_t *p_next;
104 } block_t;
106 typedef struct packet_t packet_t;
108 typedef struct output_config_t
110 /* identity */
111 int i_family;
112 struct sockaddr_storage connect_addr;
113 struct sockaddr_storage bind_addr;
114 int i_if_index_v6;
116 /* common config */
117 char *psz_displayname;
118 uint64_t i_config;
120 /* output config */
121 char *psz_service_name;
122 char *psz_service_provider;
123 uint8_t pi_ssrc[4];
124 mtime_t i_output_latency, i_max_retention;
125 int i_ttl;
126 uint8_t i_tos;
127 int i_mtu;
128 char *psz_srcaddr; /* raw packets */
129 int i_srcport;
131 /* demux config */
132 int i_tsid;
133 uint16_t i_sid; /* 0 if raw mode */
134 uint16_t *pi_pids;
135 int i_nb_pids;
136 uint16_t i_new_sid;
138 /* for pidmap from config file */
139 bool b_do_remap;
140 uint16_t pi_confpids[N_MAP_PIDS];
141 } output_config_t;
143 typedef struct output_t
145 output_config_t config;
147 /* output */
148 int i_handle;
149 packet_t *p_packets, *p_last_packet;
150 uint16_t i_seqnum;
151 mtime_t i_ref_timestamp;
152 mtime_t i_ref_wallclock;
154 /* demux */
155 int i_nb_errors;
156 mtime_t i_last_error;
157 uint8_t *p_pat_section;
158 uint8_t i_pat_version, i_pat_cc;
159 uint8_t *p_pmt_section;
160 uint8_t i_pmt_version, i_pmt_cc;
161 uint8_t *p_nit_section;
162 uint8_t i_nit_version, i_nit_cc;
163 uint8_t *p_sdt_section;
164 uint8_t i_sdt_version, i_sdt_cc;
165 block_t *p_eit_ts_buffer;
166 uint8_t i_eit_ts_buffer_offset, i_eit_cc;
167 uint16_t i_tsid;
168 // Arrays used for mapping pids.
169 // newpids is indexed using the original pid
170 uint16_t pi_newpids[MAX_PIDS];
171 uint16_t pi_freepids[MAX_PIDS]; // used where multiple streams of the same type are used
173 struct udprawpkt raw_pkt_header;
174 } output_t;
176 typedef struct ts_pid_info {
177 mtime_t i_first_packet_ts; /* Time of the first seen packet */
178 mtime_t i_last_packet_ts; /* Time of the last seen packet */
179 unsigned long i_packets; /* How much packets have been seen */
180 unsigned long i_cc_errors; /* Countinuity counter errors */
181 unsigned long i_transport_errors; /* Transport errors */
182 unsigned long i_bytes_per_sec; /* How much bytes were process last second */
183 uint8_t i_scrambling; /* Scrambling bits from the last ts packet */
184 /* 0 = Not scrambled
185 1 = Reserved for future use
186 2 = Scrambled with even key
187 3 = Scrambled with odd key */
188 } ts_pid_info_t;
190 extern int i_syslog;
191 extern int i_verbose;
192 extern output_t **pp_outputs;
193 extern int i_nb_outputs;
194 extern output_t output_dup;
195 extern bool b_passthrough;
196 extern char *psz_srv_socket;
197 extern int i_comm_fd;
198 extern int i_adapter;
199 extern int i_fenum;
200 extern int i_canum;
201 extern char *psz_delsys;
202 extern int i_dvr_buffer_size;
203 extern int i_frequency;
204 extern int i_srate;
205 extern int i_satnum;
206 extern int i_uncommitted;
207 extern int i_fec;
208 extern int i_rolloff;
209 extern int i_voltage;
210 extern int b_tone;
211 extern int i_bandwidth;
212 extern int i_inversion;
213 extern char *psz_modulation;
214 extern int i_pilot;
215 extern int i_mis;
216 extern int i_fec_lp;
217 extern int i_guard;
218 extern int i_transmission;
219 extern int i_hierarchy;
220 extern mtime_t i_frontend_timeout_duration;
221 extern mtime_t i_quit_timeout;
222 extern mtime_t i_quit_timeout_duration;
223 extern int b_budget_mode;
224 extern int b_any_type;
225 extern int b_select_pmts;
226 extern int b_random_tsid;
227 extern uint16_t i_network_id;
228 extern uint8_t *p_network_name;
229 extern size_t i_network_name_size;
230 extern mtime_t i_wallclock;
231 extern volatile int b_conf_reload;
232 extern volatile int b_exit_now;
233 extern int i_comm_fd;
234 extern char *psz_udp_src;
235 extern int i_asi_adapter;
236 extern const char *psz_native_charset;
237 extern const char *psz_dvb_charset;
238 extern enum print_type_t i_print_type;
239 extern bool b_print_enabled;
240 extern FILE *print_fh;
242 /* pid mapping */
243 extern bool b_do_remap;
244 extern uint16_t pi_newpids[N_MAP_PIDS];
245 extern void init_pid_mapping( output_t * );
247 extern void (*pf_Open)( void );
248 extern block_t * (*pf_Read)( mtime_t i_poll_timeout );
249 extern void (*pf_Reset)( void );
250 extern int (*pf_SetFilter)( uint16_t i_pid );
251 extern void (*pf_UnsetFilter)( int i_fd, uint16_t i_pid );
253 /*****************************************************************************
254 * Prototypes
255 *****************************************************************************/
257 void config_Init( output_config_t *p_config );
258 void config_Free( output_config_t *p_config );
259 bool config_ParseHost( output_config_t *p_config, char *psz_string );
261 /* Connect/Disconnect from syslogd */
262 void msg_Connect( const char *ident );
263 void msg_Disconnect( void );
265 /* */
266 __attribute__ ((format(printf, 2, 3))) void msg_Info( void *_unused, const char *psz_format, ... );
267 __attribute__ ((format(printf, 2, 3))) void msg_Err( void *_unused, const char *psz_format, ... );
268 __attribute__ ((format(printf, 2, 3))) void msg_Warn( void *_unused, const char *psz_format, ... );
269 __attribute__ ((format(printf, 2, 3))) void msg_Dbg( void *_unused, const char *psz_format, ... );
270 __attribute__ ((format(printf, 2, 3))) void msg_Raw( void *_unused, const char *psz_format, ... );
272 /* */
273 bool streq(char *a, char *b);
274 char * xstrdup(char *str);
275 mtime_t mdate( void );
276 void msleep( mtime_t delay );
277 void hexDump( uint8_t *p_data, uint32_t i_len );
278 struct addrinfo *ParseNodeService( char *_psz_string, char **ppsz_end,
279 uint16_t i_default_port );
280 char *config_stropt( char *psz_string );
282 uint8_t *psi_pack_section( uint8_t *p_sections, unsigned int *pi_size );
283 uint8_t *psi_pack_sections( uint8_t **pp_sections, unsigned int *pi_size );
284 uint8_t **psi_unpack_sections( uint8_t *p_flat_sections, unsigned int i_size );
286 void dvb_Open( void );
287 void dvb_Reset( void );
288 block_t * dvb_Read( mtime_t i_poll_timeout );
289 int dvb_SetFilter( uint16_t i_pid );
290 void dvb_UnsetFilter( int i_fd, uint16_t i_pid );
291 uint8_t dvb_FrontendStatus( uint8_t *p_answer, ssize_t *pi_size );
293 void udp_Open( void );
294 block_t * udp_Read( mtime_t i_poll_timeout );
295 void udp_Reset( void );
296 int udp_SetFilter( uint16_t i_pid );
297 void udp_UnsetFilter( int i_fd, uint16_t i_pid );
299 void asi_Open( void );
300 block_t * asi_Read( mtime_t i_poll_timeout );
301 void asi_Reset( void );
302 int asi_SetFilter( uint16_t i_pid );
303 void asi_UnsetFilter( int i_fd, uint16_t i_pid );
305 #ifdef HAVE_ASI_DELTACAST_SUPPORT
306 void asi_deltacast_Open( void );
307 block_t * asi_deltacast_Read( mtime_t i_poll_timeout );
308 void asi_deltacast_Reset( void );
309 int asi_deltacast_SetFilter( uint16_t i_pid );
310 void asi_deltacast_UnsetFilter( int i_fd, uint16_t i_pid );
311 #endif
313 void demux_Open( void );
314 void demux_Run( block_t *p_ts );
315 void demux_Change( output_t *p_output, const output_config_t *p_config );
316 void demux_ResendCAPMTs( void );
317 bool demux_PIDIsSelected( uint16_t i_pid );
318 char *demux_Iconv(void *_unused, const char *psz_encoding,
319 char *p_string, size_t i_length);
320 void demux_Close( void );
322 uint8_t *demux_get_current_packed_PAT( unsigned int *pi_pack_size );
323 uint8_t *demux_get_current_packed_CAT( unsigned int *pi_pack_size );
324 uint8_t *demux_get_current_packed_NIT( unsigned int *pi_pack_size );
325 uint8_t *demux_get_current_packed_SDT( unsigned int *pi_pack_size );
326 uint8_t *demux_get_packed_PMT( uint16_t service_id, unsigned int *pi_pack_size );
327 void demux_get_PID_info( uint16_t i_pid, uint8_t *p_data );
328 void demux_get_PIDS_info( uint8_t *p_data );
330 output_t *output_Create( const output_config_t *p_config );
331 int output_Init( output_t *p_output, const output_config_t *p_config );
332 void output_Close( output_t *p_output );
333 void output_Put( output_t *p_output, block_t *p_block );
334 mtime_t output_Send( void );
335 output_t *output_Find( const output_config_t *p_config );
336 void output_Change( output_t *p_output, const output_config_t *p_config );
337 void outputs_Close( int i_num_outputs );
339 void comm_Open( void );
340 void comm_Read( void );
342 /*****************************************************************************
343 * block_New
344 *****************************************************************************/
345 static inline block_t *block_New( void )
347 block_t *p_block = malloc(sizeof(block_t));
348 p_block->p_next = NULL;
349 p_block->i_refcount = 1;
350 return p_block;
353 /*****************************************************************************
354 * block_Delete
355 *****************************************************************************/
356 static inline void block_Delete( block_t *p_block )
358 free( p_block );
361 /*****************************************************************************
362 * block_DeleteChain
363 *****************************************************************************/
364 static inline void block_DeleteChain( block_t *p_block )
366 while ( p_block != NULL )
368 block_t *p_next = p_block->p_next;
369 free( p_block );
370 p_block = p_next;