]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
de1d6c254d5a12f46b1f0a36b6b072d9e4fa075b
[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 int      CDDARead( access_t *, uint8_t *, int );
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    called by CDDAOpen
79 */
80 static void
81 cdio_log_handler (cdio_log_level_t level, const char message[])
82 {
83   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
84
85   if( p_cdda == NULL )
86       return;
87
88   switch (level) {
89   case CDIO_LOG_DEBUG:
90   case CDIO_LOG_INFO:
91     if (p_cdda->i_debug & INPUT_DBG_CDIO)
92       msg_Dbg( p_cdda_input, message);
93     break;
94   case CDIO_LOG_WARN:
95     msg_Warn( p_cdda_input, message);
96     break;
97   case CDIO_LOG_ERROR:
98   case CDIO_LOG_ASSERT:
99     msg_Err( p_cdda_input, message);
100     break;
101   default:
102     msg_Warn( p_cdda_input, message,
103             "The above message had unknown cdio log level",
104             level);
105   }
106   return;
107 }
108
109
110 #ifdef HAVE_LIBCDDB
111 /*! This routine is called by libcddb routines on error.
112    called by CDDAOpen
113 */
114 static void
115 cddb_log_handler (cddb_log_level_t level, const char message[])
116 {
117     cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
118     switch (level)
119     {
120         case CDDB_LOG_DEBUG:
121         case CDDB_LOG_INFO:
122         if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
123
124         /* Fall through if to warn case */
125         default:
126             cdio_log_handler (level, message);
127     }
128 }
129 #endif /*HAVE_LIBCDDB*/
130
131
132 /*! This routine is when vlc is not fully set up (before full initialization)
133   or is not around (before finalization).
134 */
135 static void
136 uninit_log_handler (cdio_log_level_t level, const char message[])
137 {
138     cdda_data_t *p_cdda = NULL;
139
140     if (p_cdda_input)
141         p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
142
143      switch (level)
144      {
145         case CDIO_LOG_DEBUG:
146         case CDIO_LOG_INFO:
147         if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
148             return;
149
150         /* Fall through if to warn case */
151         case CDIO_LOG_WARN:
152             fprintf(stderr, "WARN: %s\n", message);
153             break;
154         case CDIO_LOG_ERROR:
155             fprintf(stderr, "ERROR: %s\n", message);
156             break;
157         case CDIO_LOG_ASSERT:
158             fprintf(stderr, "ASSERT ERROR: %s\n", message);
159             break;
160         default:
161             fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n", message,
162                             "The above message had unknown cdio log level",
163                             level);
164     }
165
166     /* gl_default_cdio_log_handler (level, message); */
167 }
168
169 /* Only used in audio control mode. Gets the current LSN from the 
170    CD-ROM drive. */
171 static int64_t
172 get_current_pos ( access_t *p_access ) 
173 {
174     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
175     lsn_t i_offset;
176
177 #if LIBCDIO_VERSION_NUM >= 73
178     if (p_cdda->b_audio_ctl) 
179       {
180         cdio_subchannel_t sub;
181         CdIo_t *p_cdio = p_cdda->p_cdio;
182         if (DRIVER_OP_SUCCESS == cdio_audio_read_subchannel(p_cdio, &sub)) {
183           if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED ||
184               sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) 
185             return CDIO_INVALID_LSN;
186           
187           if ( ! p_cdda->b_nav_mode ) {
188             char *psz = cdio_msf_to_str(&sub.abs_addr);
189             // fprintf(stderr, "+++disk mode abs msf %s", psz);
190             free(psz);
191             i_offset = cdio_msf_to_lsn((&sub.abs_addr));
192             // fprintf(stderr, " frame offset %d\n", i_offset);
193           } else {
194             char *psz = cdio_msf_to_str(&sub.rel_addr);
195             // fprintf(stderr, "+++track abs msf %s", psz);
196             free(psz);
197             i_offset = cdio_msf_to_lsn((&sub.rel_addr));
198             // fprintf(stderr, " frame offset %d\n", i_offset);
199           }
200         } else {
201           // fprintf(stderr, "+++Error reading current pos\n");
202           i_offset = p_cdda->i_lsn;
203         }
204       } else
205         i_offset = p_cdda->i_lsn;
206 #else 
207     i_offset = p_cdda->i_lsn;
208   ;
209 #endif
210   return i_offset;
211 }
212
213 /*****************************************************************************
214  * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
215  * an allocated pointer to the data. NULL is returned if no data
216  * read. It is also possible if we haven't read a RIFF header in which
217  * case one that we creaded during Open/Initialization is returned.
218  *****************************************************************************/
219 static block_t * CDDAReadBlocks( access_t * p_access )
220 {
221     block_t     *p_block;
222     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
223     int          i_blocks = p_cdda->i_blocks_per_read;
224
225     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), 
226               "called i_lsn: %d i_pos: %lld, size: %lld",
227               p_cdda->i_lsn, p_access->info.i_pos, p_access->info.i_size);
228
229     /* Check end of file */
230     if( p_access->info.b_eof ) return NULL;
231
232     if( !p_cdda->b_header )
233       {
234         /* Return only the dummy RIFF header we created in Open/Init */
235         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
236         memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
237         p_cdda->b_header = VLC_TRUE;
238         return p_block;
239     }
240
241     /* Check end of track */
242     while( p_cdda->i_lsn > cdio_get_track_last_lsn(p_cdda->p_cdio, 
243                                                    p_cdda->i_track) )
244     {
245         if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
246         {
247             dbg_print( (INPUT_DBG_LSN), "EOF");
248             p_access->info.b_eof = VLC_TRUE;
249             return NULL;
250         }
251
252         p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_META;
253         p_access->info.i_title++;
254         p_cdda->i_track++;
255
256         if ( p_cdda-> b_nav_mode ) {
257           char *psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
258           input_Control( p_cdda->p_input, INPUT_SET_NAME, psz_title );
259           free(psz_title);
260         } else {
261           p_access->info.i_size =
262             p_cdda->p_title[p_access->info.i_title]->i_size;
263           p_access->info.i_pos = 0;
264           p_access->info.i_update |= INPUT_UPDATE_SIZE;
265         }
266     }
267
268     /* Possibly adjust i_blocks so we don't read past the end of a track. */
269     if( p_cdda->i_lsn + i_blocks >= 
270         cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track+1) )
271     {
272       i_blocks = cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track+1 ) 
273         - p_cdda->i_lsn;
274     }
275
276     /* Do the actual reading */
277     p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
278     if( !p_block)
279     {
280       msg_Err( p_access, "Cannot get a new block of size: %i",
281                i_blocks * CDIO_CD_FRAMESIZE_RAW );
282       return NULL;
283     }
284     
285     {
286 #if LIBCDIO_VERSION_NUM >= 72
287       driver_return_code_t rc = DRIVER_OP_SUCCESS;
288
289       if ( p_cdda->e_paranoia && p_cdda->paranoia ) 
290         {
291           int i;
292           for( i = 0; i < i_blocks; i++ )
293             {
294               int16_t *p_readbuf = cdio_paranoia_read(p_cdda->paranoia, NULL);
295               char *psz_err=cdio_cddap_errors(p_cdda->paranoia_cd);
296               char *psz_mes=cdio_cddap_messages(p_cdda->paranoia_cd);
297
298               if (psz_mes || psz_err)
299                 msg_Err( p_access, "%s%s\n", psz_mes ? psz_mes: "", 
300                         psz_err ? psz_err: "" );
301               
302               if (psz_err) free(psz_err);
303               if (psz_mes) free(psz_mes);
304               if( !p_readbuf ) {
305                 msg_Err( p_access, "paranoia read error on frame %i\n", 
306                          p_cdda->i_lsn+i );
307               } else 
308                 memcpy(p_block->p_buffer + i * CDIO_CD_FRAMESIZE_RAW, 
309                        p_readbuf, CDIO_CD_FRAMESIZE_RAW);
310             }
311         }
312       else 
313         rc = cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
314                                       p_cdda->i_lsn, i_blocks);
315 #else
316 #define DRIVER_OP_SUCCESS 0
317       int rc;
318       rc = cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
319                                     p_cdda->i_lsn, i_blocks);
320 #endif    
321       if( rc != DRIVER_OP_SUCCESS )
322         {
323           msg_Err( p_access, "could not read %d sectors starting from %lu",
324                    i_blocks, (long unsigned int) p_cdda->i_lsn );
325           block_Release( p_block );
326           
327           /* If we had problems above, assume the problem is with
328              the first sector of the read and set to skip it.  In
329              the future libcdio may have cdparanoia support.
330           */
331           p_cdda->i_lsn++;
332           p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
333           return NULL;
334         }
335     }
336
337     p_cdda->i_lsn        += i_blocks;
338     p_access->info.i_pos += i_blocks * CDIO_CD_FRAMESIZE_RAW;
339
340     return p_block;
341 }
342
343 /*****************************************************************************
344  * CDDARead: Handler for audio control reads the CD-DA.
345  *****************************************************************************/
346 static int
347 CDDARead( access_t * p_access, uint8_t *p_buffer, int i_len )
348 {
349     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
350
351     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), 
352               "called lsn: %d pos: %lld, size: %lld",
353               p_cdda->i_lsn, p_access->info.i_pos, p_access->info.i_size);
354
355     /* Check end of file */
356     if( p_access->info.b_eof ) return 0;
357     
358     {
359       lsn_t i_lsn = get_current_pos(p_access);
360       if (CDIO_INVALID_LSN == i_lsn) {
361         memset( p_buffer, 0, i_len );
362         return i_len;
363       }
364     
365       p_cdda->i_lsn = i_lsn;
366       p_access->info.i_pos = p_cdda->i_lsn * CDIO_CD_FRAMESIZE_RAW;
367     }
368     
369     dbg_print((INPUT_DBG_LSN), "updated lsn: %d", p_cdda->i_lsn);
370
371     /* Check end of track */
372     while( p_cdda->i_lsn > cdio_get_track_last_lsn(p_cdda->p_cdio, 
373                                                    p_cdda->i_track) )
374     {
375         if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
376         {
377             dbg_print( (INPUT_DBG_LSN), "EOF");
378             p_access->info.b_eof = VLC_TRUE;
379             return 0;
380         }
381
382         p_access->info.i_update |= INPUT_UPDATE_TITLE;
383         p_access->info.i_title++;
384         p_cdda->i_track++;
385
386         if ( p_cdda-> b_nav_mode ) {
387           char *psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
388           input_Control( p_cdda->p_input, INPUT_SET_NAME, psz_title );
389           free(psz_title);
390         } else {
391           p_access->info.i_size =
392             p_cdda->p_title[p_access->info.i_title]->i_size;
393           p_access->info.i_pos = 0;
394           p_access->info.i_update |= INPUT_UPDATE_SIZE;
395         }
396     }
397
398     memset( p_buffer, 0, i_len );
399     return i_len;
400 }
401
402 /*! Pause CD playing via audio control */
403 static bool
404 cdda_audio_pause(CdIo_t *p_cdio)
405 {
406   bool b_ok = true;
407 #if LIBCDIO_VERSION_NUM >= 73
408   cdio_subchannel_t sub;
409   if (DRIVER_OP_SUCCESS == cdio_audio_read_subchannel(p_cdio, &sub)) {
410     if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) {
411       b_ok = DRIVER_OP_SUCCESS == cdio_audio_pause(p_cdio);
412     }
413   } else
414     b_ok = false;
415 #endif
416   return b_ok;
417 }
418
419 #if LIBCDIO_VERSION_NUM >= 73
420 /*! play CD using audio controls */
421 static driver_return_code_t
422 cdda_audio_play(CdIo_t *p_cdio, lsn_t start_lsn, lsn_t end_lsn)
423 {
424   msf_t start_msf;
425   msf_t last_msf;
426   cdio_lsn_to_msf(start_lsn, &start_msf);
427   cdio_lsn_to_msf(end_lsn, &last_msf);
428   cdda_audio_pause(p_cdio);
429   return cdio_audio_play_msf (p_cdio, &start_msf, &last_msf);
430 }
431 #endif
432
433 /****************************************************************************
434  * CDDASeek - change position for subsequent reads. For example, this
435  * can happen if the user moves a position slider bar in a GUI.
436  ****************************************************************************/
437 static int 
438 CDDASeek( access_t * p_access, int64_t i_pos )
439 {
440     cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
441
442     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
443                "lsn %lu, offset: %lld",
444                (long unsigned int) p_cdda->i_lsn, i_pos );
445
446     p_cdda->i_lsn = (i_pos / CDIO_CD_FRAMESIZE_RAW);
447
448 #if LIBCDIO_VERSION_NUM >= 72
449     if ( p_cdda->e_paranoia && p_cdda->paranoia ) 
450       cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
451 #endif
452
453 #if LIBCDIO_VERSION_NUM >= 73
454     if ( p_cdda->b_audio_ctl ) {
455       track_t i_track = cdio_get_track(p_cdda->p_cdio, p_cdda->i_lsn);
456       lsn_t last_lsn;
457
458       if ( p_cdda->b_nav_mode )
459         last_lsn = p_cdda->i_lsn + cdio_get_track_sec_count(p_cdda->p_cdio, 
460                                                              i_track);
461       else 
462         last_lsn = p_cdda->i_lsn + 
463           cdio_get_track_lsn(p_cdda->p_cdio, CDIO_CDROM_LEADOUT_TRACK);
464
465       cdda_audio_play(p_cdda->p_cdio, p_cdda->i_lsn, last_lsn);
466     }
467 #endif 
468
469     if ( ! p_cdda->b_nav_mode ) 
470       p_cdda->i_lsn += cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
471
472     /* Seeked backwards and we are doing disc mode. */
473     if ( p_cdda->b_nav_mode && p_access->info.i_pos > i_pos ) {
474       track_t i_track;
475       char *psz_title;
476       
477       for( i_track = p_cdda->i_track; 
478            i_track > 1 && 
479              p_cdda->i_lsn < cdio_get_track_lsn(p_cdda->p_cdio, i_track);
480            i_track--, p_access->info.i_title-- ) ;
481
482       p_cdda->i_track = i_track;
483       p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_META ;
484       psz_title  = CDDAFormatTitle( p_access, p_cdda->i_track );
485       input_Control( p_cdda->p_input, INPUT_SET_NAME, 
486                      psz_title );
487       free(psz_title);
488
489     }
490     
491     p_access->info.i_pos = i_pos;
492
493     return VLC_SUCCESS;
494 }
495
496 /****************************************************************************
497  * Public functions
498  ****************************************************************************/
499
500 /*****************************************************************************
501  * Open: open cdda device or image file and initialize structures
502  *       for subsequent operations.
503  *****************************************************************************/
504 int 
505 CDDAOpen( vlc_object_t *p_this )
506 {
507     access_t    *p_access = (access_t*)p_this;
508     char *      psz_source = NULL;
509     cdda_data_t *p_cdda    = NULL;
510     CdIo_t      *p_cdio;
511     track_t     i_track = 1;
512     vlc_bool_t  b_single_track = false;
513     int         i_rc = VLC_EGENERIC;
514
515     p_access->p_sys = NULL;
516
517     /* Set where to log errors messages from libcdio. */
518     p_cdda_input = p_access;
519
520     /* parse the options passed in command line : */
521
522     if( p_access->psz_path && *p_access->psz_path )
523     {
524         char *psz_parser = psz_source = strdup( p_access->psz_path );
525
526         while( *psz_parser && *psz_parser != '@' )
527         {
528             psz_parser++;
529         }
530
531         if( *psz_parser == '@' )
532         {
533             /* Found options */
534             *psz_parser = '\0';
535             ++psz_parser;
536
537             if ('T' == *psz_parser || 't' == *psz_parser )
538             ++psz_parser;
539
540             i_track = (int)strtol( psz_parser, NULL, 10 );
541             i_track = i_track ? i_track : 1;
542             b_single_track = true;
543         }
544     }
545
546     if (!psz_source || !*psz_source)
547     {
548         /* No device/track given. Continue only when this plugin was
549            selected */
550         if( !p_this->b_force ) return VLC_EGENERIC;
551
552         psz_source = var_CreateGetString( p_this, "cd-audio" );
553
554         if( !psz_source || !*psz_source )
555         {
556             /* Scan for a CD-ROM drive with a CD-DA in it. */
557             char **ppsz_drives =
558               cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
559
560             if (NULL == ppsz_drives || NULL == ppsz_drives[0] )
561             {
562                 msg_Err( p_access,
563                      "libcdio couldn't find something with a CD-DA in it" );
564                 if (ppsz_drives) cdio_free_device_list(ppsz_drives);
565                 return VLC_EGENERIC;
566             }
567
568             psz_source = strdup(ppsz_drives[0]);
569             cdio_free_device_list(ppsz_drives);
570         }
571     }
572
573     cdio_log_set_handler ( cdio_log_handler );
574
575     /* Open CDDA */
576     if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
577     {
578         msg_Warn( p_access, "could not open %s", psz_source );
579         if (psz_source) free( psz_source );
580         return VLC_EGENERIC;
581     }
582
583     p_cdda = calloc( 1, sizeof(cdda_data_t) );
584     if( p_cdda == NULL )
585     {
586         msg_Err( p_access, "out of memory" );
587         free( psz_source );
588         return VLC_ENOMEM;
589     }
590
591 #ifdef HAVE_LIBCDDB
592     cddb_log_set_handler ( cddb_log_handler );
593     p_cdda->cddb.disc = NULL;
594     p_cdda->b_cddb_enabled =
595       config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
596 #endif
597
598     p_cdda->b_cdtext =
599       config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
600
601     p_cdda->b_cdtext_prefer =
602       config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
603
604 #if LIBCDIO_VERSION_NUM >= 73
605     p_cdda->b_audio_ctl =
606       config_GetInt( p_access, MODULE_STRING "-analog-output" );
607 #endif
608
609     p_cdda->psz_source = strdup(psz_source);
610     p_cdda->b_header   = VLC_FALSE;
611     p_cdda->p_cdio     = p_cdio;
612     p_cdda->i_tracks   = 0;
613     p_cdda->i_lsn      = 0;
614     p_cdda->i_titles   = 0;
615     p_cdda->i_track    = i_track;
616     p_cdda->i_debug    = config_GetInt(p_this, MODULE_STRING 
617                                        "-debug");
618     p_cdda->b_nav_mode = config_GetInt(p_this, MODULE_STRING 
619                                        "-navigation-mode" );
620     p_cdda->i_blocks_per_read
621       = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
622
623     p_cdda->p_input  = vlc_object_find( p_access, VLC_OBJECT_INPUT,
624                                         FIND_PARENT );
625
626     if (0 == p_cdda->i_blocks_per_read)
627         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
628
629     if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
630          || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
631     {
632         msg_Warn( p_cdda_input,
633                 "Number of blocks (%d) has to be between %d and %d. "
634                 "Using %d.",
635                 p_cdda->i_blocks_per_read,
636                 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
637                 DEFAULT_BLOCKS_PER_READ );
638         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
639     }
640
641     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
642
643     /* Set up p_access */
644     if (p_cdda->b_audio_ctl) 
645       {
646         p_access->pf_read  = CDDARead;
647         p_access->pf_block = NULL;
648       } else 
649       {
650         p_access->pf_read  = NULL;
651         p_access->pf_block = CDDAReadBlocks;
652       }
653     
654     p_access->pf_control = CDDAControl;
655     p_access->pf_seek    = CDDASeek;
656
657     {
658       lsn_t i_last_lsn;
659       
660       if (p_cdda->b_nav_mode)
661           i_last_lsn = cdio_get_track_lsn(p_cdio, CDIO_CDROM_LEADOUT_TRACK);
662       else 
663           i_last_lsn = cdio_get_track_last_lsn(p_cdio, i_track);
664       
665       if (CDIO_INVALID_LSN != i_last_lsn)
666         p_access->info.i_size = i_last_lsn * (uint64_t) CDIO_CD_FRAMESIZE_RAW;
667       else 
668         p_access->info.i_size = 0;
669     }
670
671     p_access->info.i_update    = 0;
672     p_access->info.b_eof       = VLC_FALSE;
673     p_access->info.i_title     = 0;
674     p_access->info.i_seekpoint = 0;
675
676     p_access->p_sys     = (access_sys_t *) p_cdda;
677
678     /* We read the Table Of Content information */
679     i_rc = CDDAInit( p_access, p_cdda );
680     if ( VLC_SUCCESS != i_rc ) goto error;
681
682     CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
683
684 #if LIBCDIO_VERSION_NUM >= 72
685     {
686    
687       char *psz_paranoia = config_GetPsz( p_access, 
688                                           MODULE_STRING "-paranoia" );
689       p_cdda->e_paranoia = paranoia_none;
690       if( psz_paranoia && *psz_paranoia )
691       {
692
693         if( !strncmp( psz_paranoia, "full", strlen("full") )  )
694           p_cdda->e_paranoia = paranoia_full;
695         else if( !strncmp( psz_paranoia, "overlap", strlen("overlap") )  )
696           p_cdda->e_paranoia = paranoia_overlap;
697         
698         /* Use CD Paranoia? */
699         if ( p_cdda->e_paranoia ) {
700           p_cdda->paranoia_cd = cdio_cddap_identify_cdio(p_cdio, 1, NULL);
701           /* We'll set for verbose paranoia messages. */
702           cdio_cddap_verbose_set(p_cdda->paranoia_cd, CDDA_MESSAGE_PRINTIT, 
703                                  CDDA_MESSAGE_PRINTIT);
704           if ( 0 != cdio_cddap_open(p_cdda->paranoia_cd) ) {
705             msg_Warn( p_cdda_input, "Unable to get paranoia support - "
706                       "continuing without it." );
707             p_cdda->e_paranoia = paranoia_none;
708           } else {
709             p_cdda->paranoia = cdio_paranoia_init(p_cdda->paranoia_cd);
710             cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
711             
712             /* Set reading mode for full or overlap paranoia, 
713                but allow skipping sectors. */
714             cdio_paranoia_modeset(p_cdda->paranoia,
715                                   paranoia_full == p_cdda->e_paranoia ?
716                                   PARANOIA_MODE_FULL^PARANOIA_MODE_NEVERSKIP :
717                                   PARANOIA_MODE_OVERLAP^PARANOIA_MODE_NEVERSKIP
718                                   );
719           }
720         }
721       }
722     }
723 #endif    
724
725     /* Build a WAV header to put in front of the output data.
726        This gets sent back in the Block (read) routine.
727      */
728     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
729     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
730     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
731     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
732     p_cdda->waveheader.Length = 0;                     /* we just don't know */
733     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
734     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
735     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
736     SetWLE( &p_cdda->waveheader.Modus, 2);
737     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
738     SetWLE( &p_cdda->waveheader.BytesPerSample,
739             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
740     SetDWLE( &p_cdda->waveheader.BytesPerSec,
741              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
742     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
743     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
744
745     /* PTS delay */
746     var_Create( p_access, MODULE_STRING "-caching",
747                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
748     vlc_object_release( p_cdda->p_input );
749     return VLC_SUCCESS;
750
751  error:
752     cdio_destroy( p_cdda->p_cdio );
753     if( psz_source) free( psz_source );
754     if( p_cdda ) {
755       if ( p_cdda->p_input )
756         vlc_object_release( p_cdda->p_input );
757       free(p_cdda);
758     }
759     
760     return i_rc;
761
762 }
763
764 /*****************************************************************************
765  * CDDAClose: closes cdda and frees any resources associded with it.
766  *****************************************************************************/
767 void 
768 CDDAClose (vlc_object_t *p_this )
769 {
770     access_t    *p_access = (access_t *) p_this;
771     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
772     track_t      i;
773
774 #if LIBCDIO_VERSION_NUM >= 73
775     if (p_cdda->b_audio_ctl)
776       cdio_audio_stop(p_cdda->p_cdio);
777 #endif
778
779     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
780
781     /* Remove playlist titles */
782     for( i = 0; i < p_cdda->i_titles; i++ )
783     {
784         vlc_input_title_Delete( p_cdda->p_title[i] );
785     }
786
787 #ifdef HAVE_LIBCDDB
788     cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
789     if (p_cdda->b_cddb_enabled)
790       cddb_disc_destroy(p_cdda->cddb.disc);
791 #endif
792
793     cdio_destroy( p_cdda->p_cdio );
794     cdio_log_set_handler (uninit_log_handler);
795
796 #if LIBCDIO_VERSION_NUM >= 72
797     if (p_cdda->paranoia)
798       cdio_paranoia_free(p_cdda->paranoia);
799     if (p_cdda->paranoia_cd) 
800       cdio_cddap_close_no_free_cdio(p_cdda->paranoia_cd);
801 #endif
802
803     if (p_cdda->psz_mcn)    free( p_cdda->psz_mcn );
804     if (p_cdda->psz_source) free( p_cdda->psz_source );
805
806 #if LIBCDDB_VERSION_NUM >= 1
807     libcddb_shutdown();
808 #endif
809     free( p_cdda );
810     p_cdda = NULL;
811     p_cdda_input = NULL;
812 }
813
814 /*****************************************************************************
815  * Control: The front-end or vlc engine calls here to ether get
816  * information such as meta information or plugin capabilities or to
817  * issue miscellaneous "set" requests.
818  *****************************************************************************/
819 static int CDDAControl( access_t *p_access, int i_query, va_list args )
820 {
821     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
822     int          *pi_int;
823     int i;
824
825     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
826                "query %d", i_query );
827
828     switch( i_query )
829     {
830         /* Pass back a copy of meta information that was gathered when we
831            during the Open/Initialize call.
832          */
833         case ACCESS_GET_META:
834         {
835             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
836             if ( p_cdda->p_meta )
837             {
838                 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
839                 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
840                 return VLC_SUCCESS;
841             }
842             else {
843                 msg_Warn( p_access, "tried to copy NULL meta info" );
844                 return VLC_EGENERIC;
845             }
846         }
847
848         case ACCESS_CAN_CONTROL_PACE:
849           {
850             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
851             dbg_print( INPUT_DBG_META, "can control pace");
852             *pb_bool = p_cdda->b_audio_ctl ? VLC_FALSE : VLC_TRUE;
853             return VLC_SUCCESS;
854           }
855             
856         case ACCESS_CAN_FASTSEEK:
857             dbg_print( INPUT_DBG_META, "can fast seek");
858             goto common;
859         case ACCESS_CAN_SEEK:
860             dbg_print( INPUT_DBG_META, "can seek");
861             goto common;
862         case ACCESS_CAN_PAUSE:
863             dbg_print( INPUT_DBG_META, "can pause");
864     common:
865         {
866             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
867             *pb_bool = VLC_TRUE;
868             return VLC_SUCCESS;
869         }
870
871         /* */
872         case ACCESS_GET_MTU:
873         {
874             pi_int = (int*)va_arg( args, int * );
875             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
876             dbg_print( INPUT_DBG_META, "Get MTU %d", *pi_int);
877             break;
878         }
879
880         case ACCESS_GET_PTS_DELAY:
881         {
882             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
883             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
884               * MILLISECONDS_PER_SEC;
885             break;
886         }
887
888         case ACCESS_GET_TITLE_INFO:
889         {
890             input_title_t ***ppp_title = 
891               (input_title_t***)va_arg( args, input_title_t*** );
892
893             pi_int    = (int*)va_arg( args, int* );
894             *((int*)va_arg( args, int* )) = 1; /* Title offset */
895
896             dbg_print ( INPUT_DBG_EVENT,
897                         "GET TITLE: i_tracks %d, i_tracks %d",
898                         p_cdda->i_tracks, p_cdda->i_tracks );
899
900             CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
901
902             if ( p_cdda->b_nav_mode) {
903                 char *psz_title = 
904                   CDDAFormatTitle( p_access, p_cdda->i_track );
905                 input_Control( p_cdda->p_input, INPUT_SET_NAME, 
906                                psz_title );
907                 free(psz_title);
908             }
909
910             /* Duplicate title info */
911             if( p_cdda->i_titles == 0 )
912             {
913                 *pi_int = 0; ppp_title = NULL;
914                 return VLC_SUCCESS;
915             }
916             *pi_int = p_cdda->i_titles;
917             *ppp_title = calloc(1, 
918                                 sizeof( input_title_t **) * p_cdda->i_titles );
919
920             if (!*ppp_title) return VLC_ENOMEM;
921
922             for( i = 0; i < p_cdda->i_titles; i++ )
923             {
924               if ( p_cdda->p_title[i] ) {
925                    (*ppp_title)[i] =
926                      vlc_input_title_Duplicate( p_cdda->p_title[i] );
927               }
928             }
929             break;
930         }
931
932         case ACCESS_SET_TITLE:
933         {
934             i = (int)va_arg( args, int );
935
936             dbg_print( INPUT_DBG_EVENT, "set title %d", i );
937             if( i != p_access->info.i_title )
938             {
939                 const track_t i_track = p_cdda->i_track + i;
940                 /* Update info */
941                 p_access->info.i_title = i;
942                 if ( p_cdda->b_nav_mode) 
943                 {
944                     lsn_t i_last_lsn;
945                     char *psz_title = CDDAFormatTitle( p_access, i_track );
946                     input_Control( p_cdda->p_input, INPUT_SET_NAME, 
947                                    psz_title );
948                     free(psz_title);
949                     p_cdda->i_track = i_track;
950                     i_last_lsn = cdio_get_track_lsn(p_cdda->p_cdio, 
951                                                     CDIO_CDROM_LEADOUT_TRACK);
952                     if (CDIO_INVALID_LSN != i_last_lsn)
953                       p_access->info.i_size = (int64_t) CDIO_CD_FRAMESIZE_RAW 
954                         * i_last_lsn ;
955                     p_access->info.i_pos = (int64_t)
956                       cdio_get_track_lsn( p_cdda->p_cdio, i_track ) 
957                       * CDIO_CD_FRAMESIZE_RAW;
958                 } else {
959                    p_access->info.i_size = p_cdda->p_title[i]->i_size;
960                    p_access->info.i_pos  = 0;
961                 }
962                 p_access->info.i_update = 
963                   INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
964
965                 /* Next sector to read */
966                 p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, i_track);
967             }
968             break;
969         }
970
971         case ACCESS_SET_PAUSE_STATE:
972           dbg_print( INPUT_DBG_META, "Pause");
973           if (p_cdda->b_audio_ctl)
974             cdda_audio_pause(p_cdda->p_cdio);
975           break;
976
977         case ACCESS_SET_SEEKPOINT:
978         case ACCESS_SET_PRIVATE_ID_STATE:
979             dbg_print( INPUT_DBG_META, "set seekpoint/set private id state");
980             return VLC_EGENERIC;
981
982         default:
983             msg_Warn( p_access, "unimplemented query in control" );
984             return VLC_EGENERIC;
985
986     }
987     return VLC_SUCCESS;
988 }
989
990 /*****************************************************************************
991   CDDAInit:
992
993  Initialize information pertaining to the CD: the number of tracks,
994  first track number, LSNs for each track and the leadout. The leadout
995  information is stored after the last track. The LSN array is
996  0-origin, same as p_access->info.  Add first_track to get what track
997  number this is on the CD. Note: libcdio uses the real track number.
998
999  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
1000
1001  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
1002  *****************************************************************************/
1003 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
1004 {
1005     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
1006
1007     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
1008     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
1009
1010     discmode = cdio_get_discmode(p_cdda->p_cdio);
1011     switch(discmode) {
1012     case CDIO_DISC_MODE_CD_DA:
1013     case CDIO_DISC_MODE_CD_MIXED:
1014         /* These are possible for CD-DA */
1015         break;
1016     default:
1017         /* These are not possible for CD-DA */
1018         msg_Err( p_access,
1019                "Disc seems not to be CD-DA. libcdio reports it is %s",
1020                discmode2str[discmode]
1021                );
1022         return VLC_EGENERIC;
1023     }
1024
1025     /* Set reading start LSN. */
1026     p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
1027
1028     return VLC_SUCCESS;
1029 }
1030
1031 /* 
1032  * Local variables:
1033  *  mode: C
1034  *  style: gnu
1035  * End:
1036  */