playlist.c 11.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * playlist.c :  Playlist import module
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2004 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
33
#include <vlc_demux.h>
34
#include <vlc_url.h>
35

36
#if defined( _WIN32 ) || defined( __OS2__ )
37
# include <ctype.h>                          /* isalpha */
38
#endif
39
#include <assert.h>
40

41
#include "playlist.h"
42 43 44 45

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
46 47 48 49
#define SHOW_ADULT_TEXT N_( "Show shoutcast adult content" )
#define SHOW_ADULT_LONGTEXT N_( "Show NC17 rated video streams when " \
                "using shoutcast video playlists." )

50 51 52 53
#define SKIP_ADS_TEXT N_( "Skip ads" )
#define SKIP_ADS_LONGTEXT N_( "Use playlist options usually used to prevent " \
    "ads skipping to detect ads and prevent adding them to the playlist." )

54 55 56 57 58 59 60 61 62 63 64
static const char *const psz_recursive_list[] = { "none", "collapse", "expand" };
static const char *const psz_recursive_list_text[] = {
    N_("None"), N_("Collapse"), N_("Expand"), N_("Expand distant files") };

#define RECURSIVE_TEXT N_("Subdirectory behavior")
#define RECURSIVE_LONGTEXT N_( \
        "Select whether subdirectories must be expanded.\n" \
        "none: subdirectories do not appear in the playlist.\n" \
        "collapse: subdirectories appear but are expanded on first play.\n" \
        "expand: all subdirectories are expanded.\n" )

65 66 67 68 69 70 71
#define IGNORE_TEXT N_("Ignored extensions")
#define IGNORE_LONGTEXT N_( \
        "Files with these extensions will not be added to playlist when " \
        "opening a directory.\n" \
        "This is useful if you add directories that contain playlist files " \
        "for instance. Use a comma-separated list of extensions." )

72 73 74 75
#define SHOW_HIDDENFILES_TEXT N_("Show hidden files")
#define SHOW_HIDDENFILES_LONGTEXT N_( \
        "Ignore files starting with '.'" )

76 77 78 79
vlc_module_begin ()
    add_shortcut( "playlist" )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
80

81
    add_obsolete_integer( "parent-item" ) /* removed since 1.1.0 */
82

83
    add_bool( "playlist-skip-ads", true,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
84
              SKIP_ADS_TEXT, SKIP_ADS_LONGTEXT, false )
85

86 87 88 89
    set_shortname( N_("Playlist") )
    set_description( N_("Playlist") )
    add_submodule ()
        set_description( N_("M3U playlist import") )
90
        add_shortcut( "playlist", "m3u", "m3u8", "m3u-open" )
91 92
        set_capability( "demux", 10 )
        set_callbacks( Import_M3U, Close_M3U )
93 94
    add_submodule ()
        set_description( N_("RAM playlist import") )
95
        add_shortcut( "playlist", "ram-open" )
96
        set_capability( "demux", 10 )
97
        set_callbacks( Import_RAM, NULL )
98 99
    add_submodule ()
        set_description( N_("PLS playlist import") )
100
        add_shortcut( "playlist", "pls-open" )
101
        set_capability( "demux", 10 )
102
        set_callbacks( Import_PLS, NULL )
103 104
    add_submodule ()
        set_description( N_("B4S playlist import") )
105
        add_shortcut( "playlist", "b4s-open", "shout-b4s" )
106
        set_capability( "demux", 10 )
107
        set_callbacks( Import_B4S, NULL )
108 109
    add_submodule ()
        set_description( N_("DVB playlist import") )
110
        add_shortcut( "playlist", "dvb-open" )
111
        set_capability( "demux", 10 )
112
        set_callbacks( Import_DVB, NULL )
113 114
    add_submodule ()
        set_description( N_("Podcast parser") )
115
        add_shortcut( "playlist", "podcast" )
116
        set_capability( "demux", 10 )
117
        set_callbacks( Import_podcast, NULL )
118 119
    add_submodule ()
        set_description( N_("XSPF playlist import") )
120
        add_shortcut( "playlist", "xspf-open" )
121 122 123 124
        set_capability( "demux", 10 )
        set_callbacks( Import_xspf, Close_xspf )
    add_submodule ()
        set_description( N_("New winamp 5.2 shoutcast import") )
125
        add_shortcut( "playlist", "shout-winamp" )
126
        set_capability( "demux", 10 )
127
        set_callbacks( Import_Shoutcast, NULL )
128
        add_bool( "shoutcast-show-adult", false,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129
                   SHOW_ADULT_TEXT, SHOW_ADULT_LONGTEXT, false )
130 131
    add_submodule ()
        set_description( N_("ASX playlist import") )
132
        add_shortcut( "playlist", "asx-open" )
133
        set_capability( "demux", 10 )
134
        set_callbacks( Import_ASX, NULL )
135 136
    add_submodule ()
        set_description( N_("Kasenna MediaBase parser") )
137
        add_shortcut( "playlist", "sgimb" )
138 139 140 141
        set_capability( "demux", 10 )
        set_callbacks( Import_SGIMB, Close_SGIMB )
    add_submodule ()
        set_description( N_("QuickTime Media Link importer") )
142
        add_shortcut( "playlist", "qtl" )
143
        set_capability( "demux", 10 )
144
        set_callbacks( Import_QTL, NULL )
145 146
    add_submodule ()
        set_description( N_("Google Video Playlist importer") )
147
        add_shortcut( "playlist", "gvp" )
148
        set_capability( "demux", 10 )
149
        set_callbacks( Import_GVP, NULL )
150
    add_submodule ()
151
        set_description( N_("Dummy IFO demux") )
152
        add_shortcut( "playlist" )
153
        set_capability( "demux", 12 )
154
        set_callbacks( Import_IFO, NULL )
155 156
    add_submodule ()
        set_description( N_("iTunes Music Library importer") )
157
        add_shortcut( "playlist", "itml" )
158 159
        set_capability( "demux", 10 )
        set_callbacks( Import_iTML, Close_iTML )
160 161 162 163 164
    add_submodule ()
        set_description( N_("WPL playlist import") )
        add_shortcut( "playlist", "wpl" )
        set_capability( "demux", 10 )
        set_callbacks( Import_WPL, Close_WPL )
165 166 167 168 169
    add_submodule ()
        set_description( N_("Directory import") )
        add_shortcut( "playlist", "directory" )
        set_capability( "demux", 10 )
        set_callbacks( Import_Dir, Close_Dir )
170 171 172
        add_string( "recursive", "collapse" , RECURSIVE_TEXT,
                    RECURSIVE_LONGTEXT, false )
          change_string_list( psz_recursive_list, psz_recursive_list_text )
173 174
        add_string( "ignore-filetypes", "m3u,db,nfo,ini,jpg,jpeg,ljpg,gif,png,pgm,pgmyuv,pbm,pam,tga,bmp,pnm,xpm,xcf,pcx,tif,tiff,lbm,sfv,txt,sub,idx,srt,cue,ssa",
                    IGNORE_TEXT, IGNORE_LONGTEXT, false )
175 176
        add_bool( "show-hiddenfiles", false,
                   SHOW_HIDDENFILES_TEXT, SHOW_HIDDENFILES_LONGTEXT, false )
177
vlc_module_end ()
178

179 180
int Control(demux_t *demux, int query, va_list args)
{
181 182 183 184 185 186 187 188 189
    (void) demux;
    switch( query )
    {
        case DEMUX_IS_PLAYLIST:
        {
            bool *pb_bool = (bool*)va_arg( args, bool * );
            *pb_bool = true;
            return VLC_SUCCESS;
        }
190 191
        case DEMUX_GET_META:
        {
192
            return vlc_stream_vaControl(demux->s, STREAM_GET_META, args);
193 194 195 196 197 198
        }
        case DEMUX_HAS_UNSUPPORTED_META:
        {
            *(va_arg( args, bool * )) = false;
            return VLC_SUCCESS;
        }
199
    }
200 201 202
    return VLC_EGENERIC;
}

203 204
input_item_t * GetCurrentItem(demux_t *p_demux)
{
205
    input_item_t *p_current_input = input_GetItem( p_demux->p_input );
206
    input_item_Hold(p_current_input);
207 208
    return p_current_input;
}
209 210

/**
211 212 213
 * Computes the base URL.
 *
 * Rebuilds the base URL for the playlist.
214
 */
215
char *FindPrefix(demux_t *p_demux)
216
{
217
    char *url;
JP Dinger's avatar
JP Dinger committed
218

219 220 221 222
    if (unlikely(asprintf(&url, "%s://%s", p_demux->psz_access,
                          p_demux->psz_location) == -1))
        url = NULL;
    return url;
223 224 225
}

/**
226 227 228 229
 * Resolves a playlist location.
 *
 * Resolves a resource location within the playlist relative to the playlist
 * base URL.
230
 */
231
char *ProcessMRL(const char *str, const char *base)
232
{
233 234
    char const* orig = str;

235
    if (str == NULL)
236
        return NULL;
237

238 239 240 241 242 243 244 245
#if (DIR_SEP_CHAR == '\\')
    /* UNC path prefix? */
    if (strncmp(str, "\\\\", 2) == 0
    /* Drive letter prefix? */
     || (isalpha((unsigned char)str[0]) && str[1] == ':'))
        /* Assume this an absolute file path - usually true */
        return vlc_path2uri(str, NULL);
    /* TODO: drive-relative path: if (str[0] == '\\') */
246
#endif
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    /* The base URL is always an URL: it is the URL of the playlist.
     *
     * However it is not always known if the input string is a valid URL, a
     * broken URL or a local file path. As a rule, if it looks like a valid
     * URL, it must be treated as such, since most playlist formats use URLs.
     *
     * There are a few corner cases file paths that look like an URL but whose
     * URL representation does not match, notably when they contain a
     * percentage sign, a colon, a hash or a question mark. Luckily, they are
     * rather exceptional (and can be encoded as URL to make the playlist
     * work properly).
     *
     * If the input is not a valid URL, then we try to fix it up. It works in
     * all cases for URLs with incorrectly encoded segments, such as URLs with
     * white spaces or non-ASCII Unicode code points. It also works in most
     * cases where the input is a Unix-style file path, but not all.
     * It fails miserably if the playlist character encoding is misdetected.
     */
    char *rel = vlc_uri_fixup(str);
    if (rel != NULL)
        str = rel;

    char *abs = vlc_uri_resolve(base, str);
    free(rel);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

    if (abs == NULL)
    {
        /** If the input is not a valid URL, see if there is a scheme:// where
         * the scheme itself consists solely of valid scheme-characters
         * (including the VLC's scheme-extension). If it does, fall back to
         * allowing the URI in order to not break back-compatibility.
         */
        char const* scheme_end = strstr( orig, "://" );
        char const* valid_chars = "abcdefghijklmnopqrstuvwxyz"
                                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                  "0123456789" "+-./";
        if (scheme_end &&
            strspn (orig, valid_chars) == (size_t)(scheme_end - orig))
        {
            abs = strdup (orig);
        }
    }

291
    return abs;
292
}
293 294 295 296

/**
 * Checks stream Content-Type against a known one
 */
297
bool CheckMimeType( stream_t *stream, const char *mime_type )
298
{
299 300
    char* stream_mtype = stream_MimeType( stream );
    bool const match = stream_mtype && !strcasecmp( mime_type, stream_mtype );
301

302 303
    free( stream_mtype );
    return match;
304
}