]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ts.h
* Win32 network support by Boris Dor�s <babal@via.ecp.fr>.
[vlc] / plugins / mpeg / input_ts.h
1 /*****************************************************************************
2  * input_ts.h: structures of the input not exported to other modules
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_ts.h,v 1.9 2001/06/21 07:22:03 sam Exp $
6  *
7  * Authors: Henri Fallon <henri@via.ecp.fr>
8  *          Boris Dorès <babal@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define NB_DATA 16384 
26 #define NB_PES  8192
27
28 #define BUFFER_SIZE (7 * TS_PACKET_SIZE)
29
30 /*****************************************************************************
31  * thread_ts_data_t: private input data
32  *****************************************************************************/
33 typedef struct thread_ts_data_s
34
35     /* The file descriptor we select() on */
36     fd_set fds;
37     
38 #if defined( WIN32 )
39     char p_buffer[ BUFFER_SIZE ];      /* temporary buffer for readv_network */
40     int  i_length;                               /* length of the UDP packet */
41     int  i_offset;           /* number of bytes already read from the buffer */
42 #endif
43     
44 } thread_ts_data_t;
45
46 /*****************************************************************************
47  * readv_*: readv() replacements for iovec-impaired C libraries
48  *****************************************************************************/
49
50 #if defined( WIN32 )
51 static __inline__ int readv_file( int i_fd, struct iovec *p_iovec, int i_count )
52 {
53     int i_index, i_len, i_total = 0;
54     u8 *p_base;
55
56     for( i_index = i_count; i_index; i_index-- )
57     {
58         register signed int i_bytes;
59
60         i_len  = p_iovec->iov_len;
61         p_base = p_iovec->iov_base;
62
63         /* Loop is unrolled one time to spare the (i_bytes <= 0) test */
64
65         if( i_len > 0 )
66         {
67             i_bytes = read( i_fd, p_base, i_len );
68
69             if( ( i_total == 0 ) && ( i_bytes < 0 ) )
70             {
71                 return -1;
72             }
73
74             if( i_bytes <= 0 )
75             {
76                 return i_total;
77             }
78
79             i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
80
81             while( i_len > 0 )
82             {
83                 i_bytes = read( i_fd, p_base, i_len );
84
85                 if( i_bytes <= 0 )
86                 {
87                     return i_total;
88                 }
89
90                 i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
91             }
92         }
93
94         p_iovec++;
95     }
96
97     return i_total;
98 }
99
100 static __inline__ int read_network( int i_fd, char * p_base,
101                                     thread_ts_data_t *p_sys, int i_len )
102 {
103     int i_bytes;
104
105     if( p_sys->i_offset >= p_sys->i_length )
106     {
107         p_sys->i_length = recv( i_fd, p_sys->p_buffer, BUFFER_SIZE, 0 );
108         if ( p_sys->i_length == SOCKET_ERROR )
109         {
110             return -1;
111         }
112         p_sys->i_offset = 0;
113     }
114
115     if( i_len <= p_sys->i_length - p_sys->i_offset )
116     {
117          i_bytes = i_len;
118     }
119     else
120     {
121          i_bytes = p_sys->i_length - p_sys->i_offset;
122     }
123
124     memcpy( p_base, p_sys->p_buffer + p_sys->i_offset, i_bytes );
125     p_sys->i_offset += i_bytes;
126
127     return i_bytes;
128 }
129
130 static __inline__ int readv_network( int i_fd, struct iovec *p_iovec,
131                                      int i_count, thread_ts_data_t *p_sys )
132 {
133     int i_index, i_len, i_total = 0;
134     u8 *p_base;
135
136     for( i_index = i_count; i_index; i_index-- )
137     {
138         register signed int i_bytes;
139
140         i_len  = p_iovec->iov_len;
141         p_base = p_iovec->iov_base;
142
143         /* Loop is unrolled one time to spare the (i_bytes <= 0) test */
144         if( i_len > 0 )
145         {
146             i_bytes = read_network( i_fd, p_base, p_sys, i_len );
147
148             if( ( i_total == 0 ) && ( i_bytes < 0 ) )
149             {
150                 return -1;
151             }
152
153             if( i_bytes <= 0 )
154             {
155                 return i_total;
156             }
157
158             i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
159
160             while( i_len > 0 )
161             {
162                 i_bytes = read_network( i_fd, p_base, p_sys, i_len );
163
164                 if( i_bytes <= 0 )
165                 {
166                     return i_total;
167                 }
168
169                 i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
170             }
171         }
172
173         p_iovec++;
174     }
175
176     return i_total;
177 }
178 #endif
179