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