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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc/input.h>
34 #include "vcd/cdrom.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int AccessOpen ( vlc_object_t * );
40 static void AccessClose( vlc_object_t * );
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." )
48 set_description( _("Audio CD input") );
50 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
51 CACHING_LONGTEXT, VLC_TRUE );
53 set_capability( "access", 70 );
54 set_callbacks( AccessOpen, AccessClose );
55 add_shortcut( "cdda" );
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)
63 /*****************************************************************************
64 * Access: local prototypes
65 *****************************************************************************/
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 */
75 WAVEHEADER waveheader; /* Wave header for the output data */
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 * );
84 /*****************************************************************************
85 * AccessOpen: open cdda
86 *****************************************************************************/
87 static int AccessOpen( vlc_object_t *p_this )
89 input_thread_t * p_input = (input_thread_t *)p_this;
96 input_area_t * p_area;
100 /* parse the options passed in command line : */
101 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
108 while( *psz_parser && *psz_parser != '@' )
113 if( *psz_parser == '@' )
119 i_title = (int)strtol( psz_parser, NULL, 10 );
120 i_title = i_title ? i_title : 1;
125 /* No source specified, so figure it out. */
126 if( !p_input->psz_access )
131 psz_source = config_GetPsz( p_input, "cd-audio" );
132 if( !psz_source ) return -1;
136 if( !(vcddev = ioctl_Open( p_this, psz_source )) )
138 msg_Warn( p_input, "could not open %s", psz_source );
144 p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
147 msg_Err( p_input, "out of memory" );
152 p_sys->vcddev = vcddev;
154 p_input->i_mtu = CDDA_DATA_ONCE;
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 )
161 msg_Err( p_input, "unable to count tracks" );
163 else if( p_sys->i_nb_tracks <= 0 )
165 msg_Err( p_input, "no audio tracks found" );
168 if( p_sys->i_nb_tracks <= 0 )
170 ioctl_Close( p_this, p_sys->vcddev );
175 if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
180 /* Set stream and area data */
181 vlc_mutex_lock( &p_input->stream.stream_lock );
183 /* Initialize ES structures */
184 input_InitStream( p_input, 0 );
186 /* cdda input method */
187 p_input->stream.i_method = INPUT_METHOD_CDDA;
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;
193 #define area p_input->stream.pp_areas
194 for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
196 input_AddArea( p_input, i, 1 );
198 /* Absolute start offset and size */
200 (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
202 (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
203 * (off_t)CDDA_DATA_SIZE;
207 p_area = p_input->stream.pp_areas[i_title];
209 SetArea( p_input, p_area );
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
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;
218 /* Update default_pts to a suitable value for cdda access */
219 p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
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;
243 /*****************************************************************************
244 * AccessClose: closes cdda
245 *****************************************************************************/
246 static void AccessClose( vlc_object_t *p_this )
248 input_thread_t *p_input = (input_thread_t *)p_this;
249 access_sys_t *p_sys = p_input->p_access_data;
251 ioctl_Close( p_this, p_sys->vcddev );
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
260 *****************************************************************************/
261 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
263 access_sys_t *p_sys = p_input->p_access_data;
264 int i_blocks = i_len / CDDA_DATA_SIZE;
268 if( !p_sys->i_header_pos )
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);
277 if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
278 p_buffer, i_blocks, CDDA_TYPE ) < 0 )
280 msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
284 for( i_index = 0; i_index < i_blocks; i_index++ )
287 if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
289 input_area_t *p_area;
291 if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
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 ];
300 msg_Dbg( p_input, "new title" );
303 SetArea( p_input, p_area );
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
306 i_read += CDDA_DATA_SIZE;
309 if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
311 msg_Err( p_input, "must read full sectors" );
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)
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 )
332 access_sys_t *p_sys = p_input->p_access_data;
335 /* we can't use the interface slider until initilization is complete */
336 p_input->stream.b_seekable = 0;
338 if( p_area != p_input->stream.p_selected_area )
340 /* Change the default area */
341 p_input->stream.p_selected_area = p_area;
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];
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 );
352 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
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;
358 /* warn interface that something has changed */
359 p_input->stream.b_seekable = 1;
360 p_input->stream.b_changed = 1;
365 /****************************************************************************
367 ****************************************************************************/
368 static void Seek( input_thread_t * p_input, off_t i_off )
370 access_sys_t * p_sys = p_input->p_access_data;
372 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
373 + i_off / (off_t)CDDA_DATA_SIZE;
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 );