]> git.sesse.net Git - vlc/blob - src/input/input_netlist.c
Some comments for Henri.
[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     //On a besoin de p_buffers pour p_data. Il faut faire l'initialisation
69     //p_data->p_buffer = p_buffers + i * i_buffer_size
70     //p_data->p_payload_start = p_data->p_buffer
71     //p_data->p_payload_end = p_data->p_buffer + i_buffer_size
72     /* For the time being, I only handle pes and data. iovec is to come soon
73      * 
74     p_netlist->p_buffers = malloc(i_buffer_size*i_nb_data)
75     if ( p_netlist->p_buffers == NULL )
76     {
77         intf_ErrMsg ("Unable to malloc in netlist initialization (1)\n");
78         return (-1);
79     }
80     */
81     //Hum. Remplacer i_buffer_size par sizeof(data_packet_t) et rajouter
82     //un cast en (data_packet_t *) (c'est un buffer de data_packet_t, quand
83     //même.
84     //D'autre part le INPUT_READ_ONCE n'est là quand dans l'initialisation
85     //des iovec => à virer partout ailleurs.
86     p_netlist->p_data = 
87         malloc(i_buffer_size*(i_nb_data + INPUT_READ_ONCE));
88     if ( p_netlist->p_data == NULL )
89     {
90         intf_ErrMsg ("Unable to malloc in netlist initialization (2)\n");
91         return (-1);
92     }
93     //Pareil.
94     p_netlist->p_pes = 
95         malloc(i_buffer_size*(i_nb_pes + INPUT_READ_ONCE));
96     if ( p_netlist->p_pes == NULL )
97     {
98         intf_ErrMsg ("Unable to malloc in netlist initialization (3)\n");
99         return (-1);
100     }
101     //Il faut toujours caster la sortie du malloc (ça renvoie void * par
102     //défaut)
103     p_netlist->pp_free_data = 
104         malloc (i_nb_data * sizeof(data_packet_t *) );
105     if ( p_netlist->pp_free_data == NULL )
106     {
107         intf_ErrMsg ("Unable to malloc in netlist initialization (4)\n");
108     }
109     //i_nb_pes peut-être ?
110     p_netlist->pp_free_pes = 
111         malloc (i_nb_data * sizeof(pes_packet_t *) );
112     if ( p_netlist->pp_free_pes == NULL )
113     {
114         intf_ErrMsg ("Unable to malloc in netlist initialization (5)\n");
115     }
116     //p_free_iovec = malloc( (i_nb_data + INPUT_READ_ONCE) * sizeof(...) )
117
118     
119     /* Fill the data FIFO */
120     for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
121     {
122         p_netlist->pp_free_data[i_loop] = 
123             p_netlist->p_data + i_loop;
124         //manque l'initialisation de l'intérieur de p_data (cf. supra)
125     }
126     /* Fill the PES FIFO */
127     for ( i_loop = 0; i_loop < i_nb_pes + INPUT_READ_ONCE; i_loop++ )
128     {
129         p_netlist->pp_free_pes[i_loop] = 
130             p_netlist->p_pes + i_loop;
131     }
132     //p_free_iovec[i_loop].iov_base = p_buffers + i_loop * i_buffer_size
133     //p_free_iovec[i_loop].iov_len = i_buffer_size
134     /* vlc_mutex_init */
135     vlc_mutex_init (&p_netlist->lock);
136     
137     /* initialize indexes */
138     p_netlist->i_data_start = 0;
139     p_netlist->i_data_end = i_nb_data - 1;
140
141     p_netlist->i_pes_start = 0;
142     p_netlist->i_pes_end = i_nb_pes + INPUT_READ_ONCE - 1;
143
144     // p_netlist->i_iovec_start = 0;
145     // p_netlist->i_iovec_end = /* ?? */
146     //i_nb_data - 1
147     
148     p_netlist->i_nb_data = i_nb_data;
149     p_netlist->i_nb_pes = i_nb_pes;
150
151     return (0); /* Everything went all right */
152 }
153
154 /*****************************************************************************
155  * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
156  *****************************************************************************/
157 struct iovec * input_NetlistGetiovec( void * p_netlist )
158 {
159     /* fonction la plus difficile, terminer par celle-la 
160      * je la ferai plus tard :p */
161     //vérifier i_iovec_end - i_iovec_start > INPUT_READ_ONCE
162     //la grosse astuce :
163     //if( i_nb_data - i_iovec_start < INPUT_READ_ONCE )
164     //  memcpy( &p_free_iovec[i_nb_data], p_free_iovec, INPUT_READ_ONCE*... )
165     //return &p_free_iovec[i_iovec_start];
166     return ( NULL ); /* nothing yet */
167 }
168
169 //je rajoute celle-là
170 /*****************************************************************************
171  * input_NetlistMviovec: move the iovec pointer after a readv() operation
172  *****************************************************************************/
173 void input_NetlistMviovec( void * p_netlist, size_t i_nb_iovec )
174 {
175     //i_iovec_start += i_nb_iovec
176     //i_iovec_start %= i_nb_data //oui j'ai bien dit i_nb_data
177 }
178
179 /*****************************************************************************
180  * input_NetlistNewPacket: returns a free data_packet_t
181  *****************************************************************************/
182 struct data_packet_s * input_NetlistNewPacket( void * p_netlist )
183 {    
184     unsigned int i_return;
185     netlist_t * pt_netlist; /* for a cast */
186     //pas beau, pt_netlist, j'aurais préféré void * p_method_data et
187     //netlist_t * p_netlist
188     pt_netlist = ( netlist_t * ) p_netlist;
189     /* cast p_netlist -> netlist_t */
190
191     /* lock */
192     vlc_mutex_lock ( &pt_netlist->lock );
193         
194     /* check */
195     if ( pt_netlist->i_data_start == pt_netlist->i_data_end )
196     {
197         //empty peut-être ?
198         intf_ErrMsg("Full Data FIFO in netlist - Unable to allocate memory\n");
199         return ( NULL );
200     }
201     
202     i_return = (pt_netlist->i_data_start)++;
203     pt_netlist->i_data_start %= pt_netlist->i_nb_data;
204     //i_iovec_start++; i_iovec_start %= i_nb_data //oui j'ai bien dit i_nb_data
205
206     /* unlock */
207     vlc_mutex_unlock (&pt_netlist->lock);
208     
209     //risque de race condition : que se passe-t-il si après avoir rendu
210     //le lock un autre thread rend un paquet et écrase
211     //pp_free_data[i_return] ?
212     return ( pt_netlist->pp_free_data[i_return] );
213     //il faudrait aussi initialiser p_payload_start et p_payload_end
214 }
215
216 /*****************************************************************************
217  * input_NetlistNewPES: returns a free pes_packet_t
218  *****************************************************************************/
219 struct pes_packet_s * input_NetlistNewPES( void * p_netlist )
220 {
221     unsigned int i_return;
222     netlist_t * pt_netlist; /* for a cast */
223
224     //tout pareil qu'au-dessus
225     pt_netlist = (netlist_t *)p_netlist;
226     
227     /* lock */
228     vlc_mutex_lock ( &pt_netlist->lock );
229     
230     /* check */
231     if ( pt_netlist->i_pes_start == pt_netlist->i_pes_end )
232     {
233         intf_ErrMsg("Full PES FIFO in netlist - Unable to allocate memory\n");
234         return ( NULL );
235     }
236
237     i_return = (pt_netlist->i_pes_start)++;
238     pt_netlist->i_pes_start %= pt_netlist->i_nb_pes; 
239    
240     /* unlock */
241     vlc_mutex_unlock (&pt_netlist->lock);
242     
243     return ( pt_netlist->pp_free_pes[i_return] );
244     //il faudrait initialiser le pes :
245     //b_messed_up = b_data_alignment = b_discontinuity = b_has_pts = 0
246     //i_pes_size = 0
247     //p_first = NULL
248 }
249
250 /*****************************************************************************
251  * input_NetlistDeletePacket: puts a data_packet_t back into the netlist
252  *****************************************************************************/
253 void input_NetlistDeletePacket( void * p_netlist, data_packet_t * p_data )
254 {
255     netlist_t * pt_netlist; /* for a cast */
256
257     pt_netlist = (netlist_t *) p_netlist;
258
259     /* lock */
260     vlc_mutex_lock ( &pt_netlist->lock );
261
262     pt_netlist->i_data_end ++;
263     pt_netlist->i_data_end %= pt_netlist->i_nb_data;
264     //i_iovec_end++; i_iovec_end %= i_nb_data //oui j'ai bien dit i_nb_data
265     //p_free_iovec[i_iovec_end].iov_base = p_data->p_buffer
266     
267     pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_data;
268
269     /* unlock */
270     vlc_mutex_unlock (&pt_netlist->lock);    
271 }
272
273 /*****************************************************************************
274  * input_NetlistDeletePES: puts a pes_packet_t back into the netlist
275  *****************************************************************************/
276 void input_NetlistDeletePES( void * p_netlist, pes_packet_t * p_pes )
277 {
278     /* idem, plus detruire tous les data_packet_t dans p_pes->p_first,
279      * p_pes->p_first->p_next, etc. */
280     netlist_t * pt_netlist; /* for a cast */
281     data_packet_t * p_current_packet;
282     
283     pt_netlist = (netlist_t *)p_netlist;
284
285      /* lock */
286     vlc_mutex_lock ( &pt_netlist->lock );
287
288     /* free  p_pes->p_first, p_next ... */
289     p_current_packet = p_pes->p_first;
290     while ( p_current_packet != NULL )
291     {
292         /* copy of NetListDeletePacket 
293          * Duplicate code avoid many locks */
294         pt_netlist->i_data_end ++;
295         pt_netlist->i_data_end %= pt_netlist->i_nb_data;
296         //i_iovec_end++; i_iovec_end %= i_nb_data //oui j'ai bien dit i_nb_data
297         //p_free_iovec[i_iovec_end].iov_base = p_data->p_buffer
298     
299         pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_current_packet;
300
301         p_current_packet = p_current_packet->p_next;
302     }
303     
304     pt_netlist->i_pes_end ++;
305     pt_netlist->i_pes_end %= pt_netlist->i_nb_pes;
306
307     pt_netlist->pp_free_pes[pt_netlist->i_pes_end] = p_pes;
308     
309     /* unlock */
310     vlc_mutex_unlock (&pt_netlist->lock);
311 }
312
313 /*****************************************************************************
314  * input_NetlistEnd: frees all allocated structures
315  *****************************************************************************/
316 void input_NetlistEnd( input_thread_t * p_input)
317 {
318     netlist_t * p_netlist; /* for a cast */
319
320     p_netlist = ( netlist_t * ) p_input->p_method_data;
321     
322     /* free the FIFO, the buffer, and the netlist structure */
323     free (p_netlist->pp_free_data);
324     free (p_netlist->pp_free_pes);
325     free (p_netlist->p_pes);
326     free (p_netlist->p_data);
327
328     free (p_netlist);
329 }
330 //sinon c'est bien (c)