]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
Wayland/shell: implement basic window size handling
[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     access_sys_t *p_sys;
131     vcddev_t     *vcddev;
132     char         *psz_name;
133
134     if( !p_access->psz_filepath || !*p_access->psz_filepath )
135     {
136         /* Only when selected */
137         if( !p_access->psz_access || !*p_access->psz_access )
138             return VLC_EGENERIC;
139
140         psz_name = var_InheritString( p_this, "cd-audio" );
141         if( !psz_name )
142             return VLC_EGENERIC;
143     }
144     else psz_name = ToLocaleDup( p_access->psz_filepath );
145
146 #if defined( _WIN32 ) || defined( __OS2__ )
147     if( psz_name[0] && psz_name[1] == ':' &&
148         psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
149 #endif
150     /* Set up p_access */
151     STANDARD_BLOCK_ACCESS_INIT
152
153     /* Open CDDA */
154     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name ) ) == NULL )
155     {
156         msg_Warn( p_access, "could not open %s", psz_name );
157         free( psz_name );
158         free( p_sys );
159         return VLC_EGENERIC;
160     }
161     free( psz_name );
162     p_sys->vcddev = vcddev;
163
164     /* Do we play a single track ? */
165     p_sys->i_track = var_InheritInteger( p_access, "cdda-track" ) - 1;
166
167     if( p_sys->i_track < 0 )
168     {
169         /* We only do separate items if the whole disc is requested */
170         input_thread_t *p_input = access_GetParentInput( p_access );
171
172         int i_ret = -1;
173         if( p_input )
174         {
175             input_item_t *p_current = input_GetItem( p_input );
176             if( p_current )
177                 i_ret = GetTracks( p_access, p_current );
178
179             vlc_object_release( p_input );
180         }
181         if( i_ret < 0 )
182             goto error;
183     }
184     else
185     {
186         /* Build a WAV header for the output data */
187         memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
188         SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
189         SetWLE( &p_sys->waveheader.BitsPerSample, 16);
190         p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
191         p_sys->waveheader.Length = 0;               /* we just don't know */
192         p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
193         p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
194         SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
195         SetWLE( &p_sys->waveheader.Modus, 2);
196         SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
197         SetWLE( &p_sys->waveheader.BytesPerSample,
198                     2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
199         SetDWLE( &p_sys->waveheader.BytesPerSec,
200                     2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
201         p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
202         p_sys->waveheader.DataLength = 0;           /* we just don't know */
203
204         p_sys->i_first_sector = var_InheritInteger( p_access,
205                                                     "cdda-first-sector" );
206         p_sys->i_last_sector  = var_InheritInteger( p_access,
207                                                     "cdda-last-sector" );
208         /* Tracknumber in MRL */
209         if( p_sys->i_first_sector < 0 || p_sys->i_last_sector < 0 )
210         {
211             const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
212                                                      p_sys->vcddev, &p_sys->p_sectors );
213             if( p_sys->i_track >= i_titles )
214             {
215                 msg_Err( p_access, "invalid track number" );
216                 goto error;
217             }
218             p_sys->i_first_sector = p_sys->p_sectors[p_sys->i_track];
219             p_sys->i_last_sector = p_sys->p_sectors[p_sys->i_track+1];
220         }
221
222         p_sys->i_sector = p_sys->i_first_sector;
223         p_sys->size = (p_sys->i_last_sector - p_sys->i_first_sector)
224                                      * (int64_t)CDDA_DATA_SIZE;
225     }
226
227     return VLC_SUCCESS;
228
229 error:
230     free( p_sys->p_sectors );
231     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
232     free( p_sys );
233     return VLC_EGENERIC;
234 }
235
236 /*****************************************************************************
237  * Close: closes cdda
238  *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
240 {
241     access_t     *p_access = (access_t *)p_this;
242     access_sys_t *p_sys = p_access->p_sys;
243
244     free( p_sys->p_sectors );
245     ioctl_Close( p_this, p_sys->vcddev );
246     free( p_sys );
247 }
248
249 /*****************************************************************************
250  * Block: read data (CDDA_DATA_ONCE)
251  *****************************************************************************/
252 static block_t *Block( access_t *p_access )
253 {
254     access_sys_t *p_sys = p_access->p_sys;
255     int i_blocks = CDDA_BLOCKS_ONCE;
256     block_t *p_block;
257
258     if( p_sys->i_track < 0 ) p_access->info.b_eof = true;
259
260     /* Check end of file */
261     if( p_access->info.b_eof ) return NULL;
262
263     if( !p_sys->b_header )
264     {
265         /* Return only the header */
266         p_block = block_Alloc( sizeof( WAVEHEADER ) );
267         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
268         p_sys->b_header = true;
269         return p_block;
270     }
271
272     if( p_sys->i_sector >= p_sys->i_last_sector )
273     {
274         p_access->info.b_eof = true;
275         return NULL;
276     }
277
278     /* Don't read too far */
279     if( p_sys->i_sector + i_blocks >= p_sys->i_last_sector )
280         i_blocks = p_sys->i_last_sector - p_sys->i_sector;
281
282     /* Do the actual reading */
283     if( !( p_block = block_Alloc( i_blocks * CDDA_DATA_SIZE ) ) )
284     {
285         msg_Err( p_access, "cannot get a new block of size: %i",
286                  i_blocks * CDDA_DATA_SIZE );
287         return NULL;
288     }
289
290     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
291             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
292     {
293         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
294         block_Release( p_block );
295
296         /* Try to skip one sector (in case of bad sectors) */
297         p_sys->i_sector++;
298         p_access->info.i_pos += CDDA_DATA_SIZE;
299         return NULL;
300     }
301
302     /* Update a few values */
303     p_sys->i_sector += i_blocks;
304     p_access->info.i_pos += p_block->i_buffer;
305
306     return p_block;
307 }
308
309 /****************************************************************************
310  * Seek
311  ****************************************************************************/
312 static int Seek( access_t *p_access, uint64_t i_pos )
313 {
314     access_sys_t *p_sys = p_access->p_sys;
315
316     /* Next sector to read */
317     p_sys->i_sector = p_sys->i_first_sector + i_pos / CDDA_DATA_SIZE;
318     assert( p_sys->i_sector >= 0 );
319     p_access->info.i_pos = i_pos;
320
321     return VLC_SUCCESS;
322 }
323
324 /*****************************************************************************
325  * Control:
326  *****************************************************************************/
327 static int Control( access_t *p_access, int i_query, va_list args )
328 {
329     switch( i_query )
330     {
331         case ACCESS_CAN_SEEK:
332         case ACCESS_CAN_FASTSEEK:
333         case ACCESS_CAN_PAUSE:
334         case ACCESS_CAN_CONTROL_PACE:
335             *va_arg( args, bool* ) = true;
336             break;
337         case ACCESS_GET_SIZE:
338             *va_arg( args, uint64_t * ) = p_access->p_sys->size;
339             break;
340         case ACCESS_GET_PTS_DELAY:
341             *va_arg( args, int64_t * ) =
342                 INT64_C(1000) * var_InheritInteger( p_access, "disc-caching" );
343             break;
344
345         case ACCESS_SET_PAUSE_STATE:
346             break;
347
348         default:
349             return VLC_EGENERIC;
350     }
351     return VLC_SUCCESS;
352 }
353
354 static int GetTracks( access_t *p_access, input_item_t *p_current )
355 {
356     access_sys_t *p_sys = p_access->p_sys;
357
358     const int i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
359                                              p_sys->vcddev, &p_sys->p_sectors );
360     if( i_titles <= 0 )
361     {
362         if( i_titles < 0 )
363             msg_Err( p_access, "unable to count tracks" );
364         else if( i_titles <= 0 )
365             msg_Err( p_access, "no audio tracks found" );
366         return VLC_EGENERIC;;
367     }
368
369     /* */
370     input_item_SetName( p_current, "Audio CD" );
371
372     const char *psz_album = NULL;
373     const char *psz_year = NULL;
374     const char *psz_genre = NULL;
375     const char *psz_artist = NULL;
376     const char *psz_description = NULL;
377
378 /* Return true if the given string is not NULL and not empty */
379 #define NONEMPTY( psz ) ( (psz) && *(psz) )
380 /* If the given string is NULL or empty, fill it by the return value of 'code' */
381 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
382
383     /* Retreive CDDB information */
384 #ifdef HAVE_LIBCDDB
385     char psz_year_buffer[4+1];
386     msg_Dbg( p_access, "fetching infos with CDDB" );
387     cddb_disc_t *p_disc = GetCDDBInfo( p_access, i_titles, p_sys->p_sectors );
388     if( p_disc )
389     {
390         psz_album = cddb_disc_get_title( p_disc );
391         psz_genre = cddb_disc_get_genre( p_disc );
392
393         /* */
394         const unsigned i_year = cddb_disc_get_year( p_disc );
395         if( i_year > 0 )
396         {
397             psz_year = psz_year_buffer;
398             snprintf( psz_year_buffer, sizeof(psz_year_buffer), "%u", i_year );
399         }
400
401         /* Set artist only if unique */
402         for( int i = 0; i < i_titles; i++ )
403         {
404             cddb_track_t *t = cddb_disc_get_track( p_disc, i );
405             if( !t )
406                 continue;
407             const char *psz_track_artist = cddb_track_get_artist( t );
408             if( psz_artist && psz_track_artist &&
409                 strcmp( psz_artist, psz_track_artist ) )
410             {
411                 psz_artist = NULL;
412                 break;
413             }
414             psz_artist = psz_track_artist;
415         }
416     }
417     else
418         msg_Dbg( p_access, "GetCDDBInfo failed" );
419 #endif
420
421     /* CD-Text */
422     vlc_meta_t **pp_cd_text;
423     int        i_cd_text;
424
425     if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
426     {
427         msg_Dbg( p_access, "CD-TEXT information missing" );
428         i_cd_text = 0;
429         pp_cd_text = NULL;
430     }
431
432     /* Retrieve CD-TEXT information but prefer CDDB */
433     if( i_cd_text > 0 && pp_cd_text[0] )
434     {
435         const vlc_meta_t *p_disc = pp_cd_text[0];
436         ON_EMPTY( psz_album,       vlc_meta_Get( p_disc, vlc_meta_Album ) );
437         ON_EMPTY( psz_genre,       vlc_meta_Get( p_disc, vlc_meta_Genre ) );
438         ON_EMPTY( psz_artist,      vlc_meta_Get( p_disc, vlc_meta_Artist ) );
439         ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
440     }
441
442     if( NONEMPTY( psz_album ) )
443     {
444         input_item_SetName( p_current, psz_album );
445         input_item_SetAlbum( p_current, psz_album );
446     }
447
448     if( NONEMPTY( psz_genre ) )
449         input_item_SetGenre( p_current, psz_genre );
450
451     if( NONEMPTY( psz_artist ) )
452         input_item_SetArtist( p_current, psz_artist );
453
454     if( NONEMPTY( psz_year ) )
455         input_item_SetDate( p_current, psz_year );
456
457     if( NONEMPTY( psz_description ) )
458         input_item_SetDescription( p_current, psz_description );
459
460     const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
461                                CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
462     input_item_SetDuration( p_current, i_duration );
463
464     input_item_node_t *p_root = input_item_node_Create( p_current );
465
466     /* Build title table */
467     for( int i = 0; i < i_titles; i++ )
468     {
469         char *psz_uri, *psz_opt, *psz_name;
470
471         msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
472
473         if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
474             continue;
475
476         /* Define a "default name" */
477         if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
478             psz_name = psz_uri;
479
480         /* Create playlist items */
481         const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
482                                    CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
483
484         input_item_t *p_item = input_item_NewWithType( psz_uri, psz_name, 0,
485                                          NULL, 0, i_duration, ITEM_TYPE_DISC );
486         if( likely(psz_name != psz_uri) )
487             free( psz_name );
488         free( psz_uri );
489
490         if( unlikely(p_item == NULL) )
491             continue;
492
493         input_item_CopyOptions( p_current, p_item );
494
495         if( likely(asprintf( &psz_opt, "cdda-track=%i", i+1 ) != -1) )
496         {
497             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
498             free( psz_opt );
499         }
500         if( likely(asprintf( &psz_opt, "cdda-first-sector=%i",
501                              p_sys->p_sectors[i] ) != -1) )
502         {
503             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
504             free( psz_opt );
505         }
506         if( likely(asprintf( &psz_opt, "cdda-last-sector=%i",
507                              p_sys->p_sectors[i+1] ) != -1) )
508         {
509             input_item_AddOption( p_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
510             free( psz_opt );
511         }
512
513         const char *psz_track_title = NULL;
514         const char *psz_track_artist = NULL;
515         const char *psz_track_genre = NULL;
516         const char *psz_track_description = NULL;
517
518 #ifdef HAVE_LIBCDDB
519         /* Retreive CDDB information */
520         if( p_disc )
521         {
522             cddb_track_t *t = cddb_disc_get_track( p_disc, i );
523             if( t != NULL )
524             {
525                 psz_track_title = cddb_track_get_title( t );
526                 psz_track_artist = cddb_track_get_artist( t );
527             }
528         }
529 #endif
530
531         /* Retreive CD-TEXT information but prefer CDDB */
532         if( i+1 < i_cd_text && pp_cd_text[i+1] )
533         {
534             const vlc_meta_t *t = pp_cd_text[i+1];
535
536             ON_EMPTY( psz_track_title,       vlc_meta_Get( t, vlc_meta_Title ) );
537             ON_EMPTY( psz_track_artist,      vlc_meta_Get( t, vlc_meta_Artist ) );
538             ON_EMPTY( psz_track_genre,       vlc_meta_Get( t, vlc_meta_Genre ) );
539             ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
540         }
541
542         /* */
543         ON_EMPTY( psz_track_artist,       psz_artist );
544         ON_EMPTY( psz_track_genre,        psz_genre );
545         ON_EMPTY( psz_track_description,  psz_description );
546
547         /* */
548         if( NONEMPTY( psz_track_title ) )
549         {
550             input_item_SetName( p_item, psz_track_title );
551             input_item_SetTitle( p_item, psz_track_title );
552         }
553
554         if( NONEMPTY( psz_track_artist ) )
555             input_item_SetArtist( p_item, psz_track_artist );
556
557         if( NONEMPTY( psz_track_genre ) )
558             input_item_SetGenre( p_item, psz_track_genre );
559
560         if( NONEMPTY( psz_track_description ) )
561             input_item_SetDescription( p_item, psz_track_description );
562
563         if( NONEMPTY( psz_album ) )
564             input_item_SetAlbum( p_item, psz_album );
565
566         if( NONEMPTY( psz_year ) )
567             input_item_SetDate( p_item, psz_year );
568
569         char psz_num[3+1];
570         snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
571         input_item_SetTrackNum( p_item, psz_num );
572
573         input_item_node_AppendItem( p_root, p_item );
574         vlc_gc_decref( p_item );
575     }
576 #undef ON_EMPTY
577 #undef NONEMPTY
578
579     input_item_node_PostAndDelete( p_root );
580
581     /* */
582     for( int i = 0; i < i_cd_text; i++ )
583     {
584         vlc_meta_t *p_meta = pp_cd_text[i];
585         if( !p_meta )
586             continue;
587         vlc_meta_Delete( p_meta );
588     }
589     free( pp_cd_text );
590
591 #ifdef HAVE_LIBCDDB
592     if( p_disc )
593         cddb_disc_destroy( p_disc );
594 #endif
595     return VLC_SUCCESS;
596 }
597
598 #ifdef HAVE_LIBCDDB
599 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
600 {
601     if( var_InheritInteger( p_access, "album-art" ) != ALBUM_ART_ALL &&
602         !  var_InheritBool( p_access, "metadata-network-access" ) )
603     {
604         msg_Dbg( p_access, "Album art policy set to manual; no automatic fetching" );
605         return NULL;
606     }
607
608     /* */
609     cddb_conn_t *p_cddb = cddb_new();
610     if( !p_cddb )
611     {
612         msg_Warn( p_access, "unable to use CDDB" );
613         return NULL;
614     }
615
616     /* */
617
618     cddb_http_enable( p_cddb );
619
620     char *psz_tmp = var_InheritString( p_access, "cddb-server" );
621     if( psz_tmp )
622     {
623         cddb_set_server_name( p_cddb, psz_tmp );
624         free( psz_tmp );
625     }
626
627     cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
628
629     cddb_set_email_address( p_cddb, "vlc@videolan.org" );
630
631     cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
632     cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
633
634
635     char *psz_cachedir;
636     char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
637
638     if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
639         cddb_cache_enable( p_cddb );
640         cddb_cache_set_dir( p_cddb, psz_cachedir );
641         free( psz_cachedir );
642     }
643     free( psz_temp );
644
645     cddb_set_timeout( p_cddb, 10 );
646
647     /* */
648     cddb_disc_t *p_disc = cddb_disc_new();
649     if( !p_disc )
650     {
651         msg_Err( p_access, "unable to create CDDB disc structure." );
652         goto error;
653     }
654
655     int64_t i_length = 0;
656     for( int i = 0; i < i_titles; i++ )
657     {
658         cddb_track_t *t = cddb_track_new();
659         cddb_track_set_frame_offset( t, p_sectors[i] );
660         cddb_disc_add_track( p_disc, t );
661         const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
662                                (int64_t)CDDA_DATA_SIZE;
663         i_length += INT64_C(1000000) * i_size / 44100 / 4  ;
664     }
665
666     cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
667
668     if( !cddb_disc_calc_discid( p_disc ) )
669     {
670         msg_Err( p_access, "CDDB disc ID calculation failed" );
671         goto error;
672     }
673
674     const int i_matches = cddb_query( p_cddb, p_disc );
675     if( i_matches < 0 )
676     {
677         msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
678         goto error;
679     }
680     else if( i_matches == 0 )
681     {
682         msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
683         goto error;
684     }
685     else if( i_matches > 1 )
686         msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
687
688     cddb_read( p_cddb, p_disc );
689
690     cddb_destroy( p_cddb);
691     return p_disc;
692
693 error:
694     if( p_disc )
695         cddb_disc_destroy( p_disc );
696     cddb_destroy( p_cddb );
697     return NULL;
698 }
699 #endif /*HAVE_LIBCDDB*/
700