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