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

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

gbazin's avatar
   
gbazin committed
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>
zorglub's avatar
zorglub committed
42
43
#include <vlc_input.h>
#include <vlc_access.h>
44
#include <vlc_meta.h>
45
#include <vlc_charset.h> /* ToLocaleDup */
gbazin's avatar
   
gbazin committed
46

zorglub's avatar
zorglub committed
47
#include <vlc_codecs.h> /* For WAVEHEADER */
48
#include "vcd/cdrom.h"  /* For CDDA_DATA_SIZE */
gbazin's avatar
   
gbazin committed
49

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

gbazin's avatar
   
gbazin committed
55
/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
56
 * Module descriptior
gbazin's avatar
   
gbazin committed
57
 *****************************************************************************/
58
59
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
gbazin's avatar
   
gbazin committed
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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
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 )
VLC_help's avatar
VLC_help committed
82
        change_integer_range( 1, 65535 )
83
84
#endif

85
    add_shortcut( "cdda", "cddasimple" )
86
vlc_module_end ()
gbazin's avatar
   
gbazin committed
87

Laurent Aimar's avatar
Laurent Aimar committed
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
    uint64_t    size;
100

101
    /* Current position */
Laurent Aimar's avatar
Laurent Aimar committed
102
    int         i_sector;                                  /* Current Sector */
103
    int        *p_sectors;                                  /* Track sectors */
Laurent Aimar's avatar
Laurent Aimar committed
104

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

    int         i_track;
110
111
    int         i_first_sector;
    int         i_last_sector;
Laurent Aimar's avatar
Laurent Aimar committed
112
113
};

114
static block_t *Block( access_t * );
115
static int      Seek( access_t *, uint64_t );
116
static int      Control( access_t *, int, va_list );
Laurent Aimar's avatar
Laurent Aimar committed
117

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

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

gbazin's avatar
   
gbazin committed
124
/*****************************************************************************
125
 * Open: open cdda
gbazin's avatar
   
gbazin committed
126
 *****************************************************************************/
127
static int Open( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
128
{
129
    access_t     *p_access = (access_t*)p_this;
130
131
    vcddev_t     *vcddev;
    char         *psz_name;
132

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

ivoire's avatar
ivoire committed
139
        psz_name = var_InheritString( p_this, "cd-audio" );
140
        if( !psz_name )
Laurent Aimar's avatar
Laurent Aimar committed
141
            return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
142
    }
143
    else psz_name = ToLocaleDup( p_access->psz_filepath );
gbazin's avatar
   
gbazin committed
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
151
    access_sys_t *p_sys = calloc( 1, sizeof (*p_sys) );
    if( unlikely(p_sys == NULL) )
gbazin's avatar
   
gbazin committed
152
    {
153
        free( psz_name );
154
155
156
157
158
159
160
161
162
163
164
        return VLC_ENOMEM;
    }
    p_access->p_sys = p_sys;

    /* Open CDDA */
    vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name );
    if( vcddev == NULL )
        msg_Warn( p_access, "could not open %s", psz_name );
    free( psz_name );
    if( vcddev == NULL )
    {
165
        free( p_sys );
Laurent Aimar's avatar
Laurent Aimar committed
166
        return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
167
    }
168

Laurent Aimar's avatar
Laurent Aimar committed
169
    p_sys->vcddev = vcddev;
170

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
171
172
    /* Do we play a single track ? */
    p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
173

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
174
175
    if( p_sys->i_track < 0 )
    {
176
        /* We only do separate items if the whole disc is requested */
177
        input_thread_t *p_input = access_GetParentInput( p_access );
zorglub's avatar
Grmbl    
zorglub committed
178

179
        int i_ret = -1;
180
        if( p_input )
181
        {
182
183
184
185
186
            input_item_t *p_current = input_GetItem( p_input );
            if( p_current )
                i_ret = GetTracks( p_access, p_current );

            vlc_object_release( p_input );
187
        }
188
189
        if( i_ret < 0 )
            goto error;
190
191
    }
    else
192
    {
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
        /* 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 */

ivoire's avatar
ivoire committed
211
212
213
214
        p_sys->i_first_sector = var_InheritInteger( p_access,
                                                    "cdda-first-sector" );
        p_sys->i_last_sector  = var_InheritInteger( p_access,
                                                    "cdda-last-sector" );
215
216
        /* Tracknumber in MRL */
        if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
217
        {
218
219
220
            const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
                                                     p_sys->vcddev, &p_sys->p_sectors );
            if( p_sys->i_track >= i_titles )
221
            {
222
                msg_Err( p_access, "invalid track number" );
223
224
                goto error;
            }
225
226
            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];
227
        }
gbazin's avatar
   
gbazin committed
228

229
        p_sys->i_sector = p_sys->i_first_sector;
230
        p_sys->size = (p_sys->i_last_sector - p_sys->i_first_sector)
231
                                     * (int64_t)CDDA_DATA_SIZE;
232
    }
233

234
235
236
    /* Set up p_access */
    access_InitFields( p_access );
    ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
Laurent Aimar's avatar
Laurent Aimar committed
237
    return VLC_SUCCESS;
238
239

error:
Laurent Aimar's avatar
Laurent Aimar committed
240
    free( p_sys->p_sectors );
241
242
243
    ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
    free( p_sys );
    return VLC_EGENERIC;
gbazin's avatar
   
gbazin committed
244
245
246
}

/*****************************************************************************
247
 * Close: closes cdda
gbazin's avatar
   
gbazin committed
248
 *****************************************************************************/
249
static void Close( vlc_object_t *p_this )
gbazin's avatar
   
gbazin committed
250
{
251
252
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys = p_access->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
253
254

    free( p_sys->p_sectors );
Laurent Aimar's avatar
Laurent Aimar committed
255
256
    ioctl_Close( p_this, p_sys->vcddev );
    free( p_sys );
gbazin's avatar
   
gbazin committed
257
258
259
}

/*****************************************************************************
260
 * Block: read data (CDDA_DATA_ONCE)
gbazin's avatar
   
gbazin committed
261
 *****************************************************************************/
262
static block_t *Block( access_t *p_access )
gbazin's avatar
   
gbazin committed
263
{
264
    access_sys_t *p_sys = p_access->p_sys;
265
266
    int i_blocks = CDDA_BLOCKS_ONCE;
    block_t *p_block;
267

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

270
271
    /* Check end of file */
    if( p_access->info.b_eof ) return NULL;
gbazin's avatar
   
gbazin committed
272

273
    if( !p_sys->b_header )
gbazin's avatar
   
gbazin committed
274
    {
275
        /* Return only the header */
276
        p_block = block_Alloc( sizeof( WAVEHEADER ) );
277
        memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
278
        p_sys->b_header = true;
279
        return p_block;
gbazin's avatar
   
gbazin committed
280
    }
gbazin's avatar
   
gbazin committed
281

282
    if( p_sys->i_sector >= p_sys->i_last_sector )
gbazin's avatar
   
gbazin committed
283
    {
284
        p_access->info.b_eof = true;
285
        return NULL;
gbazin's avatar
   
gbazin committed
286
    }
287

288
289
290
    /* 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;
gbazin's avatar
   
gbazin committed
291

292
    /* Do the actual reading */
293
    if( !( p_block = block_Alloc( i_blocks * CDDA_DATA_SIZE ) ) )
294
    {
295
        msg_Err( p_access, "cannot get a new block of size: %i",
296
                 i_blocks * CDDA_DATA_SIZE );
297
        return NULL;
gbazin's avatar
   
gbazin committed
298
299
    }

300
301
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
            p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
gbazin's avatar
   
gbazin committed
302
    {
303
        msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
304
        block_Release( p_block );
305

306
307
308
        /* Try to skip one sector (in case of bad sectors) */
        p_sys->i_sector++;
        p_access->info.i_pos += CDDA_DATA_SIZE;
309
        return NULL;
gbazin's avatar
   
gbazin committed
310
311
    }

312
313
314
    /* Update a few values */
    p_sys->i_sector += i_blocks;
    p_access->info.i_pos += p_block->i_buffer;
315
316

    return p_block;
gbazin's avatar
   
gbazin committed
317
318
}

319
320
/****************************************************************************
 * Seek
gbazin's avatar
   
gbazin committed
321
 ****************************************************************************/
322
static int Seek( access_t *p_access, uint64_t i_pos )
gbazin's avatar
   
gbazin committed
323
{
324
    access_sys_t *p_sys = p_access->p_sys;
gbazin's avatar
   
gbazin committed
325

326
    /* Next sector to read */
327
    p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
328
    assert( p_sys->i_sector >= 0 );
329
    p_access->info.i_pos = i_pos;
gbazin's avatar
   
gbazin committed
330

331
332
    return VLC_SUCCESS;
}
gbazin's avatar
   
gbazin committed
333

334
335
336
337
338
339
340
341
342
343
344
/*****************************************************************************
 * 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:
345
            *va_arg( args, bool* ) = true;
346
            break;
347
348
349
        case ACCESS_GET_SIZE:
            *va_arg( args, uint64_t * ) = p_access->p_sys->size;
            break;
350
        case ACCESS_GET_PTS_DELAY:
351
            *va_arg( args, int64_t * ) =
352
                INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
353
354
355
            break;

        case ACCESS_SET_PAUSE_STATE:
hartman's avatar
hartman committed
356
357
            break;

358
359
360
361
        default:
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
gbazin's avatar
   
gbazin committed
362
}
363

364
static int GetTracks( access_t *p_access, input_item_t *p_current )
365
366
{
    access_sys_t *p_sys = p_access->p_sys;
367
368
369
370

    const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
                                             p_sys->vcddev, &p_sys->p_sectors );
    if( i_titles <= 0 )
371
    {
372
373
374
375
        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" );
376
377
378
        return VLC_EGENERIC;;
    }

379
    /* */
380
    input_item_SetName( p_current, "Audio CD" );
381

382
383
384
    const char *psz_album = NULL;
    const char *psz_year = NULL;
    const char *psz_genre = NULL;
385
386
    const char *psz_artist = NULL;
    const char *psz_description = NULL;
387

388
389
390
391
392
/* 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)

ivoire's avatar
ivoire committed
393
    /* Retreive CDDB information */
394
#ifdef HAVE_LIBCDDB
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
395
    char psz_year_buffer[4+1];
396
    msg_Dbg( p_access, "fetching infos with CDDB" );
397
    cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
398
    if( p_disc )
399
    {
400
        msg_Dbg( p_access, "Disc ID: %08x", cddb_disc_get_discid( p_disc ) );
401
        psz_album = cddb_disc_get_title( p_disc );
402
        psz_genre = cddb_disc_get_genre( p_disc );
403

404
        /* */
405
        const unsigned i_year = cddb_disc_get_year( p_disc );
406
407
408
409
410
411
412
413
414
        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++ )
        {
415
            cddb_track_t *t = cddb_disc_get_track( p_disc, i );
416
417
418
419
420
421
422
423
424
425
426
            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;
        }
427
    }
428
429
    else
        msg_Dbg( p_access, "GetCDDBInfo failed" );
430
#endif
431

432
    /* CD-Text */
433
434
435
436
437
438
439
440
441
442
    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;
    }

ivoire's avatar
ivoire committed
443
    /* Retrieve CD-TEXT information but prefer CDDB */
444
445
446
447
448
449
450
451
452
    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 ) );
    }

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    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 );

475
476
    input_item_node_t *p_root = input_item_node_Create( p_current );

477
    /* Build title table */
478
    for( int i = 0; i < i_titles; i++ )
479
    {
480
        char *psz_uri, *psz_opt, *psz_name;
481
482

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

484
        if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
485
            continue;
486
487

        /* Define a "default name" */
488
        if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
489
            psz_name = psz_uri;
490
491

        /* Create playlist items */
Laurent Aimar's avatar
Laurent Aimar committed
492
493
        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;
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522

        input_item_t *p_item = input_item_NewWithType( psz_uri, psz_name, 0,
                                         NULL, 0, i_duration, ITEM_TYPE_DISC );
        if( likely(psz_name != psz_uri) )
            free( psz_name );
        free( psz_uri );

        if( unlikely(p_item == NULL) )
            continue;

        input_item_CopyOptions( p_current, p_item );

        if( likely(asprintf( &psz_opt, "cdda-track=%i", i+1 ) != -1) )
        {
            input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
            free( psz_opt );
        }
        if( likely(asprintf( &psz_opt, "cdda-first-sector=%i",
                             p_sys->p_sectors[i] ) != -1) )
        {
            input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
            free( psz_opt );
        }
        if( likely(asprintf( &psz_opt, "cdda-last-sector=%i",
                             p_sys->p_sectors[i+1] ) != -1) )
        {
            input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
            free( psz_opt );
        }
523

524
525
526
527
528
        const char *psz_track_title = NULL;
        const char *psz_track_artist = NULL;
        const char *psz_track_genre = NULL;
        const char *psz_track_description = NULL;

529
#ifdef HAVE_LIBCDDB
ivoire's avatar
ivoire committed
530
        /* Retreive CDDB information */
531
        if( p_disc )
532
        {
533
            cddb_track_t *t = cddb_disc_get_track( p_disc, i );
534
            if( t != NULL )
535
            {
536
537
                psz_track_title = cddb_track_get_title( t );
                psz_track_artist = cddb_track_get_artist( t );
538
            }
539
540
        }
#endif
541

ivoire's avatar
ivoire committed
542
        /* Retreive CD-TEXT information but prefer CDDB */
543
544
545
546
547
548
549
550
551
552
        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 ) );
        }

553
554
555
556
        /* */
        ON_EMPTY( psz_track_artist,       psz_artist );
        ON_EMPTY( psz_track_genre,        psz_genre );
        ON_EMPTY( psz_track_description,  psz_description );
557

558
559
560
        /* */
        if( NONEMPTY( psz_track_title ) )
        {
561
562
            input_item_SetName( p_item, psz_track_title );
            input_item_SetTitle( p_item, psz_track_title );
563
        }
564
565

        if( NONEMPTY( psz_track_artist ) )
566
            input_item_SetArtist( p_item, psz_track_artist );
567
568

        if( NONEMPTY( psz_track_genre ) )
569
            input_item_SetGenre( p_item, psz_track_genre );
570
571

        if( NONEMPTY( psz_track_description ) )
572
            input_item_SetDescription( p_item, psz_track_description );
573
574

        if( NONEMPTY( psz_album ) )
575
            input_item_SetAlbum( p_item, psz_album );
576
577

        if( NONEMPTY( psz_year ) )
578
            input_item_SetDate( p_item, psz_year );
579

580
581
        char psz_num[3+1];
        snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
582
        input_item_SetTrackNum( p_item, psz_num );
583

584
585
        input_item_node_AppendItem( p_root, p_item );
        vlc_gc_decref( p_item );
586
    }
587
588
589
#undef ON_EMPTY
#undef NONEMPTY

590
    input_item_node_PostAndDelete( p_root );
591

592
593
594
595
596
597
598
599
600
    /* */
    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 );
601
602
603
604
605

#ifdef HAVE_LIBCDDB
    if( p_disc )
        cddb_disc_destroy( p_disc );
#endif
606
    return VLC_SUCCESS;
607
}
608
609

#ifdef HAVE_LIBCDDB
610
static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
611
{
612
613
    if( var_InheritInteger( p_access, "album-art" ) != ALBUM_ART_ALL &&
        !  var_InheritBool( p_access, "metadata-network-access" ) )
614
615
    {
        msg_Dbg( p_access, "Album art policy set to manual; no automatic fetching" );
616
        return NULL;
617
    }
618

619
620
    /* */
    cddb_conn_t *p_cddb = cddb_new();
621
622
623
    if( !p_cddb )
    {
        msg_Warn( p_access, "unable to use CDDB" );
624
        return NULL;
625
626
    }

627
    /* */
628
629
630

    cddb_http_enable( p_cddb );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
631
632
633
634
635
636
    char *psz_tmp = var_InheritString( p_access, "cddb-server" );
    if( psz_tmp )
    {
        cddb_set_server_name( p_cddb, psz_tmp );
        free( psz_tmp );
    }
637

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
638
    cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
639
640
641

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

642
643
644
    cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
    cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );

645

646
647
648
649
650
651
652
653
654
    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 );
655
656
657

    cddb_set_timeout( p_cddb, 10 );

658
659
660
    /* */
    cddb_disc_t *p_disc = cddb_disc_new();
    if( !p_disc )
661
    {
662
        msg_Err( p_access, "unable to create CDDB disc structure." );
663
        goto error;
664
665
    }

666
    int64_t i_length = 2000000; /* PreGap */
667
    for( int i = 0; i < i_titles; i++ )
668
669
    {
        cddb_track_t *t = cddb_track_new();
670
671
        cddb_track_set_frame_offset( t, p_sectors[i] + 150 );  /* Pregap offset */

672
673
674
        cddb_disc_add_track( p_disc, t );
        const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
                               (int64_t)CDDA_DATA_SIZE;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
675
        i_length += INT64_C(1000000) * i_size / 44100 / 4  ;
676
677

        msg_Dbg( p_access, "Track %i offset: %i", i, p_sectors[i] + 150 );
678
679
    }

680
    msg_Dbg( p_access, "Total length: %i", i_length/1000000 );
681
    cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
682

683
    if( !cddb_disc_calc_discid( p_disc ) )
684
685
    {
        msg_Err( p_access, "CDDB disc ID calculation failed" );
686
        goto error;
687
688
    }

689
    const int i_matches = cddb_query( p_cddb, p_disc );
690
    if( i_matches < 0 )
691
    {
692
        msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
693
694
        goto error;
    }
695
696
697
698
699
700
    else if( i_matches == 0 )
    {
        msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
        goto error;
    }
    else if( i_matches > 1 )
701
        msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
702

703
    cddb_read( p_cddb, p_disc );
704
705

    cddb_destroy( p_cddb);
706
    return p_disc;
707

708
709
710
711
712
error:
    if( p_disc )
        cddb_disc_destroy( p_disc );
    cddb_destroy( p_cddb );
    return NULL;
713
714
}
#endif /*HAVE_LIBCDDB*/
715