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