]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
Netlist embryo :
[vlc] / src / input / input_netlist.c
1 /*****************************************************************************
2  * input_netlist.c: netlist management
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  *
6  * Authors: Henri Fallon <henri@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <stdlib.h>
29
30 #include "config.h"
31 #include "common.h"
32 #include "threads.h"                                                /* mutex */
33 #include "mtime.h"
34 #include "intf_msg.h"                                           /* intf_*Msg */
35
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38 #include "input_ext-dec.h"
39
40 #include "input_netlist.h"
41 #include "input.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46
47 /*****************************************************************************
48  * input_NetlistInit: allocates netlist buffers and init indexes
49  *****************************************************************************/
50 int input_NetlistInit( input_thread_t * p_input, int i_nb_data, int i_nb_pes,
51                        size_t i_buffer_size )
52 {
53     unsigned int i_loop;
54     netlist_t * p_netlist; /* for a cast */
55
56     /* First we allocate and initialise our netlist struct */
57     p_input->p_method_data = malloc(sizeof(netlist_t));
58     
59     
60     if ( p_input->p_method_data == NULL )
61     {
62         intf_ErrMsg("Unable to malloc the netlist struct\n");
63         return (-1);
64     }
65     
66     p_netlist = (netlist_t *) p_input->p_method_data;
67     
68     /* For the time being, I only handle pes and data. iovec is to come soon
69      * 
70     p_netlist->p_buffers = malloc(i_buffer_size*i_nb_data)
71     if ( p_netlist->p_buffers == NULL )
72     {
73         intf_ErrMsg ("Unable to malloc in netlist initialization (1)\n");
74         return (-1);
75     }
76     */
77     p_netlist->p_data = 
78         malloc(i_buffer_size*(i_nb_data + INPUT_READ_ONCE));
79     if ( p_netlist->p_data == NULL )
80     {
81         intf_ErrMsg ("Unable to malloc in netlist initialization (2)\n");
82         return (-1);
83     }
84     p_netlist->p_pes = 
85         malloc(i_buffer_size*(i_nb_pes + INPUT_READ_ONCE));
86     if ( p_netlist->p_pes == NULL )
87     {
88         intf_ErrMsg ("Unable to malloc in netlist initialization (3)\n");
89         return (-1);
90     }
91     p_netlist->pp_free_data = 
92         malloc (i_nb_data * sizeof(data_packet_t *) );
93     if ( p_netlist->pp_free_data == NULL )
94     {
95         intf_ErrMsg ("Unable to malloc in netlist initialization (4)\n");
96     }
97     p_netlist->pp_free_pes = 
98         malloc (i_nb_data * sizeof(pes_packet_t *) );
99     if ( p_netlist->pp_free_pes == NULL )
100     {
101         intf_ErrMsg ("Unable to malloc in netlist initialization (5)\n");
102     }
103
104     
105     /* Fill the data FIFO */
106     for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
107     {
108         p_netlist->pp_free_data[i_loop] = 
109             p_netlist->p_data + i_loop;
110     }
111     /* Fill the PES FIFO */
112     for ( i_loop = 0; i_loop < i_nb_pes + INPUT_READ_ONCE; i_loop++ )
113     {
114         p_netlist->pp_free_pes[i_loop] = 
115             p_netlist->p_pes + i_loop;
116     }
117     /* vlc_mutex_init */
118     vlc_mutex_init (&p_netlist->lock);
119     
120     /* initialize indexes */
121     p_netlist->i_data_start = 0;
122     p_netlist->i_data_end = i_nb_data - 1;
123
124     p_netlist->i_pes_start = 0;
125     p_netlist->i_pes_end = i_nb_pes + INPUT_READ_ONCE - 1;
126
127     // p_netlist->i_iovec_start = 0;
128     // p_netlist->i_iovec_end = /* ?? */
129     
130     p_netlist->i_nb_data = i_nb_data;
131     p_netlist->i_nb_pes = i_nb_pes;
132
133     return (0); /* Everything went all right */
134 }
135
136 /*****************************************************************************
137  * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
138  *****************************************************************************/
139 struct iovec * input_NetlistGetiovec( void * p_netlist )
140 {
141     /* fonction la plus difficile, terminer par celle-la 
142      * je la ferai plus tard :p */
143     return ( NULL ); /* nothing yet */
144 }
145
146 /*****************************************************************************
147  * input_NetlistNewPacket: returns a free data_packet_t
148  *****************************************************************************/
149 struct data_packet_s * input_NetlistNewPacket( void * p_netlist )
150 {    
151     unsigned int i_return;
152     netlist_t * pt_netlist; /* for a cast */
153
154     pt_netlist = ( netlist_t * ) p_netlist;
155     /* cast p_netlist -> netlist_t */
156
157     /* lock */
158     vlc_mutex_lock ( &pt_netlist->lock );
159         
160     /* check */
161     if ( pt_netlist->i_data_start == pt_netlist->i_data_end )
162     {
163         intf_ErrMsg("Full Data FIFO in netlist - Unable to allocate memory\n");
164         return ( NULL );
165     }
166     
167     i_return = (pt_netlist->i_data_start)++;
168     pt_netlist->i_data_start %= pt_netlist->i_nb_data;
169     
170     /* unlock */
171     vlc_mutex_unlock (&pt_netlist->lock);
172     
173     return ( pt_netlist->pp_free_data[i_return] );
174 }
175
176 /*****************************************************************************
177  * input_NetlistNewPES: returns a free pes_packet_t
178  *****************************************************************************/
179 struct pes_packet_s * input_NetlistNewPES( void * p_netlist )
180 {
181     unsigned int i_return;
182     netlist_t * pt_netlist; /* for a cast */
183
184     pt_netlist = (netlist_t *)p_netlist;
185     
186     /* lock */
187     vlc_mutex_lock ( &pt_netlist->lock );
188     
189     /* check */
190     if ( pt_netlist->i_pes_start == pt_netlist->i_pes_end )
191     {
192         intf_ErrMsg("Full PES FIFO in netlist - Unable to allocate memory\n");
193         return ( NULL );
194     }
195
196     i_return = (pt_netlist->i_pes_start)++;
197     pt_netlist->i_pes_start %= pt_netlist->i_nb_pes; 
198    
199     /* unlock */
200     vlc_mutex_unlock (&pt_netlist->lock);
201     
202     return ( pt_netlist->pp_free_pes[i_return] );
203 }
204
205 /*****************************************************************************
206  * input_NetlistDeletePacket: puts a data_packet_t back into the netlist
207  *****************************************************************************/
208 void input_NetlistDeletePacket( void * p_netlist, data_packet_t * p_data )
209 {
210     netlist_t * pt_netlist; /* for a cast */
211
212     pt_netlist = (netlist_t *) p_netlist;
213
214     /* lock */
215     vlc_mutex_lock ( &pt_netlist->lock );
216
217     pt_netlist->i_data_end ++;
218     pt_netlist->i_data_end %= pt_netlist->i_nb_data;
219     
220     pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_data;
221
222     /* unlock */
223     vlc_mutex_unlock (&pt_netlist->lock);    
224 }
225
226 /*****************************************************************************
227  * input_NetlistDeletePES: puts a pes_packet_t back into the netlist
228  *****************************************************************************/
229 void input_NetlistDeletePES( void * p_netlist, pes_packet_t * p_pes )
230 {
231     /* idem, plus detruire tous les data_packet_t dans p_pes->p_first,
232      * p_pes->p_first->p_next, etc. */
233     netlist_t * pt_netlist; /* for a cast */
234     data_packet_t * p_current_packet;
235     
236     pt_netlist = (netlist_t *)p_netlist;
237
238      /* lock */
239     vlc_mutex_lock ( &pt_netlist->lock );
240
241     /* free  p_pes->p_first, p_next ... */
242     p_current_packet = p_pes->p_first;
243     while ( p_current_packet != NULL )
244     {
245         /* copy of NetListDeletePacket 
246          * Duplicate code avoid many locks */
247         pt_netlist->i_data_end ++;
248         pt_netlist->i_data_end %= pt_netlist->i_nb_data;
249     
250         pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_current_packet;
251
252         p_current_packet = p_current_packet->p_next;
253     }
254     
255     pt_netlist->i_pes_end ++;
256     pt_netlist->i_pes_end %= pt_netlist->i_nb_pes;
257
258     pt_netlist->pp_free_pes[pt_netlist->i_pes_end] = p_pes;
259     
260     /* unlock */
261     vlc_mutex_unlock (&pt_netlist->lock);
262 }
263
264 /*****************************************************************************
265  * input_NetlistEnd: frees all allocated structures
266  *****************************************************************************/
267 void input_NetlistEnd( input_thread_t * p_input)
268 {
269     netlist_t * p_netlist; /* for a cast */
270
271     p_netlist = ( netlist_t * ) p_input->p_method_data;
272     
273     /* free the FIFO, the buffer, and the netlist structure */
274     free (p_netlist->pp_free_data);
275     free (p_netlist->pp_free_pes);
276     free (p_netlist->p_pes);
277     free (p_netlist->p_data);
278
279     free (p_netlist);
280 }
281