]> git.sesse.net Git - vlc/blob - modules/access/dvb/access.c
Fixed long problem with tuning to frequencies like; 12669500 Hz which is not possible...
[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 <saman@natlab.research.philips.com>
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_socket_t *    p_satellite;
78     char *              psz_parser;
79     char *              psz_next;
80     int                 i_fd = 0;
81     unsigned int        u_adapter = 1;
82     unsigned int        u_device = 0;
83     unsigned int        u_freq = 0;
84     unsigned int        u_srate = 0;
85     unsigned int        u_lnb_lof1;
86     unsigned int        u_lnb_lof2;
87     unsigned int        u_lnb_slof;
88     int                 i_bandwidth = 0;
89     int                 i_modulation = 0;
90     int                 i_guard = 0;
91     int                 i_transmission = 0;
92     int                 i_hierarchy = 0;
93     vlc_bool_t          b_polarisation = 0;
94     int                 i_fec = 0;
95     int                 i_code_rate_HP = 0;
96     int                 i_code_rate_LP = 0;
97     vlc_bool_t          b_diseqc;
98     vlc_bool_t          b_probe;
99     char                dvr[] = DVR;
100     char                frontend[] = FRONTEND;
101     int                 i_len = 0;
102
103     /* parse the options passed in command line : */
104     psz_parser = strdup( p_input->psz_name );
105
106     if( !psz_parser )
107     {
108         return( -1 );
109     }
110
111     // Get adapter and device number to use for this dvb card
112     u_adapter = config_GetInt( p_input, "adapter" );
113     u_device  = config_GetInt( p_input, "device" );
114   
115     /* Determine frontend device information and capabilities */
116     b_probe = config_GetInt( p_input, "probe" );
117     if (b_probe)
118     {
119         if ( ioctl_InfoFrontend(p_input, &frontend_info, u_adapter, u_device) < 0 )
120         {
121             msg_Err( p_input, "(access) cannot determine frontend info" );
122             return -1;
123         }
124     }
125     else /* no frontend probing is done so use default border values. */
126     {
127         msg_Dbg( p_input, "using default values for frontend info" );
128         i_len = sizeof(FRONTEND);
129         if (snprintf(frontend, sizeof(FRONTEND), FRONTEND, u_adapter, u_device) >= i_len)
130         {
131             msg_Err( p_input, "snprintf() truncated string for FRONTEND" );
132             frontend[sizeof(FRONTEND)] = '\0';
133         }
134         strncpy(frontend_info.name, frontend, 128);
135
136         msg_Dbg(p_input, "method of access is %s", p_input->psz_access);
137         
138         frontend_info.type = FE_QPSK;
139         if (strncmp( p_input->psz_access, "qpsk",4 ) ==0)
140             frontend_info.type = FE_QPSK;
141         else if (strncmp( p_input->psz_access, "cable",5 ) ==0)
142             frontend_info.type = FE_QAM;
143         else if (strncmp( p_input->psz_access, "terrestrial",11) ==0)
144             frontend_info.type = FE_OFDM;
145
146         frontend_info.frequency_max =   12999000;
147         frontend_info.frequency_min =    9750000;
148         frontend_info.symbol_rate_max = 30000000;
149         frontend_info.symbol_rate_min =  1000000;
150     }
151     
152     /* Register Callback functions */
153     p_input->pf_read = SatelliteRead;
154     p_input->pf_set_program = SatelliteSetProgram;
155     p_input->pf_set_area = SatelliteSetArea;
156     p_input->pf_seek = SatelliteSeek;
157
158     u_freq = (unsigned int)strtol( psz_parser, &psz_next, 10 );
159     if( *psz_next )
160     {
161         psz_parser = psz_next + 1;
162         b_polarisation = (vlc_bool_t)strtol( psz_parser, &psz_next, 10 );
163         if( *psz_next )
164         {
165             psz_parser = psz_next + 1;
166             i_fec = (int)strtol( psz_parser, &psz_next, 10 );
167             if( *psz_next )
168             {
169                 psz_parser = psz_next + 1;
170                 u_srate = (unsigned int)strtol( psz_parser, &psz_next, 10 );
171             }
172         }
173     }
174
175     /* Workaround for backwards compatibility */
176     if (strncmp( p_input->psz_access, "satellite",9 ) ==0)
177     {
178         msg_Warn( p_input, "invalid frequency %d possibly in kHz, trying value *1000 Hz", u_freq );
179         u_freq *= 1000;
180     }
181     
182     /* Validating input values */
183     if ( ((u_freq) > frontend_info.frequency_max) ||
184          ((u_freq) < frontend_info.frequency_min) )
185     {
186         msg_Warn( p_input, "invalid frequency %d (Hz), using default one", u_freq );
187         u_freq = config_GetInt( p_input, "frequency" );
188         if ( ((u_freq) > frontend_info.frequency_max) ||
189              ((u_freq) < frontend_info.frequency_min) )
190         {
191             msg_Err( p_input, "invalid default frequency" );
192             return -1;
193         }
194     }
195
196     /* Workaround for backwards compatibility */
197     if (strncmp( p_input->psz_access, "satellite", 9 ) ==0)
198     {
199         msg_Warn( p_input, "invalid symbol rate %d possibly specified in kHz, trying value *1000 Hz", u_freq );
200         u_srate *= 1000;
201     }
202     
203     if ( ((u_srate) > frontend_info.symbol_rate_max) ||
204          ((u_srate) < frontend_info.symbol_rate_min) )
205     {
206         msg_Warn( p_input, "invalid symbol rate, using default one" );
207         u_srate = config_GetInt( p_input, "symbol-rate" );
208         if ( ((u_srate) > frontend_info.symbol_rate_max) ||
209              ((u_srate) < frontend_info.symbol_rate_min) )
210         {
211             msg_Err( p_input, "invalid default symbol rate" );
212             return -1;
213         }
214     }
215
216     if( b_polarisation && (b_polarisation != 1) )
217     {
218         msg_Warn( p_input, "invalid polarization, using default one" );
219         b_polarisation = config_GetInt( p_input, "polarization" );
220         if( b_polarisation && b_polarisation != 1 )
221         {
222             msg_Err( p_input, "invalid default polarization" );
223             return -1;
224         }
225     }
226
227     if( (i_fec > 9) || (i_fec < 1) )
228     {
229         msg_Warn( p_input, "invalid FEC, using default one" );
230         i_fec = config_GetInt( p_input, "fec" );
231         if( (i_fec > 9) || (i_fec < 1) )
232         {
233             msg_Err( p_input, "invalid default FEC" );
234             return -1;
235         }
236     }
237
238     /* Get antenna configuration options */
239     b_diseqc = config_GetInt( p_input, "diseqc" );
240     u_lnb_lof1 = config_GetInt( p_input, "lnb-lof1" );
241     u_lnb_lof2 = config_GetInt( p_input, "lnb-lof2" );
242     u_lnb_slof = config_GetInt( p_input, "lnb-slof" );
243
244     /* Setting frontend parameters for tuning the hardware */      
245     switch( frontend_info.type )
246     {
247         /* DVB-S: satellite and budget cards (nova) */
248         case FE_QPSK:
249             fep.frequency = u_freq;
250             fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
251             fep.u.qpsk.symbol_rate = u_srate;
252             fep.u.qpsk.fec_inner = dvb_DecodeFEC(p_input, i_fec); 
253             msg_Dbg( p_input, "satellite (QPSK) frontend found on %s", frontend_info.name );
254             break;
255             
256         /* DVB-C */
257         case FE_QAM:
258             i_modulation  = config_GetInt(p_input, "modulation");
259
260             fep.frequency = u_freq;
261             fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
262             fep.u.qam.symbol_rate = u_srate;
263             fep.u.qam.fec_inner = dvb_DecodeFEC(p_input, i_fec); 
264             fep.u.qam.modulation = dvb_DecodeModulation(p_input, i_modulation); 
265             msg_Dbg( p_input, "cable (QAM) frontend found on %s", frontend_info.name );
266             break;
267
268         /* DVB-T */
269         case FE_OFDM:
270             i_bandwidth = config_GetInt( p_input, "bandwidth");
271             i_code_rate_HP = config_GetInt(p_input, "code-rate-hp");
272             i_code_rate_LP = config_GetInt(p_input, "code-rate-lp");
273             i_modulation  = config_GetInt(p_input, "modulation");
274             i_transmission = config_GetInt(p_input, "transmission");
275             i_guard = config_GetInt(p_input, "guard");
276             i_hierarchy = config_GetInt(p_input, "hierarchy");
277             
278             fep.frequency = u_freq;
279             fep.inversion = dvb_DecodeInversion(p_input, (int) b_polarisation);
280             fep.u.ofdm.bandwidth = dvb_DecodeBandwidth(p_input, i_bandwidth);
281             fep.u.ofdm.code_rate_HP = dvb_DecodeFEC(p_input, i_code_rate_HP); 
282             fep.u.ofdm.code_rate_LP = dvb_DecodeFEC(p_input, i_code_rate_LP);
283             fep.u.ofdm.constellation = dvb_DecodeModulation(p_input, i_modulation); 
284             fep.u.ofdm.transmission_mode = dvb_DecodeTransmission(p_input, i_transmission);
285             fep.u.ofdm.guard_interval = dvb_DecodeGuardInterval(p_input, i_guard);
286             fep.u.ofdm.hierarchy_information = dvb_DecodeHierarchy(p_input, i_hierarchy);
287             msg_Dbg( p_input, "terrestrial (OFDM) frontend found on %s", frontend_info.name );
288             break;
289
290         default:
291             msg_Err( p_input, "Could not determine frontend type on %s", frontend_info.name );
292             return -1;
293     }
294
295     /* Initialise structure */
296     p_satellite = malloc( sizeof( input_socket_t ) );
297
298     if( p_satellite == NULL )
299     {
300         msg_Err( p_input, "out of memory" );
301         return -1;
302     }
303
304     p_input->p_access_data = (void *)p_satellite;
305
306     /* Open the DVR device */
307     i_len = sizeof(DVR);
308     if (snprintf(dvr, sizeof(DVR), DVR, u_adapter, u_device) >= i_len)
309     {
310         msg_Err( p_input, "snprintf() truncated string for DVR" );
311         dvr[sizeof(DVR)] = '\0';
312     }
313     msg_Dbg( p_input, "opening DVR device '%s'", dvr );
314
315     if( (p_satellite->i_handle = open( dvr,
316                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
317     {
318 #   ifdef HAVE_ERRNO_H
319         msg_Warn( p_input, "cannot open `%s' (%s)", dvr, strerror(errno) );
320 #   else
321         msg_Warn( p_input, "cannot open `%s'", dvr );
322 #   endif
323         free( p_satellite );
324         return -1;
325     }
326
327     /* Initialize the Satellite Card */
328     switch (ioctl_SetFrontend (p_input, fep, b_polarisation,
329                                u_lnb_lof1, u_lnb_lof2, u_lnb_slof,
330                                u_adapter, u_device ))
331     {
332         case -2:
333             msg_Err( p_input, "frontend returned an unexpected event" );
334             close( p_satellite->i_handle );
335             free( p_satellite );
336             return -1;
337         case -3:
338             msg_Err( p_input, "frontend returned no event" );
339             close( p_satellite->i_handle );
340             free( p_satellite );
341             return -1;
342         case -4:
343             msg_Err( p_input, "frontend: timeout when polling for event" );
344             close( p_satellite->i_handle );
345             free( p_satellite );
346             return -1;
347         case -5:
348             msg_Err( p_input, "an error occured when polling frontend device" );
349             close( p_satellite->i_handle );
350             free( p_satellite );
351             return -1;
352         case -1:
353             msg_Err( p_input, "frontend returned a failure event" );
354             close( p_satellite->i_handle );
355             free( p_satellite );
356             return -1;
357         default:
358             break;
359     }
360
361     msg_Dbg( p_input, "setting filter on PAT" );
362
363     if ( ioctl_SetDMXFilter(p_input, 0, &i_fd, 21, u_adapter, u_device ) < 0 )
364     {
365 #   ifdef HAVE_ERRNO_H
366         msg_Err( p_input, "an error occured when setting filter on PAT (%s)", strerror(errno) );
367 #   else
368         msg_Err( p_input, "an error occured when setting filter on PAT" );
369 #   endif        
370         close( p_satellite->i_handle );
371         free( p_satellite );
372         return -1;
373     }
374
375     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
376     {
377         msg_Err( p_input, "could not initialize stream structure" );
378         close( p_satellite->i_handle );
379         free( p_satellite );
380         return( -1 );
381     }
382
383     vlc_mutex_lock( &p_input->stream.stream_lock );
384
385     p_input->stream.b_pace_control = 1;
386     p_input->stream.b_seekable = 0;
387     p_input->stream.p_selected_area->i_tell = 0;
388
389     vlc_mutex_unlock( &p_input->stream.stream_lock );
390
391     p_input->i_mtu = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
392     p_input->stream.i_method = INPUT_METHOD_SATELLITE;
393
394     return 0;
395 }
396
397 /*****************************************************************************
398  * Close : Close the device
399  *****************************************************************************/
400 void E_(Close) ( vlc_object_t *p_this )
401 {
402     input_thread_t *    p_input = (input_thread_t *)p_this;
403     input_socket_t *    p_satellite;
404     unsigned int        i_es_index;
405
406     if ( p_input->stream.p_selected_program )
407     {
408         for ( i_es_index = 1 ;
409                 i_es_index < p_input->stream.p_selected_program->i_es_number;
410                 i_es_index ++ )
411         {
412 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
413             if ( p_es->p_decoder_fifo )
414             {
415                 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
416             }
417 #undef p_es
418         }
419     }
420
421     p_satellite = (input_socket_t *)p_input;
422     close( p_satellite->i_handle );
423 }
424
425 /*****************************************************************************
426  * SatelliteRead: reads data from the satellite card
427  *****************************************************************************/
428 static ssize_t SatelliteRead( input_thread_t * p_input, byte_t * p_buffer,
429                               size_t i_len )
430 {
431     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
432     ssize_t i_ret;
433     unsigned int u_adapter = 1;
434     unsigned int u_device = 0;
435     unsigned int i;
436
437     // Get adapter and device number to use for this dvb card
438     u_adapter = config_GetInt( p_input, "adapter" );
439     u_device  = config_GetInt( p_input, "device" );
440
441     /* if not set, set filters to the PMTs */
442     for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
443     {
444         if ( p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd == 0 )
445         {
446             ioctl_SetDMXFilter(p_input, p_input->stream.pp_programs[i]->pp_es[0]->i_id,
447                        &p_input->stream.pp_programs[i]->pp_es[0]->i_demux_fd,
448                        21, u_adapter, u_device );
449         }
450     }
451
452     i_ret = read( p_access_data->i_handle, p_buffer, i_len );
453  
454     if( i_ret < 0 )
455     {
456 #   ifdef HAVE_ERRNO_H
457         msg_Err( p_input, "read failed (%s)", strerror(errno) );
458 #   else
459         msg_Err( p_input, "read failed" );
460 #   endif
461     }
462
463     return i_ret;
464 }
465
466 /*****************************************************************************
467  * SatelliteSetArea : Does nothing
468  *****************************************************************************/
469 static int SatelliteSetArea( input_thread_t * p_input, input_area_t * p_area )
470 {
471     return -1;
472 }
473
474 /*****************************************************************************
475  * SatelliteSetProgram : Sets the card filters according to the
476  *                 selected program,
477  *                 and makes the appropriate changes to stream structure.
478  *****************************************************************************/
479 int SatelliteSetProgram( input_thread_t    * p_input,
480                          pgrm_descriptor_t * p_new_prg )
481 {
482     unsigned int i_es_index;
483     vlc_value_t val;
484     unsigned int u_adapter = 1;
485     unsigned int u_device = 0;
486     unsigned int u_video_type = 1; /* default video type */
487     unsigned int u_audio_type = 2; /* default audio type */
488
489     // Get adapter and device number to use for this dvb card
490     u_adapter = config_GetInt( p_input, "adapter" );
491     u_device  = config_GetInt( p_input, "device" );
492
493     if ( p_input->stream.p_selected_program )
494     {
495         for ( i_es_index = 1 ; /* 0 should be the PMT */
496                 i_es_index < p_input->stream.p_selected_program->i_es_number ;
497                 i_es_index ++ )
498         {
499 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
500             if ( p_es->p_decoder_fifo )
501             {
502                 input_UnselectES( p_input , p_es );
503             }
504             if ( p_es->i_demux_fd )
505             {
506                 ioctl_UnsetDMXFilter(p_input, p_es->i_demux_fd );
507                 p_es->i_demux_fd = 0;
508             }
509 #undef p_es
510         }
511     }
512
513     for (i_es_index = 1 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
514     {
515 #define p_es p_new_prg->pp_es[i_es_index]
516         switch( p_es->i_cat )
517         {
518             case MPEG1_VIDEO_ES:
519             case MPEG2_VIDEO_ES:
520             case MPEG2_MOTO_VIDEO_ES:
521                 if ( input_SelectES( p_input , p_es ) == 0 )
522                 {
523                     ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_video_type,
524                                        u_adapter, u_device);
525                     u_video_type += 5;
526                 }
527                 break;
528             case MPEG1_AUDIO_ES:
529             case MPEG2_AUDIO_ES:
530                 if ( input_SelectES( p_input , p_es ) == 0 )
531                 {
532                     ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, u_audio_type,
533                                        u_adapter, u_device);
534                     input_SelectES( p_input , p_es );
535                     u_audio_type += 5;
536                 }
537                 break;
538             default:
539                 ioctl_SetDMXFilter(p_input, p_es->i_id, &p_es->i_demux_fd, 21, u_adapter, u_device);
540                 input_SelectES( p_input , p_es );
541                 msg_Warn(p_input, "ES streamtype 0x%d found used as DMX_PES_OTHER !!",(int) p_es->i_cat);
542                 break;
543 #undef p_es
544         }
545     }
546
547     p_input->stream.p_selected_program = p_new_prg;
548
549     /* Update the navigation variables without triggering a callback */
550     val.i_int = p_new_prg->i_number;
551     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
552
553     return 0;
554 }
555
556 /*****************************************************************************
557  * SatelliteSeek: does nothing (not a seekable stream
558  *****************************************************************************/
559 static void SatelliteSeek( input_thread_t * p_input, off_t i_off )
560 {
561     ;
562 }
563