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