00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdlib.h>
00029
00030 #include <vlc/vlc.h>
00031 #include <vlc/input.h>
00032
00033 #include "codecs.h"
00034 #include "vcd/cdrom.h"
00035
00036
00037
00038
00039 static int Open ( vlc_object_t * );
00040 static void Close( vlc_object_t * );
00041
00042 #define CACHING_TEXT N_("Caching value in ms")
00043 #define CACHING_LONGTEXT N_( \
00044 "Allows you to modify the default caching value for cdda streams. This " \
00045 "value should be set in milliseconds units." )
00046
00047 vlc_module_begin();
00048 set_shortname( _("Audio CD"));
00049 set_description( _("Audio CD input") );
00050 set_capability( "access2", 10 );
00051 set_category( CAT_INPUT );
00052 set_subcategory( SUBCAT_INPUT_ACCESS );
00053 set_callbacks( Open, Close );
00054
00055 add_usage_hint( N_("[cdda:][device][@[track]]") );
00056 add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
00057 CACHING_LONGTEXT, VLC_TRUE );
00058 add_shortcut( "cdda" );
00059 add_shortcut( "cddasimple" );
00060 vlc_module_end();
00061
00062
00063
00064 #define CDDA_BLOCKS_ONCE 20
00065 #define CDDA_DATA_ONCE (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
00066
00067
00068
00069
00070 struct access_sys_t
00071 {
00072 vcddev_t *vcddev;
00073
00074
00075 int i_titles;
00076 input_title_t *title[99];
00077
00078
00079 int i_sector;
00080 int * p_sectors;
00081
00082
00083 WAVEHEADER waveheader;
00084 vlc_bool_t b_header;
00085 };
00086
00087 static block_t *Block( access_t * );
00088 static int Seek( access_t *, int64_t );
00089 static int Control( access_t *, int, va_list );
00090
00091
00092
00093
00094 static int Open( vlc_object_t *p_this )
00095 {
00096 access_t *p_access = (access_t*)p_this;
00097 access_sys_t *p_sys;
00098
00099 vcddev_t *vcddev;
00100 char *psz_name;
00101 int i;
00102
00103 if( !p_access->psz_path || !*p_access->psz_path )
00104 {
00105
00106 if( !p_this->b_force ) return VLC_EGENERIC;
00107
00108 psz_name = var_CreateGetString( p_this, "cd-audio" );
00109 if( !psz_name || !*psz_name )
00110 {
00111 if( psz_name ) free( psz_name );
00112 return VLC_EGENERIC;
00113 }
00114 }
00115 else psz_name = strdup( p_access->psz_path );
00116
00117
00118 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
00119 {
00120 msg_Warn( p_access, "could not open %s", psz_name );
00121 free( psz_name );
00122 return VLC_EGENERIC;
00123 }
00124 free( psz_name );
00125
00126
00127 p_access->pf_read = NULL;
00128 p_access->pf_block = Block;
00129 p_access->pf_control = Control;
00130 p_access->pf_seek = Seek;
00131 p_access->info.i_update = 0;
00132 p_access->info.i_size = 0;
00133 p_access->info.i_pos = 0;
00134 p_access->info.b_eof = VLC_FALSE;
00135 p_access->info.i_title = 0;
00136 p_access->info.i_seekpoint = 0;
00137 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
00138 memset( p_sys, 0, sizeof( access_sys_t ) );
00139 p_sys->vcddev = vcddev;
00140 p_sys->b_header = VLC_FALSE;
00141
00142
00143 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
00144 p_sys->vcddev, &p_sys->p_sectors );
00145 if( p_sys->i_titles < 0 )
00146 {
00147 msg_Err( p_access, "unable to count tracks" );
00148 goto error;
00149 }
00150 else if( p_sys->i_titles <= 0 )
00151 {
00152 msg_Err( p_access, "no audio tracks found" );
00153 goto error;
00154 }
00155
00156
00157 for( i = 0; i < p_sys->i_titles; i++ )
00158 {
00159 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
00160
00161 msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[i] );
00162 msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+1] );
00163
00164 asprintf( &t->psz_name, _("Track %i"), i + 1 );
00165 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
00166 (int64_t)CDDA_DATA_SIZE;
00167
00168 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
00169 }
00170
00171 p_sys->i_sector = p_sys->p_sectors[0];
00172 p_access->info.i_size = p_sys->title[0]->i_size;
00173
00174
00175 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
00176 SetWLE( &p_sys->waveheader.Format, 1 );
00177 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
00178 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
00179 p_sys->waveheader.Length = 0;
00180 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
00181 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
00182 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
00183 SetWLE( &p_sys->waveheader.Modus, 2);
00184 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
00185 SetWLE( &p_sys->waveheader.BytesPerSample,
00186 2 * 16 / 8 );
00187 SetDWLE( &p_sys->waveheader.BytesPerSec,
00188 2*16/8 * 44100 );
00189 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
00190 p_sys->waveheader.DataLength = 0;
00191
00192 p_access->info.i_update |= INPUT_UPDATE_META;
00193
00194
00195 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
00196 return VLC_SUCCESS;
00197
00198 error:
00199 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
00200 free( p_sys );
00201 return VLC_EGENERIC;
00202 }
00203
00204
00205
00206
00207 static void Close( vlc_object_t *p_this )
00208 {
00209 access_t *p_access = (access_t *)p_this;
00210 access_sys_t *p_sys = p_access->p_sys;
00211 int i;
00212
00213 for( i = 0; i < p_sys->i_titles; i++ )
00214 {
00215 vlc_input_title_Delete( p_sys->title[i] );
00216 }
00217
00218 ioctl_Close( p_this, p_sys->vcddev );
00219 free( p_sys );
00220 }
00221
00222
00223
00224
00225 static block_t *Block( access_t *p_access )
00226 {
00227 access_sys_t *p_sys = p_access->p_sys;
00228 int i_blocks = CDDA_BLOCKS_ONCE;
00229 block_t *p_block;
00230
00231
00232 if( p_access->info.b_eof ) return NULL;
00233
00234 if( !p_sys->b_header )
00235 {
00236
00237 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
00238 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
00239 p_sys->b_header = VLC_TRUE;
00240 return p_block;
00241 }
00242
00243
00244 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
00245 {
00246 if( p_access->info.i_title + 1 >= p_sys->i_titles )
00247 {
00248 p_access->info.b_eof = VLC_TRUE;
00249 return NULL;
00250 }
00251
00252 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
00253 INPUT_UPDATE_META;
00254 p_access->info.i_title++;
00255 p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
00256 p_access->info.i_pos = 0;
00257 }
00258
00259
00260 if( p_sys->i_sector + i_blocks >=
00261 p_sys->p_sectors[p_access->info.i_title + 1] )
00262 {
00263 i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] -
00264 p_sys->i_sector;
00265 }
00266
00267
00268 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
00269 {
00270 msg_Err( p_access, "cannot get a new block of size: %i",
00271 i_blocks * CDDA_DATA_SIZE );
00272 return NULL;
00273 }
00274
00275 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
00276 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
00277 {
00278 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
00279 block_Release( p_block );
00280
00281
00282 p_sys->i_sector++;
00283 p_access->info.i_pos += CDDA_DATA_SIZE;
00284 return NULL;
00285 }
00286
00287
00288 p_sys->i_sector += i_blocks;
00289 p_access->info.i_pos += p_block->i_buffer;
00290
00291 return p_block;
00292 }
00293
00294
00295
00296
00297 static int Seek( access_t *p_access, int64_t i_pos )
00298 {
00299 access_sys_t *p_sys = p_access->p_sys;
00300
00301
00302 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title] +
00303 i_pos / CDDA_DATA_SIZE;
00304 p_access->info.i_pos = i_pos;
00305
00306 return VLC_SUCCESS;
00307 }
00308
00309
00310
00311
00312 static int Control( access_t *p_access, int i_query, va_list args )
00313 {
00314 access_sys_t *p_sys = p_access->p_sys;
00315 vlc_bool_t *pb_bool;
00316 int *pi_int;
00317 int64_t *pi_64;
00318 input_title_t ***ppp_title;
00319 int i;
00320 char *psz_title;
00321 vlc_meta_t **pp_meta;
00322
00323 switch( i_query )
00324 {
00325
00326 case ACCESS_CAN_SEEK:
00327 case ACCESS_CAN_FASTSEEK:
00328 case ACCESS_CAN_PAUSE:
00329 case ACCESS_CAN_CONTROL_PACE:
00330 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
00331 *pb_bool = VLC_TRUE;
00332 break;
00333
00334
00335 case ACCESS_GET_MTU:
00336 pi_int = (int*)va_arg( args, int * );
00337 *pi_int = CDDA_DATA_ONCE;
00338 break;
00339
00340 case ACCESS_GET_PTS_DELAY:
00341 pi_64 = (int64_t*)va_arg( args, int64_t * );
00342 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
00343 break;
00344
00345
00346 case ACCESS_SET_PAUSE_STATE:
00347 break;
00348
00349 case ACCESS_GET_TITLE_INFO:
00350 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
00351 pi_int = (int*)va_arg( args, int* );
00352 *((int*)va_arg( args, int* )) = 1;
00353
00354
00355 *pi_int = p_sys->i_titles;
00356 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
00357 for( i = 0; i < p_sys->i_titles; i++ )
00358 {
00359 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
00360 }
00361 break;
00362
00363 case ACCESS_SET_TITLE:
00364 i = (int)va_arg( args, int );
00365 if( i != p_access->info.i_title )
00366 {
00367
00368 p_access->info.i_update |=
00369 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
00370 p_access->info.i_title = i;
00371 p_access->info.i_size = p_sys->title[i]->i_size;
00372 p_access->info.i_pos = 0;
00373
00374
00375 p_sys->i_sector = p_sys->p_sectors[i];
00376 }
00377 break;
00378
00379 case ACCESS_GET_META:
00380 psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
00381 snprintf( psz_title, 100, _("Audio CD - Track %i" ),
00382 p_access->info.i_title+1 );
00383 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
00384 *pp_meta = vlc_meta_New();
00385 vlc_meta_Add( *pp_meta, VLC_META_TITLE, psz_title );
00386 free( psz_title );
00387 break;
00388
00389 case ACCESS_SET_SEEKPOINT:
00390 case ACCESS_SET_PRIVATE_ID_STATE:
00391 return VLC_EGENERIC;
00392
00393 default:
00394 msg_Warn( p_access, "unimplemented query in control" );
00395 return VLC_EGENERIC;
00396
00397 }
00398 return VLC_SUCCESS;
00399 }