]> git.sesse.net Git - vlc/blob - modules/demux/playlist/dvb.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[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 #if 0
104     playlist_t *p_playlist;
105     char       *psz_line;
106     playlist_item_t *p_current;
107     vlc_bool_t b_play;
108
109     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
110                                                  FIND_ANYWHERE );
111     if( !p_playlist )
112     {
113         msg_Err( p_demux, "can't find playlist" );
114         return -1;
115     }
116
117     b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
118
119     playlist_ItemToNode( p_playlist, p_current );
120     p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
121
122     while( (psz_line = stream_ReadLine( p_demux->s )) )
123     {
124         playlist_item_t *p_item;
125         char **ppsz_options = NULL;
126         int  i, i_options = 0;
127         char *psz_name = NULL;
128
129         if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
130         {
131             free( psz_line );
132             continue;
133         }
134
135         EnsureUTF8( psz_name );
136
137         p_item = playlist_ItemNew( p_playlist, "dvb:", psz_name );
138         for( i = 0; i< i_options; i++ )
139         {
140             EnsureUTF8( ppsz_options[i] );
141             playlist_ItemAddOption( p_item, ppsz_options[i] );
142         }
143         playlist_NodeAddItem( p_playlist, p_item,
144                               p_current->pp_parents[0]->i_view,
145                               p_current, PLAYLIST_APPEND, PLAYLIST_END );
146
147         /* We need to declare the parents of the node as the
148          *                  * same of the parent's ones */
149         playlist_CopyParents( p_current, p_item );
150         vlc_input_item_CopyOptions( &p_current->input, &p_item->input );
151
152         while( i_options-- ) free( ppsz_options[i_options] );
153         if( ppsz_options ) free( ppsz_options );
154
155         free( psz_line );
156     }
157
158     /* Go back and play the playlist */
159     if( b_play && p_playlist->status.p_item &&
160         p_playlist->status.p_item->i_children > 0 )
161     {
162         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
163                           p_playlist->status.i_view,
164                           p_playlist->status.p_item,
165                           p_playlist->status.p_item->pp_children[0] );
166     }
167
168     vlc_object_release( p_playlist );
169     return VLC_SUCCESS;
170 #endif
171 }
172
173 static struct
174 {
175     char *psz_name;
176     char *psz_option;
177
178 } dvb_options[] =
179 {
180     { "INVERSION_OFF", "dvb-inversion=0" },
181     { "INVERSION_ON", "dvb-inversion=1" },
182     { "INVERSION_AUTO", "dvb-inversion=2" },
183
184     { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
185     { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
186     { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
187     { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
188
189     { "FEC_NONE", "dvb-fec=0" },
190     { "FEC_1_2", "dvb-fec=1" },
191     { "FEC_2_3", "dvb-fec=2" },
192     { "FEC_3_4", "dvb-fec=3" },
193     { "FEC_4_5", "dvb-fec=4" },
194     { "FEC_5_6", "dvb-fec=5" },
195     { "FEC_6_7", "dvb-fec=6" },
196     { "FEC_7_8", "dvb-fec=7" },
197     { "FEC_8_9", "dvb-fec=8" },
198     { "FEC_AUTO", "dvb-fec=9" },
199
200     { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
201     { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
202     { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
203     { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
204     { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
205
206     { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
207     { "HIERARCHY_1", "dvb-hierarchy=1" },
208     { "HIERARCHY_2", "dvb-hierarchy=2" },
209     { "HIERARCHY_4", "dvb-hierarchy=4" },
210
211     { "QPSK", "dvb-modulation=-1" },
212     { "QAM_AUTO", "dvb-modulation=0" },
213     { "QAM_16", "dvb-modulation=16" },
214     { "QAM_32", "dvb-modulation=32" },
215     { "QAM_64", "dvb-modulation=64" },
216     { "QAM_128", "dvb-modulation=128" },
217     { "QAM_256", "dvb-modulation=256" },
218
219     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
220     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
221     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
222     { 0, 0 }
223
224 };
225
226 static int ParseLine( char *psz_line, char **ppsz_name,
227                       char ***pppsz_options, int *pi_options )
228 {
229     char *psz_name = 0, *psz_parse = psz_line;
230     int i_count = 0, i_program = 0, i_frequency = 0;
231     vlc_bool_t b_valid = VLC_FALSE;
232
233     if( pppsz_options ) *pppsz_options = 0;
234     if( pi_options ) *pi_options = 0;
235     if( ppsz_name ) *ppsz_name = 0;
236
237     /* Skip leading tabs and spaces */
238     while( *psz_parse == ' ' || *psz_parse == '\t' ||
239            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
240
241     /* Ignore comments */
242     if( *psz_parse == '#' ) return VLC_FALSE;
243
244     while( psz_parse )
245     {
246         char *psz_option = 0;
247         char *psz_end = strchr( psz_parse, ':' );
248         if( psz_end ) { *psz_end = 0; psz_end++; }
249
250         if( i_count == 0 )
251         {
252             /* Channel name */
253             psz_name = psz_parse;
254         }
255         else if( i_count == 1 )
256         {
257             /* Frequency */
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 ) break;
264
265             i_frequency = i_value;
266         }
267         else
268         {
269             int i;
270
271             /* Check option name with our list */
272             for( i = 0; dvb_options[i].psz_name; i++ )
273             {
274                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
275                 {
276                     psz_option = dvb_options[i].psz_option;
277
278                     /* If we recognize one of the strings, then we are sure
279                      * the data is really valid (ie. a channels file). */
280                     b_valid = VLC_TRUE;
281                     break;
282                 }
283             }
284
285             if( !psz_option )
286             {
287                 /* Option not recognized, test if it is a number */
288                 char *psz_end;
289                 long i_value;
290
291                 i_value = strtol( psz_parse, &psz_end, 10 );
292                 if( psz_end != psz_parse &&
293                     i_value != LONG_MAX && i_value != LONG_MIN )
294                 {
295                     i_program = i_value;
296                 }
297             }
298         }
299
300         if( psz_option && pppsz_options && pi_options )
301         {
302             psz_option = strdup( psz_option );
303             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
304                          psz_option );
305         }
306
307         psz_parse = psz_end;
308         i_count++;
309     }
310
311     if( !b_valid && pppsz_options && pi_options )
312     {
313         /* This isn't a valid channels file, cleanup everything */
314         while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
315         if( *pppsz_options ) free( *pppsz_options );
316         *pppsz_options = 0; *pi_options = 0;
317     }
318
319     if( i_program && pppsz_options && pi_options )
320     {
321         char *psz_option;
322
323         asprintf( &psz_option, "program=%i", i_program );
324         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
325                      psz_option );
326     }
327     if( i_frequency && pppsz_options && pi_options )
328     {
329         char *psz_option;
330
331         asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
332         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
333                      psz_option );
334     }
335     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
336
337     return b_valid;
338 }
339
340 static int Control( demux_t *p_demux, int i_query, va_list args )
341 {
342     return VLC_EGENERIC;
343 }