]> git.sesse.net Git - vlc/blob - modules/access/dvb/access.c
Refactoring of dvb.c and access.c. Coding style and messages cleanup.
[vlc] / modules / access / dvb / access.c
1 /*****************************************************************************
2  * access.c: DVB card input v4l2 only
3  *****************************************************************************
4  * Copyright (C) 1998-2003 VideoLAN
5  *
6  * Authors: Johan Bilien <jobi@via.ecp.fr>
7  *          Jean-Paul Saman <jpsaman@wxs.nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33
34 #include "../../demux/mpeg/system.h"
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #endif
39
40 #include <fcntl.h>
41 #include <sys/types.h>
42
43 #ifdef HAVE_ERRNO_H
44 #    include <string.h>
45 #    include <errno.h>
46 #endif
47
48 #ifdef STRNCASECMP_IN_STRINGS_H
49 #   include <strings.h>
50 #endif
51
52 /* DVB Card Drivers */
53 #include <linux/dvb/dmx.h>
54 #include <linux/dvb/frontend.h>
55
56 #include "dvb.h"
57
58 #define SATELLITE_READ_ONCE 3
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
64                               size_t i_len);
65 static int     SatelliteSetArea    ( input_thread_t *, input_area_t * );
66 static int     SatelliteSetProgram ( input_thread_t *, pgrm_descriptor_t * );
67 static void    SatelliteSeek       ( input_thread_t *, off_t );
68
69 /*****************************************************************************
70  * Open: open the frontend device
71  *****************************************************************************/
72 int E_(Open) ( vlc_object_t *p_this )
73 {
74     struct dvb_frontend_info frontend_info;
75     struct dvb_frontend_parameters fep;
76     input_thread_t *    p_input = (input_thread_t *)p_this;    
77     input_dvb_t *       p_dvb;
78     char *              psz_parser;
79     char *              psz_next;
80     int                 i_fd = 0;
81     char                dvr[] = DVR;
82     char                frontend[] = FRONTEND;
83     int                 i_len = 0;
84     vlc_value_t         val;
85     int                 i_test;
86
87     /* parse the options passed in command line : */
88     psz_parser = strdup( p_input->psz_name );
89     if( !psz_parser )
90     {
91         return( -1 );
92     }
93
94     msg_Dbg(p_input, "method of access is %s", p_input->psz_access);
95
96     /* Initialise structure */
97     p_dvb = malloc( sizeof( input_dvb_t ) );
98
99     if( p_dvb == NULL )
100     {
101         msg_Err( p_input, "out of memory" );
102         return -1;
103     }
104
105     p_input->p_access_data = (void *) p_dvb;
106
107     /* Get adapter and device number to use for this dvb card */
108     p_dvb->u_adapter = config_GetInt( p_input, "adapter" );
109     p_dvb->u_device  = config_GetInt( p_input, "device" );
110     p_dvb->b_probe   = config_GetInt( p_input, "probe" );
111   
112     /* Get antenna configuration options */
113     p_dvb->b_diseqc   = config_GetInt( p_input, "diseqc" );
114     p_dvb->u_lnb_lof1 = config_GetInt( p_input, "lnb-lof1" );
115     p_dvb->u_lnb_lof2 = config_GetInt( p_input, "lnb-lof2" );
116     p_dvb->u_lnb_slof = config_GetInt( p_input, "lnb-slof" );
117
118     /* Get modulation parameters */
119     p_dvb->i_bandwidth    = config_GetInt( p_input, "bandwidth");
120     p_dvb->i_code_rate_HP = config_GetInt(p_input, "code-rate-hp");
121     p_dvb->i_code_rate_LP = config_GetInt(p_input, "code-rate-lp");
122     p_dvb->i_modulation   = config_GetInt(p_input, "modulation");
123     p_dvb->i_transmission = config_GetInt(p_input, "transmission");
124     p_dvb->i_guard        = config_GetInt(p_input, "guard");
125     p_dvb->i_hierarchy    = config_GetInt(p_input, "hierarchy");
126
127     /* Register Callback functions */
128     p_input->pf_read = SatelliteRead;
129     p_input->pf_set_program = SatelliteSetProgram;
130     p_input->pf_set_area = SatelliteSetArea;
131     p_input->pf_seek = SatelliteSeek;
132
133     /* Parse commandline */
134     i_test = strtol( psz_parser, &psz_next, 10 );
135     if( psz_next == psz_parser )
136     {
137         for ( ;; )
138         {
139             if( !strncmp( psz_parser, "frequency=",
140                                strlen( "frequency=" ) ) )
141             {
142                 p_dvb->u_freq =
143                 (unsigned int)strtol( psz_parser + strlen( "frequency=" ),
144                             &psz_parser, 0 );
145             }
146             else if( !strncmp( psz_parser, "polarization=",
147                                strlen( "polarization=" ) ) )
148             {
149                 char *psz_parser_init;
150                 psz_parser += strlen( "polarization=" );
151                 psz_parser_init = psz_parser;
152                 while ( *psz_parser != ':')
153                 {
154                    psz_parser++;
155                 }
156                 if( (!strncmp( psz_parser_init, "V" ,
157                      psz_parser - psz_parser_init ) ) || 
158                    (!strncmp( psz_parser_init, "V" ,
159                      psz_parser - psz_parser_init ) ) )
160                 {
161                     p_dvb->i_polarisation = VLC_FALSE;
162                 }
163                 else if( (!strncmp( psz_parser_init, "H" ,
164                      psz_parser - psz_parser_init ) ) ||
165                    (!strncmp( psz_parser_init, "h" ,
166                      psz_parser - psz_parser_init ) ) )
167
168                 {
169                     p_dvb->i_polarisation = VLC_TRUE;
170                 }
171                 else if( (!strncmp( psz_parser_init, "A" ,
172                      psz_parser - psz_parser_init ) ) ||
173                    (!strncmp( psz_parser_init, "a" ,
174                      psz_parser - psz_parser_init ) ) )
175
176                 {
177                     p_dvb->i_polarisation = 2;
178                 }
179             }
180             else if( !strncmp( psz_parser, "fec=",
181                                strlen( "fec=" ) ) )
182             {
183                 p_dvb->i_fec =
184                 (int)strtol( psz_parser + strlen( "fec=" ),
185                             &psz_parser, 0 );
186             }
187             else if( !strncmp( psz_parser, "srate=",
188                                strlen( "srate=" ) ) )
189             {
190                 p_dvb->u_srate =
191                 (unsigned int)strtol( psz_parser + strlen( "srate=" ),
192                             &psz_parser, 0 );
193             }
194             else if( !strncmp( psz_parser, "program=",
195                                strlen( "program=" ) ) )
196             {
197                 val.i_int = (int)strtol( psz_parser + strlen( "program=" ),
198                             &psz_parser, 0 );
199                 var_Set( p_input, "program", val );
200             }
201             else if( !strncmp( psz_parser, "diseqc",
202                                strlen( "disecq" ) ) )
203             {
204                 psz_parser += strlen("disecq");
205                 p_dvb->b_diseqc = VLC_TRUE;
206             }
207             else if( !strncmp( psz_parser, "lnb-lof1=",
208                                strlen( "lnb-lof1=" ) ) )
209             {
210                 p_dvb->u_lnb_lof1 =
211                 (unsigned int)strtol( psz_parser + strlen( "lnb-lof1=" ),
212                             &psz_parser, 0 );                
213                 frontend_info.frequency_min = p_dvb->u_lnb_lof1; /* lnb_lof1 */
214             }
215             else if( !strncmp( psz_parser, "lnb-lof2=",
216                                strlen( "lnb-lof2=" ) ) )
217             {
218                 p_dvb->u_lnb_lof2 =
219                 (unsigned int)strtol( psz_parser + strlen( "lnb-lof2=" ),
220                             &psz_parser, 0 );
221                 frontend_info.frequency_max = p_dvb->u_lnb_lof2; /* in KHz, lnb_lof2 */
222             }
223             else if( !strncmp( psz_parser, "lnb-slof=",
224                                strlen( "lnb-slof=" ) ) )
225             {
226                 p_dvb->u_lnb_slof =
227                 (unsigned int)strtol( psz_parser + strlen( "lnb-slof=" ),
228                             &psz_parser, 0 );
229             }
230             else if( !strncmp( psz_parser, "device=",
231                                strlen( "device=" ) ) )
232             {
233                 p_dvb->u_device =
234                 (unsigned int)strtol( psz_parser + strlen( "device=" ),
235                             &psz_parser, 0 );
236             }
237             else if( !strncmp( psz_parser, "adapter=",
238                                strlen( "adapter=" ) ) )
239             {
240                 p_dvb->u_adapter =
241                 (unsigned int)strtol( psz_parser + strlen( "adapter=" ),
242                             &psz_parser, 0 );
243             }
244             else if( !strncmp( psz_parser, "modulation=",
245                                strlen( "modulation=" ) ) )
246             {
247                 p_dvb->i_modulation = (int)strtol( psz_parser + strlen( "modulation=" ),
248                             &psz_parser, 0 );
249             }
250             else if( !strncmp( psz_parser, "bandwidth=",
251                                strlen( "bandwidth=" ) ) )
252             {
253                 p_dvb->i_bandwidth = (int)strtol( psz_parser + strlen( "bandwidth=" ),
254                             &psz_parser, 0 );
255             }
256             else if( !strncmp( psz_parser, "guard=",
257                                strlen( "guard=" ) ) )
258             {
259                 p_dvb->i_guard = (int)strtol( psz_parser + strlen( "guard=" ),
260                             &psz_parser, 0 );
261             }
262             else if( !strncmp( psz_parser, "transmission=",
263                                strlen( "transmission=" ) ) )
264             {
265                 p_dvb->i_transmission = (int)strtol( psz_parser + 
266                             strlen( "transmission=" ),&psz_parser, 0 );
267             }
268             else if( !strncmp( psz_parser, "hierarchy=",
269                                strlen( "hierarchy=" ) ) )
270             {
271                 p_dvb->i_hierarchy = (int)strtol( psz_parser + 
272                                 strlen( "hierarchy=" ),&psz_parser, 0 );
273             }
274             else if( !strncmp( psz_parser, "code-rate-HP=",
275                                strlen( "code-rate-HP=" ) ) )
276             {
277                 p_dvb->i_code_rate_HP = (int)strtol( psz_parser + 
278                                 strlen( "code-rate-HP=" ),&psz_parser, 0 );
279             }
280             else if( !strncmp( psz_parser, "code-rate-LP=",
281                                strlen( "code-rate-LP=" ) ) )
282             {
283                 p_dvb->i_code_rate_LP = (int)strtol( psz_parser +
284                                 strlen( "code-rate-LP=" ),&psz_parser, 0 );
285             }
286             else if( !strncmp( psz_parser, "probe",
287                                strlen( "probe" ) ) )
288             {
289                 psz_parser += strlen("probe");
290                 p_dvb->b_probe = VLC_TRUE;
291             }
292             if( *psz_parser )
293                 psz_parser++;
294             else
295                 break;
296         }
297     }
298     else
299     {
300         msg_Err(p_input, "DVB Input old syntax deprecreated");
301         free( p_dvb );
302         return -1;
303     }
304
305     /* Determine frontend device */
306     i_len = sizeof(FRONTEND);
307     if( snprintf( frontend, sizeof(FRONTEND), FRONTEND, p_dvb->u_adapter, p_dvb->u_device ) >= i_len )
308     {
309         msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
310         frontend[sizeof(FRONTEND)] = '\0';
311     }
312
313     msg_Dbg( p_input, "Opening device %s", frontend );
314     if( ( p_dvb->i_frontend = open( frontend, O_RDWR )) < 0 )
315     {
316 #   ifdef HAVE_ERRNO_H
317         msg_Err( p_input, "Opening device failed (%s)", strerror( errno ));
318 #   else
319         msg_Err( p_input, "Opening device failed");
320 #   endif
321         close( p_dvb->i_frontend );
322         free( p_dvb );
323         return -1;
324     }
325     
326     /* Determine frontend device information and capabilities */        
327     if( p_dvb->b_probe )
328     {
329         if( ioctl_InfoFrontend(p_input, &frontend_info, p_dvb->u_adapter, p_dvb->u_device) < 0 )
330         {
331             msg_Err( p_input, "(access) cannot determine frontend info" );
332             close( p_dvb->i_frontend );
333             free( p_dvb );
334             return -1;
335         }
336     }
337     else /* no frontend probing is done so use default border values. */
338     {
339         msg_Dbg( p_input, "using default values for frontend info" );
340         strncpy(frontend_info.name, frontend, 128);
341
342         frontend_info.type = FE_QPSK;
343         if( (strncmp( p_input->psz_access, "qpsk", 4) ==0) ||
344              (strncmp( p_input->psz_access, "dvb-s", 5 ) == 0) ||
345              (strncmp( p_input->psz_access, "satellite", 9 ) == 0) )
346             frontend_info.type = FE_QPSK;
347         else if( (strncmp( p_input->psz_access, "cable", 5) ==0) ||
348                   (strncmp( p_input->psz_access, "dvb-c", 5 ) == 0) )
349             frontend_info.type = FE_QAM;
350         else if( (strncmp( p_input->psz_access, "terrestrial", 11) ==0) ||
351                   (strncmp( p_input->psz_access, "dvb-t", 5 ) == 0) )
352             frontend_info.type = FE_OFDM;
353
354         frontend_info.frequency_max = p_dvb->u_lnb_lof2; /* in KHz, lnb_lof2 */
355         frontend_info.frequency_min = p_dvb->u_lnb_lof1; /* lnb_lof1 */
356
357         frontend_info.symbol_rate_max = 30000000;
358         frontend_info.symbol_rate_min =  1000000;
359     }
360
361     /* Sanity check */
362     if( ((strncmp( p_input->psz_access, "qpsk", 4 ) == 0) ||
363          (strncmp( p_input->psz_access, "dvb-s", 5 ) == 0) ||
364          (strncmp( p_input->psz_access, "satellite", 9 ) == 0) ) &&
365          (frontend_info.type != FE_QPSK) )
366     {
367         if( frontend_info.type == FE_OFDM )
368             msg_Err(p_input, "User expects DVB-S card but DVB-T card found.");
369         else if( frontend_info.type == FE_QAM )
370             msg_Err(p_input, "User expects DVB-S card but DVB-C card found.");
371         else msg_Err(p_input, "User expects DVB-S card but unknown card found.");
372
373         close( p_dvb->i_frontend );
374         free( p_dvb );        
375         return -1;
376     }
377     if( ((strncmp( p_input->psz_access, "cable", 5 ) == 0) ||
378          (strncmp( p_input->psz_access, "dvb-c", 5 ) == 0) ) &&
379          (frontend_info.type != FE_QAM) )
380     {
381         if( frontend_info.type == FE_OFDM )
382             msg_Err( p_input, "User expects DVB-C card but DVB-T card found." );
383         else if( frontend_info.type == FE_QPSK )
384             msg_Err( p_input, "User expects DVB-C card but DVB-S card found." );
385         else msg_Err( p_input, "User expects DVB-C card but unknown card found." );
386
387         close( p_dvb->i_frontend );
388         free( p_dvb );
389         return -1;
390     }
391     if( ((strncmp( p_input->psz_access, "terrestrial", 11 ) == 0) ||
392          (strncmp( p_input->psz_access, "dvb-t", 5 ) == 0) ) &&
393          (frontend_info.type != FE_OFDM) )
394     {
395         if( frontend_info.type == FE_QAM )
396             msg_Err(p_input, "User expects DVB-T card but DVB-C card found.");
397         else if( frontend_info.type == FE_QPSK )
398             msg_Err(p_input, "User expects DVB-T card but DVB-S card found.");
399         else msg_Err(p_input, "User expects DVB-T card but unknown card found.");
400
401         close( p_dvb->i_frontend );
402         free( p_dvb );
403         return -1;
404     }
405    
406 #if 0
407     /* Validating input values (QPSK in KHz, OFDM/QAM in Hz) */
408     if( ((p_dvb->u_freq) > frontend_info.frequency_max) ||
409         ((p_dvb->u_freq) < frontend_info.frequency_min) )
410     {
411         if( (p_dvb->u_freq) > frontend_info.frequency_max )
412             msg_Err( p_input, "given frequency %u (kHz) > %u (kHz) max. frequency",
413                      p_dvb->u_freq, frontend_info.frequency_max );
414         else
415             msg_Err( p_input, "given frequency %u (kHz) < %u (kHz) min.frequency",
416                      p_dvb->u_freq, frontend_info.frequency_min );
417         msg_Err( p_input, "bailing out given frequency outside specification range for this frontend" );
418
419         close( p_dvb->i_frontend );
420         free( p_dvb );
421         return -1;
422     }
423 #endif
424
425     /* Workaround for backwards compatibility */
426     if( strncmp( p_input->psz_access, "satellite", 9 ) ==0 )
427     {
428         msg_Warn( p_input, "invalid symbol rate %d possibly specified in MHz, trying value *1000 KHz", p_dvb->u_srate );
429         p_dvb->u_srate *= 1000UL;
430     }
431     
432     if( ((p_dvb->u_srate) > frontend_info.symbol_rate_max) ||
433         ((p_dvb->u_srate) < frontend_info.symbol_rate_min) )
434     {
435         msg_Warn( p_input, "invalid symbol rate, using default one" );
436         p_dvb->u_srate = config_GetInt( p_input, "symbol-rate" );
437         if( ((p_dvb->u_srate) > frontend_info.symbol_rate_max) ||
438             ((p_dvb->u_srate) < frontend_info.symbol_rate_min) )
439         {
440             msg_Err( p_input, "invalid default symbol rate" );
441
442             close( p_dvb->i_frontend );
443             free( p_dvb );
444             return -1;
445         }
446     }
447
448     if( (p_dvb->i_fec > 9) || (p_dvb->i_fec < 1) )
449     {
450         msg_Warn( p_input, "invalid FEC, using default one" );
451         p_dvb->i_fec = config_GetInt( p_input, "fec" );
452         if( (p_dvb->i_fec > 9) || (p_dvb->i_fec < 1) )
453         {
454             msg_Err( p_input, "invalid default FEC" );
455             close( p_dvb->i_frontend );
456             free( p_dvb );
457             return -1;
458         }
459     }
460
461     /* Setting frontend parameters for tuning the hardware */      
462     msg_Dbg( p_input, "Trying to tune to channel ...");
463     switch( frontend_info.type )
464     {
465         /* DVB-S: satellite and budget cards (nova) */
466         case FE_QPSK:
467             fep.frequency = p_dvb->u_freq; /* KHz */
468             fep.inversion = dvb_DecodeInversion( p_input, p_dvb->i_polarisation );
469             fep.u.qpsk.symbol_rate = p_dvb->u_srate;
470             fep.u.qpsk.fec_inner = dvb_DecodeFEC( p_input, p_dvb->i_fec ); 
471             msg_Dbg( p_input, "DVB-S: satellite (QPSK) frontend %s found", frontend_info.name );
472
473             if( ioctl_SetQPSKFrontend( p_input, fep, p_dvb->i_polarisation,
474                                p_dvb->u_lnb_lof1, p_dvb->u_lnb_lof2, p_dvb->u_lnb_slof,
475                                p_dvb->u_adapter, p_dvb->u_device ) < 0 )
476             {
477                 msg_Err( p_input, "DVB-S: tuning failed" );
478                 close( p_dvb->i_frontend );
479                 free( p_dvb );
480                 return -1;
481             }
482             break;
483             
484         /* DVB-C */
485         case FE_QAM:
486             fep.frequency = p_dvb->u_freq; /* in Hz */
487             fep.inversion = dvb_DecodeInversion( p_input, p_dvb->i_polarisation );
488             fep.u.qam.symbol_rate = p_dvb->u_srate;
489             fep.u.qam.fec_inner = dvb_DecodeFEC( p_input, p_dvb->i_fec ); 
490             fep.u.qam.modulation = dvb_DecodeModulation( p_input, p_dvb->i_modulation ); 
491             msg_Dbg( p_input, "DVB-C: cable (QAM) frontend %s found", frontend_info.name );
492             if( ioctl_SetQAMFrontend( p_input, fep, p_dvb->u_adapter, p_dvb->u_device ) < 0 )
493             {
494                 msg_Err( p_input, "DVB-C: tuning failed" );
495                 close( p_dvb->i_frontend );
496                 free( p_dvb );
497                 return -1;
498             }
499             break;
500
501         /* DVB-T */
502         case FE_OFDM:
503             fep.frequency = p_dvb->u_freq; /* in Hz */
504             fep.inversion = dvb_DecodeInversion( p_input, p_dvb->i_polarisation );
505             fep.u.ofdm.bandwidth = dvb_DecodeBandwidth( p_input, p_dvb->i_bandwidth );
506             fep.u.ofdm.code_rate_HP = dvb_DecodeFEC( p_input, p_dvb->i_code_rate_HP ); 
507             fep.u.ofdm.code_rate_LP = dvb_DecodeFEC( p_input, p_dvb->i_code_rate_LP );
508             fep.u.ofdm.constellation = dvb_DecodeModulation( p_input, p_dvb->i_modulation ); 
509             fep.u.ofdm.transmission_mode = dvb_DecodeTransmission( p_input, p_dvb->i_transmission );
510             fep.u.ofdm.guard_interval = dvb_DecodeGuardInterval( p_input, p_dvb->i_guard );
511             fep.u.ofdm.hierarchy_information = dvb_DecodeHierarchy( p_input, p_dvb->i_hierarchy );
512             msg_Dbg( p_input, "DVB-T: terrestrial (OFDM) frontend %s found", frontend_info.name );
513             if( ioctl_SetOFDMFrontend( p_input, fep, p_dvb->u_adapter, p_dvb->u_device ) < 0 )
514             {
515                 msg_Err( p_input, "DVB-T: tuning failed" );
516                 close( p_dvb->i_frontend );
517                 free( p_dvb );
518                 return -1;
519             }
520             break;
521         default:
522             msg_Err( p_input, "Could not determine frontend type on %s", frontend_info.name );
523             close( p_dvb->i_frontend );
524             free( p_dvb );
525             return -1;
526     }
527     msg_Dbg( p_input, "Tuning done.");
528
529     /* Initialise structure */
530     p_dvb->p_satellite = malloc( sizeof( input_socket_t ) );
531
532     if( p_dvb->p_satellite == NULL )
533     {
534         msg_Err( p_input, "out of memory" );
535         close( p_dvb->i_frontend );
536         free( p_dvb );
537         return -1;
538     }
539
540     /* Open the DVR device */
541     i_len = sizeof(DVR);
542     if( snprintf( dvr, sizeof(DVR), DVR, p_dvb->u_adapter, p_dvb->u_device ) >= i_len )
543     {
544         msg_Err( p_input, "snprintf() truncated string for DVR" );
545         dvr[sizeof(DVR)] = '\0';
546     }
547     msg_Dbg( p_input, "opening DVR device '%s'", dvr );
548
549     if( (p_dvb->p_satellite->i_handle = open( dvr,
550                                    /*O_NONBLOCK | O_LARGEFILE*/0 ) ) == (-1) )
551     {
552 #   ifdef HAVE_ERRNO_H
553         msg_Warn( p_input, "cannot open `%s' (%s)", dvr, strerror( errno ) );
554 #   else
555         msg_Warn( p_input, "cannot open `%s'", dvr );
556 #   endif
557         free( p_dvb->p_satellite );
558
559         close( p_dvb->i_frontend );
560         free( p_dvb );
561         return -1;
562     }
563
564     msg_Dbg( p_input, "setting filter on PAT" );
565
566     /* Set Filter on PAT packet */
567     if( ioctl_SetDMXFilter(p_input, 0, &i_fd, 21, p_dvb->u_adapter, p_dvb->u_device ) < 0 )
568     {
569 #   ifdef HAVE_ERRNO_H
570         msg_Err( p_input, "an error occured when setting filter on PAT (%s)", strerror( errno ) );
571 #   else
572         msg_Err( p_input, "an error occured when setting filter on PAT" );
573 #   endif        
574         close( p_dvb->p_satellite->i_handle );
575         free( p_dvb->p_satellite );
576
577         close( p_dvb->i_frontend );
578         free( p_dvb );
579         return -1;
580     }
581
582     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
583     {
584         msg_Err( p_input, "could not initialize stream structure" );
585         close( p_dvb->p_satellite->i_handle );
586         free( p_dvb->p_satellite );
587
588         close( p_dvb->i_frontend );
589         free( p_dvb );
590         return( -1 );
591     }
592
593     vlc_mutex_lock( &p_input->stream.stream_lock );
594
595     p_input->stream.b_pace_control = 1;
596     p_input->stream.b_seekable = 0;
597     p_input->stream.p_selected_area->i_tell = 0;
598
599     vlc_mutex_unlock( &p_input->stream.stream_lock );
600
601     p_input->i_mtu = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
602     p_input->stream.i_method = INPUT_METHOD_SATELLITE;
603
604     return 0;
605 }
606
607 /*****************************************************************************
608  * Close : Close the device
609  *****************************************************************************/
610 void E_(Close) ( vlc_object_t *p_this )
611 {
612     input_thread_t *    p_input = (input_thread_t *)p_this;
613     input_dvb_t *       p_dvb;
614     unsigned int        i_es_index;
615     
616     if( p_input->stream.p_selected_program )
617     {
618         for( i_es_index = 1 ;
619              i_es_index < p_input->stream.p_selected_program->i_es_number;
620              i_es_index ++ )
621         {
622 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
623             if( p_es->p_dec )
624             {
625                 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
626             }
627 #undef p_es
628         }
629     }
630
631     p_dvb = (input_dvb_t *)p_input->p_access_data;
632
633     close( p_dvb->p_satellite->i_handle );
634     free( p_dvb->p_satellite );
635     
636     close( p_dvb->i_frontend );
637     free( p_dvb );
638 }
639
640 /*****************************************************************************
641  * SatelliteRead: reads data from the satellite card
642  *****************************************************************************/
643 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
644                               size_t i_len )
645 {
646     input_dvb_t * p_dvb = (input_dvb_t *)p_input->p_access_data;
647     ssize_t i_ret;
648     unsigned int i;
649
650     /* if not set, set filters to the PMTs */
651     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
652     {
653         if( p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd == 0 )
654         {
655             ioctl_SetDMXFilter( p_input, p_input->stream.pp_programs[i]->pp_es[0]->i_id,
656                        &p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd,
657                        21, p_dvb->u_adapter, p_dvb->u_device );
658         }
659     }
660
661     i_ret = read( p_dvb->p_satellite->i_handle, p_buffer, i_len );
662  
663     if( i_ret < 0 )
664     {
665 #   ifdef HAVE_ERRNO_H
666         msg_Err( p_input, "read failed (%s)", strerror(errno) );
667 #   else
668         msg_Err( p_input, "read failed" );
669 #   endif
670     }
671
672     return i_ret;
673 }
674
675 /*****************************************************************************
676  * SatelliteSetArea : Does nothing
677  *****************************************************************************/
678 static int SatelliteSetArea( input_thread_t * p_input, input_area_t * p_area )
679 {
680     return -1;
681 }
682
683 /*****************************************************************************
684  * SatelliteSetProgram : Sets the card filters according to the
685  *                 selected program,
686  *                 and makes the appropriate changes to stream structure.
687  *****************************************************************************/
688 int SatelliteSetProgram( input_thread_t    * p_input,
689                          pgrm_descriptor_t * p_new_prg )
690 {
691     input_dvb_t * p_dvb = (input_dvb_t *)p_input->p_access_data;
692     unsigned int i_es_index;
693     vlc_value_t val;
694     unsigned int u_video_type = 1; /* default video type */
695     unsigned int u_audio_type = 2; /* default audio type */
696
697     if( p_input->stream.p_selected_program )
698     {
699         for( i_es_index = 1 ; /* 0 should be the PMT */
700                 i_es_index < p_input->stream.p_selected_program->i_es_number ;
701                 i_es_index ++ )
702         {
703 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
704             if( p_es->p_dec )
705             {
706                 input_UnselectES( p_input , p_es );
707             }
708             if( p_es->i_demux_fd > 0 )
709             {
710                 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
711                 p_es->i_demux_fd = 0;
712             }
713 #undef p_es
714         }
715     }
716
717     for (i_es_index = 1 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
718     {
719 #define p_es p_new_prg->pp_es[i_es_index]
720         switch( p_es->i_cat )
721         {
722             case MPEG1_VIDEO_ES:
723             case MPEG2_VIDEO_ES:
724             case MPEG2_MOTO_VIDEO_ES:
725                 if( input_SelectES( p_input , p_es ) == 0 )
726                 {
727                     ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_video_type,
728                                        p_dvb->u_adapter, p_dvb->u_device);
729                     u_video_type += 5;
730                 }
731                 break;
732             case MPEG1_AUDIO_ES:
733             case MPEG2_AUDIO_ES:
734                 if( input_SelectES( p_input , p_es ) == 0 )
735                 {
736                     ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_audio_type,
737                                        p_dvb->u_adapter, p_dvb->u_device);
738                     input_SelectES( p_input , p_es );
739                     u_audio_type += 5;
740                 }
741                 break;
742             default:
743                 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, 21, p_dvb->u_adapter, p_dvb->u_device);
744                 input_SelectES( p_input , p_es );
745                 msg_Warn(p_input, "ES streamtype 0x%d found used as DMX_PES_OTHER !!",(int) p_es->i_cat);
746                 break;
747 #undef p_es
748         }
749     }
750
751     p_input->stream.p_selected_program = p_new_prg;
752
753     /* Update the navigation variables without triggering a callback */
754     val.i_int = p_new_prg->i_number;
755     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
756
757     return 0;
758 }
759
760 /*****************************************************************************
761  * SatelliteSeek: does nothing (not a seekable stream
762  *****************************************************************************/
763 static void SatelliteSeek( input_thread_t * p_input, off_t i_off )
764 {
765     ;
766 }
767