Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

dvb.c

00001 /*****************************************************************************
00002  * dvb.c : DVB channel list import (szap/tzap/czap compatible channel lists)
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: dvb.c 12306 2005-08-20 18:10:24Z gbazin $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031 #include <vlc/intf.h>
00032 
00033 #include "playlist.h"
00034 
00035 #ifndef LONG_MAX
00036 #   define LONG_MAX 2147483647L
00037 #   define LONG_MIN (-LONG_MAX-1)
00038 #endif
00039 
00040 /*****************************************************************************
00041  * Local prototypes
00042  *****************************************************************************/
00043 static int Demux( demux_t *p_demux);
00044 static int Control( demux_t *p_demux, int i_query, va_list args );
00045 
00046 static int ParseLine( char *, char **, char ***, int *);
00047 
00048 /*****************************************************************************
00049  * Import_DVB: main import function
00050  *****************************************************************************/
00051 int E_(Import_DVB)( vlc_object_t *p_this )
00052 {
00053     demux_t *p_demux = (demux_t *)p_this;
00054     uint8_t *p_peek;
00055     int     i_peek;
00056     char    *psz_ext;
00057     vlc_bool_t b_valid = VLC_FALSE;
00058 
00059     psz_ext = strrchr ( p_demux->psz_path, '.' );
00060 
00061     if( !( psz_ext && !strncasecmp( psz_ext, ".conf", 5 ) ) &&
00062         !p_demux->b_force ) return VLC_EGENERIC;
00063 
00064     /* Check if this really is a channels file */
00065     if( (i_peek = stream_Peek( p_demux->s, &p_peek, 1024 )) > 0 )
00066     {
00067         char psz_line[1024+1];
00068         int i;
00069 
00070         for( i = 0; i < i_peek; i++ )
00071         {
00072             if( p_peek[i] == '\n' ) break;
00073             psz_line[i] = p_peek[i];
00074         }
00075         psz_line[i] = 0;
00076 
00077         if( ParseLine( psz_line, 0, 0, 0 ) ) b_valid = VLC_TRUE;
00078     }
00079 
00080     if( !b_valid ) return VLC_EGENERIC;
00081 
00082     msg_Dbg( p_demux, "found valid DVB conf playlist file");
00083 
00084     p_demux->pf_control = Control;
00085     p_demux->pf_demux = Demux;
00086 
00087     return VLC_SUCCESS;
00088 }
00089 
00090 /*****************************************************************************
00091  * Deactivate: frees unused data
00092  *****************************************************************************/
00093 void E_(Close_DVB)( vlc_object_t *p_this )
00094 {
00095 }
00096 
00097 /*****************************************************************************
00098  * Demux: The important stuff
00099  *****************************************************************************/
00100 static int Demux( demux_t *p_demux )
00101 {
00102     playlist_t *p_playlist;
00103     char       *psz_line;
00104     playlist_item_t *p_current;
00105     vlc_bool_t b_play;
00106 
00107     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
00108                                                  FIND_PARENT );
00109     if( !p_playlist )
00110     {
00111         msg_Err( p_demux, "can't find playlist" );
00112         return -1;
00113     }
00114 
00115     b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
00116 
00117     playlist_ItemToNode( p_playlist, p_current );
00118     p_current->input.i_type = ITEM_TYPE_PLAYLIST;
00119 
00120     while( (psz_line = stream_ReadLine( p_demux->s )) )
00121     {
00122         playlist_item_t *p_item;
00123         char **ppsz_options = NULL;
00124         int  i, i_options = 0;
00125         char *psz_name = NULL;
00126 
00127         if( !ParseLine( psz_line, &psz_name, &ppsz_options, &i_options ) )
00128         {
00129             free( psz_line );
00130             continue;
00131         }
00132 
00133         EnsureUTF8( psz_name );
00134 
00135         p_item = playlist_ItemNew( p_playlist, "dvb:", psz_name );
00136         for( i = 0; i< i_options; i++ )
00137         {
00138             EnsureUTF8( ppsz_options[i] );
00139             playlist_ItemAddOption( p_item, ppsz_options[i] );
00140         }
00141         playlist_NodeAddItem( p_playlist, p_item,
00142                               p_current->pp_parents[0]->i_view,
00143                               p_current, PLAYLIST_APPEND, PLAYLIST_END );
00144 
00145         /* We need to declare the parents of the node as the
00146          *                  * same of the parent's ones */
00147         playlist_CopyParents( p_current, p_item );
00148         vlc_input_item_CopyOptions( &p_current->input, &p_item->input );
00149 
00150         while( i_options-- ) free( ppsz_options[i_options] );
00151         if( ppsz_options ) free( ppsz_options );
00152 
00153         free( psz_line );
00154     }
00155 
00156     /* Go back and play the playlist */
00157     if( b_play && p_playlist->status.p_item &&
00158         p_playlist->status.p_item->i_children > 0 )
00159     {
00160         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
00161                           p_playlist->status.i_view,
00162                           p_playlist->status.p_item,
00163                           p_playlist->status.p_item->pp_children[0] );
00164     }
00165 
00166     vlc_object_release( p_playlist );
00167     return VLC_SUCCESS;
00168 }
00169 
00170 static struct
00171 {
00172     char *psz_name;
00173     char *psz_option;
00174 
00175 } dvb_options[] =
00176 {
00177     { "INVERSION_OFF", "dvb-inversion=0" },
00178     { "INVERSION_ON", "dvb-inversion=1" },
00179     { "INVERSION_AUTO", "dvb-inversion=2" },
00180 
00181     { "BANDWIDTH_AUTO", "dvb-bandwidth=0" },
00182     { "BANDWIDTH_6_MHZ", "dvb-bandwidth=6" },
00183     { "BANDWIDTH_7_MHZ", "dvb-bandwidth=7" },
00184     { "BANDWIDTH_8_MHZ", "dvb-bandwidth=8" },
00185 
00186     { "FEC_NONE", "dvb-fec=0" },
00187     { "FEC_1_2", "dvb-fec=1" },
00188     { "FEC_2_3", "dvb-fec=2" },
00189     { "FEC_3_4", "dvb-fec=3" },
00190     { "FEC_4_5", "dvb-fec=4" },
00191     { "FEC_5_6", "dvb-fec=5" },
00192     { "FEC_6_7", "dvb-fec=6" },
00193     { "FEC_7_8", "dvb-fec=7" },
00194     { "FEC_8_9", "dvb-fec=8" },
00195     { "FEC_AUTO", "dvb-fec=9" },
00196 
00197     { "GUARD_INTERVAL_AUTO", "dvb-guard=0" },
00198     { "GUARD_INTERVAL_1_4", "dvb-guard=4" },
00199     { "GUARD_INTERVAL_1_8", "dvb-guard=8" },
00200     { "GUARD_INTERVAL_1_16", "dvb-guard=16" },
00201     { "GUARD_INTERVAL_1_32", "dvb-guard=32" },
00202 
00203     { "HIERARCHY_NONE", "dvb-hierarchy=-1" },
00204     { "HIERARCHY_1", "dvb-hierarchy=1" },
00205     { "HIERARCHY_2", "dvb-hierarchy=2" },
00206     { "HIERARCHY_4", "dvb-hierarchy=4" },
00207 
00208     { "QPSK", "dvb-modulation=-1" },
00209     { "QAM_AUTO", "dvb-modulation=0" },
00210     { "QAM_16", "dvb-modulation=16" },
00211     { "QAM_32", "dvb-modulation=32" },
00212     { "QAM_64", "dvb-modulation=64" },
00213     { "QAM_128", "dvb-modulation=128" },
00214     { "QAM_256", "dvb-modulation=256" },
00215 
00216     { "TRANSMISSION_MODE_AUTO", "dvb-transmission=0" },
00217     { "TRANSMISSION_MODE_2K", "dvb-transmission=2" },
00218     { "TRANSMISSION_MODE_8K", "dvb-transmission=8" },
00219     { 0, 0 }
00220 
00221 };
00222 
00223 static int ParseLine( char *psz_line, char **ppsz_name,
00224                       char ***pppsz_options, int *pi_options )
00225 {
00226     char *psz_name = 0, *psz_parse = psz_line;
00227     int i_count = 0, i_program = 0, i_frequency = 0;
00228     vlc_bool_t b_valid = VLC_FALSE;
00229 
00230     if( pppsz_options ) *pppsz_options = 0;
00231     if( pi_options ) *pi_options = 0;
00232     if( ppsz_name ) *ppsz_name = 0;
00233 
00234     /* Skip leading tabs and spaces */
00235     while( *psz_parse == ' ' || *psz_parse == '\t' ||
00236            *psz_parse == '\n' || *psz_parse == '\r' ) psz_parse++;
00237 
00238     /* Ignore comments */
00239     if( *psz_parse == '#' ) return VLC_FALSE;
00240 
00241     while( psz_parse )
00242     {
00243         char *psz_option = 0;
00244         char *psz_end = strchr( psz_parse, ':' );
00245         if( psz_end ) { *psz_end = 0; psz_end++; }
00246 
00247         if( i_count == 0 )
00248         {
00249             /* Channel name */
00250             psz_name = psz_parse;
00251         }
00252         else if( i_count == 1 )
00253         {
00254             /* Frequency */
00255             char *psz_end;
00256             long i_value;
00257 
00258             i_value = strtol( psz_parse, &psz_end, 10 );
00259             if( psz_end == psz_parse ||
00260                 i_value == LONG_MAX || i_value == LONG_MIN ) break;
00261 
00262             i_frequency = i_value;
00263         }
00264         else
00265         {
00266             int i;
00267 
00268             /* Check option name with our list */
00269             for( i = 0; dvb_options[i].psz_name; i++ )
00270             {
00271                 if( !strcmp( psz_parse, dvb_options[i].psz_name ) )
00272                 {
00273                     psz_option = dvb_options[i].psz_option;
00274 
00275                     /* If we recognize one of the strings, then we are sure
00276                      * the data is really valid (ie. a channels file). */
00277                     b_valid = VLC_TRUE;
00278                     break;
00279                 }
00280             }
00281 
00282             if( !psz_option )
00283             {
00284                 /* Option not recognized, test if it is a number */
00285                 char *psz_end;
00286                 long i_value;
00287 
00288                 i_value = strtol( psz_parse, &psz_end, 10 );
00289                 if( psz_end != psz_parse &&
00290                     i_value != LONG_MAX && i_value != LONG_MIN )
00291                 {
00292                     i_program = i_value;
00293                 }
00294             }
00295         }
00296 
00297         if( psz_option && pppsz_options && pi_options )
00298         {
00299             psz_option = strdup( psz_option );
00300             INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
00301                          psz_option );
00302         }
00303 
00304         psz_parse = psz_end;
00305         i_count++;
00306     }
00307 
00308     if( !b_valid && pppsz_options && pi_options )
00309     {
00310         /* This isn't a valid channels file, cleanup everything */
00311         while( (*pi_options)-- ) free( (*pppsz_options)[*pi_options] );
00312         if( *pppsz_options ) free( *pppsz_options );
00313         *pppsz_options = 0; *pi_options = 0;
00314     }
00315 
00316     if( i_program && pppsz_options && pi_options )
00317     {
00318         char *psz_option;
00319 
00320         asprintf( &psz_option, "program=%i", i_program );
00321         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
00322                      psz_option );
00323     }
00324     if( i_frequency && pppsz_options && pi_options )
00325     {
00326         char *psz_option;
00327 
00328         asprintf( &psz_option, "dvb-frequency=%i", i_frequency );
00329         INSERT_ELEM( *pppsz_options, (*pi_options), (*pi_options),
00330                      psz_option );
00331     }
00332     if( ppsz_name && psz_name ) *ppsz_name = strdup( psz_name );
00333 
00334     return b_valid;
00335 }
00336 
00337 static int Control( demux_t *p_demux, int i_query, va_list args )
00338 {
00339     return VLC_EGENERIC;
00340 }

Generated on Tue Dec 20 10:14:35 2005 for vlc-0.8.4a by  doxygen 1.4.2