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