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