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