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