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