]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd.c
* Added a --dvd-css-method config option to the dvd plugin. This is implemented with
[vlc] / plugins / dvd / dvd.c
1 /*****************************************************************************
2  * dvd.c : DVD input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: dvd.c,v 1.35 2002/08/01 12:58:38 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* strdup() */
29
30 #include <vlc/vlc.h>
31
32 #ifdef GOD_DAMN_DMCA
33 #   include <stdio.h>
34 #   include <fcntl.h>
35 #   include <unistd.h>
36 #   include <sys/types.h>
37 #   include <sys/stat.h>
38 #   include <sys/uio.h>                                      /* struct iovec */
39 #   include <sys/ioctl.h>
40 #   include <dlfcn.h>
41 #   include <netinet/in.h>
42 #   include <linux/cdrom.h>
43
44 #   include "dummy_dvdcss.h"
45 #endif
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 int  E_(DVDOpen)   ( vlc_object_t * );
51 void E_(DVDClose)  ( vlc_object_t * );
52
53 int  E_(DVDInit)   ( vlc_object_t * );
54
55 #ifdef GOD_DAMN_DMCA
56 static void *p_libdvdcss;
57 static void ProbeLibDVDCSS  ( void );
58 static void UnprobeLibDVDCSS( void );
59 #endif
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 #define CSSMETHOD_TEXT N_("Method to use by libdvdcss for key decryption")
65 #define CSSMETHOD_LONGTEXT N_( \
66     "title: decrypted title key is guessed from the encrypted sectors of " \
67            "the stream. Thus it should work with a file as well as the " \
68            "DVD device. But it sometimes takes much time to decrypt a title " \
69            "key and may even fail. With this method, the key is only checked "\
70            "at the beginning of each title, so it won't work if the key " \
71            "changes in the middle of a title.\n" \
72     "disc: the disc key is first cracked, then all title keys can be " \
73            "decrypted instantly, which allows us to check them often.\n" \
74     "key: the same as \"disc\" if you don't have a file with player keys " \
75            "at compilation time. If you do, the decryption of the disc key " \
76            "will be faster with this method. It is the one that was used by " \
77            "libcss.\n" \
78     "The default method is: key.")
79
80 static char *cssmethod_list[] = { "title", "disc", "key", NULL };
81
82 vlc_module_begin();
83     int i;
84     add_category_hint( N_("[dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"), NULL );
85     add_string_from_list( "dvd-css-method", NULL, cssmethod_list, NULL,
86                           CSSMETHOD_TEXT, CSSMETHOD_LONGTEXT );
87 #ifdef GOD_DAMN_DMCA
88     set_description( _("DVD input module, uses libdvdcss if installed") );
89     i = 90;
90 #else
91     set_description( _("DVD input module, uses libdvdcss") );
92     i = 100;
93 #endif
94     add_shortcut( "dvdold" );
95     add_submodule();
96         set_capability( "access", i );
97         set_callbacks( E_(DVDOpen), E_(DVDClose) );
98     add_submodule();
99         set_capability( "demux", 0 );
100         set_callbacks( E_(DVDInit), NULL );
101 #ifdef GOD_DAMN_DMCA
102     ProbeLibDVDCSS();
103 #endif
104 vlc_module_end();
105
106 #if 0 /* FIXME */
107     UnprobeLibDVDCSS();
108 #endif
109
110 /* Following functions are local */
111
112 #ifdef GOD_DAMN_DMCA
113 /*****************************************************************************
114  * ProbeLibDVDCSS: look for a libdvdcss object.
115  *****************************************************************************
116  * This functions looks for libdvdcss, using dlopen(), and fills function
117  * pointers with what it finds. On failure, uses the dummy libdvdcss
118  * replacement provided by vlc.
119  *****************************************************************************/
120 static void ProbeLibDVDCSS( void )
121 {
122     static char *pp_filelist[] = { "libdvdcss.so.2",
123                                    "./libdvdcss.so.2",
124                                    "./lib/libdvdcss.so.2",
125                                    "libdvdcss.so.1",
126                                    "./libdvdcss.so.1",
127                                    "./lib/libdvdcss.so.1",
128                                    NULL };
129     char **pp_file = pp_filelist;
130
131     /* Try to open the dynamic object */
132     do
133     {
134         p_libdvdcss = dlopen( *pp_file, RTLD_LAZY );
135         if( p_libdvdcss != NULL )
136         {
137 //X            intf_WarnMsg( 2, "module: builtin module `dvd' found libdvdcss "
138 //X                             "in `%s'", *pp_file );
139             break;
140         }
141         pp_file++;
142
143     } while( *pp_file != NULL );
144
145     /* If libdvdcss.so was found, check that it's valid */
146     if( p_libdvdcss == NULL )
147     {
148 //X        intf_ErrMsg( "dvd warning: libdvdcss.so.2 not present" );
149     }
150     else
151     {
152         ____dvdcss_open = dlsym( p_libdvdcss, "dvdcss_open" );
153         ____dvdcss_close = dlsym( p_libdvdcss, "dvdcss_close" );
154         ____dvdcss_title = dlsym( p_libdvdcss, "dvdcss_title" );
155         ____dvdcss_seek = dlsym( p_libdvdcss, "dvdcss_seek" );
156         ____dvdcss_read = dlsym( p_libdvdcss, "dvdcss_read" );
157         ____dvdcss_readv = dlsym( p_libdvdcss, "dvdcss_readv" );
158         ____dvdcss_error = dlsym( p_libdvdcss, "dvdcss_error" );
159
160         if( ____dvdcss_open == NULL || ____dvdcss_close == NULL
161              || ____dvdcss_title == NULL || ____dvdcss_seek == NULL
162              || ____dvdcss_read == NULL || ____dvdcss_readv == NULL
163              || ____dvdcss_error == NULL )
164         {
165 //X            intf_ErrMsg( "dvd warning: missing symbols in libdvdcss.so.2, "
166 //X                         "this shouldn't happen !" );
167             dlclose( p_libdvdcss );
168             p_libdvdcss = NULL;
169         }
170     }
171
172     /* If libdvdcss was not found or was not valid, use the dummy
173      * replacement functions. */
174     if( p_libdvdcss == NULL )
175     {
176 //X        intf_ErrMsg( "dvd warning: no valid libdvdcss found, "
177 //X                     "I will only play unencrypted DVDs" );
178 //X        intf_ErrMsg( "dvd warning: get libdvdcss at "
179 //X                     "http://www.videolan.org/libdvdcss/" );
180
181         ____dvdcss_open = dummy_dvdcss_open;
182         ____dvdcss_close = dummy_dvdcss_close;
183         ____dvdcss_title = dummy_dvdcss_title;
184         ____dvdcss_seek = dummy_dvdcss_seek;
185         ____dvdcss_read = dummy_dvdcss_read;
186         ____dvdcss_readv = dummy_dvdcss_readv;
187         ____dvdcss_error = dummy_dvdcss_error;
188     }
189 }
190
191 /*****************************************************************************
192  * UnprobeLibDVDCSS: free resources allocated by ProbeLibDVDCSS, if any.
193  *****************************************************************************/
194 static void UnprobeLibDVDCSS( void )
195 {
196     if( p_libdvdcss != NULL )
197     {
198         dlclose( p_libdvdcss );
199         p_libdvdcss = NULL;
200     }
201 }
202
203 /* Dummy libdvdcss with minimal DVD access. */
204
205 /*****************************************************************************
206  * Local structure
207  *****************************************************************************/
208 struct dvdcss_s
209 {
210     /* File descriptor */
211     int i_fd;
212 };
213
214 /*****************************************************************************
215  * dvdcss_open: initialize library, open a DVD device, crack CSS key
216  *****************************************************************************/
217 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
218 {
219     dvdcss_handle dvdcss;
220     dvd_struct    dvd;
221
222     /* Allocate the library structure */
223     dvdcss = malloc( sizeof( struct dvdcss_s ) );
224     if( dvdcss == NULL )
225     {
226         fprintf( stderr, "dvd error: "
227                          "dummy libdvdcss could not allocate memory\n" );
228         return NULL;
229     }
230
231     /* Open the device */
232     dvdcss->i_fd = open( psz_target, 0 );
233     if( dvdcss->i_fd < 0 )
234     {
235         fprintf( stderr, "dvd error: "
236                          "dummy libdvdcss could not open device\n" );
237         free( dvdcss );
238         return NULL;
239     }
240
241     /* Check for encryption or ioctl failure */
242     dvd.type = DVD_STRUCT_COPYRIGHT;
243     dvd.copyright.layer_num = 0;
244     if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
245          || dvd.copyright.cpst )
246     {
247         fprintf( stderr, "dvd error: "
248                          "dummy libdvdcss could not decrypt disc\n" );
249         close( dvdcss->i_fd );
250         free( dvdcss );
251         return NULL;
252     }
253
254     return dvdcss;
255 }
256
257 /*****************************************************************************
258  * dvdcss_error: return the last libdvdcss error message
259  *****************************************************************************/
260 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
261 {
262     return "generic error";
263 }
264
265 /*****************************************************************************
266  * dvdcss_seek: seek into the device
267  *****************************************************************************/
268 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
269                                                      int i_flags )
270 {
271     off_t i_read;
272
273     i_read = lseek( dvdcss->i_fd,
274                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
275
276     return i_read / DVDCSS_BLOCK_SIZE;
277 }
278
279 /*****************************************************************************
280  * dvdcss_title: crack the current title key if needed
281  *****************************************************************************/
282 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
283 {
284     return 0;
285 }
286
287 /*****************************************************************************
288  * dvdcss_read: read data from the device, decrypt if requested
289  *****************************************************************************/
290 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
291                                                      int i_blocks,
292                                                      int i_flags )
293 {
294     int i_bytes;
295
296     i_bytes = read( dvdcss->i_fd, p_buffer,
297                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
298
299     return i_bytes / DVDCSS_BLOCK_SIZE;
300 }
301
302 /*****************************************************************************
303  * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
304  *****************************************************************************/
305 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
306                                                       int i_blocks,
307                                                       int i_flags )
308 {
309     int i_read;
310
311     i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
312
313     return i_read / DVDCSS_BLOCK_SIZE;
314 }
315
316 /*****************************************************************************
317  * dvdcss_close: close the DVD device and clean up the library
318  *****************************************************************************/
319 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
320 {
321     int i_ret;
322
323     i_ret = close( dvdcss->i_fd );
324
325     if( i_ret < 0 )
326     {
327         return i_ret;
328     }
329
330     free( dvdcss );
331
332     return 0;
333 }
334
335 #endif
336