1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
diff -uwr libdvdcss-1.2.12/src/device.c xbmc/lib/libdvd/libdvdcss/src/device.c
--- libdvdcss-1.2.12/src/device.c 2012-03-12 00:07:48 +0100
+++ xbmc/lib/libdvd/libdvdcss/src/device.c 2013-02-07 14:42:34 +0100
@@ -23,6 +23,10 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*****************************************************************************/
+/*
+ Modifications for XBMC are all contained within _XBOX (real xbox hardware) or WITH_CACHE
+*/
+
/*****************************************************************************
* Preamble
*****************************************************************************/
@@ -368,11 +372,20 @@
#if defined( WIN32 )
dvdcss->b_file = 1;
+#if defined( _XBOX )
+ // If we've passed over the device string make sure we don't try
+ // to use file based handling (libc) - we want Win2k routines ...
+ if (!stricmp(psz_device, "\\Device\\Cdrom0"))
+ dvdcss->b_file = 0;
+ else
+ dvdcss->b_file = stricmp(psz_device, "D:");
+#else
/* If device is "X:" or "X:\", we are not actually opening a file. */
if (psz_device[0] && psz_device[1] == ':' &&
(!psz_device[2] || (psz_device[2] == '\\' && !psz_device[3])))
dvdcss->b_file = 0;
+#endif // _XBOX
/* Initialize readv temporary buffer */
dvdcss->p_readv_buffer = NULL;
dvdcss->i_readv_buf_size = 0;
@@ -450,11 +463,13 @@
}
else /* ASPI */
{
+#if !defined(_XBOX)
struct w32_aspidev *fd = (struct w32_aspidev *) dvdcss->i_fd;
/* Unload aspi and free w32_aspidev structure */
FreeLibrary( (HMODULE) fd->hASPI );
free( (void*) dvdcss->i_fd );
+#endif // !_XBOX
}
/* Free readv temporary buffer */
@@ -510,9 +525,14 @@
#if defined( WIN32 )
static int win2k_open ( dvdcss_t dvdcss, char const *psz_device )
{
+#ifdef _XBOX
+ char psz_dvd[70];
+ strcpy(psz_dvd, "cdrom0:");
+#else
char psz_dvd[7];
snprintf( psz_dvd, 7, "\\\\.\\%c:", psz_device[0] );
+#endif
/* To work around an M$ bug in IOCTL_DVD_READ_STRUCTURE, we need read
* _and_ write access to the device (so we can make SCSI Pass Through
* Requests). Unfortunately this is only allowed if you have
@@ -521,17 +541,24 @@
* won't send back the right result).
* (See Microsoft Q241374: Read and Write Access Required for SCSI
* Pass Through Requests) */
+
+#ifdef WITH_CACHE
+ DWORD flags = FILE_FLAG_NO_BUFFERING; /* we handle buffering ourself */
+#else
+ DWORD flags = FILE_FLAG_RANDOM_ACCESS;
+#endif //!_XBOX
+
dvdcss->i_fd = (int)
CreateFile( psz_dvd, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
- FILE_FLAG_RANDOM_ACCESS, NULL );
+ flags, NULL );
if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
dvdcss->i_fd = (int)
CreateFile( psz_dvd, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING,
- FILE_FLAG_RANDOM_ACCESS, NULL );
+ flags, NULL );
if( (HANDLE) dvdcss->i_fd == INVALID_HANDLE_VALUE )
{
@@ -727,6 +754,9 @@
static int win2k_seek( dvdcss_t dvdcss, int i_blocks )
{
LARGE_INTEGER li_seek;
+#ifdef WITH_CACHE
+ int iBytesToSkip;
+#endif
#ifndef INVALID_SET_FILE_POINTER
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
@@ -738,6 +768,28 @@
return i_blocks;
}
+#ifdef WITH_CACHE
+
+ // if our buffer contains the position which we want to seek too, we can
+ // just decrease dwCacheBufferSize
+ iBytesToSkip = (i_blocks - dvdcss->i_pos) * DVDCSS_BLOCK_SIZE;
+ if (iBytesToSkip > 0 && iBytesToSkip < dvdcss->buffer_size)
+ {
+ dvdcss->buffer_size -= iBytesToSkip;
+ dvdcss->i_pos = i_blocks;
+ return dvdcss->i_pos;
+ }
+ else if (iBytesToSkip < 0 && (DISC_CACHE_SIZE - dvdcss->buffer_size) >= -iBytesToSkip)
+ {
+ // we want to seek backwards, and we have enough old data in our buffer
+ dvdcss->buffer_size -= iBytesToSkip; // since iBytesToSkip is negative, dwCacheBufferSize will get bigger
+ dvdcss->i_pos = i_blocks;
+ return dvdcss->i_pos;
+ }
+ else dvdcss->buffer_size = 0;
+
+#endif
+
li_seek.QuadPart = (LONGLONG)i_blocks * DVDCSS_BLOCK_SIZE;
li_seek.LowPart = SetFilePointer( (HANDLE) dvdcss->i_fd,
@@ -827,6 +879,66 @@
{
int i_bytes;
+#ifdef WITH_CACHE
+
+ if (dvdcss->buffer_size < i_blocks * DVDCSS_BLOCK_SIZE)
+ {
+ // we don't have enough data in our buffer
+ int iRemaining = i_blocks * DVDCSS_BLOCK_SIZE;
+ int iCopied = 0;
+ // copy data we already have and read again into the cache
+ if (dvdcss->buffer_size > 0) memcpy(p_buffer, dvdcss->buffer + (DISC_CACHE_SIZE - dvdcss->buffer_size), dvdcss->buffer_size);
+ iCopied = dvdcss->buffer_size;
+ iRemaining -= dvdcss->buffer_size;
+ (BYTE*)p_buffer += iCopied;
+ dvdcss->buffer_size = 0;
+
+ // if remaining size is bigger >= DISC_CACHE_SIZE, don't cache it. Just read
+ if (iRemaining >= DISC_CACHE_SIZE)
+ {
+ if (!ReadFile((HANDLE)dvdcss->i_fd, p_buffer, iRemaining, (LPDWORD)&i_bytes, NULL))
+ {
+ dvdcss->i_pos = -1;
+ return -1;
+ }
+ dvdcss->i_pos += (i_bytes + iCopied) / DVDCSS_BLOCK_SIZE;
+ return (i_bytes + iCopied) / DVDCSS_BLOCK_SIZE;
+ }
+ else
+ {
+ // read a chunk into the cache and copy the needed bytes into p_buffer
+ if (!ReadFile((HANDLE)dvdcss->i_fd, dvdcss->buffer, DISC_CACHE_SIZE, &dvdcss->buffer_size, NULL))
+ {
+ // read error, maybe we tried to read to much. Try again but now without cache
+ if (!ReadFile((HANDLE)dvdcss->i_fd, p_buffer, iRemaining, (LPDWORD)&i_bytes, NULL))
+ {
+ dvdcss->i_pos = -1;
+ return -1;
+ }
+ dvdcss->i_pos += (i_bytes + iCopied) / DVDCSS_BLOCK_SIZE;
+ return (i_bytes + iCopied) / DVDCSS_BLOCK_SIZE;
+ }
+ // copy bytes into the buffer
+ memcpy(p_buffer, dvdcss->buffer, iRemaining);
+ dvdcss->buffer_size -= iRemaining;
+ dvdcss->i_pos += (iRemaining + iCopied) / DVDCSS_BLOCK_SIZE;
+ return (iRemaining + iCopied) / DVDCSS_BLOCK_SIZE;
+ }
+ }
+ else
+ {
+ // we have enough data in our cache, just copy it
+ memcpy(p_buffer, dvdcss->buffer + (DISC_CACHE_SIZE - dvdcss->buffer_size), i_blocks * DVDCSS_BLOCK_SIZE);
+ dvdcss->buffer_size -= i_blocks * DVDCSS_BLOCK_SIZE;
+ dvdcss->i_pos += i_blocks;
+ return i_blocks;
+ }
+
+ dvdcss->i_pos = -1;
+ return -1;
+
+#else // WITH_CACHE
+
if( !ReadFile( (HANDLE) dvdcss->i_fd, p_buffer,
i_blocks * DVDCSS_BLOCK_SIZE,
(LPDWORD)&i_bytes, NULL ) )
@@ -837,6 +949,7 @@
dvdcss->i_pos += i_bytes / DVDCSS_BLOCK_SIZE;
return i_bytes / DVDCSS_BLOCK_SIZE;
+#endif // WITH_CACHE
}
static int aspi_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
diff -uwr libdvdcss-1.2.12/src/ioctl.c xbmc/lib/libdvd/libdvdcss/src/ioctl.c
--- libdvdcss-1.2.12/src/ioctl.c 2012-03-12 00:07:48 +0100
+++ xbmc/lib/libdvd/libdvdcss/src/ioctl.c 2013-02-07 14:42:34 +0100
@@ -373,6 +373,37 @@
memcpy( p_key, dvdbs.discKeyStructures, DVD_DISCKEY_SIZE );
+#elif defined( _XBOX )
+ // the next piece of code will read the disc key on the xbox for all drives (samsung included)
+ // but for some reason it takes 15 - 20 seconds longer to load a dvd if mplayer has the dvd key
+ // so we let this part fail and will only use the modified ioctl_ReadTitleKey code.
+ // don't get this delay, and i'm surprised it worked as the ReadTitleKey function didn't work.
+
+ DWORD dwBytesRead;
+ DVD_READ_STRUCTURE st;
+ char buffer[DVD_DISCKEY_SIZE];
+
+ memset( &buffer, 0, sizeof( buffer ) );
+ memset( &st, 0, sizeof( st ) );
+
+ st.BlockByteOffset.QuadPart = 0;
+ st.SessionId = *pi_agid;
+ st.Format = DvdDiskKeyDescriptor;
+
+ i_ret = DeviceIoControl((HANDLE) i_fd, IOCTL_DVD_READ_STRUCTURE, &st,
+ sizeof(st), buffer, DVD_DISCKEY_SIZE, &dwBytesRead, NULL ) ? 0 : -1;
+
+ if (i_ret < 0) // didn't work
+ {
+ printf("Failed to read disc key\n");
+ return i_ret;
+ }
+
+ // copy the returned key into our key buffer
+ int i;
+ for (i = 0; i < DVD_DISCKEY_SIZE; i++)
+ p_key[i] = buffer[i+4];
+
#elif defined( WIN32 )
if( WIN2K ) /* NT/2k/XP */
{
@@ -541,6 +572,25 @@
memcpy( p_key, dvdbs.titleKeyValue, DVD_KEY_SIZE );
+#elif defined( _XBOX ) && 0 //Faulty wrong key returned, original for WIN32 works thou so use it instead
+ DWORD dwBytesRead;
+ DVD_READ_STRUCTURE st;
+ char buffer[2048+4];
+
+ memset( &buffer, 0, sizeof( buffer ) );
+
+ st.BlockByteOffset.QuadPart = (LONGLONG) i_pos * 2048 /*DVDCSS_BLOCK_SIZE*/;
+ st.SessionId = *pi_agid;
+ st.Format = DvdDiskKeyDescriptor;
+
+ i_ret = DeviceIoControl((HANDLE) i_fd, IOCTL_DVD_READ_STRUCTURE, &st, sizeof(st), buffer, 2048+4, &dwBytesRead, NULL ) ? 0 : -1;
+ if( i_ret < 0 )
+ {
+ return i_ret;
+ }
+
+ memcpy( p_key, &(buffer[4]), 2048);
+
#elif defined( WIN32 )
if( WIN2K ) /* NT/2k/XP */
{
@@ -693,8 +743,13 @@
ULONG id;
DWORD tmp;
+#if defined( _XBOX)
+ i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_START_SESSION,
+ NULL, 0, &id, sizeof(id), &tmp, NULL ) ? 0 : -1;
+#else
i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_START_SESSION,
&tmp, 4, &id, sizeof( id ), &tmp, NULL ) ? 0 : -1;
+#endif
*pi_agid = id;
}
@@ -1336,8 +1391,13 @@
memcpy( key->KeyData, p_challenge, DVD_CHALLENGE_SIZE );
+#if defined(_XBOX)
+ i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
+ key->KeyLength, NULL, 0, &tmp, NULL ) ? 0 : -1;
+#else
i_ret = DeviceIoControl( (HANDLE) i_fd, IOCTL_DVD_SEND_KEY, key,
key->KeyLength, key, key->KeyLength, &tmp, NULL ) ? 0 : -1;
+#endif
}
else
{
diff -uwr libdvdcss-1.2.12/src/ioctl.h xbmc/lib/libdvd/libdvdcss/src/ioctl.h
--- libdvdcss-1.2.12/src/ioctl.h 2012-03-12 00:07:48 +0100
+++ xbmc/lib/libdvd/libdvdcss/src/ioctl.h 2013-02-07 14:42:34 +0100
@@ -324,7 +324,11 @@
typedef DWORD (CALLBACK *GETASPI32SUPPORTINFO)(VOID);
typedef DWORD (CALLBACK *SENDASPI32COMMAND)(LPVOID);
+#if defined(_XBOX)
+#define WIN2K 1
+#else
#define WIN2K ( GetVersion() < 0x80000000 )
+#endif // _XBOX
#define ASPI_HAID 0
#define ASPI_TARGET 0
#define DTYPE_CDROM 0x05
diff -uwr libdvdcss-1.2.12/src/libdvdcss.c xbmc/lib/libdvd/libdvdcss/src/libdvdcss.c
--- libdvdcss-1.2.12/src/libdvdcss.c 2012-03-12 00:07:48 +0100
+++ xbmc/lib/libdvd/libdvdcss/src/libdvdcss.c 2013-02-07 14:42:34 +0100
@@ -195,6 +195,10 @@
dvdcss->b_debug = 0;
dvdcss->b_errors = 0;
+#ifdef WITH_CACHE
+ dvdcss->buffer_size = 0;
+#endif
+
/*
* Find verbosity from DVDCSS_VERBOSE environment variable
*/
@@ -388,7 +392,7 @@
dvdcss->b_scrambled = i_ret;
}
}
-
+ /* if wo don't have b_ioctls, we don't have a disk key, make sure area is nulled */
memset( dvdcss->css.p_disc_key, 0, KEY_SIZE );
/* If disc is CSS protected and the ioctls work, authenticate the drive */
if( dvdcss->b_scrambled && dvdcss->b_ioctls )
@@ -533,8 +537,26 @@
goto nocache;
}
+#ifdef _XBOX
+ //due to xbox file system having a limited length on folders/files,
+ //make separate folder for disk name first
+ if(psz_title[0] == '\0')
+ strcat(psz_title, "NONAME");
+
+ i += sprintf( dvdcss->psz_cachefile + i, "/%s", psz_title);
+
+ i_ret = mkdir( dvdcss->psz_cachefile );
+ if( i_ret < 0 && errno != EEXIST )
+ {
+ print_error( dvdcss, "failed creating cache titledirectory" );
+ dvdcss->psz_cachefile[0] = '\0';
+ goto nocache;
+ }
+ i += sprintf( dvdcss->psz_cachefile + i, "/%s%s", psz_serial, psz_key );
+#else
i += sprintf( dvdcss->psz_cachefile + i, "/%s-%s%s", psz_title,
psz_serial, psz_key );
+#endif
#if !defined( WIN32 ) || defined( SYS_CYGWIN )
i_ret = mkdir( dvdcss->psz_cachefile, 0755 );
#else
|