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

pls.c

00001 /*****************************************************************************
00002  * pls.c : PLS playlist format import
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: pls.c 12836 2005-10-15 13:23:08Z sigmunau $
00006  *
00007  * Authors: Clément Stenac <[email protected]>
00008  * Authors: Sigmund Augdal Helberg <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>                                      /* malloc(), free() */
00029 
00030 #include <vlc/vlc.h>
00031 #include <vlc/input.h>
00032 #include <vlc/intf.h>
00033 
00034 #include <errno.h>                                                 /* ENOMEM */
00035 #include "playlist.h"
00036 
00037 struct demux_sys_t
00038 {
00039     char *psz_prefix;
00040 };
00041 
00042 /*****************************************************************************
00043  * Local prototypes
00044  *****************************************************************************/
00045 static int Demux( demux_t *p_demux);
00046 static int Control( demux_t *p_demux, int i_query, va_list args );
00047 
00048 /*****************************************************************************
00049  * Import_PLS: main import function
00050  *****************************************************************************/
00051 int E_(Import_PLS)( vlc_object_t *p_this )
00052 {
00053     demux_t *p_demux = (demux_t *)p_this;
00054 
00055     uint8_t *p_peek;
00056     char    *psz_ext;
00057 
00058     if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
00059     psz_ext = strrchr ( p_demux->psz_path, '.' );
00060 
00061     if( !strncasecmp( (char *)p_peek, "[playlist]", 10 ) )
00062     {
00063         ;
00064     }
00065     else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
00066              ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
00067     {
00068         ;
00069     }
00070     else return VLC_EGENERIC;
00071 
00072     msg_Dbg( p_demux, "found valid PLS playlist file");
00073 
00074     p_demux->pf_control = Control;
00075     p_demux->pf_demux = Demux;
00076     p_demux->p_sys = malloc( sizeof(demux_sys_t) );
00077     if( p_demux->p_sys == NULL )
00078     {
00079         msg_Err( p_demux, "Out of memory" );
00080         return VLC_ENOMEM;
00081     }
00082     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
00083 
00084     return VLC_SUCCESS;
00085 }
00086 
00087 /*****************************************************************************
00088  * Deactivate: frees unused data
00089  *****************************************************************************/
00090 void E_(Close_PLS)( vlc_object_t *p_this )
00091 {
00092     demux_t *p_demux = (demux_t *)p_this;
00093     if( p_demux->p_sys->psz_prefix )
00094     {
00095         free( p_demux->p_sys->psz_prefix );
00096     }
00097     free( p_demux->p_sys );
00098 }
00099 
00100 static int Demux( demux_t *p_demux )
00101 {
00102     mtime_t        i_duration = -1;
00103     char          *psz_name = NULL;
00104     char          *psz_line;
00105     char          *psz_mrl = NULL;
00106     char          *psz_key;
00107     char          *psz_value;
00108     playlist_t    *p_playlist;
00109     int            i_position;
00110     int            i_item = -1;
00111     int            i_new_item = 0;
00112     int            i_key_length;
00113     playlist_item_t *p_parent;
00114     vlc_bool_t b_play;
00115 
00116     p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
00117                                                  FIND_PARENT );
00118     if( !p_playlist )
00119     {
00120         msg_Err( p_demux, "can't find playlist" );
00121         return -1;
00122     }
00123 
00124     b_play = E_(FindItem)( p_demux, p_playlist, &p_parent );
00125     p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
00126 
00127     /* Change the item to a node */
00128     if( p_parent->i_children == -1)
00129     {
00130         playlist_ItemToNode( p_playlist,p_parent );
00131     }
00132 
00133     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
00134     {
00135         if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) )
00136         {
00137             free( psz_line );
00138             continue;
00139         }
00140         psz_key = psz_line;
00141         psz_value = strchr( psz_line, '=' );
00142         if( psz_value )
00143         {
00144             *psz_value='\0';
00145             psz_value++;
00146         }
00147         else
00148         {
00149             msg_Warn( p_demux, "invalid line in pls file" );
00150             free( psz_line );
00151             continue;
00152         }
00153         if( !strcasecmp( psz_key, "version" ) )
00154         {
00155             msg_Dbg( p_demux, "pls file version: %s", psz_value );
00156             free( psz_line );
00157             continue;
00158         }
00159         /* find the number part of of file1, title1 or length1 etc */
00160         i_key_length = strlen( psz_key );
00161         if( i_key_length >= 5 ) /* file1 type case */
00162         {
00163             i_new_item = atoi( psz_key + 4 );
00164             if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
00165             {
00166                 i_new_item = atoi( psz_key + 5 );
00167                 if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
00168                 {
00169                     i_new_item = atoi( psz_key + 6 );
00170                 }
00171             }
00172         }
00173         if( i_new_item == 0 )
00174         {
00175             msg_Warn( p_demux, "couldn't find number of items" );
00176             free( psz_line );
00177             continue;
00178         }
00179         if( i_item == -1 )
00180         {
00181             i_item = i_new_item;
00182         }
00183         /* we found a new item, insert the previous */
00184         if( i_item != i_new_item )
00185         {
00186             if( psz_mrl )
00187             {
00188                 playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
00189                                                             psz_name );
00190 
00191                 playlist_NodeAddItem( p_playlist,p_item,
00192                                       p_parent->pp_parents[0]->i_view,
00193                                       p_parent,
00194                                       PLAYLIST_APPEND, PLAYLIST_END );
00195 
00196                 playlist_CopyParents( p_parent, p_item );
00197                 if( i_duration != -1 )
00198                 {
00199                     //playlist_SetDuration( p_playlist, i_position, i_duration );
00200                 }
00201                 i_position++;
00202                 free( psz_mrl );
00203                 psz_mrl = NULL;
00204 
00205                 vlc_input_item_CopyOptions( &p_parent->input,
00206                                             &p_item->input );
00207             }
00208             else
00209             {
00210                 msg_Warn( p_demux, "no file= part found for item %d", i_item );
00211             }
00212             if( psz_name )
00213             {
00214                 free( psz_name );
00215                 psz_name = NULL;
00216             }
00217             i_duration = -1;
00218             i_item = i_new_item;
00219             i_new_item = 0;
00220         }
00221         if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) )
00222         {
00223             psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
00224         }
00225         else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
00226         {
00227             psz_name = strdup( psz_value );
00228         }
00229         else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
00230         {
00231             i_duration = atoi( psz_value );
00232             if( i_duration != -1 )
00233             {
00234                 i_duration *= 1000000;
00235             }
00236         }
00237         else
00238         {
00239             msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
00240         }
00241         free( psz_line );
00242     }
00243     /* Add last object */
00244     if( psz_mrl )
00245     {
00246         playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
00247                                                     psz_name );
00248 
00249         playlist_NodeAddItem( p_playlist,p_item,
00250                               p_parent->pp_parents[0]->i_view,
00251                               p_parent,
00252                               PLAYLIST_APPEND, PLAYLIST_END );
00253 
00254         playlist_CopyParents( p_parent, p_item );
00255         if( i_duration != -1 )
00256         {
00257             //playlist_SetDuration( p_playlist, i_position, i_duration );
00258         }
00259         free( psz_mrl );
00260         psz_mrl = NULL;
00261 
00262         vlc_input_item_CopyOptions( &p_parent->input,
00263                                     &p_item->input );
00264     }
00265     else
00266     {
00267         msg_Warn( p_demux, "no file= part found for item %d", i_item );
00268     }
00269     if( psz_name )
00270     {
00271         free( psz_name );
00272         psz_name = NULL;
00273     }
00274 
00275     if( b_play && p_playlist->status.p_item &&
00276         p_playlist->status.p_item->i_children > 0 )
00277     {
00278         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
00279                           p_playlist->status.i_view,
00280                           p_playlist->status.p_item,
00281                           p_playlist->status.p_item->pp_children[0] );
00282     }
00283     vlc_object_release( p_playlist );
00284     return VLC_SUCCESS;
00285 }
00286 
00287 static int Control( demux_t *p_demux, int i_query, va_list args )
00288 {
00289     return VLC_EGENERIC;
00290 }

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