]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/input_ts.h
* Changed pf_read prototype and minor changes.
[vlc] / plugins / mpeg_system / 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.4 2001/12/27 03:47:09 massiot 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 /* UDP packets contain 1500 bytes, that is 7 TS packets */
26 #define TS_READ_ONCE 7
27
28 #ifdef WIN32
29 #   define BUFFER_SIZE (7 * TS_PACKET_SIZE)
30 #endif
31
32 /*****************************************************************************
33  * thread_ts_data_t: private input data
34  *****************************************************************************/
35 typedef struct thread_ts_data_s
36
37     /* The file descriptor we select() on */
38     fd_set fds;
39     
40 #if defined( WIN32 )
41     char p_buffer[ BUFFER_SIZE ];      /* temporary buffer for readv_network */
42     int  i_length;                               /* length of the UDP packet */
43     int  i_offset;           /* number of bytes already read from the buffer */
44 #endif
45     
46 } thread_ts_data_t;
47
48 /*****************************************************************************
49  * network readv() replacement for iovec-impaired C libraries
50  *****************************************************************************/
51 #if defined(WIN32)
52 static __inline__ int read_network( int i_fd, char * p_base,
53                                     thread_ts_data_t *p_sys, int i_len )
54 {
55     int i_bytes;
56
57     if( p_sys->i_offset >= p_sys->i_length )
58     {
59         p_sys->i_length = recv( i_fd, p_sys->p_buffer, BUFFER_SIZE, 0 );
60         if ( p_sys->i_length == SOCKET_ERROR )
61         {
62             return -1;
63         }
64         p_sys->i_offset = 0;
65     }
66
67     if( i_len <= p_sys->i_length - p_sys->i_offset )
68     {
69          i_bytes = i_len;
70     }
71     else
72     {
73          i_bytes = p_sys->i_length - p_sys->i_offset;
74     }
75
76     p_main->fast_memcpy( p_base, p_sys->p_buffer + p_sys->i_offset, i_bytes );
77     p_sys->i_offset += i_bytes;
78
79     return i_bytes;
80 }
81
82 static __inline__ int readv_network( int i_fd, struct iovec *p_iovec,
83                                      int i_count, thread_ts_data_t *p_sys )
84 {
85     int i_index, i_len, i_total = 0;
86     u8 *p_base;
87
88     for( i_index = i_count; i_index; i_index-- )
89     {
90         register signed int i_bytes;
91
92         i_len  = p_iovec->iov_len;
93         p_base = p_iovec->iov_base;
94
95         /* Loop is unrolled one time to spare the (i_bytes <= 0) test */
96         if( i_len > 0 )
97         {
98             i_bytes = read_network( i_fd, p_base, p_sys, i_len );
99
100             if( ( i_total == 0 ) && ( i_bytes < 0 ) )
101             {
102                 return -1;
103             }
104
105             if( i_bytes <= 0 )
106             {
107                 return i_total;
108             }
109
110             i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
111
112             while( i_len > 0 )
113             {
114                 i_bytes = read_network( i_fd, p_base, p_sys, i_len );
115
116                 if( i_bytes <= 0 )
117                 {
118                     return i_total;
119                 }
120
121                 i_len -= i_bytes; i_total += i_bytes; p_base += i_bytes;
122             }
123         }
124
125         p_iovec++;
126     }
127
128     return i_total;
129 }
130 #endif
131