1 /*****************************************************************************
2 * cddax.c : CD digital audio input module for vlc using libcdio
3 *****************************************************************************
4 * Copyright (C) 2000,2003 VideoLAN
5 * $Id: access.c,v 1.3 2003/11/30 02:41:00 rocky Exp $
7 * Authors: Rocky Bernstein <rocky@panix.com>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <sys/types.h>
35 #include <cdio/cdio.h>
36 #include <cdio/cd_types.h>
49 /* how many blocks Open will read in each loop */
50 #define CDDA_BLOCKS_ONCE 1
51 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
53 /* FIXME: This variable is a hack. Would be nice to eliminate. */
54 static input_thread_t *p_cdda_input = NULL;
56 /*****************************************************************************
58 *****************************************************************************/
59 static int CDDARead ( input_thread_t *, byte_t *, size_t );
60 static void CDDASeek ( input_thread_t *, off_t );
61 static int CDDASetArea ( input_thread_t *, input_area_t * );
62 static int CDDASetProgram ( input_thread_t *, pgrm_descriptor_t * );
64 /****************************************************************************
66 ****************************************************************************/
69 E_(DebugCallback) ( vlc_object_t *p_this, const char *psz_name,
70 vlc_value_t oldval, vlc_value_t val, void *p_data )
74 if (NULL == p_cdda_input) return VLC_EGENERIC;
76 p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
78 if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
79 msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
80 p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
82 p_cdda->i_debug = val.i_int;
86 /* process messages that originate from libcdio. */
88 cdio_log_handler (cdio_log_level_t level, const char message[])
90 cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
94 if (p_cdda->i_debug & INPUT_DBG_CDIO)
95 msg_Dbg( p_cdda_input, message);
98 msg_Warn( p_cdda_input, message);
101 case CDIO_LOG_ASSERT:
102 msg_Err( p_cdda_input, message);
105 msg_Warn( p_cdda_input, message,
106 _("The above message had unknown vcdimager log level"),
113 /*****************************************************************************
115 *****************************************************************************/
117 E_(Open)( vlc_object_t *p_this )
119 input_thread_t * p_input = (input_thread_t *)p_this;
123 cdda_data_t * p_cdda;
128 /* Set where to log errors messages from libcdio. */
129 p_cdda_input = (input_thread_t *)p_this;
131 /* parse the options passed in command line : */
132 psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
139 while( *psz_parser && *psz_parser != '@' )
144 if( *psz_parser == '@' )
150 if ('T' == *psz_parser || 't' == *psz_parser )
153 i_title = (int)strtol( psz_parser, NULL, 10 );
154 i_title = i_title ? i_title : 1;
158 /* No source specified, so figure it out. */
159 if( !p_input->psz_access ) {
163 psz_source = config_GetPsz( p_input, MODULE_STRING "-device" );
165 if( !psz_source || 0==strlen(psz_source) ) {
166 /* Scan for a CD-ROM drive with a CD-DA in it. */
168 cdio_get_devices_with_cap(NULL, CDIO_FS_AUDIO, false);
169 if (NULL == cd_drives) return -1;
170 if (cd_drives[0] == NULL) {
171 cdio_free_device_list(cd_drives);
174 psz_source = strdup(cd_drives[0]);
175 cdio_free_device_list(cd_drives);
180 cdio_log_set_handler ( cdio_log_handler );
182 if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
184 msg_Warn( p_input, "could not open %s", psz_source );
190 p_cdda = malloc( sizeof(cdda_data_t) );
193 msg_Err( p_input, "out of memory" );
198 p_cdda->p_cddev = p_cddev;
199 p_cdda->i_debug = config_GetInt( p_this, MODULE_STRING "-debug" );
200 p_input->p_access_data = (void *)p_cdda;
202 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
204 p_input->i_mtu = CDDA_DATA_ONCE;
206 /* We read the Table Of Content information */
207 p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
208 p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
209 if( p_cdda->i_nb_tracks < 0 )
210 msg_Err( p_input, "unable to count tracks" );
211 else if( p_cdda->i_nb_tracks <= 0 )
212 msg_Err( p_input, "no audio tracks found" );
214 if( p_cdda->i_nb_tracks <= 1)
216 ioctl_Close( p_cdda->p_cddev );
221 if( i_title >= p_cdda->i_nb_tracks || i_title < 1 )
224 /* Set stream and area data */
225 vlc_mutex_lock( &p_input->stream.stream_lock );
227 /* Initialize ES structures */
228 input_InitStream( p_input, 0 );
230 /* cdda input method */
231 p_input->stream.i_method = INPUT_METHOD_CDDA;
233 p_input->stream.b_pace_control = 1;
234 p_input->stream.b_seekable = 1;
235 p_input->stream.i_mux_rate = 44100 * 4 / 50;
237 #define area p_input->stream.pp_areas
238 for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
240 input_AddArea( p_input, i, 1 );
242 /* Absolute start offset and size */
244 (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
246 (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
247 * (off_t)CDIO_CD_FRAMESIZE_RAW;
251 CDDAPlay( p_input, i_title);
253 vlc_mutex_unlock( &p_input->stream.stream_lock );
255 if( !p_input->psz_demux || !*p_input->psz_demux )
257 p_input->psz_demux = "cdda";
260 p_input->pf_read = CDDARead;
261 p_input->pf_seek = CDDASeek;
262 p_input->pf_set_area = CDDASetArea;
263 p_input->pf_set_program = CDDASetProgram;
265 /* Update default_pts to a suitable value for cdda access */
266 p_input->i_pts_delay = config_GetInt( p_input,
267 MODULE_STRING "-caching" ) * 1000;
269 p_cdda->p_intf = intf_Create( p_input, "cddax" );
270 intf_RunThread( p_cdda->p_intf );
275 /*****************************************************************************
276 * CDDAPlay: Arrange things so we play the specified track.
277 * VLC_TRUE is returned if there was no error.
278 *****************************************************************************/
280 CDDAPlay( input_thread_t *p_input, int i_track )
282 cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
284 if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
287 CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
291 /*****************************************************************************
292 * CDDAClose: closes cdda
293 *****************************************************************************/
295 E_(Close)( vlc_object_t *p_this )
297 input_thread_t * p_input = (input_thread_t *)p_this;
298 cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
300 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
301 ioctl_Close( p_cdda->p_cddev );
306 /*****************************************************************************
307 * CDDARead: reads from the CDDA into PES packets.
308 *****************************************************************************
309 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
311 *****************************************************************************/
312 static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
315 cdda_data_t * p_cdda;
320 p_cdda = (cdda_data_t *)p_input->p_access_data;
324 /* Compute the number of blocks we have to read */
326 i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
328 for ( i_index = 0; i_index < i_blocks; i_index++ )
331 if (cdio_read_audio_sector(p_cdda->p_cddev->cdio, p_buffer,
332 p_cdda->i_sector) != 0)
334 msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
339 if ( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
341 input_area_t *p_area;
343 dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
344 "end of track, cur: %u", p_cdda->i_sector );
346 if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )
349 vlc_mutex_lock( &p_input->stream.stream_lock );
350 p_area = p_input->stream.pp_areas[
351 p_input->stream.p_selected_area->i_id + 1 ];
354 CDDASetArea( p_input, p_area );
355 vlc_mutex_unlock( &p_input->stream.stream_lock );
357 i_read += CDIO_CD_FRAMESIZE_RAW;
360 if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
362 msg_Err( p_input, "must read full sectors" );
368 /*****************************************************************************
369 * CDDASetProgram: Does nothing since a CDDA is mono_program
370 *****************************************************************************/
371 static int CDDASetProgram( input_thread_t * p_input,
372 pgrm_descriptor_t * p_program)
374 cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
375 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
379 /*****************************************************************************
380 * CDDASetArea: initialize input data for title x.
381 * It should be called for each user navigation request.
382 ****************************************************************************/
383 static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
385 cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
388 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
390 /* we can't use the interface slider until initilization is complete */
391 p_input->stream.b_seekable = 0;
393 if( p_area != p_input->stream.p_selected_area )
395 /* Change the default area */
396 p_input->stream.p_selected_area = p_area;
398 /* Change the current track */
399 p_cdda->i_track = p_area->i_id - 1;
400 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
402 /* Update the navigation variables without triggering a callback */
403 val.i_int = p_area->i_id;
404 var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
407 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
409 p_input->stream.p_selected_area->i_tell =
410 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
411 - p_input->stream.p_selected_area->i_start;
413 /* warn interface that something has changed */
414 p_input->stream.b_seekable = 1;
415 p_input->stream.b_changed = 1;
420 /****************************************************************************
422 ****************************************************************************/
423 static void CDDASeek( input_thread_t * p_input, off_t i_off )
425 cdda_data_t * p_cdda;
427 p_cdda = (cdda_data_t *) p_input->p_access_data;
429 p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
430 + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
432 vlc_mutex_lock( &p_input->stream.stream_lock );
433 p_input->stream.p_selected_area->i_tell =
434 (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
435 - p_input->stream.p_selected_area->i_start;
437 vlc_mutex_unlock( &p_input->stream.stream_lock );
439 dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
440 "sector %ud, offset: %lld, i_tell: %lld", p_cdda->i_sector, i_off,
441 p_input->stream.p_selected_area->i_tell );