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") );
49 set_capability( "access2", 10 );
50 set_callbacks( Open, Close );
52 add_usage_hint( N_("[cdda:][device][@[track]]") );
53 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
54 CACHING_LONGTEXT, VLC_TRUE );
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 );
102 /* Command line: cdda://[dev_path][@title] */
103 if( ( psz = strchr( psz_dup, '@' ) ) )
107 i_title = strtol( psz, NULL, 0 );
110 if( *psz_dup == '\0' )
114 /* Only when selected */
115 if( strcmp( p_access->psz_access, "cdda" ) )
119 psz_dup = var_CreateGetString( p_access, "cd-audio" );
120 if( *psz_dup == '\0' )
128 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_dup )) == NULL )
130 msg_Warn( p_access, "could not open %s", psz_dup );
136 /* Set up p_access */
137 p_access->pf_read = NULL;
138 p_access->pf_block = Block;
139 p_access->pf_control = Control;
140 p_access->pf_seek = Seek;
141 p_access->info.i_update = 0;
142 p_access->info.i_size = 0;
143 p_access->info.i_pos = 0;
144 p_access->info.b_eof = VLC_FALSE;
145 p_access->info.i_title = 0;
146 p_access->info.i_seekpoint = 0;
147 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
148 memset( p_sys, 0, sizeof( access_sys_t ) );
149 p_sys->vcddev = vcddev;
150 p_sys->b_header = VLC_FALSE;
152 /* We read the Table Of Content information */
153 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
154 p_sys->vcddev, &p_sys->p_sectors );
155 if( p_sys->i_titles < 0 )
157 msg_Err( p_access, "unable to count tracks" );
160 else if( p_sys->i_titles <= 0 )
162 msg_Err( p_access, "no audio tracks found" );
166 /* Build title table */
167 for( i = 0; i < p_sys->i_titles; i++ )
169 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
171 msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[i] );
172 msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+1] );
174 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
175 (int64_t)CDDA_DATA_SIZE;
177 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
180 /* Starting title and sector */
181 if( i_title >= p_sys->i_titles ) i_title = 0;
182 p_sys->i_sector = p_sys->p_sectors[i_title];
183 p_access->info.i_title = i_title;
184 p_access->info.i_size = p_sys->title[i_title]->i_size;
186 /* Build a WAV header for the output data */
187 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
188 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
189 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
190 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
191 p_sys->waveheader.Length = 0; /* we just don't know */
192 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
193 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
194 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
195 SetWLE( &p_sys->waveheader.Modus, 2);
196 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
197 SetWLE( &p_sys->waveheader.BytesPerSample,
198 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
199 SetDWLE( &p_sys->waveheader.BytesPerSec,
200 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
201 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
202 p_sys->waveheader.DataLength = 0; /* we just don't know */
205 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
209 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
214 /*****************************************************************************
216 *****************************************************************************/
217 static void Close( vlc_object_t *p_this )
219 access_t *p_access = (access_t *)p_this;
220 access_sys_t *p_sys = p_access->p_sys;
223 for( i = 0; i < p_sys->i_titles; i++ )
225 vlc_input_title_Delete( p_sys->title[i] );
228 ioctl_Close( p_this, p_sys->vcddev );
232 /*****************************************************************************
233 * Block: read data (CDDA_DATA_ONCE)
234 *****************************************************************************/
235 static block_t *Block( access_t *p_access )
237 access_sys_t *p_sys = p_access->p_sys;
238 int i_blocks = CDDA_BLOCKS_ONCE;
241 /* Check end of file */
242 if( p_access->info.b_eof ) return NULL;
244 if( !p_sys->b_header )
246 /* Return only the header */
247 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
248 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
249 p_sys->b_header = VLC_TRUE;
253 /* Check end of title */
254 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
256 if( p_access->info.i_title + 1 >= p_sys->i_titles )
258 p_access->info.b_eof = VLC_TRUE;
262 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
263 p_access->info.i_title++;
264 p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
265 p_access->info.i_pos = 0;
268 /* Don't read after the end of a title */
269 if( p_sys->i_sector + i_blocks >=
270 p_sys->p_sectors[p_access->info.i_title + 1] )
272 i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] -
276 /* Do the actual reading */
277 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
279 msg_Err( p_access, "cannot get a new block of size: %i",
280 i_blocks * CDDA_DATA_SIZE );
284 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
285 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
287 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
288 block_Release( p_block );
290 /* Try to skip one sector (in case of bad sectors) */
292 p_access->info.i_pos += CDDA_DATA_SIZE;
296 /* Update a few values */
297 p_sys->i_sector += i_blocks;
298 p_access->info.i_pos += p_block->i_buffer;
303 /****************************************************************************
305 ****************************************************************************/
306 static int Seek( access_t *p_access, int64_t i_pos )
308 access_sys_t *p_sys = p_access->p_sys;
310 /* Next sector to read */
311 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title] +
312 i_pos / CDDA_DATA_SIZE;
313 p_access->info.i_pos = i_pos;
318 /*****************************************************************************
320 *****************************************************************************/
321 static int Control( access_t *p_access, int i_query, va_list args )
323 access_sys_t *p_sys = p_access->p_sys;
327 input_title_t ***ppp_title;
333 case ACCESS_CAN_SEEK:
334 case ACCESS_CAN_FASTSEEK:
335 case ACCESS_CAN_PAUSE:
336 case ACCESS_CAN_CONTROL_PACE:
337 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
343 pi_int = (int*)va_arg( args, int * );
344 *pi_int = CDDA_DATA_ONCE;
347 case ACCESS_GET_PTS_DELAY:
348 pi_64 = (int64_t*)va_arg( args, int64_t * );
349 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
353 case ACCESS_SET_PAUSE_STATE:
356 case ACCESS_GET_TITLE_INFO:
357 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
358 pi_int = (int*)va_arg( args, int* );
360 /* Duplicate title infos */
361 *pi_int = p_sys->i_titles;
362 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
363 for( i = 0; i < p_sys->i_titles; i++ )
365 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
369 case ACCESS_SET_TITLE:
370 i = (int)va_arg( args, int );
371 if( i != p_access->info.i_title )
374 p_access->info.i_update |=
375 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
376 p_access->info.i_title = i;
377 p_access->info.i_size = p_sys->title[i]->i_size;
378 p_access->info.i_pos = 0;
380 /* Next sector to read */
381 p_sys->i_sector = p_sys->p_sectors[i];
385 case ACCESS_SET_SEEKPOINT:
389 msg_Err( p_access, "unimplemented query in control" );