1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003 VideoLAN
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 Open ( vlc_object_t * );
40 static void Close( 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( "access2", 10 );
54 set_callbacks( Open, Close );
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 */
73 input_title_t *title[99]; /* No more that 99 track in a cd-audio */
76 int i_sector; /* Current Sector */
77 int * p_sectors; /* Track sectors */
79 /* Wave header for the output data */
80 WAVEHEADER waveheader;
84 static block_t *Block( access_t * );
85 static int Seek( access_t *, int64_t );
86 static int Control( access_t *, int, va_list );
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
93 access_t *p_access = (access_t*)p_this;
96 char *psz_dup = strdup( p_access->psz_path );
103 /* Command line: cdda://[dev_path][@title] */
104 if( ( psz = strchr( psz_dup, '@' ) ) )
108 i_title = strtol( psz, NULL, 0 );
111 if( *psz_dup == '\0' )
115 /* Only when selected */
116 if( *p_access->psz_access == '\0' )
120 var_Create( p_access, "cd-audio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
121 var_Get( p_access, "cd-audio", &val );
122 psz_dup = val.psz_string;
124 if( *psz_dup == '\0' )
132 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_dup )) == NULL )
134 msg_Warn( p_access, "could not open %s", psz_dup );
140 /* Set up p_access */
141 p_access->pf_read = NULL;
142 p_access->pf_block = Block;
143 p_access->pf_control = Control;
144 p_access->pf_seek = Seek;
145 p_access->info.i_update = 0;
146 p_access->info.i_size = 0;
147 p_access->info.i_pos = 0;
148 p_access->info.b_eof = VLC_FALSE;
149 p_access->info.i_title = 0;
150 p_access->info.i_seekpoint = 0;
151 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
152 memset( p_sys, 0, sizeof( access_sys_t ) );
153 p_sys->vcddev = vcddev;
155 /* We read the Table Of Content information */
156 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
157 p_sys->vcddev, &p_sys->p_sectors );
158 if( p_sys->i_titles < 0 )
160 msg_Err( p_access, "unable to count tracks" );
163 else if( p_sys->i_titles <= 0 )
165 msg_Err( p_access, "no audio tracks found" );
169 /* Build title table */
170 for( i = 0; i < p_sys->i_titles; i++ )
172 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
174 fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[i] );
175 fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+1] );
177 t->i_size = sizeof( WAVEHEADER ) +
178 ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
179 (int64_t)CDDA_DATA_SIZE;
181 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
184 /* Starting title and sector */
185 if( i_title >= p_sys->i_titles )
187 p_sys->i_sector = p_sys->p_sectors[i_title];
188 p_access->info.i_title = i_title;
189 p_access->info.i_size = p_sys->title[i_title]->i_size;
191 /* Build a WAV header for the output data */
192 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
193 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
194 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
195 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
196 p_sys->waveheader.Length = 0; /* we just don't know */
197 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
198 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
199 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
200 SetWLE( &p_sys->waveheader.Modus, 2);
201 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
202 SetWLE( &p_sys->waveheader.BytesPerSample,
203 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
204 SetDWLE( &p_sys->waveheader.BytesPerSec,
205 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
206 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
207 p_sys->waveheader.DataLength = 0; /* we just don't know */
210 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
214 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
219 /*****************************************************************************
221 *****************************************************************************/
222 static void Close( vlc_object_t *p_this )
224 access_t *p_access = (access_t *)p_this;
225 access_sys_t *p_sys = p_access->p_sys;
228 for( i = 0; i < p_sys->i_titles; i++ )
230 vlc_input_title_Delete( p_sys->title[i] );
233 ioctl_Close( p_this, p_sys->vcddev );
237 /*****************************************************************************
238 * Block: read data (CDDA_DATA_ONCE)
239 *****************************************************************************/
240 static block_t *Block( access_t *p_access )
242 access_sys_t *p_sys = p_access->p_sys;
248 if( p_access->info.b_eof )
251 if( p_access->info.i_pos < sizeof( WAVEHEADER ) )
253 /* Return only the header */
254 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
255 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
257 p_block->i_buffer -= p_access->info.i_pos;
258 p_block->p_buffer += p_access->info.i_pos;
260 p_access->info.i_pos += p_block->i_buffer;
265 i_blocks = CDDA_BLOCKS_ONCE;
266 if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 1 ] )
268 i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] - p_sys->i_sector;
269 if( i_blocks <= 0 ) i_blocks = 1; /* Should never occur */
272 p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE );
274 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev, p_sys->i_sector,
275 p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
277 msg_Err( p_access, "cannot read a sector" );
278 block_Release( p_block );
281 i_blocks = 1; /* Next sector */
285 for( i = 0; i < i_blocks; i++ )
287 /* A good sector read */
291 /* Check end of title */
292 if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
294 if( p_access->info.i_title + 1 >= p_sys->i_titles )
296 p_access->info.b_eof = VLC_TRUE;
299 p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
300 p_access->info.i_title++;
301 p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
302 p_access->info.i_pos = sizeof( WAVEHEADER );
308 block_Release( p_block );
314 int i_skip = ( p_access->info.i_pos - sizeof( WAVEHEADER ) ) % CDDA_DATA_SIZE;
316 /* Really read data */
317 p_block->i_buffer = i_read * CDDA_DATA_SIZE;
319 p_block->i_buffer -= i_skip;
320 p_block->p_buffer += i_skip;
322 p_access->info.i_pos += p_block->i_buffer;
328 /****************************************************************************
330 ****************************************************************************/
331 static int Seek( access_t *p_access, int64_t i_pos )
333 access_sys_t * p_sys = p_access->p_sys;
335 p_access->info.i_pos = i_pos;
337 if( i_pos >= sizeof( WAVEHEADER ) )
340 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title] +
341 ( i_pos - sizeof( WAVEHEADER ) ) / (int64_t)CDDA_DATA_SIZE;
342 if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
344 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title + 1] - 1;
348 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title];
350 p_access->info.b_eof = VLC_FALSE;
354 /*****************************************************************************
356 *****************************************************************************/
357 static int Control( access_t *p_access, int i_query, va_list args )
359 access_sys_t *p_sys = p_access->p_sys;
363 input_title_t ***ppp_title;
370 case ACCESS_CAN_SEEK:
371 case ACCESS_CAN_FASTSEEK:
372 case ACCESS_CAN_PAUSE:
373 case ACCESS_CAN_CONTROL_PACE:
374 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
380 pi_int = (int*)va_arg( args, int * );
381 *pi_int = CDDA_DATA_ONCE;
384 case ACCESS_GET_PTS_DELAY:
385 pi_64 = (int64_t*)va_arg( args, int64_t * );
386 var_Get( p_access, "cdda-caching", &val );
387 *pi_64 = val.i_int * 1000;
391 case ACCESS_SET_PAUSE_STATE:
394 case ACCESS_GET_TITLE_INFO:
395 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
396 pi_int = (int*)va_arg( args, int* );
398 /* Duplicate title infos */
399 *pi_int = p_sys->i_titles;
400 *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_titles );
401 for( i = 0; i < p_sys->i_titles; i++ )
403 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
407 case ACCESS_SET_TITLE:
408 i = (int)va_arg( args, int );
409 if( i != p_access->info.i_title )
412 p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
413 p_access->info.i_title = i;
414 p_access->info.i_size = p_sys->title[i]->i_size;
415 p_access->info.i_pos = sizeof(WAVEHEADER);
417 /* Next sector to read */
418 p_sys->i_sector = p_sys->p_sectors[i];
422 case ACCESS_SET_SEEKPOINT:
426 msg_Err( p_access, "unimplemented query in control" );