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