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"
35 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
39 typedef struct AsyncReader
41 const IBaseFilterVtbl * lpVtbl;
42 const IFileSourceFilterVtbl * lpVtblFSF;
45 FILTER_INFO filterInfo;
47 CRITICAL_SECTION csFilter;
54 static const IBaseFilterVtbl AsyncReader_Vtbl;
55 static const IFileSourceFilterVtbl FileSource_Vtbl;
56 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
58 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
60 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
62 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
65 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
67 /* FIXME: implement */
71 static unsigned char byte_from_hex_char(WCHAR wHex)
73 switch (tolowerW(wHex))
92 return wHex - 'a' + 10;
98 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
108 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
110 /* format: "offset, bytestocompare, mask, value" */
112 ulOffset = strtolW(wszPatternString, NULL, 10);
114 if (!(wszPatternString = strchrW(wszPatternString, ',')))
117 wszPatternString++; /* skip ',' */
119 ulBytes = strtolW(wszPatternString, NULL, 10);
121 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
122 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
123 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
125 /* default mask is match everything */
126 memset(pbMask, 0xFF, ulBytes);
128 if (!(wszPatternString = strchrW(wszPatternString, ',')))
131 wszPatternString++; /* skip ',' */
135 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
138 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
140 if ((strpos % 2) == 1) /* odd numbered position */
141 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
143 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
146 if (!(wszPatternString = strchrW(wszPatternString, ',')))
149 wszPatternString++; /* skip ',' */
154 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
157 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
159 if ((strpos % 2) == 1) /* odd numbered position */
160 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
162 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
167 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
172 for (i = 0; i < ulBytes; i++)
173 if ((pbFile[i] & pbMask[i]) != pbValue[i])
180 HeapFree(GetProcessHeap(), 0, pbMask);
181 HeapFree(GetProcessHeap(), 0, pbValue);
182 HeapFree(GetProcessHeap(), 0, pbFile);
184 /* if we encountered no errors with this string, and there is a following tuple, then we
185 * have to match that as well to succeed */
186 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
187 return process_pattern_string(wszPatternString + 1, pReader);
192 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
194 HKEY hkeyMediaType = NULL;
197 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
199 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
200 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
202 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType));
208 for (indexMajor = 0; !bFound; indexMajor++)
211 WCHAR wszMajorKeyName[CHARS_IN_GUID];
212 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
213 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
215 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
217 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
219 TRACE("%s\n", debugstr_w(wszMajorKeyName));
220 if (!strcmpW(wszExtensions, wszMajorKeyName))
222 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
229 for (indexMinor = 0; !bFound; indexMinor++)
232 WCHAR wszMinorKeyName[CHARS_IN_GUID];
233 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
237 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
240 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
243 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
245 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
248 for (indexValue = 0; !bFound; indexValue++)
251 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
252 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
253 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
254 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
255 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
258 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
260 HeapFree(GetProcessHeap(), 0, wszPatternString);
264 /* if it is not the source filter value */
265 if (strcmpW(wszValueName, wszSourceFilter))
267 if (process_pattern_string(wszPatternString, pReader) == S_OK)
269 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
270 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
274 HeapFree(GetProcessHeap(), 0, wszPatternString);
276 CloseHandle(hkeyMinor);
279 CloseHandle(hkeyMajor);
282 CloseHandle(hkeyMediaType);
284 if (SUCCEEDED(hr) && !bFound)
286 ERR("Media class not found\n");
290 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
295 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
297 AsyncReader *pAsyncRead;
300 return CLASS_E_NOAGGREGATION;
302 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
305 return E_OUTOFMEMORY;
307 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
308 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
309 pAsyncRead->refCount = 1;
310 pAsyncRead->filterInfo.achName[0] = '\0';
311 pAsyncRead->filterInfo.pGraph = NULL;
312 pAsyncRead->pOutputPin = NULL;
314 InitializeCriticalSection(&pAsyncRead->csFilter);
316 pAsyncRead->pszFileName = NULL;
317 pAsyncRead->pmt = NULL;
319 *ppv = (LPVOID)pAsyncRead;
321 TRACE("-- created at %p\n", pAsyncRead);
326 /** IUnknown methods **/
328 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
330 AsyncReader *This = (AsyncReader *)iface;
332 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
336 if (IsEqualIID(riid, &IID_IUnknown))
338 else if (IsEqualIID(riid, &IID_IPersist))
340 else if (IsEqualIID(riid, &IID_IMediaFilter))
342 else if (IsEqualIID(riid, &IID_IBaseFilter))
344 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
345 *ppv = (LPVOID)(&This->lpVtblFSF);
349 IUnknown_AddRef((IUnknown *)(*ppv));
353 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
355 return E_NOINTERFACE;
358 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
360 AsyncReader *This = (AsyncReader *)iface;
361 ULONG refCount = InterlockedIncrement(&This->refCount);
363 TRACE("(%p/%p)->() AddRef from %ld\n", This, iface, refCount - 1);
368 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
370 AsyncReader *This = (AsyncReader *)iface;
371 ULONG refCount = InterlockedDecrement(&This->refCount);
373 TRACE("(%p/%p)->() Release from %ld\n", This, iface, refCount + 1);
377 if (This->pOutputPin)
378 IPin_Release(This->pOutputPin);
379 DeleteCriticalSection(&This->csFilter);
388 /** IPersist methods **/
390 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
392 TRACE("(%p)\n", pClsid);
394 *pClsid = CLSID_AsyncReader;
399 /** IMediaFilter methods **/
401 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
403 AsyncReader *This = (AsyncReader *)iface;
407 This->state = State_Stopped;
412 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
414 AsyncReader *This = (AsyncReader *)iface;
418 This->state = State_Paused;
423 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
425 AsyncReader *This = (AsyncReader *)iface;
427 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
429 This->state = State_Running;
434 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
436 AsyncReader *This = (AsyncReader *)iface;
438 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
440 *pState = This->state;
445 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
447 /* AsyncReader *This = (AsyncReader *)iface;*/
449 TRACE("(%p)\n", pClock);
454 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
456 /* AsyncReader *This = (AsyncReader *)iface;*/
458 TRACE("(%p)\n", ppClock);
463 /** IBaseFilter methods **/
465 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
468 AsyncReader *This = (AsyncReader *)iface;
470 TRACE("(%p)\n", ppEnum);
472 epd.cPins = This->pOutputPin ? 1 : 0;
473 epd.ppPins = &This->pOutputPin;
474 return IEnumPinsImpl_Construct(&epd, ppEnum);
477 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
479 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
484 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
486 AsyncReader *This = (AsyncReader *)iface;
488 TRACE("(%p)\n", pInfo);
490 strcpyW(pInfo->achName, This->filterInfo.achName);
491 pInfo->pGraph = This->filterInfo.pGraph;
494 IFilterGraph_AddRef(pInfo->pGraph);
499 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
501 AsyncReader *This = (AsyncReader *)iface;
503 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
506 strcpyW(This->filterInfo.achName, pName);
508 *This->filterInfo.achName = 0;
509 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
514 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
516 FIXME("(%p)\n", pVendorInfo);
521 static const IBaseFilterVtbl AsyncReader_Vtbl =
523 AsyncReader_QueryInterface,
526 AsyncReader_GetClassID,
530 AsyncReader_GetState,
531 AsyncReader_SetSyncSource,
532 AsyncReader_GetSyncSource,
533 AsyncReader_EnumPins,
535 AsyncReader_QueryFilterInfo,
536 AsyncReader_JoinFilterGraph,
537 AsyncReader_QueryVendorInfo
540 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
542 AsyncReader *This = impl_from_IFileSourceFilter(iface);
544 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
547 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
549 AsyncReader *This = impl_from_IFileSourceFilter(iface);
551 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
554 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
556 AsyncReader *This = impl_from_IFileSourceFilter(iface);
558 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
561 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
565 IAsyncReader * pReader = NULL;
566 AsyncReader *This = impl_from_IFileSourceFilter(iface);
568 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
571 /* FIXME: check the sharing values that native uses */
572 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
574 if (hFile == INVALID_HANDLE_VALUE)
576 return HRESULT_FROM_WIN32(GetLastError());
580 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
583 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
585 /* store file name & media type */
588 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
589 strcpyW(This->pszFileName, pszFileName);
590 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
593 This->pmt->bFixedSizeSamples = TRUE;
594 This->pmt->bTemporalCompression = FALSE;
595 This->pmt->cbFormat = 0;
596 This->pmt->pbFormat = NULL;
597 This->pmt->pUnk = NULL;
598 This->pmt->lSampleSize = 0;
599 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
600 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
603 CoTaskMemFree(This->pmt);
608 CopyMediaType(This->pmt, pmt);
612 IAsyncReader_Release(pReader);
616 if (This->pOutputPin)
618 IPin_Release(This->pOutputPin);
619 This->pOutputPin = NULL;
621 if (This->pszFileName)
623 CoTaskMemFree(This->pszFileName);
624 This->pszFileName = NULL;
629 /* FIXME: check return codes */
633 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
635 AsyncReader *This = impl_from_IFileSourceFilter(iface);
637 TRACE("(%p, %p)\n", ppszFileName, pmt);
639 /* copy file name & media type if available, otherwise clear the outputs */
640 if (This->pszFileName)
642 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
643 strcpyW(*ppszFileName, This->pszFileName);
646 *ppszFileName = NULL;
650 CopyMediaType(pmt, This->pmt);
653 ZeroMemory(pmt, sizeof(*pmt));
658 static const IFileSourceFilterVtbl FileSource_Vtbl =
660 FileSource_QueryInterface,
664 FileSource_GetCurFile
668 /* the dwUserData passed back to user */
669 typedef struct DATAREQUEST
671 IMediaSample * pSample; /* sample passed to us by user */
672 DWORD_PTR dwUserData; /* user data passed to us */
673 OVERLAPPED ovl; /* our overlapped structure */
675 struct DATAREQUEST * pNext; /* next data request in list */
678 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
680 DATAREQUEST * pCurrent;
681 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
683 pCurrent->pNext = pItem;
686 typedef struct FileAsyncReader
689 const struct IAsyncReaderVtbl * lpVtblAR;
694 DATAREQUEST * pHead; /* head of data request list */
695 CRITICAL_SECTION csList; /* critical section to protect operations on list */
698 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
700 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
703 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
705 AsyncReader *This = (AsyncReader *)iface;
707 FIXME("(%p, %p)\n", iface, pmt);
709 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
710 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
711 IsEqualGUID(&pmt->formattype, &FORMAT_None))
717 /* overriden pin functions */
719 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
721 FileAsyncReader *This = (FileAsyncReader *)iface;
722 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
726 if (IsEqualIID(riid, &IID_IUnknown))
728 else if (IsEqualIID(riid, &IID_IPin))
730 else if (IsEqualIID(riid, &IID_IAsyncReader))
731 *ppv = (LPVOID)&This->lpVtblAR;
735 IUnknown_AddRef((IUnknown *)(*ppv));
739 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
741 return E_NOINTERFACE;
744 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
746 FileAsyncReader *This = (FileAsyncReader *)iface;
747 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
753 DATAREQUEST * pCurrent;
755 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
757 pNext = pCurrent->pNext;
758 CoTaskMemFree(pCurrent);
760 CloseHandle(This->hFile);
761 CloseHandle(This->hEvent);
768 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
770 ENUMMEDIADETAILS emd;
771 FileAsyncReader *This = (FileAsyncReader *)iface;
773 TRACE("(%p)\n", ppEnum);
776 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
778 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
781 static const IPinVtbl FileAsyncReaderPin_Vtbl =
783 FileAsyncReaderPin_QueryInterface,
785 FileAsyncReaderPin_Release,
787 OutputPin_ReceiveConnection,
789 IPinImpl_ConnectedTo,
790 IPinImpl_ConnectionMediaType,
791 IPinImpl_QueryPinInfo,
792 IPinImpl_QueryDirection,
794 IPinImpl_QueryAccept,
795 FileAsyncReaderPin_EnumMediaTypes,
796 IPinImpl_QueryInternalConnections,
797 OutputPin_EndOfStream,
798 OutputPin_BeginFlush,
803 /* Function called as a helper to IPin_Connect */
804 /* specific AM_MEDIA_TYPE - it cannot be NULL */
805 /* this differs from standard OutputPin_ConnectSpecific only in that it
806 * doesn't need the IMemInputPin interface on the receiving pin */
807 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
809 OutputPin *This = (OutputPin *)iface;
812 TRACE("(%p, %p)\n", pReceivePin, pmt);
813 dump_AM_MEDIA_TYPE(pmt);
815 /* FIXME: call queryacceptproc */
817 This->pin.pConnectedTo = pReceivePin;
818 IPin_AddRef(pReceivePin);
819 CopyMediaType(&This->pin.mtCurrent, pmt);
821 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
825 IPin_Release(This->pin.pConnectedTo);
826 This->pin.pConnectedTo = NULL;
827 FreeMediaType(&This->pin.mtCurrent);
830 TRACE(" -- %lx\n", hr);
834 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
836 FileAsyncReader * pPinImpl;
841 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
844 return E_OUTOFMEMORY;
846 piOutput.dir = PINDIR_OUTPUT;
847 piOutput.pFilter = pBaseFilter;
848 strcpyW(piOutput.achName, wszOutputPinName);
850 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
852 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
853 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
854 pPinImpl->hFile = hFile;
855 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
856 pPinImpl->bFlushing = FALSE;
857 pPinImpl->pHead = NULL;
858 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
859 InitializeCriticalSection(&pPinImpl->csList);
861 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
869 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
871 FileAsyncReader *This = impl_from_IAsyncReader(iface);
873 return IPin_QueryInterface((IPin *)This, riid, ppv);
876 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
878 FileAsyncReader *This = impl_from_IAsyncReader(iface);
880 return IPin_AddRef((IPin *)This);
883 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
885 FileAsyncReader *This = impl_from_IAsyncReader(iface);
887 return IPin_Release((IPin *)This);
890 #define DEF_ALIGNMENT 1
892 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
896 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
898 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
899 pProps->cbAlign = DEF_ALIGNMENT;
903 ALLOCATOR_PROPERTIES PropsActual;
904 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
905 /* FIXME: check we are still aligned */
908 IMemAllocator_AddRef(pPreferred);
909 *ppActual = pPreferred;
910 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
917 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
921 ALLOCATOR_PROPERTIES PropsActual;
922 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
923 /* FIXME: check we are still aligned */
926 IMemAllocator_AddRef(pPreferred);
927 *ppActual = pPreferred;
928 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
937 IMemAllocator_Release(pPreferred);
940 TRACE("-- %lx\n", hr);
944 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
945 * however, this would be quite complicated to do and may be a bit error prone */
946 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
948 REFERENCE_TIME Start;
950 DATAREQUEST * pDataRq;
953 FileAsyncReader *This = impl_from_IAsyncReader(iface);
955 TRACE("(%p, %lx)\n", pSample, dwUser);
957 /* check flushing state */
959 return VFW_E_WRONG_STATE;
961 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
964 /* get start and stop positions in bytes */
966 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
969 hr = IMediaSample_GetPointer(pSample, &pBuffer);
973 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
975 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
976 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
977 pDataRq->ovl.hEvent = This->hEvent;
978 pDataRq->dwUserData = dwUser;
979 pDataRq->pNext = NULL;
980 /* we violate traditional COM rules here by maintaining
981 * a reference to the sample, but not calling AddRef, but
982 * that's what MSDN says to do */
983 pDataRq->pSample = pSample;
985 EnterCriticalSection(&This->csList);
988 /* adds data request to end of list */
989 queue(This->pHead, pDataRq);
991 This->pHead = pDataRq;
993 LeaveCriticalSection(&This->csList);
995 /* this is definitely not how it is implemented on Win9x
996 * as they do not support async reads on files, but it is
997 * sooo much easier to use this than messing around with threads!
999 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1000 hr = HRESULT_FROM_WIN32(GetLastError());
1002 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1003 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1007 if (FAILED(hr) && pDataRq)
1009 EnterCriticalSection(&This->csList);
1011 DATAREQUEST * pCurrent;
1012 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1013 if (pCurrent->pNext == pDataRq)
1015 pCurrent->pNext = pDataRq->pNext;
1019 LeaveCriticalSection(&This->csList);
1020 CoTaskMemFree(pDataRq);
1023 TRACE("-- %lx\n", hr);
1027 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1030 DATAREQUEST * pDataRq = NULL;
1031 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1033 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1035 /* FIXME: we could do with improving this by waiting for an array of event handles
1036 * and then determining which one finished and removing that from the list, otherwise
1037 * we will end up waiting for longer than we should do, if a later request finishes
1038 * before an earlier one */
1043 /* we return immediately if flushing */
1044 if (This->bFlushing)
1045 hr = VFW_E_WRONG_STATE;
1049 /* wait for the read to finish or timeout */
1050 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1055 EnterCriticalSection(&This->csList);
1057 pDataRq = This->pHead;
1058 if (pDataRq == NULL)
1061 This->pHead = pDataRq->pNext;
1063 LeaveCriticalSection(&This->csList);
1069 /* get any errors */
1070 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1071 hr = HRESULT_FROM_WIN32(GetLastError());
1076 *ppSample = pDataRq->pSample;
1077 *pdwUser = pDataRq->dwUserData;
1083 /* no need to close event handle since we will close it when the pin is destroyed */
1084 CoTaskMemFree(pDataRq);
1087 TRACE("-- %lx\n", hr);
1091 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1093 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1096 REFERENCE_TIME tStart;
1097 REFERENCE_TIME tStop;
1100 TRACE("(%p)\n", pSample);
1102 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1105 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1108 hr = FileAsyncReader_SyncRead(iface,
1109 BYTES_FROM_MEDIATIME(tStart),
1110 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1113 TRACE("-- %lx\n", hr);
1117 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1121 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1123 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1125 ZeroMemory(&ovl, sizeof(ovl));
1127 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1128 /* NOTE: llPosition is the actual byte position to start reading from */
1129 ovl.u.s.Offset = (DWORD) llPosition;
1130 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1132 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1133 hr = HRESULT_FROM_WIN32(GetLastError());
1135 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1142 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1143 hr = HRESULT_FROM_WIN32(GetLastError());
1146 CloseHandle(ovl.hEvent);
1148 TRACE("-- %lx\n", hr);
1152 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1156 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1158 TRACE("(%p, %p)\n", pTotal, pAvailable);
1160 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1161 (GetLastError() != NO_ERROR))
1162 return HRESULT_FROM_WIN32(GetLastError());
1164 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1166 *pAvailable = *pTotal;
1171 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1173 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1177 This->bFlushing = TRUE;
1178 CancelIo(This->hFile);
1179 SetEvent(This->hEvent);
1181 /* FIXME: free list */
1186 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1188 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1192 This->bFlushing = FALSE;
1197 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1199 FileAsyncReader_QueryInterface,
1200 FileAsyncReader_AddRef,
1201 FileAsyncReader_Release,
1202 FileAsyncReader_RequestAllocator,
1203 FileAsyncReader_Request,
1204 FileAsyncReader_WaitForNext,
1205 FileAsyncReader_SyncReadAligned,
1206 FileAsyncReader_SyncRead,
1207 FileAsyncReader_Length,
1208 FileAsyncReader_BeginFlush,
1209 FileAsyncReader_EndFlush,