1 /*****************************************************************************
2 * cdda.c : CD digital audio input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2003-2006, 2008-2009 VLC authors and 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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 * - Improve CDDB support (non-blocking, ...)
28 * - Fix tracknumber in MRL
31 /*****************************************************************************
33 *****************************************************************************/
44 #include <vlc_common.h>
45 #include <vlc_demux.h>
46 #include <vlc_plugin.h>
47 #include <vlc_input.h>
48 #include <vlc_access.h>
50 #include <vlc_charset.h> /* ToLocaleDup */
53 #include "vcd/cdrom.h" /* For CDDA_DATA_SIZE */
56 #include <cddb/cddb.h>
60 static vcddev_t
*DiscOpen(vlc_object_t
*obj
, const char *location
,
61 const char *path
, unsigned *restrict trackp
)
65 *trackp
= var_InheritInteger(obj
, "cdda-track");
68 devpath
= ToLocaleDup(path
);
69 else if (location
[0] != '\0')
71 #if (DIR_SEP_CHAR == '/')
72 char *dec
= vlc_uri_decode_duplicate(location
);
76 /* GNOME CDDA syntax */
77 const char *sl
= strrchr(dec
, '/');
80 if (sscanf(sl
, "/Track %2u", trackp
) == 1)
86 if (unlikely(asprintf(&devpath
, "/dev/%s", dec
) == -1))
95 devpath
= var_InheritString(obj
, "cd-audio");
100 #if defined (_WIN32) || defined (__OS2__)
101 /* Trim backslash after drive letter */
102 if (devpath
[0] != '\0' && !strcmp(&devpath
[1], ":" DIR_SEP
))
107 vcddev_t
*dev
= ioctl_Open(obj
, devpath
);
109 msg_Warn(obj
, "cannot open disc %s", devpath
);
115 /* how many blocks Demux() will read in each iteration */
116 #define CDDA_BLOCKS_ONCE 20
120 vcddev_t
*vcddev
; /* vcd device descriptor */
124 unsigned start
; /**< Track first sector */
125 unsigned length
; /**< Track total sectors */
126 unsigned position
; /**< Current offset within track sectors */
129 static int Demux(demux_t
*demux
)
131 demux_sys_t
*sys
= demux
->p_sys
;
132 unsigned count
= CDDA_BLOCKS_ONCE
;
134 if (sys
->position
>= sys
->length
)
135 return VLC_DEMUXER_EOF
;
137 if (sys
->position
+ count
>= sys
->length
)
138 count
= sys
->length
- sys
->position
;
140 block_t
*block
= block_Alloc(count
* CDDA_DATA_SIZE
);
141 if (unlikely(block
== NULL
))
142 return VLC_DEMUXER_EOF
;
144 if (ioctl_ReadSectors(VLC_OBJECT(demux
), sys
->vcddev
,
145 sys
->start
+ sys
->position
,
146 block
->p_buffer
, count
, CDDA_TYPE
) < 0)
148 msg_Err(demux
, "cannot read sector %u", sys
->position
);
149 block_Release(block
);
151 /* Skip potentially bad sector */
153 return VLC_DEMUXER_SUCCESS
;
156 sys
->position
+= count
;
158 block
->i_nb_samples
= block
->i_buffer
/ 4;
159 block
->i_dts
= block
->i_pts
= date_Get(&sys
->pts
);
160 date_Increment(&sys
->pts
, block
->i_nb_samples
);
162 es_out_Send(demux
->out
, sys
->es
, block
);
163 es_out_SetPCR(demux
->out
, date_Get(&sys
->pts
));
164 return VLC_DEMUXER_SUCCESS
;
167 static int DemuxControl(demux_t
*demux
, int query
, va_list args
)
169 demux_sys_t
*sys
= demux
->p_sys
;
171 /* One sector is 40000/3 µs */
172 static_assert (CDDA_DATA_SIZE
* CLOCK_FREQ
* 3 ==
173 4 * 44100 * INT64_C(40000), "Wrong time/sector ratio");
178 case DEMUX_CAN_PAUSE
:
179 case DEMUX_CAN_CONTROL_PACE
:
180 *va_arg(args
, bool*) = true;
182 case DEMUX_GET_PTS_DELAY
:
183 *va_arg(args
, int64_t *) =
184 INT64_C(1000) * var_InheritInteger(demux
, "disc-caching");
187 case DEMUX_SET_PAUSE_STATE
:
190 case DEMUX_GET_POSITION
:
191 *va_arg(args
, double *) = (double)(sys
->position
)
192 / (double)(sys
->length
);
195 case DEMUX_SET_POSITION
:
196 sys
->position
= lround(va_arg(args
, double) * sys
->length
);
199 case DEMUX_GET_LENGTH
:
200 *va_arg(args
, mtime_t
*) = (INT64_C(40000) * sys
->length
) / 3;
203 *va_arg(args
, mtime_t
*) = (INT64_C(40000) * sys
->position
) / 3;
206 sys
->position
= (va_arg(args
, mtime_t
) * 3) / INT64_C(40000);
215 static int DemuxOpen(vlc_object_t
*obj
, vcddev_t
*dev
, unsigned track
)
217 demux_t
*demux
= (demux_t
*)obj
;
219 if (demux
->out
== NULL
)
222 demux_sys_t
*sys
= vlc_obj_malloc(obj
, sizeof (*sys
));
223 if (unlikely(sys
== NULL
))
228 sys
->start
= var_InheritInteger(obj
, "cdda-first-sector");
229 sys
->length
= var_InheritInteger(obj
, "cdda-last-sector") - sys
->start
;
231 /* Track number in input item */
232 if (sys
->start
== (unsigned)-1 || sys
->length
== (unsigned)-1)
234 int *sectors
= NULL
; /* Track sectors */
235 unsigned titles
= ioctl_GetTracksMap(obj
, dev
, §ors
);
239 msg_Err(obj
, "invalid track number: %u/%u", track
, titles
);
244 sys
->start
= sectors
[track
- 1];
245 sys
->length
= sectors
[track
] - sys
->start
;
251 es_format_Init(&fmt
, AUDIO_ES
, VLC_CODEC_S16L
);
252 fmt
.audio
.i_rate
= 44100;
253 fmt
.audio
.i_channels
= 2;
254 sys
->es
= es_out_Add(demux
->out
, &fmt
);
256 date_Init(&sys
->pts
, 44100, 1);
257 date_Set(&sys
->pts
, VLC_TS_0
);
260 demux
->pf_demux
= Demux
;
261 demux
->pf_control
= DemuxControl
;
265 ioctl_Close(obj
, dev
);
269 /*****************************************************************************
270 * Access: local prototypes
271 *****************************************************************************/
274 vcddev_t
*vcddev
; /* vcd device descriptor */
275 int *p_sectors
; /* Track sectors */
278 vlc_meta_t
**cdtextv
;
285 static cddb_disc_t
*GetCDDBInfo( vlc_object_t
*obj
, int i_titles
, int *p_sectors
)
287 if( !var_InheritBool( obj
, "metadata-network-access" ) )
289 msg_Dbg( obj
, "album art policy set to manual: not fetching" );
294 cddb_conn_t
*p_cddb
= cddb_new();
297 msg_Warn( obj
, "unable to use CDDB" );
303 cddb_http_enable( p_cddb
);
305 char *psz_tmp
= var_InheritString( obj
, "cddb-server" );
308 cddb_set_server_name( p_cddb
, psz_tmp
);
312 cddb_set_server_port( p_cddb
, var_InheritInteger( obj
, "cddb-port" ) );
314 cddb_set_email_address( p_cddb
, "vlc@videolan.org" );
316 cddb_set_http_path_query( p_cddb
, "/~cddb/cddb.cgi" );
317 cddb_set_http_path_submit( p_cddb
, "/~cddb/submit.cgi" );
321 char *psz_temp
= config_GetUserDir( VLC_CACHE_DIR
);
323 if( asprintf( &psz_cachedir
, "%s" DIR_SEP
"cddb", psz_temp
) > 0 ) {
324 cddb_cache_enable( p_cddb
);
325 cddb_cache_set_dir( p_cddb
, psz_cachedir
);
326 free( psz_cachedir
);
330 cddb_set_timeout( p_cddb
, 10 );
333 cddb_disc_t
*p_disc
= cddb_disc_new();
336 msg_Err( obj
, "unable to create CDDB disc structure." );
340 int64_t i_length
= 2000000; /* PreGap */
341 for( int i
= 0; i
< i_titles
; i
++ )
343 cddb_track_t
*t
= cddb_track_new();
344 cddb_track_set_frame_offset( t
, p_sectors
[i
] + 150 ); /* Pregap offset */
346 cddb_disc_add_track( p_disc
, t
);
347 const int64_t i_size
= ( p_sectors
[i
+1] - p_sectors
[i
] ) *
348 (int64_t)CDDA_DATA_SIZE
;
349 i_length
+= INT64_C(1000000) * i_size
/ 44100 / 4 ;
351 msg_Dbg( obj
, "Track %i offset: %i", i
, p_sectors
[i
] + 150 );
354 msg_Dbg( obj
, "Total length: %i", (int)(i_length
/1000000) );
355 cddb_disc_set_length( p_disc
, (int)(i_length
/1000000) );
357 if( !cddb_disc_calc_discid( p_disc
) )
359 msg_Err( obj
, "CDDB disc ID calculation failed" );
363 const int i_matches
= cddb_query( p_cddb
, p_disc
);
366 msg_Warn( obj
, "CDDB error: %s", cddb_error_str(errno
) );
369 else if( i_matches
== 0 )
371 msg_Dbg( obj
, "Couldn't find any matches in CDDB." );
374 else if( i_matches
> 1 )
375 msg_Warn( obj
, "found %d matches in CDDB. Using first one.", i_matches
);
377 cddb_read( p_cddb
, p_disc
);
379 cddb_destroy( p_cddb
);
384 cddb_disc_destroy( p_disc
);
385 cddb_destroy( p_cddb
);
388 #endif /* HAVE_LIBCDDB */
390 static void AccessGetMeta(stream_t
*access
, vlc_meta_t
*meta
)
392 access_sys_t
*sys
= access
->p_sys
;
394 vlc_meta_SetTitle(meta
, "Audio CD");
396 /* Retrieve CD-TEXT information */
397 if (sys
->cdtextc
> 0 && sys
->cdtextv
[0] != NULL
)
398 vlc_meta_Merge(meta
, sys
->cdtextv
[0]);
400 /* Return true if the given string is not NULL and not empty */
401 #define NONEMPTY( psz ) ( (psz) && *(psz) )
402 /* If the given string is NULL or empty, fill it by the return value of 'code' */
403 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
405 /* Retrieve CDDB information (preferred over CD-TEXT) */
407 if (sys
->cddb
!= NULL
)
409 const char *str
= cddb_disc_get_title(sys
->cddb
);
411 vlc_meta_SetTitle(meta
, str
);
413 str
= cddb_disc_get_genre(sys
->cddb
);
415 vlc_meta_SetGenre(meta
, str
);
417 const unsigned year
= cddb_disc_get_year(sys
->cddb
);
422 snprintf(yearbuf
, sizeof (yearbuf
), "%u", year
);
423 vlc_meta_SetDate(meta
, yearbuf
);
426 /* Set artist only if identical across tracks */
427 str
= cddb_disc_get_artist(sys
->cddb
);
430 for (int i
= 0; i
< sys
->titles
; i
++)
432 cddb_track_t
*t
= cddb_disc_get_track(sys
->cddb
, i
);
436 const char *track_artist
= cddb_track_get_artist(t
);
437 if (NONEMPTY(track_artist
))
442 if (strcmp(str
, track_artist
))
454 static int ReadDir(stream_t
*access
, input_item_node_t
*node
)
456 access_sys_t
*sys
= access
->p_sys
;
458 /* Build title table */
459 for (int i
= 0; i
< sys
->titles
; i
++)
461 msg_Dbg(access
, "track[%d] start=%d", i
, sys
->p_sectors
[i
]);
463 /* Initial/default name */
466 if (unlikely(asprintf(&name
, _("Audio CD - Track %02i"), i
+ 1) == -1))
469 /* Create playlist items */
470 const mtime_t duration
=
471 (mtime_t
)(sys
->p_sectors
[i
+ 1] - sys
->p_sectors
[i
])
472 * CDDA_DATA_SIZE
* CLOCK_FREQ
/ 44100 / 2 / 2;
474 input_item_t
*item
= input_item_NewDisc(access
->psz_url
,
475 (name
!= NULL
) ? name
:
476 access
->psz_url
, duration
);
479 if (unlikely(item
== NULL
))
483 if (likely(asprintf(&opt
, "cdda-track=%i", i
+ 1) != -1))
485 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
489 if (likely(asprintf(&opt
, "cdda-first-sector=%i",
490 sys
->p_sectors
[i
]) != -1))
492 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
496 if (likely(asprintf(&opt
, "cdda-last-sector=%i",
497 sys
->p_sectors
[i
+ 1]) != -1))
499 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
503 const char *title
= NULL
;
504 const char *artist
= NULL
;
505 const char *album
= NULL
;
506 const char *genre
= NULL
;
507 const char *description
= NULL
;
511 if (sys
->cddb
!= NULL
)
513 cddb_track_t
*t
= cddb_disc_get_track(sys
->cddb
, i
);
516 title
= cddb_track_get_title(t
);
517 artist
= cddb_track_get_artist(t
);
520 ON_EMPTY(artist
, cddb_disc_get_artist(sys
->cddb
));
521 album
= cddb_disc_get_title(sys
->cddb
);
522 genre
= cddb_disc_get_genre(sys
->cddb
);
523 year
= cddb_disc_get_year(sys
->cddb
);
528 if (sys
->cdtextc
> 0 && (m
= sys
->cdtextv
[0]) != NULL
)
530 ON_EMPTY(artist
, vlc_meta_Get(m
, vlc_meta_Artist
));
531 ON_EMPTY(album
, vlc_meta_Get(m
, vlc_meta_Album
));
532 ON_EMPTY(genre
, vlc_meta_Get(m
, vlc_meta_Genre
));
533 description
= vlc_meta_Get(m
, vlc_meta_Description
);
536 if (i
+ 1 < sys
->cdtextc
&& (m
= sys
->cdtextv
[i
+ 1]) != NULL
)
538 ON_EMPTY(title
, vlc_meta_Get(m
, vlc_meta_Title
));
539 ON_EMPTY(artist
, vlc_meta_Get(m
, vlc_meta_Artist
));
540 ON_EMPTY(genre
, vlc_meta_Get(m
, vlc_meta_Genre
));
541 ON_EMPTY(description
, vlc_meta_Get(m
, vlc_meta_Description
));
546 input_item_SetName(item
, title
);
547 input_item_SetTitle(item
, title
);
550 if (NONEMPTY(artist
))
551 input_item_SetArtist(item
, artist
);
554 input_item_SetGenre(item
, genre
);
556 if (NONEMPTY(description
))
557 input_item_SetDescription(item
, description
);
560 input_item_SetAlbum(item
, album
);
566 snprintf(yearbuf
, sizeof (yearbuf
), "%u", year
);
567 input_item_SetDate(item
, yearbuf
);
571 snprintf(num
, sizeof (num
), "%d", i
+ 1);
572 input_item_SetTrackNum(item
, num
);
574 input_item_node_AppendItem(node
, item
);
575 input_item_Release(item
);
582 static int AccessControl(stream_t
*access
, int query
, va_list args
)
584 if (query
== STREAM_GET_META
)
586 AccessGetMeta(access
, va_arg(args
, vlc_meta_t
*));
589 return access_vaDirectoryControlHelper(access
, query
, args
);
592 static int AccessOpen(vlc_object_t
*obj
, vcddev_t
*dev
)
594 stream_t
*access
= (stream_t
*)obj
;
595 /* Only whole discs here */
596 access_sys_t
*sys
= vlc_obj_malloc(obj
, sizeof (*sys
));
597 if (unlikely(sys
== NULL
))
599 ioctl_Close(obj
, dev
);
604 sys
->p_sectors
= NULL
;
606 sys
->titles
= ioctl_GetTracksMap(obj
, dev
, &sys
->p_sectors
);
609 msg_Err(obj
, "cannot count tracks");
613 if (sys
->titles
== 0)
615 msg_Err(obj
, "no audio tracks found");
620 msg_Dbg(obj
, "retrieving metadata with CDDB");
622 sys
->cddb
= GetCDDBInfo(obj
, sys
->titles
, sys
->p_sectors
);
623 if (sys
->cddb
!= NULL
)
624 msg_Dbg(obj
, "disc ID: 0x%08x", cddb_disc_get_discid(sys
->cddb
));
626 msg_Dbg(obj
, "CDDB failure");
629 if (ioctl_GetCdText(obj
, dev
, &sys
->cdtextv
, &sys
->cdtextc
))
631 msg_Dbg(obj
, "CD-TEXT information missing");
637 access
->pf_read
= NULL
;
638 access
->pf_block
= NULL
;
639 access
->pf_readdir
= ReadDir
;
640 access
->pf_seek
= NULL
;
641 access
->pf_control
= AccessControl
;
645 free(sys
->p_sectors
);
646 ioctl_Close(obj
, dev
);
650 static void AccessClose(access_sys_t
*sys
)
652 for (int i
= 0; i
< sys
->cdtextc
; i
++)
654 vlc_meta_t
*meta
= sys
->cdtextv
[i
];
656 vlc_meta_Delete(meta
);
661 if (sys
->cddb
!= NULL
)
662 cddb_disc_destroy(sys
->cddb
);
665 free(sys
->p_sectors
);
668 static int Open(vlc_object_t
*obj
)
670 stream_t
*stream
= (stream_t
*)obj
;
673 vcddev_t
*dev
= DiscOpen(obj
, stream
->psz_location
, stream
->psz_filepath
,
679 return AccessOpen(obj
, dev
);
681 return DemuxOpen(obj
, dev
, track
);
684 static void Close(vlc_object_t
*obj
)
686 stream_t
*stream
= (stream_t
*)obj
;
687 void *sys
= stream
->p_sys
;
689 if (stream
->pf_readdir
!= NULL
)
692 static_assert(offsetof(demux_sys_t
, vcddev
) == 0, "Invalid cast");
693 static_assert(offsetof(access_sys_t
, vcddev
) == 0, "Invalid cast");
694 ioctl_Close(obj
, *(vcddev_t
**)sys
);
697 /*****************************************************************************
699 *****************************************************************************/
700 #define CDAUDIO_DEV_TEXT N_("Audio CD device")
701 #if defined( _WIN32 ) || defined( __OS2__ )
702 # define CDAUDIO_DEV_LONGTEXT N_( \
703 "This is the default Audio CD drive (or file) to use. Don't forget the " \
704 "colon after the drive letter (e.g. D:)")
705 # define CD_DEVICE "D:"
707 # define CDAUDIO_DEV_LONGTEXT N_( \
708 "This is the default Audio CD device to use." )
709 # if defined(__OpenBSD__)
710 # define CD_DEVICE "/dev/cd0c"
711 # elif defined(__linux__)
712 # define CD_DEVICE "/dev/sr0"
714 # define CD_DEVICE "/dev/cdrom"
719 set_shortname( N_("Audio CD") )
720 set_description( N_("Audio CD input") )
721 set_capability( "access", 0 )
722 set_category( CAT_INPUT
)
723 set_subcategory( SUBCAT_INPUT_ACCESS
)
724 set_callbacks(Open
, Close
)
726 add_loadfile("cd-audio", CD_DEVICE
, CDAUDIO_DEV_TEXT
, CDAUDIO_DEV_LONGTEXT
)
728 add_usage_hint( N_("[cdda:][device][@[track]]") )
729 add_integer( "cdda-track", 0 , NULL
, NULL
, true )
731 add_integer( "cdda-first-sector", -1, NULL
, NULL
, true )
733 add_integer( "cdda-last-sector", -1, NULL
, NULL
, true )
737 add_string( "cddb-server", "freedb.videolan.org", N_( "CDDB Server" ),
738 N_( "Address of the CDDB server to use." ), true )
739 add_integer( "cddb-port", 80, N_( "CDDB port" ),
740 N_( "CDDB Server port to use." ), true )
741 change_integer_range( 1, 65535 )
744 add_shortcut( "cdda", "cddasimple" )