pls.c 7.56 KB
Newer Older
1 2 3
/*****************************************************************************
 * pls.c : PLS playlist format import
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30
 *****************************************************************************/

/*****************************************************************************
 * 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
    psz_ext = strrchr ( p_demux->psz_path, '.' );

61
    if( !strncasecmp( (char *)p_peek, "[playlist]", 10 ) )
62 63 64 65 66 67 68 69
    {
        ;
    }
    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

    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 )
    {
79
        msg_Err( p_demux, "out of memory" );
80 81
        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;
    int            i_position;
    int            i_item = -1;
    int            i_new_item = 0;
    int            i_key_length;

113
    INIT_PLAYLIST_STUFF;
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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
157
            msg_Warn( p_demux, "couldn't find number of items" );
158 159 160 161 162 163 164 165 166 167 168 169
            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 )
            {
170 171 172 173 174 175
                p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,
                                            0, NULL, -1 );
                vlc_input_item_CopyOptions( p_current->p_input, p_input );
                playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
                                p_item_in_category, (i_parent_id > 0 ) ?
                                VLC_TRUE: VLC_FALSE, PLAYLIST_APPEND );
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            }
            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 ) )
        {
192
            psz_mrl = E_(ProcessMRL)( psz_value, p_demux->p_sys->psz_prefix );
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        }
        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 )
    {
215 216 217 218 219
        p_input = input_ItemNewExt( p_playlist, psz_mrl, psz_name,0, NULL, -1 );
        vlc_input_item_CopyOptions( p_current->p_input, p_input );
        playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
                                p_item_in_category, (i_parent_id > 0 ) ?
                                VLC_TRUE: VLC_FALSE, PLAYLIST_APPEND );
220 221 222 223 224 225 226 227 228 229 230 231 232
        free( psz_mrl );
        psz_mrl = NULL;
    }
    else
    {
        msg_Warn( p_demux, "no file= part found for item %d", i_item );
    }
    if( psz_name )
    {
        free( psz_name );
        psz_name = NULL;
    }

233
    HANDLE_PLAY_AND_RELEASE;
234 235 236 237 238 239 240
    return VLC_SUCCESS;
}

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