cdda.c 22.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * cdda.c : CD digital audio input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000, 2003-2006, 2008-2009 VLC authors and VideoLAN
5
 * $Id$
6 7 8 9
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Gildas Bazin <gbazin@netcourrier.com>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * 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
13 14 15 16
 * (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
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24
 *****************************************************************************/

25 26
/**
 * Todo:
27
 *   - Improve CDDB support (non-blocking, ...)
28 29 30
 *   - Fix tracknumber in MRL
 */

31 32 33 34
/*****************************************************************************
 * Preamble
 *****************************************************************************/

35 36 37
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
38
#include <assert.h>
39

40
#include <vlc_common.h>
41
#include <vlc_plugin.h>
42 43
#include <vlc_input.h>
#include <vlc_access.h>
44
#include <vlc_meta.h>
45
#include <vlc_charset.h> /* ToLocaleDup */
46

47
#include <vlc_codecs.h> /* For WAVEHEADER */
48
#include "vcd/cdrom.h"  /* For CDDA_DATA_SIZE */
49

50
#ifdef HAVE_LIBCDDB
51 52
 #include <cddb/cddb.h>
 #include <errno.h>
53 54
#endif

55
/*****************************************************************************
56
 * Module descriptior
57
 *****************************************************************************/
58 59
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
60

61
vlc_module_begin ()
62
    set_shortname( N_("Audio CD") )
63 64 65 66 67 68 69
    set_description( N_("Audio CD input") )
    set_capability( "access", 10 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
    set_callbacks( Open, Close )

    add_usage_hint( N_("[cdda:][device][@[track]]") )
70
    add_integer( "cdda-track", 0 , NULL, NULL, true )
71
        change_volatile ()
72
    add_integer( "cdda-first-sector", -1, NULL, NULL, true )
73
        change_volatile ()
74
    add_integer( "cdda-last-sector", -1, NULL, NULL, true )
75
        change_volatile ()
76

77
#ifdef HAVE_LIBCDDB
78
    add_string( "cddb-server", "freedb.videolan.org", N_( "CDDB Server" ),
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
79
            N_( "Address of the CDDB server to use." ), true )
80
    add_integer( "cddb-port", 80, N_( "CDDB port" ),
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
81
            N_( "CDDB Server port to use." ), true )
Kaarlo Raiha's avatar
Kaarlo Raiha committed
82
        change_integer_range( 1, 65535 )
83 84
#endif

85
    add_shortcut( "cdda", "cddasimple" )
86
vlc_module_end ()
87

88 89 90 91 92 93 94 95 96 97 98

/* how many blocks VCDRead will read in each loop */
#define CDDA_BLOCKS_ONCE 20
#define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)

/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
struct access_sys_t
{
    vcddev_t    *vcddev;                            /* vcd device descriptor */
99

100
    /* Current position */
101
    int         i_sector;                                  /* Current Sector */
102
    int        *p_sectors;                                  /* Track sectors */
103

104 105
    /* Wave header for the output data */
    WAVEHEADER  waveheader;
106
    bool        b_header;
107 108

    int         i_track;
109 110
    int         i_first_sector;
    int         i_last_sector;
111 112
};

113
static block_t *Block( access_t * );
114
static int      Seek( access_t *, uint64_t );
115
static int      Control( access_t *, int, va_list );
116

117
static int GetTracks( access_t *p_access, input_item_t *p_current );
118

119
#ifdef HAVE_LIBCDDB
120
static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors );
121 122
#endif

123
/*****************************************************************************
124
 * Open: open cdda
125
 *****************************************************************************/
126
static int Open( vlc_object_t *p_this )
127
{
128 129
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
130 131
    vcddev_t     *vcddev;
    char         *psz_name;
132

133
    if( !p_access->psz_filepath || !*p_access->psz_filepath )
134
    {
135
        /* Only when selected */
136 137
        if( !p_access->psz_access || !*p_access->psz_access )
            return VLC_EGENERIC;
138

139
        psz_name = var_InheritString( p_this, "cd-audio" );
140
        if( !psz_name )
141
            return VLC_EGENERIC;
142
    }
143
    else psz_name = ToLocaleDup( p_access->psz_filepath );
144

145
#if defined( WIN32 ) || defined( __OS2__ )
146 147 148 149
    if( psz_name[0] && psz_name[1] == ':' &&
        psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
#endif

150
    /* Open CDDA */
151
    if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name ) ) == NULL )
152
    {
153 154
        msg_Warn( p_access, "could not open %s", psz_name );
        free( psz_name );
155
        return VLC_EGENERIC;
156
    }
157
    free( psz_name );
158 159

    /* Set up p_access */
160
    STANDARD_BLOCK_ACCESS_INIT
161
    p_sys->vcddev = vcddev;
162

163 164
    /* Do we play a single track ? */
    p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
165

166 167
    if( p_sys->i_track < 0 )
    {
168
        /* We only do separate items if the whole disc is requested */
169
        input_thread_t *p_input = access_GetParentInput( p_access );
Clément Stenac's avatar
Clément Stenac committed
170

171
        int i_ret = -1;
172
        if( p_input )
173
        {
174 175 176 177 178
            input_item_t *p_current = input_GetItem( p_input );
            if( p_current )
                i_ret = GetTracks( p_access, p_current );

            vlc_object_release( p_input );
179
        }
180 181
        if( i_ret < 0 )
            goto error;
182 183
    }
    else
184
    {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        /* Build a WAV header for the output data */
        memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
        SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
        SetWLE( &p_sys->waveheader.BitsPerSample, 16);
        p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
        p_sys->waveheader.Length = 0;               /* we just don't know */
        p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
        p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
        SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
        SetWLE( &p_sys->waveheader.Modus, 2);
        SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
        SetWLE( &p_sys->waveheader.BytesPerSample,
                    2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
        SetDWLE( &p_sys->waveheader.BytesPerSec,
                    2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
        p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
        p_sys->waveheader.DataLength = 0;           /* we just don't know */

203 204 205 206
        p_sys->i_first_sector = var_InheritInteger( p_access,
                                                    "cdda-first-sector" );
        p_sys->i_last_sector  = var_InheritInteger( p_access,
                                                    "cdda-last-sector" );
207 208
        /* Tracknumber in MRL */
        if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
209
        {
210 211 212
            const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
                                                     p_sys->vcddev, &p_sys->p_sectors );
            if( p_sys->i_track >= i_titles )
213
            {
214
                msg_Err( p_access, "invalid track number" );
215 216
                goto error;
            }
217 218
            p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
            p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
219
        }
220

221 222 223
        p_sys->i_sector = p_sys->i_first_sector;
        p_access->info.i_size = (p_sys->i_last_sector - p_sys->i_first_sector)
                                     * (int64_t)CDDA_DATA_SIZE;
224
    }
225

226
    return VLC_SUCCESS;
227 228

error:
Laurent Aimar's avatar
Laurent Aimar committed
229
    free( p_sys->p_sectors );
230 231 232
    ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
    free( p_sys );
    return VLC_EGENERIC;
233 234 235
}

/*****************************************************************************
236
 * Close: closes cdda
237
 *****************************************************************************/
238
static void Close( vlc_object_t *p_this )
239
{
240 241
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys = p_access->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
242 243

    free( p_sys->p_sectors );
244 245
    ioctl_Close( p_this, p_sys->vcddev );
    free( p_sys );
246 247 248
}

/*****************************************************************************
249
 * Block: read data (CDDA_DATA_ONCE)
250
 *****************************************************************************/
251
static block_t *Block( access_t *p_access )
252
{
253
    access_sys_t *p_sys = p_access->p_sys;
254 255
    int i_blocks = CDDA_BLOCKS_ONCE;
    block_t *p_block;
256

257
    if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
258

259 260
    /* Check end of file */
    if( p_access->info.b_eof ) return NULL;
261

262
    if( !p_sys->b_header )
263
    {
264
        /* Return only the header */
265
        p_block = block_Alloc( sizeof( WAVEHEADER ) );
266
        memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
267
        p_sys->b_header = true;
268
        return p_block;
269
    }
270

271
    if( p_sys->i_sector >= p_sys->i_last_sector )
272
    {
273
        p_access->info.b_eof = true;
274
        return NULL;
275
    }
276

277 278 279
    /* Don't read too far */
    if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
        i_blocks = p_sys->i_last_sector - p_sys->i_sector;
280

281
    /* Do the actual reading */
282
    if( !( p_block = block_Alloc( i_blocks * CDDA_DATA_SIZE ) ) )
283
    {
284
        msg_Err( p_access, "cannot get a new block of size: %i",
285
                 i_blocks * CDDA_DATA_SIZE );
286
        return NULL;
287 288
    }

289 290
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
            p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
291
    {
292
        msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
293
        block_Release( p_block );
294

295 296 297
        /* Try to skip one sector (in case of bad sectors) */
        p_sys->i_sector++;
        p_access->info.i_pos += CDDA_DATA_SIZE;
298
        return NULL;
299 300
    }

301 302 303
    /* Update a few values */
    p_sys->i_sector += i_blocks;
    p_access->info.i_pos += p_block->i_buffer;
304 305

    return p_block;
306 307
}

308 309
/****************************************************************************
 * Seek
310
 ****************************************************************************/
311
static int Seek( access_t *p_access, uint64_t i_pos )
312
{
313
    access_sys_t *p_sys = p_access->p_sys;
314

315
    /* Next sector to read */
316
    p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
317
    assert( p_sys->i_sector >= 0 );
318
    p_access->info.i_pos = i_pos;
319

320 321
    return VLC_SUCCESS;
}
322

323 324 325 326 327 328 329 330 331 332 333
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    switch( i_query )
    {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
334
            *va_arg( args, bool* ) = true;
335 336 337
            break;

        case ACCESS_GET_PTS_DELAY:
338
            *va_arg( args, int64_t * ) =
339
                INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
340 341 342
            break;

        case ACCESS_SET_PAUSE_STATE:
343 344
            break;

345 346
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
347
        case ACCESS_GET_META:
348
        case ACCESS_SET_SEEKPOINT:
349
        case ACCESS_SET_PRIVATE_ID_STATE:
350
        case ACCESS_GET_CONTENT_TYPE:
351
            return VLC_EGENERIC;
352

353
        default:
354
            msg_Warn( p_access, "unimplemented query in control" );
355 356 357
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
358
}
359

360
static int GetTracks( access_t *p_access, input_item_t *p_current )
361 362
{
    access_sys_t *p_sys = p_access->p_sys;
363 364 365 366

    const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
                                             p_sys->vcddev, &p_sys->p_sectors );
    if( i_titles <= 0 )
367
    {
368 369 370 371
        if( i_titles < 0 )
            msg_Err( p_access, "unable to count tracks" );
        else if( i_titles <= 0 )
            msg_Err( p_access, "no audio tracks found" );
372 373 374
        return VLC_EGENERIC;;
    }

375
    /* */
376
    input_item_SetName( p_current, "Audio CD" );
377

378 379 380
    const char *psz_album = NULL;
    const char *psz_year = NULL;
    const char *psz_genre = NULL;
381 382
    const char *psz_artist = NULL;
    const char *psz_description = NULL;
383

384 385 386 387 388
/* Return true if the given string is not NULL and not empty */
#define NONEMPTY( psz ) ( (psz) && *(psz) )
/* If the given string is NULL or empty, fill it by the return value of 'code' */
#define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)

389
    /* Retreive CDDB information */
390
#ifdef HAVE_LIBCDDB
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
391
    char psz_year_buffer[4+1];
392
    msg_Dbg( p_access, "fetching infos with CDDB" );
393
    cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
394
    if( p_disc )
395
    {
396
        psz_album = cddb_disc_get_title( p_disc );
397
        psz_genre = cddb_disc_get_genre( p_disc );
398

399
        /* */
400
        const unsigned i_year = cddb_disc_get_year( p_disc );
401 402 403 404 405 406 407 408 409
        if( i_year > 0 )
        {
            psz_year = psz_year_buffer;
            snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
        }

        /* Set artist only if unique */
        for( int i = 0; i < i_titles; i++ )
        {
410
            cddb_track_t *t = cddb_disc_get_track( p_disc, i );
411 412 413 414 415 416 417 418 419 420 421
            if( !t )
                continue;
            const char *psz_track_artist = cddb_track_get_artist( t );
            if( psz_artist && psz_track_artist &&
                strcmp( psz_artist, psz_track_artist ) )
            {
                psz_artist = NULL;
                break;
            }
            psz_artist = psz_track_artist;
        }
422
    }
423
#endif
424

425
    /* CD-Text */
426 427 428 429 430 431 432 433 434 435
    vlc_meta_t **pp_cd_text;
    int        i_cd_text;

    if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
    {
        msg_Dbg( p_access, "CD-TEXT information missing" );
        i_cd_text = 0;
        pp_cd_text = NULL;
    }

436
    /* Retrieve CD-TEXT information but prefer CDDB */
437 438 439 440 441 442 443 444 445
    if( i_cd_text > 0 && pp_cd_text[0] )
    {
        const vlc_meta_t *p_disc = pp_cd_text[0];
        ON_EMPTY( psz_album,       vlc_meta_Get( p_disc, vlc_meta_Album ) );
        ON_EMPTY( psz_genre,       vlc_meta_Get( p_disc, vlc_meta_Genre ) );
        ON_EMPTY( psz_artist,      vlc_meta_Get( p_disc, vlc_meta_Artist ) );
        ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
    }

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    if( NONEMPTY( psz_album ) )
    {
        input_item_SetName( p_current, psz_album );
        input_item_SetAlbum( p_current, psz_album );
    }

    if( NONEMPTY( psz_genre ) )
        input_item_SetGenre( p_current, psz_genre );

    if( NONEMPTY( psz_artist ) )
        input_item_SetArtist( p_current, psz_artist );

    if( NONEMPTY( psz_year ) )
        input_item_SetDate( p_current, psz_year );

    if( NONEMPTY( psz_description ) )
        input_item_SetDescription( p_current, psz_description );

    const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
                               CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
    input_item_SetDuration( p_current, i_duration );

468 469
    input_item_node_t *p_root = input_item_node_Create( p_current );

470
    /* Build title table */
471
    for( int i = 0; i < i_titles; i++ )
472
    {
473 474
        input_item_t *p_input_item;

475
        char *psz_uri, *psz_opt, *psz_first, *psz_last;
476 477 478
        char *psz_name;

        msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
479

480
        /* */
481
        if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
482 483 484 485 486
            psz_uri = NULL;
        if( asprintf( &psz_opt, "cdda-track=%i", i+1 ) == -1 )
            psz_opt = NULL;
        if( asprintf( &psz_first, "cdda-first-sector=%i",p_sys->p_sectors[i] ) == -1 )
            psz_first = NULL;
487 488
        if( asprintf( &psz_last, "cdda-last-sector=%i", p_sys->p_sectors[i+1] ) == -1 )
            psz_last = NULL;
489 490

        /* Define a "default name" */
491
        if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
492
            psz_name = NULL;
493 494

        /* Create playlist items */
495 496
        const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
                                   CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
497 498
        p_input_item = input_item_NewWithType( psz_uri, psz_name, 0, NULL, 0,
                                               i_duration, ITEM_TYPE_DISC );
499
        input_item_CopyOptions( p_current, p_input_item );
500 501 502
        input_item_AddOption( p_input_item, psz_first, VLC_INPUT_OPTION_TRUSTED );
        input_item_AddOption( p_input_item, psz_last, VLC_INPUT_OPTION_TRUSTED );
        input_item_AddOption( p_input_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
503

504 505 506 507 508
        const char *psz_track_title = NULL;
        const char *psz_track_artist = NULL;
        const char *psz_track_genre = NULL;
        const char *psz_track_description = NULL;

509
#ifdef HAVE_LIBCDDB
510
        /* Retreive CDDB information */
511
        if( p_disc )
512
        {
513
            cddb_track_t *t = cddb_disc_get_track( p_disc, i );
514
            if( t != NULL )
515
            {
516 517
                psz_track_title = cddb_track_get_title( t );
                psz_track_artist = cddb_track_get_artist( t );
518
            }
519 520
        }
#endif
521

522
        /* Retreive CD-TEXT information but prefer CDDB */
523 524 525 526 527 528 529 530 531 532
        if( i+1 < i_cd_text && pp_cd_text[i+1] )
        {
            const vlc_meta_t *t = pp_cd_text[i+1];

            ON_EMPTY( psz_track_title,       vlc_meta_Get( t, vlc_meta_Title ) );
            ON_EMPTY( psz_track_artist,      vlc_meta_Get( t, vlc_meta_Artist ) );
            ON_EMPTY( psz_track_genre,       vlc_meta_Get( t, vlc_meta_Genre ) );
            ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
        }

533 534 535 536
        /* */
        ON_EMPTY( psz_track_artist,       psz_artist );
        ON_EMPTY( psz_track_genre,        psz_genre );
        ON_EMPTY( psz_track_description,  psz_description );
537

538 539 540 541 542
        /* */
        if( NONEMPTY( psz_track_title ) )
        {
            input_item_SetName( p_input_item, psz_track_title );
            input_item_SetTitle( p_input_item, psz_track_title );
543
        }
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

        if( NONEMPTY( psz_track_artist ) )
            input_item_SetArtist( p_input_item, psz_track_artist );

        if( NONEMPTY( psz_track_genre ) )
            input_item_SetGenre( p_input_item, psz_track_genre );

        if( NONEMPTY( psz_track_description ) )
            input_item_SetDescription( p_input_item, psz_track_description );

        if( NONEMPTY( psz_album ) )
            input_item_SetAlbum( p_input_item, psz_album );

        if( NONEMPTY( psz_year ) )
            input_item_SetDate( p_input_item, psz_year );

560 561 562 563
        char psz_num[3+1];
        snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
        input_item_SetTrackNum( p_input_item, psz_num );

564
        input_item_node_AppendItem( p_root, p_input_item );
565
        vlc_gc_decref( p_input_item );
566 567
        free( psz_uri ); free( psz_opt ); free( psz_name );
        free( psz_first ); free( psz_last );
568
    }
569 570 571
#undef ON_EMPTY
#undef NONEMPTY

572
    input_item_node_PostAndDelete( p_root );
573

574 575 576 577 578 579 580 581 582
    /* */
    for( int i = 0; i < i_cd_text; i++ )
    {
        vlc_meta_t *p_meta = pp_cd_text[i];
        if( !p_meta )
            continue;
        vlc_meta_Delete( p_meta );
    }
    free( pp_cd_text );
583 584 585 586 587

#ifdef HAVE_LIBCDDB
    if( p_disc )
        cddb_disc_destroy( p_disc );
#endif
588
    return VLC_SUCCESS;
589
}
590 591

#ifdef HAVE_LIBCDDB
592
static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
593
{
594
    if( var_InheritInteger( p_access, "album-art" ) == ALBUM_ART_WHEN_ASKED )
595 596
        return NULL;

597 598
    /* */
    cddb_conn_t *p_cddb = cddb_new();
599 600 601
    if( !p_cddb )
    {
        msg_Warn( p_access, "unable to use CDDB" );
602
        return NULL;
603 604
    }

605
    /* */
606 607 608

    cddb_http_enable( p_cddb );

609 610 611 612 613 614
    char *psz_tmp = var_InheritString( p_access, "cddb-server" );
    if( psz_tmp )
    {
        cddb_set_server_name( p_cddb, psz_tmp );
        free( psz_tmp );
    }
615

616
    cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
617 618 619

    cddb_set_email_address( p_cddb, "vlc@videolan.org" );

620 621 622
    cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
    cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );

623

624 625 626 627 628 629 630 631 632
    char *psz_cachedir;
    char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );

    if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
        cddb_cache_enable( p_cddb );
        cddb_cache_set_dir( p_cddb, psz_cachedir );
        free( psz_cachedir );
    }
    free( psz_temp );
633 634 635

    cddb_set_timeout( p_cddb, 10 );

636 637 638
    /* */
    cddb_disc_t *p_disc = cddb_disc_new();
    if( !p_disc )
639
    {
640
        msg_Err( p_access, "unable to create CDDB disc structure." );
641
        goto error;
642 643
    }

644 645
    int64_t i_length = 0;
    for( int i = 0; i < i_titles; i++ )
646 647
    {
        cddb_track_t *t = cddb_track_new();
648 649 650 651
        cddb_track_set_frame_offset( t, p_sectors[i] );
        cddb_disc_add_track( p_disc, t );
        const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
                               (int64_t)CDDA_DATA_SIZE;
652
        i_length += INT64_C(1000000) * i_size / 44100 / 4  ;
653 654
    }

655
    cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
656

657
    if( !cddb_disc_calc_discid( p_disc ) )
658 659
    {
        msg_Err( p_access, "CDDB disc ID calculation failed" );
660
        goto error;
661 662
    }

663
    const int i_matches = cddb_query( p_cddb, p_disc );
664
    if( i_matches < 0 )
665
    {
666
        msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
667 668
        goto error;
    }
669 670 671 672 673 674
    else if( i_matches == 0 )
    {
        msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
        goto error;
    }
    else if( i_matches > 1 )
675
        msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
676

677
    cddb_read( p_cddb, p_disc );
678 679

    cddb_destroy( p_cddb);
680
    return p_disc;
681

682 683 684 685 686
error:
    if( p_disc )
        cddb_disc_destroy( p_disc );
    cddb_destroy( p_cddb );
    return NULL;
687 688
}
#endif /*HAVE_LIBCDDB*/
689