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