Added IClassFactory->CreateInstance aggregation checks.
[wine/dcerpc.git] / dlls / quartz / filesource.c
blob90ef4c91637a345d64b5db7eee2e30a2b2b6d450
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 #include "quartz_private.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25 #include "pin.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "ntstatus.h"
31 #include <assert.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
35 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
37 typedef struct AsyncReader
39 const struct IBaseFilterVtbl * lpVtbl;
40 const struct IFileSourceFilterVtbl * lpVtblFSF;
42 ULONG refCount;
43 FILTER_INFO filterInfo;
44 FILTER_STATE state;
45 CRITICAL_SECTION csFilter;
47 IPin * pOutputPin;
48 LPOLESTR pszFileName;
49 AM_MEDIA_TYPE * pmt;
50 } AsyncReader;
52 static const struct IBaseFilterVtbl AsyncReader_Vtbl;
53 static const struct IFileSourceFilterVtbl FileSource_Vtbl;
54 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl;
56 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
58 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
59 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
61 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
62 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
64 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
66 /* FIXME: implement */
67 return E_NOTIMPL;
70 static unsigned char byte_from_hex_char(WCHAR wHex)
72 switch (tolowerW(wHex))
74 case '0':
75 case '1':
76 case '2':
77 case '3':
78 case '4':
79 case '5':
80 case '6':
81 case '7':
82 case '8':
83 case '9':
84 return wHex - '0';
85 case 'a':
86 case 'b':
87 case 'c':
88 case 'd':
89 case 'e':
90 case 'f':
91 return wHex - 'a' + 10;
92 default:
93 return 0;
97 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
99 ULONG ulOffset;
100 ULONG ulBytes;
101 BYTE * pbMask;
102 BYTE * pbValue;
103 BYTE * pbFile;
104 HRESULT hr = S_OK;
105 ULONG strpos;
107 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
109 /* format: "offset, bytestocompare, mask, value" */
111 ulOffset = strtolW(wszPatternString, NULL, 10);
113 if (!(wszPatternString = strchrW(wszPatternString, ',')))
114 return E_INVALIDARG;
116 wszPatternString++; /* skip ',' */
118 ulBytes = strtolW(wszPatternString, NULL, 10);
120 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
121 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
122 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
124 /* default mask is match everything */
125 memset(pbMask, 0xFF, ulBytes);
127 if (!(wszPatternString = strchrW(wszPatternString, ',')))
128 hr = E_INVALIDARG;
130 wszPatternString++; /* skip ',' */
132 if (hr == S_OK)
134 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
137 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
139 if ((strpos % 2) == 1) /* odd numbered position */
140 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
141 else
142 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
145 if (!(wszPatternString = strchrW(wszPatternString, ',')))
146 hr = E_INVALIDARG;
148 wszPatternString++; /* skip ',' */
151 if (hr == S_OK)
153 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
156 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
158 if ((strpos % 2) == 1) /* odd numbered position */
159 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
160 else
161 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
165 if (hr == S_OK)
166 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
168 if (hr == S_OK)
170 ULONG i;
171 for (i = 0; i < ulBytes; i++)
172 if ((pbFile[i] & pbMask[i]) != pbValue[i])
174 hr = S_FALSE;
175 break;
179 HeapFree(GetProcessHeap(), 0, pbMask);
180 HeapFree(GetProcessHeap(), 0, pbValue);
181 HeapFree(GetProcessHeap(), 0, pbFile);
183 /* if we encountered no errors with this string, and there is a following tuple, then we
184 * have to match that as well to succeed */
185 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
186 return process_pattern_string(wszPatternString + 1, pReader);
187 else
188 return hr;
191 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
193 HKEY hkeyMediaType = NULL;
194 HRESULT hr = S_OK;
195 BOOL bFound = FALSE;
196 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
198 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
199 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
201 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType));
203 if (SUCCEEDED(hr))
205 DWORD indexMajor;
207 for (indexMajor = 0; !bFound; indexMajor++)
209 HKEY hkeyMajor;
210 WCHAR wszMajorKeyName[CHARS_IN_GUID];
211 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
212 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
214 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
215 break;
216 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
217 break;
218 TRACE("%s\n", debugstr_w(wszMajorKeyName));
219 if (!strcmpW(wszExtensions, wszMajorKeyName))
221 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
222 bFound = TRUE;
224 else
226 DWORD indexMinor;
228 for (indexMinor = 0; !bFound; indexMinor++)
230 HKEY hkeyMinor;
231 WCHAR wszMinorKeyName[CHARS_IN_GUID];
232 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
233 DWORD maxValueLen;
234 DWORD indexValue;
236 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
237 break;
239 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
240 break;
242 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
244 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
245 break;
247 for (indexValue = 0; !bFound; indexValue++)
249 DWORD dwType;
250 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
251 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
252 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
253 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
254 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
255 LONG temp;
257 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
259 HeapFree(GetProcessHeap(), 0, wszPatternString);
260 break;
263 /* if it is not the source filter value */
264 if (strcmpW(wszValueName, wszSourceFilter))
266 if (process_pattern_string(wszPatternString, pReader) == S_OK)
268 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
269 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
270 bFound = TRUE;
273 HeapFree(GetProcessHeap(), 0, wszPatternString);
275 CloseHandle(hkeyMinor);
278 CloseHandle(hkeyMajor);
281 CloseHandle(hkeyMediaType);
283 if (SUCCEEDED(hr) && !bFound)
285 ERR("Media class not found\n");
286 hr = S_FALSE;
288 else if (bFound)
289 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
291 return hr;
294 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
296 AsyncReader *pAsyncRead;
298 if( pUnkOuter )
299 return CLASS_E_NOAGGREGATION;
301 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
303 if (!pAsyncRead)
304 return E_OUTOFMEMORY;
306 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
307 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
308 pAsyncRead->refCount = 1;
309 pAsyncRead->filterInfo.achName[0] = '\0';
310 pAsyncRead->filterInfo.pGraph = NULL;
311 pAsyncRead->pOutputPin = NULL;
313 InitializeCriticalSection(&pAsyncRead->csFilter);
315 pAsyncRead->pszFileName = NULL;
316 pAsyncRead->pmt = NULL;
318 *ppv = (LPVOID)pAsyncRead;
320 TRACE("-- created at %p\n", pAsyncRead);
322 return S_OK;
325 /** IUnkown methods **/
327 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
329 AsyncReader *This = (AsyncReader *)iface;
331 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
333 *ppv = NULL;
335 if (IsEqualIID(riid, &IID_IUnknown))
336 *ppv = (LPVOID)This;
337 else if (IsEqualIID(riid, &IID_IPersist))
338 *ppv = (LPVOID)This;
339 else if (IsEqualIID(riid, &IID_IMediaFilter))
340 *ppv = (LPVOID)This;
341 else if (IsEqualIID(riid, &IID_IBaseFilter))
342 *ppv = (LPVOID)This;
343 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
344 *ppv = (LPVOID)(&This->lpVtblFSF);
346 if (*ppv)
348 IUnknown_AddRef((IUnknown *)(*ppv));
349 return S_OK;
352 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
354 return E_NOINTERFACE;
357 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
359 AsyncReader *This = (AsyncReader *)iface;
361 TRACE("()\n");
363 return InterlockedIncrement(&This->refCount);
366 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
368 AsyncReader *This = (AsyncReader *)iface;
370 TRACE("()\n");
372 if (!InterlockedDecrement(&This->refCount))
374 if (This->pOutputPin)
375 IPin_Release(This->pOutputPin);
376 DeleteCriticalSection(&This->csFilter);
377 This->lpVtbl = NULL;
378 CoTaskMemFree(This);
379 return 0;
381 else
382 return This->refCount;
385 /** IPersist methods **/
387 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
389 TRACE("(%p)\n", pClsid);
391 *pClsid = CLSID_AsyncReader;
393 return S_OK;
396 /** IMediaFilter methods **/
398 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
400 AsyncReader *This = (AsyncReader *)iface;
402 TRACE("()\n");
404 This->state = State_Stopped;
406 return S_OK;
409 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
411 AsyncReader *This = (AsyncReader *)iface;
413 TRACE("()\n");
415 This->state = State_Paused;
417 return S_OK;
420 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
422 AsyncReader *This = (AsyncReader *)iface;
424 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
426 This->state = State_Running;
428 return S_OK;
431 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
433 AsyncReader *This = (AsyncReader *)iface;
435 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
437 *pState = This->state;
439 return S_OK;
442 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
444 /* AsyncReader *This = (AsyncReader *)iface;*/
446 TRACE("(%p)\n", pClock);
448 return S_OK;
451 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
453 /* AsyncReader *This = (AsyncReader *)iface;*/
455 TRACE("(%p)\n", ppClock);
457 return S_OK;
460 /** IBaseFilter methods **/
462 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
464 ENUMPINDETAILS epd;
465 AsyncReader *This = (AsyncReader *)iface;
467 TRACE("(%p)\n", ppEnum);
469 epd.cPins = This->pOutputPin ? 1 : 0;
470 epd.ppPins = &This->pOutputPin;
471 return IEnumPinsImpl_Construct(&epd, ppEnum);
474 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
476 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
478 return E_NOTIMPL;
481 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
483 AsyncReader *This = (AsyncReader *)iface;
485 TRACE("(%p)\n", pInfo);
487 strcpyW(pInfo->achName, This->filterInfo.achName);
488 pInfo->pGraph = This->filterInfo.pGraph;
490 if (pInfo->pGraph)
491 IFilterGraph_AddRef(pInfo->pGraph);
493 return S_OK;
496 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
498 AsyncReader *This = (AsyncReader *)iface;
500 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
502 if (pName)
503 strcpyW(This->filterInfo.achName, pName);
504 else
505 *This->filterInfo.achName = 0;
506 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
508 return S_OK;
511 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
513 FIXME("(%p)\n", pVendorInfo);
515 return E_NOTIMPL;
518 static const IBaseFilterVtbl AsyncReader_Vtbl =
520 AsyncReader_QueryInterface,
521 AsyncReader_AddRef,
522 AsyncReader_Release,
523 AsyncReader_GetClassID,
524 AsyncReader_Stop,
525 AsyncReader_Pause,
526 AsyncReader_Run,
527 AsyncReader_GetState,
528 AsyncReader_SetSyncSource,
529 AsyncReader_GetSyncSource,
530 AsyncReader_EnumPins,
531 AsyncReader_FindPin,
532 AsyncReader_QueryFilterInfo,
533 AsyncReader_JoinFilterGraph,
534 AsyncReader_QueryVendorInfo
537 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
539 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
541 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
544 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
546 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
548 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
551 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
553 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
555 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
558 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
560 HRESULT hr;
561 HANDLE hFile;
562 IAsyncReader * pReader = NULL;
563 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
565 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
567 /* open file */
568 /* FIXME: check the sharing values that native uses */
569 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
571 if (hFile == INVALID_HANDLE_VALUE)
573 return HRESULT_FROM_WIN32(GetLastError());
576 /* create pin */
577 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
579 if (SUCCEEDED(hr))
580 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
582 /* store file name & media type */
583 if (SUCCEEDED(hr))
585 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
586 strcpyW(This->pszFileName, pszFileName);
587 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
588 if (!pmt)
590 This->pmt->bFixedSizeSamples = TRUE;
591 This->pmt->bTemporalCompression = FALSE;
592 This->pmt->cbFormat = 0;
593 This->pmt->pbFormat = NULL;
594 This->pmt->pUnk = NULL;
595 This->pmt->lSampleSize = 0;
596 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
597 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
598 if (FAILED(hr))
600 CoTaskMemFree(This->pmt);
601 This->pmt = NULL;
604 else
605 CopyMediaType(This->pmt, pmt);
608 if (pReader)
609 IAsyncReader_Release(pReader);
611 if (FAILED(hr))
613 if (This->pOutputPin)
615 IPin_Release(This->pOutputPin);
616 This->pOutputPin = NULL;
618 if (This->pszFileName)
620 CoTaskMemFree(This->pszFileName);
621 This->pszFileName = NULL;
623 CloseHandle(hFile);
626 /* FIXME: check return codes */
627 return hr;
630 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
632 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
634 TRACE("(%p, %p)\n", ppszFileName, pmt);
636 /* copy file name & media type if available, otherwise clear the outputs */
637 if (This->pszFileName)
639 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
640 strcpyW(*ppszFileName, This->pszFileName);
642 else
643 *ppszFileName = NULL;
645 if (This->pmt)
647 CopyMediaType(pmt, This->pmt);
649 else
650 ZeroMemory(pmt, sizeof(*pmt));
652 return S_OK;
655 static const IFileSourceFilterVtbl FileSource_Vtbl =
657 FileSource_QueryInterface,
658 FileSource_AddRef,
659 FileSource_Release,
660 FileSource_Load,
661 FileSource_GetCurFile
665 /* the dwUserData passed back to user */
666 typedef struct DATAREQUEST
668 IMediaSample * pSample; /* sample passed to us by user */
669 DWORD_PTR dwUserData; /* user data passed to us */
670 OVERLAPPED ovl; /* our overlapped structure */
672 struct DATAREQUEST * pNext; /* next data request in list */
673 } DATAREQUEST;
675 void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
677 DATAREQUEST * pCurrent;
678 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
680 pCurrent->pNext = pItem;
683 typedef struct FileAsyncReader
685 OutputPin pin;
686 const struct IAsyncReaderVtbl * lpVtblAR;
688 HANDLE hFile;
689 HANDLE hEvent;
690 BOOL bFlushing;
691 DATAREQUEST * pHead; /* head of data request list */
692 CRITICAL_SECTION csList; /* critical section to protect operations on list */
693 } FileAsyncReader;
695 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
697 AsyncReader *This = (AsyncReader *)iface;
699 FIXME("(%p, %p)\n", iface, pmt);
701 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
702 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
703 IsEqualGUID(&pmt->formattype, &FORMAT_None))
704 return S_OK;
706 return S_FALSE;
709 /* overriden pin functions */
711 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
713 FileAsyncReader *This = (FileAsyncReader *)iface;
714 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
716 *ppv = NULL;
718 if (IsEqualIID(riid, &IID_IUnknown))
719 *ppv = (LPVOID)This;
720 else if (IsEqualIID(riid, &IID_IPin))
721 *ppv = (LPVOID)This;
722 else if (IsEqualIID(riid, &IID_IAsyncReader))
723 *ppv = (LPVOID)&This->lpVtblAR;
725 if (*ppv)
727 IUnknown_AddRef((IUnknown *)(*ppv));
728 return S_OK;
731 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
733 return E_NOINTERFACE;
736 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
738 FileAsyncReader *This = (FileAsyncReader *)iface;
740 TRACE("()\n");
742 if (!InterlockedDecrement(&This->pin.pin.refCount))
744 DATAREQUEST * pCurrent;
745 DATAREQUEST * pNext;
746 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
748 pNext = pCurrent->pNext;
749 CoTaskMemFree(pCurrent);
751 CloseHandle(This->hFile);
752 CloseHandle(This->hEvent);
753 CoTaskMemFree(This);
754 return 0;
756 return This->pin.pin.refCount;
759 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
761 ENUMMEDIADETAILS emd;
762 FileAsyncReader *This = (FileAsyncReader *)iface;
764 TRACE("(%p)\n", ppEnum);
766 emd.cMediaTypes = 1;
767 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
769 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
772 static const IPinVtbl FileAsyncReaderPin_Vtbl =
774 FileAsyncReaderPin_QueryInterface,
775 IPinImpl_AddRef,
776 FileAsyncReaderPin_Release,
777 OutputPin_Connect,
778 OutputPin_ReceiveConnection,
779 IPinImpl_Disconnect,
780 IPinImpl_ConnectedTo,
781 IPinImpl_ConnectionMediaType,
782 IPinImpl_QueryPinInfo,
783 IPinImpl_QueryDirection,
784 IPinImpl_QueryId,
785 IPinImpl_QueryAccept,
786 FileAsyncReaderPin_EnumMediaTypes,
787 IPinImpl_QueryInternalConnections,
788 OutputPin_EndOfStream,
789 OutputPin_BeginFlush,
790 OutputPin_EndFlush,
791 OutputPin_NewSegment
794 /* Function called as a helper to IPin_Connect */
795 /* specific AM_MEDIA_TYPE - it cannot be NULL */
796 /* this differs from standard OutputPin_ConnectSpecific only in that it
797 * doesn't need the IMemInputPin interface on the receiving pin */
798 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
800 OutputPin *This = (OutputPin *)iface;
801 HRESULT hr;
803 TRACE("(%p, %p)\n", pReceivePin, pmt);
804 dump_AM_MEDIA_TYPE(pmt);
806 /* FIXME: call queryacceptproc */
808 This->pin.pConnectedTo = pReceivePin;
809 IPin_AddRef(pReceivePin);
810 CopyMediaType(&This->pin.mtCurrent, pmt);
812 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
814 if (FAILED(hr))
816 IPin_Release(This->pin.pConnectedTo);
817 This->pin.pConnectedTo = NULL;
818 DeleteMediaType(&This->pin.mtCurrent);
821 TRACE(" -- %lx\n", hr);
822 return hr;
825 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
827 FileAsyncReader * pPinImpl;
828 PIN_INFO piOutput;
830 *ppPin = NULL;
832 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
834 if (!pPinImpl)
835 return E_OUTOFMEMORY;
837 piOutput.dir = PINDIR_OUTPUT;
838 piOutput.pFilter = pBaseFilter;
839 strcpyW(piOutput.achName, wszOutputPinName);
841 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
843 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
844 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
845 pPinImpl->hFile = hFile;
846 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
847 pPinImpl->bFlushing = FALSE;
848 pPinImpl->pHead = NULL;
849 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
850 InitializeCriticalSection(&pPinImpl->csList);
852 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
853 return S_OK;
855 return E_FAIL;
858 /* IAsyncReader */
860 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
862 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
864 return IPin_QueryInterface((IPin *)This, riid, ppv);
867 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
869 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
871 return IPin_AddRef((IPin *)This);
874 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
876 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
878 return IPin_Release((IPin *)This);
881 #define DEF_ALIGNMENT 1
883 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
885 HRESULT hr = S_OK;
887 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
889 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
890 pProps->cbAlign = DEF_ALIGNMENT;
892 if (pPreferred)
894 ALLOCATOR_PROPERTIES PropsActual;
895 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
896 /* FIXME: check we are still aligned */
897 if (SUCCEEDED(hr))
899 IMemAllocator_AddRef(pPreferred);
900 *ppActual = pPreferred;
901 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
902 return S_OK;
906 pPreferred = NULL;
908 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
910 if (SUCCEEDED(hr))
912 ALLOCATOR_PROPERTIES PropsActual;
913 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
914 /* FIXME: check we are still aligned */
915 if (SUCCEEDED(hr))
917 IMemAllocator_AddRef(pPreferred);
918 *ppActual = pPreferred;
919 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
920 return S_OK;
924 if (FAILED(hr))
926 *ppActual = NULL;
927 if (pPreferred)
928 IMemAllocator_Release(pPreferred);
931 TRACE("-- %lx\n", hr);
932 return hr;
935 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
936 * however, this would be quite complicated to do and may be a bit error prone */
937 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
939 REFERENCE_TIME Start;
940 REFERENCE_TIME Stop;
941 DATAREQUEST * pDataRq;
942 BYTE * pBuffer;
943 HRESULT hr = S_OK;
944 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
946 TRACE("(%p, %lx)\n", pSample, dwUser);
948 /* check flushing state */
949 if (This->bFlushing)
950 return VFW_E_WRONG_STATE;
952 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
953 hr = E_OUTOFMEMORY;
955 /* get start and stop positions in bytes */
956 if (SUCCEEDED(hr))
957 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
959 if (SUCCEEDED(hr))
960 hr = IMediaSample_GetPointer(pSample, &pBuffer);
962 if (SUCCEEDED(hr))
964 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
966 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
967 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
968 pDataRq->ovl.hEvent = This->hEvent;
969 pDataRq->dwUserData = dwUser;
970 pDataRq->pNext = NULL;
971 /* we violate traditional COM rules here by maintaining
972 * a reference to the sample, but not calling AddRef, but
973 * that's what MSDN says to do */
974 pDataRq->pSample = pSample;
976 EnterCriticalSection(&This->csList);
978 if (This->pHead)
979 /* adds data request to end of list */
980 queue(This->pHead, pDataRq);
981 else
982 This->pHead = pDataRq;
984 LeaveCriticalSection(&This->csList);
986 /* this is definitely not how it is implemented on Win9x
987 * as they do not support async reads on files, but it is
988 * sooo much easier to use this than messing around with threads!
990 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
991 hr = HRESULT_FROM_WIN32(GetLastError());
993 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
994 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
995 hr = S_OK;
998 if (FAILED(hr) && pDataRq)
1000 EnterCriticalSection(&This->csList);
1002 DATAREQUEST * pCurrent;
1003 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1004 if (pCurrent->pNext == pDataRq)
1006 pCurrent->pNext = pDataRq->pNext;
1007 break;
1010 LeaveCriticalSection(&This->csList);
1011 CoTaskMemFree(pDataRq);
1014 TRACE("-- %lx\n", hr);
1015 return hr;
1018 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1020 HRESULT hr = S_OK;
1021 DATAREQUEST * pDataRq = NULL;
1022 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1024 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1026 /* FIXME: we could do with improving this by waiting for an array of event handles
1027 * and then determining which one finished and removing that from the list, otherwise
1028 * we will end up waiting for longer than we should do, if a later request finishes
1029 * before an earlier one */
1031 *ppSample = NULL;
1032 *pdwUser = 0;
1034 /* we return immediately if flushing */
1035 if (This->bFlushing)
1036 hr = VFW_E_WRONG_STATE;
1038 if (SUCCEEDED(hr))
1040 /* wait for the read to finish or timeout */
1041 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1042 hr = VFW_E_TIMEOUT;
1044 if (SUCCEEDED(hr))
1046 EnterCriticalSection(&This->csList);
1048 pDataRq = This->pHead;
1049 if (pDataRq == NULL)
1050 hr = E_FAIL;
1051 else
1052 This->pHead = pDataRq->pNext;
1054 LeaveCriticalSection(&This->csList);
1057 if (SUCCEEDED(hr))
1059 DWORD dwBytes;
1060 /* get any errors */
1061 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1062 hr = HRESULT_FROM_WIN32(GetLastError());
1065 if (SUCCEEDED(hr))
1067 *ppSample = pDataRq->pSample;
1068 *pdwUser = pDataRq->dwUserData;
1071 /* clean up */
1072 if (pDataRq)
1074 /* no need to close event handle since we will close it when the pin is destroyed */
1075 CoTaskMemFree(pDataRq);
1078 TRACE("-- %lx\n", hr);
1079 return hr;
1082 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1084 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1086 BYTE * pBuffer;
1087 REFERENCE_TIME tStart;
1088 REFERENCE_TIME tStop;
1089 HRESULT hr;
1091 TRACE("(%p)\n", pSample);
1093 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1095 if (SUCCEEDED(hr))
1096 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1098 if (SUCCEEDED(hr))
1099 hr = FileAsyncReader_SyncRead(iface,
1100 BYTES_FROM_MEDIATIME(tStart),
1101 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1102 pBuffer);
1104 TRACE("-- %lx\n", hr);
1105 return hr;
1108 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1110 OVERLAPPED ovl;
1111 HRESULT hr = S_OK;
1112 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1114 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1116 ZeroMemory(&ovl, sizeof(ovl));
1118 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1119 /* NOTE: llPosition is the actual byte position to start reading from */
1120 ovl.Offset = (DWORD) llPosition;
1121 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1123 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1124 hr = HRESULT_FROM_WIN32(GetLastError());
1126 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1127 hr = S_OK;
1129 if (SUCCEEDED(hr))
1131 DWORD dwBytesRead;
1133 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1134 hr = HRESULT_FROM_WIN32(GetLastError());
1137 CloseHandle(ovl.hEvent);
1139 TRACE("-- %lx\n", hr);
1140 return hr;
1143 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1145 DWORD dwSizeLow;
1146 DWORD dwSizeHigh;
1147 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1149 TRACE("(%p, %p)\n", pTotal, pAvailable);
1151 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1152 (GetLastError() != NO_ERROR))
1153 return HRESULT_FROM_WIN32(GetLastError());
1155 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1157 *pAvailable = *pTotal;
1159 return S_OK;
1162 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1164 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1166 TRACE("()\n");
1168 This->bFlushing = TRUE;
1169 CancelIo(This->hFile);
1170 SetEvent(This->hEvent);
1172 /* FIXME: free list */
1174 return S_OK;
1177 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1179 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1181 TRACE("()\n");
1183 This->bFlushing = FALSE;
1185 return S_OK;
1188 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1190 FileAsyncReader_QueryInterface,
1191 FileAsyncReader_AddRef,
1192 FileAsyncReader_Release,
1193 FileAsyncReader_RequestAllocator,
1194 FileAsyncReader_Request,
1195 FileAsyncReader_WaitForNext,
1196 FileAsyncReader_SyncReadAligned,
1197 FileAsyncReader_SyncRead,
1198 FileAsyncReader_Length,
1199 FileAsyncReader_BeginFlush,
1200 FileAsyncReader_EndFlush,