]> git.sesse.net Git - vlc/blob - plugins/dvd/dvd.c
* ./src/video_output/video_output.c: fixed a vout4 image starvation bug.
[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.29 2002/04/04 05:08:05 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 <videolan/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  * 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 );
52
53 /*****************************************************************************
54  * Local prototypes.
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  * Build configuration tree.
64  *****************************************************************************/
65 MODULE_CONFIG_START
66 ADD_CATEGORY_HINT( "[dvd:][device][@raw_device][@[title][,[chapter][,angle]]]", NULL )
67 MODULE_CONFIG_STOP
68
69 MODULE_INIT_START
70     ADD_CAPABILITY( DEMUX, 0 )
71 #ifdef GOD_DAMN_DMCA
72     SET_DESCRIPTION( "DVD input module, uses libdvdcss if present" )
73     ADD_CAPABILITY( ACCESS, 90 )
74 #else
75     SET_DESCRIPTION( "DVD input module, uses libdvdcss" )
76     ADD_CAPABILITY( ACCESS, 100 )
77 #endif
78     ADD_SHORTCUT( "dvd" )
79 MODULE_INIT_STOP
80
81 MODULE_ACTIVATE_START
82     _M( access_getfunctions)( &p_module->p_functions->access );
83     _M( demux_getfunctions)( &p_module->p_functions->demux );
84 #ifdef GOD_DAMN_DMCA
85     ProbeLibDVDCSS();
86 #endif
87 MODULE_ACTIVATE_STOP
88
89 MODULE_DEACTIVATE_START
90 #ifdef GOD_DAMN_DMCA
91     UnprobeLibDVDCSS();
92 #endif
93 MODULE_DEACTIVATE_STOP
94
95
96 /* Following functions are local */
97
98 #ifdef GOD_DAMN_DMCA
99 /*****************************************************************************
100  * ProbeLibDVDCSS: look for a libdvdcss object.
101  *****************************************************************************
102  * This functions looks for libdvdcss, using dlopen(), and fills function
103  * pointers with what it finds. On failure, uses the dummy libdvdcss
104  * replacement provided by vlc.
105  *****************************************************************************/
106 static void ProbeLibDVDCSS( void )
107 {
108     static char *pp_filelist[4] = { "libdvdcss.so.2",
109                                     "./libdvdcss.so.2",
110                                     "./lib/libdvdcss.so.2",
111                                     "libdvdcss.so.1",
112                                     "./libdvdcss.so.1",
113                                     "./lib/libdvdcss.so.1",
114                                     NULL };
115     char **pp_file = pp_filelist;
116
117     /* Try to open the dynamic object */
118     do
119     {
120         p_libdvdcss = dlopen( *pp_file, RTLD_LAZY );
121         if( p_libdvdcss != NULL )
122         {
123             intf_WarnMsg( 2, "module: builtin module `dvd' found libdvdcss "
124                              "in `%s'", *pp_file );
125             break;
126         }
127         pp_file++;
128
129     } while( *pp_file != NULL );
130
131     /* If libdvdcss.so was found, check that it's valid */
132     if( p_libdvdcss == NULL )
133     {
134         intf_ErrMsg( "dvd warning: libdvdcss.so.2 not present" );
135     }
136     else
137     {
138         ____dvdcss_open = dlsym( p_libdvdcss, "dvdcss_open" );
139         ____dvdcss_close = dlsym( p_libdvdcss, "dvdcss_close" );
140         ____dvdcss_title = dlsym( p_libdvdcss, "dvdcss_title" );
141         ____dvdcss_seek = dlsym( p_libdvdcss, "dvdcss_seek" );
142         ____dvdcss_read = dlsym( p_libdvdcss, "dvdcss_read" );
143         ____dvdcss_readv = dlsym( p_libdvdcss, "dvdcss_readv" );
144         ____dvdcss_error = dlsym( p_libdvdcss, "dvdcss_error" );
145
146         if( ____dvdcss_open == NULL || ____dvdcss_close == NULL
147              || ____dvdcss_title == NULL || ____dvdcss_seek == NULL
148              || ____dvdcss_read == NULL || ____dvdcss_readv == NULL
149              || ____dvdcss_error == NULL )
150         {
151             intf_ErrMsg( "dvd warning: missing symbols in libdvdcss.so.2, "
152                          "this shouldn't happen !" );
153             dlclose( p_libdvdcss );
154             p_libdvdcss = NULL;
155         }
156     }
157
158     /* If libdvdcss was not found or was not valid, use the dummy
159      * replacement functions. */
160     if( p_libdvdcss == NULL )
161     {
162         intf_ErrMsg( "dvd warning: no valid libdvdcss found, "
163                      "I will only play unencrypted DVDs" );
164         intf_ErrMsg( "dvd warning: get libdvdcss at "
165                      "http://www.videolan.org/libdvdcss/" );
166
167         ____dvdcss_open = dummy_dvdcss_open;
168         ____dvdcss_close = dummy_dvdcss_close;
169         ____dvdcss_title = dummy_dvdcss_title;
170         ____dvdcss_seek = dummy_dvdcss_seek;
171         ____dvdcss_read = dummy_dvdcss_read;
172         ____dvdcss_readv = dummy_dvdcss_readv;
173         ____dvdcss_error = dummy_dvdcss_error;
174     }
175 }
176
177 /*****************************************************************************
178  * UnprobeLibDVDCSS: free resources allocated by ProbeLibDVDCSS, if any.
179  *****************************************************************************/
180 static void UnprobeLibDVDCSS( void )
181 {
182     if( p_libdvdcss != NULL )
183     {
184         dlclose( p_libdvdcss );
185         p_libdvdcss = NULL;
186     }
187 }
188
189 /* Dummy libdvdcss with minimal DVD access. */
190
191 /*****************************************************************************
192  * Local structure
193  *****************************************************************************/
194 struct dvdcss_s
195 {
196     /* File descriptor */
197     int i_fd;
198 };
199
200 /*****************************************************************************
201  * dvdcss_open: initialize library, open a DVD device, crack CSS key
202  *****************************************************************************/
203 extern dvdcss_handle dummy_dvdcss_open ( char *psz_target )
204 {
205     dvdcss_handle dvdcss;
206     dvd_struct    dvd;
207
208     /* Allocate the library structure */
209     dvdcss = malloc( sizeof( struct dvdcss_s ) );
210     if( dvdcss == NULL )
211     {
212         fprintf( stderr, "dvd error: "
213                          "dummy libdvdcss could not allocate memory\n" );
214         return NULL;
215     }
216
217     /* Open the device */
218     dvdcss->i_fd = open( psz_target, 0 );
219     if( dvdcss->i_fd < 0 )
220     {
221         fprintf( stderr, "dvd error: "
222                          "dummy libdvdcss could not open device\n" );
223         free( dvdcss );
224         return NULL;
225     }
226
227     /* Check for encryption or ioctl failure */
228     dvd.type = DVD_STRUCT_COPYRIGHT;
229     dvd.copyright.layer_num = 0;
230     if( ioctl( dvdcss->i_fd, DVD_READ_STRUCT, &dvd ) != 0
231          || dvd.copyright.cpst )
232     {
233         fprintf( stderr, "dvd error: "
234                          "dummy libdvdcss could not decrypt disc\n" );
235         close( dvdcss->i_fd );
236         free( dvdcss );
237         return NULL;
238     }
239
240     return dvdcss;
241 }
242
243 /*****************************************************************************
244  * dvdcss_error: return the last libdvdcss error message
245  *****************************************************************************/
246 extern char * dummy_dvdcss_error ( dvdcss_handle dvdcss )
247 {
248     return "generic error";
249 }
250
251 /*****************************************************************************
252  * dvdcss_seek: seek into the device
253  *****************************************************************************/
254 extern int dummy_dvdcss_seek ( dvdcss_handle dvdcss, int i_blocks,
255                                                      int i_flags )
256 {
257     off_t i_read;
258
259     i_read = lseek( dvdcss->i_fd,
260                     (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE, SEEK_SET );
261
262     return i_read / DVDCSS_BLOCK_SIZE;
263 }
264
265 /*****************************************************************************
266  * dvdcss_title: crack the current title key if needed
267  *****************************************************************************/
268 extern int dummy_dvdcss_title ( dvdcss_handle dvdcss, int i_block )
269 {
270     return 0;
271 }
272
273 /*****************************************************************************
274  * dvdcss_read: read data from the device, decrypt if requested
275  *****************************************************************************/
276 extern int dummy_dvdcss_read ( dvdcss_handle dvdcss, void *p_buffer,
277                                                      int i_blocks,
278                                                      int i_flags )
279 {
280     int i_bytes;
281
282     i_bytes = read( dvdcss->i_fd, p_buffer,
283                     (size_t)i_blocks * DVDCSS_BLOCK_SIZE );
284
285     return i_bytes / DVDCSS_BLOCK_SIZE;
286 }
287
288 /*****************************************************************************
289  * dvdcss_readv: read data to an iovec structure, decrypt if reaquested
290  *****************************************************************************/
291 extern int dummy_dvdcss_readv ( dvdcss_handle dvdcss, void *p_iovec,
292                                                       int i_blocks,
293                                                       int i_flags )
294 {
295     int i_read;
296
297     i_read = readv( dvdcss->i_fd, (struct iovec*)p_iovec, i_blocks );
298
299     return i_read / DVDCSS_BLOCK_SIZE;
300 }
301
302 /*****************************************************************************
303  * dvdcss_close: close the DVD device and clean up the library
304  *****************************************************************************/
305 extern int dummy_dvdcss_close ( dvdcss_handle dvdcss )
306 {
307     int i_ret;
308
309     i_ret = close( dvdcss->i_fd );
310
311     if( i_ret < 0 )
312     {
313         return i_ret;
314     }
315
316     free( dvdcss );
317
318     return 0;
319 }
320
321 #endif
322