1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003 VideoLAN
5 * $Id: cdda.c,v 1.13 2004/02/05 22:56:12 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>
33 #include "vcd/cdrom.h"
35 typedef struct WAVEHEADER
37 uint32_t MainChunkID; // it will be 'RIFF'
39 uint32_t ChunkTypeID; // it will be 'WAVE'
40 uint32_t SubChunkID; // it will be 'fmt '
41 uint32_t SubChunkLength;
46 uint16_t BytesPerSample;
47 uint16_t BitsPerSample;
48 uint32_t DataChunkID; // it will be 'data'
52 /*****************************************************************************
54 *****************************************************************************/
55 static int AccessOpen ( vlc_object_t * );
56 static void AccessClose( vlc_object_t * );
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60 "Allows you to modify the default caching value for cdda streams. This " \
61 "value should be set in milliseconds units." )
64 set_description( _("Audio CD input") );
66 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
67 CACHING_LONGTEXT, VLC_TRUE );
69 set_capability( "access", 70 );
70 set_callbacks( AccessOpen, AccessClose );
71 add_shortcut( "cdda" );
75 /* how many blocks VCDRead will read in each loop */
76 #define CDDA_BLOCKS_ONCE 20
77 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
79 /*****************************************************************************
80 * Access: local prototypes
81 *****************************************************************************/
84 vcddev_t *vcddev; /* vcd device descriptor */
85 int i_nb_tracks; /* Nb of tracks (titles) */
86 int i_track; /* Current track */
87 int i_sector; /* Current Sector */
88 int * p_sectors; /* Track sectors */
89 vlc_bool_t b_end_of_track; /* If the end of track was reached */
91 WAVEHEADER waveheader; /* Wave header for the output data */
95 static int Read ( input_thread_t *, byte_t *, size_t );
96 static void Seek ( input_thread_t *, off_t );
97 static int SetArea ( input_thread_t *, input_area_t * );
98 static int SetProgram( input_thread_t *, pgrm_descriptor_t * );
100 /*****************************************************************************
101 * AccessOpen: open cdda
102 *****************************************************************************/
103 static int AccessOpen( vlc_object_t *p_this )
105 input_thread_t * p_input = (input_thread_t *)p_this;
106 access_sys_t * p_sys;
112 input_area_t * p_area;
116 /* parse the options passed in command line : */
117 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
124 while( *psz_parser && *psz_parser != '@' )
129 if( *psz_parser == '@' )
135 i_title = (int)strtol( psz_parser, NULL, 10 );
136 i_title = i_title ? i_title : 1;
141 /* No source specified, so figure it out. */
142 if( !p_input->psz_access )
147 psz_source = config_GetPsz( p_input, "cd-audio" );
148 if( !psz_source ) return -1;
152 if( !(vcddev = ioctl_Open( p_this, psz_source )) )
154 msg_Warn( p_input, "could not open %s", psz_source );
160 p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
163 msg_Err( p_input, "out of memory" );
168 p_sys->vcddev = vcddev;
170 p_input->i_mtu = CDDA_DATA_ONCE;
172 /* We read the Table Of Content information */
173 p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
174 p_sys->vcddev, &p_sys->p_sectors );
175 if( p_sys->i_nb_tracks < 0 )
177 msg_Err( p_input, "unable to count tracks" );
179 else if( p_sys->i_nb_tracks <= 0 )
181 msg_Err( p_input, "no audio tracks found" );
184 if( p_sys->i_nb_tracks <= 0 )
186 ioctl_Close( p_this, p_sys->vcddev );
191 if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
196 /* Set stream and area data */
197 vlc_mutex_lock( &p_input->stream.stream_lock );
199 /* Initialize ES structures */
200 input_InitStream( p_input, 0 );
202 /* cdda input method */
203 p_input->stream.i_method = INPUT_METHOD_CDDA;
205 p_input->stream.b_pace_control = 1;
206 p_input->stream.b_seekable = 1;
207 p_input->stream.i_mux_rate = 44100 * 4 / 50;
209 #define area p_input->stream.pp_areas
210 for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
212 input_AddArea( p_input, i, 1 );
214 /* Absolute start offset and size */
216 (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
218 (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
219 * (off_t)CDDA_DATA_SIZE;
223 p_area = p_input->stream.pp_areas[i_title];
225 SetArea( p_input, p_area );
227 vlc_mutex_unlock( &p_input->stream.stream_lock );
229 p_input->pf_read = Read;
230 p_input->pf_seek = Seek;
231 p_input->pf_set_area = SetArea;
232 p_input->pf_set_program = SetProgram;
234 /* Update default_pts to a suitable value for cdda access */
235 p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
237 /* Build a WAV header for the output data */
238 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
239 p_sys->waveheader.Format = 1 /*WAVE_FORMAT_PCM*/;
240 p_sys->waveheader.BitsPerSample = 16;
241 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
242 p_sys->waveheader.Length = 0; /* we just don't know */
243 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
244 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
245 p_sys->waveheader.SubChunkLength = 16;
246 p_sys->waveheader.Modus = 2;
247 p_sys->waveheader.SampleFreq = 44100;
248 p_sys->waveheader.BytesPerSample =
249 p_sys->waveheader.Modus * p_sys->waveheader.BitsPerSample / 8;
250 p_sys->waveheader.BytesPerSec =
251 p_sys->waveheader.BytesPerSample * p_sys->waveheader.SampleFreq;
252 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
253 p_sys->waveheader.DataLength = 0; /* we just don't know */
254 p_sys->i_header_pos = 0;
259 /*****************************************************************************
260 * AccessClose: closes cdda
261 *****************************************************************************/
262 static void AccessClose( vlc_object_t *p_this )
264 input_thread_t *p_input = (input_thread_t *)p_this;
265 access_sys_t *p_sys = p_input->p_access_data;
267 ioctl_Close( p_this, p_sys->vcddev );
271 /*****************************************************************************
272 * Read: reads from the CDDA into PES packets.
273 *****************************************************************************
274 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
276 *****************************************************************************/
277 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
279 access_sys_t *p_sys = p_input->p_access_data;
280 int i_blocks = i_len / CDDA_DATA_SIZE;
284 if( !p_sys->i_header_pos )
286 p_sys->i_header_pos = sizeof(WAVEHEADER);
287 i_blocks = (i_len - sizeof(WAVEHEADER)) / CDDA_DATA_SIZE;
288 memcpy( p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
289 p_buffer += sizeof(WAVEHEADER);
292 if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
293 p_buffer, i_blocks, CDDA_TYPE ) < 0 )
295 msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
299 for( i_index = 0; i_index < i_blocks; i_index++ )
302 if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
304 input_area_t *p_area;
306 if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
311 vlc_mutex_lock( &p_input->stream.stream_lock );
312 p_area = p_input->stream.pp_areas[
313 p_input->stream.p_selected_area->i_id + 1 ];
315 msg_Dbg( p_input, "new title" );
318 SetArea( p_input, p_area );
319 vlc_mutex_unlock( &p_input->stream.stream_lock );
321 i_read += CDDA_DATA_SIZE;
324 if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
326 msg_Err( p_input, "must read full sectors" );
332 /*****************************************************************************
333 * SetProgram: Does nothing since a CDDA is mono_program
334 *****************************************************************************/
335 static int SetProgram( input_thread_t * p_input,
336 pgrm_descriptor_t * p_program)
341 /*****************************************************************************
342 * SetArea: initialize input data for title x.
343 * It should be called for each user navigation request.
344 ****************************************************************************/
345 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
347 access_sys_t *p_sys = p_input->p_access_data;
350 /* we can't use the interface slider until initilization is complete */
351 p_input->stream.b_seekable = 0;
353 if( p_area != p_input->stream.p_selected_area )
355 /* Change the default area */
356 p_input->stream.p_selected_area = p_area;
358 /* Change the current track */
359 p_sys->i_track = p_area->i_id - 1;
360 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
362 /* Update the navigation variables without triggering a callback */
363 val.i_int = p_area->i_id;
364 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
367 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
369 p_input->stream.p_selected_area->i_tell =
370 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
371 - p_input->stream.p_selected_area->i_start;
373 /* warn interface that something has changed */
374 p_input->stream.b_seekable = 1;
375 p_input->stream.b_changed = 1;
380 /****************************************************************************
382 ****************************************************************************/
383 static void Seek( input_thread_t * p_input, off_t i_off )
385 access_sys_t * p_sys = p_input->p_access_data;
387 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
388 + i_off / (off_t)CDDA_DATA_SIZE;
390 vlc_mutex_lock( &p_input->stream.stream_lock );
391 p_input->stream.p_selected_area->i_tell =
392 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
393 - p_input->stream.p_selected_area->i_start;
394 vlc_mutex_unlock( &p_input->stream.stream_lock );