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