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