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