1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: cdda.c,v 1.9 2003/11/29 16:29:24 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 /*****************************************************************************
37 *****************************************************************************/
38 static int AccessOpen ( vlc_object_t * );
39 static void AccessClose( vlc_object_t * );
41 static int DemuxOpen ( vlc_object_t * );
42 static void DemuxClose ( vlc_object_t * );
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 miliseconds units." )
50 set_description( _("CD Audio input") );
51 set_capability( "access", 70 );
52 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
53 set_callbacks( AccessOpen, AccessClose );
54 add_shortcut( "cdda" );
57 set_description( _("CD Audio demux") );
58 set_capability( "demux", 0 );
59 set_callbacks( DemuxOpen, DemuxClose );
60 add_shortcut( "cdda" );
64 /* how many blocks VCDRead will read in each loop */
65 #define CDDA_BLOCKS_ONCE 20
66 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
68 /*****************************************************************************
69 * Access: local prototypes
70 *****************************************************************************/
73 vcddev_t *vcddev; /* vcd device descriptor */
74 int i_nb_tracks; /* Nb of tracks (titles) */
75 int i_track; /* Current track */
76 int i_sector; /* Current Sector */
77 int * p_sectors; /* Track sectors */
78 vlc_bool_t b_end_of_track; /* If the end of track was reached */
82 static int Read ( input_thread_t *, byte_t *, size_t );
83 static void Seek ( input_thread_t *, off_t );
84 static int SetArea ( input_thread_t *, input_area_t * );
85 static int SetProgram( input_thread_t *, pgrm_descriptor_t * );
87 /*****************************************************************************
88 * AccessOpen: open cdda
89 *****************************************************************************/
90 static int AccessOpen( vlc_object_t *p_this )
92 input_thread_t * p_input = (input_thread_t *)p_this;
99 input_area_t * p_area;
103 /* parse the options passed in command line : */
104 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
111 while( *psz_parser && *psz_parser != '@' )
116 if( *psz_parser == '@' )
122 i_title = (int)strtol( psz_parser, NULL, 10 );
123 i_title = i_title ? i_title : 1;
128 if( !p_input->psz_access )
133 psz_source = config_GetPsz( p_input, "vcd" );
134 if( !psz_source ) return -1;
138 if( !(vcddev = ioctl_Open( p_this, psz_source )) )
140 msg_Warn( p_input, "could not open %s", psz_source );
146 p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
149 msg_Err( p_input, "out of memory" );
154 p_sys->vcddev = vcddev;
156 p_input->i_mtu = CDDA_DATA_ONCE;
158 /* We read the Table Of Content information */
159 p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
160 p_sys->vcddev, &p_sys->p_sectors );
161 if( p_sys->i_nb_tracks < 0 )
163 msg_Err( p_input, "unable to count tracks" );
165 else if( p_sys->i_nb_tracks <= 0 )
167 msg_Err( p_input, "no audio tracks found" );
170 if( p_sys->i_nb_tracks <= 1)
172 ioctl_Close( p_this, p_sys->vcddev );
177 if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
182 /* Set stream and area data */
183 vlc_mutex_lock( &p_input->stream.stream_lock );
185 /* Initialize ES structures */
186 input_InitStream( p_input, 0 );
188 /* cdda input method */
189 p_input->stream.i_method = INPUT_METHOD_CDDA;
191 p_input->stream.b_pace_control = 1;
192 p_input->stream.b_seekable = 1;
193 p_input->stream.i_mux_rate = 44100 * 4 / 50;
195 #define area p_input->stream.pp_areas
196 for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
198 input_AddArea( p_input, i, 1 );
200 /* Absolute start offset and size */
202 (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
204 (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
205 * (off_t)CDDA_DATA_SIZE;
209 p_area = p_input->stream.pp_areas[i_title];
211 SetArea( p_input, p_area );
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
215 if( !p_input->psz_demux || !*p_input->psz_demux )
217 p_input->psz_demux = "cdda";
220 p_input->pf_read = Read;
221 p_input->pf_seek = Seek;
222 p_input->pf_set_area = SetArea;
223 p_input->pf_set_program = SetProgram;
225 /* Update default_pts to a suitable value for cdda access */
226 p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
231 /*****************************************************************************
232 * AccessClose: closes cdda
233 *****************************************************************************/
234 static void AccessClose( vlc_object_t *p_this )
236 input_thread_t *p_input = (input_thread_t *)p_this;
237 access_sys_t *p_sys = p_input->p_access_data;
239 ioctl_Close( p_this, p_sys->vcddev );
243 /*****************************************************************************
244 * Read: reads from the CDDA into PES packets.
245 *****************************************************************************
246 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
248 *****************************************************************************/
249 static int Read( input_thread_t * p_input, byte_t * p_buffer,
252 access_sys_t *p_sys = p_input->p_access_data;
253 int i_blocks = i_len / CDDA_DATA_SIZE;
258 if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
259 p_buffer, i_blocks, CDDA_TYPE ) < 0 )
261 msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
265 for( i_index = 0; i_index < i_blocks; i_index++ )
268 if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
270 input_area_t *p_area;
272 if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
277 vlc_mutex_lock( &p_input->stream.stream_lock );
278 p_area = p_input->stream.pp_areas[
279 p_input->stream.p_selected_area->i_id + 1 ];
281 msg_Dbg( p_input, "new title" );
284 SetArea( p_input, p_area );
285 vlc_mutex_unlock( &p_input->stream.stream_lock );
287 i_read += CDDA_DATA_SIZE;
290 if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
292 msg_Err( p_input, "must read full sectors" );
299 /*****************************************************************************
300 * SetProgram: Does nothing since a CDDA is mono_program
301 *****************************************************************************/
302 static int SetProgram( input_thread_t * p_input,
303 pgrm_descriptor_t * p_program)
309 /*****************************************************************************
310 * SetArea: initialize input data for title x.
311 * It should be called for each user navigation request.
312 ****************************************************************************/
313 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
315 access_sys_t *p_sys = p_input->p_access_data;
318 /* we can't use the interface slider until initilization is complete */
319 p_input->stream.b_seekable = 0;
321 if( p_area != p_input->stream.p_selected_area )
323 /* Change the default area */
324 p_input->stream.p_selected_area = p_area;
326 /* Change the current track */
327 p_sys->i_track = p_area->i_id - 1;
328 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
330 /* Update the navigation variables without triggering a callback */
331 val.i_int = p_area->i_id;
332 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
335 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
337 p_input->stream.p_selected_area->i_tell =
338 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
339 - p_input->stream.p_selected_area->i_start;
341 /* warn interface that something has changed */
342 p_input->stream.b_seekable = 1;
343 p_input->stream.b_changed = 1;
349 /****************************************************************************
351 ****************************************************************************/
352 static void Seek( input_thread_t * p_input, off_t i_off )
354 access_sys_t * p_sys = p_input->p_access_data;
356 p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
357 + i_off / (off_t)CDDA_DATA_SIZE;
359 vlc_mutex_lock( &p_input->stream.stream_lock );
360 p_input->stream.p_selected_area->i_tell =
361 (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
362 - p_input->stream.p_selected_area->i_start;
363 vlc_mutex_unlock( &p_input->stream.stream_lock );
369 /*****************************************************************************
370 * Demux: local prototypes
371 *****************************************************************************/
378 static int Demux ( input_thread_t * p_input );
380 /****************************************************************************
382 ****************************************************************************/
383 static int DemuxOpen ( vlc_object_t * p_this)
385 input_thread_t *p_input = (input_thread_t *)p_this;
390 if( p_input->stream.i_method != INPUT_METHOD_CDDA )
395 p_input->pf_demux = Demux;
396 p_input->pf_rewind = NULL;
397 p_input->pf_demux_control = demux_vaControlDefault;
398 p_input->p_demux_data = p_sys = malloc( sizeof( es_descriptor_t ) );
401 vlc_mutex_lock( &p_input->stream.stream_lock );
402 if( input_InitStream( p_input, 0 ) == -1)
404 vlc_mutex_unlock( &p_input->stream.stream_lock );
405 msg_Err( p_input, "cannot init stream" );
409 p_input->stream.i_mux_rate = 4 * 44100 / 50;
410 vlc_mutex_unlock( &p_input->stream.stream_lock );
412 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', 'r', 'a', 'w' ) );
413 fmt.audio.i_channels = 2;
414 fmt.audio.i_rate = 44100;
415 fmt.audio.i_bitspersample = 16;
416 fmt.audio.i_blockalign = 4;
417 fmt.i_bitrate = 4 * 44100 * 8;
419 p_sys->p_es = es_out_Add( p_input->p_es_out, &fmt );
424 /****************************************************************************
426 ****************************************************************************/
427 static void DemuxClose( vlc_object_t * p_this)
429 input_thread_t *p_input = (input_thread_t*)p_this;
430 demux_sys_t *p_sys = (demux_sys_t*)p_input->p_demux_data;
436 /****************************************************************************
438 ****************************************************************************/
439 static int Demux( input_thread_t * p_input )
441 demux_sys_t *p_sys = (demux_sys_t*)p_input->p_demux_data;
445 input_ClockManageRef( p_input,
446 p_input->stream.p_selected_program,
449 if( ( p_block = stream_Block( p_input->s, CDDA_DATA_SIZE ) ) == NULL )
455 p_block->i_pts = input_ClockGetTS( p_input,
456 p_input->stream.p_selected_program,
458 p_block->i_length = (mtime_t)90000 * (mtime_t)p_block->i_buffer/44100/4;
460 p_sys->i_pts += p_block->i_length;
462 es_out_Send( p_input->p_es_out, p_sys->p_es, p_block );