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