]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
* toolbox:
[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: cdda.c,v 1.11 2003/12/22 02:24:51 sam Exp $
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 "vcd/cdrom.h"
34
35 /*****************************************************************************
36  * Module descriptior
37  *****************************************************************************/
38 static int  AccessOpen ( vlc_object_t * );
39 static void AccessClose( vlc_object_t * );
40
41 static int  DemuxOpen  ( vlc_object_t * );
42 static void DemuxClose ( vlc_object_t * );
43
44 #define CACHING_TEXT N_("Caching value in ms")
45 #define CACHING_LONGTEXT N_( \
46     "Allows you to modify the default caching value for cdda streams. This " \
47     "value should be set in milliseconds units." )
48
49 vlc_module_begin();
50     set_description( _("Audio CD input") );
51     set_capability( "access", 70 );
52     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
53     set_callbacks( AccessOpen, AccessClose );
54     add_shortcut( "cdda" );
55
56     add_submodule();
57         set_description( _("Audio CD demux") );
58         set_capability( "demux", 0 );
59         set_callbacks( DemuxOpen, DemuxClose );
60         add_shortcut( "cdda" );
61 vlc_module_end();
62
63
64 /* how many blocks VCDRead will read in each loop */
65 #define CDDA_BLOCKS_ONCE 20
66 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
67
68 /*****************************************************************************
69  * Access: local prototypes
70  *****************************************************************************/
71 struct access_sys_t
72 {
73     vcddev_t    *vcddev;                            /* vcd device descriptor */
74     int         i_nb_tracks;                        /* Nb of tracks (titles) */
75     int         i_track;                                    /* Current track */
76     int         i_sector;                                  /* Current Sector */
77     int *       p_sectors;                                  /* Track sectors */
78     vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
79
80 };
81
82 static int  Read      ( input_thread_t *, byte_t *, size_t );
83 static void Seek      ( input_thread_t *, off_t );
84 static int  SetArea   ( input_thread_t *, input_area_t * );
85 static int  SetProgram( input_thread_t *, pgrm_descriptor_t * );
86
87 /*****************************************************************************
88  * AccessOpen: open cdda
89  *****************************************************************************/
90 static int AccessOpen( vlc_object_t *p_this )
91 {
92     input_thread_t *        p_input = (input_thread_t *)p_this;
93     access_sys_t *          p_sys;
94
95     char *                  psz_orig;
96     char *                  psz_parser;
97     char *                  psz_source;
98     int                     i;
99     input_area_t *          p_area;
100     int                     i_title = 1;
101     vcddev_t                *vcddev;
102
103     /* parse the options passed in command line : */
104     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
105
106     if( !psz_orig )
107     {
108         return( -1 );
109     }
110
111     while( *psz_parser && *psz_parser != '@' )
112     {
113         psz_parser++;
114     }
115
116     if( *psz_parser == '@' )
117     {
118         /* Found options */
119         *psz_parser = '\0';
120         ++psz_parser;
121
122         i_title = (int)strtol( psz_parser, NULL, 10 );
123         i_title = i_title ? i_title : 1;
124     }
125
126     if( !*psz_source )
127     {
128         /* No source specified, so figure it out. */
129         if( !p_input->psz_access )
130         {
131             free( psz_orig );
132             return VLC_EGENERIC;
133         }
134         psz_source = config_GetPsz( p_input, "cd-audio" );
135         if( !psz_source ) return -1;
136     }
137
138     /* Open CDDA */
139     if( !(vcddev = ioctl_Open( p_this, psz_source )) )
140     {
141         msg_Warn( p_input, "could not open %s", psz_source );
142         free( psz_source );
143         return VLC_EGENERIC;
144     }
145     free( psz_source );
146
147     p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
148     if( p_sys == NULL )
149     {
150         msg_Err( p_input, "out of memory" );
151         free( psz_source );
152         return VLC_EGENERIC;
153     }
154
155     p_sys->vcddev = vcddev;
156
157     p_input->i_mtu = CDDA_DATA_ONCE;
158
159     /* We read the Table Of Content information */
160     p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
161                                              p_sys->vcddev, &p_sys->p_sectors );
162     if( p_sys->i_nb_tracks < 0 )
163     {
164         msg_Err( p_input, "unable to count tracks" );
165     }
166     else if( p_sys->i_nb_tracks <= 0 )
167     {
168         msg_Err( p_input, "no audio tracks found" );
169     }
170
171     if( p_sys->i_nb_tracks <= 1)
172     {
173         ioctl_Close( p_this, p_sys->vcddev );
174         free( p_sys );
175         return VLC_EGENERIC;
176     }
177
178     if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
179     {
180         i_title = 1;
181     }
182
183     /* Set stream and area data */
184     vlc_mutex_lock( &p_input->stream.stream_lock );
185
186     /* Initialize ES structures */
187     input_InitStream( p_input, 0 );
188
189     /* cdda input method */
190     p_input->stream.i_method = INPUT_METHOD_CDDA;
191
192     p_input->stream.b_pace_control = 1;
193     p_input->stream.b_seekable = 1;
194     p_input->stream.i_mux_rate = 44100 * 4 / 50;
195
196 #define area p_input->stream.pp_areas
197     for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
198     {
199         input_AddArea( p_input, i, 1 );
200
201         /* Absolute start offset and size */
202         area[i]->i_start =
203             (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
204         area[i]->i_size =
205             (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
206             * (off_t)CDDA_DATA_SIZE;
207     }
208 #undef area
209
210     p_area = p_input->stream.pp_areas[i_title];
211
212     SetArea( p_input, p_area );
213
214     vlc_mutex_unlock( &p_input->stream.stream_lock );
215
216     if( !p_input->psz_demux || !*p_input->psz_demux )
217     {
218         p_input->psz_demux = "cdda";
219     }
220
221     p_input->pf_read = Read;
222     p_input->pf_seek = Seek;
223     p_input->pf_set_area = SetArea;
224     p_input->pf_set_program = SetProgram;
225
226     /* Update default_pts to a suitable value for cdda access */
227     p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
228
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * AccessClose: closes cdda
234  *****************************************************************************/
235 static void AccessClose( vlc_object_t *p_this )
236 {
237     input_thread_t *p_input = (input_thread_t *)p_this;
238     access_sys_t   *p_sys = p_input->p_access_data;
239
240     ioctl_Close( p_this, p_sys->vcddev );
241     free( p_sys );
242 }
243
244 /*****************************************************************************
245  * Read: reads from the CDDA into PES packets.
246  *****************************************************************************
247  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
248  * bytes.
249  *****************************************************************************/
250 static int Read( input_thread_t * p_input, byte_t * p_buffer,
251                      size_t i_len )
252 {
253     access_sys_t *p_sys = p_input->p_access_data;
254     int          i_blocks = i_len / CDDA_DATA_SIZE;
255     int          i_read = 0;
256     int          i_index;
257
258
259     if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
260                            p_buffer, i_blocks, CDDA_TYPE ) < 0 )
261     {
262         msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
263         return -1;
264     }
265
266     for( i_index = 0; i_index < i_blocks; i_index++ )
267     {
268         p_sys->i_sector ++;
269         if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
270         {
271             input_area_t *p_area;
272
273             if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
274             {
275                 return 0; /* EOF */
276             }
277
278             vlc_mutex_lock( &p_input->stream.stream_lock );
279             p_area = p_input->stream.pp_areas[
280                     p_input->stream.p_selected_area->i_id + 1 ];
281
282             msg_Dbg( p_input, "new title" );
283
284             p_area->i_part = 1;
285             SetArea( p_input, p_area );
286             vlc_mutex_unlock( &p_input->stream.stream_lock );
287         }
288         i_read += CDDA_DATA_SIZE;
289     }
290
291     if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
292     {
293         msg_Err( p_input, "must read full sectors" );
294     }
295
296     return i_read;
297 }
298
299
300 /*****************************************************************************
301  * SetProgram: Does nothing since a CDDA is mono_program
302  *****************************************************************************/
303 static int SetProgram( input_thread_t * p_input,
304                            pgrm_descriptor_t * p_program)
305 {
306     return VLC_EGENERIC;
307 }
308
309
310 /*****************************************************************************
311  * SetArea: initialize input data for title x.
312  * It should be called for each user navigation request.
313  ****************************************************************************/
314 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
315 {
316     access_sys_t *p_sys = p_input->p_access_data;
317     vlc_value_t  val;
318
319     /* we can't use the interface slider until initilization is complete */
320     p_input->stream.b_seekable = 0;
321
322     if( p_area != p_input->stream.p_selected_area )
323     {
324         /* Change the default area */
325         p_input->stream.p_selected_area = p_area;
326
327         /* Change the current track */
328         p_sys->i_track = p_area->i_id - 1;
329         p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
330
331         /* Update the navigation variables without triggering a callback */
332         val.i_int = p_area->i_id;
333         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
334     }
335
336     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
337
338     p_input->stream.p_selected_area->i_tell =
339         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
340          - p_input->stream.p_selected_area->i_start;
341
342     /* warn interface that something has changed */
343     p_input->stream.b_seekable = 1;
344     p_input->stream.b_changed = 1;
345
346     return 0;
347 }
348
349
350 /****************************************************************************
351  * Seek
352  ****************************************************************************/
353 static void Seek( input_thread_t * p_input, off_t i_off )
354 {
355     access_sys_t * p_sys = p_input->p_access_data;
356
357     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
358                        + i_off / (off_t)CDDA_DATA_SIZE;
359
360     vlc_mutex_lock( &p_input->stream.stream_lock );
361     p_input->stream.p_selected_area->i_tell =
362         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
363          - p_input->stream.p_selected_area->i_start;
364     vlc_mutex_unlock( &p_input->stream.stream_lock );
365 }
366
367
368
369
370 /*****************************************************************************
371  * Demux: local prototypes
372  *****************************************************************************/
373 struct demux_sys_t
374 {
375     es_out_id_t *p_es;
376     mtime_t     i_pts;
377 };
378
379 static int  Demux     ( input_thread_t * p_input );
380
381 /****************************************************************************
382  * DemuxOpen:
383  ****************************************************************************/
384 static int  DemuxOpen    ( vlc_object_t * p_this)
385 {
386     input_thread_t *p_input = (input_thread_t *)p_this;
387     demux_sys_t    *p_sys;
388
389     es_format_t    fmt;
390
391     if( p_input->stream.i_method != INPUT_METHOD_CDDA )
392     {
393         return VLC_EGENERIC;
394     }
395
396     p_input->pf_demux  = Demux;
397     p_input->pf_rewind = NULL;
398     p_input->pf_demux_control = demux_vaControlDefault;
399     p_input->p_demux_data = p_sys = malloc( sizeof( es_descriptor_t ) );
400     p_sys->i_pts = 0;
401
402     vlc_mutex_lock( &p_input->stream.stream_lock );
403     if( input_InitStream( p_input, 0 ) == -1)
404     {
405         vlc_mutex_unlock( &p_input->stream.stream_lock );
406         msg_Err( p_input, "cannot init stream" );
407         free( p_sys );
408         return VLC_EGENERIC;
409     }
410     p_input->stream.i_mux_rate = 4 * 44100 / 50;
411     vlc_mutex_unlock( &p_input->stream.stream_lock );
412
413     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
414     fmt.audio.i_channels = 2;
415     fmt.audio.i_rate = 44100;
416     fmt.audio.i_bitspersample = 16;
417     fmt.audio.i_blockalign = 4;
418     fmt.i_bitrate = 4 * 44100 * 8;
419
420     p_sys->p_es =  es_out_Add( p_input->p_es_out, &fmt );
421
422     return VLC_SUCCESS;
423 }
424
425 /****************************************************************************
426  * DemuxClose:
427  ****************************************************************************/
428 static void DemuxClose( vlc_object_t * p_this)
429 {
430     input_thread_t *p_input = (input_thread_t*)p_this;
431     demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
432
433     free( p_sys );
434     return;
435 }
436
437 /****************************************************************************
438  * Demux:
439  ****************************************************************************/
440 static int  Demux( input_thread_t * p_input )
441 {
442     demux_sys_t    *p_sys = (demux_sys_t*)p_input->p_demux_data;
443     block_t        *p_block;
444
445
446     input_ClockManageRef( p_input,
447                           p_input->stream.p_selected_program,
448                           p_sys->i_pts );
449
450     if( ( p_block = stream_Block( p_input->s, CDDA_DATA_SIZE ) ) == NULL )
451     {
452         /* eof */
453         return 0;
454     }
455     p_block->i_dts =
456     p_block->i_pts = input_ClockGetTS( p_input,
457                                        p_input->stream.p_selected_program,
458                                        p_sys->i_pts );
459     p_block->i_length = (mtime_t)90000 * (mtime_t)p_block->i_buffer/44100/4;
460
461     p_sys->i_pts += p_block->i_length;
462
463     es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );
464
465     return 1;
466 }