]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
direct3d11: implement the pixel format fallback
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003-2006, 2008-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /**
26  * Todo:
27  *   - Improve CDDB support (non-blocking, ...)
28  *   - Fix tracknumber in MRL
29  */
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38 #include <assert.h>
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_input.h>
43 #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  #include <cddb/cddb.h>
52  #include <errno.h>
53 #endif
54
55 /*****************************************************************************
56  * Module descriptior
57  *****************************************************************************/
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 vlc_module_begin ()
62     set_shortname( N_("Audio CD") )
63     set_description( N_("Audio CD input") )
64     set_capability( "access", 10 )
65     set_category( CAT_INPUT )
66     set_subcategory( SUBCAT_INPUT_ACCESS )
67     set_callbacks( Open, Close )
68
69     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" ),
79             N_( "Address of the CDDB server to use." ), true )
80     add_integer( "cddb-port", 80, N_( "CDDB port" ),
81             N_( "CDDB Server port to use." ), true )
82         change_integer_range( 1, 65535 )
83 #endif
84
85     add_shortcut( "cdda", "cddasimple" )
86 vlc_module_end ()
87
88
89 /* how many blocks VCDRead will read in each loop */
90 #define CDDA_BLOCKS_ONCE 20
91 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
92
93 /*****************************************************************************
94  * Access: local prototypes
95  *****************************************************************************/
96 struct access_sys_t
97 {
98     vcddev_t    *vcddev;                            /* vcd device descriptor */
99     uint64_t    size;
100
101     /* Current position */
102     int         i_sector;                                  /* Current Sector */
103     int        *p_sectors;                                  /* Track sectors */
104
105     /* Wave header for the output data */
106     WAVEHEADER  waveheader;
107     bool        b_header;
108
109     int         i_track;
110     int         i_first_sector;
111     int         i_last_sector;
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 );
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 #endif
123
124 /*****************************************************************************
125  * Open: open cdda
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     access_t     *p_access = (access_t*)p_this;
130     vcddev_t     *vcddev;
131     char         *psz_name;
132
133     if( !p_access->psz_filepath || !*p_access->psz_filepath )
134     {
135         /* Only when selected */
136         if( !p_access->psz_access || !*p_access->psz_access )
137             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     if( psz_name[0] && psz_name[1] == ':' &&
147         psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
148 #endif
149
150     access_sys_t *p_sys = calloc( 1, sizeof (*p_sys) );
151     if( unlikely(p_sys == NULL) )
152     {
153         free( psz_name );
154         return VLC_ENOMEM;
155     }
156     p_access->p_sys = p_sys;
157
158     /* Open CDDA */
159     vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name );
160     if( vcddev == NULL )
161         msg_Warn( p_access, "could not open %s", psz_name );
162     free( psz_name );
163     if( vcddev == NULL )
164     {
165         free( p_sys );
166         return VLC_EGENERIC;
167     }
168
169     p_sys->vcddev = vcddev;
170
171     /* Do we play a single track ? */
172     p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
173
174     if( p_sys->i_track < 0 )
175     {
176         /* We only do separate items if the whole disc is requested */
177         input_thread_t *p_input = access_GetParentInput( p_access );
178
179         int i_ret = -1;
180         if( p_input )
181         {
182             input_item_t *p_current = input_GetItem( p_input );
183             if( p_current )
184                 i_ret = GetTracks( p_access, p_current );
185
186             vlc_object_release( p_input );
187         }
188         if( i_ret < 0 )
189             goto error;
190     }
191     else
192     {
193         /* Build a WAV header for the output data */
194         memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
195         SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
196         SetWLE( &p_sys->waveheader.BitsPerSample, 16);
197         p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
198         p_sys->waveheader.Length = 0;               /* we just don't know */
199         p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
200         p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
201         SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
202         SetWLE( &p_sys->waveheader.Modus, 2);
203         SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
204         SetWLE( &p_sys->waveheader.BytesPerSample,
205                     2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
206         SetDWLE( &p_sys->waveheader.BytesPerSec,
207                     2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
208         p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
209         p_sys->waveheader.DataLength = 0;           /* we just don't know */
210
211         p_sys->i_first_sector = var_InheritInteger( p_access,
212                                                     "cdda-first-sector" );
213         p_sys->i_last_sector  = var_InheritInteger( p_access,
214                                                     "cdda-last-sector" );
215         /* Tracknumber in MRL */
216         if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
217         {
218             const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
219                                                      p_sys->vcddev, &p_sys->p_sectors );
220             if( p_sys->i_track >= i_titles )
221             {
222                 msg_Err( p_access, "invalid track number" );
223                 goto error;
224             }
225             p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
226             p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
227         }
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     /* Set up p_access */
235     access_InitFields( p_access );
236     ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
237     return VLC_SUCCESS;
238
239 error:
240     free( p_sys->p_sectors );
241     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
242     free( p_sys );
243     return VLC_EGENERIC;
244 }
245
246 /*****************************************************************************
247  * Close: closes cdda
248  *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
250 {
251     access_t     *p_access = (access_t *)p_this;
252     access_sys_t *p_sys = p_access->p_sys;
253
254     free( p_sys->p_sectors );
255     ioctl_Close( p_this, p_sys->vcddev );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Block: read data (CDDA_DATA_ONCE)
261  *****************************************************************************/
262 static block_t *Block( access_t *p_access )
263 {
264     access_sys_t *p_sys = p_access->p_sys;
265     int i_blocks = CDDA_BLOCKS_ONCE;
266     block_t *p_block;
267
268     if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
269
270     /* Check end of file */
271     if( p_access->info.b_eof ) return NULL;
272
273     if( !p_sys->b_header )
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;
280     }
281
282     if( p_sys->i_sector >= p_sys->i_last_sector )
283     {
284         p_access->info.b_eof = true;
285         return NULL;
286     }
287
288     /* Don't read too far */
289     if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
290         i_blocks = p_sys->i_last_sector - p_sys->i_sector;
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;
298     }
299
300     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
301             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
302     {
303         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
304         block_Release( p_block );
305
306         /* Try to skip one sector (in case of bad sectors) */
307         p_sys->i_sector++;
308         p_access->info.i_pos += CDDA_DATA_SIZE;
309         return NULL;
310     }
311
312     /* Update a few values */
313     p_sys->i_sector += i_blocks;
314     p_access->info.i_pos += p_block->i_buffer;
315
316     return p_block;
317 }
318
319 /****************************************************************************
320  * Seek
321  ****************************************************************************/
322 static int Seek( access_t *p_access, uint64_t i_pos )
323 {
324     access_sys_t *p_sys = p_access->p_sys;
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;
330
331     return VLC_SUCCESS;
332 }
333
334 /*****************************************************************************
335  * Control:
336  *****************************************************************************/
337 static int Control( access_t *p_access, int i_query, va_list args )
338 {
339     switch( i_query )
340     {
341         case ACCESS_CAN_SEEK:
342         case ACCESS_CAN_FASTSEEK:
343         case ACCESS_CAN_PAUSE:
344         case ACCESS_CAN_CONTROL_PACE:
345             *va_arg( args, bool* ) = true;
346             break;
347         case ACCESS_GET_SIZE:
348             *va_arg( args, uint64_t * ) = p_access->p_sys->size;
349             break;
350         case ACCESS_GET_PTS_DELAY:
351             *va_arg( args, int64_t * ) =
352                 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
353             break;
354
355         case ACCESS_SET_PAUSE_STATE:
356             break;
357
358         default:
359             return VLC_EGENERIC;
360     }
361     return VLC_SUCCESS;
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     const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
369                                              p_sys->vcddev, &p_sys->p_sectors );
370     if( i_titles <= 0 )
371     {
372         if( i_titles < 0 )
373             msg_Err( p_access, "unable to count tracks" );
374         else if( i_titles <= 0 )
375             msg_Err( p_access, "no audio tracks found" );
376         return VLC_EGENERIC;;
377     }
378
379     /* */
380     input_item_SetName( p_current, "Audio CD" );
381
382     const char *psz_album = NULL;
383     const char *psz_year = NULL;
384     const char *psz_genre = NULL;
385     const char *psz_artist = NULL;
386     const char *psz_description = NULL;
387
388 /* Return true if the given string is not NULL and not empty */
389 #define NONEMPTY( psz ) ( (psz) && *(psz) )
390 /* If the given string is NULL or empty, fill it by the return value of 'code' */
391 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
392
393     /* Retreive CDDB information */
394 #ifdef HAVE_LIBCDDB
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         if( i_year > 0 )
407         {
408             psz_year = psz_year_buffer;
409             snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
410         }
411
412         /* Set artist only if unique */
413         for( int i = 0; i < i_titles; i++ )
414         {
415             cddb_track_t *t = cddb_disc_get_track( p_disc, i );
416             if( !t )
417                 continue;
418             const char *psz_track_artist = cddb_track_get_artist( t );
419             if( psz_artist && psz_track_artist &&
420                 strcmp( psz_artist, psz_track_artist ) )
421             {
422                 psz_artist = NULL;
423                 break;
424             }
425             psz_artist = psz_track_artist;
426         }
427     }
428     else
429         msg_Dbg( p_access, "GetCDDBInfo failed" );
430 #endif
431
432     /* CD-Text */
433     vlc_meta_t **pp_cd_text;
434     int        i_cd_text;
435
436     if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
437     {
438         msg_Dbg( p_access, "CD-TEXT information missing" );
439         i_cd_text = 0;
440         pp_cd_text = NULL;
441     }
442
443     /* Retrieve CD-TEXT information but prefer CDDB */
444     if( i_cd_text > 0 && pp_cd_text[0] )
445     {
446         const vlc_meta_t *p_disc = pp_cd_text[0];
447         ON_EMPTY( psz_album,       vlc_meta_Get( p_disc, vlc_meta_Album ) );
448         ON_EMPTY( psz_genre,       vlc_meta_Get( p_disc, vlc_meta_Genre ) );
449         ON_EMPTY( psz_artist,      vlc_meta_Get( p_disc, vlc_meta_Artist ) );
450         ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
451     }
452
453     if( NONEMPTY( psz_album ) )
454     {
455         input_item_SetName( p_current, psz_album );
456         input_item_SetAlbum( p_current, psz_album );
457     }
458
459     if( NONEMPTY( psz_genre ) )
460         input_item_SetGenre( p_current, psz_genre );
461
462     if( NONEMPTY( psz_artist ) )
463         input_item_SetArtist( p_current, psz_artist );
464
465     if( NONEMPTY( psz_year ) )
466         input_item_SetDate( p_current, psz_year );
467
468     if( NONEMPTY( psz_description ) )
469         input_item_SetDescription( p_current, psz_description );
470
471     const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
472                                CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
473     input_item_SetDuration( p_current, i_duration );
474
475     input_item_node_t *p_root = input_item_node_Create( p_current );
476
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 */
492         const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
493                                    CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
494
495         input_item_t *p_item = input_item_NewWithType( psz_uri, psz_name, 0,
496                                          NULL, 0, i_duration, ITEM_TYPE_DISC );
497         if( likely(psz_name != psz_uri) )
498             free( psz_name );
499         free( psz_uri );
500
501         if( unlikely(p_item == NULL) )
502             continue;
503
504         input_item_CopyOptions( p_current, p_item );
505
506         if( likely(asprintf( &psz_opt, "cdda-track=%i", i+1 ) != -1) )
507         {
508             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
509             free( psz_opt );
510         }
511         if( likely(asprintf( &psz_opt, "cdda-first-sector=%i",
512                              p_sys->p_sectors[i] ) != -1) )
513         {
514             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
515             free( psz_opt );
516         }
517         if( likely(asprintf( &psz_opt, "cdda-last-sector=%i",
518                              p_sys->p_sectors[i+1] ) != -1) )
519         {
520             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
521             free( psz_opt );
522         }
523
524         const char *psz_track_title = NULL;
525         const char *psz_track_artist = NULL;
526         const char *psz_track_genre = NULL;
527         const char *psz_track_description = NULL;
528
529 #ifdef HAVE_LIBCDDB
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                 psz_track_title = cddb_track_get_title( t );
537                 psz_track_artist = cddb_track_get_artist( t );
538             }
539         }
540 #endif
541
542         /* Retreive CD-TEXT information but prefer CDDB */
543         if( i+1 < i_cd_text && pp_cd_text[i+1] )
544         {
545             const vlc_meta_t *t = pp_cd_text[i+1];
546
547             ON_EMPTY( psz_track_title,       vlc_meta_Get( t, vlc_meta_Title ) );
548             ON_EMPTY( psz_track_artist,      vlc_meta_Get( t, vlc_meta_Artist ) );
549             ON_EMPTY( psz_track_genre,       vlc_meta_Get( t, vlc_meta_Genre ) );
550             ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
551         }
552
553         /* */
554         ON_EMPTY( psz_track_artist,       psz_artist );
555         ON_EMPTY( psz_track_genre,        psz_genre );
556         ON_EMPTY( psz_track_description,  psz_description );
557
558         /* */
559         if( NONEMPTY( psz_track_title ) )
560         {
561             input_item_SetName( p_item, psz_track_title );
562             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         char psz_num[3+1];
581         snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
582         input_item_SetTrackNum( p_item, psz_num );
583
584         input_item_node_AppendItem( p_root, p_item );
585         vlc_gc_decref( p_item );
586     }
587 #undef ON_EMPTY
588 #undef NONEMPTY
589
590     input_item_node_PostAndDelete( p_root );
591
592     /* */
593     for( int i = 0; i < i_cd_text; i++ )
594     {
595         vlc_meta_t *p_meta = pp_cd_text[i];
596         if( !p_meta )
597             continue;
598         vlc_meta_Delete( p_meta );
599     }
600     free( pp_cd_text );
601
602 #ifdef HAVE_LIBCDDB
603     if( p_disc )
604         cddb_disc_destroy( p_disc );
605 #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     if( var_InheritInteger( p_access, "album-art" ) != ALBUM_ART_ALL &&
613         !  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     if( !p_cddb )
622     {
623         msg_Warn( p_access, "unable to use CDDB" );
624         return NULL;
625     }
626
627     /* */
628
629     cddb_http_enable( p_cddb );
630
631     char *psz_tmp = var_InheritString( p_access, "cddb-server" );
632     if( psz_tmp )
633     {
634         cddb_set_server_name( p_cddb, psz_tmp );
635         free( psz_tmp );
636     }
637
638     cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
639
640     cddb_set_email_address( p_cddb, "vlc@videolan.org" );
641
642     cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
643     cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
644
645
646     char *psz_cachedir;
647     char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
648
649     if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
650         cddb_cache_enable( p_cddb );
651         cddb_cache_set_dir( p_cddb, psz_cachedir );
652         free( psz_cachedir );
653     }
654     free( psz_temp );
655
656     cddb_set_timeout( p_cddb, 10 );
657
658     /* */
659     cddb_disc_t *p_disc = cddb_disc_new();
660     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         cddb_track_set_frame_offset( t, p_sectors[i] + 150 );  /* Pregap offset */
671
672         cddb_disc_add_track( p_disc, t );
673         const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
674                                (int64_t)CDDA_DATA_SIZE;
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", (int)(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         goto error;
694     }
695     else if( i_matches == 0 )
696     {
697         msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
698         goto error;
699     }
700     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 error:
709     if( p_disc )
710         cddb_disc_destroy( p_disc );
711     cddb_destroy( p_cddb );
712     return NULL;
713 }
714 #endif /*HAVE_LIBCDDB*/
715