1 /*****************************************************************************
2 * input_ts.c: TS demux and netlist management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: input_ts.c,v 1.2 2001/02/14 15:58:29 henri Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
44 #include "stream_control.h"
45 #include "input_ext-intf.h"
46 #include "input_ext-dec.h"
51 #include "mpeg_system.h"
52 #include "input_netlist.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static int TSProbe ( probedata_t * );
58 static int TSRead ( struct input_thread_s *,
59 data_packet_t * p_packets[INPUT_READ_ONCE] );
60 static void TSInit ( struct input_thread_s * );
61 static void TSEnd ( struct input_thread_s * );
63 /*****************************************************************************
64 * Functions exported as capabilities. They are declared as static so that
65 * we don't pollute the namespace too much.
66 *****************************************************************************/
67 void input_getfunctions( function_list_t * p_function_list )
69 #define input p_function_list->functions.input
70 p_function_list->pf_probe = TSProbe;
71 input.pf_init = TSInit;
72 input.pf_open = input_FileOpen;
73 input.pf_close = input_FileClose;
75 input.pf_read = TSRead;
76 input.pf_demux = input_DemuxTS;
77 input.pf_new_packet = input_NetlistNewPacket;
78 input.pf_new_pes = input_NetlistNewPES;
79 input.pf_delete_packet = input_NetlistDeletePacket;
80 input.pf_delete_pes = input_NetlistDeletePES;
81 input.pf_rewind = NULL;
86 /*****************************************************************************
87 * TSProbe: verifies that the stream is a TS stream
88 *****************************************************************************/
89 static int TSProbe( probedata_t * p_data )
91 if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
96 /* verify that the first byte is 0x47 */
100 /*****************************************************************************
101 * TSInit: initializes TS structures
102 *****************************************************************************/
103 static void TSInit( input_thread_t * p_input )
105 /* Initialize netlist and TS structures */
106 thread_ts_data_t * p_method;
107 pgrm_ts_data_t * p_pgrm_demux;
108 es_descriptor_t * kludge1;
110 /* Initialise structure */
111 p_method = malloc( sizeof( thread_ts_data_t ) );
112 if( p_method == NULL )
114 intf_ErrMsg( "Out of memory" );
115 p_input->b_error = 1;
119 p_input->p_plugin_data = (void *)p_method;
120 p_input->p_method_data = NULL;
123 /* XXX : For the time being, only file, after, i'll do the network */
125 /* Initialize netlist */
126 if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE,
129 intf_ErrMsg( "Could not initialize netlist" );
133 /* Initialize the stream */
134 input_InitStream( p_input, sizeof( stream_ts_data_t ) );
136 /* FIXME : PSIDemux and PSIDecode */
137 /* Add audio and video programs */
138 /* p_input->stream.pp_programs[0] = */
139 input_AddProgram( p_input, 0, sizeof( pgrm_ts_data_t ) );
141 (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
142 p_pgrm_demux->i_pcr_pid = 0x78;
144 kludge1 = input_AddES( p_input, p_input->stream.pp_programs[0],
145 0x78, sizeof( es_ts_data_t ) );
148 kludge1->i_type = MPEG2_VIDEO_ES;
150 input_SelectES( p_input, kludge1 );
152 vlc_mutex_lock( &(p_input->stream.stream_lock) );
153 p_input->stream.pp_programs[0]->b_is_ok = 1;
154 vlc_mutex_unlock( &(p_input->stream.stream_lock) );
157 intf_ErrMsg("End of TSINIT");
160 /*****************************************************************************
161 * TSEnd: frees unused data
162 *****************************************************************************/
163 static void TSEnd( input_thread_t * p_input )
168 /*****************************************************************************
169 * TSRead: reads data packets
170 *****************************************************************************
171 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
173 *****************************************************************************/
174 static int TSRead( input_thread_t * p_input,
175 data_packet_t * pp_packets[INPUT_READ_ONCE] )
177 unsigned int i_read, i_loop;
178 struct iovec * p_iovec;
180 memset( pp_packets, 0, INPUT_READ_ONCE*sizeof(data_packet_t *) );
182 p_iovec = input_NetlistGetiovec( p_input->p_method_data );
184 if ( p_iovec == NULL )
186 return( -1 ); /* empty netlist */
189 i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
193 intf_ErrMsg( "Could not readv" );
197 input_NetlistMviovec( p_input->p_method_data,
198 (int)(i_read/TS_PACKET_SIZE) , pp_packets );
200 /* check correct TS header */
201 for( i_loop=0; i_loop < (int)(i_read/TS_PACKET_SIZE); i_loop++ )
203 if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
204 intf_ErrMsg( "Bad TS Packet (starcode != 0x47)." );