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