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