]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
Fix bugs in disc-view slider. (May still have a couple left)
[vlc] / modules / access / cdda / access.c
1 /*****************************************************************************
2  * access.c : CD digital audio input module for vlc using libcdio
3  *****************************************************************************
4  * Copyright (C) 2000, 2003, 2004, 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Rocky Bernstein <rocky@panix.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "callback.h"      /* FIXME - reorganize callback.h, cdda.h better */
30 #include "cdda.h"          /* private structures. Also #includes vlc things */
31 #include "info.h"          /* headers for meta info retrieval */
32 #include <vlc_playlist.h>  /* Has to come *after* cdda.h */
33 #include "vlc_keys.h"
34
35 #include <cdio/cdio.h>
36 #include <cdio/logging.h>
37 #include <cdio/cd_types.h>
38
39 #include <stdio.h>
40
41 /* #ifdef variables below are defined via config.h via #include vlc above. */
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
45
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
48 #endif
49
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53
54 #ifdef HAVE_UNISTD_H
55 #   include <unistd.h>
56 #endif
57
58 /* FIXME: This variable is a hack. Would be nice to eliminate. */
59 access_t *p_cdda_input = NULL;
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static block_t *CDDAReadBlocks( access_t * p_access );
65 static int      CDDASeek( access_t * p_access, int64_t i_pos );
66 static int      CDDAControl( access_t *p_access, int i_query,
67                              va_list args );
68
69 static int      CDDAInit( access_t *p_access, cdda_data_t *p_cdda ) ;
70
71
72 /****************************************************************************
73  * Private functions
74  ****************************************************************************/
75
76 /* process messages that originate from libcdio. */
77 static void
78 cdio_log_handler (cdio_log_level_t level, const char message[])
79 {
80   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
81
82   if( p_cdda == NULL )
83       return;
84
85   switch (level) {
86   case CDIO_LOG_DEBUG:
87   case CDIO_LOG_INFO:
88     if (p_cdda->i_debug & INPUT_DBG_CDIO)
89       msg_Dbg( p_cdda_input, message);
90     break;
91   case CDIO_LOG_WARN:
92     msg_Warn( p_cdda_input, message);
93     break;
94   case CDIO_LOG_ERROR:
95   case CDIO_LOG_ASSERT:
96     msg_Err( p_cdda_input, message);
97     break;
98   default:
99     msg_Warn( p_cdda_input, message,
100             "The above message had unknown cdio log level",
101             level);
102   }
103   return;
104 }
105
106
107 #ifdef HAVE_LIBCDDB
108 /*! This routine is called by libcddb routines on error.
109    Setup is done by init_input_plugin.
110 */
111 static void
112 cddb_log_handler (cddb_log_level_t level, const char message[])
113 {
114     cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
115     switch (level)
116     {
117         case CDDB_LOG_DEBUG:
118         case CDDB_LOG_INFO:
119         if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
120
121         /* Fall through if to warn case */
122         default:
123             cdio_log_handler (level, message);
124     }
125 }
126 #endif /*HAVE_LIBCDDB*/
127
128
129 /*! This routine is when xine is not fully set up (before full initialization)
130    or is not around (before finalization).
131 */
132 static void
133 uninit_log_handler (cdio_log_level_t level, const char message[])
134 {
135     cdda_data_t *p_cdda = NULL;
136
137     if (p_cdda_input)
138         p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
139
140      switch (level)
141      {
142         case CDIO_LOG_DEBUG:
143         case CDIO_LOG_INFO:
144         if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
145             return;
146
147         /* Fall through if to warn case */
148         case CDIO_LOG_WARN:
149             fprintf(stderr, "WARN: %s\n", message);
150             break;
151         case CDIO_LOG_ERROR:
152             fprintf(stderr, "ERROR: %s\n", message);
153             break;
154         case CDIO_LOG_ASSERT:
155             fprintf(stderr, "ASSERT ERROR: %s\n", message);
156             break;
157         default:
158             fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n", message,
159                             "The above message had unknown cdio log level",
160                             level);
161     }
162
163     /* gl_default_cdio_log_handler (level, message); */
164 }
165
166 /*****************************************************************************
167  * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
168  * an allocated pointer to the data. NULL is returned if no data
169  * read. It is also possible if we haven't read a RIFF header in which
170  * case one that we creaded during Open/Initialization is returned.
171  *****************************************************************************/
172 static block_t * CDDAReadBlocks( access_t * p_access )
173 {
174     block_t     *p_block;
175     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
176     int          i_blocks = p_cdda->i_blocks_per_read;
177
178     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), 
179               "called %d pos: %lld, size: %lld",
180               p_cdda->i_lsn, p_access->info.i_pos, p_access->info.i_size);
181
182     /* Check end of file */
183     if( p_access->info.b_eof ) return NULL;
184
185     if( !p_cdda->b_header )
186       {
187         /* Return only the dummy RIFF header we created in Open/Init */
188         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
189         memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
190         p_cdda->b_header = VLC_TRUE;
191         return p_block;
192     }
193
194     /* Check end of track */
195     while( p_cdda->i_lsn > cdio_get_track_last_lsn(p_cdda->p_cdio, 
196                                                    p_cdda->i_track) )
197     {
198         if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
199         {
200             p_access->info.b_eof = VLC_TRUE;
201             return NULL;
202         }
203
204         p_access->info.i_update |= INPUT_UPDATE_TITLE;
205         p_access->info.i_title++;
206         p_cdda->i_track++;
207
208         if ( p_cdda-> b_nav_mode ) {
209           char *psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
210           input_Control( p_cdda->p_input, INPUT_SET_NAME, psz_title );
211           free(psz_title);
212         } else {
213           p_access->info.i_size =
214             p_cdda->p_title[p_access->info.i_title]->i_size;
215           p_access->info.i_pos = 0;
216           p_access->info.i_update |= INPUT_UPDATE_SIZE;
217         }
218     }
219
220     /* Possibly adjust i_blocks so we don't read past the end of a track. */
221     if( p_cdda->i_lsn + i_blocks >= 
222         cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track+1) )
223     {
224       i_blocks = cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track+1 ) 
225         - p_cdda->i_lsn;
226     }
227
228     /* Do the actual reading */
229     p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
230     if( !p_block)
231     {
232       msg_Err( p_access, "Cannot get a new block of size: %i",
233                i_blocks * CDIO_CD_FRAMESIZE_RAW );
234       return NULL;
235     }
236     
237     {
238 #if LIBCDIO_VERSION_NUM >= 72
239       driver_return_code_t rc = DRIVER_OP_SUCCESS;
240
241       if ( p_cdda->e_paranoia && p_cdda->paranoia ) 
242         {
243           int i;
244           for( i = 0; i < i_blocks; i++ )
245             {
246               int16_t *p_readbuf = cdio_paranoia_read(p_cdda->paranoia, NULL);
247               char *psz_err=cdio_cddap_errors(p_cdda->paranoia_cd);
248               char *psz_mes=cdio_cddap_messages(p_cdda->paranoia_cd);
249
250               if (psz_mes || psz_err)
251                 msg_Err( p_access, "%s%s\n", psz_mes ? psz_mes: "", 
252                         psz_err ? psz_err: "" );
253               
254               if (psz_err) free(psz_err);
255               if (psz_mes) free(psz_mes);
256               if( !p_readbuf ) {
257                 msg_Err( p_access, "paranoia read error on frame %i\n", 
258                          p_cdda->i_lsn+i );
259               } else 
260                 memcpy(p_block->p_buffer + i * CDIO_CD_FRAMESIZE_RAW, 
261                        p_readbuf, CDIO_CD_FRAMESIZE_RAW);
262             }
263         }
264       else 
265         rc = cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
266                                       p_cdda->i_lsn, i_blocks);
267 #else
268 #define DRIVER_OP_SUCCESS 0
269       int rc;
270       rc = cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
271                                     p_cdda->i_lsn, i_blocks);
272 #endif    
273       if( rc != DRIVER_OP_SUCCESS )
274         {
275           msg_Err( p_access, "could not read %d sectors starting from %lu",
276                    i_blocks, (long unsigned int) p_cdda->i_lsn );
277           block_Release( p_block );
278           
279           /* If we had problems above, assume the problem is with
280              the first sector of the read and set to skip it.  In
281              the future libcdio may have cdparanoia support.
282           */
283           p_cdda->i_lsn++;
284           p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
285           return NULL;
286         }
287     }
288
289     p_cdda->i_lsn        += i_blocks;
290     p_access->info.i_pos += i_blocks * CDIO_CD_FRAMESIZE_RAW;
291
292     return p_block;
293 }
294
295 /****************************************************************************
296  * CDDASeek - change position for subsequent reads. For example, this
297  * can happen if the user moves a position slider bar in a GUI.
298  ****************************************************************************/
299 static int CDDASeek( access_t * p_access, int64_t i_pos )
300 {
301     cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
302
303     p_cdda->i_lsn = (i_pos / CDIO_CD_FRAMESIZE_RAW);
304
305 #if LIBCDIO_VERSION_NUM >= 72
306     if ( p_cdda->e_paranoia && p_cdda->paranoia ) 
307       cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
308 #endif
309
310     if ( ! p_cdda->b_nav_mode ) 
311       p_cdda->i_lsn += cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
312
313     /* Seeked backwards and we are doing disc mode. */
314     if ( p_cdda->b_nav_mode && p_access->info.i_pos > i_pos ) {
315       track_t i_track;
316       char *psz_title;
317       
318       for( i_track = p_cdda->i_track; 
319            i_track > 1 && 
320              p_cdda->i_lsn < cdio_get_track_lsn(p_cdda->p_cdio, i_track);
321            i_track--, p_access->info.i_title-- ) ;
322
323       p_cdda->i_track = i_track;
324       p_access->info.i_update |= INPUT_UPDATE_TITLE;
325       psz_title  = CDDAFormatTitle( p_access, p_cdda->i_track );
326       input_Control( p_cdda->p_input, INPUT_SET_NAME, 
327                      psz_title );
328       free(psz_title);
329
330     }
331     
332     p_access->info.i_pos = i_pos;
333
334     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
335                "lsn %lu, offset: %lld",
336                (long unsigned int) p_cdda->i_lsn, i_pos );
337     return VLC_SUCCESS;
338 }
339
340 /****************************************************************************
341  * Public functions
342  ****************************************************************************/
343
344 /*****************************************************************************
345  * Open: open cdda device or image file and initialize structures
346  *       for subsequent operations.
347  *****************************************************************************/
348 int 
349 CDDAOpen( vlc_object_t *p_this )
350 {
351     access_t    *p_access = (access_t*)p_this;
352     char *      psz_source = NULL;
353     cdda_data_t *p_cdda    = NULL;
354     CdIo_t      *p_cdio;
355     track_t     i_track = 1;
356     vlc_bool_t  b_single_track = false;
357     int         i_rc = VLC_EGENERIC;
358
359     p_access->p_sys = NULL;
360
361     /* Set where to log errors messages from libcdio. */
362     p_cdda_input = p_access;
363
364     /* parse the options passed in command line : */
365
366     if( p_access->psz_path && *p_access->psz_path )
367     {
368         char *psz_parser = psz_source = strdup( p_access->psz_path );
369
370         while( *psz_parser && *psz_parser != '@' )
371         {
372             psz_parser++;
373         }
374
375         if( *psz_parser == '@' )
376         {
377             /* Found options */
378             *psz_parser = '\0';
379             ++psz_parser;
380
381             if ('T' == *psz_parser || 't' == *psz_parser )
382             ++psz_parser;
383
384             i_track = (int)strtol( psz_parser, NULL, 10 );
385             i_track = i_track ? i_track : 1;
386             b_single_track = true;
387         }
388     }
389
390     if (!psz_source || !*psz_source)
391     {
392         /* No device/track given. Continue only when this plugin was
393            selected */
394         if( !p_this->b_force ) return VLC_EGENERIC;
395
396         psz_source = var_CreateGetString( p_this, "cd-audio" );
397
398         if( !psz_source || !*psz_source )
399         {
400             /* Scan for a CD-ROM drive with a CD-DA in it. */
401             char **cd_drives =
402               cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
403
404             if (NULL == cd_drives || NULL == cd_drives[0] )
405             {
406                 msg_Err( p_access,
407                      "libcdio couldn't find something with a CD-DA in it" );
408                 if (cd_drives) cdio_free_device_list(cd_drives);
409                 return VLC_EGENERIC;
410             }
411
412             psz_source = strdup(cd_drives[0]);
413             cdio_free_device_list(cd_drives);
414         }
415     }
416
417     cdio_log_set_handler ( cdio_log_handler );
418
419     /* Open CDDA */
420     if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
421     {
422         msg_Warn( p_access, "could not open %s", psz_source );
423         if (psz_source) free( psz_source );
424         return VLC_EGENERIC;
425     }
426
427     p_cdda = calloc( 1, sizeof(cdda_data_t) );
428     if( p_cdda == NULL )
429     {
430         msg_Err( p_access, "out of memory" );
431         free( psz_source );
432         return VLC_ENOMEM;
433     }
434
435 #ifdef HAVE_LIBCDDB
436     cddb_log_set_handler ( cddb_log_handler );
437     p_cdda->cddb.disc = NULL;
438     p_cdda->b_cddb_enabled =
439       config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
440 #endif
441
442     p_cdda->b_cdtext =
443       config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
444
445     p_cdda->b_cdtext_prefer =
446       config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
447
448     p_cdda->psz_source = strdup(psz_source);
449     p_cdda->b_header   = VLC_FALSE;
450     p_cdda->p_cdio     = p_cdio;
451     p_cdda->i_tracks   = 0;
452     p_cdda->i_titles   = 0;
453     p_cdda->i_track    = i_track;
454     p_cdda->i_debug    = config_GetInt(p_this, MODULE_STRING 
455                                        "-debug");
456     p_cdda->b_nav_mode = config_GetInt(p_this, MODULE_STRING 
457                                        "-navigation-mode" );
458     p_cdda->i_blocks_per_read
459       = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
460
461     p_cdda->p_input  = vlc_object_find( p_access, VLC_OBJECT_INPUT,
462                                         FIND_PARENT );
463
464     if (0 == p_cdda->i_blocks_per_read)
465         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
466
467     if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
468          || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
469     {
470         msg_Warn( p_cdda_input,
471                 "Number of blocks (%d) has to be between %d and %d. "
472                 "Using %d.",
473                 p_cdda->i_blocks_per_read,
474                 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
475                 DEFAULT_BLOCKS_PER_READ );
476         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
477     }
478
479     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
480
481     /* Set up p_access */
482     p_access->pf_read    = NULL;
483     p_access->pf_block   = CDDAReadBlocks;
484     p_access->pf_control = CDDAControl;
485     p_access->pf_seek    = CDDASeek;
486
487     {
488       lsn_t i_last_lsn;
489       
490       if (p_cdda->b_nav_mode)
491           i_last_lsn = cdio_get_track_lsn(p_cdio, CDIO_CDROM_LEADOUT_TRACK);
492       else 
493           i_last_lsn = cdio_get_track_last_lsn(p_cdio, i_track);
494       
495       if (CDIO_INVALID_LSN != i_last_lsn)
496         p_access->info.i_size = i_last_lsn * (uint64_t) CDIO_CD_FRAMESIZE_RAW;
497       else 
498         p_access->info.i_size = 0;
499     }
500
501     p_access->info.i_update    = 0;
502     p_access->info.b_eof       = VLC_FALSE;
503     p_access->info.i_title     = 0;
504     p_access->info.i_seekpoint = 0;
505
506     p_access->p_sys     = (access_sys_t *) p_cdda;
507
508     /* We read the Table Of Content information */
509     i_rc = CDDAInit( p_access, p_cdda );
510     if ( VLC_SUCCESS != i_rc ) goto error;
511
512     CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
513
514 #if LIBCDIO_VERSION_NUM >= 72
515     {
516    
517       char *psz_paranoia = config_GetPsz( p_access, 
518                                           MODULE_STRING "-paranoia" );
519       p_cdda->e_paranoia = paranoia_none;
520       if( psz_paranoia && *psz_paranoia )
521       {
522
523         if( !strncmp( psz_paranoia, "full", strlen("full") )  )
524           p_cdda->e_paranoia = paranoia_full;
525         else if( !strncmp( psz_paranoia, "overlap", strlen("overlap") )  )
526           p_cdda->e_paranoia = paranoia_overlap;
527         
528         /* Use CD Paranoia? */
529         if ( p_cdda->e_paranoia ) {
530           p_cdda->paranoia_cd = cdio_cddap_identify_cdio(p_cdio, 1, NULL);
531           /* We'll set for verbose paranoia messages. */
532           cdio_cddap_verbose_set(p_cdda->paranoia_cd, CDDA_MESSAGE_PRINTIT, 
533                                  CDDA_MESSAGE_PRINTIT);
534           if ( 0 != cdio_cddap_open(p_cdda->paranoia_cd) ) {
535             msg_Warn( p_cdda_input, "Unable to get paranoia support - "
536                       "continuing without it." );
537             p_cdda->e_paranoia = paranoia_none;
538           } else {
539             p_cdda->paranoia = cdio_paranoia_init(p_cdda->paranoia_cd);
540             cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
541             
542             /* Set reading mode for full or overlap paranoia, 
543                but allow skipping sectors. */
544             cdio_paranoia_modeset(p_cdda->paranoia,
545                                   paranoia_full == p_cdda->e_paranoia ?
546                                   PARANOIA_MODE_FULL^PARANOIA_MODE_NEVERSKIP :
547                                   PARANOIA_MODE_OVERLAP^PARANOIA_MODE_NEVERSKIP
548                                   );
549           }
550         }
551       }
552     }
553 #endif    
554
555     /* Build a WAV header to put in front of the output data.
556        This gets sent back in the Block (read) routine.
557      */
558     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
559     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
560     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
561     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
562     p_cdda->waveheader.Length = 0;                     /* we just don't know */
563     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
564     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
565     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
566     SetWLE( &p_cdda->waveheader.Modus, 2);
567     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
568     SetWLE( &p_cdda->waveheader.BytesPerSample,
569             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
570     SetDWLE( &p_cdda->waveheader.BytesPerSec,
571              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
572     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
573     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
574
575     /* PTS delay */
576     var_Create( p_access, MODULE_STRING "-caching",
577                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
578     vlc_object_release( p_cdda->p_input );
579     return VLC_SUCCESS;
580
581  error:
582     cdio_destroy( p_cdda->p_cdio );
583     if( psz_source) free( psz_source );
584     if( p_cdda ) {
585       if ( p_cdda->p_input )
586         vlc_object_release( p_cdda->p_input );
587       free(p_cdda);
588     }
589     
590     return i_rc;
591
592 }
593
594 /*****************************************************************************
595  * CDDAClose: closes cdda and frees any resources associded with it.
596  *****************************************************************************/
597 void 
598 CDDAClose (vlc_object_t *p_this )
599 {
600     access_t    *p_access = (access_t *) p_this;
601     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
602     track_t      i;
603
604     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
605
606     /* Remove playlist titles */
607     for( i = 0; i < p_cdda->i_titles; i++ )
608     {
609         vlc_input_title_Delete( p_cdda->p_title[i] );
610     }
611
612 #ifdef HAVE_LIBCDDB
613     cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
614     if (p_cdda->b_cddb_enabled)
615       cddb_disc_destroy(p_cdda->cddb.disc);
616 #endif
617
618     cdio_destroy( p_cdda->p_cdio );
619     cdio_log_set_handler (uninit_log_handler);
620
621 #if LIBCDIO_VERSION_NUM >= 72
622     if (p_cdda->paranoia)
623       cdio_paranoia_free(p_cdda->paranoia);
624     if (p_cdda->paranoia_cd) 
625       cdio_cddap_close_no_free_cdio(p_cdda->paranoia_cd);
626 #endif
627
628     if (p_cdda->psz_mcn)    free( p_cdda->psz_mcn );
629     if (p_cdda->psz_source) free( p_cdda->psz_source );
630
631 #if LIBCDDB_VERSION_NUM >= 1
632     libcddb_shutdown();
633 #endif
634     free( p_cdda );
635     p_cdda = NULL;
636     p_cdda_input = NULL;
637 }
638
639 /*****************************************************************************
640  * Control: The front-end or vlc engine calls here to ether get
641  * information such as meta information or plugin capabilities or to
642  * issue miscellaneous "set" requests.
643  *****************************************************************************/
644 static int CDDAControl( access_t *p_access, int i_query, va_list args )
645 {
646     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
647     int          *pi_int;
648     int i;
649
650     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
651                "query %d", i_query );
652
653     switch( i_query )
654     {
655         /* Pass back a copy of meta information that was gathered when we
656            during the Open/Initialize call.
657          */
658         case ACCESS_GET_META:
659         {
660             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
661             if ( p_cdda->p_meta )
662             {
663                 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
664                 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
665                 return VLC_SUCCESS;
666             }
667             else {
668                 msg_Warn( p_access, "tried to copy NULL meta info" );
669                 return VLC_EGENERIC;
670             }
671         }
672
673         case ACCESS_CAN_CONTROL_PACE:
674         case ACCESS_CAN_FASTSEEK:
675         case ACCESS_CAN_SEEK:
676         case ACCESS_CAN_PAUSE:
677         {
678             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
679             dbg_print( INPUT_DBG_META, 
680                        "can seek/fastseek/pause/control pace");
681             *pb_bool = VLC_TRUE;
682             return VLC_SUCCESS;
683         }
684
685         /* */
686         case ACCESS_GET_MTU:
687         {
688             pi_int = (int*)va_arg( args, int * );
689             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
690             dbg_print( INPUT_DBG_META, "Get MTU %d", *pi_int);
691             break;
692         }
693
694         case ACCESS_GET_PTS_DELAY:
695         {
696             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
697             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
698               * MILLISECONDS_PER_SEC;
699             break;
700         }
701
702         /* */
703         case ACCESS_SET_PAUSE_STATE:
704             dbg_print( INPUT_DBG_META, "Set pause state");
705             break;
706
707         case ACCESS_GET_TITLE_INFO:
708         {
709             input_title_t ***ppp_title = 
710               (input_title_t***)va_arg( args, input_title_t*** );
711
712             pi_int    = (int*)va_arg( args, int* );
713             *((int*)va_arg( args, int* )) = 1; /* Title offset */
714
715             dbg_print ( INPUT_DBG_EVENT,
716                         "GET TITLE: i_tracks %d, i_tracks %d",
717                         p_cdda->i_tracks, p_cdda->i_tracks );
718
719             CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
720
721             if ( p_cdda->b_nav_mode) {
722                 char *psz_title = 
723                   CDDAFormatTitle( p_access, p_cdda->i_track );
724                 input_Control( p_cdda->p_input, INPUT_SET_NAME, 
725                                psz_title );
726                 free(psz_title);
727             }
728
729             /* Duplicate title info */
730             if( p_cdda->i_titles == 0 )
731             {
732                 *pi_int = 0; ppp_title = NULL;
733                 return VLC_SUCCESS;
734             }
735             *pi_int = p_cdda->i_titles;
736             *ppp_title = calloc(1, 
737                                 sizeof( input_title_t **) * p_cdda->i_titles );
738
739             if (!*ppp_title) return VLC_ENOMEM;
740
741             for( i = 0; i < p_cdda->i_titles; i++ )
742             {
743               if ( p_cdda->p_title[i] ) {
744                    (*ppp_title)[i] =
745                      vlc_input_title_Duplicate( p_cdda->p_title[i] );
746               }
747             }
748             break;
749         }
750
751         case ACCESS_SET_TITLE:
752         {
753             i = (int)va_arg( args, int );
754
755             dbg_print( INPUT_DBG_EVENT, "set title %d", i );
756             if( i != p_access->info.i_title )
757             {
758                 /* Update info */
759                 p_access->info.i_title = i;
760                 if ( p_cdda->b_nav_mode) 
761                 {
762                     lsn_t i_last_lsn;
763                     char *psz_title = 
764                       CDDAFormatTitle( p_access, i+1 );
765                     input_Control( p_cdda->p_input, INPUT_SET_NAME, 
766                                    psz_title );
767                     free(psz_title);
768                     p_cdda->i_track = p_cdda->i_first_track+i;
769                     i_last_lsn = cdio_get_track_lsn(p_cdda->p_cdio, 
770                                                     CDIO_CDROM_LEADOUT_TRACK);
771                     if (CDIO_INVALID_LSN != i_last_lsn)
772                       p_access->info.i_size = (int64_t) CDIO_CD_FRAMESIZE_RAW 
773                         * i_last_lsn ;
774                     p_access->info.i_pos = (int64_t)
775                       cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track ) 
776                       * CDIO_CD_FRAMESIZE_RAW;
777                 } else {
778                    p_access->info.i_size = p_cdda->p_title[i]->i_size;
779                    p_access->info.i_pos = 0;
780                 }
781                 p_access->info.i_update = 
782                   INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
783
784                 /* Next sector to read */
785                 p_cdda->i_lsn = 
786                   cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
787             }
788             break;
789         }
790
791         case ACCESS_SET_SEEKPOINT:
792         case ACCESS_SET_PRIVATE_ID_STATE:
793             dbg_print( INPUT_DBG_META, "set seekpoint/set private id state");
794             return VLC_EGENERIC;
795
796         default:
797             msg_Warn( p_access, "unimplemented query in control" );
798             return VLC_EGENERIC;
799
800     }
801     return VLC_SUCCESS;
802 }
803
804 /*****************************************************************************
805   CDDAInit:
806
807  Initialize information pertaining to the CD: the number of tracks,
808  first track number, LSNs for each track and the leadout. The leadout
809  information is stored after the last track. The LSN array is
810  0-origin, same as p_access->info.  Add first_track to get what track
811  number this is on the CD. Note: libcdio uses the real track number.
812
813  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
814
815  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
816  *****************************************************************************/
817 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
818 {
819     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
820
821     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
822     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
823
824     discmode = cdio_get_discmode(p_cdda->p_cdio);
825     switch(discmode) {
826     case CDIO_DISC_MODE_CD_DA:
827     case CDIO_DISC_MODE_CD_MIXED:
828         /* These are possible for CD-DA */
829         break;
830     default:
831         /* These are not possible for CD-DA */
832         msg_Err( p_access,
833                "Disc seems not to be CD-DA. libcdio reports it is %s",
834                discmode2str[discmode]
835                );
836         return VLC_EGENERIC;
837     }
838
839     /* Set reading start LSN. */
840     p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
841
842     return VLC_SUCCESS;
843 }
844
845 /* 
846  * Local variables:
847  *  mode: C
848  *  style: gnu
849  * End:
850  */