File: input_ts.c

package info (click to toggle)
vlc 0.2.92-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,076 kB
  • ctags: 7,147
  • sloc: ansic: 62,829; cpp: 5,824; sh: 2,469; xml: 2,351; makefile: 1,291; python: 503; perl: 19
file content (349 lines) | stat: -rw-r--r-- 11,017 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*****************************************************************************
 * input_ts.c: TS demux and netlist management
 *****************************************************************************
 * Copyright (C) 1998-2001 VideoLAN
 * $Id: input_ts.c,v 1.42.2.1 2001/12/31 01:21:45 massiot Exp $
 *
 * Authors: Henri Fallon <henri@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#define MODULE_NAME ts
#include "modules_inner.h"

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifdef STRNCASECMP_IN_STRINGS_H
#   include <strings.h>
#endif

#include <sys/types.h>

#if !defined( _MSC_VER )
#   include <sys/time.h>
#endif

#ifdef SYS_NTO
#   include <sys/select.h>
#endif

#include <sys/stat.h>

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif

#include <fcntl.h>

#if defined( WIN32 )
#   include <io.h>
#   include <winsock2.h>
#else
#   include <sys/uio.h>                                      /* struct iovec */
#endif

#include "common.h"
#include "intf_msg.h"
#include "threads.h"
#include "mtime.h"
#include "tests.h"

#if defined( WIN32 )
#   include "input_iovec.h"
#endif

#include "modules.h"
#include "modules_export.h"

#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"
#include "input_ext-plugins.h"

#include "input_ts.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  TSProbe     ( probedata_t * );
static void TSInit      ( struct input_thread_s * );
static void TSEnd       ( struct input_thread_s * );
static int  TSRead      ( struct input_thread_s *,
                          data_packet_t ** );

/*****************************************************************************
 * Declare a buffer manager
 *****************************************************************************/
#define FLAGS           BUFFERS_UNIQUE_SIZE
#define NB_LIFO         1
DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
DECLARE_BUFFERS_TOIO( FLAGS, TS_PACKET_SIZE );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
void _M( input_getfunctions )( function_list_t * p_function_list )
{
#define input p_function_list->functions.input
    p_function_list->pf_probe = TSProbe;
    input.pf_init             = TSInit;
    input.pf_open             = NULL;
    input.pf_close            = NULL;
    input.pf_end              = TSEnd;
    input.pf_init_bit_stream  = InitBitstream;
    input.pf_set_area         = NULL;
    input.pf_set_program      = input_SetProgram;
    input.pf_read             = TSRead;
    input.pf_demux            = input_DemuxTS;
    input.pf_new_packet       = input_NewPacket;
    input.pf_new_pes          = input_NewPES;
    input.pf_delete_packet    = input_DeletePacket;
    input.pf_delete_pes       = input_DeletePES;
    input.pf_rewind           = NULL;
    input.pf_seek             = NULL;
#undef input
}

/*****************************************************************************
 * TSProbe: verifies that the stream is a TS stream
 *****************************************************************************/
static int TSProbe( probedata_t * p_data )
{
    input_thread_t * p_input = (input_thread_t *)p_data;

    char * psz_name = p_input->p_source;
    int i_score = 2;

    if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
    {
        return( 999 );
    }

    if( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
            || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
    {
        /* If the user specified "udp:" then it's probably a network stream */
        return( 999 );
    }

    if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
    {
        /* If the user specified "file:" then it's probably a file */
        psz_name += 5;
    }

    if( ( strlen(psz_name) > 3 ) &&
                    !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
    {
        /* If it is a ".ts" file it's probably a TS file ... */
        return( 900 );
    }

    return( i_score );
}

/*****************************************************************************
 * TSInit: initializes TS structures
 *****************************************************************************/
static void TSInit( input_thread_t * p_input )
{
    thread_ts_data_t    * p_method;
    es_descriptor_t     * p_pat_es;
    es_ts_data_t        * p_demux_data;
    stream_ts_data_t    * p_stream_data;

    /* Initialise structure */
    p_method = malloc( sizeof( thread_ts_data_t ) );
    if( p_method == NULL )
    {
        intf_ErrMsg( "TS input : Out of memory" );
        p_input->b_error = 1;
        return;
    }

#if defined( WIN32 )
    p_method->i_length = 0;
    p_method->i_offset = 0;
#endif

    p_input->p_plugin_data = (void *)p_method;
    p_input->p_method_data = NULL;


    if( (p_input->p_method_data = input_BuffersInit()) == NULL )
    {
        p_input->b_error = 1;
        return;
    }

    /* Initialize the stream */
    input_InitStream( p_input, sizeof( stream_ts_data_t ) );

    p_input->stream.p_selected_area->i_tell = 0;

    /* Init */
    p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
    p_stream_data->i_pat_version = PAT_UNINITIALIZED ;

    /* We'll have to catch the PAT in order to continue
     * Then the input will catch the PMT and then the others ES
     * The PAT es is indepedent of any program. */
    p_pat_es = input_AddES( p_input, NULL,
                           0x00, sizeof( es_ts_data_t ) );
    p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
    p_demux_data->b_psi = 1;
    p_demux_data->i_psi_type = PSI_IS_PAT;
    p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
    p_demux_data->p_psi_section->b_is_complete = 1;

}

/*****************************************************************************
 * TSEnd: frees unused data
 *****************************************************************************/
static void TSEnd( input_thread_t * p_input )
{
    es_descriptor_t     * p_pat_es;

    p_pat_es = input_FindES( p_input, 0x00 );

    if( p_pat_es != NULL )
        input_DelES( p_input, p_pat_es );

    free(p_input->p_plugin_data);
    input_BuffersEnd( p_input->p_method_data );
}

/*****************************************************************************
 * TSRead: reads data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
 * packets.
 *****************************************************************************/
static int TSRead( input_thread_t * p_input,
                   data_packet_t ** pp_data )
{
    thread_ts_data_t    * p_method;
    int             i_read = 0, i_loop;
    int             i_data = 1;
    struct iovec    p_iovec[TS_READ_ONCE];
    data_packet_t * p_data;
    struct timeval  timeout;

    /* Init */
    p_method = ( thread_ts_data_t * )p_input->p_plugin_data;

    /* Initialize file descriptor set */
    FD_ZERO( &(p_method->fds) );
    FD_SET( p_input->i_handle, &(p_method->fds) );

    /* We'll wait 0.5 second if nothing happens */
    timeout.tv_sec = 0;
    timeout.tv_usec = 500000;

    /* Fill if some data is available */
#if defined( WIN32 )
    if ( ! p_input->stream.b_pace_control ) 
#endif
    {
        i_data = select( p_input->i_handle + 1, &p_method->fds,
                         NULL, NULL, &timeout );
    }

    if( i_data == -1 )
    {
        intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
        return( -1 );
    }
    
    if( i_data )
    {
        /* Get iovecs */
        *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_iovec,
                                               TS_READ_ONCE );

        if ( p_data == NULL )
        {
            return( -1 );
        }

#if defined( WIN32 )
        if( p_input->stream.b_pace_control )
        {
            i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
        }
        else
        {
            i_read = readv_network( p_input->i_handle, p_iovec,
                                    TS_READ_ONCE, p_method );
        }
#else
        i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );

        /* Shouldn't happen, but it does - at least under Linux */
        if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
        {
            /* just ignore that error */
            intf_ErrMsg( "input error: 0 bytes read" );
            i_read = 0;
        }
#endif
        /* Error */
        if( i_read == -1 )
        {
            intf_ErrMsg( "input error: TS readv error" );
            p_input->pf_delete_packet( p_input->p_method_data, p_data );
            return( -1 );
        }
        p_input->stream.p_selected_area->i_tell += i_read;
        i_read /= TS_PACKET_SIZE;

        /* Check correct TS header */
        for( i_loop = 0; i_loop < i_read; i_loop++ )
        {
            if( (*pp_data)->p_demux_start[0] != 0x47 )
            {
                intf_ErrMsg( "input error: bad TS packet (starts with "
                             "0x%.2x, should be 0x47)",
                             p_data->p_demux_start[0] );
            }
            pp_data = &(*pp_data)->p_next;
        }

        if( i_read != TS_READ_ONCE )
        {
            /* Delete remaining packets */
            p_input->pf_delete_packet( p_input->p_method_data, *pp_data );
            *pp_data = NULL;
        }
    }
    return( i_read );
}