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