]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
Release playlist
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "codecs.h"
34 #include "vcd/cdrom.h"
35
36 #include <vlc_playlist.h>
37
38 /*****************************************************************************
39  * Module descriptior
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 #define CACHING_TEXT N_("Caching value in ms")
45 #define CACHING_LONGTEXT N_( \
46     "Allows you to modify the default caching value for cdda streams. This " \
47     "value should be set in milliseconds units." )
48
49 vlc_module_begin();
50     set_shortname( _("Audio CD"));
51     set_description( _("Audio CD input") );
52     set_capability( "access2", 10 );
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_ACCESS );
55     set_callbacks( Open, Close );
56
57     add_usage_hint( N_("[cdda:][device][@[track]]") );
58     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
59                  CACHING_LONGTEXT, VLC_TRUE );
60     add_bool( "cdda-separate-tracks", VLC_TRUE, NULL, NULL, NULL, VLC_TRUE );
61     add_integer( "cdda-track", -1 , NULL, NULL, NULL, VLC_TRUE );
62     add_shortcut( "cdda" );
63     add_shortcut( "cddasimple" );
64 vlc_module_end();
65
66
67 /* how many blocks VCDRead will read in each loop */
68 #define CDDA_BLOCKS_ONCE 20
69 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
70
71 /*****************************************************************************
72  * Access: local prototypes
73  *****************************************************************************/
74 struct access_sys_t
75 {
76     vcddev_t    *vcddev;                            /* vcd device descriptor */
77
78     /* Title infos */
79     int           i_titles;
80     input_title_t *title[99];         /* No more that 99 track in a cd-audio */
81
82     /* Current position */
83     int         i_sector;                                  /* Current Sector */
84     int *       p_sectors;                                  /* Track sectors */
85
86     /* Wave header for the output data */
87     WAVEHEADER  waveheader;
88     vlc_bool_t  b_header;
89
90     vlc_bool_t  b_separate_items;
91     vlc_bool_t  b_single_track;
92     int         i_track;
93 };
94
95 static block_t *Block( access_t * );
96 static int      Seek( access_t *, int64_t );
97 static int      Control( access_t *, int, va_list );
98
99 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
100                       playlist_t *p_playlist, playlist_item_t *p_parent );
101
102 /*****************************************************************************
103  * Open: open cdda
104  *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
106 {
107     access_t     *p_access = (access_t*)p_this;
108     access_sys_t *p_sys;
109
110     vcddev_t *vcddev;
111     char *psz_name;
112
113     vlc_bool_t b_separate_requested;
114     vlc_bool_t b_play = VLC_FALSE;
115     input_thread_t *p_input;
116     playlist_item_t *p_item = NULL;
117     playlist_t *p_playlist  = NULL;
118     int i_ret;
119     int i_track;
120
121     if( !p_access->psz_path || !*p_access->psz_path )
122     {
123         /* Only when selected */
124         if( !p_this->b_force ) return VLC_EGENERIC;
125
126         psz_name = var_CreateGetString( p_this, "cd-audio" );
127         if( !psz_name || !*psz_name )
128         {
129             if( psz_name ) free( psz_name );
130             return VLC_EGENERIC;
131         }
132     }
133     else psz_name = strdup( p_access->psz_path );
134
135     /* Open CDDA */
136     if( (vcddev = ioctl_Open( VLC_OBJECT(p_access), psz_name )) == NULL )
137     {
138         msg_Warn( p_access, "could not open %s", psz_name );
139         free( psz_name );
140         return VLC_EGENERIC;
141     }
142     free( psz_name );
143
144     /* Set up p_access */
145     p_access->pf_read = NULL;
146     p_access->pf_block = Block;
147     p_access->pf_control = Control;
148     p_access->pf_seek = Seek;
149     p_access->info.i_update = 0;
150     p_access->info.i_size = 0;
151     p_access->info.i_pos = 0;
152     p_access->info.b_eof = VLC_FALSE;
153     p_access->info.i_title = 0;
154     p_access->info.i_seekpoint = 0;
155     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
156     memset( p_sys, 0, sizeof( access_sys_t ) );
157     p_sys->vcddev = vcddev;
158     p_sys->b_separate_items = VLC_FALSE;
159     p_sys->b_single_track = VLC_FALSE;
160     p_sys->b_header = VLC_FALSE;
161
162     b_separate_requested = var_CreateGetBool( p_access,
163                                               "cdda-separate-tracks" );
164
165     /* We only do separate items if the whole disc is requested -
166      *  Dirty hack we access some private data ! */
167     p_input = (input_thread_t *)( p_access->p_parent );
168     /* Do we play a single track ? */
169     i_track = var_CreateGetInteger( p_access, "cdda-track" );
170     if( b_separate_requested && p_input->input.i_title_start == -1 &&
171         i_track <= 0 )
172     {
173         p_sys->b_separate_items = VLC_TRUE;
174     }
175     if( i_track > 0 )
176     {
177         p_sys->b_single_track = VLC_TRUE;
178         p_sys->i_track = i_track - 1;
179     }
180
181     msg_Dbg( p_access, "Separate items : %i - Single track : %i",
182                         p_sys->b_separate_items, p_sys->b_single_track );
183
184     if( p_sys->b_separate_items )
185     {
186         p_playlist = (playlist_t *) vlc_object_find( p_access,
187                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
188
189         if( !p_playlist ) return VLC_EGENERIC;
190
191         /* Let's check if we need to play */
192         if( &p_playlist->status.p_item->input ==
193              ((input_thread_t *)p_access->p_parent)->input.p_item )
194         {
195             p_item = p_playlist->status.p_item;
196             b_play = VLC_TRUE;
197             msg_Dbg( p_access, "starting Audio CD playback");
198         }
199         else
200         {
201             input_item_t *p_current = ( (input_thread_t*)p_access->p_parent)->
202                                          input.p_item;
203             p_item = playlist_LockItemGetByInput( p_playlist, p_current );
204             msg_Dbg( p_access, "not starting Audio CD playback");
205
206             if( !p_item )
207             {
208                 msg_Dbg( p_playlist, "unable to find item in playlist");
209                return -1;
210             }
211             b_play = VLC_FALSE;
212         }
213     }
214
215     /* We read the Table Of Content information */
216     if( 1 )
217     {
218         i_ret = GetTracks( p_access, p_sys->b_separate_items,
219                            p_playlist, p_item );
220         if( i_ret < 0 )
221         {
222             goto error;
223         }
224     }
225
226     /* Build a WAV header for the output data */
227     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
228     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
229     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
230     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
231     p_sys->waveheader.Length = 0;                     /* we just don't know */
232     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
233     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
234     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
235     SetWLE( &p_sys->waveheader.Modus, 2);
236     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
237     SetWLE( &p_sys->waveheader.BytesPerSample,
238             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
239     SetDWLE( &p_sys->waveheader.BytesPerSec,
240              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
241     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
242     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
243
244     /* Only update META if we are in old mode */
245     if( !p_sys->b_separate_items && !p_sys->b_single_track )
246     {
247         p_access->info.i_update |= INPUT_UPDATE_META;
248     }
249
250     /* Position on the right sector and update size */
251     if( p_sys->b_single_track )
252     {
253         p_sys->i_sector = p_sys->p_sectors[ p_sys->i_track ];
254         p_access->info.i_size =
255             ( p_sys->p_sectors[p_sys->i_track+1] -
256               p_sys->p_sectors[p_sys->i_track] ) *
257                         (int64_t)CDDA_DATA_SIZE;
258     }
259
260     /* PTS delay */
261     var_Create( p_access, "cdda-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
262
263     if( b_play )
264     {
265         playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
266                           p_playlist->status.i_view, p_playlist->status.p_item,
267                           NULL );
268     }
269
270     if( p_playlist ) vlc_object_release( p_playlist );
271
272     return VLC_SUCCESS;
273
274 error:
275     ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
276     free( p_sys );
277     if( p_playlist ) vlc_object_release( p_playlist );
278     return VLC_EGENERIC;
279 }
280
281 /*****************************************************************************
282  * Close: closes cdda
283  *****************************************************************************/
284 static void Close( vlc_object_t *p_this )
285 {
286     access_t     *p_access = (access_t *)p_this;
287     access_sys_t *p_sys = p_access->p_sys;
288     int          i;
289
290     for( i = 0; i < p_sys->i_titles; i++ )
291     {
292         vlc_input_title_Delete( p_sys->title[i] );
293     }
294
295     ioctl_Close( p_this, p_sys->vcddev );
296     free( p_sys );
297 }
298
299 /*****************************************************************************
300  * Block: read data (CDDA_DATA_ONCE)
301  *****************************************************************************/
302 static block_t *Block( access_t *p_access )
303 {
304     access_sys_t *p_sys = p_access->p_sys;
305     int i_blocks = CDDA_BLOCKS_ONCE;
306     block_t *p_block;
307
308     if( p_sys->b_separate_items ) p_access->info.b_eof = VLC_TRUE;
309
310     /* Check end of file */
311     if( p_access->info.b_eof ) return NULL;
312
313     if( !p_sys->b_header )
314     {
315         /* Return only the header */
316         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
317         memcpy( p_block->p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
318         p_sys->b_header = VLC_TRUE;
319         return p_block;
320     }
321
322     /* Check end of title - Single track */
323     if( p_sys->b_single_track )
324     {
325         if( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_track + 1] )
326         {
327             p_access->info.b_eof = VLC_TRUE;
328             return NULL;
329         }
330         /* Don't read too far */
331         if( p_sys->i_sector + i_blocks >=
332             p_sys->p_sectors[p_sys->i_track +1] )
333         {
334             i_blocks = p_sys->p_sectors[p_sys->i_track+1] - p_sys->i_sector;
335         }
336     }
337     else
338     {
339         /* Check end of title - Normal */
340         while( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 1] )
341         {
342             if( p_access->info.i_title + 1 >= p_sys->i_titles )
343             {
344                 p_access->info.b_eof = VLC_TRUE;
345                 return NULL;
346             }
347
348             p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE |
349                                        INPUT_UPDATE_META;
350             p_access->info.i_title++;
351             p_access->info.i_size =
352                         p_sys->title[p_access->info.i_title]->i_size;
353             p_access->info.i_pos = 0;
354         }
355         /* Don't read too far */
356         if( p_sys->i_sector + i_blocks >=                                                       p_sys->p_sectors[p_access->info.i_title + 1] )
357         {
358             i_blocks = p_sys->p_sectors[ p_access->info.i_title + 1] -
359                          p_sys->i_sector;
360         }
361     }
362
363     /* Do the actual reading */
364     if( !( p_block = block_New( p_access, i_blocks * CDDA_DATA_SIZE ) ) )
365     {
366         msg_Err( p_access, "cannot get a new block of size: %i",
367                  i_blocks * CDDA_DATA_SIZE );
368         return NULL;
369     }
370
371     if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
372             p_sys->i_sector, p_block->p_buffer, i_blocks, CDDA_TYPE ) < 0 )
373     {
374         msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
375         block_Release( p_block );
376
377         /* Try to skip one sector (in case of bad sectors) */
378         p_sys->i_sector++;
379         p_access->info.i_pos += CDDA_DATA_SIZE;
380         return NULL;
381     }
382
383     /* Update a few values */
384     p_sys->i_sector += i_blocks;
385     p_access->info.i_pos += p_block->i_buffer;
386
387     return p_block;
388 }
389
390 /****************************************************************************
391  * Seek
392  ****************************************************************************/
393 static int Seek( access_t *p_access, int64_t i_pos )
394 {
395     access_sys_t *p_sys = p_access->p_sys;
396
397     /* Next sector to read */
398     p_sys->i_sector = p_sys->p_sectors[
399                                         (p_sys->b_single_track ?
400                                          p_sys->i_track :
401                                          p_access->info.i_title + 1)
402                                       ] +
403                                 i_pos / CDDA_DATA_SIZE;
404     p_access->info.i_pos = i_pos;
405
406     return VLC_SUCCESS;
407 }
408
409 /*****************************************************************************
410  * Control:
411  *****************************************************************************/
412 static int Control( access_t *p_access, int i_query, va_list args )
413 {
414     access_sys_t *p_sys = p_access->p_sys;
415     vlc_bool_t   *pb_bool;
416     int          *pi_int;
417     int64_t      *pi_64;
418     input_title_t ***ppp_title;
419     int i;
420     char         *psz_title;
421     vlc_meta_t  **pp_meta;
422
423     switch( i_query )
424     {
425         /* */
426         case ACCESS_CAN_SEEK:
427         case ACCESS_CAN_FASTSEEK:
428         case ACCESS_CAN_PAUSE:
429         case ACCESS_CAN_CONTROL_PACE:
430             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
431             *pb_bool = VLC_TRUE;
432             break;
433
434         /* */
435         case ACCESS_GET_MTU:
436             pi_int = (int*)va_arg( args, int * );
437             *pi_int = CDDA_DATA_ONCE;
438             break;
439
440         case ACCESS_GET_PTS_DELAY:
441             pi_64 = (int64_t*)va_arg( args, int64_t * );
442             *pi_64 = var_GetInteger( p_access, "cdda-caching" ) * 1000;
443             break;
444
445         /* */
446         case ACCESS_SET_PAUSE_STATE:
447             break;
448
449         case ACCESS_GET_TITLE_INFO:
450             if( p_sys->b_single_track )
451                 return VLC_EGENERIC;
452             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
453             pi_int    = (int*)va_arg( args, int* );
454             *((int*)va_arg( args, int* )) = 1; /* Title offset */
455
456             /* Duplicate title infos */
457             *pi_int = p_sys->i_titles;
458             *ppp_title = malloc(sizeof( input_title_t **) * p_sys->i_titles );
459             for( i = 0; i < p_sys->i_titles; i++ )
460             {
461                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
462             }
463             break;
464
465         case ACCESS_SET_TITLE:
466             if( p_sys->b_single_track ) return VLC_EGENERIC;
467             i = (int)va_arg( args, int );
468             if( i != p_access->info.i_title )
469             {
470                 /* Update info */
471                 p_access->info.i_update |=
472                     INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE|INPUT_UPDATE_META;
473                 p_access->info.i_title = i;
474                 p_access->info.i_size = p_sys->title[i]->i_size;
475                 p_access->info.i_pos = 0;
476
477                 /* Next sector to read */
478                 p_sys->i_sector = p_sys->p_sectors[i];
479             }
480             break;
481
482         case ACCESS_GET_META:
483              if( p_sys->b_single_track ) return VLC_EGENERIC;
484              psz_title = malloc( strlen( _("Audio CD - Track ") ) + 5 );
485              snprintf( psz_title, 100, _("Audio CD - Track %i" ),
486                                         p_access->info.i_title+1 );
487              pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
488              *pp_meta = vlc_meta_New();
489              vlc_meta_Add( *pp_meta, VLC_META_TITLE, psz_title );
490              free( psz_title );
491              break;
492
493         case ACCESS_SET_SEEKPOINT:
494         case ACCESS_SET_PRIVATE_ID_STATE:
495             return VLC_EGENERIC;
496
497         default:
498             msg_Warn( p_access, "unimplemented query in control" );
499             return VLC_EGENERIC;
500
501     }
502     return VLC_SUCCESS;
503 }
504
505 static int GetTracks( access_t *p_access, vlc_bool_t b_separate,
506                       playlist_t *p_playlist, playlist_item_t *p_parent )
507 {
508     access_sys_t *p_sys = p_access->p_sys;
509     int i;
510     playlist_item_t *p_item;
511     p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
512                                           p_sys->vcddev, &p_sys->p_sectors );
513     if( p_sys->i_titles < 0 )
514     {
515         msg_Err( p_access, "unable to count tracks" );
516         return VLC_EGENERIC;;
517     }
518     else if( p_sys->i_titles <= 0 )
519     {
520         msg_Err( p_access, "no audio tracks found" );
521         return VLC_EGENERIC;
522     }
523
524     if( b_separate )
525     {
526         if( p_parent->i_children == -1 )
527         {
528             playlist_LockItemToNode( p_playlist, p_parent );
529         }
530     }
531
532     /* Build title table */
533     for( i = 0; i < p_sys->i_titles; i++ )
534     {
535         msg_Dbg( p_access, "track[%d] start=%d", i, p_sys->p_sectors[i] );
536         if( !b_separate )
537         {
538             input_title_t *t = p_sys->title[i] = vlc_input_title_New();
539
540             asprintf( &t->psz_name, _("Track %i"), i + 1 );
541             t->i_size = ( p_sys->p_sectors[i+1] - p_sys->p_sectors[i] ) *
542                         (int64_t)CDDA_DATA_SIZE;
543
544             t->i_length = I64C(1000000) * t->i_size / 44100 / 4;
545         }
546         else
547         {
548             char *psz_uri;
549             int i_path_len = p_access->psz_path ? strlen( p_access->psz_path )
550                                                 : 0;
551             char *psz_name = malloc( strlen( _("Audio CD - Track ") ) + 5 );
552             char *psz_opt = (char*)malloc( 14 );
553
554             psz_uri = (char*)malloc( i_path_len + 13 );
555             snprintf( psz_uri, i_path_len + 13, "cdda://%s",
556                                 p_access->psz_path ? p_access->psz_path : "" );
557             snprintf( psz_name, 100, _("Audio CD - Track %i" ),
558                                      (i+1) );
559             snprintf( psz_opt, 14, "cdda-track=%i", i+1 );
560             /* Create playlist items */
561             p_item = playlist_ItemNewWithType( VLC_OBJECT( p_playlist ),
562                                  psz_uri, psz_name, ITEM_TYPE_DISC );
563             playlist_ItemAddOption( p_item, psz_opt );
564             playlist_NodeAddItem( p_playlist, p_item,
565                                   p_parent->pp_parents[0]->i_view,
566                                   p_parent, PLAYLIST_APPEND, PLAYLIST_END );
567             free( psz_uri ); free( psz_opt );
568         }
569     }
570
571     p_sys->i_sector = p_sys->p_sectors[0];
572     if( p_sys->b_separate_items )
573     {
574         p_sys->i_titles = 0;
575     }
576
577    return VLC_SUCCESS;
578 }