1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003 VideoLAN
5 * $Id: cdda.c,v 1.16 2004/02/24 17:43:31 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" );
56 add_shortcut( "cddasimple" );
60 /* how many blocks VCDRead will read in each loop */
61 #define CDDA_BLOCKS_ONCE 20
62 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
64 /*****************************************************************************
65 * Access: local prototypes
66 *****************************************************************************/
69 vcddev_t *vcddev; /* vcd device descriptor */
70 int i_nb_tracks; /* Nb of tracks (titles) */
71 int i_track; /* Current track */
72 int i_sector; /* Current Sector */
73 int * p_sectors; /* Track sectors */
74 vlc_bool_t b_end_of_track; /* If the end of track was reached */
76 WAVEHEADER waveheader; /* Wave header for the output data */
80 static int Read ( input_thread_t *, byte_t *, size_t );
81 static void Seek ( input_thread_t *, off_t );
82 static int SetArea ( input_thread_t *, input_area_t * );
83 static int SetProgram( input_thread_t *, pgrm_descriptor_t * );
85 /*****************************************************************************
86 * AccessOpen: open cdda
87 *****************************************************************************/
88 static int AccessOpen( vlc_object_t *p_this )
90 input_thread_t * p_input = (input_thread_t *)p_this;
97 input_area_t * p_area;
101 /* parse the options passed in command line : */
102 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
109 while( *psz_parser && *psz_parser != '@' )
114 if( *psz_parser == '@' )
120 i_title = (int)strtol( psz_parser, NULL, 10 );
121 i_title = i_title ? i_title : 1;
126 /* No source specified, so figure it out. */
127 if( !p_input->psz_access )
132 psz_source = config_GetPsz( p_input, "cd-audio" );
133 if( !psz_source ) return -1;
137 if( !(vcddev = ioctl_Open( p_this, psz_source )) )
139 msg_Warn( p_input, "could not open %s", psz_source );
145 p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
148 msg_Err( p_input, "out of memory" );
153 p_sys->vcddev = vcddev;
155 p_input->i_mtu = CDDA_DATA_ONCE;
157 /* We read the Table Of Content information */
158 p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
159 p_sys->vcddev, &p_sys->p_sectors );
160 if( p_sys->i_nb_tracks < 0 )
162 msg_Err( p_input, "unable to count tracks" );
164 else if( p_sys->i_nb_tracks <= 0 )
166 msg_Err( p_input, "no audio tracks found" );
169 if( p_sys->i_nb_tracks <= 0 )
171 ioctl_Close( p_this, p_sys->vcddev );
176 if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
181 /* Set stream and area data */
182 vlc_mutex_lock( &p_input->stream.stream_lock );
184 /* Initialize ES structures */
185 input_InitStream( p_input, 0 );
187 /* cdda input method */
188 p_input->stream.i_method = INPUT_METHOD_CDDA;
190 p_input->stream.b_pace_control = 1;
191 p_input->stream.b_seekable = 1;
192 p_input->stream.i_mux_rate = 44100 * 4 / 50;
194 #define area p_input->stream.pp_areas
195 for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
197 input_AddArea( p_input, i, 1 );
199 /* Absolute start offset and size */
201 (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
203 (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
204 * (off_t)CDDA_DATA_SIZE;
208 p_area = p_input->stream.pp_areas[i_title];
210 SetArea( p_input, p_area );
212 vlc_mutex_unlock( &p_input->stream.stream_lock );
214 p_input->pf_read = Read;
215 p_input->pf_seek = Seek;
216 p_input->pf_set_area = SetArea;
217 p_input->pf_set_program = SetProgram;
219 /* Update default_pts to a suitable value for cdda access */
220 p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
222 /* Build a WAV header for the output data */
223 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
224 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
225 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
226 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
227 p_sys->waveheader.Length = 0; /* we just don't know */
228 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
229 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
230 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
231 SetWLE( &p_sys->waveheader.Modus, 2);
232 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
233 SetWLE( &p_sys->waveheader.BytesPerSample,
234 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
235 SetDWLE( &p_sys->waveheader.BytesPerSec,
236 16 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
237 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
238 p_sys->waveheader.DataLength = 0; /* we just don't know */
239 p_sys->i_header_pos = 0;
244 /*****************************************************************************
245 * AccessClose: closes cdda
246 *****************************************************************************/
247 static void AccessClose( vlc_object_t *p_this )
249 input_thread_t *p_input = (input_thread_t *)p_this;
250 access_sys_t *p_sys = p_input->p_access_data;
252 ioctl_Close( p_this, p_sys->vcddev );
256 /*****************************************************************************
257 * Read: reads from the CDDA into PES packets.
258 *****************************************************************************
259 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
261 *****************************************************************************/
262 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
264 access_sys_t *p_sys = p_input->p_access_data;
265 int i_blocks = i_len / CDDA_DATA_SIZE;
269 if( !p_sys->i_header_pos )
271 p_sys->i_header_pos = sizeof(WAVEHEADER);
272 i_blocks = (i_len - sizeof(WAVEHEADER)) / CDDA_DATA_SIZE;
273 memcpy( p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
274 p_buffer += sizeof(WAVEHEADER);
275 i_read += sizeof(WAVEHEADER);
278 if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
279 p_buffer, i_blocks, CDDA_TYPE ) < 0 )
281 msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
285 for( i_index = 0; i_index < i_blocks; i_index++ )
288 if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
290 input_area_t *p_area;
292 if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
297 vlc_mutex_lock( &p_input->stream.stream_lock );
298 p_area = p_input->stream.pp_areas[
299 p_input->stream.p_selected_area->i_id + 1 ];
301 msg_Dbg( p_input, "new title" );
304 SetArea( p_input, p_area );
305 vlc_mutex_unlock( &p_input->stream.stream_lock );
307 i_read += CDDA_DATA_SIZE;
310 if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
312 msg_Err( p_input, "must read full sectors" );
318 /*****************************************************************************
319 * SetProgram: Does nothing since a CDDA is mono_program
320 *****************************************************************************/
321 static int SetProgram( input_thread_t * p_input,
322 pgrm_descriptor_t * p_program)
327 /*****************************************************************************
328 * SetArea: initialize input data for title x.
329 * It should be called for each user navigation request.
330 ****************************************************************************/
331 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
333 access_sys_t *p_sys = p_input->p_access_data;
336 /* we can't use the interface slider until initilization is complete */
337 p_input->stream.b_seekable = 0;
339 if( p_area != p_input->stream.p_selected_area )
341 /* Change the default area */
342 p_input->stream.p_selected_area = p_area;
344 /* Change the current track */
345 p_sys->i_track = p_area->i_id - 1;
346 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
348 /* Update the navigation variables without triggering a callback */
349 val.i_int = p_area->i_id;
350 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
353 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
355 p_input->stream.p_selected_area->i_tell =
356 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
357 - p_input->stream.p_selected_area->i_start;
359 /* warn interface that something has changed */
360 p_input->stream.b_seekable = 1;
361 p_input->stream.b_changed = 1;
366 /****************************************************************************
368 ****************************************************************************/
369 static void Seek( input_thread_t * p_input, off_t i_off )
371 access_sys_t * p_sys = p_input->p_access_data;
373 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
374 + i_off / (off_t)CDDA_DATA_SIZE;
376 vlc_mutex_lock( &p_input->stream.stream_lock );
377 p_input->stream.p_selected_area->i_tell =
378 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
379 - p_input->stream.p_selected_area->i_start;
380 vlc_mutex_unlock( &p_input->stream.stream_lock );