]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
A couple more fixes. One to set meta information when in disc mode.
[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 #if LIBCDIO_VERSION_NUM >= 73
403 /*! Pause CD playing via audio control */
404 static bool
405 cdda_audio_pause(CdIo_t *p_cdio)
406 {
407   bool b_ok = true;
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   return b_ok;
416 }
417
418 /*! play CD using audio controls */
419 static driver_return_code_t
420 cdda_audio_play(CdIo_t *p_cdio, lsn_t start_lsn, lsn_t end_lsn)
421 {
422   msf_t start_msf;
423   msf_t last_msf;
424   cdio_lsn_to_msf(start_lsn, &start_msf);
425   cdio_lsn_to_msf(end_lsn, &last_msf);
426   cdda_audio_pause(p_cdio);
427   return cdio_audio_play_msf (p_cdio, &start_msf, &last_msf);
428 }
429 #endif
430
431 /****************************************************************************
432  * CDDASeek - change position for subsequent reads. For example, this
433  * can happen if the user moves a position slider bar in a GUI.
434  ****************************************************************************/
435 static int 
436 CDDASeek( access_t * p_access, int64_t i_pos )
437 {
438     cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
439
440     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
441                "lsn %lu, offset: %lld",
442                (long unsigned int) p_cdda->i_lsn, i_pos );
443
444     p_cdda->i_lsn = (i_pos / CDIO_CD_FRAMESIZE_RAW);
445
446 #if LIBCDIO_VERSION_NUM >= 72
447     if ( p_cdda->e_paranoia && p_cdda->paranoia ) 
448       cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
449 #endif
450
451 #if LIBCDIO_VERSION_NUM >= 73
452     if ( p_cdda->b_audio_ctl ) {
453       track_t i_track = cdio_get_track(p_cdda->p_cdio, p_cdda->i_lsn);
454       lsn_t last_lsn;
455
456       if ( p_cdda->b_nav_mode )
457         last_lsn = p_cdda->i_lsn + cdio_get_track_sec_count(p_cdda->p_cdio, 
458                                                              i_track);
459       else 
460         last_lsn = p_cdda->i_lsn + 
461           cdio_get_track_lsn(p_cdda->p_cdio, CDIO_CDROM_LEADOUT_TRACK);
462
463       cdda_audio_play(p_cdda->p_cdio, p_cdda->i_lsn, last_lsn);
464     }
465 #endif 
466
467     if ( ! p_cdda->b_nav_mode ) 
468       p_cdda->i_lsn += cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
469
470     /* Seeked backwards and we are doing disc mode. */
471     if ( p_cdda->b_nav_mode && p_access->info.i_pos > i_pos ) {
472       track_t i_track;
473       char *psz_title;
474       
475       for( i_track = p_cdda->i_track; 
476            i_track > 1 && 
477              p_cdda->i_lsn < cdio_get_track_lsn(p_cdda->p_cdio, i_track);
478            i_track--, p_access->info.i_title-- ) ;
479
480       p_cdda->i_track = i_track;
481       p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_META ;
482       psz_title  = CDDAFormatTitle( p_access, p_cdda->i_track );
483       input_Control( p_cdda->p_input, INPUT_SET_NAME, 
484                      psz_title );
485       free(psz_title);
486
487     }
488     
489     p_access->info.i_pos = i_pos;
490
491     return VLC_SUCCESS;
492 }
493
494 /****************************************************************************
495  * Public functions
496  ****************************************************************************/
497
498 /*****************************************************************************
499  * Open: open cdda device or image file and initialize structures
500  *       for subsequent operations.
501  *****************************************************************************/
502 int 
503 CDDAOpen( vlc_object_t *p_this )
504 {
505     access_t    *p_access = (access_t*)p_this;
506     char *      psz_source = NULL;
507     cdda_data_t *p_cdda    = NULL;
508     CdIo_t      *p_cdio;
509     track_t     i_track = 1;
510     vlc_bool_t  b_single_track = false;
511     int         i_rc = VLC_EGENERIC;
512
513     p_access->p_sys = NULL;
514
515     /* Set where to log errors messages from libcdio. */
516     p_cdda_input = p_access;
517
518     /* parse the options passed in command line : */
519
520     if( p_access->psz_path && *p_access->psz_path )
521     {
522         char *psz_parser = psz_source = strdup( p_access->psz_path );
523
524         while( *psz_parser && *psz_parser != '@' )
525         {
526             psz_parser++;
527         }
528
529         if( *psz_parser == '@' )
530         {
531             /* Found options */
532             *psz_parser = '\0';
533             ++psz_parser;
534
535             if ('T' == *psz_parser || 't' == *psz_parser )
536             ++psz_parser;
537
538             i_track = (int)strtol( psz_parser, NULL, 10 );
539             i_track = i_track ? i_track : 1;
540             b_single_track = true;
541         }
542     }
543
544     if (!psz_source || !*psz_source)
545     {
546         /* No device/track given. Continue only when this plugin was
547            selected */
548         if( !p_this->b_force ) return VLC_EGENERIC;
549
550         psz_source = var_CreateGetString( p_this, "cd-audio" );
551
552         if( !psz_source || !*psz_source )
553         {
554             /* Scan for a CD-ROM drive with a CD-DA in it. */
555             char **ppsz_drives =
556               cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
557
558             if (NULL == ppsz_drives || NULL == ppsz_drives[0] )
559             {
560                 msg_Err( p_access,
561                      "libcdio couldn't find something with a CD-DA in it" );
562                 if (ppsz_drives) cdio_free_device_list(ppsz_drives);
563                 return VLC_EGENERIC;
564             }
565
566             psz_source = strdup(ppsz_drives[0]);
567             cdio_free_device_list(ppsz_drives);
568         }
569     }
570
571     cdio_log_set_handler ( cdio_log_handler );
572
573     /* Open CDDA */
574     if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
575     {
576         msg_Warn( p_access, "could not open %s", psz_source );
577         if (psz_source) free( psz_source );
578         return VLC_EGENERIC;
579     }
580
581     p_cdda = calloc( 1, sizeof(cdda_data_t) );
582     if( p_cdda == NULL )
583     {
584         msg_Err( p_access, "out of memory" );
585         free( psz_source );
586         return VLC_ENOMEM;
587     }
588
589 #ifdef HAVE_LIBCDDB
590     cddb_log_set_handler ( cddb_log_handler );
591     p_cdda->cddb.disc = NULL;
592     p_cdda->b_cddb_enabled =
593       config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
594 #endif
595
596     p_cdda->b_cdtext =
597       config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
598
599     p_cdda->b_cdtext_prefer =
600       config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
601
602 #if LIBCDIO_VERSION_NUM >= 73
603     p_cdda->b_audio_ctl =
604       config_GetInt( p_access, MODULE_STRING "-analog-output" );
605 #endif
606
607     p_cdda->psz_source = strdup(psz_source);
608     p_cdda->b_header   = VLC_FALSE;
609     p_cdda->p_cdio     = p_cdio;
610     p_cdda->i_tracks   = 0;
611     p_cdda->i_lsn      = 0;
612     p_cdda->i_titles   = 0;
613     p_cdda->i_track    = i_track;
614     p_cdda->i_debug    = config_GetInt(p_this, MODULE_STRING 
615                                        "-debug");
616     p_cdda->b_nav_mode = config_GetInt(p_this, MODULE_STRING 
617                                        "-navigation-mode" );
618     p_cdda->i_blocks_per_read
619       = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
620
621     p_cdda->p_input  = vlc_object_find( p_access, VLC_OBJECT_INPUT,
622                                         FIND_PARENT );
623
624     if (0 == p_cdda->i_blocks_per_read)
625         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
626
627     if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
628          || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
629     {
630         msg_Warn( p_cdda_input,
631                 "Number of blocks (%d) has to be between %d and %d. "
632                 "Using %d.",
633                 p_cdda->i_blocks_per_read,
634                 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
635                 DEFAULT_BLOCKS_PER_READ );
636         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
637     }
638
639     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
640
641     /* Set up p_access */
642     if (p_cdda->b_audio_ctl) 
643       {
644         p_access->pf_read  = CDDARead;
645         p_access->pf_block = NULL;
646       } else 
647       {
648         p_access->pf_read  = NULL;
649         p_access->pf_block = CDDAReadBlocks;
650       }
651     
652     p_access->pf_control = CDDAControl;
653     p_access->pf_seek    = CDDASeek;
654
655     {
656       lsn_t i_last_lsn;
657       
658       if (p_cdda->b_nav_mode)
659           i_last_lsn = cdio_get_track_lsn(p_cdio, CDIO_CDROM_LEADOUT_TRACK);
660       else 
661           i_last_lsn = cdio_get_track_last_lsn(p_cdio, i_track);
662       
663       if (CDIO_INVALID_LSN != i_last_lsn)
664         p_access->info.i_size = i_last_lsn * (uint64_t) CDIO_CD_FRAMESIZE_RAW;
665       else 
666         p_access->info.i_size = 0;
667     }
668
669     p_access->info.i_update    = 0;
670     p_access->info.b_eof       = VLC_FALSE;
671     p_access->info.i_title     = 0;
672     p_access->info.i_seekpoint = 0;
673
674     p_access->p_sys     = (access_sys_t *) p_cdda;
675
676     /* We read the Table Of Content information */
677     i_rc = CDDAInit( p_access, p_cdda );
678     if ( VLC_SUCCESS != i_rc ) goto error;
679
680     CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
681
682 #if LIBCDIO_VERSION_NUM >= 72
683     {
684    
685       char *psz_paranoia = config_GetPsz( p_access, 
686                                           MODULE_STRING "-paranoia" );
687       p_cdda->e_paranoia = paranoia_none;
688       if( psz_paranoia && *psz_paranoia )
689       {
690
691         if( !strncmp( psz_paranoia, "full", strlen("full") )  )
692           p_cdda->e_paranoia = paranoia_full;
693         else if( !strncmp( psz_paranoia, "overlap", strlen("overlap") )  )
694           p_cdda->e_paranoia = paranoia_overlap;
695         
696         /* Use CD Paranoia? */
697         if ( p_cdda->e_paranoia ) {
698           p_cdda->paranoia_cd = cdio_cddap_identify_cdio(p_cdio, 1, NULL);
699           /* We'll set for verbose paranoia messages. */
700           cdio_cddap_verbose_set(p_cdda->paranoia_cd, CDDA_MESSAGE_PRINTIT, 
701                                  CDDA_MESSAGE_PRINTIT);
702           if ( 0 != cdio_cddap_open(p_cdda->paranoia_cd) ) {
703             msg_Warn( p_cdda_input, "Unable to get paranoia support - "
704                       "continuing without it." );
705             p_cdda->e_paranoia = paranoia_none;
706           } else {
707             p_cdda->paranoia = cdio_paranoia_init(p_cdda->paranoia_cd);
708             cdio_paranoia_seek(p_cdda->paranoia, p_cdda->i_lsn, SEEK_SET);
709             
710             /* Set reading mode for full or overlap paranoia, 
711                but allow skipping sectors. */
712             cdio_paranoia_modeset(p_cdda->paranoia,
713                                   paranoia_full == p_cdda->e_paranoia ?
714                                   PARANOIA_MODE_FULL^PARANOIA_MODE_NEVERSKIP :
715                                   PARANOIA_MODE_OVERLAP^PARANOIA_MODE_NEVERSKIP
716                                   );
717           }
718         }
719       }
720     }
721 #endif    
722
723     /* Build a WAV header to put in front of the output data.
724        This gets sent back in the Block (read) routine.
725      */
726     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
727     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
728     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
729     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
730     p_cdda->waveheader.Length = 0;                     /* we just don't know */
731     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
732     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
733     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
734     SetWLE( &p_cdda->waveheader.Modus, 2);
735     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
736     SetWLE( &p_cdda->waveheader.BytesPerSample,
737             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
738     SetDWLE( &p_cdda->waveheader.BytesPerSec,
739              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
740     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
741     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
742
743     /* PTS delay */
744     var_Create( p_access, MODULE_STRING "-caching",
745                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
746     vlc_object_release( p_cdda->p_input );
747     return VLC_SUCCESS;
748
749  error:
750     cdio_destroy( p_cdda->p_cdio );
751     if( psz_source) free( psz_source );
752     if( p_cdda ) {
753       if ( p_cdda->p_input )
754         vlc_object_release( p_cdda->p_input );
755       free(p_cdda);
756     }
757     
758     return i_rc;
759
760 }
761
762 /*****************************************************************************
763  * CDDAClose: closes cdda and frees any resources associded with it.
764  *****************************************************************************/
765 void 
766 CDDAClose (vlc_object_t *p_this )
767 {
768     access_t    *p_access = (access_t *) p_this;
769     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
770     track_t      i;
771
772     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
773
774     /* Remove playlist titles */
775     for( i = 0; i < p_cdda->i_titles; i++ )
776     {
777         vlc_input_title_Delete( p_cdda->p_title[i] );
778     }
779
780 #ifdef HAVE_LIBCDDB
781     cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
782     if (p_cdda->b_cddb_enabled)
783       cddb_disc_destroy(p_cdda->cddb.disc);
784 #endif
785
786     cdio_destroy( p_cdda->p_cdio );
787     cdio_log_set_handler (uninit_log_handler);
788
789 #if LIBCDIO_VERSION_NUM >= 72
790     if (p_cdda->paranoia)
791       cdio_paranoia_free(p_cdda->paranoia);
792     if (p_cdda->paranoia_cd) 
793       cdio_cddap_close_no_free_cdio(p_cdda->paranoia_cd);
794 #endif
795
796     if (p_cdda->psz_mcn)    free( p_cdda->psz_mcn );
797     if (p_cdda->psz_source) free( p_cdda->psz_source );
798
799 #if LIBCDDB_VERSION_NUM >= 1
800     libcddb_shutdown();
801 #endif
802     free( p_cdda );
803     p_cdda = NULL;
804     p_cdda_input = NULL;
805 }
806
807 /*****************************************************************************
808  * Control: The front-end or vlc engine calls here to ether get
809  * information such as meta information or plugin capabilities or to
810  * issue miscellaneous "set" requests.
811  *****************************************************************************/
812 static int CDDAControl( access_t *p_access, int i_query, va_list args )
813 {
814     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
815     int          *pi_int;
816     int i;
817
818     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
819                "query %d", i_query );
820
821     switch( i_query )
822     {
823         /* Pass back a copy of meta information that was gathered when we
824            during the Open/Initialize call.
825          */
826         case ACCESS_GET_META:
827         {
828             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
829             if ( p_cdda->p_meta )
830             {
831                 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
832                 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
833                 return VLC_SUCCESS;
834             }
835             else {
836                 msg_Warn( p_access, "tried to copy NULL meta info" );
837                 return VLC_EGENERIC;
838             }
839         }
840
841         case ACCESS_CAN_CONTROL_PACE:
842           {
843             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
844             dbg_print( INPUT_DBG_META, "can control pace");
845             *pb_bool = p_cdda->b_audio_ctl ? VLC_FALSE : VLC_TRUE;
846             return VLC_SUCCESS;
847           }
848             
849         case ACCESS_CAN_FASTSEEK:
850             dbg_print( INPUT_DBG_META, "can fast seek");
851             goto common;
852         case ACCESS_CAN_SEEK:
853             dbg_print( INPUT_DBG_META, "can seek");
854             goto common;
855         case ACCESS_CAN_PAUSE:
856             dbg_print( INPUT_DBG_META, "can pause");
857     common:
858         {
859             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
860             *pb_bool = VLC_TRUE;
861             return VLC_SUCCESS;
862         }
863
864         /* */
865         case ACCESS_GET_MTU:
866         {
867             pi_int = (int*)va_arg( args, int * );
868             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
869             dbg_print( INPUT_DBG_META, "Get MTU %d", *pi_int);
870             break;
871         }
872
873         case ACCESS_GET_PTS_DELAY:
874         {
875             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
876             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
877               * MILLISECONDS_PER_SEC;
878             break;
879         }
880
881         case ACCESS_GET_TITLE_INFO:
882         {
883             input_title_t ***ppp_title = 
884               (input_title_t***)va_arg( args, input_title_t*** );
885
886             pi_int    = (int*)va_arg( args, int* );
887             *((int*)va_arg( args, int* )) = 1; /* Title offset */
888
889             dbg_print ( INPUT_DBG_EVENT,
890                         "GET TITLE: i_tracks %d, i_tracks %d",
891                         p_cdda->i_tracks, p_cdda->i_tracks );
892
893             CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
894
895             if ( p_cdda->b_nav_mode) {
896                 char *psz_title = 
897                   CDDAFormatTitle( p_access, p_cdda->i_track );
898                 input_Control( p_cdda->p_input, INPUT_SET_NAME, 
899                                psz_title );
900                 free(psz_title);
901             }
902
903             /* Duplicate title info */
904             if( p_cdda->i_titles == 0 )
905             {
906                 *pi_int = 0; ppp_title = NULL;
907                 return VLC_SUCCESS;
908             }
909             *pi_int = p_cdda->i_titles;
910             *ppp_title = calloc(1, 
911                                 sizeof( input_title_t **) * p_cdda->i_titles );
912
913             if (!*ppp_title) return VLC_ENOMEM;
914
915             for( i = 0; i < p_cdda->i_titles; i++ )
916             {
917               if ( p_cdda->p_title[i] ) {
918                    (*ppp_title)[i] =
919                      vlc_input_title_Duplicate( p_cdda->p_title[i] );
920               }
921             }
922             break;
923         }
924
925         case ACCESS_SET_TITLE:
926         {
927             i = (int)va_arg( args, int );
928
929             dbg_print( INPUT_DBG_EVENT, "set title %d", i );
930             if( i != p_access->info.i_title )
931             {
932                 const track_t i_track = p_cdda->i_track + i;
933                 /* Update info */
934                 p_access->info.i_title = i;
935                 if ( p_cdda->b_nav_mode) 
936                 {
937                     lsn_t i_last_lsn;
938                     char *psz_title = CDDAFormatTitle( p_access, i_track );
939                     input_Control( p_cdda->p_input, INPUT_SET_NAME, 
940                                    psz_title );
941                     free(psz_title);
942                     p_cdda->i_track = i_track;
943                     i_last_lsn = cdio_get_track_lsn(p_cdda->p_cdio, 
944                                                     CDIO_CDROM_LEADOUT_TRACK);
945                     if (CDIO_INVALID_LSN != i_last_lsn)
946                       p_access->info.i_size = (int64_t) CDIO_CD_FRAMESIZE_RAW 
947                         * i_last_lsn ;
948                     p_access->info.i_pos = (int64_t)
949                       cdio_get_track_lsn( p_cdda->p_cdio, i_track ) 
950                       * CDIO_CD_FRAMESIZE_RAW;
951                 } else {
952                    p_access->info.i_size = p_cdda->p_title[i]->i_size;
953                    p_access->info.i_pos  = 0;
954                 }
955                 p_access->info.i_update = 
956                   INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
957
958                 /* Next sector to read */
959                 p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, i_track);
960             }
961             break;
962         }
963
964         case ACCESS_SET_PAUSE_STATE:
965           dbg_print( INPUT_DBG_META, "Pause");
966           if (p_cdda->b_audio_ctl)
967             cdda_audio_pause(p_cdda->p_cdio);
968           break;
969
970         case ACCESS_SET_SEEKPOINT:
971         case ACCESS_SET_PRIVATE_ID_STATE:
972             dbg_print( INPUT_DBG_META, "set seekpoint/set private id state");
973             return VLC_EGENERIC;
974
975         default:
976             msg_Warn( p_access, "unimplemented query in control" );
977             return VLC_EGENERIC;
978
979     }
980     return VLC_SUCCESS;
981 }
982
983 /*****************************************************************************
984   CDDAInit:
985
986  Initialize information pertaining to the CD: the number of tracks,
987  first track number, LSNs for each track and the leadout. The leadout
988  information is stored after the last track. The LSN array is
989  0-origin, same as p_access->info.  Add first_track to get what track
990  number this is on the CD. Note: libcdio uses the real track number.
991
992  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
993
994  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
995  *****************************************************************************/
996 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
997 {
998     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
999
1000     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
1001     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
1002
1003     discmode = cdio_get_discmode(p_cdda->p_cdio);
1004     switch(discmode) {
1005     case CDIO_DISC_MODE_CD_DA:
1006     case CDIO_DISC_MODE_CD_MIXED:
1007         /* These are possible for CD-DA */
1008         break;
1009     default:
1010         /* These are not possible for CD-DA */
1011         msg_Err( p_access,
1012                "Disc seems not to be CD-DA. libcdio reports it is %s",
1013                discmode2str[discmode]
1014                );
1015         return VLC_EGENERIC;
1016     }
1017
1018     /* Set reading start LSN. */
1019     p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
1020
1021     return VLC_SUCCESS;
1022 }
1023
1024 /* 
1025  * Local variables:
1026  *  mode: C
1027  *  style: gnu
1028  * End:
1029  */