]> git.sesse.net Git - vlc/blob - modules/access/dvb/access.c
Oops. b_demux should be flase
[vlc] / modules / access / dvb / access.c
1 /*****************************************************************************
2  * access.c: DVB card input v4l2 only
3  *****************************************************************************
4  * Copyright (C) 1998-2005 the VideoLAN team
5  *
6  * Authors: Johan Bilien <jobi@via.ecp.fr>
7  *          Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_access.h>
37 #include <vlc_input.h>
38 #include <vlc_interface.h>
39 #include <vlc_dialog.h>
40
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #endif
44
45 #include <fcntl.h>
46 #include <sys/types.h>
47 #include <sys/poll.h>
48
49 #include <errno.h>
50
51 /* Include dvbpsi headers */
52 #ifdef HAVE_DVBPSI_DR_H
53 #   include <dvbpsi/dvbpsi.h>
54 #   include <dvbpsi/descriptor.h>
55 #   include <dvbpsi/pat.h>
56 #   include <dvbpsi/pmt.h>
57 #   include <dvbpsi/dr.h>
58 #   include <dvbpsi/psi.h>
59 #   include <dvbpsi/demux.h>
60 #   include <dvbpsi/sdt.h>
61 #else
62 #   include "dvbpsi.h"
63 #   include "descriptor.h"
64 #   include "tables/pat.h"
65 #   include "tables/pmt.h"
66 #   include "descriptors/dr.h"
67 #   include "psi.h"
68 #   include "demux.h"
69 #   include "tables/sdt.h"
70 #endif
71
72 #ifdef ENABLE_HTTPD
73 #   include "vlc_httpd.h"
74 #endif
75
76 #include "dvb.h"
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 static int  Open( vlc_object_t *p_this );
82 static void Close( vlc_object_t *p_this );
83
84 #define CACHING_TEXT N_("Caching value in ms")
85 #define CACHING_LONGTEXT N_( \
86     "Caching value for DVB streams. This " \
87     "value should be set in milliseconds." )
88
89 #define ADAPTER_TEXT N_("Adapter card to tune")
90 #define ADAPTER_LONGTEXT N_("Adapter cards have a device file in directory named /dev/dvb/adapter[n] with n>=0.")
91
92 #define DEVICE_TEXT N_("Device number to use on adapter")
93 #define DEVICE_LONGTEXT ""
94
95 #define FREQ_TEXT N_("Transponder/multiplex frequency")
96 #define FREQ_LONGTEXT N_("In kHz for DVB-S or Hz for DVB-C/T")
97
98 #define INVERSION_TEXT N_("Inversion mode")
99 #define INVERSION_LONGTEXT N_("Inversion mode [0=off, 1=on, 2=auto]")
100
101 #define PROBE_TEXT N_("Probe DVB card for capabilities")
102 #define PROBE_LONGTEXT N_("Some DVB cards do not like to be probed for their capabilities, you can disable this feature if you experience some trouble.")
103
104 #define BUDGET_TEXT N_("Budget mode")
105 #define BUDGET_LONGTEXT N_("This allows you to stream an entire transponder with a \"budget\" card.")
106
107 /* Satellite */
108 #define SATNO_TEXT N_("Satellite number in the Diseqc system")
109 #define SATNO_LONGTEXT N_("[0=no diseqc, 1-4=satellite number].")
110
111 #define VOLTAGE_TEXT N_("LNB voltage")
112 #define VOLTAGE_LONGTEXT N_("In Volts [0, 13=vertical, 18=horizontal].")
113
114 #define HIGH_VOLTAGE_TEXT N_("High LNB voltage")
115 #define HIGH_VOLTAGE_LONGTEXT N_("Enable high voltage if your cables are " \
116     "particularly long. This is not supported by all frontends.")
117
118 #define TONE_TEXT N_("22 kHz tone")
119 #define TONE_LONGTEXT N_("[0=off, 1=on, -1=auto].")
120
121 #define FEC_TEXT N_("Transponder FEC")
122 #define FEC_LONGTEXT N_("FEC=Forward Error Correction mode [9=auto].")
123
124 #define SRATE_TEXT N_("Transponder symbol rate in kHz")
125 #define SRATE_LONGTEXT ""
126
127 #define LNB_LOF1_TEXT N_("Antenna lnb_lof1 (kHz)")
128 #define LNB_LOF1_LONGTEXT ""
129
130 #define LNB_LOF2_TEXT N_("Antenna lnb_lof2 (kHz)")
131 #define LNB_LOF2_LONGTEXT ""
132
133 #define LNB_SLOF_TEXT N_("Antenna lnb_slof (kHz)")
134 #define LNB_SLOF_LONGTEXT ""
135
136 /* Cable */
137 #define MODULATION_TEXT N_("Modulation type")
138 #define MODULATION_LONGTEXT N_("Modulation type for front-end device.")
139
140 /* Terrestrial */
141 #define CODE_RATE_HP_TEXT N_("Terrestrial high priority stream code rate (FEC)")
142 #define CODE_RATE_HP_LONGTEXT ""
143
144 #define CODE_RATE_LP_TEXT N_("Terrestrial low priority stream code rate (FEC)")
145 #define CODE_RATE_LP_LONGTEXT ""
146
147 #define BANDWIDTH_TEXT N_("Terrestrial bandwidth")
148 #define BANDWIDTH_LONGTEXT N_("Terrestrial bandwidth [0=auto,6,7,8 in MHz]")
149
150 #define GUARD_TEXT N_("Terrestrial guard interval")
151 #define GUARD_LONGTEXT ""
152
153 #define TRANSMISSION_TEXT N_("Terrestrial transmission mode")
154 #define TRANSMISSION_LONGTEXT ""
155
156 #define HIERARCHY_TEXT N_("Terrestrial hierarchy mode")
157 #define HIERARCHY_LONGTEXT ""
158
159 #define HOST_TEXT N_( "HTTP Host address" )
160 #define HOST_LONGTEXT N_( \
161     "To enable the internal HTTP server, set its address and port here." )
162
163 #define USER_TEXT N_( "HTTP user name" )
164 #define USER_LONGTEXT N_( \
165     "User name the administrator will use to log into " \
166     "the internal HTTP server." )
167
168 #define PASSWORD_TEXT N_( "HTTP password" )
169 #define PASSWORD_LONGTEXT N_( \
170     "Password the administrator will use to log into " \
171     "the internal HTTP server." )
172
173 #define ACL_TEXT N_( "HTTP ACL" )
174 #define ACL_LONGTEXT N_( \
175     "Access control list (equivalent to .hosts) file path, " \
176     "which will limit the range of IPs entitled to log into the internal " \
177     "HTTP server." )
178
179 #define CERT_TEXT N_( "Certificate file" )
180 #define CERT_LONGTEXT N_( "HTTP interface x509 PEM certificate file " \
181                           "(enables SSL)" )
182
183 #define KEY_TEXT N_( "Private key file" )
184 #define KEY_LONGTEXT N_( "HTTP interface x509 PEM private key file" )
185
186 #define CA_TEXT N_( "Root CA file" )
187 #define CA_LONGTEXT N_( "HTTP interface x509 PEM trusted root CA " \
188                         "certificates file" )
189
190 #define CRL_TEXT N_( "CRL file" )
191 #define CRL_LONGTEXT N_( "HTTP interface Certificates Revocation List file" )
192
193 vlc_module_begin ()
194     set_shortname( N_("DVB") )
195     set_description( N_("DVB input with v4l2 support") )
196     set_category( CAT_INPUT )
197     set_subcategory( SUBCAT_INPUT_ACCESS )
198
199     add_integer( "dvb-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
200                  CACHING_LONGTEXT, true )
201     add_integer( "dvb-adapter", 0, NULL, ADAPTER_TEXT, ADAPTER_LONGTEXT,
202                  false )
203     add_integer( "dvb-device", 0, NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
204                  true )
205     add_integer( "dvb-frequency", 0, NULL, FREQ_TEXT, FREQ_LONGTEXT,
206                  false )
207     add_integer( "dvb-inversion", 2, NULL, INVERSION_TEXT, INVERSION_LONGTEXT,
208                  true )
209     add_bool( "dvb-probe", 1, NULL, PROBE_TEXT, PROBE_LONGTEXT, true )
210     add_bool( "dvb-budget-mode", 0, NULL, BUDGET_TEXT, BUDGET_LONGTEXT,
211               true )
212     /* DVB-S (satellite) */
213     add_integer( "dvb-satno", 0, NULL, SATNO_TEXT, SATNO_LONGTEXT,
214                  true )
215     add_integer( "dvb-voltage", 13, NULL, VOLTAGE_TEXT, VOLTAGE_LONGTEXT,
216                  true )
217     add_bool( "dvb-high-voltage", 0, NULL, HIGH_VOLTAGE_TEXT,
218               HIGH_VOLTAGE_LONGTEXT, true )
219     add_integer( "dvb-tone", -1, NULL, TONE_TEXT, TONE_LONGTEXT,
220                  true )
221     add_integer( "dvb-fec", 9, NULL, FEC_TEXT, FEC_LONGTEXT, true )
222     add_integer( "dvb-srate", 27500000, NULL, SRATE_TEXT, SRATE_LONGTEXT,
223                  false )
224     add_integer( "dvb-lnb-lof1", 0, NULL, LNB_LOF1_TEXT,
225                  LNB_LOF1_LONGTEXT, true )
226     add_integer( "dvb-lnb-lof2", 0, NULL, LNB_LOF2_TEXT,
227                  LNB_LOF2_LONGTEXT, true )
228     add_integer( "dvb-lnb-slof", 0, NULL, LNB_SLOF_TEXT,
229                  LNB_SLOF_LONGTEXT, true )
230     /* DVB-C (cable) */
231     add_integer( "dvb-modulation", 0, NULL, MODULATION_TEXT,
232                  MODULATION_LONGTEXT, true )
233     /* DVB-T (terrestrial) */
234     add_integer( "dvb-code-rate-hp", 9, NULL, CODE_RATE_HP_TEXT,
235                  CODE_RATE_HP_LONGTEXT, true )
236     add_integer( "dvb-code-rate-lp", 9, NULL, CODE_RATE_LP_TEXT,
237                  CODE_RATE_LP_LONGTEXT, true )
238     add_integer( "dvb-bandwidth", 0, NULL, BANDWIDTH_TEXT, BANDWIDTH_LONGTEXT,
239                  true )
240     add_integer( "dvb-guard", 0, NULL, GUARD_TEXT, GUARD_LONGTEXT, true )
241     add_integer( "dvb-transmission", 0, NULL, TRANSMISSION_TEXT,
242                  TRANSMISSION_LONGTEXT, true )
243     add_integer( "dvb-hierarchy", 0, NULL, HIERARCHY_TEXT, HIERARCHY_LONGTEXT,
244                  true )
245 #ifdef ENABLE_HTTPD
246     /* MMI HTTP interface */
247     set_section( N_("HTTP server" ), 0 )
248     add_string( "dvb-http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT,
249                 true )
250     add_string( "dvb-http-user", NULL, NULL, USER_TEXT, USER_LONGTEXT,
251                 true )
252     add_string( "dvb-http-password", NULL, NULL, PASSWORD_TEXT,
253                 PASSWORD_LONGTEXT, true )
254     add_string( "dvb-http-acl", NULL, NULL, ACL_TEXT, ACL_LONGTEXT,
255                 true )
256     add_string( "dvb-http-intf-cert", NULL, NULL, CERT_TEXT, CERT_LONGTEXT,
257                 true )
258     add_string( "dvb-http-intf-key",  NULL, NULL, KEY_TEXT,  KEY_LONGTEXT,
259                 true )
260     add_string( "dvb-http-intf-ca",   NULL, NULL, CA_TEXT,   CA_LONGTEXT,
261                 true )
262     add_string( "dvb-http-intf-crl",  NULL, NULL, CRL_TEXT,  CRL_LONGTEXT,
263                 true )
264 #endif
265
266     set_capability( "access", 0 )
267     add_shortcut( "dvb" )      /* Generic name */
268
269     add_shortcut( "dvb-s" )    /* Satellite */
270     add_shortcut( "qpsk" )
271     add_shortcut( "satellite" )
272
273     add_shortcut( "dvb-c" )    /* Cable */
274     add_shortcut( "cable" )
275
276     add_shortcut( "dvb-t" )    /* Terrestrial */
277     add_shortcut( "terrestrial" )
278
279     add_shortcut( "atsc" )     /* Atsc */
280     add_shortcut( "usdigital" )
281
282     set_callbacks( Open, Close )
283
284 vlc_module_end ()
285
286
287 /*****************************************************************************
288  * Local prototypes
289  *****************************************************************************/
290 static block_t *Block( access_t * );
291 static int Control( access_t *, int, va_list );
292
293 static block_t *BlockScan( access_t * );
294
295 #define DVB_READ_ONCE 20
296 #define DVB_READ_ONCE_START 2
297 #define DVB_READ_ONCE_SCAN 1
298 #define TS_PACKET_SIZE 188
299
300 #define DVB_SCAN_MAX_SIGNAL_TIME (300*1000)
301 #define DVB_SCAN_MAX_LOCK_TIME (5000*1000)
302 #define DVB_SCAN_MAX_PROBE_TIME (30000*1000)
303
304 static void FilterUnset( access_t *, int i_max );
305 static void FilterUnsetPID( access_t *, int i_pid );
306 static void FilterSet( access_t *, int i_pid, int i_type );
307
308 static void VarInit( access_t * );
309 static int  ParseMRL( access_t * );
310
311 /*****************************************************************************
312  * Open: open the frontend device
313  *****************************************************************************/
314 static int Open( vlc_object_t *p_this )
315 {
316     access_t     *p_access = (access_t*)p_this;
317     access_sys_t *p_sys;
318
319     /* Only if selected */
320     if( *p_access->psz_access == '\0' )
321         return VLC_EGENERIC;
322
323     /* Set up access */
324     p_access->pf_read = NULL;
325     p_access->pf_block = Block;
326     p_access->pf_control = Control;
327     p_access->pf_seek = NULL;
328
329     access_InitFields( p_access );
330
331     p_access->p_sys = p_sys = calloc( 1, sizeof( access_sys_t ) );
332     if( !p_sys )
333         return VLC_ENOMEM;
334
335     /* Create all variables */
336     VarInit( p_access );
337
338     /* Parse the command line */
339     if( ParseMRL( p_access ) )
340     {
341         free( p_sys );
342         return VLC_EGENERIC;
343     }
344
345     /* Getting frontend info */
346     if( FrontendOpen( p_access) )
347     {
348         free( p_sys );
349         return VLC_EGENERIC;
350     }
351
352     /* */
353     p_sys->b_scan_mode = var_GetInteger( p_access, "dvb-frequency" ) == 0;
354     if( p_sys->b_scan_mode )
355     {
356         msg_Dbg( p_access, "DVB scan mode selected" );
357         p_access->pf_block = BlockScan;
358     }
359     else
360     {
361         /* Setting frontend parameters for tuning the hardware */
362         msg_Dbg( p_access, "trying to tune the frontend...");
363         if( FrontendSet( p_access ) < 0 )
364         {
365             FrontendClose( p_access );
366             free( p_sys );
367             return VLC_EGENERIC;
368         }
369     }
370
371     /* Opening DVR device */
372     if( DVROpen( p_access ) < 0 )
373     {
374         FrontendClose( p_access );
375         free( p_sys );
376         return VLC_EGENERIC;
377     }
378
379     if( p_sys->b_scan_mode )
380     {
381         scan_parameter_t parameter;
382
383         msg_Dbg( p_access, "setting filter on PAT/NIT/SDT (DVB only)" );
384         FilterSet( p_access, 0x00, OTHER_TYPE );    // PAT
385         FilterSet( p_access, 0x10, OTHER_TYPE );    // NIT
386         FilterSet( p_access, 0x11, OTHER_TYPE );    // SDT
387
388         if( FrontendGetScanParameter( p_access, &parameter ) ||
389             scan_Init( VLC_OBJECT(p_access), &p_sys->scan, &parameter ) )
390         {
391             Close( VLC_OBJECT(p_access) );
392             return VLC_EGENERIC;
393         }
394     }
395     else
396     {
397         p_sys->b_budget_mode = var_GetBool( p_access, "dvb-budget-mode" );
398         if( p_sys->b_budget_mode )
399         {
400             msg_Dbg( p_access, "setting filter on all PIDs" );
401             FilterSet( p_access, 0x2000, OTHER_TYPE );
402         }
403         else
404         {
405             msg_Dbg( p_access, "setting filter on PAT" );
406             FilterSet( p_access, 0x0, OTHER_TYPE );
407         }
408
409         CAMOpen( p_access );
410
411 #ifdef ENABLE_HTTPD
412         HTTPOpen( p_access );
413 #endif
414     }
415
416     if( p_sys->b_scan_mode )
417         p_sys->i_read_once = DVB_READ_ONCE_SCAN;
418     else if( p_sys->b_budget_mode )
419         p_sys->i_read_once = DVB_READ_ONCE;
420     else
421         p_sys->i_read_once = DVB_READ_ONCE_START;
422
423     return VLC_SUCCESS;
424 }
425
426 /*****************************************************************************
427  * Close : Close the device
428  *****************************************************************************/
429 static void Close( vlc_object_t *p_this )
430 {
431     access_t     *p_access = (access_t*)p_this;
432     access_sys_t *p_sys = p_access->p_sys;
433
434     FilterUnset( p_access, p_sys->b_budget_mode && !p_sys->b_scan_mode ? 1 : MAX_DEMUX );
435
436     DVRClose( p_access );
437     FrontendClose( p_access );
438     if( p_sys->b_scan_mode )
439         scan_Clean( &p_sys->scan );
440     else
441         CAMClose( p_access );
442
443 #ifdef ENABLE_HTTPD
444     if( !p_sys->b_scan_mode )
445         HTTPClose( p_access );
446 #endif
447
448     free( p_sys );
449 }
450
451 /*****************************************************************************
452  * Block:
453  *****************************************************************************/
454 static block_t *Block( access_t *p_access )
455 {
456     access_sys_t *p_sys = p_access->p_sys;
457     block_t *p_block;
458
459     for ( ; ; )
460     {
461         struct pollfd ufds[2];
462         int i_ret;
463
464         /* Initialize file descriptor sets */
465         memset (ufds, 0, sizeof (ufds));
466         ufds[0].fd = p_sys->i_handle;
467         ufds[0].events = POLLIN;
468         ufds[1].fd = p_sys->i_frontend_handle;
469         ufds[1].events = POLLPRI;
470
471         /* We'll wait 0.5 second if nothing happens */
472         /* Find if some data is available */
473         i_ret = poll( ufds, 2, 500 );
474
475         if ( !vlc_object_alive (p_access) )
476             return NULL;
477
478         if ( i_ret < 0 )
479         {
480             if( errno == EINTR )
481                 continue;
482
483             msg_Err( p_access, "poll error: %m" );
484             return NULL;
485         }
486
487         if ( p_sys->i_ca_handle && mdate() > p_sys->i_ca_next_event )
488         {
489             CAMPoll( p_access );
490             p_sys->i_ca_next_event = mdate() + p_sys->i_ca_timeout;
491         }
492
493         if ( ufds[1].revents )
494         {
495             FrontendPoll( p_access );
496         }
497
498 #ifdef ENABLE_HTTPD
499         if ( p_sys->i_httpd_timeout && mdate() > p_sys->i_httpd_timeout )
500         {
501             vlc_mutex_lock( &p_sys->httpd_mutex );
502             if ( p_sys->b_request_frontend_info )
503             {
504                 msg_Warn( p_access, "frontend timeout for HTTP interface" );
505                 p_sys->b_request_frontend_info = false;
506                 p_sys->psz_frontend_info = strdup( "Timeout getting info\n" );
507             }
508             if ( p_sys->b_request_mmi_info )
509             {
510                 msg_Warn( p_access, "MMI timeout for HTTP interface" );
511                 p_sys->b_request_mmi_info = false;
512                 p_sys->psz_mmi_info = strdup( "Timeout getting info\n" );
513             }
514             vlc_cond_signal( &p_sys->httpd_cond );
515             vlc_mutex_unlock( &p_sys->httpd_mutex );
516         }
517
518         if ( p_sys->b_request_frontend_info )
519         {
520             FrontendStatus( p_access );
521         }
522
523         if ( p_sys->b_request_mmi_info )
524         {
525             CAMStatus( p_access );
526         }
527 #endif
528
529         if ( p_sys->i_frontend_timeout && mdate() > p_sys->i_frontend_timeout )
530         {
531             msg_Warn( p_access, "no lock, tuning again" );
532             FrontendSet( p_access );
533         }
534
535         if ( ufds[0].revents )
536         {
537             p_block = block_New( p_access,
538                                  p_sys->i_read_once * TS_PACKET_SIZE );
539             if( ( i_ret = read( p_sys->i_handle, p_block->p_buffer,
540                                 p_sys->i_read_once * TS_PACKET_SIZE ) ) <= 0 )
541             {
542                 msg_Warn( p_access, "read failed (%m)" );
543                 block_Release( p_block );
544                 continue;
545             }
546             p_block->i_buffer = i_ret;
547             break;
548         }
549     }
550
551     if( p_sys->i_read_once < DVB_READ_ONCE )
552         p_sys->i_read_once++;
553
554     /* Update moderatly the signal properties */
555     if( (p_sys->i_stat_counter++ % 100) == 0 )
556         p_access->info.i_update |= INPUT_UPDATE_SIGNAL;
557
558     return p_block;
559 }
560
561 /*****************************************************************************
562  * BlockScan:
563  *****************************************************************************/
564 static block_t *BlockScan( access_t *p_access )
565 {
566     access_sys_t *p_sys = p_access->p_sys;
567     scan_t *p_scan = &p_sys->scan;
568     scan_configuration_t cfg;
569     scan_session_t session;
570
571     /* */
572     if( scan_Next( p_scan, &cfg ) )
573     {
574         const bool b_first_eof = !p_access->info.b_eof;
575
576         if( b_first_eof )
577             msg_Warn( p_access, "Scanning finished" );
578
579         /* */
580         p_access->info.b_eof = true;
581         return b_first_eof ? scan_GetM3U( p_scan ) : NULL;
582     }
583
584     /* */
585
586     if( scan_session_Init( VLC_OBJECT(p_access), &session, &cfg ) )
587         return NULL;
588
589     /* */
590     msg_Dbg( p_access, "Scanning frequency %d", cfg.i_frequency );
591     var_SetInteger( p_access, "dvb-frequency", cfg.i_frequency );
592     var_SetInteger( p_access, "dvb-bandwidth", cfg.i_bandwidth );
593
594     /* Setting frontend parameters for tuning the hardware */
595     if( FrontendSet( p_access ) < 0 )
596     {
597         msg_Err( p_access, "Failed to tune the frontend" );
598         p_access->info.b_eof = true;
599         return NULL;
600     }
601
602     /* */
603     int64_t i_scan_start = mdate();
604
605     bool b_has_dvb_signal = false;
606     bool b_has_lock = false;
607     int i_best_snr = -1;
608
609     for ( ; ; )
610     {
611         struct pollfd ufds[2];
612         int i_ret;
613
614         /* Initialize file descriptor sets */
615         memset (ufds, 0, sizeof (ufds));
616         ufds[0].fd = p_sys->i_handle;
617         ufds[0].events = POLLIN;
618         ufds[1].fd = p_sys->i_frontend_handle;
619         ufds[1].events = POLLPRI;
620
621         /* We'll wait 0.1 second if nothing happens */
622         /* Find if some data is available */
623         i_ret = poll( ufds, 2, 100 );
624
625         if( !vlc_object_alive (p_access) || scan_IsCancelled( p_scan ) )
626             break;
627
628         if( i_ret <= 0 )
629         {
630             const mtime_t i_scan_time = mdate() - i_scan_start;
631             frontend_status_t status;
632
633             FrontendGetStatus( p_access, &status );
634
635             b_has_dvb_signal |= status.b_has_carrier;
636             b_has_lock |= status.b_has_lock;
637
638             if( ( !b_has_dvb_signal && i_scan_time > DVB_SCAN_MAX_SIGNAL_TIME ) ||
639                 ( !b_has_lock && i_scan_time > DVB_SCAN_MAX_LOCK_TIME ) ||
640                 ( i_scan_time > DVB_SCAN_MAX_PROBE_TIME ) )
641             {
642                 msg_Dbg( p_access, "timed out scanning current frequency (s=%d l=%d)", b_has_dvb_signal, b_has_lock );
643                 break;
644             }
645         }
646
647         if( i_ret < 0 )
648         {
649             if( errno == EINTR )
650                 continue;
651
652             msg_Err( p_access, "poll error: %m" );
653             scan_session_Clean( p_scan, &session );
654
655             p_access->info.b_eof = true;
656             return NULL;
657         }
658
659         if( ufds[1].revents )
660         {
661             frontend_statistic_t stat;
662
663             FrontendPoll( p_access );
664
665             if( !FrontendGetStatistic( p_access, &stat ) )
666             {
667                 if( stat.i_snr > i_best_snr )
668                     i_best_snr = stat.i_snr;
669             }
670         }
671
672         if ( p_sys->i_frontend_timeout && mdate() > p_sys->i_frontend_timeout )
673         {
674             msg_Warn( p_access, "no lock, tuning again" );
675             FrontendSet( p_access );
676         }
677
678         if ( ufds[0].revents )
679         {
680             const int i_read_once = 1;
681             block_t *p_block = block_New( p_access, i_read_once * TS_PACKET_SIZE );
682
683             if( ( i_ret = read( p_sys->i_handle, p_block->p_buffer,
684                                 i_read_once * TS_PACKET_SIZE ) ) <= 0 )
685             {
686                 msg_Warn( p_access, "read failed (%m)" );
687                 block_Release( p_block );
688                 continue;
689             }
690             p_block->i_buffer = i_ret;
691
692             /* */
693             if( scan_session_Push( &session, p_block ) )
694             {
695                 msg_Dbg( p_access, "finished scanning current frequency" );
696                 break;
697             }
698         }
699     }
700
701     /* */
702     if( i_best_snr > 0 )
703         scan_service_SetSNR( &session, i_best_snr );
704
705     scan_session_Clean( p_scan, &session );
706     return NULL;
707 }
708
709 /*****************************************************************************
710  * Control:
711  *****************************************************************************/
712 static int Control( access_t *p_access, int i_query, va_list args )
713 {
714     access_sys_t *p_sys = p_access->p_sys;
715     bool         *pb_bool, b_bool;
716     int          i_int;
717     int64_t      *pi_64;
718     double       *pf1, *pf2;
719     dvbpsi_pmt_t *p_pmt;
720     frontend_statistic_t stat;
721
722     switch( i_query )
723     {
724         /* */
725         case ACCESS_CAN_SEEK:
726         case ACCESS_CAN_FASTSEEK:
727         case ACCESS_CAN_PAUSE:
728         case ACCESS_CAN_CONTROL_PACE:
729             pb_bool = (bool*)va_arg( args, bool* );
730             *pb_bool = false;
731             break;
732         /* */
733         case ACCESS_GET_PTS_DELAY:
734             pi_64 = (int64_t*)va_arg( args, int64_t * );
735             *pi_64 = var_GetInteger( p_access, "dvb-caching" ) * 1000;
736             break;
737
738         /* */
739         case ACCESS_SET_PAUSE_STATE:
740         case ACCESS_GET_TITLE_INFO:
741         case ACCESS_SET_TITLE:
742         case ACCESS_SET_SEEKPOINT:
743         case ACCESS_GET_CONTENT_TYPE:
744             return VLC_EGENERIC;
745
746         case ACCESS_GET_SIGNAL:
747             pf1 = (double*)va_arg( args, double * );
748             pf2 = (double*)va_arg( args, double * );
749
750             *pf1 = *pf2 = 0;
751             if( !FrontendGetStatistic( p_access, &stat ) )
752             {
753                 *pf1 = (double)stat.i_snr / 65535.0;
754                 *pf2 = (double)stat.i_signal_strenth / 65535.0;
755             }
756             return VLC_SUCCESS;
757
758         case ACCESS_SET_PRIVATE_ID_STATE:
759             if( p_sys->b_scan_mode )
760                 return VLC_EGENERIC;
761
762             i_int  = (int)va_arg( args, int );               /* Private data (pid for now)*/
763             b_bool = (bool)va_arg( args, int ); /* b_selected */
764             if( !p_sys->b_budget_mode )
765             {
766                 /* FIXME we may want to give the real type (me ?, I don't ;) */
767                 if( b_bool )
768                     FilterSet( p_access, i_int, OTHER_TYPE );
769                 else
770                     FilterUnsetPID( p_access, i_int );
771             }
772             break;
773
774         case ACCESS_SET_PRIVATE_ID_CA:
775             if( p_sys->b_scan_mode )
776                 return VLC_EGENERIC;
777
778             p_pmt = (dvbpsi_pmt_t *)va_arg( args, dvbpsi_pmt_t * );
779
780             CAMSet( p_access, p_pmt );
781             break;
782
783         default:
784             msg_Warn( p_access, "unimplemented query in control" );
785             return VLC_EGENERIC;
786
787     }
788     return VLC_SUCCESS;
789 }
790
791 /*****************************************************************************
792  * FilterSet/FilterUnset:
793  *****************************************************************************/
794 static void FilterSet( access_t *p_access, int i_pid, int i_type )
795 {
796     access_sys_t *p_sys = p_access->p_sys;
797     int i;
798
799     /* Find first free slot */
800     for( i = 0; i < MAX_DEMUX; i++ )
801     {
802         if( !p_sys->p_demux_handles[i].i_type )
803             break;
804
805         if( p_sys->p_demux_handles[i].i_pid == i_pid )
806             return; /* Already set */
807     }
808
809     if( i >= MAX_DEMUX )
810     {
811         msg_Err( p_access, "no free p_demux_handles !" );
812         return;
813     }
814
815     if( DMXSetFilter( p_access, i_pid,
816                            &p_sys->p_demux_handles[i].i_handle, i_type ) )
817     {
818         msg_Err( p_access, "DMXSetFilter failed" );
819         return;
820     }
821     p_sys->p_demux_handles[i].i_type = i_type;
822     p_sys->p_demux_handles[i].i_pid = i_pid;
823
824     if( p_sys->i_read_once < DVB_READ_ONCE )
825         p_sys->i_read_once++;
826 }
827
828 static void FilterUnset( access_t *p_access, int i_max )
829 {
830     access_sys_t *p_sys = p_access->p_sys;
831     int i;
832
833     for( i = 0; i < i_max; i++ )
834     {
835         if( p_sys->p_demux_handles[i].i_type )
836         {
837             DMXUnsetFilter( p_access, p_sys->p_demux_handles[i].i_handle );
838             p_sys->p_demux_handles[i].i_type = 0;
839         }
840     }
841 }
842
843 static void FilterUnsetPID( access_t *p_access, int i_pid )
844 {
845     access_sys_t *p_sys = p_access->p_sys;
846     int i;
847
848     for( i = 0; i < MAX_DEMUX; i++ )
849     {
850         if( p_sys->p_demux_handles[i].i_type &&
851             p_sys->p_demux_handles[i].i_pid == i_pid )
852         {
853             DMXUnsetFilter( p_access, p_sys->p_demux_handles[i].i_handle );
854             p_sys->p_demux_handles[i].i_type = 0;
855         }
856     }
857 }
858
859 /*****************************************************************************
860  * VarInit/ParseMRL:
861  *****************************************************************************/
862 static void VarInit( access_t *p_access )
863 {
864     /* */
865     var_Create( p_access, "dvb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
866
867     /* */
868     var_Create( p_access, "dvb-adapter", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
869     var_Create( p_access, "dvb-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
870     var_Create( p_access, "dvb-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
871     var_Create( p_access, "dvb-inversion", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
872     var_Create( p_access, "dvb-probe", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
873     var_Create( p_access, "dvb-budget-mode", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
874
875     /* */
876     var_Create( p_access, "dvb-satno", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
877     var_Create( p_access, "dvb-voltage", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
878     var_Create( p_access, "dvb-high-voltage", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
879     var_Create( p_access, "dvb-tone", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
880     var_Create( p_access, "dvb-fec", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
881     var_Create( p_access, "dvb-srate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
882     var_Create( p_access, "dvb-lnb-lof1", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
883     var_Create( p_access, "dvb-lnb-lof2", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
884     var_Create( p_access, "dvb-lnb-slof", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
885
886     /* */
887     var_Create( p_access, "dvb-modulation", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
888
889     /* */
890     var_Create( p_access, "dvb-code-rate-hp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
891     var_Create( p_access, "dvb-code-rate-lp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
892     var_Create( p_access, "dvb-bandwidth", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
893     var_Create( p_access, "dvb-transmission", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
894     var_Create( p_access, "dvb-guard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
895     var_Create( p_access, "dvb-hierarchy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
896
897 #ifdef ENABLE_HTTPD
898     var_Create( p_access, "dvb-http-host", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
899     var_Create( p_access, "dvb-http-user", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
900     var_Create( p_access, "dvb-http-password", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
901     var_Create( p_access, "dvb-http-acl", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
902     var_Create( p_access, "dvb-http-intf-cert", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
903     var_Create( p_access, "dvb-http-intf-key", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
904     var_Create( p_access, "dvb-http-intf-ca", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
905     var_Create( p_access, "dvb-http-intf-crl", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
906 #endif
907 }
908
909 /* */
910 static int ParseMRL( access_t *p_access )
911 {
912     char *psz_dup = strdup( p_access->psz_path );
913     char *psz_parser = psz_dup;
914     char *psz_next;
915     vlc_value_t         val;
916
917 #define GET_OPTION_INT( option )                                            \
918     if ( !strncmp( psz_parser, option "=", strlen(option "=") ) )           \
919     {                                                                       \
920         val.i_int = strtol( psz_parser + strlen(option "="), &psz_parser,   \
921                             0 );                                            \
922         var_Set( p_access, "dvb-" option, val );                            \
923     }
924
925 #define GET_OPTION_BOOL( option )                                           \
926     if ( !strncmp( psz_parser, option "=", strlen(option "=") ) )           \
927     {                                                                       \
928         val.b_bool = strtol( psz_parser + strlen(option "="), &psz_parser,  \
929                              0 );                                           \
930         var_Set( p_access, "dvb-" option, val );                            \
931     }
932
933     /* Test for old syntax */
934     strtol( psz_parser, &psz_next, 10 );
935     if( psz_next != psz_parser )
936     {
937         msg_Err( p_access, "the DVB input old syntax is deprecated, use vlc "
938                           "-p dvb to see an explanation of the new syntax" );
939         dialog_FatalWait( p_access, _("Input syntax is deprecated"),
940             _("The given syntax is deprecated. Run \"vlc -p dvb\" to see an " \
941                 "explanation of the new syntax.") );
942         free( psz_dup );
943         return VLC_EGENERIC;
944     }
945
946     while( *psz_parser )
947     {
948         GET_OPTION_INT("adapter")
949         else GET_OPTION_INT("device")
950         else GET_OPTION_INT("frequency")
951         else GET_OPTION_INT("inversion")
952         else GET_OPTION_BOOL("probe")
953         else GET_OPTION_BOOL("budget-mode")
954
955         else GET_OPTION_INT("voltage")
956         else GET_OPTION_BOOL("high-voltage")
957         else GET_OPTION_INT("tone")
958         else GET_OPTION_INT("satno")
959         else GET_OPTION_INT("fec")
960         else GET_OPTION_INT("srate")
961         else GET_OPTION_INT("lnb-lof1")
962         else GET_OPTION_INT("lnb-lof2")
963         else GET_OPTION_INT("lnb-slof")
964
965         else GET_OPTION_INT("modulation")
966
967         else GET_OPTION_INT("code-rate-hp")
968         else GET_OPTION_INT("code-rate-lp")
969         else GET_OPTION_INT("bandwidth")
970         else GET_OPTION_INT("transmission")
971         else GET_OPTION_INT("guard")
972         else GET_OPTION_INT("hierarchy")
973
974         /* Redundant with voltage but much easier to use */
975         else if( !strncmp( psz_parser, "polarization=",
976                            strlen( "polarization=" ) ) )
977         {
978             psz_parser += strlen( "polarization=" );
979             if ( *psz_parser == 'V' || *psz_parser == 'v' )
980                 val.i_int = 13;
981             else if ( *psz_parser == 'H' || *psz_parser == 'h' )
982                 val.i_int = 18;
983             else
984             {
985                 msg_Err( p_access, "illegal polarization %c", *psz_parser );
986                 dialog_Fatal( p_access, _("Invalid polarization"),
987                                 _("The provided polarization \"%c\" is not valid."),
988                                 *psz_parser );
989                 free( psz_dup );
990                 return VLC_EGENERIC;
991             }
992             var_Set( p_access, "dvb-voltage", val );
993         }
994         else
995         {
996             msg_Err( p_access, "unknown option (%s)", psz_parser );
997             free( psz_dup );
998             return VLC_EGENERIC;
999         }
1000
1001         if ( *psz_parser )
1002             psz_parser++;
1003     }
1004 #undef GET_OPTION_INT
1005 #undef GET_OPTION_BOOL
1006
1007     free( psz_dup );
1008     return VLC_SUCCESS;
1009 }
1010