]> git.sesse.net Git - vlc/blob - modules/access/dvd/dvd.c
(A few minor pending patches I had around)
[vlc] / modules / access / 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.3 2002/08/26 09:12:46 sam 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 "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 void E_(DVDEnd)    ( vlc_object_t * );
55
56 #ifdef GOD_DAMN_DMCA
57 static void *p_libdvdcss;
58 static void ProbeLibDVDCSS  ( void );
59 static void UnprobeLibDVDCSS( void );
60 #endif
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 #define CSSMETHOD_TEXT N_("method to use by libdvdcss for key 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 " \
79            "libcss.\n" \
80     "The default method is: key.")
81
82 static char *cssmethod_list[] = { "title", "disc", "key", NULL };
83
84 vlc_module_begin();
85     int i;
86     add_category_hint( N_("[dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"), NULL );
87     add_string_from_list( "dvd-css-method", NULL, cssmethod_list, NULL,
88                           CSSMETHOD_TEXT, CSSMETHOD_LONGTEXT );
89 #ifdef GOD_DAMN_DMCA
90     set_description( _("DVD input module, uses libdvdcss if installed") );
91     i = 90;
92 #else
93     set_description( _("DVD input module, uses libdvdcss") );
94     i = 100;
95 #endif
96     add_shortcut( "dvdold" );
97     add_submodule();
98         set_capability( "access", i );
99         set_callbacks( E_(DVDOpen), E_(DVDClose) );
100     add_submodule();
101         set_capability( "demux", 0 );
102         set_callbacks( E_(DVDInit), E_(DVDEnd) );
103 #ifdef GOD_DAMN_DMCA
104     ProbeLibDVDCSS();
105 #endif
106 vlc_module_end();
107
108 #if 0 /* FIXME */
109     UnprobeLibDVDCSS();
110 #endif
111
112 /* Following functions are local */
113
114 #ifdef GOD_DAMN_DMCA
115 /*****************************************************************************
116  * ProbeLibDVDCSS: look for a libdvdcss object.
117  *****************************************************************************
118  * This functions looks for libdvdcss, using dlopen(), and fills function
119  * pointers with what it finds. On failure, uses the dummy libdvdcss
120  * replacement provided by vlc.
121  *****************************************************************************/
122 static void ProbeLibDVDCSS( void )
123 {
124     static char *pp_filelist[] = { "libdvdcss.so.2",
125                                    "./libdvdcss.so.2",
126                                    "./lib/libdvdcss.so.2",
127                                    "libdvdcss.so.1",
128                                    "./libdvdcss.so.1",
129                                    "./lib/libdvdcss.so.1",
130                                    NULL };
131     char **pp_file = pp_filelist;
132
133     /* Try to open the dynamic object */
134     do
135     {
136         p_libdvdcss = dlopen( *pp_file, RTLD_LAZY );
137         if( p_libdvdcss != NULL )
138         {
139 //X            intf_WarnMsg( 2, "module: builtin module `dvd' found libdvdcss "
140 //X                             "in `%s'", *pp_file );
141             break;
142         }
143         pp_file++;
144
145     } while( *pp_file != NULL );
146
147     /* If libdvdcss.so was found, check that it's valid */
148     if( p_libdvdcss == NULL )
149     {
150 //X        intf_ErrMsg( "dvd warning: libdvdcss.so.2 not present" );
151     }
152     else
153     {
154         ____dvdcss_open = dlsym( p_libdvdcss, "dvdcss_open" );
155         ____dvdcss_close = dlsym( p_libdvdcss, "dvdcss_close" );
156         ____dvdcss_title = dlsym( p_libdvdcss, "dvdcss_title" );
157         ____dvdcss_seek = dlsym( p_libdvdcss, "dvdcss_seek" );
158         ____dvdcss_read = dlsym( p_libdvdcss, "dvdcss_read" );
159         ____dvdcss_readv = dlsym( p_libdvdcss, "dvdcss_readv" );
160         ____dvdcss_error = dlsym( p_libdvdcss, "dvdcss_error" );
161
162         if( ____dvdcss_open == NULL || ____dvdcss_close == NULL
163              || ____dvdcss_title == NULL || ____dvdcss_seek == NULL
164              || ____dvdcss_read == NULL || ____dvdcss_readv == NULL
165              || ____dvdcss_error == NULL )
166         {
167 //X            intf_ErrMsg( "dvd warning: missing symbols in libdvdcss.so.2, "
168 //X                         "this shouldn't happen !" );
169             dlclose( p_libdvdcss );
170             p_libdvdcss = NULL;
171         }
172     }
173
174     /* If libdvdcss was not found or was not valid, use the dummy
175      * replacement functions. */
176     if( p_libdvdcss == NULL )
177     {
178 //X        intf_ErrMsg( "dvd warning: no valid libdvdcss found, "
179 //X                     "I will only play unencrypted DVDs" );
180 //X        intf_ErrMsg( "dvd warning: get libdvdcss at "
181 //X                     "http://www.videolan.org/libdvdcss/" );
182
183         ____dvdcss_open = dummy_dvdcss_open;
184         ____dvdcss_close = dummy_dvdcss_close;
185         ____dvdcss_title = dummy_dvdcss_title;
186         ____dvdcss_seek = dummy_dvdcss_seek;
187         ____dvdcss_read = dummy_dvdcss_read;
188         ____dvdcss_readv = dummy_dvdcss_readv;
189         ____dvdcss_error = dummy_dvdcss_error;
190     }
191 }
192
193 /*****************************************************************************
194  * UnprobeLibDVDCSS: free resources allocated by ProbeLibDVDCSS, if any.
195  *****************************************************************************/
196 static void UnprobeLibDVDCSS( void )
197 {
198     if( p_libdvdcss != NULL )
199     {
200         dlclose( p_libdvdcss );
201         p_libdvdcss = NULL;
202     }
203 }
204
205 /* Dummy libdvdcss with minimal DVD access. */
206
207 /*****************************************************************************
208  * Local structure
209  *****************************************************************************/
210 struct dvdcss_s
211 {
212     /* File descriptor */
213     int i_fd;
214 };
215
216 /*****************************************************************************
217  * dvdcss_open: initialize library, open a DVD device, crack CSS key
218  *****************************************************************************/
219 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
220 {
221     dvdcss_handle dvdcss;
222     dvd_struct    dvd;
223
224     /* Allocate the library structure */
225     dvdcss = malloc( sizeof( struct dvdcss_s ) );
226     if( dvdcss == NULL )
227     {
228         fprintf( stderr, "dvd error: "
229                          "dummy libdvdcss could not allocate memory\n" );
230         return NULL;
231     }
232
233     /* Open the device */
234     dvdcss->i_fd = open( psz_target, 0 );
235     if( dvdcss->i_fd < 0 )
236     {
237         fprintf( stderr, "dvd error: "
238                          "dummy libdvdcss could not open device\n" );
239         free( dvdcss );
240         return NULL;
241     }
242
243     /* Check for encryption or ioctl failure */
244     dvd.type = DVD_STRUCT_COPYRIGHT;
245     dvd.copyright.layer_num = 0;
246     if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
247          || dvd.copyright.cpst )
248     {
249         fprintf( stderr, "dvd error: "
250                          "dummy libdvdcss could not decrypt disc\n" );
251         close( dvdcss->i_fd );
252         free( dvdcss );
253         return NULL;
254     }
255
256     return dvdcss;
257 }
258
259 /*****************************************************************************
260  * dvdcss_error: return the last libdvdcss error message
261  *****************************************************************************/
262 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
263 {
264     return "generic error";
265 }
266
267 /*****************************************************************************
268  * dvdcss_seek: seek into the device
269  *****************************************************************************/
270 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
271                                                      int i_flags )
272 {
273     off_t i_read;
274
275     i_read = lseek( dvdcss->i_fd,
276                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
277
278     return i_read / DVDCSS_BLOCK_SIZE;
279 }
280
281 /*****************************************************************************
282  * dvdcss_title: crack the current title key if needed
283  *****************************************************************************/
284 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
285 {
286     return 0;
287 }
288
289 /*****************************************************************************
290  * dvdcss_read: read data from the device, decrypt if requested
291  *****************************************************************************/
292 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
293                                                      int i_blocks,
294                                                      int i_flags )
295 {
296     int i_bytes;
297
298     i_bytes = read( dvdcss->i_fd, p_buffer,
299                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
300
301     return i_bytes / DVDCSS_BLOCK_SIZE;
302 }
303
304 /*****************************************************************************
305  * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
306  *****************************************************************************/
307 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
308                                                       int i_blocks,
309                                                       int i_flags )
310 {
311     int i_read;
312
313     i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
314
315     return i_read / DVDCSS_BLOCK_SIZE;
316 }
317
318 /*****************************************************************************
319  * dvdcss_close: close the DVD device and clean up the library
320  *****************************************************************************/
321 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
322 {
323     int i_ret;
324
325     i_ret = close( dvdcss->i_fd );
326
327     if( i_ret < 0 )
328     {
329         return i_ret;
330     }
331
332     free( dvdcss );
333
334     return 0;
335 }
336
337 #endif
338