]> git.sesse.net Git - vlc/blob - modules/demux/playlist/dvb.c
modules/demux/playlist/*.c: Use the new playlist independant way to announce sub...
[vlc] / modules / demux / playlist / dvb.c
1 /*****************************************************************************
2  * dvb.c : DVB channel list import (szap/tzap/czap compatible channel lists)
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_interface.h>
30 #include <vlc_charset.h>
31
32 #include "playlist.h"
33
34 #ifndef LONG_MAX
35 #   define LONG_MAX 2147483647L
36 #   define LONG_MIN (-LONG_MAX-1)
37 #endif
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int Demux( demux_t *p_demux);
43 static int Control( demux_t *p_demux, int i_query, va_list args );
44
45 static int ParseLine( char *, char **, char ***, int *);
46
47 /*****************************************************************************
48  * Import_DVB: main import function
49  *****************************************************************************/
50 int E_(Import_DVB)( vlc_object_t *p_this )
51 {
52     demux_t *p_demux = (demux_t *)p_this;
53     uint8_t *p_peek;
54     int     i_peek;
55     vlc_bool_t b_valid = VLC_FALSE;
56
57     if( !demux2_IsPathExtension( p_demux, ".conf" ) && !p_demux->b_force )
58         return VLC_EGENERIC;
59
60     /* Check if this really is a channels file */
61     if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
62     {
63         char psz_line[1024+1];
64         int i;
65
66         for( i = 0; i < i_peek; i++ )
67         {
68             if( p_peek[i] == '\n' ) break;
69             psz_line[i] = p_peek[i];
70         }
71         psz_line[i] = 0;
72
73         if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = VLC_TRUE;
74     }
75
76     if( !b_valid ) return VLC_EGENERIC;
77
78     msg_Dbg( p_demux, "found valid DVB conf playlist file");
79     p_demux->pf_control = Control;
80     p_demux->pf_demux = Demux;
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Deactivate: frees unused data
87  *****************************************************************************/
88 void E_(Close_DVB)( vlc_object_t *p_this )
89 {
90 }
91
92 /*****************************************************************************
93  * Demux: The important stuff
94  *****************************************************************************/
95 static int Demux( demux_t *p_demux )
96 {
97     char       *psz_line;
98     input_item_t *p_input;
99     INIT_PLAYLIST_STUFF;
100
101     while( (psz_line = stream_ReadLine( p_demux->s )) )
102     {
103         char **ppsz_options = NULL;
104         int  i, i_options = 0;
105         char *psz_name = NULL;
106
107         if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
108         {
109             free( psz_line );
110             continue;
111         }
112
113         EnsureUTF8( psz_name );
114
115         p_input = input_ItemNewExt( p_playlist, "dvb:", psz_name, 0, NULL, -1 );
116         for( i = 0; i< i_options; i++ )
117         {
118             EnsureUTF8( ppsz_options[i] );
119             input_ItemAddOption( p_input, ppsz_options[i] );
120         }
121         input_ItemAddSubItem( p_current_input, p_input );
122
123         while( i_options-- ) free( ppsz_options[i_options] );
124         if( ppsz_options ) free( ppsz_options );
125
126         free( psz_line );
127     }
128
129     HANDLE_PLAY_AND_RELEASE;
130     return -1; /* Needed for correct operation of go back */
131 }
132
133 static struct
134 {
135     const char *psz_name;
136     const char *psz_option;
137
138 } dvb_options[] =
139 {
140     { "INVERSION_OFF", "dvb-inversion=0" },
141     { "INVERSION_ON", "dvb-inversion=1" },
142     { "INVERSION_AUTO", "dvb-inversion=2" },
143
144     { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
145     { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
146     { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
147     { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
148
149     { "FEC_NONE", "dvb-fec=0" },
150     { "FEC_1_2", "dvb-fec=1" },
151     { "FEC_2_3", "dvb-fec=2" },
152     { "FEC_3_4", "dvb-fec=3" },
153     { "FEC_4_5", "dvb-fec=4" },
154     { "FEC_5_6", "dvb-fec=5" },
155     { "FEC_6_7", "dvb-fec=6" },
156     { "FEC_7_8", "dvb-fec=7" },
157     { "FEC_8_9", "dvb-fec=8" },
158     { "FEC_AUTO", "dvb-fec=9" },
159
160     { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
161     { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
162     { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
163     { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
164     { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
165
166     { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
167     { "HIERARCHY_1", "dvb-hierarchy=1" },
168     { "HIERARCHY_2", "dvb-hierarchy=2" },
169     { "HIERARCHY_4", "dvb-hierarchy=4" },
170
171     { "QPSK", "dvb-modulation=-1" },
172     { "QAM_AUTO", "dvb-modulation=0" },
173     { "QAM_16", "dvb-modulation=16" },
174     { "QAM_32", "dvb-modulation=32" },
175     { "QAM_64", "dvb-modulation=64" },
176     { "QAM_128", "dvb-modulation=128" },
177     { "QAM_256", "dvb-modulation=256" },
178
179     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
180     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
181     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
182     { 0, 0 }
183
184 };
185
186 static int ParseLine( char *psz_line, char **ppsz_name,
187                       char ***pppsz_options, int *pi_options )
188 {
189     char *psz_name = 0, *psz_parse = psz_line;
190     int i_count = 0, i_program = 0, i_frequency = 0;
191     vlc_bool_t b_valid = VLC_FALSE;
192
193     if( pppsz_options ) *pppsz_options = 0;
194     if( pi_options ) *pi_options = 0;
195     if( ppsz_name ) *ppsz_name = 0;
196
197     /* Skip leading tabs and spaces */
198     while( *psz_parse == ' ' || *psz_parse == '\t' ||
199            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
200
201     /* Ignore comments */
202     if( *psz_parse == '#' ) return VLC_FALSE;
203
204     while( psz_parse )
205     {
206         const char *psz_option = 0;
207         char *psz_end = strchr( psz_parse, ':' );
208         if( psz_end ) { *psz_end = 0; psz_end++; }
209
210         if( i_count == 0 )
211         {
212             /* Channel name */
213             psz_name = psz_parse;
214         }
215         else if( i_count == 1 )
216         {
217             /* Frequency */
218             char *psz_end;
219             long i_value;
220
221             i_value = strtol( psz_parse, &psz_end, 10 );
222             if( psz_end == psz_parse ||
223                 i_value == LONG_MAX || i_value == LONG_MIN ) break;
224
225             i_frequency = i_value;
226         }
227         else
228         {
229             int i;
230
231             /* Check option name with our list */
232             for( i = 0; dvb_options[i].psz_name; i++ )
233             {
234                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
235                 {
236                     psz_option = dvb_options[i].psz_option;
237
238                     /* If we recognize one of the strings, then we are sure
239                      * the data is really valid (ie. a channels file). */
240                     b_valid = VLC_TRUE;
241                     break;
242                 }
243             }
244
245             if( !psz_option )
246             {
247                 /* Option not recognized, test if it is a number */
248                 char *psz_end;
249                 long i_value;
250
251                 i_value = strtol( psz_parse, &psz_end, 10 );
252                 if( psz_end != psz_parse &&
253                     i_value != LONG_MAX && i_value != LONG_MIN )
254                 {
255                     i_program = i_value;
256                 }
257             }
258         }
259
260         if( psz_option && pppsz_options && pi_options )
261         {
262             char *psz_dup = strdup( psz_option );
263             if (psz_dup != NULL)
264                 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
265                              psz_dup );
266         }
267
268         psz_parse = psz_end;
269         i_count++;
270     }
271
272     if( !b_valid && pppsz_options && pi_options )
273     {
274         /* This isn't a valid channels file, cleanup everything */
275         while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
276         if( *pppsz_options ) free( *pppsz_options );
277         *pppsz_options = 0; *pi_options = 0;
278     }
279
280     if( i_program && pppsz_options && pi_options )
281     {
282         char *psz_option;
283
284         asprintf( &psz_option, "program=%i", i_program );
285         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
286                      psz_option );
287     }
288     if( i_frequency && pppsz_options && pi_options )
289     {
290         char *psz_option;
291
292         asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
293         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
294                      psz_option );
295     }
296     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
297
298     return b_valid;
299 }
300
301 static int Control( demux_t *p_demux, int i_query, va_list args )
302 {
303     return VLC_EGENERIC;
304 }