1 /*****************************************************************************
2 * dvd.c : DVD input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: dvd.c,v 1.32 2002/06/01 12:31:58 sam 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>
44 # include "dummy_dvdcss.h"
47 /*****************************************************************************
48 * Capabilities defined in the other files.
49 *****************************************************************************/
50 void _M( access_getfunctions)( function_list_t * p_function_list );
51 void _M( demux_getfunctions)( function_list_t * p_function_list );
53 /*****************************************************************************
55 *****************************************************************************/
57 static void *p_libdvdcss;
58 static void ProbeLibDVDCSS ( void );
59 static void UnprobeLibDVDCSS( void );
62 /*****************************************************************************
63 * Build configuration tree.
64 *****************************************************************************/
66 ADD_CATEGORY_HINT( N_("[dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"), NULL )
70 ADD_CAPABILITY( DEMUX, 0 )
72 SET_DESCRIPTION( _("DVD input module, uses libdvdcss if present") )
73 ADD_CAPABILITY( ACCESS, 90 )
75 SET_DESCRIPTION( _("DVD input module, uses libdvdcss") )
76 ADD_CAPABILITY( ACCESS, 100 )
81 _M( access_getfunctions)( &p_module->p_functions->access );
82 _M( demux_getfunctions)( &p_module->p_functions->demux );
88 MODULE_DEACTIVATE_START
92 MODULE_DEACTIVATE_STOP
95 /* Following functions are local */
98 /*****************************************************************************
99 * ProbeLibDVDCSS: look for a libdvdcss object.
100 *****************************************************************************
101 * This functions looks for libdvdcss, using dlopen(), and fills function
102 * pointers with what it finds. On failure, uses the dummy libdvdcss
103 * replacement provided by vlc.
104 *****************************************************************************/
105 static void ProbeLibDVDCSS( void )
107 static char *pp_filelist[] = { "libdvdcss.so.2",
109 "./lib/libdvdcss.so.2",
112 "./lib/libdvdcss.so.1",
114 char **pp_file = pp_filelist;
116 /* Try to open the dynamic object */
119 p_libdvdcss = dlopen( *pp_file, RTLD_LAZY );
120 if( p_libdvdcss != NULL )
122 //X intf_WarnMsg( 2, "module: builtin module `dvd' found libdvdcss "
123 //X "in `%s'", *pp_file );
128 } while( *pp_file != NULL );
130 /* If libdvdcss.so was found, check that it's valid */
131 if( p_libdvdcss == NULL )
133 //X intf_ErrMsg( "dvd warning: libdvdcss.so.2 not present" );
137 ____dvdcss_open = dlsym( p_libdvdcss, "dvdcss_open" );
138 ____dvdcss_close = dlsym( p_libdvdcss, "dvdcss_close" );
139 ____dvdcss_title = dlsym( p_libdvdcss, "dvdcss_title" );
140 ____dvdcss_seek = dlsym( p_libdvdcss, "dvdcss_seek" );
141 ____dvdcss_read = dlsym( p_libdvdcss, "dvdcss_read" );
142 ____dvdcss_readv = dlsym( p_libdvdcss, "dvdcss_readv" );
143 ____dvdcss_error = dlsym( p_libdvdcss, "dvdcss_error" );
145 if( ____dvdcss_open == NULL || ____dvdcss_close == NULL
146 || ____dvdcss_title == NULL || ____dvdcss_seek == NULL
147 || ____dvdcss_read == NULL || ____dvdcss_readv == NULL
148 || ____dvdcss_error == NULL )
150 //X intf_ErrMsg( "dvd warning: missing symbols in libdvdcss.so.2, "
151 //X "this shouldn't happen !" );
152 dlclose( p_libdvdcss );
157 /* If libdvdcss was not found or was not valid, use the dummy
158 * replacement functions. */
159 if( p_libdvdcss == NULL )
161 //X intf_ErrMsg( "dvd warning: no valid libdvdcss found, "
162 //X "I will only play unencrypted DVDs" );
163 //X intf_ErrMsg( "dvd warning: get libdvdcss at "
164 //X "http://www.videolan.org/libdvdcss/" );
166 ____dvdcss_open = dummy_dvdcss_open;
167 ____dvdcss_close = dummy_dvdcss_close;
168 ____dvdcss_title = dummy_dvdcss_title;
169 ____dvdcss_seek = dummy_dvdcss_seek;
170 ____dvdcss_read = dummy_dvdcss_read;
171 ____dvdcss_readv = dummy_dvdcss_readv;
172 ____dvdcss_error = dummy_dvdcss_error;
176 /*****************************************************************************
177 * UnprobeLibDVDCSS: free resources allocated by ProbeLibDVDCSS, if any.
178 *****************************************************************************/
179 static void UnprobeLibDVDCSS( void )
181 if( p_libdvdcss != NULL )
183 dlclose( p_libdvdcss );
188 /* Dummy libdvdcss with minimal DVD access. */
190 /*****************************************************************************
192 *****************************************************************************/
195 /* File descriptor */
199 /*****************************************************************************
200 * dvdcss_open: initialize library, open a DVD device, crack CSS key
201 *****************************************************************************/
202 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
204 dvdcss_handle dvdcss;
207 /* Allocate the library structure */
208 dvdcss = malloc( sizeof( struct dvdcss_s ) );
211 fprintf( stderr, "dvd error: "
212 "dummy libdvdcss could not allocate memory\n" );
216 /* Open the device */
217 dvdcss->i_fd = open( psz_target, 0 );
218 if( dvdcss->i_fd < 0 )
220 fprintf( stderr, "dvd error: "
221 "dummy libdvdcss could not open device\n" );
226 /* Check for encryption or ioctl failure */
227 dvd.type = DVD_STRUCT_COPYRIGHT;
228 dvd.copyright.layer_num = 0;
229 if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
230 || dvd.copyright.cpst )
232 fprintf( stderr, "dvd error: "
233 "dummy libdvdcss could not decrypt disc\n" );
234 close( dvdcss->i_fd );
242 /*****************************************************************************
243 * dvdcss_error: return the last libdvdcss error message
244 *****************************************************************************/
245 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
247 return "generic error";
250 /*****************************************************************************
251 * dvdcss_seek: seek into the device
252 *****************************************************************************/
253 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
258 i_read = lseek( dvdcss->i_fd,
259 (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
261 return i_read / DVDCSS_BLOCK_SIZE;
264 /*****************************************************************************
265 * dvdcss_title: crack the current title key if needed
266 *****************************************************************************/
267 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
272 /*****************************************************************************
273 * dvdcss_read: read data from the device, decrypt if requested
274 *****************************************************************************/
275 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
281 i_bytes = read( dvdcss->i_fd, p_buffer,
282 (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
284 return i_bytes / DVDCSS_BLOCK_SIZE;
287 /*****************************************************************************
288 * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
289 *****************************************************************************/
290 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
296 i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
298 return i_read / DVDCSS_BLOCK_SIZE;
301 /*****************************************************************************
302 * dvdcss_close: close the DVD device and clean up the library
303 *****************************************************************************/
304 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
308 i_ret = close( dvdcss->i_fd );