]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
Add a disc-mode and navigation-style control.
[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 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 block_t *CDDAReadBlocks( access_t * p_access );
65 static int      CDDASeek( access_t * p_access, int64_t i_pos );
66 static int      CDDAControl( access_t *p_access, int i_query,
67                              va_list args );
68
69 static int      CDDAInit( access_t *p_access, cdda_data_t *p_cdda ) ;
70
71
72 /****************************************************************************
73  * Private functions
74  ****************************************************************************/
75
76 /* process messages that originate from libcdio. */
77 static void
78 cdio_log_handler (cdio_log_level_t level, const char message[])
79 {
80   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
81
82   if( p_cdda == NULL )
83       return;
84
85   switch (level) {
86   case CDIO_LOG_DEBUG:
87   case CDIO_LOG_INFO:
88     if (p_cdda->i_debug & INPUT_DBG_CDIO)
89       msg_Dbg( p_cdda_input, message);
90     break;
91   case CDIO_LOG_WARN:
92     msg_Warn( p_cdda_input, message);
93     break;
94   case CDIO_LOG_ERROR:
95   case CDIO_LOG_ASSERT:
96     msg_Err( p_cdda_input, message);
97     break;
98   default:
99     msg_Warn( p_cdda_input, message,
100             "The above message had unknown cdio log level",
101             level);
102   }
103   return;
104 }
105
106
107 #ifdef HAVE_LIBCDDB
108 /*! This routine is called by libcddb routines on error.
109    Setup is done by init_input_plugin.
110 */
111 static void
112 cddb_log_handler (cddb_log_level_t level, const char message[])
113 {
114     cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
115     switch (level)
116     {
117         case CDDB_LOG_DEBUG:
118         case CDDB_LOG_INFO:
119         if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
120
121         /* Fall through if to warn case */
122         default:
123             cdio_log_handler (level, message);
124     }
125 }
126 #endif /*HAVE_LIBCDDB*/
127
128
129 /*! This routine is when xine is not fully set up (before full initialization)
130    or is not around (before finalization).
131 */
132 static void
133 uninit_log_handler (cdio_log_level_t level, const char message[])
134 {
135     cdda_data_t *p_cdda = NULL;
136
137     if (p_cdda_input)
138         p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
139
140      switch (level)
141      {
142         case CDIO_LOG_DEBUG:
143         case CDIO_LOG_INFO:
144         if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
145             return;
146
147         /* Fall through if to warn case */
148         case CDIO_LOG_WARN:
149             fprintf(stderr, "WARN: %s\n", message);
150             break;
151         case CDIO_LOG_ERROR:
152             fprintf(stderr, "ERROR: %s\n", message);
153             break;
154         case CDIO_LOG_ASSERT:
155             fprintf(stderr, "ASSERT ERROR: %s\n", message);
156             break;
157         default:
158             fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n", message,
159                             "The above message had unknown cdio log level",
160                             level);
161     }
162
163     /* gl_default_cdio_log_handler (level, message); */
164 }
165
166 /*****************************************************************************
167  * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
168  * an allocated pointer to the data. NULL is returned if no data
169  * read. It is also possible if we haven't read a RIFF header in which
170  * case one that we creaded during Open/Initialization is returned.
171  *****************************************************************************/
172 static block_t * CDDAReadBlocks( access_t * p_access )
173 {
174     block_t     *p_block;
175     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
176     int          i_blocks = p_cdda->i_blocks_per_read;
177
178     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d",
179               p_cdda->i_lsn);
180
181     /* Check end of file */
182     if( p_access->info.b_eof ) return NULL;
183
184     if( !p_cdda->b_header )
185       {
186         /* Return only the dummy RIFF header we created in Open/Init */
187         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
188         memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
189         p_cdda->b_header = VLC_TRUE;
190         return p_block;
191     }
192
193     /* Check end of track */
194     while( p_cdda->i_lsn >= cdio_get_track_lsn(p_cdda->p_cdio, 
195                                                p_cdda->i_track+1) )
196     {
197         if( p_cdda->i_track >= p_cdda->i_first_track + p_cdda->i_titles - 1 )
198         {
199             p_access->info.b_eof = VLC_TRUE;
200             return NULL;
201         }
202
203         p_access->info.i_update |= INPUT_UPDATE_TITLE;
204         p_access->info.i_title++;
205         p_cdda->i_track++;
206
207         if ( p_cdda-> b_nav_mode ) {
208           char *psz_title = CDDAFormatTitle( p_access, p_cdda->i_track );
209           input_Control( p_cdda->p_input, INPUT_SET_NAME, psz_title );
210           free(psz_title);
211         } else {
212           p_access->info.i_size =
213             p_cdda->p_title[p_access->info.i_title]->i_size;
214           p_access->info.i_pos = 0;
215           p_access->info.i_update |= INPUT_UPDATE_SIZE;
216         }
217     }
218
219     /* Possibly adjust i_blocks so we don't read past the end of a track. */
220     if( p_cdda->i_lsn + i_blocks >= 
221         cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track+1) )
222     {
223       i_blocks = cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track+1 ) 
224         - p_cdda->i_lsn;
225     }
226
227     /* Do the actual reading */
228     p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
229     if( !p_block)
230     {
231       msg_Err( p_access, "Cannot get a new block of size: %i",
232                i_blocks * CDIO_CD_FRAMESIZE_RAW );
233       return NULL;
234     }
235
236     if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
237                                  p_cdda->i_lsn, i_blocks) != 0 )
238     {
239         msg_Err( p_access, "could not read sector %lu",
240                  (long unsigned int) p_cdda->i_lsn );
241         block_Release( p_block );
242
243         /* If we had problems above, assume the problem is with
244            the first sector of the read and set to skip it.  In
245            the future libcdio may have cdparanoia support.
246         */
247         p_cdda->i_lsn++;
248         p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
249         return NULL;
250     }
251
252     p_cdda->i_lsn        += i_blocks;
253     p_access->info.i_pos += p_block->i_buffer;
254
255     return p_block;
256 }
257
258 /****************************************************************************
259  * CDDASeek - change position for subsequent reads. For example, this
260  * can happen if the user moves a position slider bar in a GUI.
261  ****************************************************************************/
262 static int CDDASeek( access_t * p_access, int64_t i_pos )
263 {
264     cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
265
266     p_cdda->i_lsn = (i_pos / CDIO_CD_FRAMESIZE_RAW);
267
268     if ( ! p_cdda->b_nav_mode ) 
269       p_cdda->i_lsn += cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
270
271     /* Seeked backwards and we are doing disc mode. */
272     if ( p_cdda->b_nav_mode && p_access->info.i_pos > i_pos ) {
273       track_t i_track;
274       char *psz_title;
275       
276       for( i_track = p_cdda->i_track; 
277            i_track > 1 && 
278              p_cdda->i_lsn < cdio_get_track_lsn(p_cdda->p_cdio, i_track);
279            i_track--, p_access->info.i_title-- ) ;
280
281       p_cdda->i_track = i_track;
282       p_access->info.i_update |= INPUT_UPDATE_TITLE;
283       psz_title  = CDDAFormatTitle( p_access, p_cdda->i_track );
284       input_Control( p_cdda->p_input, INPUT_SET_NAME, 
285                      psz_title );
286       free(psz_title);
287
288     }
289     
290     p_access->info.i_pos = i_pos;
291
292     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
293                "lsn %lu, offset: %lld",
294                (long unsigned int) p_cdda->i_lsn, i_pos );
295     return VLC_SUCCESS;
296 }
297
298 /****************************************************************************
299  * Public functions
300  ****************************************************************************/
301
302 /*****************************************************************************
303  * Open: open cdda device or image file and initialize structures
304  *       for subsequent operations.
305  *****************************************************************************/
306 int 
307 CDDAOpen( vlc_object_t *p_this )
308 {
309     access_t    *p_access = (access_t*)p_this;
310     char *      psz_source = NULL;
311     cdda_data_t *p_cdda    = NULL;
312     CdIo        *p_cdio;
313     track_t     i_track = 1;
314     vlc_bool_t  b_single_track = false;
315     int         i_rc = VLC_EGENERIC;
316
317     p_access->p_sys = NULL;
318
319     /* Set where to log errors messages from libcdio. */
320     p_cdda_input = p_access;
321
322     /* parse the options passed in command line : */
323
324     if( p_access->psz_path && *p_access->psz_path )
325     {
326         char *psz_parser = psz_source = strdup( p_access->psz_path );
327
328         while( *psz_parser && *psz_parser != '@' )
329         {
330             psz_parser++;
331         }
332
333         if( *psz_parser == '@' )
334         {
335             /* Found options */
336             *psz_parser = '\0';
337             ++psz_parser;
338
339             if ('T' == *psz_parser || 't' == *psz_parser )
340             ++psz_parser;
341
342             i_track = (int)strtol( psz_parser, NULL, 10 );
343             i_track = i_track ? i_track : 1;
344             b_single_track = true;
345         }
346     }
347
348     if (!psz_source || !*psz_source)
349     {
350         /* No device/track given. Continue only when this plugin was
351            selected */
352         if( !p_this->b_force ) return VLC_EGENERIC;
353
354         psz_source = var_CreateGetString( p_this, "cd-audio" );
355
356         if( !psz_source || !*psz_source )
357         {
358             /* Scan for a CD-ROM drive with a CD-DA in it. */
359             char **cd_drives =
360               cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
361
362             if (NULL == cd_drives || NULL == cd_drives[0] )
363             {
364                 msg_Err( p_access,
365                      "libcdio couldn't find something with a CD-DA in it" );
366                 if (cd_drives) cdio_free_device_list(cd_drives);
367                 return VLC_EGENERIC;
368             }
369
370             psz_source = strdup(cd_drives[0]);
371             cdio_free_device_list(cd_drives);
372         }
373     }
374
375     cdio_log_set_handler ( cdio_log_handler );
376
377     /* Open CDDA */
378     if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
379     {
380         msg_Warn( p_access, "could not open %s", psz_source );
381         if (psz_source) free( psz_source );
382         return VLC_EGENERIC;
383     }
384
385     p_cdda = malloc( sizeof(cdda_data_t) );
386     if( p_cdda == NULL )
387     {
388         msg_Err( p_access, "out of memory" );
389         free( psz_source );
390         return VLC_ENOMEM;
391     }
392     memset( p_cdda, 0, sizeof(cdda_data_t) );
393
394 #ifdef HAVE_LIBCDDB
395     cddb_log_set_handler ( cddb_log_handler );
396     p_cdda->cddb.disc = NULL;
397     p_cdda->b_cddb_enabled =
398       config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
399 #endif
400
401     p_cdda->b_cdtext_enabled =
402       config_GetInt( p_access, MODULE_STRING "-cdtext-enabled" );
403
404     p_cdda->b_cdtext_prefer =
405       config_GetInt( p_access, MODULE_STRING "-cdtext-prefer" );
406
407     p_cdda->psz_source = strdup(psz_source);
408     p_cdda->b_header   = VLC_FALSE;
409     p_cdda->p_cdio     = p_cdio;
410     p_cdda->i_tracks   = 0;
411     p_cdda->i_titles   = 0;
412     p_cdda->i_track    = i_track;
413     p_cdda->i_debug    = config_GetInt(p_this, MODULE_STRING 
414                                        "-debug");
415     p_cdda->b_nav_mode = config_GetInt(p_this, MODULE_STRING 
416                                        "-navigation-mode" );
417     p_cdda->i_blocks_per_read
418       = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
419
420     p_cdda->p_input  = vlc_object_find( p_access, VLC_OBJECT_INPUT,
421                                         FIND_PARENT );
422
423     if (0 == p_cdda->i_blocks_per_read)
424         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
425
426     if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ
427          || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ )
428     {
429         msg_Warn( p_cdda_input,
430                 "Number of blocks (%d) has to be between %d and %d. "
431                 "Using %d.",
432                 p_cdda->i_blocks_per_read,
433                 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
434                 DEFAULT_BLOCKS_PER_READ );
435         p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
436     }
437
438     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
439
440     /* Set up p_access */
441     p_access->pf_read    = NULL;
442     p_access->pf_block   = CDDAReadBlocks;
443     p_access->pf_control = CDDAControl;
444     p_access->pf_seek    = CDDASeek;
445
446     p_access->info.i_size      = 0;
447
448     p_access->info.i_update    = 0;
449     p_access->info.b_eof       = VLC_FALSE;
450     p_access->info.i_title     = 0;
451     p_access->info.i_seekpoint = 0;
452
453     p_access->p_sys     = (access_sys_t *) p_cdda;
454
455     /* We read the Table Of Content information */
456     i_rc = CDDAInit( p_access, p_cdda );
457     if ( VLC_SUCCESS != i_rc ) goto error;
458
459     CDDAFixupPlaylist( p_access, p_cdda, b_single_track );
460
461     /* Build a WAV header to put in front of the output data.
462        This gets sent back in the Block (read) routine.
463      */
464     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
465     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
466     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
467     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
468     p_cdda->waveheader.Length = 0;                     /* we just don't know */
469     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
470     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
471     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
472     SetWLE( &p_cdda->waveheader.Modus, 2);
473     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
474     SetWLE( &p_cdda->waveheader.BytesPerSample,
475             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
476     SetDWLE( &p_cdda->waveheader.BytesPerSec,
477              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
478     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
479     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
480
481     /* PTS delay */
482     var_Create( p_access, MODULE_STRING "-caching",
483                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
484     vlc_object_release( p_cdda->p_input );
485     return VLC_SUCCESS;
486
487  error:
488     cdio_destroy( p_cdda->p_cdio );
489     if( psz_source) free( psz_source );
490     if( p_cdda ) {
491       if ( p_cdda->p_input )
492         vlc_object_release( p_cdda->p_input );
493       free(p_cdda);
494     }
495     
496     return i_rc;
497
498 }
499
500 /*****************************************************************************
501  * CDDAClose: closes cdda and frees any resources associded with it.
502  *****************************************************************************/
503 void 
504 CDDAClose (vlc_object_t *p_this )
505 {
506     access_t    *p_access = (access_t *) p_this;
507     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
508     track_t      i;
509
510     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
511
512     /* Remove playlist titles */
513     for( i = 0; i < p_cdda->i_titles; i++ )
514     {
515         vlc_input_title_Delete( p_cdda->p_title[i] );
516     }
517
518     cdio_destroy( p_cdda->p_cdio );
519
520     cdio_log_set_handler (uninit_log_handler);
521
522 #ifdef HAVE_LIBCDDB
523     cddb_log_set_handler ((cddb_log_handler_t) uninit_log_handler);
524     if (p_cdda->b_cddb_enabled)
525       cddb_disc_destroy(p_cdda->cddb.disc);
526 #endif
527
528     if (p_cdda->psz_mcn)    free( p_cdda->psz_mcn );
529     if (p_cdda->psz_source) free( p_cdda->psz_source );
530     free( p_cdda );
531     p_cdda_input = NULL;
532 }
533
534 /*****************************************************************************
535  * Control: The front-end or vlc engine calls here to ether get
536  * information such as meta information or plugin capabilities or to
537  * issue miscellaneous "set" requests.
538  *****************************************************************************/
539 static int CDDAControl( access_t *p_access, int i_query, va_list args )
540 {
541     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
542     int          *pi_int;
543     int i;
544
545     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
546                "query %d", i_query );
547
548     switch( i_query )
549     {
550         /* Pass back a copy of meta information that was gathered when we
551            during the Open/Initialize call.
552          */
553         case ACCESS_GET_META:
554         {
555             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
556             if ( p_cdda->p_meta )
557             {
558                 *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
559                 dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
560                 return VLC_SUCCESS;
561             }
562             else {
563                 msg_Warn( p_access, "tried to copy NULL meta info" );
564                 return VLC_EGENERIC;
565             }
566         }
567
568         case ACCESS_CAN_SEEK:
569         case ACCESS_CAN_FASTSEEK:
570         case ACCESS_CAN_PAUSE:
571         case ACCESS_CAN_CONTROL_PACE:
572         {
573             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
574             *pb_bool = VLC_TRUE;
575             return VLC_SUCCESS;;
576         }
577
578         /* */
579         case ACCESS_GET_MTU:
580         {
581             pi_int = (int*)va_arg( args, int * );
582             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
583             break;
584         }
585
586         case ACCESS_GET_PTS_DELAY:
587         {
588             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
589             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
590               * MILLISECONDS_PER_SEC;
591             break;
592         }
593
594         /* */
595         case ACCESS_SET_PAUSE_STATE:
596             break;
597
598         case ACCESS_GET_TITLE_INFO:
599         {
600             input_title_t ***ppp_title = 
601               (input_title_t***)va_arg( args, input_title_t*** );
602
603             pi_int    = (int*)va_arg( args, int* );
604             *((int*)va_arg( args, int* )) = 1; /* Title offset */
605
606             dbg_print ( INPUT_DBG_EVENT,
607                         "GET TITLE: i_tracks %d, i_tracks %d",
608                         p_cdda->i_tracks, p_cdda->i_tracks );
609
610             CDDAMetaInfo( p_access, CDIO_INVALID_TRACK );
611
612             if ( p_cdda->b_nav_mode) {
613                 char *psz_title = 
614                   CDDAFormatTitle( p_access, p_cdda->i_track );
615                 input_Control( p_cdda->p_input, INPUT_SET_NAME, 
616                                psz_title );
617                 free(psz_title);
618             }
619
620             /* Duplicate title info */
621             if( p_cdda->i_titles == 0 )
622             {
623                 *pi_int = 0; ppp_title = NULL;
624                 return VLC_SUCCESS;
625             }
626             *pi_int = p_cdda->i_titles;
627             *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_titles );
628
629             if (!*ppp_title) return VLC_ENOMEM;
630
631             for( i = 0; i < p_cdda->i_titles; i++ )
632             {
633               if ( p_cdda->p_title[i] ) {
634                    (*ppp_title)[i] =
635                      vlc_input_title_Duplicate( p_cdda->p_title[i] );
636               }
637             }
638             break;
639         }
640
641         case ACCESS_SET_TITLE:
642         {
643             i = (int)va_arg( args, int );
644
645             dbg_print( INPUT_DBG_EVENT, "set title %d", i );
646             if( i != p_access->info.i_title )
647             {
648                 /* Update info */
649                 p_access->info.i_update |= INPUT_UPDATE_TITLE;
650                 p_access->info.i_title = i;
651                 if ( p_cdda->b_nav_mode) {
652                     char *psz_title = 
653                       CDDAFormatTitle( p_access, i+1 );
654                     input_Control( p_cdda->p_input, INPUT_SET_NAME, 
655                                    psz_title );
656                     free(psz_title);
657                     p_cdda->i_track = i+1;
658                     p_access->info.i_pos = 
659                       cdio_get_track_lsn( p_cdda->p_cdio, p_cdda->i_track ) 
660                       * CDIO_CD_FRAMESIZE_RAW;
661                 } else {
662                    p_access->info.i_update |= INPUT_UPDATE_SIZE;
663                    p_access->info.i_size = p_cdda->p_title[i]->i_size;
664                    p_access->info.i_pos = 0;
665                 }
666
667                 /* Next sector to read */
668                 p_cdda->i_lsn = 
669                   cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
670             }
671             break;
672         }
673
674         case ACCESS_SET_SEEKPOINT:
675         case ACCESS_SET_PRIVATE_ID_STATE:
676             return VLC_EGENERIC;
677
678         default:
679             msg_Warn( p_access, "unimplemented query in control" );
680             return VLC_EGENERIC;
681
682     }
683     return VLC_SUCCESS;
684 }
685
686 /*****************************************************************************
687   CDDAInit:
688
689  Initialize information pertaining to the CD: the number of tracks,
690  first track number, LSNs for each track and the leadout. The leadout
691  information is stored after the last track. The LSN array is
692  0-origin, same as p_access->info.  Add first_track to get what track
693  number this is on the CD. Note: libcdio uses the real track number.
694
695  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
696
697  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
698  *****************************************************************************/
699 static int CDDAInit( access_t *p_access, cdda_data_t *p_cdda )
700 {
701     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
702
703     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
704     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
705
706     discmode = cdio_get_discmode(p_cdda->p_cdio);
707     switch(discmode) {
708     case CDIO_DISC_MODE_CD_DA:
709     case CDIO_DISC_MODE_CD_MIXED:
710         /* These are possible for CD-DA */
711         break;
712     default:
713         /* These are not possible for CD-DA */
714         msg_Err( p_access,
715                "Disc seems not to be CD-DA. libcdio reports it is %s",
716                discmode2str[discmode]
717                );
718         return VLC_EGENERIC;
719     }
720
721     /* Set reading start LSN. */
722     p_cdda->i_lsn = cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_track);
723
724     return VLC_SUCCESS;
725 }
726
727 /* 
728  * Local variables:
729  *  mode: C
730  *  style: gnu
731  * End:
732  */