]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
* Don't bitch anymore about not found access_demux plugins.
[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     /* Current position */
76     int         i_sector;                                  /* Current Sector */
77     int *       p_sectors;                                  /* Track sectors */
78
79     /* Wave header for the output data */
80     WAVEHEADER  waveheader;
81     vlc_bool_t  b_header;
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     vcddev_t *vcddev;
97     char *psz_name;
98     int  i;
99
100     if( !p_access->psz_path || !*p_access->psz_path )
101     {
102         /* Only when selected */
103         if( !p_this->b_force ) return VLC_EGENERIC;
104
105         psz_name = var_CreateGetString( p_this, "cd-audio" );
106         if( !psz_name || !*psz_name )
107         {
108             if( psz_name ) free( psz_name );
109             return VLC_EGENERIC;
110         }
111     }
112     else psz_name = strdup( p_access->psz_path );
113
114     /* Open CDDA */
115     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
116     {
117         msg_Warn( p_access, "could not open %s", psz_name );
118         free( psz_name );
119         return VLC_EGENERIC;
120     }
121     free( psz_name );
122
123     /* Set up p_access */
124     p_access->pf_read = NULL;
125     p_access->pf_block = Block;
126     p_access->pf_control = Control;
127     p_access->pf_seek = Seek;
128     p_access->info.i_update = 0;
129     p_access->info.i_size = 0;
130     p_access->info.i_pos = 0;
131     p_access->info.b_eof = VLC_FALSE;
132     p_access->info.i_title = 0;
133     p_access->info.i_seekpoint = 0;
134     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
135     memset( p_sys, 0, sizeof( access_sys_t ) );
136     p_sys->vcddev = vcddev;
137     p_sys->b_header = VLC_FALSE;
138
139     /* We read the Table Of Content information */
140     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
141                                           p_sys->vcddev, &p_sys->p_sectors );
142     if( p_sys->i_titles < 0 )
143     {
144         msg_Err( p_access, "unable to count tracks" );
145         goto error;
146     }
147     else if( p_sys->i_titles <= 0 )
148     {
149         msg_Err( p_access, "no audio tracks found" );
150         goto error;
151     }
152
153     /* Build title table */
154     for( i = 0; i < p_sys->i_titles; i++ )
155     {
156         input_title_t *t = p_sys->title[i] = vlc_input_title_New();
157
158         msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[i] );
159         msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+1] );
160
161         asprintf( &t->psz_name, _("Track %i"), i + 1 );
162         t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
163                     (int64_t)CDDA_DATA_SIZE;
164
165         t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
166     }
167
168     p_sys->i_sector = p_sys->p_sectors[0];
169     p_access->info.i_size = p_sys->title[0]->i_size;
170
171     /* Build a WAV header for the output data */
172     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
173     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
174     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
175     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
176     p_sys->waveheader.Length = 0;                     /* we just don't know */
177     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
178     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
179     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
180     SetWLE( &p_sys->waveheader.Modus, 2);
181     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
182     SetWLE( &p_sys->waveheader.BytesPerSample,
183             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
184     SetDWLE( &p_sys->waveheader.BytesPerSec,
185              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
186     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
187     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
188
189     /* PTS delay */
190     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
191     return VLC_SUCCESS;
192
193 error:
194     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
195     free( p_sys );
196     return VLC_EGENERIC;
197 }
198
199 /*****************************************************************************
200  * Close: closes cdda
201  *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
203 {
204     access_t     *p_access = (access_t *)p_this;
205     access_sys_t *p_sys = p_access->p_sys;
206     int          i;
207
208     for( i = 0; i < p_sys->i_titles; i++ )
209     {
210         vlc_input_title_Delete( p_sys->title[i] );
211     }
212
213     ioctl_Close( p_this, p_sys->vcddev );
214     free( p_sys );
215 }
216
217 /*****************************************************************************
218  * Block: read data (CDDA_DATA_ONCE)
219  *****************************************************************************/
220 static block_t *Block( access_t *p_access )
221 {
222     access_sys_t *p_sys = p_access->p_sys;
223     int i_blocks = CDDA_BLOCKS_ONCE;
224     block_t *p_block;
225
226     /* Check end of file */
227     if( p_access->info.b_eof ) return NULL;
228
229     if( !p_sys->b_header )
230     {
231         /* Return only the header */
232         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
233         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
234         p_sys->b_header = VLC_TRUE;
235         return p_block;
236     }
237
238     /* Check end of title */
239     while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
240     {
241         if( p_access->info.i_title + 1 >= p_sys->i_titles )
242         {
243             p_access->info.b_eof = VLC_TRUE;
244             return NULL;
245         }
246
247         p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
248         p_access->info.i_title++;
249         p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
250         p_access->info.i_pos = 0;
251     }
252
253     /* Don't read after the end of a title */
254     if( p_sys->i_sector + i_blocks >=
255         p_sys->p_sectors[p_access->info.i_title + 1] )
256     {
257         i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] -
258                    p_sys->i_sector;
259     }
260
261     /* Do the actual reading */
262     if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
263     {
264         msg_Err( p_access, "cannot get a new block of size: %i",
265                  i_blocks * CDDA_DATA_SIZE );
266         return NULL;
267     }
268
269     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
270             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
271     {
272         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
273         block_Release( p_block );
274
275         /* Try to skip one sector (in case of bad sectors) */
276         p_sys->i_sector++;
277         p_access->info.i_pos += CDDA_DATA_SIZE;
278         return NULL;
279     }
280
281     /* Update a few values */
282     p_sys->i_sector += i_blocks;
283     p_access->info.i_pos += p_block->i_buffer;
284
285     return p_block;
286 }
287
288 /****************************************************************************
289  * Seek
290  ****************************************************************************/
291 static int Seek( access_t *p_access, int64_t i_pos )
292 {
293     access_sys_t *p_sys = p_access->p_sys;
294
295     /* Next sector to read */
296     p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title] +
297         i_pos / CDDA_DATA_SIZE;
298     p_access->info.i_pos = i_pos;
299
300     return VLC_SUCCESS;
301 }
302
303 /*****************************************************************************
304  * Control:
305  *****************************************************************************/
306 static int Control( access_t *p_access, int i_query, va_list args )
307 {
308     access_sys_t *p_sys = p_access->p_sys;
309     vlc_bool_t   *pb_bool;
310     int          *pi_int;
311     int64_t      *pi_64;
312     input_title_t ***ppp_title;
313     int i;
314
315     switch( i_query )
316     {
317         /* */
318         case ACCESS_CAN_SEEK:
319         case ACCESS_CAN_FASTSEEK:
320         case ACCESS_CAN_PAUSE:
321         case ACCESS_CAN_CONTROL_PACE:
322             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
323             *pb_bool = VLC_TRUE;
324             break;
325
326         /* */
327         case ACCESS_GET_MTU:
328             pi_int = (int*)va_arg( args, int * );
329             *pi_int = CDDA_DATA_ONCE;
330             break;
331
332         case ACCESS_GET_PTS_DELAY:
333             pi_64 = (int64_t*)va_arg( args, int64_t * );
334             *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
335             break;
336
337         /* */
338         case ACCESS_SET_PAUSE_STATE:
339             break;
340
341         case ACCESS_GET_TITLE_INFO:
342             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
343             pi_int    = (int*)va_arg( args, int* );
344             *((int*)va_arg( args, int* )) = 1; /* Title offset */
345
346             /* Duplicate title infos */
347             *pi_int = p_sys->i_titles;
348             *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
349             for( i = 0; i < p_sys->i_titles; i++ )
350             {
351                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
352             }
353             break;
354
355         case ACCESS_SET_TITLE:
356             i = (int)va_arg( args, int );
357             if( i != p_access->info.i_title )
358             {
359                 /* Update info */
360                 p_access->info.i_update |=
361                     INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
362                 p_access->info.i_title = i;
363                 p_access->info.i_size = p_sys->title[i]->i_size;
364                 p_access->info.i_pos = 0;
365
366                 /* Next sector to read */
367                 p_sys->i_sector = p_sys->p_sectors[i];
368             }
369             break;
370
371         case ACCESS_SET_SEEKPOINT:
372         case ACCESS_SET_PRIVATE_ID_STATE:
373             return VLC_EGENERIC;
374
375         default:
376             msg_Warn( p_access, "unimplemented query in control" );
377             return VLC_EGENERIC;
378
379     }
380     return VLC_SUCCESS;
381 }