]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
access: cdda: fix resource leak
[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 #endif
418
419     /* CD-Text */
420     vlc_meta_t **pp_cd_text;
421     int        i_cd_text;
422
423     if( ioctl_GetCdText( VLC_OBJECT(p_access), p_sys->vcddev, &pp_cd_text, &i_cd_text ) )
424     {
425         msg_Dbg( p_access, "CD-TEXT information missing" );
426         i_cd_text = 0;
427         pp_cd_text = NULL;
428     }
429
430     /* Retrieve CD-TEXT information but prefer CDDB */
431     if( i_cd_text > 0 && pp_cd_text[0] )
432     {
433         const vlc_meta_t *p_disc = pp_cd_text[0];
434         ON_EMPTY( psz_album,       vlc_meta_Get( p_disc, vlc_meta_Album ) );
435         ON_EMPTY( psz_genre,       vlc_meta_Get( p_disc, vlc_meta_Genre ) );
436         ON_EMPTY( psz_artist,      vlc_meta_Get( p_disc, vlc_meta_Artist ) );
437         ON_EMPTY( psz_description, vlc_meta_Get( p_disc, vlc_meta_Description ) );
438     }
439
440     if( NONEMPTY( psz_album ) )
441     {
442         input_item_SetName( p_current, psz_album );
443         input_item_SetAlbum( p_current, psz_album );
444     }
445
446     if( NONEMPTY( psz_genre ) )
447         input_item_SetGenre( p_current, psz_genre );
448
449     if( NONEMPTY( psz_artist ) )
450         input_item_SetArtist( p_current, psz_artist );
451
452     if( NONEMPTY( psz_year ) )
453         input_item_SetDate( p_current, psz_year );
454
455     if( NONEMPTY( psz_description ) )
456         input_item_SetDescription( p_current, psz_description );
457
458     const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i_titles] - p_sys->p_sectors[0] ) *
459                                CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
460     input_item_SetDuration( p_current, i_duration );
461
462     input_item_node_t *p_root = input_item_node_Create( p_current );
463
464     /* Build title table */
465     for( int i = 0; i < i_titles; i++ )
466     {
467         input_item_t *p_input_item;
468
469         char *psz_uri, *psz_opt, *psz_first, *psz_last;
470         char *psz_name;
471
472         msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
473
474         /* */
475         if( asprintf( &psz_uri, "cdda://%s", p_access->psz_location ) == -1 )
476             psz_uri = NULL;
477         if( asprintf( &psz_opt, "cdda-track=%i", i+1 ) == -1 )
478             psz_opt = NULL;
479         if( asprintf( &psz_first, "cdda-first-sector=%i",p_sys->p_sectors[i] ) == -1 )
480             psz_first = NULL;
481         if( asprintf( &psz_last, "cdda-last-sector=%i", p_sys->p_sectors[i+1] ) == -1 )
482             psz_last = NULL;
483
484         /* Define a "default name" */
485         if( asprintf( &psz_name, _("Audio CD - Track %02i"), (i+1) ) == -1 )
486             psz_name = NULL;
487
488         /* Create playlist items */
489         const mtime_t i_duration = (int64_t)( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
490                                    CDDA_DATA_SIZE * 1000000 / 44100 / 2 / 2;
491         p_input_item = input_item_NewWithType( psz_uri, psz_name, 0, NULL, 0,
492                                                i_duration, ITEM_TYPE_DISC );
493         input_item_CopyOptions( p_current, p_input_item );
494         input_item_AddOption( p_input_item, psz_first, VLC_INPUT_OPTION_TRUSTED );
495         input_item_AddOption( p_input_item, psz_last, VLC_INPUT_OPTION_TRUSTED );
496         input_item_AddOption( p_input_item, psz_opt, VLC_INPUT_OPTION_TRUSTED );
497
498         const char *psz_track_title = NULL;
499         const char *psz_track_artist = NULL;
500         const char *psz_track_genre = NULL;
501         const char *psz_track_description = NULL;
502
503 #ifdef HAVE_LIBCDDB
504         /* Retreive CDDB information */
505         if( p_disc )
506         {
507             cddb_track_t *t = cddb_disc_get_track( p_disc, i );
508             if( t != NULL )
509             {
510                 psz_track_title = cddb_track_get_title( t );
511                 psz_track_artist = cddb_track_get_artist( t );
512             }
513         }
514 #endif
515
516         /* Retreive CD-TEXT information but prefer CDDB */
517         if( i+1 < i_cd_text && pp_cd_text[i+1] )
518         {
519             const vlc_meta_t *t = pp_cd_text[i+1];
520
521             ON_EMPTY( psz_track_title,       vlc_meta_Get( t, vlc_meta_Title ) );
522             ON_EMPTY( psz_track_artist,      vlc_meta_Get( t, vlc_meta_Artist ) );
523             ON_EMPTY( psz_track_genre,       vlc_meta_Get( t, vlc_meta_Genre ) );
524             ON_EMPTY( psz_track_description, vlc_meta_Get( t, vlc_meta_Description ) );
525         }
526
527         /* */
528         ON_EMPTY( psz_track_artist,       psz_artist );
529         ON_EMPTY( psz_track_genre,        psz_genre );
530         ON_EMPTY( psz_track_description,  psz_description );
531
532         /* */
533         if( NONEMPTY( psz_track_title ) )
534         {
535             input_item_SetName( p_input_item, psz_track_title );
536             input_item_SetTitle( p_input_item, psz_track_title );
537         }
538
539         if( NONEMPTY( psz_track_artist ) )
540             input_item_SetArtist( p_input_item, psz_track_artist );
541
542         if( NONEMPTY( psz_track_genre ) )
543             input_item_SetGenre( p_input_item, psz_track_genre );
544
545         if( NONEMPTY( psz_track_description ) )
546             input_item_SetDescription( p_input_item, psz_track_description );
547
548         if( NONEMPTY( psz_album ) )
549             input_item_SetAlbum( p_input_item, psz_album );
550
551         if( NONEMPTY( psz_year ) )
552             input_item_SetDate( p_input_item, psz_year );
553
554         char psz_num[3+1];
555         snprintf( psz_num, sizeof(psz_num), "%d", 1+i );
556         input_item_SetTrackNum( p_input_item, psz_num );
557
558         input_item_node_AppendItem( p_root, p_input_item );
559         vlc_gc_decref( p_input_item );
560         free( psz_uri ); free( psz_opt ); free( psz_name );
561         free( psz_first ); free( psz_last );
562     }
563 #undef ON_EMPTY
564 #undef NONEMPTY
565
566     input_item_node_PostAndDelete( p_root );
567
568     /* */
569     for( int i = 0; i < i_cd_text; i++ )
570     {
571         vlc_meta_t *p_meta = pp_cd_text[i];
572         if( !p_meta )
573             continue;
574         vlc_meta_Delete( p_meta );
575     }
576     free( pp_cd_text );
577
578 #ifdef HAVE_LIBCDDB
579     if( p_disc )
580         cddb_disc_destroy( p_disc );
581 #endif
582     return VLC_SUCCESS;
583 }
584
585 #ifdef HAVE_LIBCDDB
586 static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
587 {
588     if( var_InheritInteger( p_access, "album-art" ) == ALBUM_ART_WHEN_ASKED )
589         return NULL;
590
591     /* */
592     cddb_conn_t *p_cddb = cddb_new();
593     if( !p_cddb )
594     {
595         msg_Warn( p_access, "unable to use CDDB" );
596         return NULL;
597     }
598
599     /* */
600
601     cddb_http_enable( p_cddb );
602
603     char *psz_tmp = var_InheritString( p_access, "cddb-server" );
604     if( psz_tmp )
605     {
606         cddb_set_server_name( p_cddb, psz_tmp );
607         free( psz_tmp );
608     }
609
610     cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );
611
612     cddb_set_email_address( p_cddb, "vlc@videolan.org" );
613
614     cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
615     cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );
616
617
618     char *psz_cachedir;
619     char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );
620
621     if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
622         cddb_cache_enable( p_cddb );
623         cddb_cache_set_dir( p_cddb, psz_cachedir );
624         free( psz_cachedir );
625     }
626     free( psz_temp );
627
628     cddb_set_timeout( p_cddb, 10 );
629
630     /* */
631     cddb_disc_t *p_disc = cddb_disc_new();
632     if( !p_disc )
633     {
634         msg_Err( p_access, "unable to create CDDB disc structure." );
635         goto error;
636     }
637
638     int64_t i_length = 0;
639     for( int i = 0; i < i_titles; i++ )
640     {
641         cddb_track_t *t = cddb_track_new();
642         cddb_track_set_frame_offset( t, p_sectors[i] );
643         cddb_disc_add_track( p_disc, t );
644         const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
645                                (int64_t)CDDA_DATA_SIZE;
646         i_length += INT64_C(1000000) * i_size / 44100 / 4  ;
647     }
648
649     cddb_disc_set_length( p_disc, (int)(i_length/1000000) );
650
651     if( !cddb_disc_calc_discid( p_disc ) )
652     {
653         msg_Err( p_access, "CDDB disc ID calculation failed" );
654         goto error;
655     }
656
657     const int i_matches = cddb_query( p_cddb, p_disc );
658     if( i_matches < 0 )
659     {
660         msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno) );
661         goto error;
662     }
663     else if( i_matches == 0 )
664     {
665         msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
666         goto error;
667     }
668     else if( i_matches > 1 )
669         msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );
670
671     cddb_read( p_cddb, p_disc );
672
673     cddb_destroy( p_cddb);
674     return p_disc;
675
676 error:
677     if( p_disc )
678         cddb_disc_destroy( p_disc );
679     cddb_destroy( p_cddb );
680     return NULL;
681 }
682 #endif /*HAVE_LIBCDDB*/
683