]> git.sesse.net Git - vlc/blob - modules/demux/playlist/dvb.c
0be77b3016d86e97a99dea9cb693df5664da3428
[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 <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/intf.h>
32 #include "charset.h"
33
34 #include "playlist.h"
35
36 #ifndef LONG_MAX
37 #   define LONG_MAX 2147483647L
38 #   define LONG_MIN (-LONG_MAX-1)
39 #endif
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int Demux( demux_t *p_demux);
45 static int Control( demux_t *p_demux, int i_query, va_list args );
46
47 static int ParseLine( char *, char **, char ***, int *);
48
49 /*****************************************************************************
50  * Import_DVB: main import function
51  *****************************************************************************/
52 int E_(Import_DVB)( vlc_object_t *p_this )
53 {
54     demux_t *p_demux = (demux_t *)p_this;
55     uint8_t *p_peek;
56     int     i_peek;
57     char    *psz_ext;
58     vlc_bool_t b_valid = VLC_FALSE;
59
60     psz_ext = strrchr ( p_demux->psz_path, '.' );
61
62     if( !( psz_ext && !strncasecmp( psz_ext, ".conf", 5 ) ) &&
63         !p_demux->b_force ) return VLC_EGENERIC;
64
65     /* Check if this really is a channels file */
66     if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
67     {
68         char psz_line[1024+1];
69         int i;
70
71         for( i = 0; i < i_peek; i++ )
72         {
73             if( p_peek[i] == '\n' ) break;
74             psz_line[i] = p_peek[i];
75         }
76         psz_line[i] = 0;
77
78         if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = VLC_TRUE;
79     }
80
81     if( !b_valid ) return VLC_EGENERIC;
82
83     msg_Dbg( p_demux, "found valid DVB conf playlist file");
84
85     p_demux->pf_control = Control;
86     p_demux->pf_demux = Demux;
87
88     return VLC_SUCCESS;
89 }
90
91 /*****************************************************************************
92  * Deactivate: frees unused data
93  *****************************************************************************/
94 void E_(Close_DVB)( vlc_object_t *p_this )
95 {
96 }
97
98 /*****************************************************************************
99  * Demux: The important stuff
100  *****************************************************************************/
101 static int Demux( demux_t *p_demux )
102 {
103     char       *psz_line;
104     INIT_PLAYLIST_STUFF;
105
106     while( (psz_line = stream_ReadLine( p_demux->s )) )
107     {
108         playlist_item_t *p_item;
109         char **ppsz_options = NULL;
110         int  i, 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
121         p_input = input_ItemNewExt( p_playlist, "dvb:", psz_name, 0, NULL, -1 );
122         for( i = 0; i< i_options; i++ )
123         {
124             EnsureUTF8( ppsz_options[i] );
125             vlc_input_item_AddOption( p_input, ppsz_options[i] );
126         }
127         playlist_AddWhereverNeeded( p_playlist, p_input, p_current, 
128                                     p_item_in_category,
129                                     (i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
130                                     PLAYLIST_APPEND );
131
132         while( i_options-- ) free( ppsz_options[i_options] );
133         if( ppsz_options ) free( ppsz_options );
134
135         free( psz_line );
136     }
137
138     HANDLE_PLAY_AND_RELEASE;
139     return VLC_SUCCESS;
140 }
141
142 static struct
143 {
144     char *psz_name;
145     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
188     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
189     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
190     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
191     { 0, 0 }
192
193 };
194
195 static int ParseLine( char *psz_line, char **ppsz_name,
196                       char ***pppsz_options, int *pi_options )
197 {
198     char *psz_name = 0, *psz_parse = psz_line;
199     int i_count = 0, i_program = 0, i_frequency = 0;
200     vlc_bool_t b_valid = VLC_FALSE;
201
202     if( pppsz_options ) *pppsz_options = 0;
203     if( pi_options ) *pi_options = 0;
204     if( ppsz_name ) *ppsz_name = 0;
205
206     /* Skip leading tabs and spaces */
207     while( *psz_parse == ' ' || *psz_parse == '\t' ||
208            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
209
210     /* Ignore comments */
211     if( *psz_parse == '#' ) return VLC_FALSE;
212
213     while( psz_parse )
214     {
215         char *psz_option = 0;
216         char *psz_end = strchr( psz_parse, ':' );
217         if( psz_end ) { *psz_end = 0; psz_end++; }
218
219         if( i_count == 0 )
220         {
221             /* Channel name */
222             psz_name = psz_parse;
223         }
224         else if( i_count == 1 )
225         {
226             /* Frequency */
227             char *psz_end;
228             long i_value;
229
230             i_value = strtol( psz_parse, &psz_end, 10 );
231             if( psz_end == psz_parse ||
232                 i_value == LONG_MAX || i_value == LONG_MIN ) break;
233
234             i_frequency = i_value;
235         }
236         else
237         {
238             int i;
239
240             /* Check option name with our list */
241             for( i = 0; dvb_options[i].psz_name; i++ )
242             {
243                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
244                 {
245                     psz_option = dvb_options[i].psz_option;
246
247                     /* If we recognize one of the strings, then we are sure
248                      * the data is really valid (ie. a channels file). */
249                     b_valid = VLC_TRUE;
250                     break;
251                 }
252             }
253
254             if( !psz_option )
255             {
256                 /* Option not recognized, test if it is a number */
257                 char *psz_end;
258                 long i_value;
259
260                 i_value = strtol( psz_parse, &psz_end, 10 );
261                 if( psz_end != psz_parse &&
262                     i_value != LONG_MAX && i_value != LONG_MIN )
263                 {
264                     i_program = i_value;
265                 }
266             }
267         }
268
269         if( psz_option && pppsz_options && pi_options )
270         {
271             psz_option = strdup( psz_option );
272             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
273                          psz_option );
274         }
275
276         psz_parse = psz_end;
277         i_count++;
278     }
279
280     if( !b_valid && pppsz_options && pi_options )
281     {
282         /* This isn't a valid channels file, cleanup everything */
283         while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
284         if( *pppsz_options ) free( *pppsz_options );
285         *pppsz_options = 0; *pi_options = 0;
286     }
287
288     if( i_program && pppsz_options && pi_options )
289     {
290         char *psz_option;
291
292         asprintf( &psz_option, "program=%i", i_program );
293         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
294                      psz_option );
295     }
296     if( i_frequency && pppsz_options && pi_options )
297     {
298         char *psz_option;
299
300         asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
301         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
302                      psz_option );
303     }
304     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
305
306     return b_valid;
307 }
308
309 static int Control( demux_t *p_demux, int i_query, va_list args )
310 {
311     return VLC_EGENERIC;
312 }