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