optimize allocations with a buffer pool
[dvblast.git] / dvblast.h
blob14fcc09a9ed5fb27d5b3a16f1c4411972f82f47d
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 #ifndef container_of
32 # define container_of(ptr, type, member) ({ \
33 const typeof( ((type *)0)->member ) *_mptr = (ptr); \
34 (type *)( (char *)_mptr - offsetof(type,member) );})
35 #endif
37 /* Defines for pid mapping */
38 #define N_MAP_PIDS 4
39 /* Offsets in the command line args for the pid mapping */
40 typedef enum
42 I_PMTPID = 0, I_APID, I_VPID, I_SPUPID
43 } pidmap_offset;
45 /* Impossible PID value */
46 #define UNUSED_PID (MAX_PIDS + 1)
48 /*****************************************************************************
49 * Raw udp packet structure with flexible-array payload
50 *****************************************************************************/
51 struct udpheader { // FAVOR_BSD hell ...
52 u_int16_t source;
53 u_int16_t dest;
54 u_int16_t len;
55 u_int16_t check;
58 #if defined(__FreeBSD__) || defined(__APPLE__)
59 struct iphdr {
60 unsigned int ihl:4;
61 unsigned int version:4;
62 uint8_t tos;
63 uint16_t tot_len;
64 uint16_t id;
65 uint16_t frag_off;
66 uint8_t ttl;
67 uint8_t protocol;
68 uint16_t check;
69 uint32_t saddr;
70 uint32_t daddr;
72 #endif
74 struct udprawpkt {
75 struct iphdr iph;
76 struct udpheader udph;
77 uint8_t payload[];
78 } __attribute__((packed));
80 /*****************************************************************************
81 * Output configuration flags (for output_t -> i_config) - bit values
82 * Bit 0 : Set for watch mode
83 * Bit 1 : Set output still present
84 * Bit 2 : Set if output is valid (replaces m_addr != 0 tests)
85 * Bit 3 : Set for UDP, otherwise use RTP if a network stream
86 * Bit 4 : Set for file / FIFO output, unset for network (future use)
87 * Bit 5 : Set if DVB conformance tables are inserted
88 * Bit 6 : Set if DVB EIT schedule tables are forwarded
89 * Bit 7 : Set for RAW socket output
90 *****************************************************************************/
92 #define OUTPUT_WATCH 0x01
93 #define OUTPUT_STILL_PRESENT 0x02
94 #define OUTPUT_VALID 0x04
95 #define OUTPUT_UDP 0x08
96 #define OUTPUT_FILE 0x10
97 #define OUTPUT_DVB 0x20
98 #define OUTPUT_EPG 0x40
99 #define OUTPUT_RAW 0x80
101 typedef int64_t mtime_t;
103 typedef struct block_t
105 uint8_t p_ts[TS_SIZE];
106 int i_refcount;
107 mtime_t i_dts;
108 uint16_t tmp_pid;
109 struct block_t *p_next;
110 } block_t;
112 typedef struct packet_t packet_t;
114 typedef struct output_config_t
116 /* identity */
117 int i_family;
118 struct sockaddr_storage connect_addr;
119 struct sockaddr_storage bind_addr;
120 int i_if_index_v6;
122 /* common config */
123 char *psz_displayname;
124 uint64_t i_config;
126 /* output config */
127 char *psz_service_name;
128 char *psz_service_provider;
129 uint8_t pi_ssrc[4];
130 mtime_t i_output_latency, i_max_retention;
131 int i_ttl;
132 uint8_t i_tos;
133 int i_mtu;
134 char *psz_srcaddr; /* raw packets */
135 int i_srcport;
137 /* demux config */
138 int i_tsid;
139 uint16_t i_sid; /* 0 if raw mode */
140 uint16_t *pi_pids;
141 int i_nb_pids;
142 uint16_t i_new_sid;
144 /* for pidmap from config file */
145 bool b_do_remap;
146 uint16_t pi_confpids[N_MAP_PIDS];
147 } output_config_t;
149 typedef struct output_t
151 output_config_t config;
153 /* output */
154 int i_handle;
155 packet_t *p_packets, *p_last_packet;
156 packet_t *p_packet_lifo;
157 unsigned int i_packet_count;
158 uint16_t i_seqnum;
160 /* demux */
161 int i_nb_errors;
162 mtime_t i_last_error;
163 uint8_t *p_pat_section;
164 uint8_t i_pat_version, i_pat_cc;
165 uint8_t *p_pmt_section;
166 uint8_t i_pmt_version, i_pmt_cc;
167 uint8_t *p_nit_section;
168 uint8_t i_nit_version, i_nit_cc;
169 uint8_t *p_sdt_section;
170 uint8_t i_sdt_version, i_sdt_cc;
171 block_t *p_eit_ts_buffer;
172 uint8_t i_eit_ts_buffer_offset, i_eit_cc;
173 uint16_t i_tsid;
174 // Arrays used for mapping pids.
175 // newpids is indexed using the original pid
176 uint16_t pi_newpids[MAX_PIDS];
177 uint16_t pi_freepids[MAX_PIDS]; // used where multiple streams of the same type are used
179 struct udprawpkt raw_pkt_header;
180 } output_t;
182 typedef struct ts_pid_info {
183 mtime_t i_first_packet_ts; /* Time of the first seen packet */
184 mtime_t i_last_packet_ts; /* Time of the last seen packet */
185 unsigned long i_packets; /* How much packets have been seen */
186 unsigned long i_cc_errors; /* Countinuity counter errors */
187 unsigned long i_transport_errors; /* Transport errors */
188 unsigned long i_bytes_per_sec; /* How much bytes were process last second */
189 uint8_t i_scrambling; /* Scrambling bits from the last ts packet */
190 /* 0 = Not scrambled
191 1 = Reserved for future use
192 2 = Scrambled with even key
193 3 = Scrambled with odd key */
194 } ts_pid_info_t;
196 extern struct ev_loop *event_loop;
197 extern int i_syslog;
198 extern int i_verbose;
199 extern output_t **pp_outputs;
200 extern int i_nb_outputs;
201 extern output_t output_dup;
202 extern bool b_passthrough;
203 extern char *psz_srv_socket;
204 extern int i_adapter;
205 extern int i_fenum;
206 extern int i_canum;
207 extern char *psz_delsys;
208 extern int i_dvr_buffer_size;
209 extern int i_frequency;
210 extern int i_srate;
211 extern int i_satnum;
212 extern int i_uncommitted;
213 extern int i_fec;
214 extern int i_rolloff;
215 extern int i_voltage;
216 extern int b_tone;
217 extern int i_bandwidth;
218 extern int i_inversion;
219 extern char *psz_modulation;
220 extern int i_pilot;
221 extern int i_mis;
222 extern int i_fec_lp;
223 extern int i_guard;
224 extern int i_transmission;
225 extern int i_hierarchy;
226 extern mtime_t i_frontend_timeout_duration;
227 extern mtime_t i_quit_timeout_duration;
228 extern int b_budget_mode;
229 extern int b_any_type;
230 extern int b_select_pmts;
231 extern int b_random_tsid;
232 extern uint16_t i_network_id;
233 extern uint8_t *p_network_name;
234 extern size_t i_network_name_size;
235 extern bool b_enable_emm;
236 extern bool b_enable_ecm;
237 extern mtime_t i_wallclock;
238 extern char *psz_udp_src;
239 extern int i_asi_adapter;
240 extern const char *psz_native_charset;
241 extern const char *psz_dvb_charset;
242 extern enum print_type_t i_print_type;
243 extern bool b_print_enabled;
244 extern FILE *print_fh;
246 /* pid mapping */
247 extern bool b_do_remap;
248 extern uint16_t pi_newpids[N_MAP_PIDS];
249 extern void init_pid_mapping( output_t * );
251 extern void (*pf_Open)( void );
252 extern void (*pf_Reset)( void );
253 extern int (*pf_SetFilter)( uint16_t i_pid );
254 extern void (*pf_UnsetFilter)( int i_fd, uint16_t i_pid );
256 /*****************************************************************************
257 * Prototypes
258 *****************************************************************************/
260 void config_Init( output_config_t *p_config );
261 void config_Free( output_config_t *p_config );
262 bool config_ParseHost( output_config_t *p_config, char *psz_string );
264 /* Connect/Disconnect from syslogd */
265 void msg_Connect( const char *ident );
266 void msg_Disconnect( void );
268 /* */
269 __attribute__ ((format(printf, 2, 3))) void msg_Info( void *_unused, const char *psz_format, ... );
270 __attribute__ ((format(printf, 2, 3))) void msg_Err( void *_unused, const char *psz_format, ... );
271 __attribute__ ((format(printf, 2, 3))) void msg_Warn( void *_unused, const char *psz_format, ... );
272 __attribute__ ((format(printf, 2, 3))) void msg_Dbg( void *_unused, const char *psz_format, ... );
273 __attribute__ ((format(printf, 2, 3))) void msg_Raw( void *_unused, const char *psz_format, ... );
275 /* */
276 bool streq(char *a, char *b);
277 char * xstrdup(char *str);
278 mtime_t mdate( void );
279 void msleep( mtime_t delay );
280 void hexDump( uint8_t *p_data, uint32_t i_len );
281 struct addrinfo *ParseNodeService( char *_psz_string, char **ppsz_end,
282 uint16_t i_default_port );
283 char *config_stropt( char *psz_string );
284 void config_ReadFile(void);
286 uint8_t *psi_pack_section( uint8_t *p_sections, unsigned int *pi_size );
287 uint8_t *psi_pack_sections( uint8_t **pp_sections, unsigned int *pi_size );
288 uint8_t **psi_unpack_sections( uint8_t *p_flat_sections, unsigned int i_size );
290 void dvb_Open( void );
291 void dvb_Reset( void );
292 int dvb_SetFilter( uint16_t i_pid );
293 void dvb_UnsetFilter( int i_fd, uint16_t i_pid );
294 uint8_t dvb_FrontendStatus( uint8_t *p_answer, ssize_t *pi_size );
296 void udp_Open( void );
297 void udp_Reset( void );
298 int udp_SetFilter( uint16_t i_pid );
299 void udp_UnsetFilter( int i_fd, uint16_t i_pid );
301 void asi_Open( void );
302 void asi_Reset( void );
303 int asi_SetFilter( uint16_t i_pid );
304 void asi_UnsetFilter( int i_fd, uint16_t i_pid );
306 #ifdef HAVE_ASI_DELTACAST_SUPPORT
307 void asi_deltacast_Open( void );
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 output_t *output_Find( const output_config_t *p_config );
335 void output_Change( output_t *p_output, const output_config_t *p_config );
336 void outputs_Init( void );
337 void outputs_Close( int i_num_outputs );
339 void comm_Open( void );
340 void comm_Close( void );
342 block_t *block_New( void );
343 void block_Delete( block_t *p_block );
344 void block_Vacuum( void );
346 /*****************************************************************************
347 * block_DeleteChain
348 *****************************************************************************/
349 static inline void block_DeleteChain( block_t *p_block )
351 while ( p_block != NULL )
353 block_t *p_next = p_block->p_next;
354 block_Delete( p_block );
355 p_block = p_next;