[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / quartz / filesource.c
1 /*
2 * File Source Filter
3 *
4 * Copyright 2003 Robert Shearman
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "quartz_private.h"
22
23 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
24
25 typedef struct AsyncReader
26 {
27 BaseFilter filter;
28 IFileSourceFilter IFileSourceFilter_iface;
29 IAMFilterMiscFlags IAMFilterMiscFlags_iface;
30
31 IPin * pOutputPin;
32 LPOLESTR pszFileName;
33 AM_MEDIA_TYPE * pmt;
34 } AsyncReader;
35
36 static inline AsyncReader *impl_from_BaseFilter(BaseFilter *iface)
37 {
38 return CONTAINING_RECORD(iface, AsyncReader, filter);
39 }
40
41 static inline AsyncReader *impl_from_IBaseFilter(IBaseFilter *iface)
42 {
43 return CONTAINING_RECORD(iface, AsyncReader, filter.IBaseFilter_iface);
44 }
45
46 static inline AsyncReader *impl_from_IFileSourceFilter(IFileSourceFilter *iface)
47 {
48 return CONTAINING_RECORD(iface, AsyncReader, IFileSourceFilter_iface);
49 }
50
51 static inline AsyncReader *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
52 {
53 return CONTAINING_RECORD(iface, AsyncReader, IAMFilterMiscFlags_iface);
54 }
55
56 static const IBaseFilterVtbl AsyncReader_Vtbl;
57 static const IFileSourceFilterVtbl FileSource_Vtbl;
58 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
60
61 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
62
63 static const WCHAR mediatype_name[] = {
64 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
65 static const WCHAR subtype_name[] = {
66 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
67 static const WCHAR source_filter_name[] = {
68 'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
69
70 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
71 {
72 WCHAR *extension;
73 LONG l;
74 HKEY hsub;
75 WCHAR keying[39];
76 DWORD size;
77
78 if (!pszFileName)
79 return E_POINTER;
80
81 /* Get the part of the name that matters */
82 extension = PathFindExtensionW(pszFileName);
83 if (*extension != '.')
84 return E_FAIL;
85
86 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
87 if (l)
88 return E_FAIL;
89
90 if (majorType)
91 {
92 size = sizeof(keying);
93 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
94 if (!l)
95 CLSIDFromString(keying, majorType);
96 }
97
98 if (minorType)
99 {
100 size = sizeof(keying);
101 if (!l)
102 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
103 if (!l)
104 CLSIDFromString(keying, minorType);
105 }
106
107 if (sourceFilter)
108 {
109 size = sizeof(keying);
110 if (!l)
111 l = RegQueryValueExW(hsub, source_filter_name, NULL, NULL, (LPBYTE)keying, &size);
112 if (!l)
113 CLSIDFromString(keying, sourceFilter);
114 }
115
116 RegCloseKey(hsub);
117
118 if (!l)
119 return S_OK;
120 return E_FAIL;
121 }
122
123 static unsigned char byte_from_hex_char(WCHAR wHex)
124 {
125 switch (tolowerW(wHex))
126 {
127 case '0':
128 case '1':
129 case '2':
130 case '3':
131 case '4':
132 case '5':
133 case '6':
134 case '7':
135 case '8':
136 case '9':
137 return (wHex - '0') & 0xf;
138 case 'a':
139 case 'b':
140 case 'c':
141 case 'd':
142 case 'e':
143 case 'f':
144 return (wHex - 'a' + 10) & 0xf;
145 default:
146 return 0;
147 }
148 }
149
150 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
151 {
152 ULONG ulOffset;
153 ULONG ulBytes;
154 BYTE * pbMask;
155 BYTE * pbValue;
156 BYTE * pbFile;
157 HRESULT hr = S_OK;
158 ULONG strpos;
159
160 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
161
162 /* format: "offset, bytestocompare, mask, value" */
163
164 ulOffset = strtolW(wszPatternString, NULL, 10);
165
166 if (!(wszPatternString = strchrW(wszPatternString, ',')))
167 return E_INVALIDARG;
168
169 wszPatternString++; /* skip ',' */
170
171 ulBytes = strtolW(wszPatternString, NULL, 10);
172
173 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
174 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
175 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
176
177 /* default mask is match everything */
178 memset(pbMask, 0xFF, ulBytes);
179
180 if (!(wszPatternString = strchrW(wszPatternString, ',')))
181 hr = E_INVALIDARG;
182
183 if (hr == S_OK)
184 {
185 wszPatternString++; /* skip ',' */
186 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
187
188 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
189 {
190 if ((strpos % 2) == 1) /* odd numbered position */
191 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
192 else
193 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
194 }
195
196 if (!(wszPatternString = strchrW(wszPatternString, ',')))
197 hr = E_INVALIDARG;
198 else
199 wszPatternString++; /* skip ',' */
200 }
201
202 if (hr == S_OK)
203 {
204 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
205 ;
206
207 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
208 {
209 if ((strpos % 2) == 1) /* odd numbered position */
210 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
211 else
212 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
213 }
214 }
215
216 if (hr == S_OK)
217 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
218
219 if (hr == S_OK)
220 {
221 ULONG i;
222 for (i = 0; i < ulBytes; i++)
223 if ((pbFile[i] & pbMask[i]) != pbValue[i])
224 {
225 hr = S_FALSE;
226 break;
227 }
228 }
229
230 HeapFree(GetProcessHeap(), 0, pbMask);
231 HeapFree(GetProcessHeap(), 0, pbValue);
232 HeapFree(GetProcessHeap(), 0, pbFile);
233
234 /* if we encountered no errors with this string, and there is a following tuple, then we
235 * have to match that as well to succeed */
236 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
237 return process_pattern_string(wszPatternString + 1, pReader);
238 else
239 return hr;
240 }
241
242 HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
243 {
244 HKEY hkeyMediaType = NULL;
245 LONG lRet;
246 HRESULT hr = S_OK;
247 BOOL bFound = FALSE;
248 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
249
250 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
251
252 if(majorType)
253 *majorType = GUID_NULL;
254 if(minorType)
255 *minorType = GUID_NULL;
256 if(sourceFilter)
257 *sourceFilter = GUID_NULL;
258
259 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
260 hr = HRESULT_FROM_WIN32(lRet);
261
262 if (SUCCEEDED(hr))
263 {
264 DWORD indexMajor;
265
266 for (indexMajor = 0; !bFound; indexMajor++)
267 {
268 HKEY hkeyMajor;
269 WCHAR wszMajorKeyName[CHARS_IN_GUID];
270 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
271 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
272
273 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
274 break;
275 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
276 break;
277 TRACE("%s\n", debugstr_w(wszMajorKeyName));
278 if (!strcmpW(wszExtensions, wszMajorKeyName))
279 {
280 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType, sourceFilter) == S_OK)
281 bFound = TRUE;
282 }
283 /* We need a reader interface to check bytes */
284 else if (pReader)
285 {
286 DWORD indexMinor;
287
288 for (indexMinor = 0; !bFound; indexMinor++)
289 {
290 HKEY hkeyMinor;
291 WCHAR wszMinorKeyName[CHARS_IN_GUID];
292 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
293 WCHAR wszSourceFilterKeyName[CHARS_IN_GUID];
294 DWORD dwSourceFilterKeyNameLen = sizeof(wszSourceFilterKeyName);
295 DWORD maxValueLen;
296 DWORD indexValue;
297
298 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
299 break;
300
301 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
302 break;
303
304 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
305
306 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
307 break;
308
309 for (indexValue = 0; !bFound; indexValue++)
310 {
311 DWORD dwType;
312 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
313 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
314 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
315 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
316
317 if (RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen) != ERROR_SUCCESS)
318 {
319 HeapFree(GetProcessHeap(), 0, wszPatternString);
320 break;
321 }
322
323 if (strcmpW(wszValueName, source_filter_name)==0) {
324 HeapFree(GetProcessHeap(), 0, wszPatternString);
325 continue;
326 }
327
328 /* if it is not the source filter value */
329 if (process_pattern_string(wszPatternString, pReader) == S_OK)
330 {
331 HeapFree(GetProcessHeap(), 0, wszPatternString);
332 if (majorType && FAILED(CLSIDFromString(wszMajorKeyName, majorType)))
333 break;
334 if (minorType && FAILED(CLSIDFromString(wszMinorKeyName, minorType)))
335 break;
336 if (sourceFilter)
337 {
338 /* Look up the source filter key */
339 if (RegQueryValueExW(hkeyMinor, source_filter_name, NULL, NULL, (LPBYTE)wszSourceFilterKeyName, &dwSourceFilterKeyNameLen))
340 break;
341 if (FAILED(CLSIDFromString(wszSourceFilterKeyName, sourceFilter)))
342 break;
343 }
344 bFound = TRUE;
345 } else
346 HeapFree(GetProcessHeap(), 0, wszPatternString);
347 }
348 CloseHandle(hkeyMinor);
349 }
350 }
351 CloseHandle(hkeyMajor);
352 }
353 }
354 CloseHandle(hkeyMediaType);
355
356 if (SUCCEEDED(hr) && !bFound)
357 {
358 ERR("Media class not found\n");
359 hr = E_FAIL;
360 }
361 else if (bFound)
362 {
363 TRACE("Found file's class:\n");
364 if(majorType)
365 TRACE("\tmajor = %s\n", qzdebugstr_guid(majorType));
366 if(minorType)
367 TRACE("\tsubtype = %s\n", qzdebugstr_guid(minorType));
368 if(sourceFilter)
369 TRACE("\tsource filter = %s\n", qzdebugstr_guid(sourceFilter));
370 }
371
372 return hr;
373 }
374
375 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
376 {
377 AsyncReader *This = impl_from_BaseFilter(iface);
378
379 if (pos >= 1 || !This->pOutputPin)
380 return NULL;
381
382 IPin_AddRef(This->pOutputPin);
383 return This->pOutputPin;
384 }
385
386 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
387 {
388 AsyncReader *This = impl_from_BaseFilter(iface);
389
390 if (!This->pOutputPin)
391 return 0;
392 else
393 return 1;
394 }
395
396 static const BaseFilterFuncTable BaseFuncTable = {
397 AsyncReader_GetPin,
398 AsyncReader_GetPinCount
399 };
400
401 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
402 {
403 AsyncReader *pAsyncRead;
404
405 if( pUnkOuter )
406 return CLASS_E_NOAGGREGATION;
407
408 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
409
410 if (!pAsyncRead)
411 return E_OUTOFMEMORY;
412
413 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
414
415 pAsyncRead->IFileSourceFilter_iface.lpVtbl = &FileSource_Vtbl;
416 pAsyncRead->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
417 pAsyncRead->pOutputPin = NULL;
418
419 pAsyncRead->pszFileName = NULL;
420 pAsyncRead->pmt = NULL;
421
422 *ppv = pAsyncRead;
423
424 TRACE("-- created at %p\n", pAsyncRead);
425
426 return S_OK;
427 }
428
429 /** IUnknown methods **/
430
431 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
432 {
433 AsyncReader *This = impl_from_IBaseFilter(iface);
434
435 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
436
437 *ppv = NULL;
438
439 if (IsEqualIID(riid, &IID_IUnknown))
440 *ppv = &This->filter.IBaseFilter_iface;
441 else if (IsEqualIID(riid, &IID_IPersist))
442 *ppv = &This->filter.IBaseFilter_iface;
443 else if (IsEqualIID(riid, &IID_IMediaFilter))
444 *ppv = &This->filter.IBaseFilter_iface;
445 else if (IsEqualIID(riid, &IID_IBaseFilter))
446 *ppv = &This->filter.IBaseFilter_iface;
447 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
448 *ppv = &This->IFileSourceFilter_iface;
449 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
450 *ppv = &This->IAMFilterMiscFlags_iface;
451
452 if (*ppv)
453 {
454 IUnknown_AddRef((IUnknown *)(*ppv));
455 return S_OK;
456 }
457
458 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
459 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
460 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
461
462 return E_NOINTERFACE;
463 }
464
465 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
466 {
467 AsyncReader *This = impl_from_IBaseFilter(iface);
468 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
469
470 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
471
472 if (!refCount)
473 {
474 if (This->pOutputPin)
475 {
476 IPin *pConnectedTo;
477 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
478 {
479 IPin_Disconnect(pConnectedTo);
480 IPin_Release(pConnectedTo);
481 }
482 IPin_Disconnect(This->pOutputPin);
483 IPin_Release(This->pOutputPin);
484 }
485 CoTaskMemFree(This->pszFileName);
486 if (This->pmt)
487 FreeMediaType(This->pmt);
488 BaseFilter_Destroy(&This->filter);
489 CoTaskMemFree(This);
490 return 0;
491 }
492 else
493 return refCount;
494 }
495
496 /** IMediaFilter methods **/
497
498 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
499 {
500 AsyncReader *This = impl_from_IBaseFilter(iface);
501
502 TRACE("()\n");
503
504 This->filter.state = State_Stopped;
505
506 return S_OK;
507 }
508
509 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
510 {
511 AsyncReader *This = impl_from_IBaseFilter(iface);
512
513 TRACE("()\n");
514
515 This->filter.state = State_Paused;
516
517 return S_OK;
518 }
519
520 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
521 {
522 AsyncReader *This = impl_from_IBaseFilter(iface);
523
524 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
525
526 This->filter.state = State_Running;
527
528 return S_OK;
529 }
530
531 /** IBaseFilter methods **/
532
533 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
534 {
535 AsyncReader *This = impl_from_IBaseFilter(iface);
536 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
537
538 if (!Id || !ppPin)
539 return E_POINTER;
540
541 if (strcmpW(Id, wszOutputPinName))
542 {
543 *ppPin = NULL;
544 return VFW_E_NOT_FOUND;
545 }
546
547 *ppPin = This->pOutputPin;
548 IPin_AddRef(*ppPin);
549 return S_OK;
550 }
551
552 static const IBaseFilterVtbl AsyncReader_Vtbl =
553 {
554 AsyncReader_QueryInterface,
555 BaseFilterImpl_AddRef,
556 AsyncReader_Release,
557 BaseFilterImpl_GetClassID,
558 AsyncReader_Stop,
559 AsyncReader_Pause,
560 AsyncReader_Run,
561 BaseFilterImpl_GetState,
562 BaseFilterImpl_SetSyncSource,
563 BaseFilterImpl_GetSyncSource,
564 BaseFilterImpl_EnumPins,
565 AsyncReader_FindPin,
566 BaseFilterImpl_QueryFilterInfo,
567 BaseFilterImpl_JoinFilterGraph,
568 BaseFilterImpl_QueryVendorInfo
569 };
570
571 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
572 {
573 AsyncReader *This = impl_from_IFileSourceFilter(iface);
574
575 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
576 }
577
578 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
579 {
580 AsyncReader *This = impl_from_IFileSourceFilter(iface);
581
582 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
583 }
584
585 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
586 {
587 AsyncReader *This = impl_from_IFileSourceFilter(iface);
588
589 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
590 }
591
592 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
593 {
594 HRESULT hr;
595 HANDLE hFile;
596 IAsyncReader * pReader = NULL;
597 AsyncReader *This = impl_from_IFileSourceFilter(iface);
598
599 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
600
601 if (!pszFileName)
602 return E_POINTER;
603
604 /* open file */
605 /* FIXME: check the sharing values that native uses */
606 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
607
608 if (hFile == INVALID_HANDLE_VALUE)
609 {
610 return HRESULT_FROM_WIN32(GetLastError());
611 }
612
613 /* create pin */
614 hr = FileAsyncReader_Construct(hFile, &This->filter.IBaseFilter_iface, &This->filter.csFilter, &This->pOutputPin);
615 BaseFilterImpl_IncrementPinVersion(&This->filter);
616
617 if (SUCCEEDED(hr))
618 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
619
620 /* store file name & media type */
621 if (SUCCEEDED(hr))
622 {
623 CoTaskMemFree(This->pszFileName);
624 if (This->pmt)
625 FreeMediaType(This->pmt);
626
627 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
628 strcpyW(This->pszFileName, pszFileName);
629
630 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
631 if (!pmt)
632 {
633 This->pmt->bFixedSizeSamples = TRUE;
634 This->pmt->bTemporalCompression = FALSE;
635 This->pmt->cbFormat = 0;
636 This->pmt->pbFormat = NULL;
637 This->pmt->pUnk = NULL;
638 This->pmt->lSampleSize = 0;
639 This->pmt->formattype = FORMAT_None;
640 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype, NULL);
641 if (FAILED(hr))
642 {
643 This->pmt->majortype = MEDIATYPE_Stream;
644 This->pmt->subtype = MEDIASUBTYPE_NULL;
645 hr = S_OK;
646 }
647 }
648 else
649 CopyMediaType(This->pmt, pmt);
650 }
651
652 if (pReader)
653 IAsyncReader_Release(pReader);
654
655 if (FAILED(hr))
656 {
657 if (This->pOutputPin)
658 {
659 IPin_Release(This->pOutputPin);
660 This->pOutputPin = NULL;
661 }
662
663 CoTaskMemFree(This->pszFileName);
664 if (This->pmt)
665 FreeMediaType(This->pmt);
666 This->pszFileName = NULL;
667 This->pmt = NULL;
668
669 CloseHandle(hFile);
670 }
671
672 /* FIXME: check return codes */
673 return hr;
674 }
675
676 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
677 {
678 AsyncReader *This = impl_from_IFileSourceFilter(iface);
679
680 TRACE("(%p, %p)\n", ppszFileName, pmt);
681
682 if (!ppszFileName)
683 return E_POINTER;
684
685 /* copy file name & media type if available, otherwise clear the outputs */
686 if (This->pszFileName)
687 {
688 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
689 strcpyW(*ppszFileName, This->pszFileName);
690 }
691 else
692 *ppszFileName = NULL;
693
694 if (pmt)
695 {
696 if (This->pmt)
697 CopyMediaType(pmt, This->pmt);
698 else
699 ZeroMemory(pmt, sizeof(*pmt));
700 }
701
702 return S_OK;
703 }
704
705 static const IFileSourceFilterVtbl FileSource_Vtbl =
706 {
707 FileSource_QueryInterface,
708 FileSource_AddRef,
709 FileSource_Release,
710 FileSource_Load,
711 FileSource_GetCurFile
712 };
713
714
715 /* the dwUserData passed back to user */
716 typedef struct DATAREQUEST
717 {
718 IMediaSample * pSample; /* sample passed to us by user */
719 DWORD_PTR dwUserData; /* user data passed to us */
720 OVERLAPPED ovl; /* our overlapped structure */
721 } DATAREQUEST;
722
723 typedef struct FileAsyncReader
724 {
725 BaseOutputPin pin;
726 IAsyncReader IAsyncReader_iface;
727
728 ALLOCATOR_PROPERTIES allocProps;
729 HANDLE hFile;
730 BOOL bFlushing;
731 /* Why would you need more? Every sample has its own handle */
732 LONG queued_number;
733 LONG samples;
734 LONG oldest_sample;
735 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
736 DATAREQUEST *sample_list;
737
738 /* Have a handle for every sample, and then one more as flushing handle */
739 HANDLE *handle_list;
740 } FileAsyncReader;
741
742 static inline FileAsyncReader *impl_from_IPin(IPin *iface)
743 {
744 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin.IPin_iface);
745 }
746
747 static inline FileAsyncReader *impl_from_BasePin(BasePin *iface)
748 {
749 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin);
750 }
751
752 static inline FileAsyncReader *impl_from_BaseOutputPin(BaseOutputPin *iface)
753 {
754 return CONTAINING_RECORD(iface, FileAsyncReader, pin);
755 }
756
757 static inline BaseOutputPin *impl_BaseOutputPin_from_BasePin(BasePin *iface)
758 {
759 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
760 }
761
762 static inline FileAsyncReader *impl_from_IAsyncReader(IAsyncReader *iface)
763 {
764 return CONTAINING_RECORD(iface, FileAsyncReader, IAsyncReader_iface);
765 }
766
767 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
768 {
769 FileAsyncReader *This = impl_from_IPin(iface);
770 AM_MEDIA_TYPE *pmt_filter = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt;
771
772 FIXME("(%p, %p)\n", iface, pmt);
773
774 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
775 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
776 IsEqualGUID(&pmt->formattype, &FORMAT_None))
777 return S_OK;
778
779 return S_FALSE;
780 }
781
782 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
783 {
784 FileAsyncReader *This = impl_from_BasePin(iface);
785 if (iPosition < 0)
786 return E_INVALIDARG;
787 if (iPosition > 0)
788 return VFW_S_NO_MORE_ITEMS;
789 CopyMediaType(pmt, impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt);
790 return S_OK;
791 }
792
793 /* overridden pin functions */
794
795 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
796 {
797 FileAsyncReader *This = impl_from_IPin(iface);
798 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
799
800 *ppv = NULL;
801
802 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IPin))
803 *ppv = &This->pin.pin.IPin_iface;
804 else if (IsEqualIID(riid, &IID_IAsyncReader))
805 *ppv = &This->IAsyncReader_iface;
806
807 if (*ppv)
808 {
809 IUnknown_AddRef((IUnknown *)*ppv);
810 return S_OK;
811 }
812
813 if (!IsEqualIID(riid, &IID_IMediaSeeking))
814 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
815
816 return E_NOINTERFACE;
817 }
818
819 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
820 {
821 FileAsyncReader *This = impl_from_IPin(iface);
822 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
823 int x;
824
825 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
826
827 if (!refCount)
828 {
829 CoTaskMemFree(This->sample_list);
830 if (This->handle_list)
831 {
832 for (x = 0; x <= This->samples; ++x)
833 CloseHandle(This->handle_list[x]);
834 CoTaskMemFree(This->handle_list);
835 }
836 CloseHandle(This->hFile);
837 This->csList.DebugInfo->Spare[0] = 0;
838 DeleteCriticalSection(&This->csList);
839 BaseOutputPin_Destroy(&This->pin);
840 return 0;
841 }
842 return refCount;
843 }
844
845 static const IPinVtbl FileAsyncReaderPin_Vtbl =
846 {
847 FileAsyncReaderPin_QueryInterface,
848 BasePinImpl_AddRef,
849 FileAsyncReaderPin_Release,
850 BaseOutputPinImpl_Connect,
851 BaseOutputPinImpl_ReceiveConnection,
852 BasePinImpl_Disconnect,
853 BasePinImpl_ConnectedTo,
854 BasePinImpl_ConnectionMediaType,
855 BasePinImpl_QueryPinInfo,
856 BasePinImpl_QueryDirection,
857 BasePinImpl_QueryId,
858 FileAsyncReaderPin_QueryAccept,
859 BasePinImpl_EnumMediaTypes,
860 BasePinImpl_QueryInternalConnections,
861 BaseOutputPinImpl_EndOfStream,
862 BaseOutputPinImpl_BeginFlush,
863 BaseOutputPinImpl_EndFlush,
864 BasePinImpl_NewSegment
865 };
866
867 /* Function called as a helper to IPin_Connect */
868 /* specific AM_MEDIA_TYPE - it cannot be NULL */
869 /* this differs from standard OutputPin_AttemptConnection only in that it
870 * doesn't need the IMemInputPin interface on the receiving pin */
871 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
872 {
873 BaseOutputPin *This = impl_BaseOutputPin_from_BasePin(iface);
874 HRESULT hr;
875
876 TRACE("(%p, %p)\n", pReceivePin, pmt);
877 dump_AM_MEDIA_TYPE(pmt);
878
879 /* FIXME: call queryacceptproc */
880
881 This->pin.pConnectedTo = pReceivePin;
882 IPin_AddRef(pReceivePin);
883 CopyMediaType(&This->pin.mtCurrent, pmt);
884
885 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
886
887 if (FAILED(hr))
888 {
889 IPin_Release(This->pin.pConnectedTo);
890 This->pin.pConnectedTo = NULL;
891 FreeMediaType(&This->pin.mtCurrent);
892 }
893
894 TRACE(" -- %x\n", hr);
895 return hr;
896 }
897
898 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
899 {
900 FileAsyncReader *This = impl_from_BaseOutputPin(iface);
901 ALLOCATOR_PROPERTIES actual;
902
903 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
904 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
905 if (ppropInputRequest->cbPrefix)
906 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
907 if (ppropInputRequest->cbBuffer)
908 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
909 if (ppropInputRequest->cBuffers)
910 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
911
912 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
913 }
914
915 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
916 {
917 NULL,
918 FileAsyncReaderPin_AttemptConnection,
919 BasePinImpl_GetMediaTypeVersion,
920 FileAsyncReaderPin_GetMediaType
921 },
922 FileAsyncReaderPin_DecideBufferSize,
923 BaseOutputPinImpl_DecideAllocator,
924 BaseOutputPinImpl_BreakConnect
925 };
926
927 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
928 {
929 PIN_INFO piOutput;
930 HRESULT hr;
931
932 *ppPin = NULL;
933 piOutput.dir = PINDIR_OUTPUT;
934 piOutput.pFilter = pBaseFilter;
935 strcpyW(piOutput.achName, wszOutputPinName);
936 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseOutputFuncTable, pCritSec, ppPin);
937
938 if (SUCCEEDED(hr))
939 {
940 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
941 pPinImpl->IAsyncReader_iface.lpVtbl = &FileAsyncReader_Vtbl;
942 pPinImpl->hFile = hFile;
943 pPinImpl->bFlushing = FALSE;
944 pPinImpl->sample_list = NULL;
945 pPinImpl->handle_list = NULL;
946 pPinImpl->queued_number = 0;
947 InitializeCriticalSection(&pPinImpl->csList);
948 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
949 }
950 return hr;
951 }
952
953 /* IAsyncReader */
954
955 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
956 {
957 FileAsyncReader *This = impl_from_IAsyncReader(iface);
958
959 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
960 }
961
962 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
963 {
964 FileAsyncReader *This = impl_from_IAsyncReader(iface);
965
966 return IPin_AddRef(&This->pin.pin.IPin_iface);
967 }
968
969 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
970 {
971 FileAsyncReader *This = impl_from_IAsyncReader(iface);
972
973 return IPin_Release(&This->pin.pin.IPin_iface);
974 }
975
976 #define DEF_ALIGNMENT 1
977
978 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
979 {
980 FileAsyncReader *This = impl_from_IAsyncReader(iface);
981
982 HRESULT hr = S_OK;
983
984 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
985
986 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
987 pProps->cbAlign = DEF_ALIGNMENT;
988
989 if (pPreferred)
990 {
991 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
992 /* FIXME: check we are still aligned */
993 if (SUCCEEDED(hr))
994 {
995 IMemAllocator_AddRef(pPreferred);
996 *ppActual = pPreferred;
997 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
998 goto done;
999 }
1000 }
1001
1002 pPreferred = NULL;
1003
1004 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1005
1006 if (SUCCEEDED(hr))
1007 {
1008 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1009 /* FIXME: check we are still aligned */
1010 if (SUCCEEDED(hr))
1011 {
1012 *ppActual = pPreferred;
1013 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1014 }
1015 }
1016
1017 done:
1018 if (SUCCEEDED(hr))
1019 {
1020 CoTaskMemFree(This->sample_list);
1021 if (This->handle_list)
1022 {
1023 int x;
1024 for (x = 0; x <= This->samples; ++x)
1025 CloseHandle(This->handle_list[x]);
1026 CoTaskMemFree(This->handle_list);
1027 }
1028
1029 This->samples = pProps->cBuffers;
1030 This->oldest_sample = 0;
1031 TRACE("Samples: %u\n", This->samples);
1032 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1033 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1034
1035 if (This->sample_list && This->handle_list)
1036 {
1037 int x;
1038 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1039 for (x = 0; x < This->samples; ++x)
1040 {
1041 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1042 if (x + 1 < This->samples)
1043 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1044 }
1045 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1046 This->allocProps = *pProps;
1047 }
1048 else
1049 {
1050 hr = E_OUTOFMEMORY;
1051 CoTaskMemFree(This->sample_list);
1052 CoTaskMemFree(This->handle_list);
1053 This->samples = 0;
1054 This->sample_list = NULL;
1055 This->handle_list = NULL;
1056 }
1057 }
1058
1059 if (FAILED(hr))
1060 {
1061 *ppActual = NULL;
1062 if (pPreferred)
1063 IMemAllocator_Release(pPreferred);
1064 }
1065
1066 TRACE("-- %x\n", hr);
1067 return hr;
1068 }
1069
1070 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1071 * however, this would be quite complicated to do and may be a bit error prone */
1072 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1073 {
1074 HRESULT hr = S_OK;
1075 REFERENCE_TIME Start;
1076 REFERENCE_TIME Stop;
1077 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1078 LPBYTE pBuffer = NULL;
1079
1080 TRACE("(%p, %lx)\n", pSample, dwUser);
1081
1082 if (!pSample)
1083 return E_POINTER;
1084
1085 /* get start and stop positions in bytes */
1086 if (SUCCEEDED(hr))
1087 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1088
1089 if (SUCCEEDED(hr))
1090 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1091
1092 EnterCriticalSection(&This->csList);
1093 if (This->bFlushing)
1094 {
1095 LeaveCriticalSection(&This->csList);
1096 return VFW_E_WRONG_STATE;
1097 }
1098
1099 if (SUCCEEDED(hr))
1100 {
1101 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1102 DATAREQUEST *pDataRq;
1103 int x;
1104
1105 /* Try to insert above the waiting sample if possible */
1106 for (x = This->oldest_sample; x < This->samples; ++x)
1107 {
1108 if (!This->sample_list[x].pSample)
1109 break;
1110 }
1111
1112 if (x >= This->samples)
1113 for (x = 0; x < This->oldest_sample; ++x)
1114 {
1115 if (!This->sample_list[x].pSample)
1116 break;
1117 }
1118
1119 /* There must be a sample we have found */
1120 assert(x < This->samples);
1121 ++This->queued_number;
1122
1123 pDataRq = This->sample_list + x;
1124
1125 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1126 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1127 pDataRq->dwUserData = dwUser;
1128
1129 /* we violate traditional COM rules here by maintaining
1130 * a reference to the sample, but not calling AddRef, but
1131 * that's what MSDN says to do */
1132 pDataRq->pSample = pSample;
1133
1134 /* this is definitely not how it is implemented on Win9x
1135 * as they do not support async reads on files, but it is
1136 * sooo much easier to use this than messing around with threads!
1137 */
1138 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1139 hr = HRESULT_FROM_WIN32(GetLastError());
1140
1141 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1142 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1143 hr = S_OK;
1144 }
1145
1146 LeaveCriticalSection(&This->csList);
1147
1148 TRACE("-- %x\n", hr);
1149 return hr;
1150 }
1151
1152 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1153 {
1154 HRESULT hr = S_OK;
1155 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1156 DWORD buffer = ~0;
1157
1158 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1159
1160 *ppSample = NULL;
1161 *pdwUser = 0;
1162
1163 EnterCriticalSection(&This->csList);
1164 if (!This->bFlushing)
1165 {
1166 LONG oldest = This->oldest_sample;
1167
1168 if (!This->queued_number)
1169 {
1170 /* It could be that nothing is queued right now, but that can be fixed */
1171 WARN("Called without samples in queue and not flushing!!\n");
1172 }
1173 LeaveCriticalSection(&This->csList);
1174
1175 /* wait for an object to read, or time out */
1176 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1177
1178 EnterCriticalSection(&This->csList);
1179 if (buffer <= This->samples)
1180 {
1181 /* Re-scale the buffer back to normal */
1182 buffer += oldest;
1183
1184 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1185 if (buffer > This->samples)
1186 buffer -= This->samples + 1;
1187 assert(buffer <= This->samples);
1188 }
1189
1190 if (buffer >= This->samples)
1191 {
1192 if (buffer != This->samples)
1193 {
1194 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1195 hr = VFW_E_TIMEOUT;
1196 }
1197 else
1198 hr = VFW_E_WRONG_STATE;
1199 buffer = ~0;
1200 }
1201 else
1202 --This->queued_number;
1203 }
1204
1205 if (This->bFlushing && buffer == ~0)
1206 {
1207 for (buffer = 0; buffer < This->samples; ++buffer)
1208 {
1209 if (This->sample_list[buffer].pSample)
1210 {
1211 ResetEvent(This->handle_list[buffer]);
1212 break;
1213 }
1214 }
1215 if (buffer == This->samples)
1216 {
1217 assert(!This->queued_number);
1218 hr = VFW_E_TIMEOUT;
1219 }
1220 else
1221 {
1222 --This->queued_number;
1223 hr = S_OK;
1224 }
1225 }
1226
1227 if (SUCCEEDED(hr))
1228 {
1229 REFERENCE_TIME rtStart, rtStop;
1230 REFERENCE_TIME rtSampleStart, rtSampleStop;
1231 DATAREQUEST *pDataRq = This->sample_list + buffer;
1232 DWORD dwBytes = 0;
1233
1234 /* get any errors */
1235 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1236 hr = HRESULT_FROM_WIN32(GetLastError());
1237
1238 /* Return the sample no matter what so it can be destroyed */
1239 *ppSample = pDataRq->pSample;
1240 *pdwUser = pDataRq->dwUserData;
1241
1242 if (This->bFlushing)
1243 hr = VFW_E_WRONG_STATE;
1244
1245 if (FAILED(hr))
1246 dwBytes = 0;
1247
1248 /* Set the time on the sample */
1249 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1250
1251 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1252 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1253 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1254
1255 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1256 assert(rtStart == rtSampleStart);
1257 assert(rtStop <= rtSampleStop);
1258
1259 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1260 assert(rtStart == rtSampleStart);
1261 if (hr == S_OK)
1262 assert(rtStop == rtSampleStop);
1263 else
1264 assert(rtStop == rtStart);
1265
1266 This->sample_list[buffer].pSample = NULL;
1267 assert(This->oldest_sample < This->samples);
1268
1269 if (buffer == This->oldest_sample)
1270 {
1271 LONG x;
1272 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1273 if (This->sample_list[x].pSample)
1274 break;
1275 if (x >= This->samples)
1276 for (x = 0; x < This->oldest_sample; ++x)
1277 if (This->sample_list[x].pSample)
1278 break;
1279 if (This->oldest_sample == x)
1280 /* No samples found, reset to 0 */
1281 x = 0;
1282 This->oldest_sample = x;
1283 }
1284 }
1285 LeaveCriticalSection(&This->csList);
1286
1287 TRACE("-- %x\n", hr);
1288 return hr;
1289 }
1290
1291 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1292
1293 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1294 {
1295 BYTE * pBuffer;
1296 REFERENCE_TIME tStart;
1297 REFERENCE_TIME tStop;
1298 HRESULT hr;
1299
1300 TRACE("(%p)\n", pSample);
1301
1302 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1303
1304 if (SUCCEEDED(hr))
1305 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1306
1307 if (SUCCEEDED(hr))
1308 hr = FileAsyncReader_SyncRead(iface,
1309 BYTES_FROM_MEDIATIME(tStart),
1310 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1311 pBuffer);
1312
1313 TRACE("-- %x\n", hr);
1314 return hr;
1315 }
1316
1317 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1318 {
1319 OVERLAPPED ovl;
1320 HRESULT hr = S_OK;
1321 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1322
1323 TRACE("(%s, %d, %p)\n", wine_dbgstr_longlong(llPosition), lLength, pBuffer);
1324
1325 ZeroMemory(&ovl, sizeof(ovl));
1326
1327 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1328 /* NOTE: llPosition is the actual byte position to start reading from */
1329 ovl.u.s.Offset = (DWORD) llPosition;
1330 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1331
1332 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1333 hr = HRESULT_FROM_WIN32(GetLastError());
1334
1335 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1336 hr = S_OK;
1337
1338 if (SUCCEEDED(hr))
1339 {
1340 DWORD dwBytesRead;
1341
1342 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1343 hr = HRESULT_FROM_WIN32(GetLastError());
1344 }
1345
1346 CloseHandle(ovl.hEvent);
1347
1348 TRACE("-- %x\n", hr);
1349 return hr;
1350 }
1351
1352 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1353 {
1354 DWORD dwSizeLow;
1355 DWORD dwSizeHigh;
1356 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1357
1358 TRACE("(%p, %p)\n", pTotal, pAvailable);
1359
1360 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1361 (GetLastError() != NO_ERROR))
1362 return HRESULT_FROM_WIN32(GetLastError());
1363
1364 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1365
1366 *pAvailable = *pTotal;
1367
1368 return S_OK;
1369 }
1370
1371 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1372 {
1373 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1374
1375 TRACE("()\n");
1376
1377 EnterCriticalSection(&This->csList);
1378 This->bFlushing = TRUE;
1379 CancelIo(This->hFile);
1380 SetEvent(This->handle_list[This->samples]);
1381 LeaveCriticalSection(&This->csList);
1382
1383 return S_OK;
1384 }
1385
1386 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1387 {
1388 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1389 int x;
1390
1391 TRACE("()\n");
1392
1393 EnterCriticalSection(&This->csList);
1394 ResetEvent(This->handle_list[This->samples]);
1395 This->bFlushing = FALSE;
1396 for (x = 0; x < This->samples; ++x)
1397 assert(!This->sample_list[x].pSample);
1398
1399 LeaveCriticalSection(&This->csList);
1400
1401 return S_OK;
1402 }
1403
1404 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1405 {
1406 FileAsyncReader_QueryInterface,
1407 FileAsyncReader_AddRef,
1408 FileAsyncReader_Release,
1409 FileAsyncReader_RequestAllocator,
1410 FileAsyncReader_Request,
1411 FileAsyncReader_WaitForNext,
1412 FileAsyncReader_SyncReadAligned,
1413 FileAsyncReader_SyncRead,
1414 FileAsyncReader_Length,
1415 FileAsyncReader_BeginFlush,
1416 FileAsyncReader_EndFlush,
1417 };
1418
1419
1420 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1421 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1422 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
1423 }
1424
1425 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1426 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1427 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
1428 }
1429
1430 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1431 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1432 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
1433 }
1434
1435 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1436 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1437 }
1438
1439 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1440 AMFilterMiscFlags_QueryInterface,
1441 AMFilterMiscFlags_AddRef,
1442 AMFilterMiscFlags_Release,
1443 AMFilterMiscFlags_GetMiscFlags
1444 };