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 typedef struct AsyncReader
39 const struct IBaseFilterVtbl * lpVtbl;
40 const struct IFileSourceFilterVtbl * lpVtblFSF;
43 FILTER_INFO filterInfo;
45 CRITICAL_SECTION csFilter;
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 */
70 static unsigned char byte_from_hex_char(WCHAR wHex)
72 switch (tolowerW(wHex))
91 return wHex - 'a' + 10;
97 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
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, ',')))
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, ',')))
130 wszPatternString++; /* skip ',' */
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);
142 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
145 if (!(wszPatternString = strchrW(wszPatternString, ',')))
148 wszPatternString++; /* skip ',' */
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);
161 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
166 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
171 for (i = 0; i < ulBytes; i++)
172 if ((pbFile[i] & pbMask[i]) != pbValue[i])
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);
191 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
193 HKEY hkeyMediaType = NULL;
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));
207 for (indexMajor = 0; !bFound; indexMajor++)
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)
216 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
218 TRACE("%s\n", debugstr_w(wszMajorKeyName));
219 if (!strcmpW(wszExtensions, wszMajorKeyName))
221 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
228 for (indexMinor = 0; !bFound; indexMinor++)
231 WCHAR wszMinorKeyName[CHARS_IN_GUID];
232 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
236 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
239 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
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)
247 for (indexValue = 0; !bFound; indexValue++)
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};
257 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
259 HeapFree(GetProcessHeap(), 0, wszPatternString);
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)))
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");
289 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
294 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
296 AsyncReader *pAsyncRead;
299 return CLASS_E_NOAGGREGATION;
301 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
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);
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);
335 if (IsEqualIID(riid, &IID_IUnknown))
337 else if (IsEqualIID(riid, &IID_IPersist))
339 else if (IsEqualIID(riid, &IID_IMediaFilter))
341 else if (IsEqualIID(riid, &IID_IBaseFilter))
343 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
344 *ppv = (LPVOID)(&This->lpVtblFSF);
348 IUnknown_AddRef((IUnknown *)(*ppv));
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;
360 ULONG refCount = InterlockedIncrement(&This->refCount);
362 TRACE("(%p/%p)->() AddRef from %ld\n", This, iface, refCount - 1);
367 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
369 AsyncReader *This = (AsyncReader *)iface;
370 ULONG refCount = InterlockedDecrement(&This->refCount);
372 TRACE("(%p/%p)->() Release from %ld\n", This, iface, refCount + 1);
376 if (This->pOutputPin)
377 IPin_Release(This->pOutputPin);
378 DeleteCriticalSection(&This->csFilter);
387 /** IPersist methods **/
389 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
391 TRACE("(%p)\n", pClsid);
393 *pClsid = CLSID_AsyncReader;
398 /** IMediaFilter methods **/
400 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
402 AsyncReader *This = (AsyncReader *)iface;
406 This->state = State_Stopped;
411 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
413 AsyncReader *This = (AsyncReader *)iface;
417 This->state = State_Paused;
422 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
424 AsyncReader *This = (AsyncReader *)iface;
426 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
428 This->state = State_Running;
433 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
435 AsyncReader *This = (AsyncReader *)iface;
437 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
439 *pState = This->state;
444 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
446 /* AsyncReader *This = (AsyncReader *)iface;*/
448 TRACE("(%p)\n", pClock);
453 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
455 /* AsyncReader *This = (AsyncReader *)iface;*/
457 TRACE("(%p)\n", ppClock);
462 /** IBaseFilter methods **/
464 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
467 AsyncReader *This = (AsyncReader *)iface;
469 TRACE("(%p)\n", ppEnum);
471 epd.cPins = This->pOutputPin ? 1 : 0;
472 epd.ppPins = &This->pOutputPin;
473 return IEnumPinsImpl_Construct(&epd, ppEnum);
476 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
478 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
483 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
485 AsyncReader *This = (AsyncReader *)iface;
487 TRACE("(%p)\n", pInfo);
489 strcpyW(pInfo->achName, This->filterInfo.achName);
490 pInfo->pGraph = This->filterInfo.pGraph;
493 IFilterGraph_AddRef(pInfo->pGraph);
498 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
500 AsyncReader *This = (AsyncReader *)iface;
502 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
505 strcpyW(This->filterInfo.achName, pName);
507 *This->filterInfo.achName = 0;
508 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
513 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
515 FIXME("(%p)\n", pVendorInfo);
520 static const IBaseFilterVtbl AsyncReader_Vtbl =
522 AsyncReader_QueryInterface,
525 AsyncReader_GetClassID,
529 AsyncReader_GetState,
530 AsyncReader_SetSyncSource,
531 AsyncReader_GetSyncSource,
532 AsyncReader_EnumPins,
534 AsyncReader_QueryFilterInfo,
535 AsyncReader_JoinFilterGraph,
536 AsyncReader_QueryVendorInfo
539 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
541 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
543 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
546 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
548 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
550 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
553 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
555 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
557 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
560 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
564 IAsyncReader * pReader = NULL;
565 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
567 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
570 /* FIXME: check the sharing values that native uses */
571 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
573 if (hFile == INVALID_HANDLE_VALUE)
575 return HRESULT_FROM_WIN32(GetLastError());
579 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
582 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
584 /* store file name & media type */
587 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
588 strcpyW(This->pszFileName, pszFileName);
589 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
592 This->pmt->bFixedSizeSamples = TRUE;
593 This->pmt->bTemporalCompression = FALSE;
594 This->pmt->cbFormat = 0;
595 This->pmt->pbFormat = NULL;
596 This->pmt->pUnk = NULL;
597 This->pmt->lSampleSize = 0;
598 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
599 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
602 CoTaskMemFree(This->pmt);
607 CopyMediaType(This->pmt, pmt);
611 IAsyncReader_Release(pReader);
615 if (This->pOutputPin)
617 IPin_Release(This->pOutputPin);
618 This->pOutputPin = NULL;
620 if (This->pszFileName)
622 CoTaskMemFree(This->pszFileName);
623 This->pszFileName = NULL;
628 /* FIXME: check return codes */
632 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
634 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
636 TRACE("(%p, %p)\n", ppszFileName, pmt);
638 /* copy file name & media type if available, otherwise clear the outputs */
639 if (This->pszFileName)
641 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
642 strcpyW(*ppszFileName, This->pszFileName);
645 *ppszFileName = NULL;
649 CopyMediaType(pmt, This->pmt);
652 ZeroMemory(pmt, sizeof(*pmt));
657 static const IFileSourceFilterVtbl FileSource_Vtbl =
659 FileSource_QueryInterface,
663 FileSource_GetCurFile
667 /* the dwUserData passed back to user */
668 typedef struct DATAREQUEST
670 IMediaSample * pSample; /* sample passed to us by user */
671 DWORD_PTR dwUserData; /* user data passed to us */
672 OVERLAPPED ovl; /* our overlapped structure */
674 struct DATAREQUEST * pNext; /* next data request in list */
677 void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
679 DATAREQUEST * pCurrent;
680 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
682 pCurrent->pNext = pItem;
685 typedef struct FileAsyncReader
688 const struct IAsyncReaderVtbl * lpVtblAR;
693 DATAREQUEST * pHead; /* head of data request list */
694 CRITICAL_SECTION csList; /* critical section to protect operations on list */
697 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
699 AsyncReader *This = (AsyncReader *)iface;
701 FIXME("(%p, %p)\n", iface, pmt);
703 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
704 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
705 IsEqualGUID(&pmt->formattype, &FORMAT_None))
711 /* overriden pin functions */
713 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
715 FileAsyncReader *This = (FileAsyncReader *)iface;
716 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
720 if (IsEqualIID(riid, &IID_IUnknown))
722 else if (IsEqualIID(riid, &IID_IPin))
724 else if (IsEqualIID(riid, &IID_IAsyncReader))
725 *ppv = (LPVOID)&This->lpVtblAR;
729 IUnknown_AddRef((IUnknown *)(*ppv));
733 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
735 return E_NOINTERFACE;
738 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
740 FileAsyncReader *This = (FileAsyncReader *)iface;
741 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
747 DATAREQUEST * pCurrent;
749 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
751 pNext = pCurrent->pNext;
752 CoTaskMemFree(pCurrent);
754 CloseHandle(This->hFile);
755 CloseHandle(This->hEvent);
762 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
764 ENUMMEDIADETAILS emd;
765 FileAsyncReader *This = (FileAsyncReader *)iface;
767 TRACE("(%p)\n", ppEnum);
770 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
772 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
775 static const IPinVtbl FileAsyncReaderPin_Vtbl =
777 FileAsyncReaderPin_QueryInterface,
779 FileAsyncReaderPin_Release,
781 OutputPin_ReceiveConnection,
783 IPinImpl_ConnectedTo,
784 IPinImpl_ConnectionMediaType,
785 IPinImpl_QueryPinInfo,
786 IPinImpl_QueryDirection,
788 IPinImpl_QueryAccept,
789 FileAsyncReaderPin_EnumMediaTypes,
790 IPinImpl_QueryInternalConnections,
791 OutputPin_EndOfStream,
792 OutputPin_BeginFlush,
797 /* Function called as a helper to IPin_Connect */
798 /* specific AM_MEDIA_TYPE - it cannot be NULL */
799 /* this differs from standard OutputPin_ConnectSpecific only in that it
800 * doesn't need the IMemInputPin interface on the receiving pin */
801 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
803 OutputPin *This = (OutputPin *)iface;
806 TRACE("(%p, %p)\n", pReceivePin, pmt);
807 dump_AM_MEDIA_TYPE(pmt);
809 /* FIXME: call queryacceptproc */
811 This->pin.pConnectedTo = pReceivePin;
812 IPin_AddRef(pReceivePin);
813 CopyMediaType(&This->pin.mtCurrent, pmt);
815 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
819 IPin_Release(This->pin.pConnectedTo);
820 This->pin.pConnectedTo = NULL;
821 DeleteMediaType(&This->pin.mtCurrent);
824 TRACE(" -- %lx\n", hr);
828 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
830 FileAsyncReader * pPinImpl;
835 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
838 return E_OUTOFMEMORY;
840 piOutput.dir = PINDIR_OUTPUT;
841 piOutput.pFilter = pBaseFilter;
842 strcpyW(piOutput.achName, wszOutputPinName);
844 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
846 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
847 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
848 pPinImpl->hFile = hFile;
849 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
850 pPinImpl->bFlushing = FALSE;
851 pPinImpl->pHead = NULL;
852 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
853 InitializeCriticalSection(&pPinImpl->csList);
855 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
863 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
865 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
867 return IPin_QueryInterface((IPin *)This, riid, ppv);
870 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
872 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
874 return IPin_AddRef((IPin *)This);
877 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
879 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
881 return IPin_Release((IPin *)This);
884 #define DEF_ALIGNMENT 1
886 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
890 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
892 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
893 pProps->cbAlign = DEF_ALIGNMENT;
897 ALLOCATOR_PROPERTIES PropsActual;
898 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
899 /* FIXME: check we are still aligned */
902 IMemAllocator_AddRef(pPreferred);
903 *ppActual = pPreferred;
904 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
911 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
915 ALLOCATOR_PROPERTIES PropsActual;
916 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
917 /* FIXME: check we are still aligned */
920 IMemAllocator_AddRef(pPreferred);
921 *ppActual = pPreferred;
922 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
931 IMemAllocator_Release(pPreferred);
934 TRACE("-- %lx\n", hr);
938 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
939 * however, this would be quite complicated to do and may be a bit error prone */
940 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
942 REFERENCE_TIME Start;
944 DATAREQUEST * pDataRq;
947 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
949 TRACE("(%p, %lx)\n", pSample, dwUser);
951 /* check flushing state */
953 return VFW_E_WRONG_STATE;
955 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
958 /* get start and stop positions in bytes */
960 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
963 hr = IMediaSample_GetPointer(pSample, &pBuffer);
967 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
969 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
970 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
971 pDataRq->ovl.hEvent = This->hEvent;
972 pDataRq->dwUserData = dwUser;
973 pDataRq->pNext = NULL;
974 /* we violate traditional COM rules here by maintaining
975 * a reference to the sample, but not calling AddRef, but
976 * that's what MSDN says to do */
977 pDataRq->pSample = pSample;
979 EnterCriticalSection(&This->csList);
982 /* adds data request to end of list */
983 queue(This->pHead, pDataRq);
985 This->pHead = pDataRq;
987 LeaveCriticalSection(&This->csList);
989 /* this is definitely not how it is implemented on Win9x
990 * as they do not support async reads on files, but it is
991 * sooo much easier to use this than messing around with threads!
993 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
994 hr = HRESULT_FROM_WIN32(GetLastError());
996 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
997 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1001 if (FAILED(hr) && pDataRq)
1003 EnterCriticalSection(&This->csList);
1005 DATAREQUEST * pCurrent;
1006 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1007 if (pCurrent->pNext == pDataRq)
1009 pCurrent->pNext = pDataRq->pNext;
1013 LeaveCriticalSection(&This->csList);
1014 CoTaskMemFree(pDataRq);
1017 TRACE("-- %lx\n", hr);
1021 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1024 DATAREQUEST * pDataRq = NULL;
1025 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1027 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1029 /* FIXME: we could do with improving this by waiting for an array of event handles
1030 * and then determining which one finished and removing that from the list, otherwise
1031 * we will end up waiting for longer than we should do, if a later request finishes
1032 * before an earlier one */
1037 /* we return immediately if flushing */
1038 if (This->bFlushing)
1039 hr = VFW_E_WRONG_STATE;
1043 /* wait for the read to finish or timeout */
1044 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1049 EnterCriticalSection(&This->csList);
1051 pDataRq = This->pHead;
1052 if (pDataRq == NULL)
1055 This->pHead = pDataRq->pNext;
1057 LeaveCriticalSection(&This->csList);
1063 /* get any errors */
1064 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1065 hr = HRESULT_FROM_WIN32(GetLastError());
1070 *ppSample = pDataRq->pSample;
1071 *pdwUser = pDataRq->dwUserData;
1077 /* no need to close event handle since we will close it when the pin is destroyed */
1078 CoTaskMemFree(pDataRq);
1081 TRACE("-- %lx\n", hr);
1085 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1087 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1090 REFERENCE_TIME tStart;
1091 REFERENCE_TIME tStop;
1094 TRACE("(%p)\n", pSample);
1096 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1099 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1102 hr = FileAsyncReader_SyncRead(iface,
1103 BYTES_FROM_MEDIATIME(tStart),
1104 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1107 TRACE("-- %lx\n", hr);
1111 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1115 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1117 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1119 ZeroMemory(&ovl, sizeof(ovl));
1121 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1122 /* NOTE: llPosition is the actual byte position to start reading from */
1123 ovl.Offset = (DWORD) llPosition;
1124 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1126 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1127 hr = HRESULT_FROM_WIN32(GetLastError());
1129 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1136 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1137 hr = HRESULT_FROM_WIN32(GetLastError());
1140 CloseHandle(ovl.hEvent);
1142 TRACE("-- %lx\n", hr);
1146 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1150 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1152 TRACE("(%p, %p)\n", pTotal, pAvailable);
1154 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1155 (GetLastError() != NO_ERROR))
1156 return HRESULT_FROM_WIN32(GetLastError());
1158 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1160 *pAvailable = *pTotal;
1165 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1167 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1171 This->bFlushing = TRUE;
1172 CancelIo(This->hFile);
1173 SetEvent(This->hEvent);
1175 /* FIXME: free list */
1180 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1182 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1186 This->bFlushing = FALSE;
1191 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1193 FileAsyncReader_QueryInterface,
1194 FileAsyncReader_AddRef,
1195 FileAsyncReader_Release,
1196 FileAsyncReader_RequestAllocator,
1197 FileAsyncReader_Request,
1198 FileAsyncReader_WaitForNext,
1199 FileAsyncReader_SyncReadAligned,
1200 FileAsyncReader_SyncRead,
1201 FileAsyncReader_Length,
1202 FileAsyncReader_BeginFlush,
1203 FileAsyncReader_EndFlush,