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