]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
Add CDDB support to CDDA module (Refs:#459)
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "codecs.h"
34 #include "vcd/cdrom.h"
35
36 #include <vlc_playlist.h>
37
38 #ifdef HAVE_LIBCDDB
39 #include <cddb/cddb.h>
40 #endif
41
42 /*****************************************************************************
43  * Module descriptior
44  *****************************************************************************/
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 #define CACHING_TEXT N_("Caching value in ms")
49 #define CACHING_LONGTEXT N_( \
50     "Allows you to modify the default caching value for cdda streams. This " \
51     "value should be set in milliseconds units." )
52
53 vlc_module_begin();
54     set_shortname( _("Audio CD"));
55     set_description( _("Audio CD input") );
56     set_capability( "access2", 10 );
57     set_category( CAT_INPUT );
58     set_subcategory( SUBCAT_INPUT_ACCESS );
59     set_callbacks( Open, Close );
60
61     add_usage_hint( N_("[cdda:][device][@[track]]") );
62     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
63                  CACHING_LONGTEXT, VLC_TRUE );
64     add_bool( "cdda-separate-tracks", VLC_TRUE, NULL, NULL, NULL, VLC_TRUE );
65     add_integer( "cdda-track", -1 , NULL, NULL, NULL, VLC_TRUE );
66     add_string( "cddb-server", "freedb.freedb.org", NULL,
67                 N_( "CDDB Server" ), N_( "Adress of the CDDB server to use" ),
68                 VLC_TRUE );
69     add_integer( "cddb-port", 8880, NULL,
70                 N_( "CDDB port" ), N_( "CDDB Server port to use" ),
71                 VLC_TRUE );
72     add_shortcut( "cdda" );
73     add_shortcut( "cddasimple" );
74 vlc_module_end();
75
76
77 /* how many blocks VCDRead will read in each loop */
78 #define CDDA_BLOCKS_ONCE 20
79 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
80
81 /*****************************************************************************
82  * Access: local prototypes
83  *****************************************************************************/
84 struct access_sys_t
85 {
86     vcddev_t    *vcddev;                            /* vcd device descriptor */
87
88     /* Title infos */
89     int           i_titles;
90     input_title_t *title[99];         /* No more that 99 track in a cd-audio */
91
92     /* Current position */
93     int         i_sector;                                  /* Current Sector */
94     int *       p_sectors;                                  /* Track sectors */
95
96     /* Wave header for the output data */
97     WAVEHEADER  waveheader;
98     vlc_bool_t  b_header;
99
100     vlc_bool_t  b_separate_items;
101     vlc_bool_t  b_single_track;
102     int         i_track;
103
104 #ifdef HAVE_LIBCDDB
105     cddb_disc_t *p_disc;
106 #endif
107 };
108
109 static block_t *Block( access_t * );
110 static int      Seek( access_t *, int64_t );
111 static int      Control( access_t *, int, va_list );
112
113 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
114                       playlist_t *p_playlist, playlist_item_t *p_parent );
115
116 #ifdef HAVE_LIBCDDB
117 static void GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors );
118 #endif
119
120 /*****************************************************************************
121  * Open: open cdda
122  *****************************************************************************/
123 static int Open( vlc_object_t *p_this )
124 {
125     access_t     *p_access = (access_t*)p_this;
126     access_sys_t *p_sys;
127
128     vcddev_t *vcddev;
129     char *psz_name;
130
131     vlc_bool_t b_separate_requested;
132     vlc_bool_t b_play = VLC_FALSE;
133     input_thread_t *p_input;
134     playlist_item_t *p_item = NULL;
135     playlist_t *p_playlist  = NULL;
136     int i_ret;
137     int i_track;
138
139     if( !p_access->psz_path || !*p_access->psz_path )
140     {
141         /* Only when selected */
142         if( !p_this->b_force ) return VLC_EGENERIC;
143
144         psz_name = var_CreateGetString( p_this, "cd-audio" );
145         if( !psz_name || !*psz_name )
146         {
147             if( psz_name ) free( psz_name );
148             return VLC_EGENERIC;
149         }
150     }
151     else psz_name = strdup( p_access->psz_path );
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         return VLC_EGENERIC;
159     }
160     free( psz_name );
161
162     /* Set up p_access */
163     p_access->pf_read = NULL;
164     p_access->pf_block = Block;
165     p_access->pf_control = Control;
166     p_access->pf_seek = Seek;
167     p_access->info.i_update = 0;
168     p_access->info.i_size = 0;
169     p_access->info.i_pos = 0;
170     p_access->info.b_eof = VLC_FALSE;
171     p_access->info.i_title = 0;
172     p_access->info.i_seekpoint = 0;
173     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
174     memset( p_sys, 0, sizeof( access_sys_t ) );
175     p_sys->vcddev = vcddev;
176     p_sys->b_separate_items = VLC_FALSE;
177     p_sys->b_single_track = VLC_FALSE;
178     p_sys->b_header = VLC_FALSE;
179
180     b_separate_requested = var_CreateGetBool( p_access,
181                                               "cdda-separate-tracks" );
182
183     /* We only do separate items if the whole disc is requested -
184      *  Dirty hack we access some private data ! */
185     p_input = (input_thread_t *)( p_access->p_parent );
186     /* Do we play a single track ? */
187     i_track = var_CreateGetInteger( p_access, "cdda-track" );
188     if( b_separate_requested && p_input->input.i_title_start == -1 &&
189         i_track <= 0 )
190     {
191         p_sys->b_separate_items = VLC_TRUE;
192     }
193     if( i_track > 0 )
194     {
195         p_sys->b_single_track = VLC_TRUE;
196         p_sys->i_track = i_track - 1;
197     }
198
199     msg_Dbg( p_access, "Separate items : %i - Single track : %i",
200                         p_sys->b_separate_items, p_sys->b_single_track );
201
202     if( p_sys->b_separate_items )
203     {
204         p_playlist = (playlist_t *) vlc_object_find( p_access,
205                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
206
207         if( !p_playlist ) return VLC_EGENERIC;
208
209         /* Let's check if we need to play */
210         if( &p_playlist->status.p_item->input ==
211              ((input_thread_t *)p_access->p_parent)->input.p_item )
212         {
213             p_item = p_playlist->status.p_item;
214             b_play = VLC_TRUE;
215             msg_Dbg( p_access, "starting Audio CD playback");
216         }
217         else
218         {
219             input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
220                                          input.p_item;
221             p_item = playlist_LockItemGetByInput( p_playlist, p_current );
222             msg_Dbg( p_access, "not starting Audio CD playback");
223
224             if( !p_item )
225             {
226                 msg_Dbg( p_playlist, "unable to find item in playlist");
227                return -1;
228             }
229             b_play = VLC_FALSE;
230         }
231     }
232
233     /* We read the Table Of Content information */
234     if( 1 )
235     {
236         i_ret = GetTracks( p_access, p_sys->b_separate_items,
237                            p_playlist, p_item );
238         if( i_ret < 0 )
239         {
240             goto error;
241         }
242     }
243
244     /* Build a WAV header for the output data */
245     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
246     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
247     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
248     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
249     p_sys->waveheader.Length = 0;                     /* we just don't know */
250     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
251     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
252     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
253     SetWLE( &p_sys->waveheader.Modus, 2);
254     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
255     SetWLE( &p_sys->waveheader.BytesPerSample,
256             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
257     SetDWLE( &p_sys->waveheader.BytesPerSec,
258              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
259     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
260     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
261
262     /* Only update META if we are in old mode */
263     if( !p_sys->b_separate_items && !p_sys->b_single_track )
264     {
265         p_access->info.i_update |= INPUT_UPDATE_META;
266     }
267
268     /* Position on the right sector and update size */
269     if( p_sys->b_single_track )
270     {
271         p_sys->i_sector = p_sys->p_sectors[ p_sys->i_track ];
272         p_access->info.i_size =
273             ( p_sys->p_sectors[p_sys->i_track+1] -
274               p_sys->p_sectors[p_sys->i_track] ) *
275                         (int64_t)CDDA_DATA_SIZE;
276     }
277
278     /* PTS delay */
279     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
280
281     if( b_play )
282     {
283         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
284                           p_playlist->status.i_view, p_playlist->status.p_item,
285                           NULL );
286     }
287
288     if( p_playlist ) vlc_object_release( p_playlist );
289
290     return VLC_SUCCESS;
291
292 error:
293     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
294     free( p_sys );
295     if( p_playlist ) vlc_object_release( p_playlist );
296     return VLC_EGENERIC;
297 }
298
299 /*****************************************************************************
300  * Close: closes cdda
301  *****************************************************************************/
302 static void Close( vlc_object_t *p_this )
303 {
304     access_t     *p_access = (access_t *)p_this;
305     access_sys_t *p_sys = p_access->p_sys;
306     int          i;
307
308     for( i = 0; i < p_sys->i_titles; i++ )
309     {
310         vlc_input_title_Delete( p_sys->title[i] );
311     }
312
313     ioctl_Close( p_this, p_sys->vcddev );
314     free( p_sys );
315 }
316
317 /*****************************************************************************
318  * Block: read data (CDDA_DATA_ONCE)
319  *****************************************************************************/
320 static block_t *Block( access_t *p_access )
321 {
322     access_sys_t *p_sys = p_access->p_sys;
323     int i_blocks = CDDA_BLOCKS_ONCE;
324     block_t *p_block;
325
326     if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
327
328     /* Check end of file */
329     if( p_access->info.b_eof ) return NULL;
330
331     if( !p_sys->b_header )
332     {
333         /* Return only the header */
334         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
335         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
336         p_sys->b_header = VLC_TRUE;
337         return p_block;
338     }
339
340     /* Check end of title - Single track */
341     if( p_sys->b_single_track )
342     {
343         if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
344         {
345             p_access->info.b_eof = VLC_TRUE;
346             return NULL;
347         }
348         /* Don't read too far */
349         if( p_sys->i_sector + i_blocks >=
350             p_sys->p_sectors[p_sys->i_track +1] )
351         {
352             i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
353         }
354     }
355     else
356     {
357         /* Check end of title - Normal */
358         while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
359         {
360             if( p_access->info.i_title + 1 >= p_sys->i_titles )
361             {
362                 p_access->info.b_eof = VLC_TRUE;
363                 return NULL;
364             }
365
366             p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
367                                        INPUT_UPDATE_META;
368             p_access->info.i_title++;
369             p_access->info.i_size =
370                         p_sys->title[p_access->info.i_title]->i_size;
371             p_access->info.i_pos = 0;
372         }
373         /* Don't read too far */
374         if( p_sys->i_sector + i_blocks >=                                                       p_sys->p_sectors[p_access->info.i_title + 1] )
375         {
376             i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
377                          p_sys->i_sector;
378         }
379     }
380
381     /* Do the actual reading */
382     if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
383     {
384         msg_Err( p_access, "cannot get a new block of size: %i",
385                  i_blocks * CDDA_DATA_SIZE );
386         return NULL;
387     }
388
389     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
390             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
391     {
392         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
393         block_Release( p_block );
394
395         /* Try to skip one sector (in case of bad sectors) */
396         p_sys->i_sector++;
397         p_access->info.i_pos += CDDA_DATA_SIZE;
398         return NULL;
399     }
400
401     /* Update a few values */
402     p_sys->i_sector += i_blocks;
403     p_access->info.i_pos += p_block->i_buffer;
404
405     return p_block;
406 }
407
408 /****************************************************************************
409  * Seek
410  ****************************************************************************/
411 static int Seek( access_t *p_access, int64_t i_pos )
412 {
413     access_sys_t *p_sys = p_access->p_sys;
414
415     /* Next sector to read */
416     p_sys->i_sector = p_sys->p_sectors[
417                                         (p_sys->b_single_track ?
418                                          p_sys->i_track :
419                                          p_access->info.i_title + 1)
420                                       ] +
421                                 i_pos / CDDA_DATA_SIZE;
422     p_access->info.i_pos = i_pos;
423
424     return VLC_SUCCESS;
425 }
426
427 /*****************************************************************************
428  * Control:
429  *****************************************************************************/
430 static int Control( access_t *p_access, int i_query, va_list args )
431 {
432     access_sys_t *p_sys = p_access->p_sys;
433     vlc_bool_t   *pb_bool;
434     int          *pi_int;
435     int64_t      *pi_64;
436     input_title_t ***ppp_title;
437     int i;
438     char         *psz_title;
439     vlc_meta_t  **pp_meta;
440
441     switch( i_query )
442     {
443         /* */
444         case ACCESS_CAN_SEEK:
445         case ACCESS_CAN_FASTSEEK:
446         case ACCESS_CAN_PAUSE:
447         case ACCESS_CAN_CONTROL_PACE:
448             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
449             *pb_bool = VLC_TRUE;
450             break;
451
452         /* */
453         case ACCESS_GET_MTU:
454             pi_int = (int*)va_arg( args, int * );
455             *pi_int = CDDA_DATA_ONCE;
456             break;
457
458         case ACCESS_GET_PTS_DELAY:
459             pi_64 = (int64_t*)va_arg( args, int64_t * );
460             *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
461             break;
462
463         /* */
464         case ACCESS_SET_PAUSE_STATE:
465             break;
466
467         case ACCESS_GET_TITLE_INFO:
468             if( p_sys->b_single_track )
469                 return VLC_EGENERIC;
470             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
471             pi_int    = (int*)va_arg( args, int* );
472             *((int*)va_arg( args, int* )) = 1; /* Title offset */
473
474             /* Duplicate title infos */
475             *pi_int = p_sys->i_titles;
476             *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
477             for( i = 0; i < p_sys->i_titles; i++ )
478             {
479                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
480             }
481             break;
482
483         case ACCESS_SET_TITLE:
484             if( p_sys->b_single_track ) return VLC_EGENERIC;
485             i = (int)va_arg( args, int );
486             if( i != p_access->info.i_title )
487             {
488                 /* Update info */
489                 p_access->info.i_update |=
490                     INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
491                 p_access->info.i_title = i;
492                 p_access->info.i_size = p_sys->title[i]->i_size;
493                 p_access->info.i_pos = 0;
494
495                 /* Next sector to read */
496                 p_sys->i_sector = p_sys->p_sectors[i];
497             }
498             break;
499
500         case ACCESS_GET_META:
501              if( p_sys->b_single_track ) return VLC_EGENERIC;
502              psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
503              snprintf( psz_title, 100, _("Audio CD - Track %i" ),
504                                         p_access->info.i_title+1 );
505              pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
506              *pp_meta = vlc_meta_New();
507              vlc_meta_Add( *pp_meta, VLC_META_TITLE, psz_title );
508              free( psz_title );
509              break;
510
511         case ACCESS_SET_SEEKPOINT:
512         case ACCESS_SET_PRIVATE_ID_STATE:
513             return VLC_EGENERIC;
514
515         default:
516             msg_Warn( p_access, "unimplemented query in control" );
517             return VLC_EGENERIC;
518
519     }
520     return VLC_SUCCESS;
521 }
522
523 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
524                       playlist_t *p_playlist, playlist_item_t *p_parent )
525 {
526     access_sys_t *p_sys = p_access->p_sys;
527     int i;
528     playlist_item_t *p_item;
529     char *psz_name;
530     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
531                                           p_sys->vcddev, &p_sys->p_sectors );
532     if( p_sys->i_titles < 0 )
533     {
534         msg_Err( p_access, "unable to count tracks" );
535         return VLC_EGENERIC;;
536     }
537     else if( p_sys->i_titles <= 0 )
538     {
539         msg_Err( p_access, "no audio tracks found" );
540         return VLC_EGENERIC;
541     }
542
543     if( b_separate )
544     {
545         if( p_parent->i_children == -1 )
546         {
547             playlist_LockItemToNode( p_playlist, p_parent );
548         }
549         psz_name = strdup( "Audio CD" );
550         vlc_mutex_lock( &p_playlist->object_lock );
551         playlist_ItemSetName( p_parent, psz_name );
552         vlc_mutex_unlock( &p_playlist->object_lock );
553         var_SetInteger( p_playlist, "item-change",
554                         p_parent->input.i_id );
555         free( psz_name );
556
557 #ifdef HAVE_LIBCDDB
558         GetCDDBInfo( p_access, p_sys->i_titles, p_sys->p_sectors );
559         if( p_sys->p_disc )
560         {
561             if( p_sys->p_disc->title )
562             {
563                 asprintf( &psz_name, "%s", p_sys->p_disc->title );
564                 vlc_mutex_lock( &p_playlist->object_lock );
565                 playlist_ItemSetName( p_parent, psz_name );
566                 vlc_mutex_unlock( &p_playlist->object_lock );
567                 var_SetInteger( p_playlist, "item-change",
568                                 p_parent->input.i_id );
569                 free( psz_name );
570             }
571         }
572 #endif
573     }
574
575     /* Build title table */
576     for( i = 0; i < p_sys->i_titles; i++ )
577     {
578         msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
579         if( !b_separate )
580         {
581             input_title_t *t = p_sys->title[i] = vlc_input_title_New();
582
583             asprintf( &t->psz_name, _("Track %i"), i + 1 );
584             t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
585                         (int64_t)CDDA_DATA_SIZE;
586
587             t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
588         }
589         else
590         {
591             char *psz_uri; int i_size, i_length;
592             int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
593                                                 : 0;
594             char *psz_opt;
595
596             psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
597             psz_opt = malloc( strlen( "cdda-track=" ) + 3 );
598
599             psz_uri = (char*)malloc( i_path_len + 13 );
600             snprintf( psz_uri, i_path_len + 13, "cdda://%s",
601                                 p_access->psz_path ? p_access->psz_path : "" );
602             sprintf( psz_opt, "cdda-track=%i", i+1 );
603
604             /* Define a "default name" */
605             sprintf( psz_name, _("Audio CD - Track %i"), (i+1) );
606
607             /* Create playlist items */
608             p_item = playlist_ItemNewWithType( VLC_OBJECT( p_playlist ),
609                                  psz_uri, psz_name, ITEM_TYPE_DISC );
610             playlist_ItemAddOption( p_item, psz_opt );
611 #ifdef HAVE_LIBCDDB
612             /* If we have CDDB info, change the name */
613             if( p_sys->p_disc )
614             {
615                 char *psz_result;
616                 cddb_track_t *t = cddb_disc_get_track( p_sys->p_disc, i );
617                 if( t!= NULL )
618                 {
619                     if( t->title != NULL )
620                     {
621                         vlc_input_item_AddInfo( &p_item->input,
622                                             _("Meta-information"),
623                                             VLC_META_TITLE, t->title );
624                         if( p_item->input.psz_name )
625                             free( p_item->input.psz_name );
626                         asprintf( &p_item->input.psz_name, "%s", t->title );
627                     }
628                     psz_result = cddb_track_get_artist( t );
629                     if( psz_result )
630                     {
631                         vlc_input_item_AddInfo( &p_item->input,
632                                             _("Meta-information"),
633                                             VLC_META_ARTIST, psz_result );
634                     }
635                 }
636             }
637 #endif
638             playlist_NodeAddItem( p_playlist, p_item,
639                                   p_parent->pp_parents[0]->i_view,
640                                   p_parent, PLAYLIST_APPEND, PLAYLIST_END );
641             free( psz_uri ); free( psz_opt ); free( psz_name );
642         }
643     }
644
645     p_sys->i_sector = p_sys->p_sectors[0];
646     if( p_sys->b_separate_items )
647     {
648         p_sys->i_titles = 0;
649     }
650
651    return VLC_SUCCESS;
652 }
653
654 #ifdef HAVE_LIBCDDB
655 static void GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
656 {
657     int i, i_matches;
658     int64_t  i_length = 0, i_size = 0;
659     cddb_conn_t  *p_cddb = cddb_new();
660
661 //    cddb_log_set_handler( CDDBLogHandler );
662
663     if( !p_cddb )
664     {
665         msg_Warn( p_access, "unable to use CDDB" );
666         goto cddb_destroy;
667     }
668
669     cddb_set_email_address( p_cddb, "vlc@videolan.org" );
670     cddb_set_server_name( p_cddb, config_GetPsz( p_access, "cddb-server" ) );
671     cddb_set_server_port( p_cddb, config_GetInt( p_access, "cddb-port" ) );
672
673     /// \todo
674     cddb_cache_disable( p_cddb );
675
676 //    cddb_cache_set_dir( p_cddb,
677 //                     config_GetPsz( p_access,
678 //                                    MODULE_STRING "-cddb-cachedir") );
679
680     cddb_set_timeout( p_cddb, 10 );
681
682     /// \todo
683     cddb_http_disable( p_cddb);
684
685     p_access->p_sys->p_disc = cddb_disc_new();
686
687     if(! p_access->p_sys->p_disc )
688     {
689         msg_Err( p_access, "Unable to create CDDB disc structure." );
690         goto cddb_end;
691     }
692
693     for(i = 0; i < i_titles ; i++ )
694     {
695         cddb_track_t *t = cddb_track_new();
696         cddb_track_set_frame_offset(t, p_sectors[i] );
697         cddb_disc_add_track( p_access->p_sys->p_disc, t );
698         i_size = ( p_sectors[i+1] - p_sectors[i] ) *
699                    (int64_t)CDDA_DATA_SIZE;
700         i_length += I64C(1000000) * i_size / 44100 / 4  ;
701     }
702
703     cddb_disc_set_length( p_access->p_sys->p_disc, (int)(i_length/1000000) );
704
705     if (!cddb_disc_calc_discid(p_access->p_sys->p_disc ))
706     {
707         msg_Err( p_access, "CDDB disc ID calculation failed" );
708         goto cddb_destroy;
709     }
710
711     i_matches = cddb_query( p_cddb, p_access->p_sys->p_disc);
712
713     if (i_matches > 0)
714     {
715         if (i_matches > 1)
716              msg_Warn( p_access, "Found %d matches in CDDB. Using first one.",
717                                  i_matches);
718         cddb_read( p_cddb, p_access->p_sys->p_disc );
719
720 //        cddb_disc_print( p_access->p_sys->p_disc );
721     }
722     else
723     {
724         msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno));
725     }
726
727 cddb_destroy:
728     cddb_destroy( p_cddb);
729
730 cddb_end: ;
731 }
732 #endif /*HAVE_LIBCDDB*/