1 /*****************************************************************************
2 * input_netlist.c: netlist management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
6 * Authors: Henri Fallon <henri@videolan.org>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
32 #include "threads.h" /* mutex */
34 #include "intf_msg.h" /* intf_*Msg */
36 #include "stream_control.h"
37 #include "input_ext-intf.h"
38 #include "input_ext-dec.h"
40 #include "input_netlist.h"
43 /*****************************************************************************
45 *****************************************************************************/
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 )
54 netlist_t * p_netlist; /* for a cast */
56 /* First we allocate and initialise our netlist struct */
57 p_input->p_method_data = malloc(sizeof(netlist_t));
60 if ( p_input->p_method_data == NULL )
62 intf_ErrMsg("Unable to malloc the netlist struct\n");
66 p_netlist = (netlist_t *) p_input->p_method_data;
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
74 p_netlist->p_buffers = malloc(i_buffer_size*i_nb_data)
75 if ( p_netlist->p_buffers == NULL )
77 intf_ErrMsg ("Unable to malloc in netlist initialization (1)\n");
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
84 //D'autre part le INPUT_READ_ONCE n'est là quand dans l'initialisation
85 //des iovec => à virer partout ailleurs.
87 malloc(i_buffer_size*(i_nb_data + INPUT_READ_ONCE));
88 if ( p_netlist->p_data == NULL )
90 intf_ErrMsg ("Unable to malloc in netlist initialization (2)\n");
95 malloc(i_buffer_size*(i_nb_pes + INPUT_READ_ONCE));
96 if ( p_netlist->p_pes == NULL )
98 intf_ErrMsg ("Unable to malloc in netlist initialization (3)\n");
101 //Il faut toujours caster la sortie du malloc (ça renvoie void * par
103 p_netlist->pp_free_data =
104 malloc (i_nb_data * sizeof(data_packet_t *) );
105 if ( p_netlist->pp_free_data == NULL )
107 intf_ErrMsg ("Unable to malloc in netlist initialization (4)\n");
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 )
114 intf_ErrMsg ("Unable to malloc in netlist initialization (5)\n");
116 //p_free_iovec = malloc( (i_nb_data + INPUT_READ_ONCE) * sizeof(...) )
119 /* Fill the data FIFO */
120 for ( i_loop = 0; i_loop < i_nb_data; i_loop++ )
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)
126 /* Fill the PES FIFO */
127 for ( i_loop = 0; i_loop < i_nb_pes + INPUT_READ_ONCE; i_loop++ )
129 p_netlist->pp_free_pes[i_loop] =
130 p_netlist->p_pes + i_loop;
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
135 vlc_mutex_init (&p_netlist->lock);
137 /* initialize indexes */
138 p_netlist->i_data_start = 0;
139 p_netlist->i_data_end = i_nb_data - 1;
141 p_netlist->i_pes_start = 0;
142 p_netlist->i_pes_end = i_nb_pes + INPUT_READ_ONCE - 1;
144 // p_netlist->i_iovec_start = 0;
145 // p_netlist->i_iovec_end = /* ?? */
148 p_netlist->i_nb_data = i_nb_data;
149 p_netlist->i_nb_pes = i_nb_pes;
151 return (0); /* Everything went all right */
154 /*****************************************************************************
155 * input_NetlistGetiovec: returns an iovec pointer for a readv() operation
156 *****************************************************************************/
157 struct iovec * input_NetlistGetiovec( void * p_netlist )
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
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 */
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 )
175 //i_iovec_start += i_nb_iovec
176 //i_iovec_start %= i_nb_data //oui j'ai bien dit i_nb_data
179 /*****************************************************************************
180 * input_NetlistNewPacket: returns a free data_packet_t
181 *****************************************************************************/
182 struct data_packet_s * input_NetlistNewPacket( void * p_netlist )
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 */
192 vlc_mutex_lock ( &pt_netlist->lock );
195 if ( pt_netlist->i_data_start == pt_netlist->i_data_end )
198 intf_ErrMsg("Full Data FIFO in netlist - Unable to allocate memory\n");
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
207 vlc_mutex_unlock (&pt_netlist->lock);
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
216 /*****************************************************************************
217 * input_NetlistNewPES: returns a free pes_packet_t
218 *****************************************************************************/
219 struct pes_packet_s * input_NetlistNewPES( void * p_netlist )
221 unsigned int i_return;
222 netlist_t * pt_netlist; /* for a cast */
224 //tout pareil qu'au-dessus
225 pt_netlist = (netlist_t *)p_netlist;
228 vlc_mutex_lock ( &pt_netlist->lock );
231 if ( pt_netlist->i_pes_start == pt_netlist->i_pes_end )
233 intf_ErrMsg("Full PES FIFO in netlist - Unable to allocate memory\n");
237 i_return = (pt_netlist->i_pes_start)++;
238 pt_netlist->i_pes_start %= pt_netlist->i_nb_pes;
241 vlc_mutex_unlock (&pt_netlist->lock);
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
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 )
255 netlist_t * pt_netlist; /* for a cast */
257 pt_netlist = (netlist_t *) p_netlist;
260 vlc_mutex_lock ( &pt_netlist->lock );
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
267 pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_data;
270 vlc_mutex_unlock (&pt_netlist->lock);
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 )
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;
283 pt_netlist = (netlist_t *)p_netlist;
286 vlc_mutex_lock ( &pt_netlist->lock );
288 /* free p_pes->p_first, p_next ... */
289 p_current_packet = p_pes->p_first;
290 while ( p_current_packet != NULL )
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
299 pt_netlist->pp_free_data[pt_netlist->i_data_end] = p_current_packet;
301 p_current_packet = p_current_packet->p_next;
304 pt_netlist->i_pes_end ++;
305 pt_netlist->i_pes_end %= pt_netlist->i_nb_pes;
307 pt_netlist->pp_free_pes[pt_netlist->i_pes_end] = p_pes;
310 vlc_mutex_unlock (&pt_netlist->lock);
313 /*****************************************************************************
314 * input_NetlistEnd: frees all allocated structures
315 *****************************************************************************/
316 void input_NetlistEnd( input_thread_t * p_input)
318 netlist_t * p_netlist; /* for a cast */
320 p_netlist = ( netlist_t * ) p_input->p_method_data;
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);
330 //sinon c'est bien (c)