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 */
78 /* Current position */
79 int i_sector; /* Current Sector */
80 int * p_sectors; /* Track sectors */
82 /* Wave header for the output data */
83 WAVEHEADER waveheader;
87 static block_t *Block( access_t * );
88 static int Seek( access_t *, int64_t );
89 static int Control( access_t *, int, va_list );
91 /*****************************************************************************
93 * MRL syntax: [dev_path][@[title-start][-[title-end]]]
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 access_t *p_access = (access_t*)p_this;
101 int i, i_title_start = -1, i_title_end = -1;
104 /* Command line: [dev_path][@[title-start][-[title-end]]] */
105 psz_dup = p_access->psz_path? strdup( p_access->psz_path ) : 0;
106 if( psz_dup && ( psz = strchr( psz_dup, '@' ) ) )
109 i_title_start = i_title_end = strtol( psz, NULL, 0 );
111 if( ( psz = strchr( psz, '-' ) ) )
114 i_title_end = strtol( psz, NULL, 0 );
118 if( !psz_dup || !*psz_dup )
120 if( psz_dup ) free( psz_dup );
122 /* Only when selected */
123 if( !p_access->b_force ) return VLC_EGENERIC;
125 psz_dup = var_CreateGetString( p_access, "cd-audio" );
126 if( !psz_dup || !*psz_dup )
128 if( psz_dup ) free( psz_dup );
134 if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_dup )) == NULL )
136 msg_Warn( p_access, "could not open %s", psz_dup );
142 /* Set up p_access */
143 p_access->pf_read = NULL;
144 p_access->pf_block = Block;
145 p_access->pf_control = Control;
146 p_access->pf_seek = Seek;
147 p_access->info.i_update = 0;
148 p_access->info.i_size = 0;
149 p_access->info.i_pos = 0;
150 p_access->info.b_eof = VLC_FALSE;
151 p_access->info.i_title = 0;
152 p_access->info.i_seekpoint = 0;
153 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
154 memset( p_sys, 0, sizeof( access_sys_t ) );
155 p_sys->vcddev = vcddev;
156 p_sys->b_header = VLC_FALSE;
158 /* We read the Table Of Content information */
159 p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
160 p_sys->vcddev, &p_sys->p_sectors );
161 if( p_sys->i_titles < 0 )
163 msg_Err( p_access, "unable to count tracks" );
166 else if( p_sys->i_titles <= 0 )
168 msg_Err( p_access, "no audio tracks found" );
172 /* Build title table */
173 for( i = 0; i < p_sys->i_titles; i++ )
175 input_title_t *t = p_sys->title[i] = vlc_input_title_New();
177 msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[i] );
178 msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+1] );
180 asprintf( &t->psz_name, _("Track %i"), i + 1 );
181 t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
182 (int64_t)CDDA_DATA_SIZE;
184 t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
187 /* Starting title and sector */
188 if( i_title_start < 1 || i_title_start > p_sys->i_titles )
189 p_sys->i_title_start = 1;
190 else p_sys->i_title_start = i_title_start;
191 if( i_title_end < 1 || i_title_end > p_sys->i_titles )
192 p_sys->i_title_end = -1;
193 else p_sys->i_title_end = i_title_end;
195 p_sys->i_sector = p_sys->p_sectors[p_sys->i_title_start-1];
196 p_access->info.i_title = p_sys->i_title_start-1;
197 p_access->info.i_size = p_sys->title[p_sys->i_title_start-1]->i_size;
199 /* Build a WAV header for the output data */
200 memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
201 SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
202 SetWLE( &p_sys->waveheader.BitsPerSample, 16);
203 p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
204 p_sys->waveheader.Length = 0; /* we just don't know */
205 p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
206 p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
207 SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
208 SetWLE( &p_sys->waveheader.Modus, 2);
209 SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
210 SetWLE( &p_sys->waveheader.BytesPerSample,
211 2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
212 SetDWLE( &p_sys->waveheader.BytesPerSec,
213 2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
214 p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
215 p_sys->waveheader.DataLength = 0; /* we just don't know */
218 var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
222 ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
227 /*****************************************************************************
229 *****************************************************************************/
230 static void Close( vlc_object_t *p_this )
232 access_t *p_access = (access_t *)p_this;
233 access_sys_t *p_sys = p_access->p_sys;
236 for( i = 0; i < p_sys->i_titles; i++ )
238 vlc_input_title_Delete( p_sys->title[i] );
241 ioctl_Close( p_this, p_sys->vcddev );
245 /*****************************************************************************
246 * Block: read data (CDDA_DATA_ONCE)
247 *****************************************************************************/
248 static block_t *Block( access_t *p_access )
250 access_sys_t *p_sys = p_access->p_sys;
251 int i_blocks = CDDA_BLOCKS_ONCE;
254 /* Check end of file */
255 if( p_access->info.b_eof ) return NULL;
257 if( !p_sys->b_header )
259 /* Return only the header */
260 p_block = block_New( p_access, sizeof( WAVEHEADER ) );
261 memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
262 p_sys->b_header = VLC_TRUE;
266 /* Check end of title */
267 while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
269 if( p_access->info.i_title + 1 >= p_sys->i_titles ||
270 ( p_sys->i_title_end > 0 &&
271 p_access->info.i_title + 1 >= p_sys->i_title_end ) )
273 p_access->info.b_eof = VLC_TRUE;
277 p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
278 p_access->info.i_title++;
279 p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
280 p_access->info.i_pos = 0;
283 /* Don't read after the end of a title */
284 if( p_sys->i_sector + i_blocks >=
285 p_sys->p_sectors[p_access->info.i_title + 1] )
287 i_blocks = p_sys->p_sectors[p_access->info.i_title + 1 ] -
291 /* Do the actual reading */
292 if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
294 msg_Err( p_access, "cannot get a new block of size: %i",
295 i_blocks * CDDA_DATA_SIZE );
299 if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
300 p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
302 msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
303 block_Release( p_block );
305 /* Try to skip one sector (in case of bad sectors) */
307 p_access->info.i_pos += CDDA_DATA_SIZE;
311 /* Update a few values */
312 p_sys->i_sector += i_blocks;
313 p_access->info.i_pos += p_block->i_buffer;
318 /****************************************************************************
320 ****************************************************************************/
321 static int Seek( access_t *p_access, int64_t i_pos )
323 access_sys_t *p_sys = p_access->p_sys;
325 /* Next sector to read */
326 p_sys->i_sector = p_sys->p_sectors[p_access->info.i_title] +
327 i_pos / CDDA_DATA_SIZE;
328 p_access->info.i_pos = i_pos;
333 /*****************************************************************************
335 *****************************************************************************/
336 static int Control( access_t *p_access, int i_query, va_list args )
338 access_sys_t *p_sys = p_access->p_sys;
342 input_title_t ***ppp_title;
348 case ACCESS_CAN_SEEK:
349 case ACCESS_CAN_FASTSEEK:
350 case ACCESS_CAN_PAUSE:
351 case ACCESS_CAN_CONTROL_PACE:
352 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
358 pi_int = (int*)va_arg( args, int * );
359 *pi_int = CDDA_DATA_ONCE;
362 case ACCESS_GET_PTS_DELAY:
363 pi_64 = (int64_t*)va_arg( args, int64_t * );
364 *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
368 case ACCESS_SET_PAUSE_STATE:
371 case ACCESS_GET_TITLE_INFO:
372 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
373 pi_int = (int*)va_arg( args, int* );
375 /* Duplicate title infos */
376 *pi_int = p_sys->i_titles;
377 *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
378 for( i = 0; i < p_sys->i_titles; i++ )
380 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
384 case ACCESS_SET_TITLE:
385 i = (int)va_arg( args, int );
386 if( i != p_access->info.i_title )
389 p_access->info.i_update |=
390 INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
391 p_access->info.i_title = i;
392 p_access->info.i_size = p_sys->title[i]->i_size;
393 p_access->info.i_pos = 0;
395 /* Next sector to read */
396 p_sys->i_sector = p_sys->p_sectors[i];
398 /* User tries to access another title so better reset
400 p_sys->i_title_end = -1;
404 case ACCESS_SET_SEEKPOINT:
408 msg_Err( p_access, "unimplemented query in control" );