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 = CoTaskMemAlloc(sizeof(AsyncReader));
299 return E_OUTOFMEMORY;
301 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
302 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
303 pAsyncRead->refCount = 1;
304 pAsyncRead->filterInfo.achName[0] = '\0';
305 pAsyncRead->filterInfo.pGraph = NULL;
306 pAsyncRead->pOutputPin = NULL;
308 InitializeCriticalSection(&pAsyncRead->csFilter);
310 pAsyncRead->pszFileName = NULL;
311 pAsyncRead->pmt = NULL;
313 *ppv = (LPVOID)pAsyncRead;
315 TRACE("-- created at %p\n", pAsyncRead);
320 /** IUnkown methods **/
322 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
324 ICOM_THIS(AsyncReader, iface);
326 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
330 if (IsEqualIID(riid, &IID_IUnknown))
332 else if (IsEqualIID(riid, &IID_IPersist))
334 else if (IsEqualIID(riid, &IID_IMediaFilter))
336 else if (IsEqualIID(riid, &IID_IBaseFilter))
338 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
339 *ppv = (LPVOID)(&This->lpVtblFSF);
343 IUnknown_AddRef((IUnknown *)(*ppv));
347 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
349 return E_NOINTERFACE;
352 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
354 ICOM_THIS(AsyncReader, iface);
358 return InterlockedIncrement(&This->refCount);
361 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
363 ICOM_THIS(AsyncReader, iface);
367 if (!InterlockedDecrement(&This->refCount))
369 if (This->pOutputPin)
370 IPin_Release(This->pOutputPin);
371 DeleteCriticalSection(&This->csFilter);
377 return This->refCount;
380 /** IPersist methods **/
382 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
384 TRACE("(%p)\n", pClsid);
386 *pClsid = CLSID_AsyncReader;
391 /** IMediaFilter methods **/
393 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
395 ICOM_THIS(AsyncReader, iface);
399 This->state = State_Stopped;
404 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
406 ICOM_THIS(AsyncReader, iface);
410 This->state = State_Paused;
415 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
417 ICOM_THIS(AsyncReader, iface);
419 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
421 This->state = State_Running;
426 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
428 ICOM_THIS(AsyncReader, iface);
430 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
432 *pState = This->state;
437 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
439 /* ICOM_THIS(AsyncReader, iface);*/
441 TRACE("(%p)\n", pClock);
446 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
448 /* ICOM_THIS(AsyncReader, iface);*/
450 TRACE("(%p)\n", ppClock);
455 /** IBaseFilter methods **/
457 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
460 ICOM_THIS(AsyncReader, iface);
462 TRACE("(%p)\n", ppEnum);
464 epd.cPins = This->pOutputPin ? 1 : 0;
465 epd.ppPins = &This->pOutputPin;
466 return IEnumPinsImpl_Construct(&epd, ppEnum);
469 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
471 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
476 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
478 ICOM_THIS(AsyncReader, iface);
480 TRACE("(%p)\n", pInfo);
482 strcpyW(pInfo->achName, This->filterInfo.achName);
483 pInfo->pGraph = This->filterInfo.pGraph;
486 IFilterGraph_AddRef(pInfo->pGraph);
491 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
493 ICOM_THIS(AsyncReader, iface);
495 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
498 strcpyW(This->filterInfo.achName, pName);
500 *This->filterInfo.achName = 0;
501 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
506 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
508 FIXME("(%p)\n", pVendorInfo);
513 static const IBaseFilterVtbl AsyncReader_Vtbl =
515 AsyncReader_QueryInterface,
518 AsyncReader_GetClassID,
522 AsyncReader_GetState,
523 AsyncReader_SetSyncSource,
524 AsyncReader_GetSyncSource,
525 AsyncReader_EnumPins,
527 AsyncReader_QueryFilterInfo,
528 AsyncReader_JoinFilterGraph,
529 AsyncReader_QueryVendorInfo
532 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
534 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
536 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
539 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
541 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
543 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
546 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
548 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
550 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
553 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
557 IAsyncReader * pReader = NULL;
558 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
560 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
563 /* FIXME: check the sharing values that native uses */
564 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
566 if (hFile == INVALID_HANDLE_VALUE)
568 return HRESULT_FROM_WIN32(GetLastError());
572 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
575 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
577 /* store file name & media type */
580 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
581 strcpyW(This->pszFileName, pszFileName);
582 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
585 This->pmt->bFixedSizeSamples = TRUE;
586 This->pmt->bTemporalCompression = FALSE;
587 This->pmt->cbFormat = 0;
588 This->pmt->pbFormat = NULL;
589 This->pmt->pUnk = NULL;
590 This->pmt->lSampleSize = 0;
591 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
592 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
595 CoTaskMemFree(This->pmt);
600 CopyMediaType(This->pmt, pmt);
604 IAsyncReader_Release(pReader);
608 if (This->pOutputPin)
610 IPin_Release(This->pOutputPin);
611 This->pOutputPin = NULL;
613 if (This->pszFileName)
615 CoTaskMemFree(This->pszFileName);
616 This->pszFileName = NULL;
621 /* FIXME: check return codes */
625 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
627 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
629 TRACE("(%p, %p)\n", ppszFileName, pmt);
631 /* copy file name & media type if available, otherwise clear the outputs */
632 if (This->pszFileName)
634 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
635 strcpyW(*ppszFileName, This->pszFileName);
638 *ppszFileName = NULL;
642 CopyMediaType(pmt, This->pmt);
645 ZeroMemory(pmt, sizeof(*pmt));
650 static const IFileSourceFilterVtbl FileSource_Vtbl =
652 FileSource_QueryInterface,
656 FileSource_GetCurFile
660 /* the dwUserData passed back to user */
661 typedef struct DATAREQUEST
663 IMediaSample * pSample; /* sample passed to us by user */
664 DWORD_PTR dwUserData; /* user data passed to us */
665 OVERLAPPED ovl; /* our overlapped structure */
667 struct DATAREQUEST * pNext; /* next data request in list */
670 void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
672 DATAREQUEST * pCurrent;
673 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
675 pCurrent->pNext = pItem;
678 typedef struct FileAsyncReader
681 const struct IAsyncReaderVtbl * lpVtblAR;
686 DATAREQUEST * pHead; /* head of data request list */
687 CRITICAL_SECTION csList; /* critical section to protect operations on list */
690 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
692 ICOM_THIS(AsyncReader, iface);
694 FIXME("(%p, %p)\n", iface, pmt);
696 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
697 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
698 IsEqualGUID(&pmt->formattype, &FORMAT_None))
704 /* overriden pin functions */
706 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
708 ICOM_THIS(FileAsyncReader, iface);
709 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
713 if (IsEqualIID(riid, &IID_IUnknown))
715 else if (IsEqualIID(riid, &IID_IPin))
717 else if (IsEqualIID(riid, &IID_IAsyncReader))
718 *ppv = (LPVOID)&This->lpVtblAR;
722 IUnknown_AddRef((IUnknown *)(*ppv));
726 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
728 return E_NOINTERFACE;
731 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
733 ICOM_THIS(FileAsyncReader, iface);
737 if (!InterlockedDecrement(&This->pin.pin.refCount))
739 DATAREQUEST * pCurrent;
741 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
743 pNext = pCurrent->pNext;
744 CoTaskMemFree(pCurrent);
746 CloseHandle(This->hFile);
747 CloseHandle(This->hEvent);
751 return This->pin.pin.refCount;
754 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
756 ENUMMEDIADETAILS emd;
757 ICOM_THIS(FileAsyncReader, iface);
759 TRACE("(%p)\n", ppEnum);
762 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
764 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
767 static const IPinVtbl FileAsyncReaderPin_Vtbl =
769 FileAsyncReaderPin_QueryInterface,
771 FileAsyncReaderPin_Release,
773 OutputPin_ReceiveConnection,
775 IPinImpl_ConnectedTo,
776 IPinImpl_ConnectionMediaType,
777 IPinImpl_QueryPinInfo,
778 IPinImpl_QueryDirection,
780 IPinImpl_QueryAccept,
781 FileAsyncReaderPin_EnumMediaTypes,
782 IPinImpl_QueryInternalConnections,
783 OutputPin_EndOfStream,
784 OutputPin_BeginFlush,
789 /* Function called as a helper to IPin_Connect */
790 /* specific AM_MEDIA_TYPE - it cannot be NULL */
791 /* this differs from standard OutputPin_ConnectSpecific only in that it
792 * doesn't need the IMemInputPin interface on the receiving pin */
793 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
795 ICOM_THIS(OutputPin, iface);
798 TRACE("(%p, %p)\n", pReceivePin, pmt);
799 dump_AM_MEDIA_TYPE(pmt);
801 /* FIXME: call queryacceptproc */
803 This->pin.pConnectedTo = pReceivePin;
804 IPin_AddRef(pReceivePin);
805 CopyMediaType(&This->pin.mtCurrent, pmt);
807 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
811 IPin_Release(This->pin.pConnectedTo);
812 This->pin.pConnectedTo = NULL;
813 DeleteMediaType(&This->pin.mtCurrent);
816 TRACE(" -- %lx\n", hr);
820 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
822 FileAsyncReader * pPinImpl;
827 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
830 return E_OUTOFMEMORY;
832 piOutput.dir = PINDIR_OUTPUT;
833 piOutput.pFilter = pBaseFilter;
834 strcpyW(piOutput.achName, wszOutputPinName);
836 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
838 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
839 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
840 pPinImpl->hFile = hFile;
841 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
842 pPinImpl->bFlushing = FALSE;
843 pPinImpl->pHead = NULL;
844 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
845 InitializeCriticalSection(&pPinImpl->csList);
847 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
855 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
857 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
859 return IPin_QueryInterface((IPin *)This, riid, ppv);
862 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
864 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
866 return IPin_AddRef((IPin *)This);
869 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
871 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
873 return IPin_Release((IPin *)This);
876 #define DEF_ALIGNMENT 1
878 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
882 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
884 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
885 pProps->cbAlign = DEF_ALIGNMENT;
889 ALLOCATOR_PROPERTIES PropsActual;
890 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
891 /* FIXME: check we are still aligned */
894 IMemAllocator_AddRef(pPreferred);
895 *ppActual = pPreferred;
896 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
903 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
907 ALLOCATOR_PROPERTIES PropsActual;
908 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
909 /* FIXME: check we are still aligned */
912 IMemAllocator_AddRef(pPreferred);
913 *ppActual = pPreferred;
914 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
923 IMemAllocator_Release(pPreferred);
926 TRACE("-- %lx\n", hr);
930 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
931 * however, this would be quite complicated to do and may be a bit error prone */
932 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
934 REFERENCE_TIME Start;
936 DATAREQUEST * pDataRq;
939 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
941 TRACE("(%p, %lx)\n", pSample, dwUser);
943 /* check flushing state */
945 return VFW_E_WRONG_STATE;
947 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
950 /* get start and stop positions in bytes */
952 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
955 hr = IMediaSample_GetPointer(pSample, &pBuffer);
959 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
961 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
962 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
963 pDataRq->ovl.hEvent = This->hEvent;
964 pDataRq->dwUserData = dwUser;
965 pDataRq->pNext = NULL;
966 /* we violate traditional COM rules here by maintaining
967 * a reference to the sample, but not calling AddRef, but
968 * that's what MSDN says to do */
969 pDataRq->pSample = pSample;
971 EnterCriticalSection(&This->csList);
974 /* adds data request to end of list */
975 queue(This->pHead, pDataRq);
977 This->pHead = pDataRq;
979 LeaveCriticalSection(&This->csList);
981 /* this is definitely not how it is implemented on Win9x
982 * as they do not support async reads on files, but it is
983 * sooo much easier to use this than messing around with threads!
985 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
986 hr = HRESULT_FROM_WIN32(GetLastError());
988 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
989 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
993 if (FAILED(hr) && pDataRq)
995 EnterCriticalSection(&This->csList);
997 DATAREQUEST * pCurrent;
998 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
999 if (pCurrent->pNext == pDataRq)
1001 pCurrent->pNext = pDataRq->pNext;
1005 LeaveCriticalSection(&This->csList);
1006 CoTaskMemFree(pDataRq);
1009 TRACE("-- %lx\n", hr);
1013 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1016 DATAREQUEST * pDataRq = NULL;
1017 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1019 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1021 /* FIXME: we could do with improving this by waiting for an array of event handles
1022 * and then determining which one finished and removing that from the list, otherwise
1023 * we will end up waiting for longer than we should do, if a later request finishes
1024 * before an earlier one */
1029 /* we return immediately if flushing */
1030 if (This->bFlushing)
1031 hr = VFW_E_WRONG_STATE;
1035 /* wait for the read to finish or timeout */
1036 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1041 EnterCriticalSection(&This->csList);
1043 pDataRq = This->pHead;
1044 if (pDataRq == NULL)
1047 This->pHead = pDataRq->pNext;
1049 LeaveCriticalSection(&This->csList);
1055 /* get any errors */
1056 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1057 hr = HRESULT_FROM_WIN32(GetLastError());
1062 *ppSample = pDataRq->pSample;
1063 *pdwUser = pDataRq->dwUserData;
1069 /* no need to close event handle since we will close it when the pin is destroyed */
1070 CoTaskMemFree(pDataRq);
1073 TRACE("-- %lx\n", hr);
1077 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1079 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1082 REFERENCE_TIME tStart;
1083 REFERENCE_TIME tStop;
1086 TRACE("(%p)\n", pSample);
1088 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1091 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1094 hr = FileAsyncReader_SyncRead(iface,
1095 BYTES_FROM_MEDIATIME(tStart),
1096 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1099 TRACE("-- %lx\n", hr);
1103 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1107 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1109 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1111 ZeroMemory(&ovl, sizeof(ovl));
1113 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1114 /* NOTE: llPosition is the actual byte position to start reading from */
1115 ovl.Offset = (DWORD) llPosition;
1116 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1118 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1119 hr = HRESULT_FROM_WIN32(GetLastError());
1121 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1128 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1129 hr = HRESULT_FROM_WIN32(GetLastError());
1132 CloseHandle(ovl.hEvent);
1134 TRACE("-- %lx\n", hr);
1138 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1142 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1144 TRACE("(%p, %p)\n", pTotal, pAvailable);
1146 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1147 (GetLastError() != NO_ERROR))
1148 return HRESULT_FROM_WIN32(GetLastError());
1150 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1152 *pAvailable = *pTotal;
1157 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1159 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1163 This->bFlushing = TRUE;
1164 CancelIo(This->hFile);
1165 SetEvent(This->hEvent);
1167 /* FIXME: free list */
1172 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1174 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1178 This->bFlushing = FALSE;
1183 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1185 FileAsyncReader_QueryInterface,
1186 FileAsyncReader_AddRef,
1187 FileAsyncReader_Release,
1188 FileAsyncReader_RequestAllocator,
1189 FileAsyncReader_Request,
1190 FileAsyncReader_WaitForNext,
1191 FileAsyncReader_SyncReadAligned,
1192 FileAsyncReader_SyncRead,
1193 FileAsyncReader_Length,
1194 FileAsyncReader_BeginFlush,
1195 FileAsyncReader_EndFlush,