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