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