]> git.sesse.net Git - vlc/blob - modules/demux/playlist/dvb.c
input_item: compress two functions into one and rename "Add" into "Post" for clarity...
[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_node_AppendItem( p_subitems, p_input );
126         vlc_gc_decref( p_input );
127
128         while( i_options-- )
129             free( ppsz_options[i_options] );
130         free( ppsz_options );
131
132         free( psz_line );
133     }
134
135     input_item_node_PostAndDelete( p_subitems );
136
137     vlc_gc_decref(p_current_input);
138     return 0; /* Needed for correct operation of go back */
139 }
140
141 static const struct
142 {
143     const char *psz_name;
144     const char *psz_option;
145
146 } dvb_options[] =
147 {
148     { "INVERSION_OFF", "dvb-inversion=0" },
149     { "INVERSION_ON", "dvb-inversion=1" },
150     { "INVERSION_AUTO", "dvb-inversion=2" },
151
152     { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
153     { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
154     { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
155     { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
156
157     { "FEC_NONE", "dvb-fec=0" },
158     { "FEC_1_2", "dvb-fec=1" },
159     { "FEC_2_3", "dvb-fec=2" },
160     { "FEC_3_4", "dvb-fec=3" },
161     { "FEC_4_5", "dvb-fec=4" },
162     { "FEC_5_6", "dvb-fec=5" },
163     { "FEC_6_7", "dvb-fec=6" },
164     { "FEC_7_8", "dvb-fec=7" },
165     { "FEC_8_9", "dvb-fec=8" },
166     { "FEC_AUTO", "dvb-fec=9" },
167
168     { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
169     { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
170     { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
171     { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
172     { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
173
174     { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
175     { "HIERARCHY_1", "dvb-hierarchy=1" },
176     { "HIERARCHY_2", "dvb-hierarchy=2" },
177     { "HIERARCHY_4", "dvb-hierarchy=4" },
178
179     { "QPSK", "dvb-modulation=-1" },
180     { "QAM_AUTO", "dvb-modulation=0" },
181     { "QAM_16", "dvb-modulation=16" },
182     { "QAM_32", "dvb-modulation=32" },
183     { "QAM_64", "dvb-modulation=64" },
184     { "QAM_128", "dvb-modulation=128" },
185     { "QAM_256", "dvb-modulation=256" },
186     { "8VSB", "dvb-modulation=8"  },
187     { "16VSB", "dvb-modulation=16"  },
188
189     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
190     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
191     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
192     { 0, 0 }
193
194 };
195
196 static int ParseLine( char *psz_line, char **ppsz_name,
197                       char ***pppsz_options, int *pi_options )
198 {
199     char *psz_name = NULL, *psz_parse = psz_line;
200     int i_count = 0, i_program = 0, i_frequency = 0, i_symbolrate = 0;
201     bool b_valid = false;
202
203     if( pppsz_options ) *pppsz_options = NULL;
204     if( pi_options ) *pi_options = 0;
205     if( ppsz_name ) *ppsz_name = NULL;
206
207     /* Skip leading tabs and spaces */
208     while( *psz_parse == ' ' || *psz_parse == '\t' ||
209            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
210
211     /* Ignore comments */
212     if( *psz_parse == '#' ) return false;
213
214     while( psz_parse )
215     {
216         const char *psz_option = NULL;
217         char *psz_option_end = strchr( psz_parse, ':' );
218         if( psz_option_end ) { *psz_option_end = 0; psz_option_end++; }
219
220         if( i_count == 0 )
221         {
222             /* Channel name */
223             psz_name = psz_parse;
224         }
225         else if( i_count == 1 )
226         {
227             /* Frequency */
228             char *psz_end;
229             long i_value;
230
231             i_value = strtol( psz_parse, &psz_end, 10 );
232             if( psz_end == psz_parse ||
233                 i_value == LONG_MAX || i_value == LONG_MIN ) break;
234
235             i_frequency = i_value;
236         }
237         else
238         {
239             int i;
240
241             /* Check option name with our list */
242             for( i = 0; dvb_options[i].psz_name; i++ )
243             {
244                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
245                 {
246                     psz_option = dvb_options[i].psz_option;
247
248                     /* If we recognize one of the strings, then we are sure
249                      * the data is really valid (ie. a channels file). */
250                     b_valid = true;
251                     break;
252                 }
253             }
254
255             if( !psz_option )
256             {
257                 /* Option not recognized, test if it is a number */
258                 char *psz_end;
259                 long i_value;
260
261                 i_value = strtol( psz_parse, &psz_end, 10 );
262                 if( psz_end != psz_parse &&
263                     i_value != LONG_MAX && i_value != LONG_MIN &&
264                     !i_symbolrate )
265                 {
266                     i_symbolrate = i_value;
267                 }
268                 else if( psz_end != psz_parse &&
269                     i_value != LONG_MAX && i_value != LONG_MIN )
270                 {
271                     i_program = i_value;
272                 }
273             }
274         }
275
276         if( psz_option && pppsz_options && pi_options )
277         {
278             char *psz_dup = strdup( psz_option );
279             if (psz_dup != NULL)
280                 INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
281                              psz_dup );
282         }
283
284         psz_parse = psz_option_end;
285         i_count++;
286     }
287
288     if( !b_valid && pppsz_options && pi_options )
289     {
290         /* This isn't a valid channels file, cleanup everything */
291         while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
292         free( *pppsz_options );
293         *pppsz_options = NULL; *pi_options = 0;
294     }
295
296     if( i_program && pppsz_options && pi_options )
297     {
298         char *psz_option;
299
300         if( asprintf( &psz_option, "program=%i", i_program ) != -1 )
301             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
302                          psz_option );
303     }
304     if( i_frequency && pppsz_options && pi_options )
305     {
306         char *psz_option;
307
308         if( asprintf( &psz_option, "dvb-frequency=%i", i_frequency ) != -1 )
309             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
310                          psz_option );
311     }
312     if( i_symbolrate && pppsz_options && pi_options )
313     {
314         char *psz_option;
315
316         if( asprintf( &psz_option, "dvb-srate=%i", i_symbolrate ) != -1 )
317             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
318                          psz_option );
319     }
320     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
321
322     return b_valid;
323 }
324
325 static int Control( demux_t *p_demux, int i_query, va_list args )
326 {
327     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
328     return VLC_EGENERIC;
329 }