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