]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
v4l2: missing white space
[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
100     /* Current position */
101     int         i_sector;                                  /* Current Sector */
102     int        *p_sectors;                                  /* Track sectors */
103
104     /* Wave header for the output data */
105     WAVEHEADER  waveheader;
106     bool        b_header;
107
108     int         i_track;
109     int         i_first_sector;
110     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 #endif
122
123 /*****************************************************************************
124  * Open: open cdda
125  *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
127 {
128     access_t     *p_access = (access_t*)p_this;
129     access_sys_t *p_sys;
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     /* Open CDDA */
151     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name ) ) == NULL )
152     {
153         msg_Warn( p_access, "could not open %s", psz_name );
154         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     /* Do we play a single track ? */
164     p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
165
166     if( p_sys->i_track < 0 )
167     {
168         /* We only do separate items if the whole disc is requested */
169         input_thread_t *p_input = access_GetParentInput( p_access );
170
171         int i_ret = -1;
172         if( p_input )
173         {
174             input_item_t *p_current = input_GetItem( p_input );
175             if( p_current )
176                 i_ret = GetTracks( p_access, p_current );
177
178             vlc_object_release( p_input );
179         }
180         if( i_ret < 0 )
181             goto error;
182     }
183     else
184     {
185         /* Build a WAV header for the output data */
186         memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
187         SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
188         SetWLE( &p_sys->waveheader.BitsPerSample, 16);
189         p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
190         p_sys->waveheader.Length = 0;               /* we just don't know */
191         p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
192         p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
193         SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
194         SetWLE( &p_sys->waveheader.Modus, 2);
195         SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
196         SetWLE( &p_sys->waveheader.BytesPerSample,
197                     2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
198         SetDWLE( &p_sys->waveheader.BytesPerSec,
199                     2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
200         p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
201         p_sys->waveheader.DataLength = 0;           /* we just don't know */
202
203         p_sys->i_first_sector = var_InheritInteger( p_access,
204                                                     "cdda-first-sector" );
205         p_sys->i_last_sector  = var_InheritInteger( p_access,
206                                                     "cdda-last-sector" );
207         /* Tracknumber in MRL */
208         if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
209         {
210             const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
211                                                      p_sys->vcddev, &p_sys->p_sectors );
212             if( p_sys->i_track >= i_titles )
213             {
214                 msg_Err( p_access, "invalid track number" );
215                 goto error;
216             }
217             p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
218             p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
219         }
220
221         p_sys->i_sector = p_sys->i_first_sector;
222         p_access->info.i_size = (p_sys->i_last_sector - p_sys->i_first_sector)
223                                      * (int64_t)CDDA_DATA_SIZE;
224     }
225
226     return VLC_SUCCESS;
227
228 error:
229     free( p_sys->p_sectors );
230     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
231     free( p_sys );
232     return VLC_EGENERIC;
233 }
234
235 /*****************************************************************************
236  * Close: closes cdda
237  *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
239 {
240     access_t     *p_access = (access_t *)p_this;
241     access_sys_t *p_sys = p_access->p_sys;
242
243     free( p_sys->p_sectors );
244     ioctl_Close( p_this, p_sys->vcddev );
245     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     int i_blocks = CDDA_BLOCKS_ONCE;
255     block_t *p_block;
256
257     if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
258
259     /* Check end of file */
260     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     /* Don't read too far */
278     if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
279         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     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
290             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         /* Try to skip one sector (in case of bad sectors) */
296         p_sys->i_sector++;
297         p_access->info.i_pos += CDDA_DATA_SIZE;
298         return NULL;
299     }
300
301     /* Update a few values */
302     p_sys->i_sector += i_blocks;
303     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     return VLC_SUCCESS;
321 }
322
323 /*****************************************************************************
324  * Control:
325  *****************************************************************************/
326 static int Control( access_t *p_access, int i_query, va_list args )
327 {
328     switch( i_query )
329     {
330         case ACCESS_CAN_SEEK:
331         case ACCESS_CAN_FASTSEEK:
332         case ACCESS_CAN_PAUSE:
333         case ACCESS_CAN_CONTROL_PACE:
334             *va_arg( args, bool* ) = true;
335             break;
336
337         case ACCESS_GET_PTS_DELAY:
338             *va_arg( args, int64_t * ) =
339                 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
340             break;
341
342         case ACCESS_SET_PAUSE_STATE:
343             break;
344
345         case ACCESS_GET_TITLE_INFO:
346         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             return VLC_EGENERIC;
356     }
357     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     const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
365                                              p_sys->vcddev, &p_sys->p_sectors );
366     if( i_titles <= 0 )
367     {
368         if( i_titles < 0 )
369             msg_Err( p_access, "unable to count tracks" );
370         else if( i_titles <= 0 )
371             msg_Err( p_access, "no audio tracks found" );
372         return VLC_EGENERIC;;
373     }
374
375     /* */
376     input_item_SetName( p_current, "Audio CD" );
377
378     const char *psz_album = NULL;
379     const char *psz_year = NULL;
380     const char *psz_genre = NULL;
381     const char *psz_artist = NULL;
382     const char *psz_description = NULL;
383
384 /* Return true if the given string is not NULL and not empty */
385 #define NONEMPTY( psz ) ( (psz) && *(psz) )
386 /* If the given string is NULL or empty, fill it by the return value of 'code' */
387 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
388
389     /* Retreive CDDB information */
390 #ifdef HAVE_LIBCDDB
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         if( i_year > 0 )
402         {
403             psz_year = psz_year_buffer;
404             snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
405         }
406
407         /* Set artist only if unique */
408         for( int i = 0; i < i_titles; i++ )
409         {
410             cddb_track_t *t = cddb_disc_get_track( p_disc, i );
411             if( !t )
412                 continue;
413             const char *psz_track_artist = cddb_track_get_artist( t );
414             if( psz_artist && psz_track_artist &&
415                 strcmp( psz_artist, psz_track_artist ) )
416             {
417                 psz_artist = NULL;
418                 break;
419             }
420             psz_artist = psz_track_artist;
421         }
422     }
423 #endif
424
425     /* CD-Text */
426     vlc_meta_t **pp_cd_text;
427     int        i_cd_text;
428
429     if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
430     {
431         msg_Dbg( p_access, "CD-TEXT information missing" );
432         i_cd_text = 0;
433         pp_cd_text = NULL;
434     }
435
436     /* Retrieve CD-TEXT information but prefer CDDB */
437     if( i_cd_text > 0 && pp_cd_text[0] )
438     {
439         const vlc_meta_t *p_disc = pp_cd_text[0];
440         ON_EMPTY( psz_album,       vlc_meta_Get( p_disc, vlc_meta_Album ) );
441         ON_EMPTY( psz_genre,       vlc_meta_Get( p_disc, vlc_meta_Genre ) );
442         ON_EMPTY( psz_artist,      vlc_meta_Get( p_disc, vlc_meta_Artist ) );
443         ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
444     }
445
446     if( NONEMPTY( psz_album ) )
447     {
448         input_item_SetName( p_current, psz_album );
449         input_item_SetAlbum( p_current, psz_album );
450     }
451
452     if( NONEMPTY( psz_genre ) )
453         input_item_SetGenre( p_current, psz_genre );
454
455     if( NONEMPTY( psz_artist ) )
456         input_item_SetArtist( p_current, psz_artist );
457
458     if( NONEMPTY( psz_year ) )
459         input_item_SetDate( p_current, psz_year );
460
461     if( NONEMPTY( psz_description ) )
462         input_item_SetDescription( p_current, psz_description );
463
464     const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
465                                CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
466     input_item_SetDuration( p_current, i_duration );
467
468     input_item_node_t *p_root = input_item_node_Create( p_current );
469
470     /* Build title table */
471     for( int i = 0; i < i_titles; i++ )
472     {
473         input_item_t *p_input_item;
474
475         char *psz_uri, *psz_opt, *psz_first, *psz_last;
476         char *psz_name;
477
478         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             psz_uri = NULL;
483         if( asprintf( &psz_opt, "cdda-track=%i", i+1 ) == -1 )
484             psz_opt = NULL;
485         if( asprintf( &psz_first, "cdda-first-sector=%i",p_sys->p_sectors[i] ) == -1 )
486             psz_first = NULL;
487         if( asprintf( &psz_last, "cdda-last-sector=%i", p_sys->p_sectors[i+1] ) == -1 )
488             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         const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
496                                    CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
497         p_input_item = input_item_NewWithType( psz_uri, psz_name, 0, NULL, 0,
498                                                i_duration, ITEM_TYPE_DISC );
499         input_item_CopyOptions( p_current, p_input_item );
500         input_item_AddOption( p_input_item, psz_first, VLC_INPUT_OPTION_TRUSTED );
501         input_item_AddOption( p_input_item, psz_last, VLC_INPUT_OPTION_TRUSTED );
502         input_item_AddOption( p_input_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
503
504         const char *psz_track_title = NULL;
505         const char *psz_track_artist = NULL;
506         const char *psz_track_genre = NULL;
507         const char *psz_track_description = NULL;
508
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                 psz_track_title = cddb_track_get_title( t );
517                 psz_track_artist = cddb_track_get_artist( t );
518             }
519         }
520 #endif
521
522         /* Retreive CD-TEXT information but prefer CDDB */
523         if( i+1 < i_cd_text && pp_cd_text[i+1] )
524         {
525             const vlc_meta_t *t = pp_cd_text[i+1];
526
527             ON_EMPTY( psz_track_title,       vlc_meta_Get( t, vlc_meta_Title ) );
528             ON_EMPTY( psz_track_artist,      vlc_meta_Get( t, vlc_meta_Artist ) );
529             ON_EMPTY( psz_track_genre,       vlc_meta_Get( t, vlc_meta_Genre ) );
530             ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
531         }
532
533         /* */
534         ON_EMPTY( psz_track_artist,       psz_artist );
535         ON_EMPTY( psz_track_genre,        psz_genre );
536         ON_EMPTY( psz_track_description,  psz_description );
537
538         /* */
539         if( NONEMPTY( psz_track_title ) )
540         {
541             input_item_SetName( p_input_item, psz_track_title );
542             input_item_SetTitle( p_input_item, psz_track_title );
543         }
544
545         if( NONEMPTY( psz_track_artist ) )
546             input_item_SetArtist( p_input_item, psz_track_artist );
547
548         if( NONEMPTY( psz_track_genre ) )
549             input_item_SetGenre( p_input_item, psz_track_genre );
550
551         if( NONEMPTY( psz_track_description ) )
552             input_item_SetDescription( p_input_item, psz_track_description );
553
554         if( NONEMPTY( psz_album ) )
555             input_item_SetAlbum( p_input_item, psz_album );
556
557         if( NONEMPTY( psz_year ) )
558             input_item_SetDate( p_input_item, psz_year );
559
560         char psz_num[3+1];
561         snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
562         input_item_SetTrackNum( p_input_item, psz_num );
563
564         input_item_node_AppendItem( p_root, p_input_item );
565         vlc_gc_decref( p_input_item );
566         free( psz_uri ); free( psz_opt ); free( psz_name );
567         free( psz_first ); free( psz_last );
568     }
569 #undef ON_EMPTY
570 #undef NONEMPTY
571
572     input_item_node_PostAndDelete( p_root );
573
574     /* */
575     for( int i = 0; i < i_cd_text; i++ )
576     {
577         vlc_meta_t *p_meta = pp_cd_text[i];
578         if( !p_meta )
579             continue;
580         vlc_meta_Delete( p_meta );
581     }
582     free( pp_cd_text );
583
584 #ifdef HAVE_LIBCDDB
585     if( p_disc )
586         cddb_disc_destroy( p_disc );
587 #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         return NULL;
596
597     /* */
598     cddb_conn_t *p_cddb = cddb_new();
599     if( !p_cddb )
600     {
601         msg_Warn( p_access, "unable to use CDDB" );
602         return NULL;
603     }
604
605     /* */
606
607     cddb_http_enable( p_cddb );
608
609     char *psz_tmp = var_InheritString( p_access, "cddb-server" );
610     if( psz_tmp )
611     {
612         cddb_set_server_name( p_cddb, psz_tmp );
613         free( psz_tmp );
614     }
615
616     cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
617
618     cddb_set_email_address( p_cddb, "vlc@videolan.org" );
619
620     cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
621     cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
622
623
624     char *psz_cachedir;
625     char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
626
627     if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
628         cddb_cache_enable( p_cddb );
629         cddb_cache_set_dir( p_cddb, psz_cachedir );
630         free( psz_cachedir );
631     }
632     free( psz_temp );
633
634     cddb_set_timeout( p_cddb, 10 );
635
636     /* */
637     cddb_disc_t *p_disc = cddb_disc_new();
638     if( !p_disc )
639     {
640         msg_Err( p_access, "unable to create CDDB disc structure." );
641         goto error;
642     }
643
644     int64_t i_length = 0;
645     for( int i = 0; i < i_titles; i++ )
646     {
647         cddb_track_t *t = cddb_track_new();
648         cddb_track_set_frame_offset( t, p_sectors[i] );
649         cddb_disc_add_track( p_disc, t );
650         const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
651                                (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         goto error;
668     }
669     else if( i_matches == 0 )
670     {
671         msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
672         goto error;
673     }
674     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 error:
683     if( p_disc )
684         cddb_disc_destroy( p_disc );
685     cddb_destroy( p_cddb );
686     return NULL;
687 }
688 #endif /*HAVE_LIBCDDB*/
689