pls.c 9.12 KB
Newer Older
1 2 3
/*****************************************************************************
 * pls.c : PLS playlist format import
 *****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
4
 * Copyright (C) 2004 VideoLAN (Centrale Réseaux) and its contributors
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Authors: Clment Stenac <zorglub@videolan.org>
 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
31
#include <vlc/input.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <vlc/intf.h>

#include <errno.h>                                                 /* ENOMEM */
#include "playlist.h"

struct demux_sys_t
{
    char *psz_prefix;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux);
static int Control( demux_t *p_demux, int i_query, va_list args );

/*****************************************************************************
 * Import_PLS: main import function
 *****************************************************************************/
51
int E_(Import_PLS)( vlc_object_t *p_this )
52 53 54 55 56 57
{
    demux_t *p_demux = (demux_t *)p_this;

    uint8_t *p_peek;
    char    *psz_ext;

58
    if( stream_Peek( p_demux->s , &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
59 60 61 62 63 64 65 66 67 68 69
    psz_ext = strrchr ( p_demux->psz_path, '.' );

    if( !strncasecmp( p_peek, "[playlist]", 10 ) )
    {
        ;
    }
    else if( ( psz_ext && !strcasecmp( psz_ext, ".pls") ) ||
             ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "pls") ) )
    {
        ;
    }
70 71
    else return VLC_EGENERIC;

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
72
    msg_Dbg( p_demux, "found valid PLS playlist file");
73 74 75 76 77 78 79 80 81

    p_demux->pf_control = Control;
    p_demux->pf_demux = Demux;
    p_demux->p_sys = malloc( sizeof(demux_sys_t) );
    if( p_demux->p_sys == NULL )
    {
        msg_Err( p_demux, "Out of memory" );
        return VLC_ENOMEM;
    }
82
    p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
83 84 85 86 87 88 89

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
90
void E_(Close_PLS)( vlc_object_t *p_this )
91 92 93 94 95 96 97 98 99 100 101 102
{
    demux_t *p_demux = (demux_t *)p_this;
    if( p_demux->p_sys->psz_prefix )
    {
        free( p_demux->p_sys->psz_prefix );
    }
    free( p_demux->p_sys );
}

static int Demux( demux_t *p_demux )
{
    mtime_t        i_duration = -1;
103
    char          *psz_name = NULL;
104 105 106 107 108 109 110 111 112
    char          *psz_line;
    char          *psz_mrl = NULL;
    char          *psz_key;
    char          *psz_value;
    playlist_t    *p_playlist;
    int            i_position;
    int            i_item = -1;
    int            i_new_item = 0;
    int            i_key_length;
113
    playlist_item_t *p_parent;
114
    vlc_bool_t b_play;
115 116 117 118 119 120 121 122

    p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
                                                 FIND_PARENT );
    if( !p_playlist )
    {
        msg_Err( p_demux, "can't find playlist" );
        return -1;
    }
123

124
    b_play = E_(FindItem)( p_demux, p_playlist, &p_parent );
125
    p_parent->input.i_type = ITEM_TYPE_PLAYLIST;
126

127 128 129 130 131 132
    /* Change the item to a node */
    if( p_parent->i_children == -1)
    {
        playlist_ItemToNode( p_playlist,p_parent );
    }

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
    {
        if( !strncasecmp( psz_line, "[playlist]", sizeof("[playlist]")-1 ) )
        {
            free( psz_line );
            continue;
        }
        psz_key = psz_line;
        psz_value = strchr( psz_line, '=' );
        if( psz_value )
        {
            *psz_value='\0';
            psz_value++;
        }
        else
        {
            msg_Warn( p_demux, "invalid line in pls file" );
            free( psz_line );
            continue;
        }
        if( !strcasecmp( psz_key, "version" ) )
        {
            msg_Dbg( p_demux, "pls file version: %s", psz_value );
            free( psz_line );
            continue;
        }
        /* find the number part of of file1, title1 or length1 etc */
        i_key_length = strlen( psz_key );
        if( i_key_length >= 5 ) /* file1 type case */
        {
            i_new_item = atoi( psz_key + 4 );
            if( i_new_item == 0 && i_key_length >= 6 ) /* title1 type case */
            {
                i_new_item = atoi( psz_key + 5 );
                if( i_new_item == 0 && i_key_length >= 7 ) /* length1 type case */
                {
                    i_new_item = atoi( psz_key + 6 );
                }
            }
        }
        if( i_new_item == 0 )
        {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
175
            msg_Warn( p_demux, "couldn't find number of items" );
176 177 178 179 180 181 182 183 184 185 186 187
            free( psz_line );
            continue;
        }
        if( i_item == -1 )
        {
            i_item = i_new_item;
        }
        /* we found a new item, insert the previous */
        if( i_item != i_new_item )
        {
            if( psz_mrl )
            {
188 189
                playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
                                                            psz_name );
190 191 192 193 194 195

                playlist_NodeAddItem( p_playlist,p_item,
                                      p_parent->pp_parents[0]->i_view,
                                      p_parent,
                                      PLAYLIST_APPEND, PLAYLIST_END );

196
                playlist_CopyParents( p_parent, p_item );
197 198
                if( i_duration != -1 )
                {
199
                    //playlist_SetDuration( p_playlist, i_position, i_duration );
200 201 202 203
                }
                i_position++;
                free( psz_mrl );
                psz_mrl = NULL;
204 205 206

                vlc_input_item_CopyOptions( &p_parent->input,
                                            &p_item->input );
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
            }
            else
            {
                msg_Warn( p_demux, "no file= part found for item %d", i_item );
            }
            if( psz_name )
            {
                free( psz_name );
                psz_name = NULL;
            }
            i_duration = -1;
            i_item = i_new_item;
            i_new_item = 0;
        }
        if( !strncasecmp( psz_key, "file", sizeof("file") -1 ) )
        {
223
            psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        }
        else if( !strncasecmp( psz_key, "title", sizeof("title") -1 ) )
        {
            psz_name = strdup( psz_value );
        }
        else if( !strncasecmp( psz_key, "length", sizeof("length") -1 ) )
        {
            i_duration = atoi( psz_value );
            if( i_duration != -1 )
            {
                i_duration *= 1000000;
            }
        }
        else
        {
            msg_Warn( p_demux, "unknown key found in pls file: %s", psz_key );
        }
        free( psz_line );
    }
    /* Add last object */
    if( psz_mrl )
    {
246 247
        playlist_item_t *p_item = playlist_ItemNew( p_playlist, psz_mrl,
                                                    psz_name );
248 249 250 251 252 253

        playlist_NodeAddItem( p_playlist,p_item,
                              p_parent->pp_parents[0]->i_view,
                              p_parent,
                              PLAYLIST_APPEND, PLAYLIST_END );

254
        playlist_CopyParents( p_parent, p_item );
255 256
        if( i_duration != -1 )
        {
257
            //playlist_SetDuration( p_playlist, i_position, i_duration );
258 259 260
        }
        free( psz_mrl );
        psz_mrl = NULL;
261 262 263

        vlc_input_item_CopyOptions( &p_parent->input,
                                    &p_item->input );
264 265 266 267 268 269 270 271 272 273 274
    }
    else
    {
        msg_Warn( p_demux, "no file= part found for item %d", i_item );
    }
    if( psz_name )
    {
        free( psz_name );
        psz_name = NULL;
    }

275 276
    if( b_play && p_playlist->status.p_item &&
        p_playlist->status.p_item->i_children > 0 )
277 278 279
    {
        playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
                          p_playlist->status.i_view,
280 281
                          p_playlist->status.p_item,
                          p_playlist->status.p_item->pp_children[0] );
282
    }
283 284 285 286 287 288 289 290
    vlc_object_release( p_playlist );
    return VLC_SUCCESS;
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
    return VLC_EGENERIC;
}