meta.c 16.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*****************************************************************************
 * meta.c : Metadata handling
 *****************************************************************************
 * Copyright (C) 1998-2004 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
 *          Clément Stenac <zorglub@videolan.org
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
30 31
#include <vlc_input.h>
#include <vlc_stream.h>
32
#include <vlc_meta.h>
Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_playlist.h>
#include <vlc_charset.h>
35
#include <vlc_strings.h>
36
#include "../playlist/playlist_internal.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
37
#include <errno.h>
38
#include <limits.h>                                             /* PATH_MAX */
39
#include <assert.h>
40 41 42 43 44

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
45 46
#include "../libvlc.h"

47 48 49
const char *
input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
{
Rafaël Carré's avatar
Rafaël Carré committed
50
    switch( meta_type )
51
    {
Rafaël Carré's avatar
Rafaël Carré committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    case vlc_meta_Title:        return _("Title");
    case vlc_meta_Artist:       return _("Artist");
    case vlc_meta_Genre:        return _("Genre");
    case vlc_meta_Copyright:    return _("Copyright");
    case vlc_meta_Album:        return _("Album");
    case vlc_meta_TrackNumber:  return _("Track number");
    case vlc_meta_Description:  return _("Description");
    case vlc_meta_Rating:       return _("Rating");
    case vlc_meta_Date:         return _("Date");
    case vlc_meta_Setting:      return _("Setting");
    case vlc_meta_URL:          return _("URL");
    case vlc_meta_Language:     return _("Language");
    case vlc_meta_NowPlaying:   return _("Now Playing");
    case vlc_meta_Publisher:    return _("Publisher");
    case vlc_meta_EncodedBy:    return _("Encoded by");
    case vlc_meta_ArtworkURL:   return _("Artwork URL");
    case vlc_meta_TrackID:      return _("Track ID");

    default: abort();
71
    }
Rafaël Carré's avatar
Rafaël Carré committed
72
};
73

Rafaël Carré's avatar
Rafaël Carré committed
74 75
#define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
76

77
/* Return codes:
78
 *   0 : Art is in cache or is a local file
79 80 81 82
 *   1 : Art found, need to download
 *  -X : Error/not found
 */
int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
83
{
84 85
    int i_ret = VLC_EGENERIC;
    module_t *p_module;
86
    char *psz_title, *psz_artist, *psz_album;
87

88
    psz_artist = input_item_GetArtist( p_item );
89
    psz_album = input_item_GetAlbum( p_item );
90 91 92
    psz_title = input_item_GetTitle( p_item );
    if(!psz_title)
        psz_title = input_item_GetName( p_item );
93

94
    if( !psz_title && !psz_artist && !psz_album )
95
        return VLC_EGENERIC;
96

97
    free( psz_title );
98

99
    /* If we already checked this album in this session, skip */
100
    if( psz_artist && psz_album )
101
    {
102
        FOREACH_ARRAY( playlist_album_t album, pl_priv(p_playlist)->fetcher.albums )
103 104
            if( !strcmp( album.psz_artist, psz_artist ) &&
                !strcmp( album.psz_album, psz_album ) )
105
            {
106
                msg_Dbg( p_playlist, " %s - %s has already been searched",
107
                         psz_artist, psz_album );
108
        /* TODO-fenrir if we cache art filename too, we can go faster */
109 110
                free( psz_artist );
                free( psz_album );
111 112
                if( album.b_found )
                {
113 114 115 116
                    if( !strncmp( album.psz_arturl, "file://", 7 ) )
                        input_item_SetArtURL( p_item, album.psz_arturl );
                    else /* Actually get URL from cache */
                        input_FindArtInCache( p_playlist, p_item );
117 118 119 120 121 122
                    return 0;
                }
                else
                {
                    return VLC_EGENERIC;
                }
123
            }
124 125
        FOREACH_END();
    }
126 127
    free( psz_artist );
    free( psz_album );
128 129

    input_FindArtInCache( p_playlist, p_item );
130 131

    char *psz_arturl = input_item_GetArtURL( p_item );
132
    if( psz_arturl )
133
    {
134 135 136 137 138 139 140
        /* We already have an URL */
        if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
        {
            free( psz_arturl );
            return 0; /* Art is in cache, no need to go further */
        }

141
        free( psz_arturl );
142 143 144
        
        /* Art need to be put in cache */
        return 1;
145
    }
146 147 148

    PL_LOCK;
    p_playlist->p_private = p_item;
149 150 151
    psz_album = input_item_GetAlbum( p_item );
    psz_artist = input_item_GetArtist( p_item );
    psz_title = input_item_GetTitle( p_item );
152 153 154
    if( !psz_title )
        psz_title = input_item_GetName( p_item );

155
    if( psz_album && psz_artist )
156 157
    {
        msg_Dbg( p_playlist, "searching art for %s - %s",
158
             psz_artist, psz_album );
159 160 161 162
    }
    else
    {
        msg_Dbg( p_playlist, "searching art for %s",
163
             psz_title );
164
    }
165
    free( psz_title );
166

167
    p_module = module_need( p_playlist, "art finder", 0, false );
168 169 170 171 172 173 174

    if( p_module )
        i_ret = 1;
    else
        msg_Dbg( p_playlist, "unable to find art" );

    /* Record this album */
175
    if( psz_artist && psz_album )
176 177
    {
        playlist_album_t a;
178 179
        a.psz_artist = psz_artist;
        a.psz_album = psz_album;
180
        a.psz_arturl = input_item_GetArtURL( p_item );
181
        a.b_found = (i_ret == VLC_EGENERIC ? false : true );
182
        ARRAY_APPEND( pl_priv(p_playlist)->fetcher.albums, a );
183
    }
184 185 186 187 188
    else
    {
        free( psz_artist );
        free( psz_album );
    }
189 190

    if( p_module )
191
        module_unneed( p_playlist, p_module );
192 193
    p_playlist->p_private = NULL;
    PL_UNLOCK;
Clément Stenac's avatar
Clément Stenac committed
194

195
    return i_ret;
196 197
}

198
static void ArtCacheCreateDir( const char *psz_dir )
199
{
200
    char newdir[strlen( psz_dir ) + 1];
201 202 203 204
    strcpy( newdir, psz_dir );
    char * psz_newdir = newdir;
    char * psz = psz_newdir;

205
    while( *psz )
206
    {
207
        while( *psz && *psz != DIR_SEP_CHAR) psz++;
208
        if( !*psz ) break;
209
        *psz = 0;
210 211
        if( !EMPTY_STR( psz_newdir ) )
            utf8_mkdir( psz_newdir, 0700 );
212
        *psz = DIR_SEP_CHAR;
213
        psz++;
214
    }
215
    utf8_mkdir( psz_dir, 0700 );
216 217
}

218
static char * ArtCacheGetSanitizedFileName( const char *psz )
219 220 221 222
{
    char *dup = strdup(psz);
    int i;

223 224
    filename_sanitize( dup );

225 226 227 228
    /* Doesn't create a filename with invalid characters
     * TODO: several filesystems forbid several characters: list them all
     */
    for( i = 0; dup[i] != '\0'; i++ )
229
    {
230
        if( dup[i] == DIR_SEP_CHAR )
231
            dup[i] = ' ';
232
    }
233
    return dup;
234
}
235

236 237
#define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
238
                                  char *psz_dir,
239 240
                                  const char *psz_title,
                                  const char *psz_artist, const char *psz_album )
241
{
242 243
    (void)p_obj;
    char *psz_cachedir = config_GetCacheDir();
244

245
    if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
246
    {
247 248
        char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
        char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
249
        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
250
                  "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
251
                  psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
252 253
        free( psz_album_sanitized );
        free( psz_artist_sanitized );
254 255 256
    }
    else
    {
257
        char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
258
        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
259
                  "art" DIR_SEP "title" DIR_SEP "%s",
260
                  psz_cachedir, psz_title_sanitized );
261
        free( psz_title_sanitized );
262
    }
263
    free( psz_cachedir );
264
}
265 266 267



268 269
#define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
270
                                   char * psz_filename,
271 272
                                   const char *psz_title,
                                   const char *psz_artist, const char *psz_album,
273
                                   const char *psz_extension )
274
{
275
    char psz_dir[PATH_MAX+1];
276
    char * psz_ext;
277
    ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
278

279
    if( psz_extension )
280
    {
281 282
        psz_ext = strndup( psz_extension, 6 );
        filename_sanitize( psz_ext );
283
    }
284 285
    else psz_ext = strdup( "" );

286
    snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
287 288 289
              psz_dir, psz_ext );

    free( psz_ext );
290 291 292
}

static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
293
{
294 295 296
    char *psz_artist;
    char *psz_album;
    char *psz_title;
297 298
    char psz_dirpath[PATH_MAX+1];
    char psz_filepath[PATH_MAX+1];
299
    char * psz_filename;
300
    DIR * p_dir;
301

302 303 304
    psz_artist = input_item_GetArtist( p_item );
    psz_album = input_item_GetAlbum( p_item );
    psz_title = input_item_GetTitle( p_item );
305
    if( !psz_title ) psz_title = input_item_GetName( p_item );
306

307 308 309 310 311 312 313
    if( !psz_title && ( !psz_album || !psz_artist ) )
    {
        free( psz_artist );
        free( psz_album );
        free( psz_title );
        return VLC_EGENERIC;
    }
314

315
    ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
316 317 318 319
                           psz_artist, psz_album );

    free( psz_artist );
    free( psz_album );
320
    free( psz_title );
321

322
    /* Check if file exists */
323
    p_dir = utf8_opendir( psz_dirpath );
324 325
    if( !p_dir )
        return VLC_EGENERIC;
326

327
    while( (psz_filename = utf8_readdir( p_dir )) )
328
    {
329
        if( !strncmp( psz_filename, "art", 3 ) )
330
        {
331
            snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
332 333 334
                      psz_dirpath, psz_filename );
            input_item_SetArtURL( p_item, psz_filepath );
            free( psz_filename );
335 336
            closedir( p_dir );
            return VLC_SUCCESS;
337
        }
338
        free( psz_filename );
339
    }
340

341
    /* Not found */
342
    closedir( p_dir );
343
    return VLC_EGENERIC;
344 345 346 347 348 349
}

/**
 * Download the art using the URL or an art downloaded
 * This function should be called only if data is not already in cache
 */
Clément Stenac's avatar
Clément Stenac committed
350
int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
351 352 353
{
    int i_status = VLC_EGENERIC;
    stream_t *p_stream;
354
    char psz_filename[PATH_MAX+1];
355 356
    char *psz_artist = NULL;
    char *psz_album = NULL;
357
    char *psz_title = NULL;
358
    char *psz_arturl;
359
    char *psz_type;
360

361 362 363
    psz_artist = input_item_GetArtist( p_item );
    psz_album = input_item_GetAlbum( p_item );
    psz_title = input_item_GetTitle( p_item );
364 365
    if( !psz_title )
        psz_title = input_item_GetName( p_item );
366

367
    if( !psz_title && (!psz_artist || !psz_album) )
368
    {
369 370 371
        free( psz_title );
        free( psz_album );
        free( psz_artist );
372
        return VLC_EGENERIC;
373 374
    }

375 376
    psz_arturl = input_item_GetArtURL( p_item );
    assert( !EMPTY_STR( psz_arturl ) );
377

Rafaël Carré's avatar
Rafaël Carré committed
378 379 380 381 382 383 384
    if( !strncmp( psz_arturl , "file://", 7 ) )
    {
        msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
        free( psz_arturl );
        return VLC_SUCCESS;
    }
    else if( !strncmp( psz_arturl , "APIC", 4 ) )
385 386 387 388 389 390 391
    {
        msg_Warn( p_playlist, "APIC fetch not supported yet" );
        free( psz_arturl );
        return VLC_EGENERIC;
    }

    psz_type = strrchr( psz_arturl, '.' );
Rafaël Carré's avatar
Rafaël Carré committed
392
    if( psz_type && strlen( psz_type ) > 5 )
393
        psz_type = NULL; /* remove extension if it's > to 4 characters */
394

395
    /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
396

397
    ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
398 399
                        psz_album );
    ArtCacheCreateDir( psz_filename );
400
    ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
401
                         psz_album, psz_type );
402 403 404

    free( psz_artist );
    free( psz_album );
405
    free( psz_title );
406

407
    p_stream = stream_UrlNew( p_playlist, psz_arturl );
408 409
    if( p_stream )
    {
410
        uint8_t p_buffer[65536];
411 412
        long int l_read;
        FILE *p_file = utf8_fopen( psz_filename+7, "w" );
413
        if( p_file == NULL ) {
414 415
            msg_Err( p_playlist, "Unable write album art in %s",
                     psz_filename + 7 );
416
            free( psz_arturl );
417 418
            return VLC_EGENERIC;
        }
419 420
        int err = 0;
        while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
421
        {
422 423 424 425 426
            if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
            {
                err = errno;
                break;
            }
427
        }
428 429
        if( fclose( p_file ) && !err )
            err = errno;
430
        stream_Delete( p_stream );
431 432

        if( err )
433 434 435 436
        {
            errno = err;
            msg_Err( p_playlist, "%s: %m", psz_filename );
        }
437 438 439
        else
            msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );

440
        input_item_SetArtURL( p_item, psz_filename );
441 442
        i_status = VLC_SUCCESS;
    }
443
    free( psz_arturl );
444 445
    return i_status;
}
446

447 448 449
void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
{
    input_item_t *p_item = p_input->p->input.p_item;
450 451 452 453
    const char *psz_arturl;
    const char *psz_artist = NULL;
    const char *psz_album = NULL;
    const char *psz_title = NULL;
454
    char *psz_type = NULL;
455
    char psz_filename[PATH_MAX+1];
456 457 458 459 460 461 462 463
    FILE *f;
    input_attachment_t *p_attachment;
    struct stat s;
    int i_idx;

    /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
     * and then set it here to to be faster */

464 465
    psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );

466 467 468 469 470 471
    if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
    {
        msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
        return;
    }

472
    if( input_item_IsArtFetched( p_item ) )
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    {
        /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
         * condition */
        msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
        input_FindArtInCache( p_input, p_item );
        return;
    }

    /* */
    for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
    {
        if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
                     &psz_arturl[strlen("attachment://")] ) )
        {
            p_attachment = p_input->p->attachment[i_idx];
            break;
        }
    }
    if( !p_attachment || p_attachment->i_data <= 0 )
    {
        msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
494
        return;
495 496
    }

497 498 499 500 501 502 503 504
    psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
    psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
    psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
    if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
        psz_type = strdup( ".jpg" );
    else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
        psz_type = strdup( ".png" );

505
    if( !psz_title )
506
        psz_title = p_item->psz_name;
507

508
    if( (!psz_artist || !psz_album ) && !psz_title )
509
        return;
510

511
    ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
512
    ArtCacheCreateDir( psz_filename );
513
    ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
514
    free( psz_type );
515 516 517

    /* Check if we already dumped it */
    if( !utf8_stat( psz_filename+7, &s ) )
518 519
    {
        vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
520
        return;
521
    }
522 523 524 525 526

    f = utf8_fopen( psz_filename+7, "w" );
    if( f )
    {
        if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
527
            msg_Err( p_input, "%s: %m", psz_filename );
528
        else
529
        {
530
            msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
531 532
            vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
        }
533 534 535
        fclose( f );
    }
}