1 /*****************************************************************************
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 *****************************************************************************/
25 #include <sys/socket.h>
26 #include <netinet/udp.h>
27 #include <netinet/ip.h>
31 /* Defines for pid mapping */
33 /* Offsets in the command line args for the pid mapping */
36 I_PMTPID
= 0, I_APID
, I_VPID
, I_SPUPID
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 ...
52 #if defined(__FreeBSD__) || defined(__APPLE__)
55 unsigned int version
:4;
70 struct udpheader udph
;
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
];
103 struct block_t
*p_next
;
106 typedef struct packet_t packet_t
;
108 typedef struct output_config_t
112 struct sockaddr_storage connect_addr
;
113 struct sockaddr_storage bind_addr
;
117 char *psz_displayname
;
121 char *psz_service_name
;
122 char *psz_service_provider
;
124 mtime_t i_output_latency
, i_max_retention
;
128 char *psz_srcaddr
; /* raw packets */
133 uint16_t i_sid
; /* 0 if raw mode */
138 /* for pidmap from config file */
140 uint16_t pi_confpids
[N_MAP_PIDS
];
143 typedef struct output_t
145 output_config_t config
;
149 packet_t
*p_packets
, *p_last_packet
;
151 mtime_t i_ref_timestamp
;
152 mtime_t i_ref_wallclock
;
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
;
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
;
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 */
185 1 = Reserved for future use
186 2 = Scrambled with even key
187 3 = Scrambled with odd key */
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
;
201 extern char *psz_delsys
;
202 extern int i_dvr_buffer_size
;
203 extern int i_frequency
;
206 extern int i_uncommitted
;
208 extern int i_rolloff
;
209 extern int i_voltage
;
211 extern int i_bandwidth
;
212 extern int i_inversion
;
213 extern char *psz_modulation
;
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
;
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 /*****************************************************************************
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 );
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
, ... );
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
);
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 /*****************************************************************************
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;
353 /*****************************************************************************
355 *****************************************************************************/
356 static inline void block_Delete( block_t
*p_block
)
361 /*****************************************************************************
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
;