]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
* access2: added shortcuts for vcd/svcd.
[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     set_capability( "access2", 10 );
50     set_callbacks( Open, Close );
51
52     add_usage_hint( N_("[cdda:][device][@[track]]") );
53     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
54                  CACHING_LONGTEXT, VLC_TRUE );
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
102     /* Command line: cdda://[dev_path][@title] */
103     if( ( psz = strchr( psz_dup, '@' ) ) )
104     {
105         *psz++ = '\0';
106
107         i_title = strtol( psz, NULL, 0 );
108     }
109
110     if( *psz_dup == '\0' )
111     {
112         free( psz_dup );
113
114         /* Only when selected */
115         if( strcmp( p_access->psz_access, "cdda" ) )
116             return VLC_EGENERIC;
117
118
119         psz_dup = var_CreateGetString( p_access, "cd-audio" );
120         if( *psz_dup == '\0' )
121         {
122             free( psz_dup );
123             return VLC_EGENERIC;
124         }
125     }
126
127     /* Open CDDA */
128     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_dup )) == NULL )
129     {
130         msg_Warn( p_access, "could not open %s", psz_dup );
131         free( psz_dup );
132         return VLC_EGENERIC;
133     }
134     free( psz_dup );
135
136     /* Set up p_access */
137     p_access->pf_read = NULL;
138     p_access->pf_block = Block;
139     p_access->pf_control = Control;
140     p_access->pf_seek = Seek;
141     p_access->info.i_update = 0;
142     p_access->info.i_size = 0;
143     p_access->info.i_pos = 0;
144     p_access->info.b_eof = VLC_FALSE;
145     p_access->info.i_title = 0;
146     p_access->info.i_seekpoint = 0;
147     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
148     memset( p_sys, 0, sizeof( access_sys_t ) );
149     p_sys->vcddev = vcddev;
150
151     /* We read the Table Of Content information */
152     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
153                                           p_sys->vcddev, &p_sys->p_sectors );
154     if( p_sys->i_titles < 0 )
155     {
156         msg_Err( p_access, "unable to count tracks" );
157         goto error;
158     }
159     else if( p_sys->i_titles <= 0 )
160     {
161         msg_Err( p_access, "no audio tracks found" );
162         goto error;
163     }
164
165     /* Build title table */
166     for( i = 0; i < p_sys->i_titles; i++ )
167     {
168         input_title_t *t = p_sys->title[i] = vlc_input_title_New();
169
170         fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[i] );
171         fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+1] );
172
173         t->i_size = sizeof( WAVEHEADER ) +
174                     ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
175                     (int64_t)CDDA_DATA_SIZE;
176
177         t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
178     }
179
180     /* Starting title and sector */
181     if( i_title >= p_sys->i_titles )
182         i_title = 0;
183     p_sys->i_sector = p_sys->p_sectors[i_title];
184     p_access->info.i_title = i_title;
185     p_access->info.i_size = p_sys->title[i_title]->i_size;
186
187     /* Build a WAV header for the output data */
188     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
189     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
190     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
191     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
192     p_sys->waveheader.Length = 0;                     /* we just don't know */
193     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
194     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
195     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
196     SetWLE( &p_sys->waveheader.Modus, 2);
197     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
198     SetWLE( &p_sys->waveheader.BytesPerSample,
199             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
200     SetDWLE( &p_sys->waveheader.BytesPerSec,
201              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
202     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
203     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
204
205     /* Pts delay */
206     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
207     return VLC_SUCCESS;
208
209 error:
210     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
211     free( p_sys );
212     return VLC_EGENERIC;
213 }
214
215 /*****************************************************************************
216  * Close: closes cdda
217  *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
219 {
220     access_t     *p_access = (access_t *)p_this;
221     access_sys_t *p_sys = p_access->p_sys;
222     int          i;
223
224     for( i = 0; i < p_sys->i_titles; i++ )
225     {
226         vlc_input_title_Delete( p_sys->title[i] );
227     }
228
229     ioctl_Close( p_this, p_sys->vcddev );
230     free( p_sys );
231 }
232
233 /*****************************************************************************
234  * Block: read data (CDDA_DATA_ONCE)
235  *****************************************************************************/
236 static block_t *Block( access_t *p_access )
237 {
238     access_sys_t *p_sys = p_access->p_sys;
239     block_t      *p_block;
240     int i_blocks;
241     int i_read;
242     int i;
243
244     if( p_access->info.b_eof )
245         return NULL;
246
247     if( p_access->info.i_pos < sizeof( WAVEHEADER ) )
248     {
249         /* Return only the header */
250         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
251         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
252
253         p_block->i_buffer -= p_access->info.i_pos;
254         p_block->p_buffer += p_access->info.i_pos;
255
256         p_access->info.i_pos += p_block->i_buffer;
257         return p_block;
258     }
259
260     /* Read raw data */
261     i_blocks = CDDA_BLOCKS_ONCE;
262     if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1 ] )
263     {
264         i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] - p_sys->i_sector;
265         if( i_blocks <= 0 ) i_blocks = 1;   /* Should never occur */
266     }
267
268     p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE );
269
270     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev, p_sys->i_sector,
271                            p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
272     {
273         msg_Err( p_access, "cannot read a sector" );
274         block_Release( p_block );
275
276         p_block = NULL;
277         i_blocks = 1;   /* Next sector */
278     }
279
280     i_read = 0;
281     for( i = 0; i < i_blocks; i++ )
282     {
283         /* A good sector read */
284         i_read++;
285         p_sys->i_sector++;
286
287         /* Check end of title */
288         if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
289         {
290             if( p_access->info.i_title + 1 >= p_sys->i_titles )
291             {
292                 p_access->info.b_eof = VLC_TRUE;
293                 break;
294             }
295             p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
296             p_access->info.i_title++;
297             p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
298             p_access->info.i_pos = sizeof( WAVEHEADER );
299         }
300     }
301
302     if( i_read <= 0 )
303     {
304         block_Release( p_block );
305         return NULL;
306     }
307
308     if( p_block )
309     {
310         int i_skip = ( p_access->info.i_pos - sizeof( WAVEHEADER ) ) % CDDA_DATA_SIZE;
311
312         /* Really read data */
313         p_block->i_buffer = i_read * CDDA_DATA_SIZE;
314         /* */
315         p_block->i_buffer -= i_skip;
316         p_block->p_buffer += i_skip;
317
318         p_access->info.i_pos += p_block->i_buffer;
319     }
320
321     return p_block;
322 }
323
324 /****************************************************************************
325  * Seek
326  ****************************************************************************/
327 static int Seek( access_t *p_access, int64_t i_pos )
328 {
329     access_sys_t * p_sys = p_access->p_sys;
330
331     p_access->info.i_pos = i_pos;
332
333     if( i_pos >= sizeof( WAVEHEADER ) )
334     {
335         /* Fix sector */
336         p_sys->i_sector =  p_sys->p_sectors[p_access->info.i_title] +
337                            ( i_pos - sizeof( WAVEHEADER ) ) / (int64_t)CDDA_DATA_SIZE;
338         if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
339         {
340             p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title + 1] - 1;
341         }
342     }
343     else
344         p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title];
345
346     p_access->info.b_eof = VLC_FALSE;
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Control:
352  *****************************************************************************/
353 static int Control( access_t *p_access, int i_query, va_list args )
354 {
355     access_sys_t *p_sys = p_access->p_sys;
356     vlc_bool_t   *pb_bool;
357     int          *pi_int;
358     int64_t      *pi_64;
359     input_title_t ***ppp_title;
360     int i;
361
362     switch( i_query )
363     {
364         /* */
365         case ACCESS_CAN_SEEK:
366         case ACCESS_CAN_FASTSEEK:
367         case ACCESS_CAN_PAUSE:
368         case ACCESS_CAN_CONTROL_PACE:
369             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
370             *pb_bool = VLC_TRUE;
371             break;
372
373         /* */
374         case ACCESS_GET_MTU:
375             pi_int = (int*)va_arg( args, int * );
376             *pi_int = CDDA_DATA_ONCE;
377             break;
378
379         case ACCESS_GET_PTS_DELAY:
380             pi_64 = (int64_t*)va_arg( args, int64_t * );
381             *pi_64 = (int64_t)var_GetInteger( p_access, "cdda-caching" ) * I64C(1000);
382             break;
383
384         /* */
385         case ACCESS_SET_PAUSE_STATE:
386             break;
387
388         case ACCESS_GET_TITLE_INFO:
389             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
390             pi_int    = (int*)va_arg( args, int* );
391
392             /* Duplicate title infos */
393             *pi_int = p_sys->i_titles;
394             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_titles );
395             for( i = 0; i < p_sys->i_titles; i++ )
396             {
397                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
398             }
399             break;
400
401         case ACCESS_SET_TITLE:
402             i = (int)va_arg( args, int );
403             if( i != p_access->info.i_title )
404             {
405                 /* Update info */
406                 p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
407                 p_access->info.i_title = i;
408                 p_access->info.i_size = p_sys->title[i]->i_size;
409                 p_access->info.i_pos  = sizeof(WAVEHEADER);
410
411                 /* Next sector to read */
412                 p_sys->i_sector = p_sys->p_sectors[i];
413             }
414             break;
415
416         case ACCESS_SET_SEEKPOINT:
417             return VLC_EGENERIC;
418
419         default:
420             msg_Err( p_access, "unimplemented query in control" );
421             return VLC_EGENERIC;
422
423     }
424     return VLC_SUCCESS;
425 }
426
427