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