]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/mpeg_ts.c
* ./extras/MacOSX_dvdioctl: removed outdated files.
[vlc] / plugins / mpeg_system / mpeg_ts.c
1 /*****************************************************************************
2  * mpeg_ts.c : Transport Stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: mpeg_ts.c,v 1.7 2002/03/18 19:14:52 sam Exp $
6  *
7  * Authors: Henri Fallon <henri@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <videolan/vlc.h>
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36 #include "input_ext-plugins.h"
37
38 /*****************************************************************************
39  * Constants
40  *****************************************************************************/
41 #define TS_READ_ONCE 200
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static void input_getfunctions( function_list_t * p_function_list );
47 static int  TSInit      ( struct input_thread_s * );
48 static void TSEnd       ( struct input_thread_s * );
49 static int  TSDemux     ( struct input_thread_s * );
50
51 /*****************************************************************************
52  * Build configuration tree.
53  *****************************************************************************/
54 MODULE_CONFIG_START
55 MODULE_CONFIG_STOP
56
57 MODULE_INIT_START
58     SET_DESCRIPTION( "ISO 13818-1 MPEG Transport Stream input" )
59     ADD_CAPABILITY( DEMUX, 160 )
60     ADD_SHORTCUT( "ts" )
61 MODULE_INIT_STOP
62
63 MODULE_ACTIVATE_START
64     input_getfunctions( &p_module->p_functions->demux );
65 MODULE_ACTIVATE_STOP
66
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
69
70 /*****************************************************************************
71  * Functions exported as capabilities. They are declared as static so that
72  * we don't pollute the namespace too much.
73  *****************************************************************************/
74 static void input_getfunctions( function_list_t * p_function_list )
75 {
76 #define input p_function_list->functions.demux
77     input.pf_init             = TSInit;
78     input.pf_end              = TSEnd;
79     input.pf_demux            = TSDemux;
80     input.pf_rewind           = NULL;
81 #undef input
82 }
83
84 /*****************************************************************************
85  * TSInit: initializes TS structures
86  *****************************************************************************/
87 static int TSInit( input_thread_t * p_input )
88 {
89     es_descriptor_t     * p_pat_es;
90     es_ts_data_t        * p_demux_data;
91     stream_ts_data_t    * p_stream_data;
92     byte_t              * p_peek;
93
94     /* Initialize access plug-in structures. */
95     if( p_input->i_mtu == 0 )
96     {
97         /* Improve speed. */
98         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
99     }
100
101     /* Have a peep at the show. */
102     if( input_Peek( p_input, &p_peek, 1 ) < 1 )
103     {
104         intf_ErrMsg( "input error: cannot peek() (mpeg_ts)" );
105         return( -1 );
106     }
107
108     if( *p_peek != TS_SYNC_CODE )
109     {
110         if( *p_input->psz_demux && !strncmp( p_input->psz_demux, "ts", 3 ) )
111         {
112             /* User forced */
113             intf_ErrMsg( "input error: this doesn't look like a TS stream, continuing" );
114         }
115         else
116         {
117             intf_WarnMsg( 2, "input: TS plug-in discarded (no sync)" );
118             return( -1 );
119         }
120     }
121
122     /* Adapt the bufsize for our only use. */
123     if( p_input->i_mtu != 0 )
124     {
125         /* Have minimum granularity to avoid bottlenecks at the input level. */
126         p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
127     }
128
129     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
130     {
131         return( -1 );
132     }
133
134     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
135     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
136
137     /* We'll have to catch the PAT in order to continue
138      * Then the input will catch the PMT and then the others ES
139      * The PAT es is indepedent of any program. */
140     p_pat_es = input_AddES( p_input, NULL,
141                             0x00, sizeof( es_ts_data_t ) );
142     p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
143     p_demux_data->b_psi = 1;
144     p_demux_data->i_psi_type = PSI_IS_PAT;
145     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
146     p_demux_data->p_psi_section->b_is_complete = 1;
147
148     return( 0 );
149 }
150
151 /*****************************************************************************
152  * TSEnd: frees unused data
153  *****************************************************************************/
154 static void TSEnd( input_thread_t * p_input )
155 {
156 }
157
158 /*****************************************************************************
159  * TSDemux: reads and demuxes data packets
160  *****************************************************************************
161  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
162  * packets.
163  *****************************************************************************/
164 static int TSDemux( input_thread_t * p_input )
165 {
166     int             i_read_once = (p_input->i_mtu ?
167                                    p_input->i_bufsize / TS_PACKET_SIZE :
168                                    TS_READ_ONCE);
169     int             i;
170
171     for( i = 0; i < i_read_once; i++ )
172     {
173         data_packet_t *     p_data;
174         ssize_t             i_result;
175
176         i_result = input_ReadTS( p_input, &p_data );
177
178         if( i_result <= 0 )
179         {
180             return( i_result );
181         }
182
183         input_DemuxTS( p_input, p_data );
184     }
185
186     return( i_read_once );
187 }
188