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