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"
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
35 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
37 /* see IAsyncReader::Request on MSDN for the explanation of this */
38 #define BYTES_FROM_MEDIATIME(time) ((time) / 10000000)
40 typedef struct AsyncReader
42 const struct IBaseFilterVtbl * lpVtbl;
43 const struct IFileSourceFilterVtbl * lpVtblFSF;
46 FILTER_INFO filterInfo;
48 CRITICAL_SECTION csFilter;
55 static const struct IBaseFilterVtbl AsyncReader_Vtbl;
56 static const struct IFileSourceFilterVtbl FileSource_Vtbl;
57 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
61 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
62 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
64 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
65 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
67 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
69 /* FIXME: implement */
73 static unsigned char byte_from_hex_char(WCHAR wHex)
75 switch (tolowerW(wHex))
94 return wHex - 'a' + 10;
100 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
110 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
112 /* format: "offset, bytestocompare, mask, value" */
114 ulOffset = strtolW(wszPatternString, NULL, 10);
116 if (!(wszPatternString = strchrW(wszPatternString, ',')))
119 wszPatternString++; /* skip ',' */
121 ulBytes = strtolW(wszPatternString, NULL, 10);
123 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
124 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
125 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
127 /* default mask is match everything */
128 memset(pbMask, 0xFF, ulBytes);
130 if (!(wszPatternString = strchrW(wszPatternString, ',')))
133 wszPatternString++; /* skip ',' */
137 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
140 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
142 if ((strpos % 2) == 1) /* odd numbered position */
143 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
145 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
148 if (!(wszPatternString = strchrW(wszPatternString, ',')))
151 wszPatternString++; /* skip ',' */
156 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
159 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
161 if ((strpos % 2) == 1) /* odd numbered position */
162 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
164 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
169 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
174 for (i = 0; i < ulBytes; i++)
175 if ((pbFile[i] & pbMask[i]) != pbValue[i])
182 HeapFree(GetProcessHeap(), 0, pbMask);
183 HeapFree(GetProcessHeap(), 0, pbValue);
184 HeapFree(GetProcessHeap(), 0, pbFile);
186 /* if we encountered no errors with this string, and there is a following tuple, then we
187 * have to match that as well to succeed */
188 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
189 return process_pattern_string(wszPatternString + 1, pReader);
194 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
196 HKEY hkeyMediaType = NULL;
199 WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
201 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
202 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
204 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType));
210 for (indexMajor = 0; !bFound; indexMajor++)
213 WCHAR wszMajorKeyName[CHARS_IN_GUID];
214 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
215 const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
217 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
219 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
221 TRACE("%s\n", debugstr_w(wszMajorKeyName));
222 if (!strcmpW(wszExtensions, wszMajorKeyName))
224 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
231 for (indexMinor = 0; !bFound; indexMinor++)
234 WCHAR wszMinorKeyName[CHARS_IN_GUID];
235 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
239 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
242 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
245 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
247 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
250 for (indexValue = 0; !bFound; indexValue++)
253 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
254 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
255 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
256 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
257 const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
260 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
262 HeapFree(GetProcessHeap(), 0, wszPatternString);
266 /* if it is not the source filter value */
267 if (strcmpW(wszValueName, wszSourceFilter))
269 if (process_pattern_string(wszPatternString, pReader) == S_OK)
271 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
272 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
276 HeapFree(GetProcessHeap(), 0, wszPatternString);
278 CloseHandle(hkeyMinor);
281 CloseHandle(hkeyMajor);
284 CloseHandle(hkeyMediaType);
286 if (SUCCEEDED(hr) && !bFound)
288 ERR("Media class not found\n");
292 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
297 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
299 AsyncReader * pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
302 return E_OUTOFMEMORY;
304 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
305 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
306 pAsyncRead->refCount = 1;
307 pAsyncRead->filterInfo.achName[0] = '\0';
308 pAsyncRead->filterInfo.pGraph = NULL;
309 pAsyncRead->pOutputPin = NULL;
311 InitializeCriticalSection(&pAsyncRead->csFilter);
313 pAsyncRead->pszFileName = NULL;
314 pAsyncRead->pmt = NULL;
316 *ppv = (LPVOID)pAsyncRead;
318 TRACE("-- created at %p\n", pAsyncRead);
323 /** IUnkown methods **/
325 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
327 ICOM_THIS(AsyncReader, iface);
329 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
333 if (IsEqualIID(riid, &IID_IUnknown))
335 else if (IsEqualIID(riid, &IID_IPersist))
337 else if (IsEqualIID(riid, &IID_IMediaFilter))
339 else if (IsEqualIID(riid, &IID_IBaseFilter))
341 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
342 *ppv = (LPVOID)(&This->lpVtblFSF);
346 IUnknown_AddRef((IUnknown *)(*ppv));
350 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
352 return E_NOINTERFACE;
355 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
357 ICOM_THIS(AsyncReader, iface);
361 return InterlockedIncrement(&This->refCount);
364 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
366 ICOM_THIS(AsyncReader, iface);
370 if (!InterlockedDecrement(&This->refCount))
372 if (This->pOutputPin)
373 IPin_Release(This->pOutputPin);
374 DeleteCriticalSection(&This->csFilter);
380 return This->refCount;
383 /** IPersist methods **/
385 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
387 TRACE("(%p)\n", pClsid);
389 *pClsid = CLSID_AsyncReader;
394 /** IMediaFilter methods **/
396 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
398 ICOM_THIS(AsyncReader, iface);
402 This->state = State_Stopped;
407 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
409 ICOM_THIS(AsyncReader, iface);
413 This->state = State_Paused;
418 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
420 ICOM_THIS(AsyncReader, iface);
422 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
424 This->state = State_Running;
429 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
431 ICOM_THIS(AsyncReader, iface);
433 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
435 *pState = This->state;
440 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
442 /* ICOM_THIS(AsyncReader, iface);*/
444 TRACE("(%p)\n", pClock);
449 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
451 /* ICOM_THIS(AsyncReader, iface);*/
453 TRACE("(%p)\n", ppClock);
458 /** IBaseFilter methods **/
460 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
463 ICOM_THIS(AsyncReader, iface);
465 TRACE("(%p)\n", ppEnum);
467 epd.cPins = This->pOutputPin ? 1 : 0;
468 epd.ppPins = &This->pOutputPin;
469 return IEnumPinsImpl_Construct(&epd, ppEnum);
472 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
474 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
479 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
481 ICOM_THIS(AsyncReader, iface);
483 TRACE("(%p)\n", pInfo);
485 strcpyW(pInfo->achName, This->filterInfo.achName);
486 pInfo->pGraph = This->filterInfo.pGraph;
489 IFilterGraph_AddRef(pInfo->pGraph);
494 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
496 ICOM_THIS(AsyncReader, iface);
498 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
501 strcpyW(This->filterInfo.achName, pName);
503 *This->filterInfo.achName = 0;
504 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
509 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
511 FIXME("(%p)\n", pVendorInfo);
516 static const IBaseFilterVtbl AsyncReader_Vtbl =
518 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
519 AsyncReader_QueryInterface,
522 AsyncReader_GetClassID,
526 AsyncReader_GetState,
527 AsyncReader_SetSyncSource,
528 AsyncReader_GetSyncSource,
529 AsyncReader_EnumPins,
531 AsyncReader_QueryFilterInfo,
532 AsyncReader_JoinFilterGraph,
533 AsyncReader_QueryVendorInfo
536 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
538 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
540 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
543 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
545 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
547 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
550 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
552 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
554 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
557 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
561 IAsyncReader * pReader = NULL;
562 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
564 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
567 /* FIXME: check the sharing values that native uses */
568 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
570 if (hFile == INVALID_HANDLE_VALUE)
572 return HRESULT_FROM_WIN32(GetLastError());
576 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
579 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
581 /* store file name & media type */
584 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
585 strcpyW(This->pszFileName, pszFileName);
586 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
589 This->pmt->bFixedSizeSamples = TRUE;
590 This->pmt->bTemporalCompression = FALSE;
591 This->pmt->cbFormat = 0;
592 This->pmt->pbFormat = NULL;
593 This->pmt->pUnk = NULL;
594 This->pmt->lSampleSize = 0;
595 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
596 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
599 CoTaskMemFree(This->pmt);
604 CopyMediaType(This->pmt, pmt);
608 IAsyncReader_Release(pReader);
612 if (This->pOutputPin)
614 IPin_Release(This->pOutputPin);
615 This->pOutputPin = NULL;
617 if (This->pszFileName)
619 CoTaskMemFree(This->pszFileName);
620 This->pszFileName = NULL;
625 /* FIXME: check return codes */
629 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
631 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
633 TRACE("(%p, %p)\n", ppszFileName, pmt);
635 /* copy file name & media type if available, otherwise clear the outputs */
636 if (This->pszFileName)
638 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
639 strcpyW(*ppszFileName, This->pszFileName);
642 *ppszFileName = NULL;
646 CopyMediaType(pmt, This->pmt);
649 ZeroMemory(pmt, sizeof(*pmt));
654 static const IFileSourceFilterVtbl FileSource_Vtbl =
656 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
657 FileSource_QueryInterface,
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 */
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
686 const struct IAsyncReaderVtbl * lpVtblAR;
691 DATAREQUEST * pHead; /* head of data request list */
692 CRITICAL_SECTION csList; /* critical section to protect operations on list */
695 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
697 ICOM_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))
709 /* overriden pin functions */
711 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
713 ICOM_THIS(FileAsyncReader, iface);
714 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
718 if (IsEqualIID(riid, &IID_IUnknown))
720 else if (IsEqualIID(riid, &IID_IPin))
722 else if (IsEqualIID(riid, &IID_IAsyncReader))
723 *ppv = (LPVOID)&This->lpVtblAR;
727 IUnknown_AddRef((IUnknown *)(*ppv));
731 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
733 return E_NOINTERFACE;
736 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
738 ICOM_THIS(FileAsyncReader, iface);
742 if (!InterlockedDecrement(&This->pin.pin.refCount))
744 DATAREQUEST * pCurrent;
746 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
748 pNext = pCurrent->pNext;
749 CoTaskMemFree(pCurrent);
751 CloseHandle(This->hFile);
752 CloseHandle(This->hEvent);
756 return This->pin.pin.refCount;
759 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
761 ENUMMEDIADETAILS emd;
762 ICOM_THIS(FileAsyncReader, iface);
764 TRACE("(%p)\n", ppEnum);
767 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
769 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
772 static HRESULT WINAPI FileAsyncReaderPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
775 ICOM_THIS(OutputPin, iface);
777 TRACE("(%p, %p)\n", pReceivePin, pmt);
778 dump_AM_MEDIA_TYPE(pmt);
780 /* If we try to connect to ourself, we will definitely deadlock.
781 * There are other cases where we could deadlock too, but this
782 * catches the obvious case */
783 assert(pReceivePin != iface);
785 EnterCriticalSection(This->pin.pCritSec);
787 /* if we have been a specific type to connect with, then we can either connect
788 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
789 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
790 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
793 /* negotiate media type */
795 IEnumMediaTypes * pEnumCandidates;
796 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
798 if (SUCCEEDED(IPin_EnumMediaTypes(iface, &pEnumCandidates)))
800 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
802 /* try this filter's media types first */
803 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
805 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
806 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
809 CoTaskMemFree(pmtCandidate);
812 CoTaskMemFree(pmtCandidate);
814 IEnumMediaTypes_Release(pEnumCandidates);
817 /* then try receiver filter's media types */
818 if (hr != S_OK && SUCCEEDED(IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
820 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
822 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
823 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
826 CoTaskMemFree(pmtCandidate);
829 CoTaskMemFree(pmtCandidate);
831 IEnumMediaTypes_Release(pEnumCandidates);
833 } /* if negotiate media type */
835 LeaveCriticalSection(This->pin.pCritSec);
837 TRACE("-- %lx\n", hr);
841 static const IPinVtbl FileAsyncReaderPin_Vtbl =
843 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
844 FileAsyncReaderPin_QueryInterface,
846 FileAsyncReaderPin_Release,
847 FileAsyncReaderPin_Connect,
848 OutputPin_ReceiveConnection,
850 IPinImpl_ConnectedTo,
851 IPinImpl_ConnectionMediaType,
852 IPinImpl_QueryPinInfo,
853 IPinImpl_QueryDirection,
855 IPinImpl_QueryAccept,
856 FileAsyncReaderPin_EnumMediaTypes,
857 IPinImpl_QueryInternalConnections,
858 OutputPin_EndOfStream,
859 OutputPin_BeginFlush,
864 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
866 FileAsyncReader * pPinImpl;
871 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
874 return E_OUTOFMEMORY;
876 piOutput.dir = PINDIR_OUTPUT;
877 piOutput.pFilter = pBaseFilter;
878 strcpyW(piOutput.achName, wszOutputPinName);
880 if (SUCCEEDED(OutputPin_Init(&piOutput, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
882 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
883 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
884 pPinImpl->hFile = hFile;
885 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
886 pPinImpl->bFlushing = FALSE;
887 pPinImpl->pHead = NULL;
889 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
897 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
899 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
901 return IPin_QueryInterface((IPin *)This, riid, ppv);
904 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
906 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
908 return IPin_AddRef((IPin *)This);
911 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
913 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
915 return IPin_Release((IPin *)This);
918 #define DEF_ALIGNMENT 1
920 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
924 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
926 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
927 pProps->cbAlign = DEF_ALIGNMENT;
931 ALLOCATOR_PROPERTIES PropsActual;
932 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
933 /* FIXME: check we are still aligned */
936 IMemAllocator_AddRef(pPreferred);
937 *ppActual = pPreferred;
938 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
945 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
949 ALLOCATOR_PROPERTIES PropsActual;
950 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
951 /* FIXME: check we are still aligned */
954 IMemAllocator_AddRef(pPreferred);
955 *ppActual = pPreferred;
956 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
965 IMemAllocator_Release(pPreferred);
968 TRACE("-- %lx\n", hr);
972 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
973 * however, this would be quite complicated to do and may be a bit error prone */
974 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
976 REFERENCE_TIME Start;
978 DATAREQUEST * pDataRq;
981 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
983 TRACE("(%p, %lx)\n", pSample, dwUser);
985 /* check flushing state */
987 return VFW_E_WRONG_STATE;
989 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
992 /* get start and stop positions in bytes */
994 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
997 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1001 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1003 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1004 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1005 pDataRq->ovl.hEvent = This->hEvent;
1006 pDataRq->dwUserData = dwUser;
1007 pDataRq->pNext = NULL;
1008 /* we violate traditional COM rules here by maintaining
1009 * a reference to the sample, but not calling AddRef, but
1010 * that's what MSDN says to do */
1011 pDataRq->pSample = pSample;
1013 EnterCriticalSection(&This->csList);
1016 /* adds data request to end of list */
1017 queue(This->pHead, pDataRq);
1019 This->pHead = pDataRq;
1021 LeaveCriticalSection(&This->csList);
1023 /* this is definitely not how it is implemented on Win9x
1024 * as they do not support async reads on files, but it is
1025 * sooo much easier to use this than messing around with threads!
1027 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1028 hr = HRESULT_FROM_WIN32(GetLastError());
1030 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1031 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1035 if (FAILED(hr) && pDataRq)
1037 EnterCriticalSection(&This->csList);
1039 DATAREQUEST * pCurrent;
1040 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1041 if (pCurrent->pNext == pDataRq)
1043 pCurrent->pNext = pDataRq->pNext;
1047 LeaveCriticalSection(&This->csList);
1048 CoTaskMemFree(pDataRq);
1051 TRACE("-- %lx\n", hr);
1055 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1058 DATAREQUEST * pDataRq = NULL;
1059 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1061 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1063 /* FIXME: we could do with improving this by waiting for an array of event handles
1064 * and then determining which one finished and removing that from the list, otherwise
1065 * we will end up waiting for longer than we should do, if a later request finishes
1066 * before an earlier one */
1071 /* we return immediately if flushing */
1072 if (This->bFlushing)
1073 hr = VFW_E_WRONG_STATE;
1077 /* wait for the read to finish or timeout */
1078 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1083 EnterCriticalSection(&This->csList);
1085 pDataRq = This->pHead;
1086 if (pDataRq == NULL)
1089 This->pHead = pDataRq->pNext;
1091 LeaveCriticalSection(&This->csList);
1097 /* get any errors */
1098 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, TRUE))
1099 hr = HRESULT_FROM_WIN32(GetLastError());
1104 *ppSample = pDataRq->pSample;
1105 *pdwUser = pDataRq->dwUserData;
1111 /* no need to close event handle since we will close it when the pin is destroyed */
1112 CoTaskMemFree(pDataRq);
1115 TRACE("-- %lx\n", hr);
1119 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1121 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1124 REFERENCE_TIME tStart;
1125 REFERENCE_TIME tStop;
1128 TRACE("(%p)\n", pSample);
1130 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1133 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1136 hr = FileAsyncReader_SyncRead(iface,
1137 BYTES_FROM_MEDIATIME(tStart),
1138 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1141 TRACE("-- %lx\n", hr);
1145 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1149 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1151 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1153 ZeroMemory(&ovl, sizeof(ovl));
1155 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1156 /* NOTE: llPosition is the actual byte position to start reading from */
1157 ovl.Offset = (DWORD) llPosition;
1158 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1160 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1161 hr = HRESULT_FROM_WIN32(GetLastError());
1163 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1170 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1171 hr = HRESULT_FROM_WIN32(GetLastError());
1174 CloseHandle(ovl.hEvent);
1176 TRACE("-- %lx\n", hr);
1180 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1184 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1186 TRACE("(%p, %p)\n", pTotal, pAvailable);
1188 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1189 (GetLastError() != NO_ERROR))
1190 return HRESULT_FROM_WIN32(GetLastError());
1192 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1194 *pAvailable = *pTotal;
1199 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1201 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1205 This->bFlushing = TRUE;
1206 CancelIo(This->hFile);
1207 SetEvent(This->hEvent);
1209 /* FIXME: free list */
1214 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1216 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1220 This->bFlushing = FALSE;
1225 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1227 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1228 FileAsyncReader_QueryInterface,
1229 FileAsyncReader_AddRef,
1230 FileAsyncReader_Release,
1231 FileAsyncReader_RequestAllocator,
1232 FileAsyncReader_Request,
1233 FileAsyncReader_WaitForNext,
1234 FileAsyncReader_SyncReadAligned,
1235 FileAsyncReader_SyncRead,
1236 FileAsyncReader_Length,
1237 FileAsyncReader_BeginFlush,
1238 FileAsyncReader_EndFlush,