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