]> git.sesse.net Git - vlc/blob - modules/access/dvb/access.c
* include/modules_inner.h, include/modules.h: added a shortname field to the module...
[vlc] / modules / access / dvb / access.c
1 /*****************************************************************************
2  * access.c: DVB card input v4l2 only
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  *
6  * Authors: Johan Bilien <jobi@via.ecp.fr>
7  *          Jean-Paul Saman <jpsaman@wxs.nl>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34
35 #include "../../demux/mpeg/system.h"
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #endif
40
41 #include <fcntl.h>
42 #include <sys/types.h>
43
44 #ifdef HAVE_ERRNO_H
45 #    include <string.h>
46 #    include <errno.h>
47 #endif
48
49 #ifdef STRNCASECMP_IN_STRINGS_H
50 #   include <strings.h>
51 #endif
52
53 #include "dvb.h"
54
55 #define SATELLITE_READ_ONCE 3
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int     Open( vlc_object_t *p_this );
61 static void    Close( vlc_object_t *p_this );
62 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
63                               size_t i_len);
64 static int     SetArea    ( input_thread_t *, input_area_t * );
65 static int     SetProgram ( input_thread_t *, pgrm_descriptor_t * );
66 static void    Seek       ( input_thread_t *, off_t );
67 static void    AllocateDemux( input_thread_t * p_input, int i_pid,
68                               int i_type );
69 static void    CloseProgram( input_thread_t * p_input );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 #define CACHING_TEXT N_("Caching value in ms")
75 #define CACHING_LONGTEXT N_( \
76     "Allows you to modify the default caching value for dvb streams. This " \
77     "value should be set in millisecond units." )
78
79 #define PROGRAM_TEXT N_("Program to decode")
80 #define PROGRAM_LONGTEXT N_("This is a workaround for a bug in the input")
81
82 #define ADAPTER_TEXT N_("Adapter card to tune")
83 #define ADAPTER_LONGTEXT N_("Adapter cards have a device file in directory named /dev/dvb/adapter[n] with n>=0.")
84
85 #define DEVICE_TEXT N_("Device number to use on adapter")
86 #define DEVICE_LONGTEXT ""
87
88 #define FREQ_TEXT N_("Transponder/multiplex frequency")
89 #define FREQ_LONGTEXT N_("In kHz for DVB-S or Hz for DVB-C/T")
90
91 #define INVERSION_TEXT N_("Inversion mode")
92 #define INVERSION_LONGTEXT N_("Inversion mode [0=off, 1=on, 2=auto]")
93
94 #define PROBE_TEXT N_("Probe DVB card for capabilities")
95 #define PROBE_LONGTEXT N_("Some DVB cards do not like to be probed for their capabilities.")
96
97 #define LNB_LOF1_TEXT N_("Antenna lnb_lof1 (kHz)")
98 #define LNB_LOF1_LONGTEXT ""
99
100 #define LNB_LOF2_TEXT N_("Antenna lnb_lof2 (kHz)")
101 #define LNB_LOF2_LONGTEXT ""
102
103 #define LNB_SLOF_TEXT N_("Antenna lnb_slof (kHz)")
104 #define LNB_SLOF_LONGTEXT ""
105
106 /* Satellite */
107 #define BUDGET_TEXT N_("Budget mode")
108 #define BUDGET_LONGTEXT N_("This allows you to stream an entire transponder with a budget card. Budget mode is compatible with the ts2 demux.")
109
110 #define SATNO_TEXT N_("Satellite number in the Diseqc system")
111 #define SATNO_LONGTEXT N_("[0=no diseqc, 1-4=normal diseqc, -1=A, -2=B simple diseqc")
112
113 #define VOLTAGE_TEXT N_("LNB voltage")
114 #define VOLTAGE_LONGTEXT N_("In Volts [0, 13=vertical, 18=horizontal]")
115
116 #define TONE_TEXT N_("22 kHz tone")
117 #define TONE_LONGTEXT N_("[0=off, 1=on, -1=auto]")
118
119 #define FEC_TEXT N_("Transponder FEC")
120 #define FEC_LONGTEXT N_("FEC=Forward Error Correction mode [9=auto]")
121
122 #define SRATE_TEXT N_("Transponder symbol rate in kHz")
123 #define SRATE_LONGTEXT ""
124
125 /* Cable */
126 #define MODULATION_TEXT N_("Modulation type")
127 #define MODULATION_LONGTEXT N_("Modulation type for front-end device.")
128
129 /* Terrestrial */
130 #define CODE_RATE_HP_TEXT N_("Terrestrial high priority stream code rate (FEC)")
131 #define CODE_RATE_HP_LONGTEXT ""
132
133 #define CODE_RATE_LP_TEXT N_("Terrestrial low priority stream code rate (FEC)")
134 #define CODE_RATE_LP_LONGTEXT ""
135
136 #define BANDWIDTH_TEXT N_("Terrestrial bandwidth")
137 #define BANDWIDTH_LONGTEXT N_("Terrestrial bandwidth [0=auto,6,7,8 in MHz]")
138
139 #define GUARD_TEXT N_("Terrestrial guard interval")
140 #define GUARD_LONGTEXT ""
141
142 #define TRANSMISSION_TEXT N_("Terrestrial transmission mode")
143 #define TRANSMISSION_LONGTEXT ""
144
145 #define HIERARCHY_TEXT N_("Terrestrial hierarchy mode")
146 #define HIERARCHY_LONGTEXT ""
147
148 vlc_module_begin();
149     set_shortname( _("DVB") );
150     set_description( N_("DVB input with v4l2 support") );
151
152     add_integer( "dvb-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
153                  CACHING_LONGTEXT, VLC_TRUE );
154     add_integer( "dvb-adapter", 0, NULL, ADAPTER_TEXT, ADAPTER_LONGTEXT,
155                  VLC_FALSE );
156     add_integer( "dvb-device", 0, NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
157                  VLC_TRUE );
158     add_integer( "dvb-frequency", 11954000, NULL, FREQ_TEXT, FREQ_LONGTEXT,
159                  VLC_FALSE );
160     add_integer( "dvb-inversion", 2, NULL, INVERSION_TEXT, INVERSION_LONGTEXT,
161                  VLC_TRUE );
162     add_bool( "dvb-probe", 1, NULL, PROBE_TEXT, PROBE_LONGTEXT, VLC_TRUE );
163     add_integer( "dvb-lnb-lof1", 9750000, NULL, LNB_LOF1_TEXT,
164                  LNB_LOF1_LONGTEXT, VLC_TRUE );
165     add_integer( "dvb-lnb-lof2", 10600000, NULL, LNB_LOF2_TEXT,
166                  LNB_LOF2_LONGTEXT, VLC_TRUE );
167     add_integer( "dvb-lnb-slof", 11700000, NULL, LNB_SLOF_TEXT,
168                  LNB_SLOF_LONGTEXT, VLC_TRUE );
169     /* DVB-S (satellite) */
170     add_bool( "dvb-budget-mode", 0, NULL, BUDGET_TEXT, BUDGET_LONGTEXT,
171               VLC_TRUE );
172     add_integer( "dvb-satno", 0, NULL, SATNO_TEXT, SATNO_LONGTEXT,
173                  VLC_TRUE );
174     add_integer( "dvb-voltage", 13, NULL, VOLTAGE_TEXT, VOLTAGE_LONGTEXT,
175                  VLC_TRUE );
176     add_integer( "dvb-tone", -1, NULL, TONE_TEXT, TONE_LONGTEXT,
177                  VLC_TRUE );
178     add_integer( "dvb-fec", 9, NULL, FEC_TEXT, FEC_LONGTEXT, VLC_TRUE );
179     add_integer( "dvb-srate", 27500000, NULL, SRATE_TEXT, SRATE_LONGTEXT,
180                  VLC_FALSE );
181     /* DVB-T (terrestrial) */
182     add_integer( "dvb-modulation", 0, NULL, MODULATION_TEXT,
183                  MODULATION_LONGTEXT, VLC_TRUE );
184     /* DVB-T (terrestrial) */
185     add_integer( "dvb-code-rate-hp", 9, NULL, CODE_RATE_HP_TEXT,
186                  CODE_RATE_HP_LONGTEXT, VLC_TRUE );
187     add_integer( "dvb-code-rate-lp", 9, NULL, CODE_RATE_LP_TEXT,
188                  CODE_RATE_LP_LONGTEXT, VLC_TRUE );
189     add_integer( "dvb-bandwidth", 0, NULL, BANDWIDTH_TEXT, BANDWIDTH_LONGTEXT,
190                  VLC_TRUE );
191     add_integer( "dvb-guard", 0, NULL, GUARD_TEXT, GUARD_LONGTEXT, VLC_TRUE );
192     add_integer( "dvb-transmission", 0, NULL, TRANSMISSION_TEXT,
193                  TRANSMISSION_LONGTEXT, VLC_TRUE );
194     add_integer( "dvb-hierarchy", 0, NULL, HIERARCHY_TEXT, HIERARCHY_LONGTEXT,
195                  VLC_TRUE );
196
197     set_capability( "access", 0 );
198     add_shortcut( "dvb" );
199     add_shortcut( "dvb-s" );
200     add_shortcut( "qpsk" );
201     add_shortcut( "dvb-c" );
202     add_shortcut( "cable" );
203     add_shortcut( "dvb-t" );
204     add_shortcut( "terrestrial" );
205     add_shortcut( "satellite" );    /* compatibility with the interface. */
206     set_callbacks( Open, Close );
207 vlc_module_end();
208
209 /*****************************************************************************
210  * Open: open the frontend device
211  *****************************************************************************/
212 #define GET_OPTION_INT( option )                                            \
213     if ( !strncmp( psz_parser, option "=", strlen(option "=") ) )           \
214     {                                                                       \
215         val.i_int = strtol( psz_parser + strlen(option "="), &psz_parser,   \
216                             0 );                                            \
217         var_Set( p_input, "dvb-" option, val );                             \
218     }
219
220 #define GET_OPTION_BOOL( option )                                           \
221     if ( !strncmp( psz_parser, option "=", strlen(option "=") ) )           \
222     {                                                                       \
223         val.b_bool = strtol( psz_parser + strlen(option "="), &psz_parser,  \
224                              0 );                                           \
225         var_Set( p_input, "dvb-" option, val );                             \
226     }
227
228 static int Open( vlc_object_t *p_this )
229 {
230     input_thread_t *    p_input = (input_thread_t *)p_this;
231     thread_dvb_data_t * p_dvb;
232     char *              psz_parser;
233     char *              psz_next;
234     vlc_value_t         val;
235     int                 i_test;
236
237     /* Initialize structure */
238     p_dvb = (thread_dvb_data_t *)malloc( sizeof( thread_dvb_data_t ) );
239     if( p_dvb == NULL )
240     {
241         msg_Err( p_input, "out of memory" );
242         return -1;
243     }
244     memset( p_dvb, 0, sizeof(thread_dvb_data_t) );
245     p_input->p_access_data = (void *)p_dvb;
246
247     /* Register callback functions */
248     p_input->pf_read = Read;
249     p_input->pf_set_program = SetProgram;
250     p_input->pf_set_area = SetArea;
251     p_input->pf_seek = Seek;
252
253     /* Parse the options passed in command line */
254
255     psz_parser = strdup( p_input->psz_name );
256     if ( !psz_parser )
257     {
258         free( p_dvb );
259         return( -1 );
260     }
261
262     var_Create( p_input, "dvb-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
263     var_Get( p_input, "dvb-caching", &val );
264     p_input->i_pts_delay = val.i_int * 1000;
265
266     var_Create( p_input, "dvb-adapter", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
267     var_Create( p_input, "dvb-device", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
268     var_Create( p_input, "dvb-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
269     var_Create( p_input, "dvb-inversion", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
270     var_Create( p_input, "dvb-probe", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
271     var_Create( p_input, "dvb-lnb-lof1", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
272     var_Create( p_input, "dvb-lnb-lof2", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
273     var_Create( p_input, "dvb-lnb-slof", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
274
275     var_Create( p_input, "dvb-budget-mode", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
276     var_Create( p_input, "dvb-satno", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
277     var_Create( p_input, "dvb-voltage", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
278     var_Create( p_input, "dvb-tone", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
279     var_Create( p_input, "dvb-fec", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
280     var_Create( p_input, "dvb-srate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
281
282     var_Create( p_input, "dvb-modulation", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
283
284     var_Create( p_input, "dvb-code-rate-hp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
285     var_Create( p_input, "dvb-code-rate-lp", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
286     var_Create( p_input, "dvb-bandwidth", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
287     var_Create( p_input, "dvb-transmission", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
288     var_Create( p_input, "dvb-guard", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
289     var_Create( p_input, "dvb-hierarchy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
290
291     i_test = strtol( psz_parser, &psz_next, 10 );
292     if ( psz_next == psz_parser )
293     {
294         for ( ; ; )
295         {
296             GET_OPTION_INT("adapter")
297             else GET_OPTION_INT("device")
298             else GET_OPTION_INT("frequency")
299             else GET_OPTION_INT("inversion")
300             else GET_OPTION_BOOL("probe")
301             else GET_OPTION_INT("lnb-lof1")
302             else GET_OPTION_INT("lnb-lof2")
303             else GET_OPTION_INT("lnb-slof")
304
305             else GET_OPTION_BOOL("budget-mode")
306             else GET_OPTION_INT("voltage")
307             else GET_OPTION_INT("tone")
308             else GET_OPTION_INT("fec")
309             else GET_OPTION_INT("srate")
310
311             else GET_OPTION_INT("modulation")
312
313             else GET_OPTION_INT("code-rate-hp")
314             else GET_OPTION_INT("code-rate-lp")
315             else GET_OPTION_INT("bandwidth")
316             else GET_OPTION_INT("transmission")
317             else GET_OPTION_INT("guard")
318             else GET_OPTION_INT("hierarchy")
319
320             else if( !strncmp( psz_parser, "satno=",
321                                strlen( "satno=" ) ) )
322             {
323                 psz_parser += strlen( "satno=" );
324                 if ( *psz_parser == 'A' || *psz_parser == 'a' )
325                     val.i_int = -1;
326                 else if ( *psz_parser == 'B' || *psz_parser == 'b' )
327                     val.i_int = -2;
328                 else
329                     val.i_int = strtol( psz_parser, &psz_parser, 0 );
330                 var_Set( p_input, "dvb-satno", val );
331             }
332             /* Redundant with voltage but much easier to use */
333             else if( !strncmp( psz_parser, "polarization=",
334                                strlen( "polarization=" ) ) )
335             {
336                 psz_parser += strlen( "polarization=" );
337                 if ( *psz_parser == 'V' || *psz_parser == 'v' )
338                     val.i_int = 13;
339                 else if ( *psz_parser == 'H' || *psz_parser == 'h' )
340                     val.i_int = 18;
341                 else
342                 {
343                     msg_Err( p_input, "illegal polarization %c", *psz_parser );
344                     free( p_dvb );
345                     return -1;
346                 }
347                 var_Set( p_input, "dvb-voltage", val );
348             }
349             if ( *psz_parser )
350                 psz_parser++;
351             else
352                 break;
353         }
354     }
355     else
356     {
357         msg_Err( p_input, "the DVB input old syntax is deprecated, use vlc " \
358                  "-p dvb to see an explanation of the new syntax" );
359         free( p_dvb );
360         return -1;
361     }
362
363     /* Getting frontend info */
364     if ( E_(FrontendOpen)( p_input ) < 0 )
365     {
366         free( p_dvb );
367         return -1;
368     }
369
370     /* Setting frontend parameters for tuning the hardware */      
371     msg_Dbg( p_input, "trying to tune the frontend...");
372     if ( E_(FrontendSet)( p_input ) < 0 )
373     {
374         E_(FrontendClose)( p_input );
375         free( p_dvb );
376         return -1;
377     }
378
379     /* Opening DVR device */      
380     if ( E_(DVROpen)( p_input ) < 0 )
381     {
382         E_(FrontendClose)( p_input );
383         free( p_dvb );
384         return -1;
385     }
386
387     var_Get( p_input, "dvb-budget-mode", &val );
388     p_dvb->b_budget_mode = val.b_bool;
389     if ( val.b_bool )
390     {
391         msg_Dbg( p_input, "setting filter on all PIDs" );
392         AllocateDemux( p_input, 0x2000, OTHER_TYPE );
393     }
394     else
395     {
396         msg_Dbg( p_input, "setting filter on PAT" );
397         AllocateDemux( p_input, 0x0, OTHER_TYPE );
398     }
399
400     if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
401     {
402         msg_Err( p_input, "could not initialize stream structure" );
403         E_(FrontendClose)( p_input );
404         close( p_dvb->i_handle );
405         free( p_dvb );
406         return( -1 );
407     }
408
409     vlc_mutex_lock( &p_input->stream.stream_lock );
410
411     p_input->stream.b_pace_control = 0;
412     p_input->stream.b_seekable = 0;
413     p_input->stream.p_selected_area->i_tell = 0;
414
415     vlc_mutex_unlock( &p_input->stream.stream_lock );
416
417     p_input->i_mtu = SATELLITE_READ_ONCE * TS_PACKET_SIZE;
418     p_input->stream.i_method = INPUT_METHOD_SATELLITE;
419
420     return 0;
421 }
422
423 /*****************************************************************************
424  * Close : Close the device
425  *****************************************************************************/
426 static void Close( vlc_object_t *p_this )
427 {
428     input_thread_t *    p_input = (input_thread_t *)p_this;
429     thread_dvb_data_t * p_dvb = (thread_dvb_data_t *)p_input->p_access_data;
430
431     if ( !p_dvb->b_budget_mode )
432     {
433         CloseProgram( p_input );
434     }
435     if ( p_dvb->p_demux_handles[0].i_type )
436     {
437         E_(DMXUnsetFilter)( p_input, p_dvb->p_demux_handles[0].i_handle );
438         p_dvb->p_demux_handles[0].i_type = 0;
439     }
440     E_(DVRClose)( p_input );
441     E_(FrontendClose)( p_input );
442     free( p_dvb );
443 }
444
445 /*****************************************************************************
446  * Read: reads data from the satellite card
447  *****************************************************************************/
448 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
449                      size_t i_len )
450 {
451     thread_dvb_data_t * p_dvb = (thread_dvb_data_t *)p_input->p_access_data;
452     ssize_t i_ret;
453     vlc_value_t val;
454     struct timeval timeout;
455     fd_set fds;
456
457     if ( !p_dvb->b_budget_mode && !p_dvb->p_demux_handles[1].i_type )
458     {
459         int i_program;
460         unsigned int i;
461         var_Get( p_input, "program", &val );
462         i_program = val.i_int;
463
464         /* FIXME : this is not demux2-compatible */
465         for ( i = 0; i < p_input->stream.i_pgrm_number; i++ )
466         {
467             /* Only set a filter on the selected program : some boards
468              * (read: Dreambox) only have 8 filters, so you don't want to
469              * spend them on unwanted PMTs. --Meuuh */
470             if ( !i_program
471                    || p_input->stream.pp_programs[i]->i_number == i_program )
472             {
473                 msg_Dbg( p_input, "setting filter on PMT pid %d",
474                          p_input->stream.pp_programs[i]->pp_es[0]->i_id );
475                 AllocateDemux( p_input,
476                         p_input->stream.pp_programs[i]->pp_es[0]->i_id,
477                         OTHER_TYPE );
478             }
479         }
480     }
481
482     /* Find if some data is available. This won't work under Windows. */
483
484     /* Initialize file descriptor set */
485     FD_ZERO( &fds );
486     FD_SET( p_dvb->i_handle, &fds );
487
488     /* We'll wait 0.5 second if nothing happens */
489     timeout.tv_sec = 0;
490     timeout.tv_usec = 500000;
491
492     /* Find if some data is available */
493     while ( (i_ret = select( p_dvb->i_handle + 1, &fds,
494                              NULL, NULL, &timeout )) == 0
495              || (i_ret < 0 && errno == EINTR) )
496     {
497         FD_ZERO( &fds );
498         FD_SET( p_dvb->i_handle, &fds );
499         timeout.tv_sec = 0;
500         timeout.tv_usec = 500000;
501
502         if ( p_input->b_die || p_input->b_error )
503         {
504             return 0;
505         }
506     }
507
508     if ( i_ret < 0 )
509     {
510 #ifdef HAVE_ERRNO_H
511         msg_Err( p_input, "select error (%s)", strerror(errno) );
512 #else
513         msg_Err( p_input, "select error" );
514 #endif
515         return -1;
516     }
517
518     i_ret = read( p_dvb->i_handle, p_buffer, i_len );
519  
520     if( i_ret < 0 )
521     {
522 #ifdef HAVE_ERRNO_H
523         msg_Err( p_input, "read failed (%s)", strerror(errno) );
524 #else
525         msg_Err( p_input, "read failed" );
526 #endif
527     }
528
529     return i_ret;
530 }
531
532 /*****************************************************************************
533  * SetArea : Does nothing
534  *****************************************************************************/
535 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
536 {
537     return -1;
538 }
539
540 /*****************************************************************************
541  * SetProgram : Sets the card filters according to the selected program,
542  *              and makes the appropriate changes to stream structure.
543  *****************************************************************************/
544 static int SetProgram( input_thread_t    * p_input,
545                        pgrm_descriptor_t * p_new_prg )
546 {
547     thread_dvb_data_t * p_dvb = (thread_dvb_data_t *)p_input->p_access_data;
548     unsigned int i_es_index;
549     vlc_value_t val;
550     int i_video_type = VIDEO0_TYPE;
551     int i_audio_type = AUDIO0_TYPE;
552
553     if ( p_input->stream.p_selected_program )
554     {
555         for ( i_es_index = 0; /* 0 should be the PMT */
556                 i_es_index < p_input->stream.p_selected_program->i_es_number;
557                 i_es_index ++ )
558         {
559 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
560             if ( p_es->p_dec )
561             {
562                 input_UnselectES( p_input , p_es );
563             }
564 #undef p_es
565         }
566     }
567
568     if ( !p_dvb->b_budget_mode )
569     {
570         msg_Dbg( p_input, "unsetting filters on all pids" );
571         CloseProgram( p_input );
572         msg_Dbg( p_input, "setting filter on PMT pid %d",
573                  p_new_prg->pp_es[0]->i_id );
574         AllocateDemux( p_input, p_new_prg->pp_es[0]->i_id, OTHER_TYPE );
575     }
576
577     for ( i_es_index = 1; i_es_index < p_new_prg->i_es_number;
578           i_es_index++ )
579     {
580 #define p_es p_new_prg->pp_es[i_es_index]
581         switch( p_es->i_cat )
582         {
583         case VIDEO_ES:
584             if ( !p_dvb->b_budget_mode )
585             {
586                 msg_Dbg(p_input, "setting filter on video ES 0x%x",
587                         p_es->i_id);
588                 /* Always set the filter. This may seem a little odd, but
589                  * it allows you to stream the video with demuxstream
590                  * without having a decoder or a stream output behind.
591                  * The result is you'll sometimes filter a PID which you
592                  * don't really want, but in the most common cases it
593                  * should be OK. --Meuuh */
594                 AllocateDemux( p_input, p_es->i_id, i_video_type );
595                 i_video_type += TYPE_INTERVAL;
596             }
597             input_SelectES( p_input, p_es );
598             break;
599
600         case AUDIO_ES:
601             if ( !p_dvb->b_budget_mode )
602             {
603                 msg_Dbg(p_input, "setting filter on audio ES 0x%x",
604                         p_es->i_id);
605                 AllocateDemux( p_input, p_es->i_id, i_audio_type );
606                 i_audio_type += TYPE_INTERVAL;
607             }
608             input_SelectES( p_input, p_es );
609             break;
610         default:
611             if ( !p_dvb->b_budget_mode )
612             {
613                 msg_Dbg(p_input, "setting filter on other ES 0x%x",
614                         p_es->i_id);
615                 AllocateDemux( p_input, p_es->i_id, OTHER_TYPE );
616             }
617             input_SelectES( p_input, p_es );
618             break;
619         }
620 #undef p_es
621     }
622
623     p_input->stream.p_selected_program = p_new_prg;
624
625     /* Update the navigation variables without triggering a callback */
626     val.i_int = p_new_prg->i_number;
627     var_Change( p_input, "program", VLC_VAR_SETVALUE, &val, NULL );
628
629     return 0;
630 }
631
632 /*****************************************************************************
633  * Seek: does nothing (not a seekable stream
634  *****************************************************************************/
635 static void Seek( input_thread_t * p_input, off_t i_off )
636 {
637     ;
638 }
639
640 /*****************************************************************************
641  * AllocateDemux:
642  *****************************************************************************/
643 static void AllocateDemux( input_thread_t * p_input, int i_pid,
644                            int i_type )
645 {
646     thread_dvb_data_t * p_dvb = (thread_dvb_data_t *)p_input->p_access_data;
647     int                 i;
648
649     /* Find first free slot */
650     for ( i = 0; i < MAX_DEMUX; i++ )
651     {
652         if ( !p_dvb->p_demux_handles[i].i_type )
653         {
654             if ( E_(DMXSetFilter)( p_input, i_pid,
655                                    &p_dvb->p_demux_handles[i].i_handle,
656                                    i_type ) < 0 )
657             {
658                 break;
659             }
660             p_dvb->p_demux_handles[i].i_type = i_type;
661             p_dvb->p_demux_handles[i].i_pid = i_pid;
662             break;
663         }
664     }
665 }
666
667 /*****************************************************************************
668  * CloseProgram:
669  *****************************************************************************/
670 static void CloseProgram( input_thread_t * p_input )
671 {
672     thread_dvb_data_t * p_dvb = (thread_dvb_data_t *)p_input->p_access_data;
673     int                 i;
674
675     for ( i = 1; i < MAX_DEMUX; i++ )
676     {
677         if ( p_dvb->p_demux_handles[i].i_type )
678         {
679             E_(DMXUnsetFilter)( p_input, p_dvb->p_demux_handles[i].i_handle );
680             p_dvb->p_demux_handles[i].i_type = 0;
681         }
682     }
683 }
684
685