comparison libmpdvdkit2/dvd_input.c @ 15874:483e955893b8

update libdvdread to v0.9.4
author aurel
date Thu, 30 Jun 2005 22:48:26 +0000
parents 25df9508f9a8
children f0f54034c7e9
comparison
equal deleted inserted replaced
15873:276da0bb6207 15874:483e955893b8
19 * You should have received a copy of the GNU General Public License 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 20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 */ 22 */
23 23
24 #include "config.h"
25
24 #include <stdio.h> 26 #include <stdio.h>
25 #include <stdlib.h> 27 #include <stdlib.h>
26 #include <fcntl.h> 28 #include <fcntl.h>
27 #include <unistd.h> 29 #include <unistd.h>
28 30
29 #include "dvd_reader.h" 31 #include "dvd_reader.h"
30 #include "dvd_input.h" 32 #include "dvd_input.h"
31 33
34 /* The function pointers that is the exported interface of this file. */
35 dvd_input_t (*dvdinput_open) (const char *);
36 int (*dvdinput_close) (dvd_input_t);
37 int (*dvdinput_seek) (dvd_input_t, int);
38 int (*dvdinput_title) (dvd_input_t, int);
39 int (*dvdinput_read) (dvd_input_t, void *, int, int);
40 char * (*dvdinput_error) (dvd_input_t);
41
42 #ifdef HAVE_DVDCSS_DVDCSS_H
43 /* linking to libdvdcss */
32 #include "dvdcss.h" 44 #include "dvdcss.h"
33 45 #define DVDcss_open(a) dvdcss_open((char*)(a))
34 dvdcss_handle (*DVDcss_open) (const char *); 46 #define DVDcss_close dvdcss_close
35 int (*DVDcss_close) (dvdcss_handle); 47 #define DVDcss_seek dvdcss_seek
36 int (*DVDcss_seek) (dvdcss_handle, int, int); 48 #define DVDcss_title dvdcss_title
37 int (*DVDcss_title) (dvdcss_handle, int); 49 #define DVDcss_read dvdcss_read
38 int (*DVDcss_read) (dvdcss_handle, void *, int, int); 50 #define DVDcss_error dvdcss_error
39 char * (*DVDcss_error) (dvdcss_handle); 51 #else
40 52 /* dlopening libdvdcss */
41 dvd_input_t (*DVDinput_open) (const char *); 53 #include <dlfcn.h>
42 int (*DVDinput_close) (dvd_input_t); 54 typedef struct dvdcss_s *dvdcss_handle;
43 int (*DVDinput_seek) (dvd_input_t, int, int); 55 static dvdcss_handle (*DVDcss_open) (const char *);
44 int (*DVDinput_title) (dvd_input_t, int); 56 static int (*DVDcss_close) (dvdcss_handle);
45 int (*DVDinput_read) (dvd_input_t, void *, int, int); 57 static int (*DVDcss_seek) (dvdcss_handle, int, int);
46 char * (*DVDinput_error) (dvd_input_t); 58 static int (*DVDcss_title) (dvdcss_handle, int);
59 static int (*DVDcss_read) (dvdcss_handle, void *, int, int);
60 static char * (*DVDcss_error) (dvdcss_handle);
61 #endif
47 62
48 /* The DVDinput handle, add stuff here for new input methods. */ 63 /* The DVDinput handle, add stuff here for new input methods. */
49 struct dvd_input_s { 64 struct dvd_input_s {
50 /* libdvdcss handle */ 65 /* libdvdcss handle */
51 dvdcss_handle dvdcss; 66 dvdcss_handle dvdcss;
59 * initialize and open a DVD device or file. 74 * initialize and open a DVD device or file.
60 */ 75 */
61 static dvd_input_t css_open(const char *target) 76 static dvd_input_t css_open(const char *target)
62 { 77 {
63 dvd_input_t dev; 78 dvd_input_t dev;
64 79
65 /* Allocate the handle structure */ 80 /* Allocate the handle structure */
66 dev = (dvd_input_t) malloc(sizeof(struct dvd_input_s)); 81 dev = (dvd_input_t) malloc(sizeof(struct dvd_input_s));
67 if(dev == NULL) { 82 if(dev == NULL) {
68 fprintf(stderr, "libdvdread: Could not allocate memory.\n"); 83 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
69 return NULL; 84 return NULL;
70 } 85 }
71 86
72 /* Really open it with libdvdcss */ 87 /* Really open it with libdvdcss */
73 dev->dvdcss = DVDcss_open(target); 88 dev->dvdcss = DVDcss_open(target);
74 if(dev->dvdcss == 0) { 89 if(dev->dvdcss == 0) {
75 fprintf(stderr, "libdvdread: Could not open device with libdvdcss.\n"); 90 fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target);
76 free(dev); 91 free(dev);
77 return NULL; 92 return NULL;
78 } 93 }
79 94
80 return dev; 95 return dev;
89 } 104 }
90 105
91 /** 106 /**
92 * seek into the device. 107 * seek into the device.
93 */ 108 */
94 static int css_seek(dvd_input_t dev, int blocks, int flags) 109 static int css_seek(dvd_input_t dev, int blocks)
95 { 110 {
96 return DVDcss_seek(dev->dvdcss, blocks, flags); 111 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */
112 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS);
97 } 113 }
98 114
99 /** 115 /**
100 * set the block for the begining of a new title (key). 116 * set the block for the begining of a new title (key).
101 */ 117 */
129 return 0; 145 return 0;
130 } 146 }
131 147
132 148
133 149
150
151
152
153 /**
154 * initialize and open a DVD device or file.
155 */
156 static dvd_input_t file_open(const char *target)
157 {
158 dvd_input_t dev;
159
160 /* Allocate the library structure */
161 dev = (dvd_input_t) malloc(sizeof(dvd_input_t));
162 if(dev == NULL) {
163 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
164 return NULL;
165 }
166
167 /* Open the device */
168 dev->fd = open(target, O_RDONLY);
169 if(dev->fd < 0) {
170 perror("libdvdread: Could not open input");
171 free(dev);
172 return NULL;
173 }
174
175 return dev;
176 }
177
178 /**
179 * return the last error message
180 */
181 static char *file_error(dvd_input_t dev)
182 {
183 /* use strerror(errno)? */
184 return (char *)"unknown error";
185 }
186
187 /**
188 * seek into the device.
189 */
190 static int file_seek(dvd_input_t dev, int blocks)
191 {
192 off_t pos;
193
194 pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET);
195 if(pos < 0) {
196 return pos;
197 }
198 /* assert pos % DVD_VIDEO_LB_LEN == 0 */
199 return (int) (pos / DVD_VIDEO_LB_LEN);
200 }
201
202 /**
203 * set the block for the begining of a new title (key).
204 */
205 static int file_title(dvd_input_t dev, int block)
206 {
207 return -1;
208 }
209
210 /**
211 * read data from the device.
212 */
213 static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags)
214 {
215 size_t len;
216 ssize_t ret;
217
218 len = (size_t)blocks * DVD_VIDEO_LB_LEN;
219
220 while(len > 0) {
221
222 ret = read(dev->fd, buffer, len);
223
224 if(ret < 0) {
225 /* One of the reads failed, too bad. We won't even bother
226 * returning the reads that went ok, and as in the posix spec
227 * the file postition is left unspecified after a failure. */
228 return ret;
229 }
230
231 if(ret == 0) {
232 /* Nothing more to read. Return the whole blocks, if any, that we got.
233 and adjust the file possition back to the previous block boundary. */
234 size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len;
235 off_t over_read = -(bytes % DVD_VIDEO_LB_LEN);
236 /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR);
237 /* should have pos % 2048 == 0 */
238 return (int) (bytes / DVD_VIDEO_LB_LEN);
239 }
240
241 len -= ret;
242 }
243
244 return blocks;
245 }
246
247 /**
248 * close the DVD device and clean up.
249 */
250 static int file_close(dvd_input_t dev)
251 {
252 int ret;
253
254 ret = close(dev->fd);
255
256 if(ret < 0)
257 return ret;
258
259 free(dev);
260
261 return 0;
262 }
263
264
134 /** 265 /**
135 * Setup read functions with either libdvdcss or minimal DVD access. 266 * Setup read functions with either libdvdcss or minimal DVD access.
136 */ 267 */
137 int DVDInputSetup(void) 268 int dvdinput_setup(void)
138 { 269 {
139 DVDcss_open = dvdcss_open; 270 void *dvdcss_library = NULL;
140 DVDcss_close = dvdcss_close; 271 char **dvdcss_version = NULL;
141 DVDcss_title = dvdcss_title; 272
142 DVDcss_seek = dvdcss_seek; 273 #ifdef HAVE_DVDCSS_DVDCSS_H
143 DVDcss_read = dvdcss_read; 274 /* linking to libdvdcss */
144 DVDcss_error = dvdcss_error; 275 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */
145 276 /* the DVDcss_* functions have been #defined at the top */
277 dvdcss_version = &dvdcss_interface_2;
278
279 #else
280 /* dlopening libdvdcss */
281 dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY);
282
283 if(dvdcss_library != NULL) {
284 #if defined(__OpenBSD__) && !defined(__ELF__)
285 #define U_S "_"
286 #else
287 #define U_S
288 #endif
289 DVDcss_open = (dvdcss_handle (*)(const char*))
290 dlsym(dvdcss_library, U_S "dvdcss_open");
291 DVDcss_close = (int (*)(dvdcss_handle))
292 dlsym(dvdcss_library, U_S "dvdcss_close");
293 DVDcss_title = (int (*)(dvdcss_handle, int))
294 dlsym(dvdcss_library, U_S "dvdcss_title");
295 DVDcss_seek = (int (*)(dvdcss_handle, int, int))
296 dlsym(dvdcss_library, U_S "dvdcss_seek");
297 DVDcss_read = (int (*)(dvdcss_handle, void*, int, int))
298 dlsym(dvdcss_library, U_S "dvdcss_read");
299 DVDcss_error = (char* (*)(dvdcss_handle))
300 dlsym(dvdcss_library, U_S "dvdcss_error");
301
302 dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2");
303
304 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
305 fprintf(stderr,
306 "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n"
307 "libdvdread: You should get the latest version from "
308 "http://www.videolan.org/\n" );
309 dlclose(dvdcss_library);
310 dvdcss_library = NULL;
311 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_title || !DVDcss_seek
312 || !DVDcss_read || !DVDcss_error || !dvdcss_version) {
313 fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, "
314 "this shouldn't happen !\n");
315 dlclose(dvdcss_library);
316 }
317 }
318 #endif /* HAVE_DVDCSS_DVDCSS_H */
319
320 if(dvdcss_library != NULL) {
146 /* 321 /*
147 char *psz_method = getenv( "DVDCSS_METHOD" ); 322 char *psz_method = getenv( "DVDCSS_METHOD" );
148 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); 323 char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
149 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method); 324 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method);
150 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose); 325 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose);
151 */ 326 */
152 // fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n", 327 /*
153 // *dvdcss_version); 328 fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n",
154 329 *dvdcss_version);
155 /* libdvdcss wraper functions */ 330 */
156 DVDinput_open = css_open; 331
157 DVDinput_close = css_close; 332 /* libdvdcss wrapper functions */
158 DVDinput_seek = css_seek; 333 dvdinput_open = css_open;
159 DVDinput_title = css_title; 334 dvdinput_close = css_close;
160 DVDinput_read = css_read; 335 dvdinput_seek = css_seek;
161 DVDinput_error = css_error; 336 dvdinput_title = css_title;
337 dvdinput_read = css_read;
338 dvdinput_error = css_error;
162 return 1; 339 return 1;
163 340
164 } 341 } else {
342 fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n");
343
344 /* libdvdcss replacement functions */
345 dvdinput_open = file_open;
346 dvdinput_close = file_close;
347 dvdinput_seek = file_seek;
348 dvdinput_title = file_title;
349 dvdinput_read = file_read;
350 dvdinput_error = file_error;
351 return 0;
352 }
353 }