1 /*****************************************************************************
2 * access.c: DVB card input v4l2 only
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
6 * Authors: Johan Bilien <jobi@via.ecp.fr>
7 * Jean-Paul Saman <jpsaman@wxs.nl>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include <vlc/input.h>
38 #include <sys/types.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open( vlc_object_t *p_this );
48 static void Close( vlc_object_t *p_this );
50 #define CACHING_TEXT N_("Caching value in ms")
51 #define CACHING_LONGTEXT N_( \
52 "Allows you to modify the default caching value for dvb streams. This " \
53 "value should be set in millisecond units." )
55 #define PROGRAM_TEXT N_("Program to decode")
56 #define PROGRAM_LONGTEXT N_("This is a workaround for a bug in the input")
58 #define ADAPTER_TEXT N_("Adapter card to tune")
59 #define ADAPTER_LONGTEXT N_("Adapter cards have a device file in directory named /dev/dvb/adapter[n] with n>=0.")
61 #define DEVICE_TEXT N_("Device number to use on adapter")
62 #define DEVICE_LONGTEXT ""
64 #define FREQ_TEXT N_("Transponder/multiplex frequency")
65 #define FREQ_LONGTEXT N_("In kHz for DVB-S or Hz for DVB-C/T")
67 #define INVERSION_TEXT N_("Inversion mode")
68 #define INVERSION_LONGTEXT N_("Inversion mode [0=off, 1=on, 2=auto]")
70 #define PROBE_TEXT N_("Probe DVB card for capabilities")
71 #define PROBE_LONGTEXT N_("Some DVB cards do not like to be probed for their capabilities.")
73 #define LNB_LOF1_TEXT N_("Antenna lnb_lof1 (kHz)")
74 #define LNB_LOF1_LONGTEXT ""
76 #define LNB_LOF2_TEXT N_("Antenna lnb_lof2 (kHz)")
77 #define LNB_LOF2_LONGTEXT ""
79 #define LNB_SLOF_TEXT N_("Antenna lnb_slof (kHz)")
80 #define LNB_SLOF_LONGTEXT ""
83 #define BUDGET_TEXT N_("Budget mode")
84 #define BUDGET_LONGTEXT N_("This allows you to stream an entire transponder with a budget card.")
86 #define SATNO_TEXT N_("Satellite number in the Diseqc system")
87 #define SATNO_LONGTEXT N_("[0=no diseqc, 1-4=normal diseqc, -1=A, -2=B simple diseqc]")
89 #define VOLTAGE_TEXT N_("LNB voltage")
90 #define VOLTAGE_LONGTEXT N_("In Volts [0, 13=vertical, 18=horizontal]")
92 #define TONE_TEXT N_("22 kHz tone")
93 #define TONE_LONGTEXT N_("[0=off, 1=on, -1=auto]")
95 #define FEC_TEXT N_("Transponder FEC")
96 #define FEC_LONGTEXT N_("FEC=Forward Error Correction mode [9=auto]")
98 #define SRATE_TEXT N_("Transponder symbol rate in kHz")
99 #define SRATE_LONGTEXT ""
102 #define MODULATION_TEXT N_("Modulation type")
103 #define MODULATION_LONGTEXT N_("Modulation type for front-end device.")
106 #define CODE_RATE_HP_TEXT N_("Terrestrial high priority stream code rate (FEC)")
107 #define CODE_RATE_HP_LONGTEXT ""
109 #define CODE_RATE_LP_TEXT N_("Terrestrial low priority stream code rate (FEC)")
110 #define CODE_RATE_LP_LONGTEXT ""
112 #define BANDWIDTH_TEXT N_("Terrestrial bandwidth")
113 #define BANDWIDTH_LONGTEXT N_("Terrestrial bandwidth [0=auto,6,7,8 in MHz]")
115 #define GUARD_TEXT N_("Terrestrial guard interval")
116 #define GUARD_LONGTEXT ""
118 #define TRANSMISSION_TEXT N_("Terrestrial transmission mode")
119 #define TRANSMISSION_LONGTEXT ""
121 #define HIERARCHY_TEXT N_("Terrestrial hierarchy mode")
122 #define HIERARCHY_LONGTEXT ""
125 set_shortname( _("DVB") );
126 set_description( N_("DVB input with v4l2 support") );
128 add_integer( "dvb-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
129 CACHING_LONGTEXT, VLC_TRUE );
130 add_integer( "dvb-adapter", 0, NULL, ADAPTER_TEXT, ADAPTER_LONGTEXT,
132 add_integer( "dvb-device", 0, NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
134 add_integer( "dvb-frequency", 11954000, NULL, FREQ_TEXT, FREQ_LONGTEXT,
136 add_integer( "dvb-inversion", 2, NULL, INVERSION_TEXT, INVERSION_LONGTEXT,
138 add_bool( "dvb-probe", 1, NULL, PROBE_TEXT, PROBE_LONGTEXT, VLC_TRUE );
139 add_integer( "dvb-lnb-lof1", 9750000, NULL, LNB_LOF1_TEXT,
140 LNB_LOF1_LONGTEXT, VLC_TRUE );
141 add_integer( "dvb-lnb-lof2", 10600000, NULL, LNB_LOF2_TEXT,
142 LNB_LOF2_LONGTEXT, VLC_TRUE );
143 add_integer( "dvb-lnb-slof", 11700000, NULL, LNB_SLOF_TEXT,
144 LNB_SLOF_LONGTEXT, VLC_TRUE );
145 /* DVB-S (satellite) */
146 add_bool( "dvb-budget-mode", 0, NULL, BUDGET_TEXT, BUDGET_LONGTEXT,
148 add_integer( "dvb-satno", 0, NULL, SATNO_TEXT, SATNO_LONGTEXT,
150 add_integer( "dvb-voltage", 13, NULL, VOLTAGE_TEXT, VOLTAGE_LONGTEXT,
152 add_integer( "dvb-tone", -1, NULL, TONE_TEXT, TONE_LONGTEXT,
154 add_integer( "dvb-fec", 9, NULL, FEC_TEXT, FEC_LONGTEXT, VLC_TRUE );
155 add_integer( "dvb-srate", 27500000, NULL, SRATE_TEXT, SRATE_LONGTEXT,
157 /* DVB-T (terrestrial) */
158 add_integer( "dvb-modulation", 0, NULL, MODULATION_TEXT,
159 MODULATION_LONGTEXT, VLC_TRUE );
160 /* DVB-T (terrestrial) */
161 add_integer( "dvb-code-rate-hp", 9, NULL, CODE_RATE_HP_TEXT,
162 CODE_RATE_HP_LONGTEXT, VLC_TRUE );
163 add_integer( "dvb-code-rate-lp", 9, NULL, CODE_RATE_LP_TEXT,
164 CODE_RATE_LP_LONGTEXT, VLC_TRUE );
165 add_integer( "dvb-bandwidth", 0, NULL, BANDWIDTH_TEXT, BANDWIDTH_LONGTEXT,
167 add_integer( "dvb-guard", 0, NULL, GUARD_TEXT, GUARD_LONGTEXT, VLC_TRUE );
168 add_integer( "dvb-transmission", 0, NULL, TRANSMISSION_TEXT,
169 TRANSMISSION_LONGTEXT, VLC_TRUE );
170 add_integer( "dvb-hierarchy", 0, NULL, HIERARCHY_TEXT, HIERARCHY_LONGTEXT,
173 set_capability( "access", 0 );
174 add_shortcut( "dvb" );
175 add_shortcut( "dvb-s" );
176 add_shortcut( "qpsk" );
177 add_shortcut( "dvb-c" );
178 add_shortcut( "cable" );
179 add_shortcut( "dvb-t" );
180 add_shortcut( "terrestrial" );
181 add_shortcut( "satellite" ); /* compatibility with the interface. */
182 set_callbacks( Open, Close );
186 /*****************************************************************************
188 *****************************************************************************/
189 static block_t *Block( access_t * );
190 static int Control( access_t *, int, va_list );
192 #define SATELLITE_READ_ONCE 3
194 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
196 static int SetArea ( input_thread_t *, input_area_t * );
197 static int SetProgram ( input_thread_t *, pgrm_descriptor_t * );
198 static void Seek ( input_thread_t *, off_t );
199 static void AllocateDemux( input_thread_t * p_input, int i_pid,
201 static void CloseProgram( input_thread_t * p_input );
204 static void FilterUnset( access_t *, int i_start, int i_max );
205 static void FilterSet( access_t *, int i_pid, int i_type );
207 static void VarInit( access_t * );
208 static int ParseMRL( access_t * );
211 /*****************************************************************************
212 * Open: open the frontend device
213 *****************************************************************************/
214 static int Open( vlc_object_t *p_this )
216 access_t *p_access = (access_t*)p_this;
219 /* Only if selected */
220 if( *p_access->psz_acces == '\0' )
224 p_access->pf_read = NULL;
225 p_access->pf_block = Block;
226 p_access->pf_control = Control;
227 p_access->pf_seek = NULL;
228 p_access->info.i_update = 0;
229 p_access->info.i_size = 0;
230 p_access->info.i_pos = 0;
231 p_access->info.b_eof = VLC_FALSE;
232 p_access->info.i_title = 0;
233 p_access->info.i_seekpoint = 0;
235 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
236 memset( p_sys, 0, sizeof( access_sys_t ) );
238 /* Create all variables */
241 /* Parse the command line */
242 if( ParseMRL( p_access ) )
248 /* Getting frontend info */
249 if( E_(FrontendOpen)( p_access) )
255 /* Setting frontend parameters for tuning the hardware */
256 msg_Dbg( p_access, "trying to tune the frontend...");
257 if( E_(FrontendSet)( p_access ) < 0 )
259 E_(FrontendClose)( p_access );
264 /* Opening DVR device */
265 if( E_(DVROpen)( p_access ) < 0 )
267 E_(FrontendClose)( p_access );
272 p_sys->b_budget_mode = var_GetBool( p_access, "dvb-budget-mode" );
273 if( p_sys->b_budget_mode )
275 msg_Dbg( p_access, "setting filter on all PIDs" );
276 AllocateDemux( p_access, 0x2000, OTHER_TYPE );
280 msg_Dbg( p_access, "setting filter on PAT" );
281 AllocateDemux( p_access, 0x0, OTHER_TYPE );
287 /*****************************************************************************
288 * Close : Close the device
289 *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
292 access_t *p_access = (access_t*)p_this;
293 access_sys_t *p_sys = p_access->p_sys;
295 FilterUnset( p_access, 0, p_sys->b_budget_mode ? 1 : MAX_DEMUX );
297 E_(DVRClose)( p_access );
298 E_(FrontendClose)( p_access );
302 /*****************************************************************************
304 *****************************************************************************/
305 static block_t *Block( access_t *p_access )
307 access_t *p_access = (access_t*)p_this;
308 access_sys_t *p_sys = p_access->p_sys;
309 struct timeval timeout;
314 /* Initialize file descriptor set */
316 FD_SET( p_sys->i_handle, &fds );
318 /* We'll wait 0.5 second if nothing happens */
320 timeout.tv_usec = 500000;
322 /* Find if some data is available */
323 while( (i_ret = select( p_sys->i_handle + 1, &fds, NULL, NULL, &timeout )) == 0 ||
324 (i_ret < 0 && errno == EINTR) )
327 FD_SET( p_sys->i_handle, &fds );
329 timeout.tv_usec = 500000;
331 if( p_access->b_die )
337 msg_Err( p_input, "select error (%s)", strerror(errno) );
341 p_block = block_New( p_access, p_sys->i_mtu );
342 if( ( p_block->i_buffer = read( p_sys->i_handle, p_block->p_buffer, SATELLITE_READ_ONCE * TS_PACKET_SIZE ) ) <= 0 )
344 msg_Err( p_input, "read failed (%s)", strerror(errno) );
345 block_Release( p_block );
352 /*****************************************************************************
354 *****************************************************************************/
355 static int Control( access_t *p_access, int i_query, va_list args )
357 access_sys_t *p_sys = p_access->p_sys;
358 vlc_bool_t *pb_bool, b_bool;
366 case ACCESS_CAN_SEEK:
367 case ACCESS_CAN_FASTSEEK:
368 case ACCESS_CAN_PAUSE:
369 case ACCESS_CAN_CONTROL_PACE:
370 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
371 *pb_bool = VLC_FALSE;
375 pi_int = (int*)va_arg( args, int * );
376 *pi_int = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
379 case ACCESS_GET_PTS_DELAY:
380 pi_64 = (int64_t*)va_arg( args, int64_t * );
381 *pi_64 = var_GetInteger( p_access, "dvb-caching" ) * 1000;
385 case ACCESS_SET_PAUSE_STATE:
386 case ACCESS_GET_TITLE_INFO:
387 case ACCESS_SET_TITLE:
388 case ACCESS_SET_SEEKPOINT:
391 case ACCESS_SET_PRIVATE_ID_STATE:
392 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t ); /* b_selected */
393 i_int = (int)va_arg( args, int ); /* Private data (pid for now)*/
394 if( !p_sys->b_budget_mode )
396 /* FIXME we may want to give the real type (me ?, I don't ;) */
398 FilterSet( p_access, i_int, OTHER_TYPE );
400 FilterUnset( p_access, i_int, i_int+1 );
405 msg_Err( p_access, "unimplemented query in control" );
412 /*****************************************************************************
413 * FilterSet/FilterUnset:
414 *****************************************************************************/
415 static void FilterSet( access_t *p_access, int i_pid, int i_type );
417 access_sys_t *p_sys = p_access->p_sys;
420 /* Find first free slot */
421 for( i = 0; i < MAX_DEMUX; i++ )
423 if( !p_sys->p_demux_handles[i].i_type )
429 msg_Err( p_access, "no free p_demux_handles !" );
433 if( E_(DMXSetFilter)( p_access, i_pid,
434 &p_sys->p_demux_handles[i].i_handle, i_type ) )
436 msg_Err( p_access, "DMXSetFilter failed" );
439 p_sys->p_demux_handles[i].i_type = i_type;
440 p_sys->p_demux_handles[i].i_pid = i_pid;
443 static void FilterUnset( access_t *p_access, int i_start, int i_max )
445 access_sys_t *p_sys = p_access->p_sys;
448 for( i = i_start; i < i_max; i++ )
450 if( p_sys->p_demux_handles[i].i_type )
452 E_(DMXUnsetFilter)( p_access, p_sys->p_demux_handles[i].i_handle );
453 p_sys->p_demux_handles[i].i_type = 0;
458 /*****************************************************************************
460 *****************************************************************************/
461 static void VarInit( access_t *p_access )
464 var_Create( p_input, "dvb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
467 var_Create( p_input, "dvb-adapter", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
468 var_Create( p_input, "dvb-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
469 var_Create( p_input, "dvb-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
470 var_Create( p_input, "dvb-inversion", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
471 var_Create( p_input, "dvb-probe", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
472 var_Create( p_input, "dvb-lnb-lof1", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
473 var_Create( p_input, "dvb-lnb-lof2", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
474 var_Create( p_input, "dvb-lnb-slof", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
477 var_Create( p_input, "dvb-budget-mode", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
478 var_Create( p_input, "dvb-satno", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
479 var_Create( p_input, "dvb-voltage", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
480 var_Create( p_input, "dvb-tone", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
481 var_Create( p_input, "dvb-fec", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
482 var_Create( p_input, "dvb-srate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
485 var_Create( p_input, "dvb-modulation", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
488 var_Create( p_input, "dvb-code-rate-hp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
489 var_Create( p_input, "dvb-code-rate-lp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
490 var_Create( p_input, "dvb-bandwidth", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
491 var_Create( p_input, "dvb-transmission", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
492 var_Create( p_input, "dvb-guard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
493 var_Create( p_input, "dvb-hierarchy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
497 static int ParseMRL( access_t *p_access )
499 char *psz_dup = strdup( p_input->psz_path );
500 char *psz_parser = psz_dup;
504 #define GET_OPTION_INT( option ) \
505 if ( !strncmp( psz_parser, option "=", strlen(option "=") ) ) \
507 val.i_int = strtol( psz_parser+strlen(option "="), &psz_parser, 0 );\
508 var_Set( p_access, "dvb-" option, val ); \
511 #define GET_OPTION_BOOL( option ) \
512 if ( !strncmp( psz_parser, option "=", strlen(option "=") ) ) \
514 val.b_bool = strtol( psz_parser + strlen(option "="), &psz_parser, \
516 var_Set( p_access, "dvb-" option, val ); \
519 /* Test for old syntax */
520 strtol( psz_parser, &psz_next, 10 );
521 if( psz_next != psz_parser )
523 msg_Err( p_access, "the DVB input old syntax is deprecated, use vlc "
524 "-p dvb to see an explanation of the new syntax" );
531 GET_OPTION_INT("adapter")
532 else GET_OPTION_INT("device")
533 else GET_OPTION_INT("frequency")
534 else GET_OPTION_INT("inversion")
535 else GET_OPTION_BOOL("probe")
536 else GET_OPTION_INT("lnb-lof1")
537 else GET_OPTION_INT("lnb-lof2")
538 else GET_OPTION_INT("lnb-slof")
540 else GET_OPTION_BOOL("budget-mode")
541 else GET_OPTION_INT("voltage")
542 else GET_OPTION_INT("tone")
543 else GET_OPTION_INT("fec")
544 else GET_OPTION_INT("srate")
546 else GET_OPTION_INT("modulation")
548 else GET_OPTION_INT("code-rate-hp")
549 else GET_OPTION_INT("code-rate-lp")
550 else GET_OPTION_INT("bandwidth")
551 else GET_OPTION_INT("transmission")
552 else GET_OPTION_INT("guard")
553 else GET_OPTION_INT("hierarchy")
555 else if( !strncmp( psz_parser, "satno=",
556 strlen( "satno=" ) ) )
558 psz_parser += strlen( "satno=" );
559 if ( *psz_parser == 'A' || *psz_parser == 'a' )
561 else if ( *psz_parser == 'B' || *psz_parser == 'b' )
564 val.i_int = strtol( psz_parser, &psz_parser, 0 );
565 var_Set( p_access, "dvb-satno", val );
567 /* Redundant with voltage but much easier to use */
568 else if( !strncmp( psz_parser, "polarization=",
569 strlen( "polarization=" ) ) )
571 psz_parser += strlen( "polarization=" );
572 if ( *psz_parser == 'V' || *psz_parser == 'v' )
574 else if ( *psz_parser == 'H' || *psz_parser == 'h' )
578 msg_Err( p_access, "illegal polarization %c", *psz_parser );
582 var_Set( p_access, "dvb-voltage", val );
586 msg_Err( p_access, "unknown option (%d)", psz_parser );
593 #undef GET_OPTION_INT
594 #undef GET_OPTION_BOOL