Mercurial > libdvdread4.hg
annotate dvd_input.c @ 81:7e9feef7a82d src
Do not extract libdvdcss version via dvdcss_interface_2.
The variable is deprecated and only informs about the
libdvdcss API version, which carries no meaning nowadays.
| author | diego |
|---|---|
| date | Thu, 21 Mar 2013 19:23:12 +0000 |
| parents | d59aaee50e47 |
| children | aa4a7b151801 |
| rev | line source |
|---|---|
| 3 | 1 /* |
| 2 * Copyright (C) 2002 Samuel Hocevar <sam@zoy.org>, | |
| 22 | 3 * HÃ¥kan Hjort <d95hjort@dtek.chalmers.se> |
| 3 | 4 * |
|
21
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
5 * This file is part of libdvdread. |
|
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
6 * |
|
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
7 * libdvdread is free software; you can redistribute it and/or modify |
| 3 | 8 * it under the terms of the GNU General Public License as published by |
| 9 * the Free Software Foundation; either version 2 of the License, or | |
| 10 * (at your option) any later version. | |
| 20 | 11 * |
|
21
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
12 * libdvdread is distributed in the hope that it will be useful, |
| 3 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 * GNU General Public License for more details. | |
| 16 * | |
|
21
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
17 * You should have received a copy of the GNU General Public License along |
|
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
18 * with libdvdread; if not, write to the Free Software Foundation, Inc., |
|
4aa618ae094f
Use consistent license headers everywhere: Fix FSF address and boilerplate.
diego
parents:
20
diff
changeset
|
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 3 | 20 */ |
| 21 | |
| 22 #include <stdio.h> | |
| 23 #include <stdlib.h> | |
| 24 #include <fcntl.h> | |
| 25 #include <unistd.h> | |
| 26 | |
|
39
caef08851d58
Add #include for config.h. Several preprocessor definitions were used without
diego
parents:
38
diff
changeset
|
27 #include "config.h" |
|
33
c743d79f187b
Move installed headers into dvdread directory to make them easier to
reimar
parents:
30
diff
changeset
|
28 #include "dvdread/dvd_reader.h" |
| 3 | 29 #include "dvd_input.h" |
| 30 | |
| 31 | |
| 32 /* The function pointers that is the exported interface of this file. */ | |
| 33 dvd_input_t (*dvdinput_open) (const char *); | |
| 34 int (*dvdinput_close) (dvd_input_t); | |
| 35 int (*dvdinput_seek) (dvd_input_t, int); | |
| 20 | 36 int (*dvdinput_title) (dvd_input_t, int); |
| 3 | 37 int (*dvdinput_read) (dvd_input_t, void *, int, int); |
| 38 char * (*dvdinput_error) (dvd_input_t); | |
| 39 | |
| 40 #ifdef HAVE_DVDCSS_DVDCSS_H | |
| 41 /* linking to libdvdcss */ | |
| 42 #include <dvdcss/dvdcss.h> | |
| 43 #define DVDcss_open(a) dvdcss_open((char*)(a)) | |
| 44 #define DVDcss_close dvdcss_close | |
| 45 #define DVDcss_seek dvdcss_seek | |
| 46 #define DVDcss_read dvdcss_read | |
| 47 #define DVDcss_error dvdcss_error | |
| 48 #else | |
| 49 | |
| 50 /* dlopening libdvdcss */ | |
| 51 #ifdef HAVE_DLFCN_H | |
| 52 #include <dlfcn.h> | |
| 53 #else | |
| 54 /* Only needed on MINGW at the moment */ | |
| 55 #include "../../msvc/contrib/dlfcn.c" | |
| 56 #endif | |
| 57 | |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
58 typedef struct dvdcss_s *dvdcss_t; |
|
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
59 static dvdcss_t (*DVDcss_open) (const char *); |
|
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
60 static int (*DVDcss_close) (dvdcss_t); |
|
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
61 static int (*DVDcss_seek) (dvdcss_t, int, int); |
|
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
62 static int (*DVDcss_read) (dvdcss_t, void *, int, int); |
|
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
63 static char * (*DVDcss_error) (dvdcss_t); |
| 3 | 64 #endif |
| 65 | |
| 66 /* The DVDinput handle, add stuff here for new input methods. */ | |
| 67 struct dvd_input_s { | |
| 68 /* libdvdcss handle */ | |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
69 dvdcss_t dvdcss; |
| 20 | 70 |
| 3 | 71 /* dummy file input */ |
| 72 int fd; | |
| 73 }; | |
| 74 | |
| 75 | |
| 76 /** | |
| 77 * initialize and open a DVD device or file. | |
| 78 */ | |
| 79 static dvd_input_t css_open(const char *target) | |
| 80 { | |
| 81 dvd_input_t dev; | |
| 20 | 82 |
| 3 | 83 /* Allocate the handle structure */ |
| 84 dev = (dvd_input_t) malloc(sizeof(*dev)); | |
| 85 if(dev == NULL) { | |
| 86 fprintf(stderr, "libdvdread: Could not allocate memory.\n"); | |
| 87 return NULL; | |
| 88 } | |
| 20 | 89 |
| 3 | 90 /* Really open it with libdvdcss */ |
| 91 dev->dvdcss = DVDcss_open(target); | |
| 92 if(dev->dvdcss == 0) { | |
| 93 fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target); | |
| 94 free(dev); | |
| 95 return NULL; | |
| 96 } | |
| 20 | 97 |
| 3 | 98 return dev; |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * return the last error message | |
| 103 */ | |
| 104 static char *css_error(dvd_input_t dev) | |
| 105 { | |
| 106 return DVDcss_error(dev->dvdcss); | |
| 107 } | |
| 108 | |
| 109 /** | |
| 110 * seek into the device. | |
| 111 */ | |
| 112 static int css_seek(dvd_input_t dev, int blocks) | |
| 113 { | |
| 114 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */ | |
| 115 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS); | |
| 116 } | |
| 117 | |
| 118 /** | |
| 18 | 119 * set the block for the beginning of a new title (key). |
| 3 | 120 */ |
| 121 static int css_title(dvd_input_t dev, int block) | |
| 122 { | |
|
80
d59aaee50e47
Replace deprecated dvdcss_title() function by dvdcss_seek().
diego
parents:
79
diff
changeset
|
123 return DVDcss_seek(dev->dvdcss, block, DVDCSS_SEEK_KEY); |
| 3 | 124 } |
| 125 | |
| 126 /** | |
| 127 * read data from the device. | |
| 128 */ | |
| 129 static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags) | |
| 130 { | |
| 131 return DVDcss_read(dev->dvdcss, buffer, blocks, flags); | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * close the DVD device and clean up the library. | |
| 136 */ | |
| 137 static int css_close(dvd_input_t dev) | |
| 138 { | |
| 139 int ret; | |
| 140 | |
| 141 ret = DVDcss_close(dev->dvdcss); | |
| 142 | |
| 143 if(ret < 0) | |
| 144 return ret; | |
| 145 | |
| 146 free(dev); | |
| 147 | |
| 148 return 0; | |
| 149 } | |
| 150 | |
| 151 /** | |
| 152 * initialize and open a DVD device or file. | |
| 153 */ | |
| 154 static dvd_input_t file_open(const char *target) | |
| 155 { | |
| 156 dvd_input_t dev; | |
| 20 | 157 |
| 3 | 158 /* Allocate the library structure */ |
| 159 dev = (dvd_input_t) malloc(sizeof(*dev)); | |
| 160 if(dev == NULL) { | |
| 161 fprintf(stderr, "libdvdread: Could not allocate memory.\n"); | |
| 162 return NULL; | |
| 163 } | |
| 20 | 164 |
| 3 | 165 /* Open the device */ |
|
40
ce7056d60f01
in OS/2 the device must be opened in binary mode; patch by KO Myung-Hun - komh chollian net
nicodvb
parents:
39
diff
changeset
|
166 #if !defined(WIN32) && !defined(__OS2__) |
| 3 | 167 dev->fd = open(target, O_RDONLY); |
| 168 #else | |
| 169 dev->fd = open(target, O_RDONLY | O_BINARY); | |
| 170 #endif | |
| 171 if(dev->fd < 0) { | |
| 172 perror("libdvdread: Could not open input"); | |
| 173 free(dev); | |
| 174 return NULL; | |
| 175 } | |
| 20 | 176 |
| 3 | 177 return dev; |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * return the last error message | |
| 182 */ | |
| 183 static char *file_error(dvd_input_t dev) | |
| 184 { | |
| 185 /* use strerror(errno)? */ | |
| 186 return (char *)"unknown error"; | |
| 187 } | |
| 188 | |
| 189 /** | |
| 190 * seek into the device. | |
| 191 */ | |
| 192 static int file_seek(dvd_input_t dev, int blocks) | |
| 193 { | |
| 194 off_t pos; | |
| 195 | |
| 196 pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET); | |
| 197 if(pos < 0) { | |
|
27
98951f8ec89c
cosmetics: Sync indentation and similar changes from libdvdread 0.9.5.
diego
parents:
26
diff
changeset
|
198 return pos; |
| 3 | 199 } |
| 200 /* assert pos % DVD_VIDEO_LB_LEN == 0 */ | |
| 201 return (int) (pos / DVD_VIDEO_LB_LEN); | |
| 202 } | |
| 203 | |
| 204 /** | |
| 18 | 205 * set the block for the beginning of a new title (key). |
| 3 | 206 */ |
| 207 static int file_title(dvd_input_t dev, int block) | |
| 208 { | |
| 209 return -1; | |
| 210 } | |
| 211 | |
| 212 /** | |
| 213 * read data from the device. | |
| 214 */ | |
| 215 static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags) | |
| 216 { | |
| 217 size_t len; | |
| 218 ssize_t ret; | |
| 20 | 219 |
| 3 | 220 len = (size_t)blocks * DVD_VIDEO_LB_LEN; |
| 20 | 221 |
| 3 | 222 while(len > 0) { |
| 20 | 223 |
| 3 | 224 ret = read(dev->fd, buffer, len); |
| 20 | 225 |
| 3 | 226 if(ret < 0) { |
| 227 /* One of the reads failed, too bad. We won't even bother | |
| 18 | 228 * returning the reads that went OK, and as in the POSIX spec |
| 229 * the file position is left unspecified after a failure. */ | |
| 3 | 230 return ret; |
| 231 } | |
| 20 | 232 |
| 3 | 233 if(ret == 0) { |
| 18 | 234 /* Nothing more to read. Return all of the whole blocks, if any. |
| 235 * Adjust the file position back to the previous block boundary. */ | |
| 3 | 236 size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len; |
| 237 off_t over_read = -(bytes % DVD_VIDEO_LB_LEN); | |
| 238 /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR); | |
| 239 /* should have pos % 2048 == 0 */ | |
| 240 return (int) (bytes / DVD_VIDEO_LB_LEN); | |
| 241 } | |
| 20 | 242 |
| 3 | 243 len -= ret; |
| 244 } | |
| 245 | |
| 246 return blocks; | |
| 247 } | |
| 248 | |
| 249 /** | |
| 250 * close the DVD device and clean up. | |
| 251 */ | |
| 252 static int file_close(dvd_input_t dev) | |
| 253 { | |
| 254 int ret; | |
| 255 | |
| 256 ret = close(dev->fd); | |
| 257 | |
| 258 if(ret < 0) | |
| 259 return ret; | |
| 260 | |
| 261 free(dev); | |
| 262 | |
| 263 return 0; | |
| 264 } | |
| 265 | |
| 266 | |
| 267 /** | |
| 268 * Setup read functions with either libdvdcss or minimal DVD access. | |
| 269 */ | |
| 270 int dvdinput_setup(void) | |
| 271 { | |
| 272 void *dvdcss_library = NULL; | |
| 273 | |
| 274 #ifdef HAVE_DVDCSS_DVDCSS_H | |
| 275 /* linking to libdvdcss */ | |
| 276 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */ | |
| 277 | |
| 278 #else | |
| 279 /* dlopening libdvdcss */ | |
| 280 | |
| 281 #ifdef __APPLE__ | |
| 282 #define CSS_LIB "libdvdcss.2.dylib" | |
| 283 #elif defined(WIN32) | |
| 284 #define CSS_LIB "libdvdcss.dll" | |
| 30 | 285 #elif defined(__OS2__) |
| 286 #define CSS_LIB "dvdcss.dll" | |
| 3 | 287 #else |
| 288 #define CSS_LIB "libdvdcss.so.2" | |
| 289 #endif | |
| 290 dvdcss_library = dlopen(CSS_LIB, RTLD_LAZY); | |
| 291 | |
| 292 if(dvdcss_library != NULL) { | |
| 30 | 293 #if defined(__OpenBSD__) && !defined(__ELF__) || defined(__OS2__) |
| 3 | 294 #define U_S "_" |
| 295 #else | |
| 296 #define U_S | |
| 297 #endif | |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
298 DVDcss_open = (dvdcss_t (*)(const char*)) |
| 3 | 299 dlsym(dvdcss_library, U_S "dvdcss_open"); |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
300 DVDcss_close = (int (*)(dvdcss_t)) |
| 3 | 301 dlsym(dvdcss_library, U_S "dvdcss_close"); |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
302 DVDcss_seek = (int (*)(dvdcss_t, int, int)) |
| 3 | 303 dlsym(dvdcss_library, U_S "dvdcss_seek"); |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
304 DVDcss_read = (int (*)(dvdcss_t, void*, int, int)) |
| 3 | 305 dlsym(dvdcss_library, U_S "dvdcss_read"); |
|
79
dbfcd120bf24
Replace uses of the deprecated dvdcss_handle type by dvdcss_t.
diego
parents:
40
diff
changeset
|
306 DVDcss_error = (char* (*)(dvdcss_t)) |
| 3 | 307 dlsym(dvdcss_library, U_S "dvdcss_error"); |
| 20 | 308 |
| 3 | 309 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) { |
| 20 | 310 fprintf(stderr, |
| 26 | 311 "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n" |
| 312 "libdvdread: You should get the latest version from " | |
| 313 "http://www.videolan.org/\n" ); | |
| 3 | 314 dlclose(dvdcss_library); |
| 315 dvdcss_library = NULL; | |
|
80
d59aaee50e47
Replace deprecated dvdcss_title() function by dvdcss_seek().
diego
parents:
79
diff
changeset
|
316 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_seek |
|
81
7e9feef7a82d
Do not extract libdvdcss version via dvdcss_interface_2.
diego
parents:
80
diff
changeset
|
317 || !DVDcss_read || !DVDcss_error) { |
| 3 | 318 fprintf(stderr, "libdvdread: Missing symbols in %s, " |
| 26 | 319 "this shouldn't happen !\n", CSS_LIB); |
| 3 | 320 dlclose(dvdcss_library); |
| 321 } | |
| 322 } | |
| 323 #endif /* HAVE_DVDCSS_DVDCSS_H */ | |
| 20 | 324 |
| 3 | 325 if(dvdcss_library != NULL) { |
| 326 /* | |
| 327 char *psz_method = getenv( "DVDCSS_METHOD" ); | |
| 328 char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); | |
| 329 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method); | |
| 330 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose); | |
| 331 */ | |
| 20 | 332 |
| 3 | 333 /* libdvdcss wrapper functions */ |
| 334 dvdinput_open = css_open; | |
| 335 dvdinput_close = css_close; | |
| 336 dvdinput_seek = css_seek; | |
| 337 dvdinput_title = css_title; | |
| 338 dvdinput_read = css_read; | |
| 339 dvdinput_error = css_error; | |
| 340 return 1; | |
| 20 | 341 |
| 3 | 342 } else { |
| 343 fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n"); | |
| 344 | |
| 345 /* libdvdcss replacement functions */ | |
| 346 dvdinput_open = file_open; | |
| 347 dvdinput_close = file_close; | |
| 348 dvdinput_seek = file_seek; | |
| 349 dvdinput_title = file_title; | |
| 350 dvdinput_read = file_read; | |
| 351 dvdinput_error = file_error; | |
| 352 return 0; | |
| 353 } | |
| 354 } |
