1 /*****************************************************************************
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 *****************************************************************************/
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>
34 # define container_of(ptr, type, member) ({ \
35 const typeof( ((type *)0)->member ) *_mptr = (ptr); \
36 (type *)( (char *)_mptr - offsetof(type,member) );})
39 /* Defines for pid mapping */
41 /* Offsets in the command line args for the pid mapping */
44 I_PMTPID
= 0, I_APID
, I_VPID
, I_SPUPID
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 ...
60 #if defined(__FreeBSD__) || defined(__APPLE__)
63 unsigned int version
:4;
78 struct udpheader udph
;
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
];
111 struct block_t
*p_next
;
114 typedef struct packet_t packet_t
;
116 typedef struct dvb_string_t
122 typedef struct output_config_t
126 struct sockaddr_storage connect_addr
;
127 struct sockaddr_storage bind_addr
;
131 char *psz_displayname
;
135 uint16_t i_network_id
;
136 dvb_string_t network_name
;
137 dvb_string_t service_name
;
138 dvb_string_t provider_name
;
140 mtime_t i_output_latency
, i_max_retention
;
144 char *psz_srcaddr
; /* raw packets */
149 uint16_t i_sid
; /* 0 if raw mode */
156 /* for pidmap from config file */
158 uint16_t pi_confpids
[N_MAP_PIDS
];
161 typedef struct output_t
163 output_config_t config
;
167 packet_t
*p_packets
, *p_last_packet
;
168 packet_t
*p_packet_lifo
;
169 unsigned int i_packet_count
;
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
;
186 /* incomplete PID (only PCR packets) */
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
;
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 */
205 1 = Reserved for future use
206 2 = Scrambled with even key
207 3 = Scrambled with odd key */
210 extern struct ev_loop
*event_loop
;
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
;
221 extern char *psz_delsys
;
222 extern int i_dvr_buffer_size
;
223 extern int i_frequency
;
226 extern int i_uncommitted
;
228 extern int i_rolloff
;
229 extern int i_voltage
;
231 extern int i_bandwidth
;
232 extern int i_inversion
;
233 extern char *psz_modulation
;
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
;
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 /*****************************************************************************
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 );
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
, ... );
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
);
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 /*****************************************************************************
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
);