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