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