]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
configure.ac: reinstate libcdio CD-DA and VCD plugins (on demand)
[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 #include <cdio/cdda.h>
39
40 #include <stdio.h>
41
42 /* #ifdef variables below are defined via config.h via #include vlc above. */
43 #ifdef HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif
46
47 #ifdef HAVE_SYS_TYPES_H
48 #include <sys/types.h>
49 #endif
50
51 #ifdef HAVE_STRING_H
52 #include <string.h>
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #   include <unistd.h>
57 #endif
58
59 /* FIXME: This variable is a hack. Would be nice to eliminate. */
60 access_t *p_cdda_input = NULL;
61
62 /*****************************************************************************
63  * Local prototypes
64  *****************************************************************************/
65 static block_t *CDDAReadBlocks( access_t * p_access );
66 static int      CDDASeek( access_t * p_access, int64_t i_pos );
67 static int      CDDAControl( access_t *p_access, int i_query,
68                              va_list args );
69
70 static int      CDDAInit( access_t *p_access, cdda_data_t *p_cdda ) ;
71
72
73 /****************************************************************************
74  * Private functions
75  ****************************************************************************/
76
77 /* process messages that originate from libcdio. */
78 static void
79 cdio_log_handler (cdio_log_level_t level, const char message[])
80 {
81   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
82
83   if( p_cdda == NULL )
84       return;
85
86   switch (level) {
87   case CDIO_LOG_DEBUG:
88   case CDIO_LOG_INFO:
89     if (p_cdda->i_debug & INPUT_DBG_CDIO)
90       msg_Dbg( p_cdda_input, message);
91     break;
92   case CDIO_LOG_WARN:
93     msg_Warn( p_cdda_input, message);
94     break;
95   case CDIO_LOG_ERROR:
96   case CDIO_LOG_ASSERT:
97     msg_Err( p_cdda_input, message);
98     break;
99   default:
100     msg_Warn( p_cdda_input, message,
101             "The above message had unknown cdio log level",
102             level);
103   }
104   return;
105 }
106
107
108 #ifdef HAVE_LIBCDDB
109 /*! This routine is called by libcddb routines on error.
110    Setup is done by init_input_plugin.
111 */
112 static void
113 cddb_log_handler (cddb_log_level_t level, const char message[])
114 {
115     cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
116     switch (level)
117     {
118         case CDDB_LOG_DEBUG:
119         case CDDB_LOG_INFO:
120         if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
121
122         /* Fall through if to warn case */
123         default:
124             cdio_log_handler (level, message);
125     }
126 }
127 #endif /*HAVE_LIBCDDB*/
128
129
130 /*! This routine is when xine is not fully set up (before full initialization)
131    or is not around (before finalization).
132 */
133 static void
134 uninit_log_handler (cdio_log_level_t level, const char message[])
135 {
136     cdda_data_t *p_cdda = NULL;
137
138     if (p_cdda_input)
139         p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
140
141      switch (level)
142      {
143         case CDIO_LOG_DEBUG:
144         case CDIO_LOG_INFO:
145         if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
146             return;
147
148         /* Fall through if to warn case */
149         case CDIO_LOG_WARN:
150             fprintf(stderr, "WARN: %s\n", message);
151             break;
152         case CDIO_LOG_ERROR:
153             fprintf(stderr, "ERROR: %s\n", message);
154             break;
155         case CDIO_LOG_ASSERT:
156             fprintf(stderr, "ASSERT ERROR: %s\n", message);
157             break;
158         default:
159             fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n", message,
160                             "The above message had unknown cdio log level",
161                             level);
162     }
163
164     /* gl_default_cdio_log_handler (level, message); */
165 }
166
167 /*****************************************************************************
168  * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
169  * an allocated pointer to the data. NULL is returned if no data
170  * read. It is also possible if we haven't read a RIFF header in which
171  * case one that we creaded during Open/Initialization is returned.
172  *****************************************************************************/
173 static block_t * CDDAReadBlocks( access_t * p_access )
174 {
175     block_t     *p_block;
176     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
177     int          i_blocks = p_cdda->i_blocks_per_read;
178
179     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d",
180               p_cdda->i_lsn);
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_lsn(p_cdda->p_cdio, 
196                                                p_cdda->i_track+1) )
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->b_paranoia_enabled ) 
242         {
243           int i;
244           for( i = 0; i < i_blocks; i++ )
245             {
246               int16_t *p_readbuf=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 + i * CDIO_CD_FRAMESIZE_RAW, p_readbuf, 
261                        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 += p_block->i_buffer;
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->b_paranoia_enabled ) 
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_enabled =
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     p_access->info.i_size      = 0;
488
489     p_access->info.i_update    = 0;
490     p_access->info.b_eof       = VLC_FALSE;
491     p_access->info.i_title     = 0;
492     p_access->info.i_seekpoint = 0;
493
494     p_access->p_sys     = (access_sys_t *) p_cdda;
495
496     /* We read the Table Of Content information */
497     i_rc = CDDAInit( p_access, p_cdda );
498     if ( VLC_SUCCESS != i_rc ) goto error;
499
500     CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
501
502 #if LIBCDIO_VERSION_NUM >= 72
503     p_cdda->b_paranoia_enabled =
504       config_GetInt( p_access, MODULE_STRING "-paranoia-enabled" );
505
506     /* Use CD Paranoia? */
507     if ( p_cdda->b_paranoia_enabled ) {
508       p_cdda->paranoia_cd = cdio_cddap_identify_cdio(p_cdio, 1, NULL);
509       /* We'll set for verbose paranoia messages. */
510       cdio_cddap_verbose_set(p_cdda->paranoia_cd, CDDA_MESSAGE_PRINTIT, 
511                             CDDA_MESSAGE_PRINTIT);
512       if ( 0 != cdio_cddap_open(p_cdda->paranoia_cd) ) {
513         msg_Warn( p_cdda_input, "Unable to get paranoia support - "
514                   "continuing without it." );
515         p_cdda->b_paranoia_enabled = VLC_FALSE;
516       } else {
517         p_cdda->paranoia = cdio_paranoia_init(p_cdda->paranoia_cd);
518         cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
519
520         /* Set reading mode for full paranoia, but allow skipping sectors. */
521         cdio_paranoia_modeset(p_cdda->paranoia, 
522                               PARANOIA_MODE_FULL^PARANOIA_MODE_NEVERSKIP);
523       }
524       
525     }
526 #endif    
527
528     /* Build a WAV header to put in front of the output data.
529        This gets sent back in the Block (read) routine.
530      */
531     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
532     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
533     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
534     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
535     p_cdda->waveheader.Length = 0;                     /* we just don't know */
536     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
537     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
538     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
539     SetWLE( &p_cdda->waveheader.Modus, 2);
540     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
541     SetWLE( &p_cdda->waveheader.BytesPerSample,
542             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
543     SetDWLE( &p_cdda->waveheader.BytesPerSec,
544              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
545     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
546     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
547
548     /* PTS delay */
549     var_Create( p_access, MODULE_STRING "-caching",
550                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
551     vlc_object_release( p_cdda->p_input );
552     return VLC_SUCCESS;
553
554  error:
555     cdio_destroy( p_cdda->p_cdio );
556     if( psz_source) free( psz_source );
557     if( p_cdda ) {
558       if ( p_cdda->p_input )
559         vlc_object_release( p_cdda->p_input );
560       free(p_cdda);
561     }
562     
563     return i_rc;
564
565 }
566
567 /*****************************************************************************
568  * CDDAClose: closes cdda and frees any resources associded with it.
569  *****************************************************************************/
570 void 
571 CDDAClose (vlc_object_t *p_this )
572 {
573     access_t    *p_access = (access_t *) p_this;
574     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
575     track_t      i;
576
577     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
578
579     /* Remove playlist titles */
580     for( i = 0; i < p_cdda->i_titles; i++ )
581     {
582         vlc_input_title_Delete( p_cdda->p_title[i] );
583     }
584
585 #ifdef HAVE_LIBCDDB
586     cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
587     if (p_cdda->b_cddb_enabled)
588       cddb_disc_destroy(p_cdda->cddb.disc);
589 #endif
590
591     cdio_destroy( p_cdda->p_cdio );
592     cdio_log_set_handler (uninit_log_handler);
593
594 #if LIBCDIO_VERSION_NUM >= 72
595     if (p_cdda->paranoia)
596       cdio_paranoia_free(p_cdda->paranoia);
597     if (p_cdda->paranoia_cd) 
598       cdio_cddap_close_no_free_cdio(p_cdda->paranoia_cd);
599 #endif
600
601     if (p_cdda->psz_mcn)    free( p_cdda->psz_mcn );
602     if (p_cdda->psz_source) free( p_cdda->psz_source );
603     free( p_cdda );
604     p_cdda = NULL;
605     p_cdda_input = NULL;
606 }
607
608 /*****************************************************************************
609  * Control: The front-end or vlc engine calls here to ether get
610  * information such as meta information or plugin capabilities or to
611  * issue miscellaneous "set" requests.
612  *****************************************************************************/
613 static int CDDAControl( access_t *p_access, int i_query, va_list args )
614 {
615     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
616     int          *pi_int;
617     int i;
618
619     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
620                "query %d", i_query );
621
622     switch( i_query )
623     {
624         /* Pass back a copy of meta information that was gathered when we
625            during the Open/Initialize call.
626          */
627         case ACCESS_GET_META:
628         {
629             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
630             if ( p_cdda->p_meta )
631             {
632                 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
633                 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
634                 return VLC_SUCCESS;
635             }
636             else {
637                 msg_Warn( p_access, "tried to copy NULL meta info" );
638                 return VLC_EGENERIC;
639             }
640         }
641
642         case ACCESS_CAN_SEEK:
643         case ACCESS_CAN_FASTSEEK:
644         case ACCESS_CAN_PAUSE:
645         case ACCESS_CAN_CONTROL_PACE:
646         {
647             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
648             *pb_bool = VLC_TRUE;
649             return VLC_SUCCESS;;
650         }
651
652         /* */
653         case ACCESS_GET_MTU:
654         {
655             pi_int = (int*)va_arg( args, int * );
656             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
657             break;
658         }
659
660         case ACCESS_GET_PTS_DELAY:
661         {
662             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
663             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
664               * MILLISECONDS_PER_SEC;
665             break;
666         }
667
668         /* */
669         case ACCESS_SET_PAUSE_STATE:
670             break;
671
672         case ACCESS_GET_TITLE_INFO:
673         {
674             input_title_t ***ppp_title = 
675               (input_title_t***)va_arg( args, input_title_t*** );
676
677             pi_int    = (int*)va_arg( args, int* );
678             *((int*)va_arg( args, int* )) = 1; /* Title offset */
679
680             dbg_print ( INPUT_DBG_EVENT,
681                         "GET TITLE: i_tracks %d, i_tracks %d",
682                         p_cdda->i_tracks, p_cdda->i_tracks );
683
684             CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
685
686             if ( p_cdda->b_nav_mode) {
687                 char *psz_title = 
688                   CDDAFormatTitle( p_access, p_cdda->i_track );
689                 input_Control( p_cdda->p_input, INPUT_SET_NAME, 
690                                psz_title );
691                 free(psz_title);
692             }
693
694             /* Duplicate title info */
695             if( p_cdda->i_titles == 0 )
696             {
697                 *pi_int = 0; ppp_title = NULL;
698                 return VLC_SUCCESS;
699             }
700             *pi_int = p_cdda->i_titles;
701             *ppp_title = calloc(1, 
702                                 sizeof( input_title_t **) * p_cdda->i_titles );
703
704             if (!*ppp_title) return VLC_ENOMEM;
705
706             for( i = 0; i < p_cdda->i_titles; i++ )
707             {
708               if ( p_cdda->p_title[i] ) {
709                    (*ppp_title)[i] =
710                      vlc_input_title_Duplicate( p_cdda->p_title[i] );
711               }
712             }
713             break;
714         }
715
716         case ACCESS_SET_TITLE:
717         {
718             i = (int)va_arg( args, int );
719
720             dbg_print( INPUT_DBG_EVENT, "set title %d", i );
721             if( i != p_access->info.i_title )
722             {
723                 /* Update info */
724                 p_access->info.i_update |= INPUT_UPDATE_TITLE;
725                 p_access->info.i_title = i;
726                 if ( p_cdda->b_nav_mode) {
727                     char *psz_title = 
728                       CDDAFormatTitle( p_access, i+1 );
729                     input_Control( p_cdda->p_input, INPUT_SET_NAME, 
730                                    psz_title );
731                     free(psz_title);
732                     p_cdda->i_track = i+1;
733                     p_access->info.i_pos = 
734                       cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track ) 
735                       * CDIO_CD_FRAMESIZE_RAW;
736                 } else {
737                    p_access->info.i_update |= INPUT_UPDATE_SIZE;
738                    p_access->info.i_size = p_cdda->p_title[i]->i_size;
739                    p_access->info.i_pos = 0;
740                 }
741
742                 /* Next sector to read */
743                 p_cdda->i_lsn = 
744                   cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
745             }
746             break;
747         }
748
749         case ACCESS_SET_SEEKPOINT:
750         case ACCESS_SET_PRIVATE_ID_STATE:
751             return VLC_EGENERIC;
752
753         default:
754             msg_Warn( p_access, "unimplemented query in control" );
755             return VLC_EGENERIC;
756
757     }
758     return VLC_SUCCESS;
759 }
760
761 /*****************************************************************************
762   CDDAInit:
763
764  Initialize information pertaining to the CD: the number of tracks,
765  first track number, LSNs for each track and the leadout. The leadout
766  information is stored after the last track. The LSN array is
767  0-origin, same as p_access->info.  Add first_track to get what track
768  number this is on the CD. Note: libcdio uses the real track number.
769
770  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
771
772  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
773  *****************************************************************************/
774 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
775 {
776     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
777
778     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
779     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
780
781     discmode = cdio_get_discmode(p_cdda->p_cdio);
782     switch(discmode) {
783     case CDIO_DISC_MODE_CD_DA:
784     case CDIO_DISC_MODE_CD_MIXED:
785         /* These are possible for CD-DA */
786         break;
787     default:
788         /* These are not possible for CD-DA */
789         msg_Err( p_access,
790                "Disc seems not to be CD-DA. libcdio reports it is %s",
791                discmode2str[discmode]
792                );
793         return VLC_EGENERIC;
794     }
795
796     /* Set reading start LSN. */
797     p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
798
799     return VLC_SUCCESS;
800 }
801
802 /* 
803  * Local variables:
804  *  mode: C
805  *  style: gnu
806  * End:
807  */