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