]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
* cdda: converted to access2. It seems stable.
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "codecs.h"
34 #include "vcd/cdrom.h"
35
36 /*****************************************************************************
37  * Module descriptior
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44     "Allows you to modify the default caching value for cdda streams. This " \
45     "value should be set in milliseconds units." )
46
47 vlc_module_begin();
48     set_description( _("Audio CD input") );
49
50     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
51                  CACHING_LONGTEXT, VLC_TRUE );
52
53     set_capability( "access2", 10 );
54     set_callbacks( Open, Close );
55     add_shortcut( "cdda" );
56     add_shortcut( "cddasimple" );
57 vlc_module_end();
58
59
60 /* how many blocks VCDRead will read in each loop */
61 #define CDDA_BLOCKS_ONCE 20
62 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
63
64 /*****************************************************************************
65  * Access: local prototypes
66  *****************************************************************************/
67 struct access_sys_t
68 {
69     vcddev_t    *vcddev;                            /* vcd device descriptor */
70
71     /* Title infos */
72     int           i_titles;
73     input_title_t *title[99];         /* No more that 99 track in a cd-audio */
74
75     /* */
76     int         i_sector;                                  /* Current Sector */
77     int *       p_sectors;                                  /* Track sectors */
78
79     /* Wave header for the output data */
80     WAVEHEADER  waveheader;
81
82 };
83
84 static block_t *Block( access_t * );
85 static int      Seek( access_t *, int64_t );
86 static int      Control( access_t *, int, va_list );
87
88 /*****************************************************************************
89  * Open: open cdda
90  *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
92 {
93     access_t     *p_access = (access_t*)p_this;
94     access_sys_t *p_sys;
95
96     char *psz_dup = strdup( p_access->psz_path );
97     char *psz;
98     int  i;
99     int  i_title = 0;
100     vcddev_t *vcddev;
101     vlc_value_t val;
102
103     /* Command line: cdda://[dev_path][@title] */
104     if( ( psz = strchr( psz_dup, '@' ) ) )
105     {
106         *psz++ = '\0';
107
108         i_title = strtol( psz, NULL, 0 );
109     }
110
111     if( *psz_dup == '\0' )
112     {
113         free( psz_dup );
114
115         /* Only when selected */
116         if( *p_access->psz_access == '\0' )
117             return VLC_EGENERIC;
118
119
120         var_Create( p_access, "cd-audio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
121         var_Get( p_access, "cd-audio", &val );
122         psz_dup = val.psz_string;
123
124         if( *psz_dup == '\0' )
125         {
126             free( psz_dup );
127             return VLC_EGENERIC;
128         }
129     }
130
131     /* Open CDDA */
132     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_dup )) == NULL )
133     {
134         msg_Warn( p_access, "could not open %s", psz_dup );
135         free( psz_dup );
136         return VLC_EGENERIC;
137     }
138     free( psz_dup );
139
140     /* Set up p_access */
141     p_access->pf_read = NULL;
142     p_access->pf_block = Block;
143     p_access->pf_control = Control;
144     p_access->pf_seek = Seek;
145     p_access->info.i_update = 0;
146     p_access->info.i_size = 0;
147     p_access->info.i_pos = 0;
148     p_access->info.b_eof = VLC_FALSE;
149     p_access->info.i_title = 0;
150     p_access->info.i_seekpoint = 0;
151     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
152     memset( p_sys, 0, sizeof( access_sys_t ) );
153     p_sys->vcddev = vcddev;
154
155     /* We read the Table Of Content information */
156     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
157                                           p_sys->vcddev, &p_sys->p_sectors );
158     if( p_sys->i_titles < 0 )
159     {
160         msg_Err( p_access, "unable to count tracks" );
161         goto error;
162     }
163     else if( p_sys->i_titles <= 0 )
164     {
165         msg_Err( p_access, "no audio tracks found" );
166         goto error;
167     }
168
169     /* Build title table */
170     for( i = 0; i < p_sys->i_titles; i++ )
171     {
172         input_title_t *t = p_sys->title[i] = vlc_input_title_New();
173
174         fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[i] );
175         fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+1] );
176
177         t->i_size = sizeof( WAVEHEADER ) +
178                     ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
179                     (int64_t)CDDA_DATA_SIZE;
180
181         t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
182     }
183
184     /* Starting title and sector */
185     if( i_title >= p_sys->i_titles )
186         i_title = 0;
187     p_sys->i_sector = p_sys->p_sectors[i_title];
188     p_access->info.i_title = i_title;
189     p_access->info.i_size = p_sys->title[i_title]->i_size;
190
191     /* Build a WAV header for the output data */
192     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
193     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
194     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
195     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
196     p_sys->waveheader.Length = 0;                     /* we just don't know */
197     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
198     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
199     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
200     SetWLE( &p_sys->waveheader.Modus, 2);
201     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
202     SetWLE( &p_sys->waveheader.BytesPerSample,
203             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
204     SetDWLE( &p_sys->waveheader.BytesPerSec,
205              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
206     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
207     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
208
209     /* Pts delay */
210     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
211     return VLC_SUCCESS;
212
213 error:
214     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
215     free( p_sys );
216     return VLC_EGENERIC;
217 }
218
219 /*****************************************************************************
220  * Close: closes cdda
221  *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
223 {
224     access_t     *p_access = (access_t *)p_this;
225     access_sys_t *p_sys = p_access->p_sys;
226     int          i;
227
228     for( i = 0; i < p_sys->i_titles; i++ )
229     {
230         vlc_input_title_Delete( p_sys->title[i] );
231     }
232
233     ioctl_Close( p_this, p_sys->vcddev );
234     free( p_sys );
235 }
236
237 /*****************************************************************************
238  * Block: read data (CDDA_DATA_ONCE)
239  *****************************************************************************/
240 static block_t *Block( access_t *p_access )
241 {
242     access_sys_t *p_sys = p_access->p_sys;
243     block_t      *p_block;
244     int i_blocks;
245     int i_read;
246     int i;
247
248     if( p_access->info.b_eof )
249         return NULL;
250
251     if( p_access->info.i_pos < sizeof( WAVEHEADER ) )
252     {
253         /* Return only the header */
254         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
255         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
256
257         p_block->i_buffer -= p_access->info.i_pos;
258         p_block->p_buffer += p_access->info.i_pos;
259
260         p_access->info.i_pos += p_block->i_buffer;
261         return p_block;
262     }
263
264     /* Read raw data */
265     i_blocks = CDDA_BLOCKS_ONCE;
266     if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1 ] )
267     {
268         i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] - p_sys->i_sector;
269         if( i_blocks <= 0 ) i_blocks = 1;   /* Should never occur */
270     }
271
272     p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE );
273
274     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev, p_sys->i_sector,
275                            p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
276     {
277         msg_Err( p_access, "cannot read a sector" );
278         block_Release( p_block );
279
280         p_block = NULL;
281         i_blocks = 1;   /* Next sector */
282     }
283
284     i_read = 0;
285     for( i = 0; i < i_blocks; i++ )
286     {
287         /* A good sector read */
288         i_read++;
289         p_sys->i_sector++;
290
291         /* Check end of title */
292         if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
293         {
294             if( p_access->info.i_title + 1 >= p_sys->i_titles )
295             {
296                 p_access->info.b_eof = VLC_TRUE;
297                 break;
298             }
299             p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
300             p_access->info.i_title++;
301             p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
302             p_access->info.i_pos = sizeof( WAVEHEADER );
303         }
304     }
305
306     if( i_read <= 0 )
307     {
308         block_Release( p_block );
309         return NULL;
310     }
311
312     if( p_block )
313     {
314         int i_skip = ( p_access->info.i_pos - sizeof( WAVEHEADER ) ) % CDDA_DATA_SIZE;
315
316         /* Really read data */
317         p_block->i_buffer = i_read * CDDA_DATA_SIZE;
318         /* */
319         p_block->i_buffer -= i_skip;
320         p_block->p_buffer += i_skip;
321
322         p_access->info.i_pos += p_block->i_buffer;
323     }
324
325     return p_block;
326 }
327
328 /****************************************************************************
329  * Seek
330  ****************************************************************************/
331 static int Seek( access_t *p_access, int64_t i_pos )
332 {
333     access_sys_t * p_sys = p_access->p_sys;
334
335     p_access->info.i_pos = i_pos;
336
337     if( i_pos >= sizeof( WAVEHEADER ) )
338     {
339         /* Fix sector */
340         p_sys->i_sector =  p_sys->p_sectors[p_access->info.i_title] +
341                            ( i_pos - sizeof( WAVEHEADER ) ) / (int64_t)CDDA_DATA_SIZE;
342         if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
343         {
344             p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title + 1] - 1;
345         }
346     }
347     else
348         p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title];
349
350     p_access->info.b_eof = VLC_FALSE;
351     return VLC_SUCCESS;
352 }
353
354 /*****************************************************************************
355  * Control:
356  *****************************************************************************/
357 static int Control( access_t *p_access, int i_query, va_list args )
358 {
359     access_sys_t *p_sys = p_access->p_sys;
360     vlc_bool_t   *pb_bool;
361     int          *pi_int;
362     int64_t      *pi_64;
363     input_title_t ***ppp_title;
364     vlc_value_t  val;
365     int i;
366
367     switch( i_query )
368     {
369         /* */
370         case ACCESS_CAN_SEEK:
371         case ACCESS_CAN_FASTSEEK:
372         case ACCESS_CAN_PAUSE:
373         case ACCESS_CAN_CONTROL_PACE:
374             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
375             *pb_bool = VLC_TRUE;
376             break;
377
378         /* */
379         case ACCESS_GET_MTU:
380             pi_int = (int*)va_arg( args, int * );
381             *pi_int = CDDA_DATA_ONCE;
382             break;
383
384         case ACCESS_GET_PTS_DELAY:
385             pi_64 = (int64_t*)va_arg( args, int64_t * );
386             var_Get( p_access, "cdda-caching", &val );
387             *pi_64 = val.i_int * 1000;
388             break;
389
390         /* */
391         case ACCESS_SET_PAUSE_STATE:
392             break;
393
394         case ACCESS_GET_TITLE_INFO:
395             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
396             pi_int    = (int*)va_arg( args, int* );
397
398             /* Duplicate title infos */
399             *pi_int = p_sys->i_titles;
400             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_titles );
401             for( i = 0; i < p_sys->i_titles; i++ )
402             {
403                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
404             }
405             break;
406
407         case ACCESS_SET_TITLE:
408             i = (int)va_arg( args, int );
409             if( i != p_access->info.i_title )
410             {
411                 /* Update info */
412                 p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
413                 p_access->info.i_title = i;
414                 p_access->info.i_size = p_sys->title[i]->i_size;
415                 p_access->info.i_pos  = sizeof(WAVEHEADER);
416
417                 /* Next sector to read */
418                 p_sys->i_sector = p_sys->p_sectors[i];
419             }
420             break;
421
422         case ACCESS_SET_SEEKPOINT:
423             return VLC_EGENERIC;
424
425         default:
426             msg_Err( p_access, "unimplemented query in control" );
427             return VLC_EGENERIC;
428
429     }
430     return VLC_SUCCESS;
431 }
432
433