]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
* modules/access/cdda*: removed definition of WAVEHEADER.
[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.15 2004/02/14 17:25:39 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 "codecs.h"
34 #include "vcd/cdrom.h"
35
36 /*****************************************************************************
37  * Module descriptior
38  *****************************************************************************/
39 static int  AccessOpen ( vlc_object_t * );
40 static void AccessClose( 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
50     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
51                  CACHING_LONGTEXT, VLC_TRUE );
52
53     set_capability( "access", 70 );
54     set_callbacks( AccessOpen, AccessClose );
55     add_shortcut( "cdda" );
56 vlc_module_end();
57
58
59 /* how many blocks VCDRead will read in each loop */
60 #define CDDA_BLOCKS_ONCE 20
61 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
62
63 /*****************************************************************************
64  * Access: local prototypes
65  *****************************************************************************/
66 struct access_sys_t
67 {
68     vcddev_t    *vcddev;                            /* vcd device descriptor */
69     int         i_nb_tracks;                        /* Nb of tracks (titles) */
70     int         i_track;                                    /* Current track */
71     int         i_sector;                                  /* Current Sector */
72     int *       p_sectors;                                  /* Track sectors */
73     vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
74
75     WAVEHEADER  waveheader;               /* Wave header for the output data */
76     int         i_header_pos;
77 };
78
79 static int  Read      ( input_thread_t *, byte_t *, size_t );
80 static void Seek      ( input_thread_t *, off_t );
81 static int  SetArea   ( input_thread_t *, input_area_t * );
82 static int  SetProgram( input_thread_t *, pgrm_descriptor_t * );
83
84 /*****************************************************************************
85  * AccessOpen: open cdda
86  *****************************************************************************/
87 static int AccessOpen( vlc_object_t *p_this )
88 {
89     input_thread_t *        p_input = (input_thread_t *)p_this;
90     access_sys_t *          p_sys;
91
92     char *                  psz_orig;
93     char *                  psz_parser;
94     char *                  psz_source;
95     int                     i;
96     input_area_t *          p_area;
97     int                     i_title = 1;
98     vcddev_t                *vcddev;
99
100     /* parse the options passed in command line : */
101     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
102
103     if( !psz_orig )
104     {
105         return( -1 );
106     }
107
108     while( *psz_parser && *psz_parser != '@' )
109     {
110         psz_parser++;
111     }
112
113     if( *psz_parser == '@' )
114     {
115         /* Found options */
116         *psz_parser = '\0';
117         ++psz_parser;
118
119         i_title = (int)strtol( psz_parser, NULL, 10 );
120         i_title = i_title ? i_title : 1;
121     }
122
123     if( !*psz_source )
124     {
125         /* No source specified, so figure it out. */
126         if( !p_input->psz_access )
127         {
128             free( psz_orig );
129             return VLC_EGENERIC;
130         }
131         psz_source = config_GetPsz( p_input, "cd-audio" );
132         if( !psz_source ) return -1;
133     }
134
135     /* Open CDDA */
136     if( !(vcddev = ioctl_Open( p_this, psz_source )) )
137     {
138         msg_Warn( p_input, "could not open %s", psz_source );
139         free( psz_source );
140         return VLC_EGENERIC;
141     }
142     free( psz_source );
143
144     p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
145     if( p_sys == NULL )
146     {
147         msg_Err( p_input, "out of memory" );
148         free( psz_source );
149         return VLC_EGENERIC;
150     }
151
152     p_sys->vcddev = vcddev;
153
154     p_input->i_mtu = CDDA_DATA_ONCE;
155
156     /* We read the Table Of Content information */
157     p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
158                                              p_sys->vcddev, &p_sys->p_sectors );
159     if( p_sys->i_nb_tracks < 0 )
160     {
161         msg_Err( p_input, "unable to count tracks" );
162     }
163     else if( p_sys->i_nb_tracks <= 0 )
164     {
165         msg_Err( p_input, "no audio tracks found" );
166     }
167
168     if( p_sys->i_nb_tracks <= 0 )
169     {
170         ioctl_Close( p_this, p_sys->vcddev );
171         free( p_sys );
172         return VLC_EGENERIC;
173     }
174
175     if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
176     {
177         i_title = 1;
178     }
179
180     /* Set stream and area data */
181     vlc_mutex_lock( &p_input->stream.stream_lock );
182
183     /* Initialize ES structures */
184     input_InitStream( p_input, 0 );
185
186     /* cdda input method */
187     p_input->stream.i_method = INPUT_METHOD_CDDA;
188
189     p_input->stream.b_pace_control = 1;
190     p_input->stream.b_seekable = 1;
191     p_input->stream.i_mux_rate = 44100 * 4 / 50;
192
193 #define area p_input->stream.pp_areas
194     for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
195     {
196         input_AddArea( p_input, i, 1 );
197
198         /* Absolute start offset and size */
199         area[i]->i_start =
200             (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
201         area[i]->i_size =
202             (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
203             * (off_t)CDDA_DATA_SIZE;
204     }
205 #undef area
206
207     p_area = p_input->stream.pp_areas[i_title];
208
209     SetArea( p_input, p_area );
210
211     vlc_mutex_unlock( &p_input->stream.stream_lock );
212
213     p_input->pf_read = Read;
214     p_input->pf_seek = Seek;
215     p_input->pf_set_area = SetArea;
216     p_input->pf_set_program = SetProgram;
217
218     /* Update default_pts to a suitable value for cdda access */
219     p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
220
221     /* Build a WAV header for the output data */
222     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
223     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
224     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
225     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
226     p_sys->waveheader.Length = 0;                     /* we just don't know */
227     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
228     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
229     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
230     SetWLE( &p_sys->waveheader.Modus, 2);
231     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
232     SetWLE( &p_sys->waveheader.BytesPerSample,
233             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
234     SetDWLE( &p_sys->waveheader.BytesPerSec,
235              16 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
236     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
237     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
238     p_sys->i_header_pos = 0;
239
240     return VLC_SUCCESS;
241 }
242
243 /*****************************************************************************
244  * AccessClose: closes cdda
245  *****************************************************************************/
246 static void AccessClose( vlc_object_t *p_this )
247 {
248     input_thread_t *p_input = (input_thread_t *)p_this;
249     access_sys_t   *p_sys = p_input->p_access_data;
250
251     ioctl_Close( p_this, p_sys->vcddev );
252     free( p_sys );
253 }
254
255 /*****************************************************************************
256  * Read: reads from the CDDA into PES packets.
257  *****************************************************************************
258  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
259  * bytes.
260  *****************************************************************************/
261 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
262 {
263     access_sys_t *p_sys = p_input->p_access_data;
264     int          i_blocks = i_len / CDDA_DATA_SIZE;
265     int          i_read = 0;
266     int          i_index;
267
268     if( !p_sys->i_header_pos )
269     {
270         p_sys->i_header_pos = sizeof(WAVEHEADER);
271         i_blocks = (i_len - sizeof(WAVEHEADER)) / CDDA_DATA_SIZE;
272         memcpy( p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
273         p_buffer += sizeof(WAVEHEADER);
274         i_read += sizeof(WAVEHEADER);
275     }
276
277     if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
278                            p_buffer, i_blocks, CDDA_TYPE ) < 0 )
279     {
280         msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
281         return -1;
282     }
283
284     for( i_index = 0; i_index < i_blocks; i_index++ )
285     {
286         p_sys->i_sector ++;
287         if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
288         {
289             input_area_t *p_area;
290
291             if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
292             {
293                 return 0; /* EOF */
294             }
295
296             vlc_mutex_lock( &p_input->stream.stream_lock );
297             p_area = p_input->stream.pp_areas[
298                     p_input->stream.p_selected_area->i_id + 1 ];
299
300             msg_Dbg( p_input, "new title" );
301
302             p_area->i_part = 1;
303             SetArea( p_input, p_area );
304             vlc_mutex_unlock( &p_input->stream.stream_lock );
305         }
306         i_read += CDDA_DATA_SIZE;
307     }
308
309     if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
310     {
311         msg_Err( p_input, "must read full sectors" );
312     }
313
314     return i_read;
315 }
316
317 /*****************************************************************************
318  * SetProgram: Does nothing since a CDDA is mono_program
319  *****************************************************************************/
320 static int SetProgram( input_thread_t * p_input,
321                            pgrm_descriptor_t * p_program)
322 {
323     return VLC_EGENERIC;
324 }
325
326 /*****************************************************************************
327  * SetArea: initialize input data for title x.
328  * It should be called for each user navigation request.
329  ****************************************************************************/
330 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
331 {
332     access_sys_t *p_sys = p_input->p_access_data;
333     vlc_value_t  val;
334
335     /* we can't use the interface slider until initilization is complete */
336     p_input->stream.b_seekable = 0;
337
338     if( p_area != p_input->stream.p_selected_area )
339     {
340         /* Change the default area */
341         p_input->stream.p_selected_area = p_area;
342
343         /* Change the current track */
344         p_sys->i_track = p_area->i_id - 1;
345         p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
346
347         /* Update the navigation variables without triggering a callback */
348         val.i_int = p_area->i_id;
349         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
350     }
351
352     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
353
354     p_input->stream.p_selected_area->i_tell =
355         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
356          - p_input->stream.p_selected_area->i_start;
357
358     /* warn interface that something has changed */
359     p_input->stream.b_seekable = 1;
360     p_input->stream.b_changed = 1;
361
362     return 0;
363 }
364
365 /****************************************************************************
366  * Seek
367  ****************************************************************************/
368 static void Seek( input_thread_t * p_input, off_t i_off )
369 {
370     access_sys_t * p_sys = p_input->p_access_data;
371
372     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
373                        + i_off / (off_t)CDDA_DATA_SIZE;
374
375     vlc_mutex_lock( &p_input->stream.stream_lock );
376     p_input->stream.p_selected_area->i_tell =
377         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
378          - p_input->stream.p_selected_area->i_start;
379     vlc_mutex_unlock( &p_input->stream.stream_lock );
380 }