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
= VLC_TS_0
+ 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
, VLC_TS_0
+ 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
)
217 demux_t
*demux
= (demux_t
*)obj
;
220 vcddev_t
*dev
= DiscOpen(obj
, demux
->psz_location
, demux
->psz_file
,
225 if (track
== 0 /* Whole disc -> use access plugin */)
228 demux_sys_t
*sys
= vlc_obj_alloc(obj
, 1, sizeof (*sys
));
229 if (unlikely(sys
== NULL
))
234 sys
->start
= var_InheritInteger(obj
, "cdda-first-sector");
235 sys
->length
= var_InheritInteger(obj
, "cdda-last-sector") - sys
->start
;
237 /* Track number in input item */
238 if (sys
->start
== (unsigned)-1 || sys
->length
== (unsigned)-1)
240 int *sectors
= NULL
; /* Track sectors */
241 unsigned titles
= ioctl_GetTracksMap(obj
, dev
, §ors
);
245 msg_Err(obj
, "invalid track number: %u/%u", track
, titles
);
250 sys
->start
= sectors
[track
- 1];
251 sys
->length
= sectors
[track
] - sys
->start
;
257 es_format_Init(&fmt
, AUDIO_ES
, VLC_CODEC_S16L
);
258 fmt
.audio
.i_rate
= 44100;
259 fmt
.audio
.i_channels
= 2;
260 sys
->es
= es_out_Add(demux
->out
, &fmt
);
262 date_Init(&sys
->pts
, 44100, 1);
263 date_Set(&sys
->pts
, 0);
266 demux
->pf_demux
= Demux
;
267 demux
->pf_control
= DemuxControl
;
271 ioctl_Close(obj
, dev
);
275 static void DemuxClose(vlc_object_t
*obj
)
277 demux_t
*demux
= (demux_t
*)obj
;
278 demux_sys_t
*sys
= demux
->p_sys
;
280 ioctl_Close(obj
, sys
->vcddev
);
283 /*****************************************************************************
284 * Access: local prototypes
285 *****************************************************************************/
288 vcddev_t
*vcddev
; /* vcd device descriptor */
289 int *p_sectors
; /* Track sectors */
292 vlc_meta_t
**cdtextv
;
299 static cddb_disc_t
*GetCDDBInfo( vlc_object_t
*obj
, int i_titles
, int *p_sectors
)
301 if( !var_InheritBool( obj
, "metadata-network-access" ) )
303 msg_Dbg( obj
, "album art policy set to manual: not fetching" );
308 cddb_conn_t
*p_cddb
= cddb_new();
311 msg_Warn( obj
, "unable to use CDDB" );
317 cddb_http_enable( p_cddb
);
319 char *psz_tmp
= var_InheritString( obj
, "cddb-server" );
322 cddb_set_server_name( p_cddb
, psz_tmp
);
326 cddb_set_server_port( p_cddb
, var_InheritInteger( obj
, "cddb-port" ) );
328 cddb_set_email_address( p_cddb
, "vlc@videolan.org" );
330 cddb_set_http_path_query( p_cddb
, "/~cddb/cddb.cgi" );
331 cddb_set_http_path_submit( p_cddb
, "/~cddb/submit.cgi" );
335 char *psz_temp
= config_GetUserDir( VLC_CACHE_DIR
);
337 if( asprintf( &psz_cachedir
, "%s" DIR_SEP
"cddb", psz_temp
) > 0 ) {
338 cddb_cache_enable( p_cddb
);
339 cddb_cache_set_dir( p_cddb
, psz_cachedir
);
340 free( psz_cachedir
);
344 cddb_set_timeout( p_cddb
, 10 );
347 cddb_disc_t
*p_disc
= cddb_disc_new();
350 msg_Err( obj
, "unable to create CDDB disc structure." );
354 int64_t i_length
= 2000000; /* PreGap */
355 for( int i
= 0; i
< i_titles
; i
++ )
357 cddb_track_t
*t
= cddb_track_new();
358 cddb_track_set_frame_offset( t
, p_sectors
[i
] + 150 ); /* Pregap offset */
360 cddb_disc_add_track( p_disc
, t
);
361 const int64_t i_size
= ( p_sectors
[i
+1] - p_sectors
[i
] ) *
362 (int64_t)CDDA_DATA_SIZE
;
363 i_length
+= INT64_C(1000000) * i_size
/ 44100 / 4 ;
365 msg_Dbg( obj
, "Track %i offset: %i", i
, p_sectors
[i
] + 150 );
368 msg_Dbg( obj
, "Total length: %i", (int)(i_length
/1000000) );
369 cddb_disc_set_length( p_disc
, (int)(i_length
/1000000) );
371 if( !cddb_disc_calc_discid( p_disc
) )
373 msg_Err( obj
, "CDDB disc ID calculation failed" );
377 const int i_matches
= cddb_query( p_cddb
, p_disc
);
380 msg_Warn( obj
, "CDDB error: %s", cddb_error_str(errno
) );
383 else if( i_matches
== 0 )
385 msg_Dbg( obj
, "Couldn't find any matches in CDDB." );
388 else if( i_matches
> 1 )
389 msg_Warn( obj
, "found %d matches in CDDB. Using first one.", i_matches
);
391 cddb_read( p_cddb
, p_disc
);
393 cddb_destroy( p_cddb
);
398 cddb_disc_destroy( p_disc
);
399 cddb_destroy( p_cddb
);
402 #endif /* HAVE_LIBCDDB */
404 static void AccessGetMeta(stream_t
*access
, vlc_meta_t
*meta
)
406 access_sys_t
*sys
= access
->p_sys
;
408 vlc_meta_SetTitle(meta
, "Audio CD");
410 /* Retrieve CD-TEXT information */
411 if (sys
->cdtextc
> 0 && sys
->cdtextv
[0] != NULL
)
412 vlc_meta_Merge(meta
, sys
->cdtextv
[0]);
414 /* Return true if the given string is not NULL and not empty */
415 #define NONEMPTY( psz ) ( (psz) && *(psz) )
416 /* If the given string is NULL or empty, fill it by the return value of 'code' */
417 #define ON_EMPTY( psz, code ) do { if( !NONEMPTY( psz) ) { (psz) = code; } } while(0)
419 /* Retrieve CDDB information (preferred over CD-TEXT) */
421 if (sys
->cddb
!= NULL
)
423 const char *str
= cddb_disc_get_title(sys
->cddb
);
425 vlc_meta_SetTitle(meta
, str
);
427 str
= cddb_disc_get_genre(sys
->cddb
);
429 vlc_meta_SetGenre(meta
, str
);
431 const unsigned year
= cddb_disc_get_year(sys
->cddb
);
436 snprintf(yearbuf
, sizeof (yearbuf
), "%u", year
);
437 vlc_meta_SetDate(meta
, yearbuf
);
440 /* Set artist only if identical across tracks */
441 str
= cddb_disc_get_artist(sys
->cddb
);
444 for (int i
= 0; i
< sys
->titles
; i
++)
446 cddb_track_t
*t
= cddb_disc_get_track(sys
->cddb
, i
);
450 const char *track_artist
= cddb_track_get_artist(t
);
451 if (NONEMPTY(track_artist
))
456 if (strcmp(str
, track_artist
))
468 static int ReadDir(stream_t
*access
, input_item_node_t
*node
)
470 access_sys_t
*sys
= access
->p_sys
;
472 /* Build title table */
473 for (int i
= 0; i
< sys
->titles
; i
++)
475 msg_Dbg(access
, "track[%d] start=%d", i
, sys
->p_sectors
[i
]);
477 /* Initial/default name */
480 if (unlikely(asprintf(&name
, _("Audio CD - Track %02i"), i
+ 1) == -1))
483 /* Create playlist items */
484 const mtime_t duration
=
485 (mtime_t
)(sys
->p_sectors
[i
+ 1] - sys
->p_sectors
[i
])
486 * CDDA_DATA_SIZE
* CLOCK_FREQ
/ 44100 / 2 / 2;
488 input_item_t
*item
= input_item_NewDisc(access
->psz_url
,
489 (name
!= NULL
) ? name
:
490 access
->psz_url
, duration
);
493 if (unlikely(item
== NULL
))
497 if (likely(asprintf(&opt
, "cdda-track=%i", i
+ 1) != -1))
499 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
503 if (likely(asprintf(&opt
, "cdda-first-sector=%i",
504 sys
->p_sectors
[i
]) != -1))
506 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
510 if (likely(asprintf(&opt
, "cdda-last-sector=%i",
511 sys
->p_sectors
[i
+ 1]) != -1))
513 input_item_AddOption(item
, opt
, VLC_INPUT_OPTION_TRUSTED
);
517 const char *title
= NULL
;
518 const char *artist
= NULL
;
519 const char *album
= NULL
;
520 const char *genre
= NULL
;
521 const char *description
= NULL
;
525 if (sys
->cddb
!= NULL
)
527 cddb_track_t
*t
= cddb_disc_get_track(sys
->cddb
, i
);
530 title
= cddb_track_get_title(t
);
531 artist
= cddb_track_get_artist(t
);
534 ON_EMPTY(artist
, cddb_disc_get_artist(sys
->cddb
));
535 album
= cddb_disc_get_title(sys
->cddb
);
536 genre
= cddb_disc_get_genre(sys
->cddb
);
537 year
= cddb_disc_get_year(sys
->cddb
);
542 if (sys
->cdtextc
> 0 && (m
= sys
->cdtextv
[0]) != NULL
)
544 ON_EMPTY(artist
, vlc_meta_Get(m
, vlc_meta_Artist
));
545 ON_EMPTY(album
, vlc_meta_Get(m
, vlc_meta_Album
));
546 ON_EMPTY(genre
, vlc_meta_Get(m
, vlc_meta_Genre
));
547 description
= vlc_meta_Get(m
, vlc_meta_Description
);
550 if (i
+ 1 < sys
->cdtextc
&& (m
= sys
->cdtextv
[i
+ 1]) != NULL
)
552 ON_EMPTY(title
, vlc_meta_Get(m
, vlc_meta_Title
));
553 ON_EMPTY(artist
, vlc_meta_Get(m
, vlc_meta_Artist
));
554 ON_EMPTY(genre
, vlc_meta_Get(m
, vlc_meta_Genre
));
555 ON_EMPTY(description
, vlc_meta_Get(m
, vlc_meta_Description
));
560 input_item_SetName(item
, title
);
561 input_item_SetTitle(item
, title
);
564 if (NONEMPTY(artist
))
565 input_item_SetArtist(item
, artist
);
568 input_item_SetGenre(item
, genre
);
570 if (NONEMPTY(description
))
571 input_item_SetDescription(item
, description
);
574 input_item_SetAlbum(item
, album
);
580 snprintf(yearbuf
, sizeof (yearbuf
), "%u", year
);
581 input_item_SetDate(item
, yearbuf
);
585 snprintf(num
, sizeof (num
), "%d", i
+ 1);
586 input_item_SetTrackNum(item
, num
);
588 input_item_node_AppendItem(node
, item
);
589 input_item_Release(item
);
596 static int AccessControl(stream_t
*access
, int query
, va_list args
)
598 if (query
== STREAM_GET_META
)
600 AccessGetMeta(access
, va_arg(args
, vlc_meta_t
*));
603 return access_vaDirectoryControlHelper(access
, query
, args
);
606 static int AccessOpen(vlc_object_t
*obj
)
608 stream_t
*access
= (stream_t
*)obj
;
611 vcddev_t
*dev
= DiscOpen(obj
, access
->psz_location
, access
->psz_filepath
,
616 if (track
!= 0 /* Only whole discs here */)
618 ioctl_Close(obj
, dev
);
622 access_sys_t
*sys
= vlc_obj_alloc(obj
, 1, sizeof (*sys
));
623 if (unlikely(sys
== NULL
))
625 ioctl_Close(obj
, dev
);
630 sys
->p_sectors
= NULL
;
632 sys
->titles
= ioctl_GetTracksMap(obj
, dev
, &sys
->p_sectors
);
635 msg_Err(obj
, "cannot count tracks");
639 if (sys
->titles
== 0)
641 msg_Err(obj
, "no audio tracks found");
646 msg_Dbg(obj
, "retrieving metadata with CDDB");
648 sys
->cddb
= GetCDDBInfo(obj
, sys
->titles
, sys
->p_sectors
);
649 if (sys
->cddb
!= NULL
)
650 msg_Dbg(obj
, "disc ID: 0x%08x", cddb_disc_get_discid(sys
->cddb
));
652 msg_Dbg(obj
, "CDDB failure");
655 if (ioctl_GetCdText(obj
, dev
, &sys
->cdtextv
, &sys
->cdtextc
))
657 msg_Dbg(obj
, "CD-TEXT information missing");
663 access
->pf_read
= NULL
;
664 access
->pf_block
= NULL
;
665 access
->pf_readdir
= ReadDir
;
666 access
->pf_seek
= NULL
;
667 access
->pf_control
= AccessControl
;
671 free(sys
->p_sectors
);
672 ioctl_Close(obj
, dev
);
676 static void AccessClose(vlc_object_t
*obj
)
678 stream_t
*access
= (stream_t
*)obj
;
679 access_sys_t
*sys
= access
->p_sys
;
681 for (int i
= 0; i
< sys
->cdtextc
; i
++)
683 vlc_meta_t
*meta
= sys
->cdtextv
[i
];
685 vlc_meta_Delete(meta
);
690 if (sys
->cddb
!= NULL
)
691 cddb_disc_destroy(sys
->cddb
);
694 free(sys
->p_sectors
);
695 ioctl_Close(obj
, sys
->vcddev
);
698 /*****************************************************************************
700 *****************************************************************************/
701 #define CDAUDIO_DEV_TEXT N_("Audio CD device")
702 #if defined( _WIN32 ) || defined( __OS2__ )
703 # define CDAUDIO_DEV_LONGTEXT N_( \
704 "This is the default Audio CD drive (or file) to use. Don't forget the " \
705 "colon after the drive letter (e.g. D:)")
706 # define CD_DEVICE "D:"
708 # define CDAUDIO_DEV_LONGTEXT N_( \
709 "This is the default Audio CD device to use." )
710 # if defined(__OpenBSD__)
711 # define CD_DEVICE "/dev/cd0c"
712 # elif defined(__linux__)
713 # define CD_DEVICE "/dev/sr0"
715 # define CD_DEVICE "/dev/cdrom"
720 set_shortname( N_("Audio CD") )
721 set_description( N_("Audio CD input") )
722 set_capability( "access", 10 )
723 set_category( CAT_INPUT
)
724 set_subcategory( SUBCAT_INPUT_ACCESS
)
725 set_callbacks(AccessOpen
, AccessClose
)
727 add_loadfile( "cd-audio", CD_DEVICE
, CDAUDIO_DEV_TEXT
,
728 CDAUDIO_DEV_LONGTEXT
, false )
730 add_usage_hint( N_("[cdda:][device][@[track]]") )
731 add_integer( "cdda-track", 0 , NULL
, NULL
, true )
733 add_integer( "cdda-first-sector", -1, NULL
, NULL
, true )
735 add_integer( "cdda-last-sector", -1, NULL
, NULL
, true )
739 add_string( "cddb-server", "freedb.videolan.org", N_( "CDDB Server" ),
740 N_( "Address of the CDDB server to use." ), true )
741 add_integer( "cddb-port", 80, N_( "CDDB port" ),
742 N_( "CDDB Server port to use." ), true )
743 change_integer_range( 1, 65535 )
746 add_shortcut( "cdda", "cddasimple" )
749 set_capability( "access_demux", 10 )
750 set_callbacks(DemuxOpen
, DemuxClose
)