advapi32: Implement RegSetKeySecurity on top of NtSetSecurityObject.
[wine.git] / dlls / quartz / filesource.c
bloba0af3a4a50e501da3fb34d0a1d9eac24ec9e6093
1 /*
2 * File Source Filter
4 * Copyright 2003 Robert Shearman
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.
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.
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
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "quartz_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "pin.h"
29 #include "uuids.h"
30 #include "vfwmsgs.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include <assert.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
40 typedef struct AsyncReader
42 BaseFilter filter;
43 IFileSourceFilter IFileSourceFilter_iface;
44 IAMFilterMiscFlags IAMFilterMiscFlags_iface;
46 IPin * pOutputPin;
47 LPOLESTR pszFileName;
48 AM_MEDIA_TYPE * pmt;
49 } AsyncReader;
51 static inline AsyncReader *impl_from_BaseFilter(BaseFilter *iface)
53 return CONTAINING_RECORD(iface, AsyncReader, filter);
56 static inline AsyncReader *impl_from_IBaseFilter(IBaseFilter *iface)
58 return CONTAINING_RECORD(iface, AsyncReader, filter.IBaseFilter_iface);
61 static inline AsyncReader *impl_from_IFileSourceFilter(IFileSourceFilter *iface)
63 return CONTAINING_RECORD(iface, AsyncReader, IFileSourceFilter_iface);
66 static inline AsyncReader *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
68 return CONTAINING_RECORD(iface, AsyncReader, IAMFilterMiscFlags_iface);
71 static const IBaseFilterVtbl AsyncReader_Vtbl;
72 static const IFileSourceFilterVtbl FileSource_Vtbl;
73 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
74 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
76 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
78 static const WCHAR mediatype_name[] = {
79 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
80 static const WCHAR subtype_name[] = {
81 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
82 static const WCHAR source_filter_name[] = {
83 'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
85 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
87 WCHAR *extension;
88 LONG l;
89 HKEY hsub;
90 WCHAR keying[39];
91 DWORD size;
93 if (!pszFileName)
94 return E_POINTER;
96 /* Get the part of the name that matters */
97 extension = PathFindExtensionW(pszFileName);
98 if (*extension != '.')
99 return E_FAIL;
101 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
102 if (l)
103 return E_FAIL;
105 if (majorType)
107 size = sizeof(keying);
108 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
109 if (!l)
110 CLSIDFromString(keying, majorType);
113 if (minorType)
115 size = sizeof(keying);
116 if (!l)
117 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
118 if (!l)
119 CLSIDFromString(keying, minorType);
122 if (sourceFilter)
124 size = sizeof(keying);
125 if (!l)
126 l = RegQueryValueExW(hsub, source_filter_name, NULL, NULL, (LPBYTE)keying, &size);
127 if (!l)
128 CLSIDFromString(keying, sourceFilter);
131 RegCloseKey(hsub);
133 if (!l)
134 return S_OK;
135 return E_FAIL;
138 static unsigned char byte_from_hex_char(WCHAR wHex)
140 switch (tolowerW(wHex))
142 case '0':
143 case '1':
144 case '2':
145 case '3':
146 case '4':
147 case '5':
148 case '6':
149 case '7':
150 case '8':
151 case '9':
152 return (wHex - '0') & 0xf;
153 case 'a':
154 case 'b':
155 case 'c':
156 case 'd':
157 case 'e':
158 case 'f':
159 return (wHex - 'a' + 10) & 0xf;
160 default:
161 return 0;
165 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
167 ULONG ulOffset;
168 ULONG ulBytes;
169 BYTE * pbMask;
170 BYTE * pbValue;
171 BYTE * pbFile;
172 HRESULT hr = S_OK;
173 ULONG strpos;
175 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
177 /* format: "offset, bytestocompare, mask, value" */
179 ulOffset = strtolW(wszPatternString, NULL, 10);
181 if (!(wszPatternString = strchrW(wszPatternString, ',')))
182 return E_INVALIDARG;
184 wszPatternString++; /* skip ',' */
186 ulBytes = strtolW(wszPatternString, NULL, 10);
188 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
189 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
190 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
192 /* default mask is match everything */
193 memset(pbMask, 0xFF, ulBytes);
195 if (!(wszPatternString = strchrW(wszPatternString, ',')))
196 hr = E_INVALIDARG;
198 if (hr == S_OK)
200 wszPatternString++; /* skip ',' */
201 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
203 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
205 if ((strpos % 2) == 1) /* odd numbered position */
206 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
207 else
208 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
211 if (!(wszPatternString = strchrW(wszPatternString, ',')))
212 hr = E_INVALIDARG;
213 else
214 wszPatternString++; /* skip ',' */
217 if (hr == S_OK)
219 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
222 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
224 if ((strpos % 2) == 1) /* odd numbered position */
225 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
226 else
227 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
231 if (hr == S_OK)
232 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
234 if (hr == S_OK)
236 ULONG i;
237 for (i = 0; i < ulBytes; i++)
238 if ((pbFile[i] & pbMask[i]) != pbValue[i])
240 hr = S_FALSE;
241 break;
245 HeapFree(GetProcessHeap(), 0, pbMask);
246 HeapFree(GetProcessHeap(), 0, pbValue);
247 HeapFree(GetProcessHeap(), 0, pbFile);
249 /* if we encountered no errors with this string, and there is a following tuple, then we
250 * have to match that as well to succeed */
251 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
252 return process_pattern_string(wszPatternString + 1, pReader);
253 else
254 return hr;
257 HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
259 HKEY hkeyMediaType = NULL;
260 LONG lRet;
261 HRESULT hr = S_OK;
262 BOOL bFound = FALSE;
263 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
265 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
267 if(majorType)
268 *majorType = GUID_NULL;
269 if(minorType)
270 *minorType = GUID_NULL;
271 if(sourceFilter)
272 *sourceFilter = GUID_NULL;
274 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
275 hr = HRESULT_FROM_WIN32(lRet);
277 if (SUCCEEDED(hr))
279 DWORD indexMajor;
281 for (indexMajor = 0; !bFound; indexMajor++)
283 HKEY hkeyMajor;
284 WCHAR wszMajorKeyName[CHARS_IN_GUID];
285 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
286 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
288 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
289 break;
290 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
291 break;
292 TRACE("%s\n", debugstr_w(wszMajorKeyName));
293 if (!strcmpW(wszExtensions, wszMajorKeyName))
295 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType, sourceFilter) == S_OK)
296 bFound = TRUE;
298 /* We need a reader interface to check bytes */
299 else if (pReader)
301 DWORD indexMinor;
303 for (indexMinor = 0; !bFound; indexMinor++)
305 HKEY hkeyMinor;
306 WCHAR wszMinorKeyName[CHARS_IN_GUID];
307 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
308 WCHAR wszSourceFilterKeyName[CHARS_IN_GUID];
309 DWORD dwSourceFilterKeyNameLen = sizeof(wszSourceFilterKeyName);
310 DWORD maxValueLen;
311 DWORD indexValue;
313 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
314 break;
316 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
317 break;
319 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
321 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
322 break;
324 for (indexValue = 0; !bFound; indexValue++)
326 DWORD dwType;
327 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
328 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
329 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
330 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
332 if (RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen) != ERROR_SUCCESS)
334 HeapFree(GetProcessHeap(), 0, wszPatternString);
335 break;
338 if (strcmpW(wszValueName, source_filter_name)==0) {
339 HeapFree(GetProcessHeap(), 0, wszPatternString);
340 continue;
343 /* if it is not the source filter value */
344 if (process_pattern_string(wszPatternString, pReader) == S_OK)
346 HeapFree(GetProcessHeap(), 0, wszPatternString);
347 if (majorType && FAILED(CLSIDFromString(wszMajorKeyName, majorType)))
348 break;
349 if (minorType && FAILED(CLSIDFromString(wszMinorKeyName, minorType)))
350 break;
351 if (sourceFilter)
353 /* Look up the source filter key */
354 if (RegQueryValueExW(hkeyMinor, source_filter_name, NULL, NULL, (LPBYTE)wszSourceFilterKeyName, &dwSourceFilterKeyNameLen))
355 break;
356 if (FAILED(CLSIDFromString(wszSourceFilterKeyName, sourceFilter)))
357 break;
359 bFound = TRUE;
360 } else
361 HeapFree(GetProcessHeap(), 0, wszPatternString);
363 CloseHandle(hkeyMinor);
366 CloseHandle(hkeyMajor);
369 CloseHandle(hkeyMediaType);
371 if (SUCCEEDED(hr) && !bFound)
373 ERR("Media class not found\n");
374 hr = E_FAIL;
376 else if (bFound)
378 TRACE("Found file's class:\n");
379 if(majorType)
380 TRACE("\tmajor = %s\n", qzdebugstr_guid(majorType));
381 if(minorType)
382 TRACE("\tsubtype = %s\n", qzdebugstr_guid(minorType));
383 if(sourceFilter)
384 TRACE("\tsource filter = %s\n", qzdebugstr_guid(sourceFilter));
387 return hr;
390 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
392 AsyncReader *This = impl_from_BaseFilter(iface);
394 if (pos >= 1 || !This->pOutputPin)
395 return NULL;
397 IPin_AddRef(This->pOutputPin);
398 return This->pOutputPin;
401 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
403 AsyncReader *This = impl_from_BaseFilter(iface);
405 if (!This->pOutputPin)
406 return 0;
407 else
408 return 1;
411 static const BaseFilterFuncTable BaseFuncTable = {
412 AsyncReader_GetPin,
413 AsyncReader_GetPinCount
416 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
418 AsyncReader *pAsyncRead;
420 if( pUnkOuter )
421 return CLASS_E_NOAGGREGATION;
423 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
425 if (!pAsyncRead)
426 return E_OUTOFMEMORY;
428 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
430 pAsyncRead->IFileSourceFilter_iface.lpVtbl = &FileSource_Vtbl;
431 pAsyncRead->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
432 pAsyncRead->pOutputPin = NULL;
434 pAsyncRead->pszFileName = NULL;
435 pAsyncRead->pmt = NULL;
437 *ppv = pAsyncRead;
439 TRACE("-- created at %p\n", pAsyncRead);
441 return S_OK;
444 /** IUnknown methods **/
446 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
448 AsyncReader *This = impl_from_IBaseFilter(iface);
450 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
452 *ppv = NULL;
454 if (IsEqualIID(riid, &IID_IUnknown))
455 *ppv = This;
456 else if (IsEqualIID(riid, &IID_IPersist))
457 *ppv = This;
458 else if (IsEqualIID(riid, &IID_IMediaFilter))
459 *ppv = This;
460 else if (IsEqualIID(riid, &IID_IBaseFilter))
461 *ppv = This;
462 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
463 *ppv = &This->IFileSourceFilter_iface;
464 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
465 *ppv = &This->IAMFilterMiscFlags_iface;
467 if (*ppv)
469 IUnknown_AddRef((IUnknown *)(*ppv));
470 return S_OK;
473 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
474 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
475 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
477 return E_NOINTERFACE;
480 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
482 AsyncReader *This = impl_from_IBaseFilter(iface);
483 ULONG refCount = BaseFilterImpl_Release(iface);
485 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
487 if (!refCount)
489 if (This->pOutputPin)
491 IPin *pConnectedTo;
492 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
494 IPin_Disconnect(pConnectedTo);
495 IPin_Release(pConnectedTo);
497 IPin_Disconnect(This->pOutputPin);
498 IPin_Release(This->pOutputPin);
500 CoTaskMemFree(This->pszFileName);
501 if (This->pmt)
502 FreeMediaType(This->pmt);
503 CoTaskMemFree(This);
504 return 0;
506 else
507 return refCount;
510 /** IMediaFilter methods **/
512 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
514 AsyncReader *This = impl_from_IBaseFilter(iface);
516 TRACE("()\n");
518 This->filter.state = State_Stopped;
520 return S_OK;
523 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
525 AsyncReader *This = impl_from_IBaseFilter(iface);
527 TRACE("()\n");
529 This->filter.state = State_Paused;
531 return S_OK;
534 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
536 AsyncReader *This = impl_from_IBaseFilter(iface);
538 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
540 This->filter.state = State_Running;
542 return S_OK;
545 /** IBaseFilter methods **/
547 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
549 AsyncReader *This = impl_from_IBaseFilter(iface);
550 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
552 if (!Id || !ppPin)
553 return E_POINTER;
555 if (strcmpW(Id, wszOutputPinName))
557 *ppPin = NULL;
558 return VFW_E_NOT_FOUND;
561 *ppPin = This->pOutputPin;
562 IPin_AddRef(*ppPin);
563 return S_OK;
566 static const IBaseFilterVtbl AsyncReader_Vtbl =
568 AsyncReader_QueryInterface,
569 BaseFilterImpl_AddRef,
570 AsyncReader_Release,
571 BaseFilterImpl_GetClassID,
572 AsyncReader_Stop,
573 AsyncReader_Pause,
574 AsyncReader_Run,
575 BaseFilterImpl_GetState,
576 BaseFilterImpl_SetSyncSource,
577 BaseFilterImpl_GetSyncSource,
578 BaseFilterImpl_EnumPins,
579 AsyncReader_FindPin,
580 BaseFilterImpl_QueryFilterInfo,
581 BaseFilterImpl_JoinFilterGraph,
582 BaseFilterImpl_QueryVendorInfo
585 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
587 AsyncReader *This = impl_from_IFileSourceFilter(iface);
589 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
592 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
594 AsyncReader *This = impl_from_IFileSourceFilter(iface);
596 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
599 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
601 AsyncReader *This = impl_from_IFileSourceFilter(iface);
603 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
606 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
608 HRESULT hr;
609 HANDLE hFile;
610 IAsyncReader * pReader = NULL;
611 AsyncReader *This = impl_from_IFileSourceFilter(iface);
613 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
615 /* open file */
616 /* FIXME: check the sharing values that native uses */
617 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
619 if (hFile == INVALID_HANDLE_VALUE)
621 return HRESULT_FROM_WIN32(GetLastError());
624 /* create pin */
625 hr = FileAsyncReader_Construct(hFile, &This->filter.IBaseFilter_iface, &This->filter.csFilter, &This->pOutputPin);
626 BaseFilterImpl_IncrementPinVersion(&This->filter);
628 if (SUCCEEDED(hr))
629 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
631 /* store file name & media type */
632 if (SUCCEEDED(hr))
634 CoTaskMemFree(This->pszFileName);
635 if (This->pmt)
636 FreeMediaType(This->pmt);
638 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
639 strcpyW(This->pszFileName, pszFileName);
641 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
642 if (!pmt)
644 This->pmt->bFixedSizeSamples = TRUE;
645 This->pmt->bTemporalCompression = FALSE;
646 This->pmt->cbFormat = 0;
647 This->pmt->pbFormat = NULL;
648 This->pmt->pUnk = NULL;
649 This->pmt->lSampleSize = 0;
650 This->pmt->formattype = FORMAT_None;
651 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype, NULL);
652 if (FAILED(hr))
654 CoTaskMemFree(This->pmt);
655 This->pmt = NULL;
658 else
659 CopyMediaType(This->pmt, pmt);
662 if (pReader)
663 IAsyncReader_Release(pReader);
665 if (FAILED(hr))
667 if (This->pOutputPin)
669 IPin_Release(This->pOutputPin);
670 This->pOutputPin = NULL;
673 CoTaskMemFree(This->pszFileName);
674 if (This->pmt)
675 FreeMediaType(This->pmt);
676 This->pszFileName = NULL;
677 This->pmt = NULL;
679 CloseHandle(hFile);
682 /* FIXME: check return codes */
683 return hr;
686 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
688 AsyncReader *This = impl_from_IFileSourceFilter(iface);
690 TRACE("(%p, %p)\n", ppszFileName, pmt);
692 if (!ppszFileName)
693 return E_POINTER;
695 /* copy file name & media type if available, otherwise clear the outputs */
696 if (This->pszFileName)
698 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
699 strcpyW(*ppszFileName, This->pszFileName);
701 else
702 *ppszFileName = NULL;
704 if (pmt)
706 if (This->pmt)
707 CopyMediaType(pmt, This->pmt);
708 else
709 ZeroMemory(pmt, sizeof(*pmt));
712 return S_OK;
715 static const IFileSourceFilterVtbl FileSource_Vtbl =
717 FileSource_QueryInterface,
718 FileSource_AddRef,
719 FileSource_Release,
720 FileSource_Load,
721 FileSource_GetCurFile
725 /* the dwUserData passed back to user */
726 typedef struct DATAREQUEST
728 IMediaSample * pSample; /* sample passed to us by user */
729 DWORD_PTR dwUserData; /* user data passed to us */
730 OVERLAPPED ovl; /* our overlapped structure */
731 } DATAREQUEST;
733 typedef struct FileAsyncReader
735 BaseOutputPin pin;
736 IAsyncReader IAsyncReader_iface;
738 ALLOCATOR_PROPERTIES allocProps;
739 HANDLE hFile;
740 BOOL bFlushing;
741 /* Why would you need more? Every sample has its own handle */
742 LONG queued_number;
743 LONG samples;
744 LONG oldest_sample;
745 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
746 DATAREQUEST *sample_list;
748 /* Have a handle for every sample, and then one more as flushing handle */
749 HANDLE *handle_list;
750 } FileAsyncReader;
752 static inline FileAsyncReader *impl_from_IPin(IPin *iface)
754 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin.IPin_iface);
757 static inline FileAsyncReader *impl_from_BasePin(BasePin *iface)
759 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin);
762 static inline FileAsyncReader *impl_from_BaseOutputPin(BaseOutputPin *iface)
764 return CONTAINING_RECORD(iface, FileAsyncReader, pin);
767 static inline BaseOutputPin *impl_BaseOututPin_from_BasePin(BasePin *iface)
769 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
772 static inline FileAsyncReader *impl_from_IAsyncReader(IAsyncReader *iface)
774 return CONTAINING_RECORD(iface, FileAsyncReader, IAsyncReader_iface);
777 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
779 FileAsyncReader *This = impl_from_IPin(iface);
780 AM_MEDIA_TYPE *pmt_filter = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt;
782 FIXME("(%p, %p)\n", iface, pmt);
784 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
785 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
786 IsEqualGUID(&pmt->formattype, &FORMAT_None))
787 return S_OK;
789 return S_FALSE;
792 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
794 FileAsyncReader *This = impl_from_BasePin(iface);
795 if (iPosition < 0)
796 return E_INVALIDARG;
797 if (iPosition > 0)
798 return VFW_S_NO_MORE_ITEMS;
799 CopyMediaType(pmt, impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt);
800 return S_OK;
803 /* overridden pin functions */
805 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
807 FileAsyncReader *This = impl_from_IPin(iface);
808 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
810 *ppv = NULL;
812 if (IsEqualIID(riid, &IID_IUnknown))
813 *ppv = This;
814 else if (IsEqualIID(riid, &IID_IPin))
815 *ppv = This;
816 else if (IsEqualIID(riid, &IID_IAsyncReader))
817 *ppv = &This->IAsyncReader_iface;
819 if (*ppv)
821 IUnknown_AddRef((IUnknown *)(*ppv));
822 return S_OK;
825 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
826 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
828 return E_NOINTERFACE;
831 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
833 FileAsyncReader *This = impl_from_IPin(iface);
834 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
835 int x;
837 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
839 if (!refCount)
841 CoTaskMemFree(This->sample_list);
842 if (This->handle_list)
844 for (x = 0; x <= This->samples; ++x)
845 CloseHandle(This->handle_list[x]);
846 CoTaskMemFree(This->handle_list);
848 CloseHandle(This->hFile);
849 This->csList.DebugInfo->Spare[0] = 0;
850 DeleteCriticalSection(&This->csList);
851 CoTaskMemFree(This);
852 return 0;
854 return refCount;
857 static const IPinVtbl FileAsyncReaderPin_Vtbl =
859 FileAsyncReaderPin_QueryInterface,
860 BasePinImpl_AddRef,
861 FileAsyncReaderPin_Release,
862 BaseOutputPinImpl_Connect,
863 BaseOutputPinImpl_ReceiveConnection,
864 BasePinImpl_Disconnect,
865 BasePinImpl_ConnectedTo,
866 BasePinImpl_ConnectionMediaType,
867 BasePinImpl_QueryPinInfo,
868 BasePinImpl_QueryDirection,
869 BasePinImpl_QueryId,
870 FileAsyncReaderPin_QueryAccept,
871 BasePinImpl_EnumMediaTypes,
872 BasePinImpl_QueryInternalConnections,
873 BaseOutputPinImpl_EndOfStream,
874 BaseOutputPinImpl_BeginFlush,
875 BaseOutputPinImpl_EndFlush,
876 BasePinImpl_NewSegment
879 /* Function called as a helper to IPin_Connect */
880 /* specific AM_MEDIA_TYPE - it cannot be NULL */
881 /* this differs from standard OutputPin_AttemptConnection only in that it
882 * doesn't need the IMemInputPin interface on the receiving pin */
883 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
885 BaseOutputPin *This = impl_BaseOututPin_from_BasePin(iface);
886 HRESULT hr;
888 TRACE("(%p, %p)\n", pReceivePin, pmt);
889 dump_AM_MEDIA_TYPE(pmt);
891 /* FIXME: call queryacceptproc */
893 This->pin.pConnectedTo = pReceivePin;
894 IPin_AddRef(pReceivePin);
895 CopyMediaType(&This->pin.mtCurrent, pmt);
897 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
899 if (FAILED(hr))
901 IPin_Release(This->pin.pConnectedTo);
902 This->pin.pConnectedTo = NULL;
903 FreeMediaType(&This->pin.mtCurrent);
906 TRACE(" -- %x\n", hr);
907 return hr;
910 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
912 FileAsyncReader *This = impl_from_BaseOutputPin(iface);
913 ALLOCATOR_PROPERTIES actual;
915 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
916 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
917 if (ppropInputRequest->cbPrefix)
918 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
919 if (ppropInputRequest->cbBuffer)
920 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
921 if (ppropInputRequest->cBuffers)
922 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
924 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
927 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
929 NULL,
930 FileAsyncReaderPin_AttemptConnection,
931 BasePinImpl_GetMediaTypeVersion,
932 FileAsyncReaderPin_GetMediaType
934 FileAsyncReaderPin_DecideBufferSize,
935 BaseOutputPinImpl_DecideAllocator,
936 BaseOutputPinImpl_BreakConnect
939 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
941 PIN_INFO piOutput;
942 HRESULT hr;
944 *ppPin = NULL;
945 piOutput.dir = PINDIR_OUTPUT;
946 piOutput.pFilter = pBaseFilter;
947 strcpyW(piOutput.achName, wszOutputPinName);
948 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseOutputFuncTable, pCritSec, ppPin);
950 if (SUCCEEDED(hr))
952 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
953 pPinImpl->IAsyncReader_iface.lpVtbl = &FileAsyncReader_Vtbl;
954 pPinImpl->hFile = hFile;
955 pPinImpl->bFlushing = FALSE;
956 pPinImpl->sample_list = NULL;
957 pPinImpl->handle_list = NULL;
958 pPinImpl->queued_number = 0;
959 InitializeCriticalSection(&pPinImpl->csList);
960 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
962 return hr;
965 /* IAsyncReader */
967 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
969 FileAsyncReader *This = impl_from_IAsyncReader(iface);
971 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
974 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
976 FileAsyncReader *This = impl_from_IAsyncReader(iface);
978 return IPin_AddRef(&This->pin.pin.IPin_iface);
981 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
983 FileAsyncReader *This = impl_from_IAsyncReader(iface);
985 return IPin_Release(&This->pin.pin.IPin_iface);
988 #define DEF_ALIGNMENT 1
990 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
992 FileAsyncReader *This = impl_from_IAsyncReader(iface);
994 HRESULT hr = S_OK;
996 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
998 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
999 pProps->cbAlign = DEF_ALIGNMENT;
1001 if (pPreferred)
1003 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1004 /* FIXME: check we are still aligned */
1005 if (SUCCEEDED(hr))
1007 IMemAllocator_AddRef(pPreferred);
1008 *ppActual = pPreferred;
1009 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1010 goto done;
1014 pPreferred = NULL;
1016 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1018 if (SUCCEEDED(hr))
1020 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1021 /* FIXME: check we are still aligned */
1022 if (SUCCEEDED(hr))
1024 *ppActual = pPreferred;
1025 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1029 done:
1030 if (SUCCEEDED(hr))
1032 CoTaskMemFree(This->sample_list);
1033 if (This->handle_list)
1035 int x;
1036 for (x = 0; x <= This->samples; ++x)
1037 CloseHandle(This->handle_list[x]);
1038 CoTaskMemFree(This->handle_list);
1041 This->samples = pProps->cBuffers;
1042 This->oldest_sample = 0;
1043 TRACE("Samples: %u\n", This->samples);
1044 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1045 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1047 if (This->sample_list && This->handle_list)
1049 int x;
1050 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1051 for (x = 0; x < This->samples; ++x)
1053 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1054 if (x + 1 < This->samples)
1055 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1057 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1058 This->allocProps = *pProps;
1060 else
1062 hr = E_OUTOFMEMORY;
1063 CoTaskMemFree(This->sample_list);
1064 CoTaskMemFree(This->handle_list);
1065 This->samples = 0;
1066 This->sample_list = NULL;
1067 This->handle_list = NULL;
1071 if (FAILED(hr))
1073 *ppActual = NULL;
1074 if (pPreferred)
1075 IMemAllocator_Release(pPreferred);
1078 TRACE("-- %x\n", hr);
1079 return hr;
1082 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1083 * however, this would be quite complicated to do and may be a bit error prone */
1084 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1086 HRESULT hr = S_OK;
1087 REFERENCE_TIME Start;
1088 REFERENCE_TIME Stop;
1089 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1090 LPBYTE pBuffer = NULL;
1092 TRACE("(%p, %lx)\n", pSample, dwUser);
1094 if (!pSample)
1095 return E_POINTER;
1097 /* get start and stop positions in bytes */
1098 if (SUCCEEDED(hr))
1099 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1101 if (SUCCEEDED(hr))
1102 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1104 EnterCriticalSection(&This->csList);
1105 if (This->bFlushing)
1107 LeaveCriticalSection(&This->csList);
1108 return VFW_E_WRONG_STATE;
1111 if (SUCCEEDED(hr))
1113 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1114 DATAREQUEST *pDataRq;
1115 int x;
1117 /* Try to insert above the waiting sample if possible */
1118 for (x = This->oldest_sample; x < This->samples; ++x)
1120 if (!This->sample_list[x].pSample)
1121 break;
1124 if (x >= This->samples)
1125 for (x = 0; x < This->oldest_sample; ++x)
1127 if (!This->sample_list[x].pSample)
1128 break;
1131 /* There must be a sample we have found */
1132 assert(x < This->samples);
1133 ++This->queued_number;
1135 pDataRq = This->sample_list + x;
1137 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1138 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1139 pDataRq->dwUserData = dwUser;
1141 /* we violate traditional COM rules here by maintaining
1142 * a reference to the sample, but not calling AddRef, but
1143 * that's what MSDN says to do */
1144 pDataRq->pSample = pSample;
1146 /* this is definitely not how it is implemented on Win9x
1147 * as they do not support async reads on files, but it is
1148 * sooo much easier to use this than messing around with threads!
1150 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1151 hr = HRESULT_FROM_WIN32(GetLastError());
1153 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1154 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1155 hr = S_OK;
1158 LeaveCriticalSection(&This->csList);
1160 TRACE("-- %x\n", hr);
1161 return hr;
1164 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1166 HRESULT hr = S_OK;
1167 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1168 DWORD buffer = ~0;
1170 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1172 *ppSample = NULL;
1173 *pdwUser = 0;
1175 EnterCriticalSection(&This->csList);
1176 if (!This->bFlushing)
1178 LONG oldest = This->oldest_sample;
1180 if (!This->queued_number)
1182 /* It could be that nothing is queued right now, but that can be fixed */
1183 WARN("Called without samples in queue and not flushing!!\n");
1185 LeaveCriticalSection(&This->csList);
1187 /* wait for an object to read, or time out */
1188 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1190 EnterCriticalSection(&This->csList);
1191 if (buffer <= This->samples)
1193 /* Re-scale the buffer back to normal */
1194 buffer += oldest;
1196 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1197 if (buffer > This->samples)
1198 buffer -= This->samples + 1;
1199 assert(buffer <= This->samples);
1202 if (buffer >= This->samples)
1204 if (buffer != This->samples)
1206 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1207 hr = VFW_E_TIMEOUT;
1209 else
1210 hr = VFW_E_WRONG_STATE;
1211 buffer = ~0;
1213 else
1214 --This->queued_number;
1217 if (This->bFlushing && buffer == ~0)
1219 for (buffer = 0; buffer < This->samples; ++buffer)
1221 if (This->sample_list[buffer].pSample)
1223 ResetEvent(This->handle_list[buffer]);
1224 break;
1227 if (buffer == This->samples)
1229 assert(!This->queued_number);
1230 hr = VFW_E_TIMEOUT;
1232 else
1234 --This->queued_number;
1235 hr = S_OK;
1239 if (SUCCEEDED(hr))
1241 REFERENCE_TIME rtStart, rtStop;
1242 REFERENCE_TIME rtSampleStart, rtSampleStop;
1243 DATAREQUEST *pDataRq = This->sample_list + buffer;
1244 DWORD dwBytes = 0;
1246 /* get any errors */
1247 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1248 hr = HRESULT_FROM_WIN32(GetLastError());
1250 /* Return the sample no matter what so it can be destroyed */
1251 *ppSample = pDataRq->pSample;
1252 *pdwUser = pDataRq->dwUserData;
1254 if (This->bFlushing)
1255 hr = VFW_E_WRONG_STATE;
1257 if (FAILED(hr))
1258 dwBytes = 0;
1260 /* Set the time on the sample */
1261 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1263 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1264 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1265 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1267 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1268 assert(rtStart == rtSampleStart);
1269 assert(rtStop <= rtSampleStop);
1271 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1272 assert(rtStart == rtSampleStart);
1273 if (hr == S_OK)
1274 assert(rtStop == rtSampleStop);
1275 else
1276 assert(rtStop == rtStart);
1278 This->sample_list[buffer].pSample = NULL;
1279 assert(This->oldest_sample < This->samples);
1281 if (buffer == This->oldest_sample)
1283 LONG x;
1284 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1285 if (This->sample_list[x].pSample)
1286 break;
1287 if (x >= This->samples)
1288 for (x = 0; x < This->oldest_sample; ++x)
1289 if (This->sample_list[x].pSample)
1290 break;
1291 if (This->oldest_sample == x)
1292 /* No samples found, reset to 0 */
1293 x = 0;
1294 This->oldest_sample = x;
1297 LeaveCriticalSection(&This->csList);
1299 TRACE("-- %x\n", hr);
1300 return hr;
1303 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1305 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1307 BYTE * pBuffer;
1308 REFERENCE_TIME tStart;
1309 REFERENCE_TIME tStop;
1310 HRESULT hr;
1312 TRACE("(%p)\n", pSample);
1314 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1316 if (SUCCEEDED(hr))
1317 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1319 if (SUCCEEDED(hr))
1320 hr = FileAsyncReader_SyncRead(iface,
1321 BYTES_FROM_MEDIATIME(tStart),
1322 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1323 pBuffer);
1325 TRACE("-- %x\n", hr);
1326 return hr;
1329 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1331 OVERLAPPED ovl;
1332 HRESULT hr = S_OK;
1333 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1335 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1337 ZeroMemory(&ovl, sizeof(ovl));
1339 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1340 /* NOTE: llPosition is the actual byte position to start reading from */
1341 ovl.u.s.Offset = (DWORD) llPosition;
1342 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1344 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1345 hr = HRESULT_FROM_WIN32(GetLastError());
1347 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1348 hr = S_OK;
1350 if (SUCCEEDED(hr))
1352 DWORD dwBytesRead;
1354 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1355 hr = HRESULT_FROM_WIN32(GetLastError());
1358 CloseHandle(ovl.hEvent);
1360 TRACE("-- %x\n", hr);
1361 return hr;
1364 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1366 DWORD dwSizeLow;
1367 DWORD dwSizeHigh;
1368 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1370 TRACE("(%p, %p)\n", pTotal, pAvailable);
1372 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1373 (GetLastError() != NO_ERROR))
1374 return HRESULT_FROM_WIN32(GetLastError());
1376 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1378 *pAvailable = *pTotal;
1380 return S_OK;
1383 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1385 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1387 TRACE("()\n");
1389 EnterCriticalSection(&This->csList);
1390 This->bFlushing = TRUE;
1391 CancelIo(This->hFile);
1392 SetEvent(This->handle_list[This->samples]);
1393 LeaveCriticalSection(&This->csList);
1395 return S_OK;
1398 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1400 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1401 int x;
1403 TRACE("()\n");
1405 EnterCriticalSection(&This->csList);
1406 ResetEvent(This->handle_list[This->samples]);
1407 This->bFlushing = FALSE;
1408 for (x = 0; x < This->samples; ++x)
1409 assert(!This->sample_list[x].pSample);
1411 LeaveCriticalSection(&This->csList);
1413 return S_OK;
1416 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1418 FileAsyncReader_QueryInterface,
1419 FileAsyncReader_AddRef,
1420 FileAsyncReader_Release,
1421 FileAsyncReader_RequestAllocator,
1422 FileAsyncReader_Request,
1423 FileAsyncReader_WaitForNext,
1424 FileAsyncReader_SyncReadAligned,
1425 FileAsyncReader_SyncRead,
1426 FileAsyncReader_Length,
1427 FileAsyncReader_BeginFlush,
1428 FileAsyncReader_EndFlush,
1432 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1433 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1434 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1437 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1438 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1439 return IUnknown_AddRef((IUnknown*)This);
1442 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1443 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1444 return IUnknown_Release((IUnknown*)This);
1447 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1448 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1451 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1452 AMFilterMiscFlags_QueryInterface,
1453 AMFilterMiscFlags_AddRef,
1454 AMFilterMiscFlags_Release,
1455 AMFilterMiscFlags_GetMiscFlags