Added PARSE_SECURITY_URL action implementation.
[wine.git] / dlls / quartz / filesource.c
blob97df18e71ed9b358865512ad7a472826282ab76c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "ntstatus.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 const IBaseFilterVtbl * lpVtbl;
43 const IFileSourceFilterVtbl * lpVtblFSF;
45 LONG refCount;
46 FILTER_INFO filterInfo;
47 FILTER_STATE state;
48 CRITICAL_SECTION csFilter;
50 IPin * pOutputPin;
51 LPOLESTR pszFileName;
52 AM_MEDIA_TYPE * pmt;
53 } AsyncReader;
55 static const IBaseFilterVtbl AsyncReader_Vtbl;
56 static const IFileSourceFilterVtbl FileSource_Vtbl;
57 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
61 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
63 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
66 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
68 /* FIXME: implement */
69 return E_NOTIMPL;
72 static unsigned char byte_from_hex_char(WCHAR wHex)
74 switch (tolowerW(wHex))
76 case '0':
77 case '1':
78 case '2':
79 case '3':
80 case '4':
81 case '5':
82 case '6':
83 case '7':
84 case '8':
85 case '9':
86 return wHex - '0';
87 case 'a':
88 case 'b':
89 case 'c':
90 case 'd':
91 case 'e':
92 case 'f':
93 return wHex - 'a' + 10;
94 default:
95 return 0;
99 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
101 ULONG ulOffset;
102 ULONG ulBytes;
103 BYTE * pbMask;
104 BYTE * pbValue;
105 BYTE * pbFile;
106 HRESULT hr = S_OK;
107 ULONG strpos;
109 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
111 /* format: "offset, bytestocompare, mask, value" */
113 ulOffset = strtolW(wszPatternString, NULL, 10);
115 if (!(wszPatternString = strchrW(wszPatternString, ',')))
116 return E_INVALIDARG;
118 wszPatternString++; /* skip ',' */
120 ulBytes = strtolW(wszPatternString, NULL, 10);
122 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
123 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
124 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
126 /* default mask is match everything */
127 memset(pbMask, 0xFF, ulBytes);
129 if (!(wszPatternString = strchrW(wszPatternString, ',')))
130 hr = E_INVALIDARG;
132 wszPatternString++; /* skip ',' */
134 if (hr == S_OK)
136 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
139 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
141 if ((strpos % 2) == 1) /* odd numbered position */
142 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
143 else
144 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
147 if (!(wszPatternString = strchrW(wszPatternString, ',')))
148 hr = E_INVALIDARG;
150 wszPatternString++; /* skip ',' */
153 if (hr == S_OK)
155 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
158 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
160 if ((strpos % 2) == 1) /* odd numbered position */
161 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
162 else
163 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
167 if (hr == S_OK)
168 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
170 if (hr == S_OK)
172 ULONG i;
173 for (i = 0; i < ulBytes; i++)
174 if ((pbFile[i] & pbMask[i]) != pbValue[i])
176 hr = S_FALSE;
177 break;
181 HeapFree(GetProcessHeap(), 0, pbMask);
182 HeapFree(GetProcessHeap(), 0, pbValue);
183 HeapFree(GetProcessHeap(), 0, pbFile);
185 /* if we encountered no errors with this string, and there is a following tuple, then we
186 * have to match that as well to succeed */
187 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
188 return process_pattern_string(wszPatternString + 1, pReader);
189 else
190 return hr;
193 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
195 HKEY hkeyMediaType = NULL;
196 HRESULT hr = S_OK;
197 BOOL bFound = FALSE;
198 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
200 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
201 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
203 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType));
205 if (SUCCEEDED(hr))
207 DWORD indexMajor;
209 for (indexMajor = 0; !bFound; indexMajor++)
211 HKEY hkeyMajor;
212 WCHAR wszMajorKeyName[CHARS_IN_GUID];
213 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
214 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
216 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
217 break;
218 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
219 break;
220 TRACE("%s\n", debugstr_w(wszMajorKeyName));
221 if (!strcmpW(wszExtensions, wszMajorKeyName))
223 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
224 bFound = TRUE;
226 else
228 DWORD indexMinor;
230 for (indexMinor = 0; !bFound; indexMinor++)
232 HKEY hkeyMinor;
233 WCHAR wszMinorKeyName[CHARS_IN_GUID];
234 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
235 DWORD maxValueLen;
236 DWORD indexValue;
238 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
239 break;
241 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
242 break;
244 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
246 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
247 break;
249 for (indexValue = 0; !bFound; indexValue++)
251 DWORD dwType;
252 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
253 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
254 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
255 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
256 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
257 LONG temp;
259 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
261 HeapFree(GetProcessHeap(), 0, wszPatternString);
262 break;
265 /* if it is not the source filter value */
266 if (strcmpW(wszValueName, wszSourceFilter))
268 if (process_pattern_string(wszPatternString, pReader) == S_OK)
270 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
271 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
272 bFound = TRUE;
275 HeapFree(GetProcessHeap(), 0, wszPatternString);
277 CloseHandle(hkeyMinor);
280 CloseHandle(hkeyMajor);
283 CloseHandle(hkeyMediaType);
285 if (SUCCEEDED(hr) && !bFound)
287 ERR("Media class not found\n");
288 hr = E_FAIL;
290 else if (bFound)
291 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
293 return hr;
296 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
298 AsyncReader *pAsyncRead;
300 if( pUnkOuter )
301 return CLASS_E_NOAGGREGATION;
303 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
305 if (!pAsyncRead)
306 return E_OUTOFMEMORY;
308 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
309 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
310 pAsyncRead->refCount = 1;
311 pAsyncRead->filterInfo.achName[0] = '\0';
312 pAsyncRead->filterInfo.pGraph = NULL;
313 pAsyncRead->pOutputPin = NULL;
315 InitializeCriticalSection(&pAsyncRead->csFilter);
317 pAsyncRead->pszFileName = NULL;
318 pAsyncRead->pmt = NULL;
320 *ppv = (LPVOID)pAsyncRead;
322 TRACE("-- created at %p\n", pAsyncRead);
324 return S_OK;
327 /** IUnkown methods **/
329 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
331 AsyncReader *This = (AsyncReader *)iface;
333 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
335 *ppv = NULL;
337 if (IsEqualIID(riid, &IID_IUnknown))
338 *ppv = (LPVOID)This;
339 else if (IsEqualIID(riid, &IID_IPersist))
340 *ppv = (LPVOID)This;
341 else if (IsEqualIID(riid, &IID_IMediaFilter))
342 *ppv = (LPVOID)This;
343 else if (IsEqualIID(riid, &IID_IBaseFilter))
344 *ppv = (LPVOID)This;
345 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
346 *ppv = (LPVOID)(&This->lpVtblFSF);
348 if (*ppv)
350 IUnknown_AddRef((IUnknown *)(*ppv));
351 return S_OK;
354 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
356 return E_NOINTERFACE;
359 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
361 AsyncReader *This = (AsyncReader *)iface;
362 ULONG refCount = InterlockedIncrement(&This->refCount);
364 TRACE("(%p/%p)->() AddRef from %ld\n", This, iface, refCount - 1);
366 return refCount;
369 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
371 AsyncReader *This = (AsyncReader *)iface;
372 ULONG refCount = InterlockedDecrement(&This->refCount);
374 TRACE("(%p/%p)->() Release from %ld\n", This, iface, refCount + 1);
376 if (!refCount)
378 if (This->pOutputPin)
379 IPin_Release(This->pOutputPin);
380 DeleteCriticalSection(&This->csFilter);
381 This->lpVtbl = NULL;
382 CoTaskMemFree(This);
383 return 0;
385 else
386 return refCount;
389 /** IPersist methods **/
391 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
393 TRACE("(%p)\n", pClsid);
395 *pClsid = CLSID_AsyncReader;
397 return S_OK;
400 /** IMediaFilter methods **/
402 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
404 AsyncReader *This = (AsyncReader *)iface;
406 TRACE("()\n");
408 This->state = State_Stopped;
410 return S_OK;
413 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
415 AsyncReader *This = (AsyncReader *)iface;
417 TRACE("()\n");
419 This->state = State_Paused;
421 return S_OK;
424 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
426 AsyncReader *This = (AsyncReader *)iface;
428 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
430 This->state = State_Running;
432 return S_OK;
435 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
437 AsyncReader *This = (AsyncReader *)iface;
439 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
441 *pState = This->state;
443 return S_OK;
446 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
448 /* AsyncReader *This = (AsyncReader *)iface;*/
450 TRACE("(%p)\n", pClock);
452 return S_OK;
455 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
457 /* AsyncReader *This = (AsyncReader *)iface;*/
459 TRACE("(%p)\n", ppClock);
461 return S_OK;
464 /** IBaseFilter methods **/
466 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
468 ENUMPINDETAILS epd;
469 AsyncReader *This = (AsyncReader *)iface;
471 TRACE("(%p)\n", ppEnum);
473 epd.cPins = This->pOutputPin ? 1 : 0;
474 epd.ppPins = &This->pOutputPin;
475 return IEnumPinsImpl_Construct(&epd, ppEnum);
478 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
480 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
482 return E_NOTIMPL;
485 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
487 AsyncReader *This = (AsyncReader *)iface;
489 TRACE("(%p)\n", pInfo);
491 strcpyW(pInfo->achName, This->filterInfo.achName);
492 pInfo->pGraph = This->filterInfo.pGraph;
494 if (pInfo->pGraph)
495 IFilterGraph_AddRef(pInfo->pGraph);
497 return S_OK;
500 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
502 AsyncReader *This = (AsyncReader *)iface;
504 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
506 if (pName)
507 strcpyW(This->filterInfo.achName, pName);
508 else
509 *This->filterInfo.achName = 0;
510 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
512 return S_OK;
515 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
517 FIXME("(%p)\n", pVendorInfo);
519 return E_NOTIMPL;
522 static const IBaseFilterVtbl AsyncReader_Vtbl =
524 AsyncReader_QueryInterface,
525 AsyncReader_AddRef,
526 AsyncReader_Release,
527 AsyncReader_GetClassID,
528 AsyncReader_Stop,
529 AsyncReader_Pause,
530 AsyncReader_Run,
531 AsyncReader_GetState,
532 AsyncReader_SetSyncSource,
533 AsyncReader_GetSyncSource,
534 AsyncReader_EnumPins,
535 AsyncReader_FindPin,
536 AsyncReader_QueryFilterInfo,
537 AsyncReader_JoinFilterGraph,
538 AsyncReader_QueryVendorInfo
541 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
543 AsyncReader *This = impl_from_IFileSourceFilter(iface);
545 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
548 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
550 AsyncReader *This = impl_from_IFileSourceFilter(iface);
552 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
555 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
557 AsyncReader *This = impl_from_IFileSourceFilter(iface);
559 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
562 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
564 HRESULT hr;
565 HANDLE hFile;
566 IAsyncReader * pReader = NULL;
567 AsyncReader *This = impl_from_IFileSourceFilter(iface);
569 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
571 /* open file */
572 /* FIXME: check the sharing values that native uses */
573 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
575 if (hFile == INVALID_HANDLE_VALUE)
577 return HRESULT_FROM_WIN32(GetLastError());
580 /* create pin */
581 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
583 if (SUCCEEDED(hr))
584 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
586 /* store file name & media type */
587 if (SUCCEEDED(hr))
589 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
590 strcpyW(This->pszFileName, pszFileName);
591 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
592 if (!pmt)
594 This->pmt->bFixedSizeSamples = TRUE;
595 This->pmt->bTemporalCompression = FALSE;
596 This->pmt->cbFormat = 0;
597 This->pmt->pbFormat = NULL;
598 This->pmt->pUnk = NULL;
599 This->pmt->lSampleSize = 0;
600 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
601 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
602 if (FAILED(hr))
604 CoTaskMemFree(This->pmt);
605 This->pmt = NULL;
608 else
609 CopyMediaType(This->pmt, pmt);
612 if (pReader)
613 IAsyncReader_Release(pReader);
615 if (FAILED(hr))
617 if (This->pOutputPin)
619 IPin_Release(This->pOutputPin);
620 This->pOutputPin = NULL;
622 if (This->pszFileName)
624 CoTaskMemFree(This->pszFileName);
625 This->pszFileName = NULL;
627 CloseHandle(hFile);
630 /* FIXME: check return codes */
631 return hr;
634 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
636 AsyncReader *This = impl_from_IFileSourceFilter(iface);
638 TRACE("(%p, %p)\n", ppszFileName, pmt);
640 /* copy file name & media type if available, otherwise clear the outputs */
641 if (This->pszFileName)
643 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
644 strcpyW(*ppszFileName, This->pszFileName);
646 else
647 *ppszFileName = NULL;
649 if (This->pmt)
651 CopyMediaType(pmt, This->pmt);
653 else
654 ZeroMemory(pmt, sizeof(*pmt));
656 return S_OK;
659 static const IFileSourceFilterVtbl FileSource_Vtbl =
661 FileSource_QueryInterface,
662 FileSource_AddRef,
663 FileSource_Release,
664 FileSource_Load,
665 FileSource_GetCurFile
669 /* the dwUserData passed back to user */
670 typedef struct DATAREQUEST
672 IMediaSample * pSample; /* sample passed to us by user */
673 DWORD_PTR dwUserData; /* user data passed to us */
674 OVERLAPPED ovl; /* our overlapped structure */
676 struct DATAREQUEST * pNext; /* next data request in list */
677 } DATAREQUEST;
679 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
681 DATAREQUEST * pCurrent;
682 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
684 pCurrent->pNext = pItem;
687 typedef struct FileAsyncReader
689 OutputPin pin;
690 const struct IAsyncReaderVtbl * lpVtblAR;
692 HANDLE hFile;
693 HANDLE hEvent;
694 BOOL bFlushing;
695 DATAREQUEST * pHead; /* head of data request list */
696 CRITICAL_SECTION csList; /* critical section to protect operations on list */
697 } FileAsyncReader;
699 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
701 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
704 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
706 AsyncReader *This = (AsyncReader *)iface;
708 FIXME("(%p, %p)\n", iface, pmt);
710 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
711 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
712 IsEqualGUID(&pmt->formattype, &FORMAT_None))
713 return S_OK;
715 return S_FALSE;
718 /* overriden pin functions */
720 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
722 FileAsyncReader *This = (FileAsyncReader *)iface;
723 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
725 *ppv = NULL;
727 if (IsEqualIID(riid, &IID_IUnknown))
728 *ppv = (LPVOID)This;
729 else if (IsEqualIID(riid, &IID_IPin))
730 *ppv = (LPVOID)This;
731 else if (IsEqualIID(riid, &IID_IAsyncReader))
732 *ppv = (LPVOID)&This->lpVtblAR;
734 if (*ppv)
736 IUnknown_AddRef((IUnknown *)(*ppv));
737 return S_OK;
740 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
742 return E_NOINTERFACE;
745 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
747 FileAsyncReader *This = (FileAsyncReader *)iface;
748 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
750 TRACE("()\n");
752 if (!refCount)
754 DATAREQUEST * pCurrent;
755 DATAREQUEST * pNext;
756 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
758 pNext = pCurrent->pNext;
759 CoTaskMemFree(pCurrent);
761 CloseHandle(This->hFile);
762 CloseHandle(This->hEvent);
763 CoTaskMemFree(This);
764 return 0;
766 return refCount;
769 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
771 ENUMMEDIADETAILS emd;
772 FileAsyncReader *This = (FileAsyncReader *)iface;
774 TRACE("(%p)\n", ppEnum);
776 emd.cMediaTypes = 1;
777 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
779 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
782 static const IPinVtbl FileAsyncReaderPin_Vtbl =
784 FileAsyncReaderPin_QueryInterface,
785 IPinImpl_AddRef,
786 FileAsyncReaderPin_Release,
787 OutputPin_Connect,
788 OutputPin_ReceiveConnection,
789 IPinImpl_Disconnect,
790 IPinImpl_ConnectedTo,
791 IPinImpl_ConnectionMediaType,
792 IPinImpl_QueryPinInfo,
793 IPinImpl_QueryDirection,
794 IPinImpl_QueryId,
795 IPinImpl_QueryAccept,
796 FileAsyncReaderPin_EnumMediaTypes,
797 IPinImpl_QueryInternalConnections,
798 OutputPin_EndOfStream,
799 OutputPin_BeginFlush,
800 OutputPin_EndFlush,
801 OutputPin_NewSegment
804 /* Function called as a helper to IPin_Connect */
805 /* specific AM_MEDIA_TYPE - it cannot be NULL */
806 /* this differs from standard OutputPin_ConnectSpecific only in that it
807 * doesn't need the IMemInputPin interface on the receiving pin */
808 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
810 OutputPin *This = (OutputPin *)iface;
811 HRESULT hr;
813 TRACE("(%p, %p)\n", pReceivePin, pmt);
814 dump_AM_MEDIA_TYPE(pmt);
816 /* FIXME: call queryacceptproc */
818 This->pin.pConnectedTo = pReceivePin;
819 IPin_AddRef(pReceivePin);
820 CopyMediaType(&This->pin.mtCurrent, pmt);
822 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
824 if (FAILED(hr))
826 IPin_Release(This->pin.pConnectedTo);
827 This->pin.pConnectedTo = NULL;
828 FreeMediaType(&This->pin.mtCurrent);
831 TRACE(" -- %lx\n", hr);
832 return hr;
835 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
837 FileAsyncReader * pPinImpl;
838 PIN_INFO piOutput;
840 *ppPin = NULL;
842 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
844 if (!pPinImpl)
845 return E_OUTOFMEMORY;
847 piOutput.dir = PINDIR_OUTPUT;
848 piOutput.pFilter = pBaseFilter;
849 strcpyW(piOutput.achName, wszOutputPinName);
851 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
853 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
854 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
855 pPinImpl->hFile = hFile;
856 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
857 pPinImpl->bFlushing = FALSE;
858 pPinImpl->pHead = NULL;
859 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
860 InitializeCriticalSection(&pPinImpl->csList);
862 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
863 return S_OK;
865 return E_FAIL;
868 /* IAsyncReader */
870 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
872 FileAsyncReader *This = impl_from_IAsyncReader(iface);
874 return IPin_QueryInterface((IPin *)This, riid, ppv);
877 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
879 FileAsyncReader *This = impl_from_IAsyncReader(iface);
881 return IPin_AddRef((IPin *)This);
884 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
886 FileAsyncReader *This = impl_from_IAsyncReader(iface);
888 return IPin_Release((IPin *)This);
891 #define DEF_ALIGNMENT 1
893 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
895 HRESULT hr = S_OK;
897 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
899 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
900 pProps->cbAlign = DEF_ALIGNMENT;
902 if (pPreferred)
904 ALLOCATOR_PROPERTIES PropsActual;
905 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
906 /* FIXME: check we are still aligned */
907 if (SUCCEEDED(hr))
909 IMemAllocator_AddRef(pPreferred);
910 *ppActual = pPreferred;
911 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
912 return S_OK;
916 pPreferred = NULL;
918 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
920 if (SUCCEEDED(hr))
922 ALLOCATOR_PROPERTIES PropsActual;
923 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
924 /* FIXME: check we are still aligned */
925 if (SUCCEEDED(hr))
927 IMemAllocator_AddRef(pPreferred);
928 *ppActual = pPreferred;
929 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
930 return S_OK;
934 if (FAILED(hr))
936 *ppActual = NULL;
937 if (pPreferred)
938 IMemAllocator_Release(pPreferred);
941 TRACE("-- %lx\n", hr);
942 return hr;
945 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
946 * however, this would be quite complicated to do and may be a bit error prone */
947 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
949 REFERENCE_TIME Start;
950 REFERENCE_TIME Stop;
951 DATAREQUEST * pDataRq;
952 BYTE * pBuffer;
953 HRESULT hr = S_OK;
954 FileAsyncReader *This = impl_from_IAsyncReader(iface);
956 TRACE("(%p, %lx)\n", pSample, dwUser);
958 /* check flushing state */
959 if (This->bFlushing)
960 return VFW_E_WRONG_STATE;
962 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
963 hr = E_OUTOFMEMORY;
965 /* get start and stop positions in bytes */
966 if (SUCCEEDED(hr))
967 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
969 if (SUCCEEDED(hr))
970 hr = IMediaSample_GetPointer(pSample, &pBuffer);
972 if (SUCCEEDED(hr))
974 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
976 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
977 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
978 pDataRq->ovl.hEvent = This->hEvent;
979 pDataRq->dwUserData = dwUser;
980 pDataRq->pNext = NULL;
981 /* we violate traditional COM rules here by maintaining
982 * a reference to the sample, but not calling AddRef, but
983 * that's what MSDN says to do */
984 pDataRq->pSample = pSample;
986 EnterCriticalSection(&This->csList);
988 if (This->pHead)
989 /* adds data request to end of list */
990 queue(This->pHead, pDataRq);
991 else
992 This->pHead = pDataRq;
994 LeaveCriticalSection(&This->csList);
996 /* this is definitely not how it is implemented on Win9x
997 * as they do not support async reads on files, but it is
998 * sooo much easier to use this than messing around with threads!
1000 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1001 hr = HRESULT_FROM_WIN32(GetLastError());
1003 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1004 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1005 hr = S_OK;
1008 if (FAILED(hr) && pDataRq)
1010 EnterCriticalSection(&This->csList);
1012 DATAREQUEST * pCurrent;
1013 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1014 if (pCurrent->pNext == pDataRq)
1016 pCurrent->pNext = pDataRq->pNext;
1017 break;
1020 LeaveCriticalSection(&This->csList);
1021 CoTaskMemFree(pDataRq);
1024 TRACE("-- %lx\n", hr);
1025 return hr;
1028 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1030 HRESULT hr = S_OK;
1031 DATAREQUEST * pDataRq = NULL;
1032 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1034 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1036 /* FIXME: we could do with improving this by waiting for an array of event handles
1037 * and then determining which one finished and removing that from the list, otherwise
1038 * we will end up waiting for longer than we should do, if a later request finishes
1039 * before an earlier one */
1041 *ppSample = NULL;
1042 *pdwUser = 0;
1044 /* we return immediately if flushing */
1045 if (This->bFlushing)
1046 hr = VFW_E_WRONG_STATE;
1048 if (SUCCEEDED(hr))
1050 /* wait for the read to finish or timeout */
1051 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1052 hr = VFW_E_TIMEOUT;
1054 if (SUCCEEDED(hr))
1056 EnterCriticalSection(&This->csList);
1058 pDataRq = This->pHead;
1059 if (pDataRq == NULL)
1060 hr = E_FAIL;
1061 else
1062 This->pHead = pDataRq->pNext;
1064 LeaveCriticalSection(&This->csList);
1067 if (SUCCEEDED(hr))
1069 DWORD dwBytes;
1070 /* get any errors */
1071 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1072 hr = HRESULT_FROM_WIN32(GetLastError());
1075 if (SUCCEEDED(hr))
1077 *ppSample = pDataRq->pSample;
1078 *pdwUser = pDataRq->dwUserData;
1081 /* clean up */
1082 if (pDataRq)
1084 /* no need to close event handle since we will close it when the pin is destroyed */
1085 CoTaskMemFree(pDataRq);
1088 TRACE("-- %lx\n", hr);
1089 return hr;
1092 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1094 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1096 BYTE * pBuffer;
1097 REFERENCE_TIME tStart;
1098 REFERENCE_TIME tStop;
1099 HRESULT hr;
1101 TRACE("(%p)\n", pSample);
1103 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1105 if (SUCCEEDED(hr))
1106 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1108 if (SUCCEEDED(hr))
1109 hr = FileAsyncReader_SyncRead(iface,
1110 BYTES_FROM_MEDIATIME(tStart),
1111 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1112 pBuffer);
1114 TRACE("-- %lx\n", hr);
1115 return hr;
1118 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1120 OVERLAPPED ovl;
1121 HRESULT hr = S_OK;
1122 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1124 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1126 ZeroMemory(&ovl, sizeof(ovl));
1128 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1129 /* NOTE: llPosition is the actual byte position to start reading from */
1130 ovl.u.s.Offset = (DWORD) llPosition;
1131 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1133 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1134 hr = HRESULT_FROM_WIN32(GetLastError());
1136 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1137 hr = S_OK;
1139 if (SUCCEEDED(hr))
1141 DWORD dwBytesRead;
1143 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1144 hr = HRESULT_FROM_WIN32(GetLastError());
1147 CloseHandle(ovl.hEvent);
1149 TRACE("-- %lx\n", hr);
1150 return hr;
1153 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1155 DWORD dwSizeLow;
1156 DWORD dwSizeHigh;
1157 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1159 TRACE("(%p, %p)\n", pTotal, pAvailable);
1161 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1162 (GetLastError() != NO_ERROR))
1163 return HRESULT_FROM_WIN32(GetLastError());
1165 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1167 *pAvailable = *pTotal;
1169 return S_OK;
1172 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1174 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1176 TRACE("()\n");
1178 This->bFlushing = TRUE;
1179 CancelIo(This->hFile);
1180 SetEvent(This->hEvent);
1182 /* FIXME: free list */
1184 return S_OK;
1187 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1189 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1191 TRACE("()\n");
1193 This->bFlushing = FALSE;
1195 return S_OK;
1198 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1200 FileAsyncReader_QueryInterface,
1201 FileAsyncReader_AddRef,
1202 FileAsyncReader_Release,
1203 FileAsyncReader_RequestAllocator,
1204 FileAsyncReader_Request,
1205 FileAsyncReader_WaitForNext,
1206 FileAsyncReader_SyncReadAligned,
1207 FileAsyncReader_SyncRead,
1208 FileAsyncReader_Length,
1209 FileAsyncReader_BeginFlush,
1210 FileAsyncReader_EndFlush,