1 /*****************************************************************************
2 * dvd.c : DVD input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: dvd.c,v 1.11 2004/01/25 18:53:06 gbazin Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* strdup() */
36 # include <sys/types.h>
37 # include <sys/stat.h>
38 # include <sys/uio.h> /* struct iovec */
39 # include <sys/ioctl.h>
41 # include <netinet/in.h>
42 # include <linux/cdrom.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 int E_(DVDOpen) ( vlc_object_t * );
51 void E_(DVDClose) ( vlc_object_t * );
53 int E_(DVDInit) ( vlc_object_t * );
54 void E_(DVDEnd) ( vlc_object_t * );
57 static void *p_libdvdcss;
58 static void ProbeLibDVDCSS ( void );
59 static void UnprobeLibDVDCSS( void );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define CSSMETHOD_TEXT N_("Method used by libdvdcss for decryption")
66 #define CSSMETHOD_LONGTEXT N_( \
67 "Set the method used by libdvdcss for key decryption.\n" \
68 "title: decrypted title key is guessed from the encrypted sectors of " \
69 "the stream. Thus it should work with a file as well as the " \
70 "DVD device. But it sometimes takes much time to decrypt a title " \
71 "key and may even fail. With this method, the key is only checked "\
72 "at the beginning of each title, so it won't work if the key " \
73 "changes in the middle of a title.\n" \
74 "disc: the disc key is first cracked, then all title keys can be " \
75 "decrypted instantly, which allows us to check them often.\n" \
76 "key: the same as \"disc\" if you don't have a file with player keys " \
77 "at compilation time. If you do, the decryption of the disc key " \
78 "will be faster with this method. It is the one that was used by " \
80 "The default method is: key.")
82 static char *psz_css_list[] = { "title", "disc", "key" };
83 static char *psz_css_list_text[] = { N_("title"), N_("Disc"), N_("Key") };
87 add_usage_hint( N_("[dvd:][device][@raw_device][@[title][,[chapter][,angle]]]") );
89 add_string( "dvdcss-method", NULL, NULL, CSSMETHOD_TEXT,
90 CSSMETHOD_LONGTEXT, VLC_TRUE );
91 change_string_list( psz_css_list, psz_css_list_text, 0 );
94 set_description( _("DVD input (uses libdvdcss if installed)") );
97 set_description( _("DVD input (uses libdvdcss)") );
101 add_shortcut( "dvdold" );
102 add_shortcut( "dvdsimple" );
103 set_capability( "access", i );
104 set_callbacks( E_(DVDOpen), E_(DVDClose) );
107 set_capability( "demux", 0 );
108 set_callbacks( E_(DVDInit), E_(DVDEnd) );
118 /* Following functions are local */
121 /*****************************************************************************
122 * ProbeLibDVDCSS: look for a libdvdcss object.
123 *****************************************************************************
124 * This functions looks for libdvdcss, using dlopen(), and fills function
125 * pointers with what it finds. On failure, uses the dummy libdvdcss
126 * replacement provided by vlc.
127 *****************************************************************************/
128 static void ProbeLibDVDCSS( void )
130 static char *pp_filelist[] = { "libdvdcss.so.2",
132 "./lib/libdvdcss.so.2",
135 "./lib/libdvdcss.so.1",
137 char **pp_file = pp_filelist;
139 /* Try to open the dynamic object */
142 p_libdvdcss = dlopen( *pp_file, RTLD_LAZY );
143 if( p_libdvdcss != NULL )
145 //X intf_WarnMsg( 2, "module: builtin module `dvd' found libdvdcss "
146 //X "in `%s'", *pp_file );
151 } while( *pp_file != NULL );
153 /* If libdvdcss.so was found, check that it's valid */
154 if( p_libdvdcss == NULL )
156 //X intf_ErrMsg( "dvd warning: libdvdcss.so.2 not present" );
160 ____dvdcss_open = dlsym( p_libdvdcss, "dvdcss_open" );
161 ____dvdcss_close = dlsym( p_libdvdcss, "dvdcss_close" );
162 ____dvdcss_title = dlsym( p_libdvdcss, "dvdcss_title" );
163 ____dvdcss_seek = dlsym( p_libdvdcss, "dvdcss_seek" );
164 ____dvdcss_read = dlsym( p_libdvdcss, "dvdcss_read" );
165 ____dvdcss_readv = dlsym( p_libdvdcss, "dvdcss_readv" );
166 ____dvdcss_error = dlsym( p_libdvdcss, "dvdcss_error" );
168 if( ____dvdcss_open == NULL || ____dvdcss_close == NULL
169 || ____dvdcss_title == NULL || ____dvdcss_seek == NULL
170 || ____dvdcss_read == NULL || ____dvdcss_readv == NULL
171 || ____dvdcss_error == NULL )
173 //X intf_ErrMsg( "dvd warning: missing symbols in libdvdcss.so.2, "
174 //X "this shouldn't happen !" );
175 dlclose( p_libdvdcss );
180 /* If libdvdcss was not found or was not valid, use the dummy
181 * replacement functions. */
182 if( p_libdvdcss == NULL )
184 //X intf_ErrMsg( "dvd warning: no valid libdvdcss found, "
185 //X "I will only play unencrypted DVDs" );
186 //X intf_ErrMsg( "dvd warning: get libdvdcss at "
187 //X "http://www.videolan.org/libdvdcss/" );
189 ____dvdcss_open = dummy_dvdcss_open;
190 ____dvdcss_close = dummy_dvdcss_close;
191 ____dvdcss_title = dummy_dvdcss_title;
192 ____dvdcss_seek = dummy_dvdcss_seek;
193 ____dvdcss_read = dummy_dvdcss_read;
194 ____dvdcss_readv = dummy_dvdcss_readv;
195 ____dvdcss_error = dummy_dvdcss_error;
199 /*****************************************************************************
200 * UnprobeLibDVDCSS: free resources allocated by ProbeLibDVDCSS, if any.
201 *****************************************************************************/
202 static void UnprobeLibDVDCSS( void )
204 if( p_libdvdcss != NULL )
206 dlclose( p_libdvdcss );
211 /* Dummy libdvdcss with minimal DVD access. */
213 /*****************************************************************************
215 *****************************************************************************/
218 /* File descriptor */
222 /*****************************************************************************
223 * dvdcss_open: initialize library, open a DVD device, crack CSS key
224 *****************************************************************************/
225 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
227 dvdcss_handle dvdcss;
230 /* Allocate the library structure */
231 dvdcss = malloc( sizeof( struct dvdcss_s ) );
234 fprintf( stderr, "dvd error: "
235 "dummy libdvdcss could not allocate memory\n" );
239 /* Open the device */
240 dvdcss->i_fd = open( psz_target, 0 );
241 if( dvdcss->i_fd < 0 )
243 fprintf( stderr, "dvd error: "
244 "dummy libdvdcss could not open device\n" );
249 /* Check for encryption or ioctl failure */
250 dvd.type = DVD_STRUCT_COPYRIGHT;
251 dvd.copyright.layer_num = 0;
252 if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
253 || dvd.copyright.cpst )
255 fprintf( stderr, "dvd error: "
256 "dummy libdvdcss could not decrypt disc\n" );
257 close( dvdcss->i_fd );
265 /*****************************************************************************
266 * dvdcss_error: return the last libdvdcss error message
267 *****************************************************************************/
268 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
270 return "generic error";
273 /*****************************************************************************
274 * dvdcss_seek: seek into the device
275 *****************************************************************************/
276 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
281 i_read = lseek( dvdcss->i_fd,
282 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
284 return i_read / DVDCSS_BLOCK_SIZE;
287 /*****************************************************************************
288 * dvdcss_title: crack the current title key if needed
289 *****************************************************************************/
290 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
295 /*****************************************************************************
296 * dvdcss_read: read data from the device, decrypt if requested
297 *****************************************************************************/
298 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
304 i_bytes = read( dvdcss->i_fd, p_buffer,
305 (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
307 return i_bytes / DVDCSS_BLOCK_SIZE;
310 /*****************************************************************************
311 * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
312 *****************************************************************************/
313 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
319 i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
321 return i_read / DVDCSS_BLOCK_SIZE;
324 /*****************************************************************************
325 * dvdcss_close: close the DVD device and clean up the library
326 *****************************************************************************/
327 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
331 i_ret = close( dvdcss->i_fd );